2007-10-18 [mones] 3.0.2cvs86
[claws.git] / src / gtk / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif 
23
24 #include <glib.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkfilesel.h>
28 #include <gtk/gtkentry.h>
29 #include <gtk/gtkmain.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkeditable.h>
32 #include <gtk/gtkstock.h>
33 #include <gtk/gtkdialog.h>
34 #include <gtk/gtkfilechooser.h>
35 #include <gtk/gtkfilechooserdialog.h>
36
37 #ifdef MAEMO
38 #include <hildon-widgets/hildon-file-chooser-dialog.h>
39 #endif
40
41 #include "claws.h"
42 #include "filesel.h"
43 #include "manage_window.h"
44 #include "gtkutils.h"
45 #include "utils.h"
46 #include "codeconv.h"
47 #include "procmime.h"
48 #include "prefs_common.h"
49
50 static void
51 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
52 {
53         GtkWidget *preview;
54         char *filename = NULL, *type = NULL;
55         GdkPixbuf *pixbuf = NULL;
56         gboolean have_preview = FALSE;
57
58         preview = GTK_WIDGET (data);
59         filename = gtk_file_chooser_get_preview_filename (file_chooser);
60
61         if (filename == NULL) {
62                 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
63                 return;
64         }
65         type = procmime_get_mime_type(filename);
66         
67         if (type && !strncmp(type, "image/", 6)) 
68                 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
69         
70         g_free(type);
71         g_free (filename);
72
73         if (pixbuf) {
74                 have_preview = TRUE;
75                 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
76                 gdk_pixbuf_unref (pixbuf);
77         }
78
79         gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
80 }
81         
82 static GList *filesel_create(const gchar *title, const gchar *path,
83                              gboolean multiple_files,
84                              gboolean open, gboolean folder_mode,
85                              const gchar *filter)
86 {
87         GSList *slist = NULL, *slist_orig = NULL;
88         GList *list = NULL;
89
90         gint action = (open == TRUE) ? 
91                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
92                                                GTK_FILE_CHOOSER_ACTION_OPEN):
93                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
94                                                GTK_FILE_CHOOSER_ACTION_SAVE);
95                         
96         gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
97 #ifdef MAEMO
98         GtkWidget *chooser;
99         if( path && strcmp(path, get_plugin_dir()) == 0 ) {
100                 chooser = gtk_file_chooser_dialog_new_with_backend
101                                         (title, NULL, action, "gtk+",
102                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
103                                         action_btn, GTK_RESPONSE_ACCEPT, 
104                                         NULL);
105         }
106         else {
107                 chooser = hildon_file_chooser_dialog_new (NULL, action);
108         }
109 #else
110         GtkWidget *chooser = gtk_file_chooser_dialog_new_with_backend
111                                 (title, NULL, action, "gtk+",
112                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
113                                 action_btn, GTK_RESPONSE_ACCEPT, 
114                                 NULL);
115 #endif
116         if (filter != NULL) {
117                 GtkFileFilter *file_filter = gtk_file_filter_new();
118                 gtk_file_filter_add_pattern(file_filter, filter);
119                 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
120                                             file_filter);
121         }
122
123         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
124                 GtkImage *preview;
125                 preview = GTK_IMAGE(gtk_image_new ());
126                 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
127                 g_signal_connect (chooser, "update-preview",
128                             G_CALLBACK (update_preview_cb), preview);
129
130         }
131
132         if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
133                 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
134         }
135
136         manage_window_set_transient (GTK_WINDOW(chooser));
137         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
138
139         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
140
141         if (path && strlen(path) > 0) {
142                 char *filename = NULL;
143                 char *realpath = g_strdup(path);
144                 char *tmp = NULL;
145                 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
146                         filename = "";
147                 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
148                         filename++;
149                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
150                 } else {
151                         filename = (char *) path;
152                         g_free(realpath); 
153                         realpath = g_strdup(get_home_dir());
154                 }
155                 tmp = NULL;
156                 if (g_utf8_validate(realpath, -1, NULL))
157                         tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
158                 if (tmp == NULL)
159                         tmp = g_strdup(realpath);
160                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
161                 g_free(tmp);
162                 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
163                         tmp = NULL;
164                         if (g_utf8_validate(filename, -1, NULL))
165                                 tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
166                         if (tmp == NULL)
167                                 tmp = g_strdup(filename);
168                         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), tmp);
169                         g_free(tmp);
170                 }
171                 g_free(realpath);
172         } else {
173                 gchar *tmp = NULL;
174                 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
175                         prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
176                 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
177                         tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
178                 if (tmp == NULL)
179                         tmp = g_strdup(prefs_common.attach_load_dir);
180                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
181                 g_free(tmp);
182         }
183
184 #ifdef MAEMO
185         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK 
186                 || gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
187 #else
188         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) 
189 #endif
190                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
191         
192         manage_window_focus_out(chooser, NULL, NULL);
193         gtk_widget_destroy (chooser);
194
195         slist_orig = slist;
196         
197         if (slist) {
198                 gchar *tmp = g_strdup(slist->data);
199
200                 if (!path && prefs_common.attach_load_dir)
201                         g_free(prefs_common.attach_load_dir);
202                 
203                 if (strrchr(tmp, G_DIR_SEPARATOR))
204                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
205
206                 if (!path)
207                         prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
208
209                 g_free(tmp);
210         }
211
212         while (slist) {
213                 list = g_list_append(list, slist->data);
214                 slist = slist->next;
215         }
216         
217         if (slist_orig)
218                 g_slist_free(slist_orig);
219         
220         return list;
221 }
222
223 /**
224  * This function lets the user select multiple files.
225  * This opens an Open type dialog.
226  * @param title the title of the dialog
227  */
228 GList *filesel_select_multiple_files_open(const gchar *title)
229 {
230         return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
231 }
232
233 GList *filesel_select_multiple_files_open_with_filter(  const gchar *title,
234                                                         const gchar *path,
235                                                         const gchar *filter)
236 {
237         return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
238 }
239
240 /**
241  * This function lets the user select one file.
242  * This opens an Open type dialog if "file" is NULL, 
243  * Save dialog if "file" contains a path.
244  * @param title the title of the dialog
245  * @param path the optional path to save to
246  */
247 static gchar *filesel_select_file(const gchar *title, const gchar *path,
248                                   gboolean open, gboolean folder_mode,
249                                   const gchar *filter)
250 {
251         GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
252         gchar * result = NULL;
253         if (list) {
254                 result = g_strdup(list->data);
255         }
256         g_list_free(list);
257         return result;
258 }
259 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
260 {
261         return filesel_select_file (title, path, TRUE, FALSE, NULL);
262 }
263
264 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
265                                             const gchar *filter)
266 {
267         return filesel_select_file (title, path, TRUE, FALSE, filter);
268 }
269
270 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
271 {
272         return filesel_select_file (title, path, FALSE, FALSE, NULL);
273 }
274
275 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
276 {
277         return filesel_select_file (title, path, TRUE, TRUE, NULL);
278 }
279
280 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
281 {
282         return filesel_select_file (title, path, FALSE, TRUE, NULL);
283 }
284