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