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