1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * programs/spectra/application.cc
6  *
7  * Copyright (C) 2007-2011 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 #include "config.h"
26 #include "application.h"
27 #include "document.h"
28 #include "view.h"
29 #include "window.h"
30 #include <gcugtk/filechooser.h>
31 #include <gcugtk/message.h>
32 #include <gcu/loader.h>
33 #include <glib/gi18n.h>
34 #include <clocale>
35 #include <map>
36 
37 using namespace gcu;
38 using namespace std;
39 
gsvApplication()40 gsvApplication::gsvApplication (): gcugtk::Application (_("GSpectrum"), DATADIR, "gspectrum")
41 {
42 	// First, initialize plugins manager
43 	go_plugins_init (NULL, NULL, NULL, NULL, TRUE, GO_TYPE_PLUGIN_LOADER_MODULE);
44 	gcu::Loader::Init (this);
45 	m_SupportedMimeTypes.push_back ("chemical/x-jcamp-dx");
46 	map<string, LoaderStruct>::iterator it;
47 	bool found = Loader::GetFirstLoader (it);
48 	while (found) {
49 		if ((*it).second.supportsSpectra) {
50 			if ((*it).second.read)
51 				AddMimeType (m_SupportedMimeTypes, (*it).first);
52 			if ((*it).second.write)
53 				AddMimeType (m_WriteableMimeTypes, (*it).first);
54 		}
55 		found = Loader::GetNextLoader (it);
56 	}
57 	SetImageWidth (600);
58 }
59 
~gsvApplication()60 gsvApplication::~gsvApplication ()
61 {
62 }
63 
OnFileNew()64 gsvDocument *gsvApplication::OnFileNew ()
65 {
66 	gsvDocument* Doc = new gsvDocument (this);
67 	Doc->SetTitle (_("GSpectrum"));
68 	new gsvWindow (this, Doc);
69 	return Doc;
70 }
71 
OnFileOpen(gsvDocument * Doc)72 void gsvApplication::OnFileOpen (gsvDocument *Doc)
73 {
74 	gcugtk::FileChooser (this, false, m_SupportedMimeTypes, Doc);
75 }
76 
OnQuit()77 void gsvApplication::OnQuit ()
78 {
79 	gsvDocument *Doc;
80 	while (m_Docs.size () > 0) {
81 		Doc = dynamic_cast <gsvDocument *> (*m_Docs.begin ());
82 		dynamic_cast <gsvView *> (Doc->GetView ())->GetWindow ()->OnFileClose ();
83 	}
84 }
85 
FileProcess(const gchar * filename,const gchar * mime_type,bool bSave,GtkWindow * window,Document * Doc)86 bool gsvApplication::FileProcess (const gchar* filename, const gchar* mime_type, bool bSave, GtkWindow *window, Document *Doc)
87 {
88 	gsvDocument *pDoc = dynamic_cast <gsvDocument *> (Doc);
89 	if(bSave) {
90 		GFile *file = g_file_new_for_uri (filename);
91 		bool err = g_file_query_exists (file, NULL);
92 		gint result = GTK_RESPONSE_YES;
93 		if (err) {
94 			char *unescaped = g_uri_unescape_string (filename, NULL);
95 			gchar * message = g_strdup_printf (_("File %s\nexists, overwrite?"), unescaped);
96 			g_free (unescaped);
97 			gcugtk::Message *box = new gcugtk::Message (this, message, GTK_MESSAGE_QUESTION,
98 			                                            GTK_BUTTONS_YES_NO, window);
99 			result = box->Run ();
100 			g_free (message);
101 		}
102 		if (result == GTK_RESPONSE_YES) {
103 			g_file_delete (file, NULL, NULL);
104 			dynamic_cast <gsvView *> (pDoc->GetView ())->SaveAsImage (filename, mime_type, GetImageWidth (), GetImageHeight ());
105 		}
106 		g_object_unref (file);
107 	} else {
108 		if (pDoc && !pDoc->GetEmpty ())
109 			pDoc = NULL;
110 		if (!pDoc)
111 			pDoc = OnFileNew ();
112 		if (!strcmp (mime_type, "chemical/x-jcamp-dx"))
113 			pDoc->Load (filename, mime_type);
114 		else {
115 			ContentType ctype = Load (filename, mime_type, pDoc);
116 			if (ctype != gcu::ContentTypeSpectrum)
117 				return false;
118 		}
119 		GtkRecentData data;
120 		data.display_name = g_strdup (pDoc->GetTitle ().c_str ());
121 		if (*data.display_name == 0) {
122 			g_free (data.display_name);
123 			char *title = g_path_get_basename (filename);
124 			char *buf = g_uri_unescape_string (title, NULL);
125 			g_free (title);
126 			data.display_name = buf;
127 			pDoc->SetTitle (data.display_name);
128 		}
129 		char *dirname = g_path_get_dirname (filename);
130 		SetCurDir (dirname);
131 		g_free (dirname);
132 		data.description = NULL;
133 		data.mime_type = (char*) mime_type;
134 		data.app_name = const_cast<char*> ("gspectrum");
135 		data.app_exec = const_cast<char*> ("gspectrum %u");
136 		data.groups = NULL;
137 		data.is_private =  FALSE;
138 		gtk_recent_manager_add_full (GetRecentManager (), filename, &data);
139 		g_free (data.display_name);
140 	}
141 	return false;
142 }
143 
OnSaveAsImage(gsvDocument * Doc)144 void gsvApplication::OnSaveAsImage (gsvDocument *Doc)
145 {
146 	if (!Doc)
147 		return;
148 	list<string> l;
149 	char const *mime;
150 	map<string, GdkPixbufFormat*>::iterator i, end = m_SupportedPixbufFormats.end ();
151 	for (i = m_SupportedPixbufFormats.begin (); i != end; i++)
152 		l.push_front ((*i).first.c_str ());
153 	if (go_image_get_format_from_name ("eps") != GO_IMAGE_FORMAT_UNKNOWN) {
154 		mime = go_image_format_to_mime ("eps");
155 		if (mime)
156 			l.push_front (mime);
157 	}
158 	l.push_front ("application/postscript");
159 	l.push_front ("application/pdf");
160 	l.push_front ("image/svg+xml");
161 	gcugtk::FileChooser (this, true, l, Doc, _("Save as image"), GetImageSizeWidget ());
162 }
163 
AddMimeType(list<string> & l,string const & mime_type)164 void gsvApplication::AddMimeType (list<string> &l, string const& mime_type)
165 {
166 	list<string>::iterator i, iend = l.end ();
167 	for (i = l.begin (); i != iend; i++)
168 		if (*i == mime_type)
169 			break;
170 	if (i == iend)
171 		l.push_back (mime_type);
172 	else
173 		g_warning ("Duplicate mime type: %s", mime_type.c_str ());
174 }
175