2008-09-12 [colin] 3.5.0cvs103
[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/gtk.h>
27
28 #ifdef MAEMO
29 #ifdef CHINOOK
30 #include <hildon/hildon-file-chooser-dialog.h>
31 #else
32 #include <hildon-widgets/hildon-file-chooser-dialog.h>
33 #endif
34 #endif
35
36 #include "claws.h"
37 #include "filesel.h"
38 #include "manage_window.h"
39 #include "gtkutils.h"
40 #include "utils.h"
41 #include "codeconv.h"
42 #include "procmime.h"
43 #include "prefs_common.h"
44
45 static void
46 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
47 {
48         GtkWidget *preview;
49         char *filename = NULL, *type = NULL;
50         GdkPixbuf *pixbuf = NULL;
51         gboolean have_preview = FALSE;
52
53         preview = GTK_WIDGET (data);
54         filename = gtk_file_chooser_get_preview_filename (file_chooser);
55
56         if (filename == NULL) {
57                 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
58                 return;
59         }
60         type = procmime_get_mime_type(filename);
61         
62         if (type && !strncmp(type, "image/", 6)) 
63                 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
64         
65         g_free(type);
66         g_free (filename);
67
68         if (pixbuf) {
69                 have_preview = TRUE;
70                 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
71                 gdk_pixbuf_unref (pixbuf);
72         }
73
74         gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
75 }
76         
77 static GList *filesel_create(const gchar *title, const gchar *path,
78                              gboolean multiple_files,
79                              gboolean open, gboolean folder_mode,
80                              const gchar *filter)
81 {
82         GSList *slist = NULL, *slist_orig = NULL;
83         GList *list = NULL;
84
85         gint action = (open == TRUE) ? 
86                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
87                                                GTK_FILE_CHOOSER_ACTION_OPEN):
88                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
89                                                GTK_FILE_CHOOSER_ACTION_SAVE);
90                         
91         gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
92 #ifdef MAEMO
93         GtkWidget *chooser;
94         if( path && strcmp(path, get_plugin_dir()) == 0 ) {
95 #if !GTK_CHECK_VERSION(2,14,0)
96                 chooser = gtk_file_chooser_dialog_new_with_backend
97                                         (title, NULL, action, "gtk+",
98                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
99                                         action_btn, GTK_RESPONSE_ACCEPT, 
100                                         NULL);
101 #else
102                 chooser = gtk_file_chooser_dialog_new
103                                         (title, NULL, action,
104                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
105                                         action_btn, GTK_RESPONSE_ACCEPT, 
106                                         NULL);
107 #endif
108         }
109         else {
110                 chooser = hildon_file_chooser_dialog_new (NULL, action);
111         }
112 #else
113 #if !GTK_CHECK_VERSION(2,14,0)
114         GtkWidget *chooser = gtk_file_chooser_dialog_new_with_backend
115                                 (title, NULL, action, "gtk+",
116                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
117                                 action_btn, GTK_RESPONSE_ACCEPT, 
118                                 NULL);
119 #else
120         GtkWidget *chooser = gtk_file_chooser_dialog_new
121                                 (title, NULL, action, 
122                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
123                                 action_btn, GTK_RESPONSE_ACCEPT, 
124                                 NULL);
125 #endif
126 #endif
127         if (filter != NULL) {
128                 GtkFileFilter *file_filter = gtk_file_filter_new();
129                 gtk_file_filter_add_pattern(file_filter, filter);
130                 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
131                                             file_filter);
132         }
133
134         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
135                 GtkImage *preview;
136                 preview = GTK_IMAGE(gtk_image_new ());
137                 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
138                 g_signal_connect (chooser, "update-preview",
139                             G_CALLBACK (update_preview_cb), preview);
140
141         }
142
143         if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
144                 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
145         }
146
147         manage_window_set_transient (GTK_WINDOW(chooser));
148         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
149
150         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
151
152         if (path && strlen(path) > 0) {
153                 char *filename = NULL;
154                 char *realpath = g_strdup(path);
155                 char *tmp = NULL;
156                 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
157                         filename = "";
158                 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
159                         filename++;
160                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
161                 } else {
162                         filename = (char *) path;
163                         g_free(realpath); 
164                         realpath = g_strdup(get_home_dir());
165                 }
166                 tmp = NULL;
167                 if (g_utf8_validate(realpath, -1, NULL))
168                         tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
169                 if (tmp == NULL)
170                         tmp = g_strdup(realpath);
171                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
172                 g_free(tmp);
173                 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
174                         tmp = NULL;
175                         if (g_utf8_validate(filename, -1, NULL))
176                                 tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
177                         if (tmp == NULL)
178                                 tmp = g_strdup(filename);
179                         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), tmp);
180                         g_free(tmp);
181                 }
182                 g_free(realpath);
183         } else {
184                 gchar *tmp = NULL;
185                 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
186                         prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
187                 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
188                         tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
189                 if (tmp == NULL)
190                         tmp = g_strdup(prefs_common.attach_load_dir);
191                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
192                 g_free(tmp);
193         }
194
195 #ifdef MAEMO
196         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK 
197                 || gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
198 #else
199         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) 
200 #endif
201                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
202         
203         manage_window_focus_out(chooser, NULL, NULL);
204         gtk_widget_destroy (chooser);
205
206         slist_orig = slist;
207         
208         if (slist) {
209                 gchar *tmp = g_strdup(slist->data);
210
211                 if (!path && prefs_common.attach_load_dir)
212                         g_free(prefs_common.attach_load_dir);
213                 
214                 if (strrchr(tmp, G_DIR_SEPARATOR))
215                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
216
217                 if (!path)
218                         prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
219
220                 g_free(tmp);
221         }
222
223         while (slist) {
224                 list = g_list_append(list, slist->data);
225                 slist = slist->next;
226         }
227         
228         if (slist_orig)
229                 g_slist_free(slist_orig);
230         
231         return list;
232 }
233
234 /**
235  * This function lets the user select multiple files.
236  * This opens an Open type dialog.
237  * @param title the title of the dialog
238  */
239 GList *filesel_select_multiple_files_open(const gchar *title)
240 {
241         return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
242 }
243
244 GList *filesel_select_multiple_files_open_with_filter(  const gchar *title,
245                                                         const gchar *path,
246                                                         const gchar *filter)
247 {
248         return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
249 }
250
251 /**
252  * This function lets the user select one file.
253  * This opens an Open type dialog if "file" is NULL, 
254  * Save dialog if "file" contains a path.
255  * @param title the title of the dialog
256  * @param path the optional path to save to
257  */
258 static gchar *filesel_select_file(const gchar *title, const gchar *path,
259                                   gboolean open, gboolean folder_mode,
260                                   const gchar *filter)
261 {
262         GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
263         gchar * result = NULL;
264         if (list) {
265                 result = g_strdup(list->data);
266         }
267         g_list_free(list);
268         return result;
269 }
270 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
271 {
272         return filesel_select_file (title, path, TRUE, FALSE, NULL);
273 }
274
275 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
276                                             const gchar *filter)
277 {
278         return filesel_select_file (title, path, TRUE, FALSE, filter);
279 }
280
281 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
282 {
283         return filesel_select_file (title, path, FALSE, FALSE, NULL);
284 }
285
286 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
287 {
288         return filesel_select_file (title, path, TRUE, TRUE, NULL);
289 }
290
291 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
292 {
293         return filesel_select_file (title, path, FALSE, TRUE, NULL);
294 }
295