1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * gcugtk/filechooser.cc
6  *
7  * Copyright (C) 2006-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 "filechooser.h"
27 #include <gcugtk/application.h>
28 #include <gcu/document.h>
29 #include <glib/gi18n-lib.h>
30 
31 using namespace std;
32 
33 namespace gcugtk
34 {
35 
FileChooser(Application * App,bool Save,list<string> mime_types,gcu::Document * pDoc,char const * title,GtkWidget * extra_widget)36 FileChooser::FileChooser (Application *App, bool Save, list<string> mime_types, gcu::Document *pDoc, char const *title, GtkWidget *extra_widget)
37 {
38 	char* filename = NULL;
39 	m_pDoc = pDoc;
40 	dialog = (GtkFileChooser*) gtk_file_chooser_dialog_new (
41 															(title != NULL)? title: ((Save)? _("Save as"): _("Open")),
42 															App->GetWindow(),
43 															(Save)? GTK_FILE_CHOOSER_ACTION_SAVE: GTK_FILE_CHOOSER_ACTION_OPEN,
44 															(Save)? GTK_STOCK_SAVE: GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
45 															GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
46 															NULL);
47 	if (extra_widget)
48 		gtk_file_chooser_set_extra_widget (dialog, extra_widget);
49 	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
50 	GtkFileChooser* chooser = GTK_FILE_CHOOSER (dialog);
51 	GtkFileFilter* filter = gtk_file_filter_new ();
52 	list<string>::iterator i, iend = mime_types.end ();
53 	for (i = mime_types.begin (); i != iend; i++)
54 		gtk_file_filter_add_mime_type (filter, (*i).c_str ());
55 	GtkComboBoxText *format_combo = NULL;
56 	if (!Save)
57 		gtk_file_chooser_set_select_multiple (chooser, true);
58 	if (mime_types.size () > 1) {
59 		GtkWidget *grid = gtk_grid_new ();
60 		if (gtk_check_version (3, 2, 0))
61 			gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
62 		else
63 			gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
64 		GtkWidget *label = gtk_label_new_with_mnemonic (_("File _type:"));
65 		format_combo = GTK_COMBO_BOX_TEXT (gtk_combo_box_text_new ());
66 		gtk_combo_box_text_append_text (format_combo, _("Automatic"));
67 		for (i = mime_types.begin (); i != iend; i++) {
68 			char *type = go_mime_type_get_description ((*i).c_str ());
69 			if (type) {
70 				gtk_combo_box_text_append_text (format_combo, type);
71 				g_free (type);
72 			} else
73 				gtk_combo_box_text_append_text (format_combo, (*i).c_str ());
74 		}
75 		gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
76 
77 		gtk_container_add (GTK_CONTAINER (grid), label);
78 		gtk_container_add (GTK_CONTAINER (grid), GTK_WIDGET (format_combo));
79 		gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (format_combo));
80 
81 		if (extra_widget) {
82 			if (GTK_IS_CONTAINER (extra_widget))
83 				gtk_container_add (GTK_CONTAINER (extra_widget), grid);
84 			else
85 				g_warning ("not implemented, please file a bug report");
86 		} else
87 			gtk_file_chooser_set_extra_widget (dialog, grid);
88 		gtk_widget_show_all (grid);
89 	}
90 	gtk_file_chooser_set_filter (chooser, filter);
91 	// Now add network directories
92 	gtk_file_chooser_set_local_only (chooser, false);
93 	char const* dir = App->GetCurDir ();
94 	if (dir)
95 		gtk_file_chooser_set_current_folder_uri (chooser, dir);
96 	while (gtk_widget_show_all (GTK_WIDGET (dialog)), gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
97 		// find the mime_type
98 		string mime_type;
99 		if (mime_types.size () == 1)
100 			mime_type = mime_types.front ();
101 		else if (mime_types.size () > 0) {
102 			int j = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
103 			if (j > 0) {
104 				i = mime_types.begin ();
105 				while (--j > 0)
106 					i++;
107 				mime_type = *i;
108 			}
109 		}
110 		if (Save) {
111 			filename = gtk_file_chooser_get_uri (chooser);
112 			if (mime_type.length () == 0) {
113 				char *mime = go_get_mime_type (filename);
114 				// ensure the found mime type is in the list
115 				bool found = false;
116 				if (mime) {
117 					list<string>::iterator it, itend = mime_types.end ();
118 					for (it = mime_types.begin (); it != itend; it++)
119 						if (*it == mime) {
120 							mime_type = *it;
121 							found = true;
122 							break;
123 						}
124 				}
125 				g_free (mime);
126 				if (!found)
127 					mime_type = mime_types.front ();
128 			}
129 			gtk_widget_hide (GTK_WIDGET (dialog));
130 			if (!App->FileProcess (filename, mime_type.c_str (), Save, GTK_WINDOW (dialog), m_pDoc)) {
131 				g_free (filename);
132 				break;
133 			}
134 			g_free (filename);
135 		} else {
136 			GSList* files = gtk_file_chooser_get_uris (chooser);
137 			GSList* iter = files;
138 			gtk_widget_hide (GTK_WIDGET (dialog));
139 			while (iter) {
140 				filename = (char*) iter->data;
141 				if (!mime_type.length ()) {
142 					char *mime = go_get_mime_type (filename);
143 					if (mime)
144 						mime_type = mime;
145 					g_free (mime);
146 				}
147 				App->FileProcess(filename, mime_type.c_str (), Save, GTK_WINDOW (dialog), m_pDoc);
148 				g_free (filename);
149 				iter = iter->next;
150 			}
151 			g_slist_free (files);
152 			break;
153 		}
154 	}
155 	gtk_widget_destroy (GTK_WIDGET (dialog));
156 }
157 
158 }	//	namespace gcu
159