50127a0691342fd21456e9f03609178c4b44f4d3
[claws.git] / src / gtk / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 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                 g_object_unref(G_OBJECT(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
128 #if GLIB_CHECK_VERSION(2,16,0)
129         gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser), FALSE);
130 #endif
131
132         if (filter != NULL) {
133                 GtkFileFilter *file_filter = gtk_file_filter_new();
134                 gtk_file_filter_add_pattern(file_filter, filter);
135                 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
136                                             file_filter);
137         }
138
139         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
140                 GtkImage *preview;
141                 preview = GTK_IMAGE(gtk_image_new ());
142                 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
143                 g_signal_connect (chooser, "update-preview",
144                             G_CALLBACK (update_preview_cb), preview);
145
146         }
147
148         if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
149                 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
150         }
151
152         manage_window_set_transient (GTK_WINDOW(chooser));
153         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
154
155         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
156
157         if (path && strlen(path) > 0) {
158                 char *filename = NULL;
159                 char *realpath = g_strdup(path);
160                 char *tmp = NULL;
161                 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
162                         filename = "";
163                 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
164                         filename++;
165                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
166                 } else {
167                         filename = (char *) path;
168                         g_free(realpath); 
169                         realpath = g_strdup(get_home_dir());
170                 }
171                 tmp = NULL;
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                         tmp = NULL;
180                         if (g_utf8_validate(filename, -1, NULL))
181                                 tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
182                         if (tmp == NULL)
183                                 tmp = g_strdup(filename);
184                         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), tmp);
185                         g_free(tmp);
186                 }
187                 g_free(realpath);
188         } else {
189                 gchar *tmp = NULL;
190                 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
191                         prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
192                 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
193                         tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
194                 if (tmp == NULL)
195                         tmp = g_strdup(prefs_common.attach_load_dir);
196                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
197                 g_free(tmp);
198         }
199
200 #ifdef MAEMO
201         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK 
202                 || gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
203 #else
204         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) 
205 #endif
206                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
207         
208         manage_window_focus_out(chooser, NULL, NULL);
209         gtk_widget_destroy (chooser);
210
211         slist_orig = slist;
212         
213         if (slist) {
214                 gchar *tmp = g_strdup(slist->data);
215
216                 if (!path && prefs_common.attach_load_dir)
217                         g_free(prefs_common.attach_load_dir);
218                 
219                 if (strrchr(tmp, G_DIR_SEPARATOR))
220                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
221
222                 if (!path)
223                         prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
224
225                 g_free(tmp);
226         }
227
228         while (slist) {
229                 list = g_list_append(list, slist->data);
230                 slist = slist->next;
231         }
232         
233         if (slist_orig)
234                 g_slist_free(slist_orig);
235         
236         return list;
237 }
238
239 /**
240  * This function lets the user select multiple files.
241  * This opens an Open type dialog.
242  * @param title the title of the dialog
243  */
244 GList *filesel_select_multiple_files_open(const gchar *title)
245 {
246         return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
247 }
248
249 GList *filesel_select_multiple_files_open_with_filter(  const gchar *title,
250                                                         const gchar *path,
251                                                         const gchar *filter)
252 {
253         return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
254 }
255
256 /**
257  * This function lets the user select one file.
258  * This opens an Open type dialog if "file" is NULL, 
259  * Save dialog if "file" contains a path.
260  * @param title the title of the dialog
261  * @param path the optional path to save to
262  */
263 static gchar *filesel_select_file(const gchar *title, const gchar *path,
264                                   gboolean open, gboolean folder_mode,
265                                   const gchar *filter)
266 {
267         GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
268         gchar * result = NULL;
269         if (list) {
270                 result = g_strdup(list->data);
271         }
272         g_list_free(list);
273         return result;
274 }
275 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
276 {
277         return filesel_select_file (title, path, TRUE, FALSE, NULL);
278 }
279
280 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
281                                             const gchar *filter)
282 {
283         return filesel_select_file (title, path, TRUE, FALSE, filter);
284 }
285
286 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
287 {
288         return filesel_select_file (title, path, FALSE, FALSE, NULL);
289 }
290
291 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
292 {
293         return filesel_select_file (title, path, TRUE, TRUE, NULL);
294 }
295
296 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
297 {
298         return filesel_select_file (title, path, FALSE, TRUE, NULL);
299 }
300