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