2007-04-18 [paul] 2.9.0cvs7
[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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 #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                 gdk_pixbuf_unref (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         GtkWidget *chooser = gtk_file_chooser_dialog_new_with_backend
94                                 (title, NULL, action, "gtk+",
95                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
96                                 action_btn, GTK_RESPONSE_ACCEPT, 
97                                 NULL);
98         if (filter != NULL) {
99                 GtkFileFilter *file_filter = gtk_file_filter_new();
100                 gtk_file_filter_add_pattern(file_filter, filter);
101                 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
102                                             file_filter);
103         }
104
105         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
106                 GtkImage *preview;
107                 preview = GTK_IMAGE(gtk_image_new ());
108                 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
109                 g_signal_connect (chooser, "update-preview",
110                             G_CALLBACK (update_preview_cb), preview);
111
112         }
113
114         if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
115                 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
116         }
117
118         manage_window_set_transient (GTK_WINDOW(chooser));
119         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
120
121         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
122
123         if (path && strlen(path) > 0) {
124                 char *filename = NULL;
125                 char *realpath = g_strdup(path);
126                 char *tmp = NULL;
127                 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
128                         filename = "";
129                 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
130                         filename++;
131                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
132                 } else {
133                         filename = (char *) path;
134                         g_free(realpath); 
135                         realpath = g_strdup(get_home_dir());
136                 }
137                 tmp = NULL;
138                 if (g_utf8_validate(realpath, -1, NULL))
139                         tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
140                 if (tmp == NULL)
141                         tmp = g_strdup(realpath);
142                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
143                 g_free(tmp);
144                 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
145                         tmp = NULL;
146                         if (g_utf8_validate(filename, -1, NULL))
147                                 tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
148                         if (tmp == NULL)
149                                 tmp = g_strdup(filename);
150                         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), tmp);
151                         g_free(tmp);
152                 }
153                 g_free(realpath);
154         } else {
155                 gchar *tmp = NULL;
156                 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
157                         prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
158                 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
159                         tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
160                 if (tmp == NULL)
161                         tmp = g_strdup(prefs_common.attach_load_dir);
162                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
163                 g_free(tmp);
164         }
165
166         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) 
167                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
168         
169         manage_window_focus_out(chooser, NULL, NULL);
170         gtk_widget_destroy (chooser);
171
172         slist_orig = slist;
173         
174         if (slist) {
175                 gchar *tmp = g_strdup(slist->data);
176
177                 if (!path && prefs_common.attach_load_dir)
178                         g_free(prefs_common.attach_load_dir);
179                 
180                 if (strrchr(tmp, G_DIR_SEPARATOR))
181                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
182
183                 if (!path)
184                         prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
185
186                 g_free(tmp);
187         }
188
189         while (slist) {
190                 list = g_list_append(list, slist->data);
191                 slist = slist->next;
192         }
193         
194         if (slist_orig)
195                 g_slist_free(slist_orig);
196         
197         return list;
198 }
199
200 /**
201  * This function lets the user select multiple files.
202  * This opens an Open type dialog.
203  * @param title the title of the dialog
204  */
205 GList *filesel_select_multiple_files_open(const gchar *title)
206 {
207         return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
208 }
209
210 GList *filesel_select_multiple_files_open_with_filter(  const gchar *title,
211                                                         const gchar *path,
212                                                         const gchar *filter)
213 {
214         return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
215 }
216
217 /**
218  * This function lets the user select one file.
219  * This opens an Open type dialog if "file" is NULL, 
220  * Save dialog if "file" contains a path.
221  * @param title the title of the dialog
222  * @param path the optional path to save to
223  */
224 static gchar *filesel_select_file(const gchar *title, const gchar *path,
225                                   gboolean open, gboolean folder_mode,
226                                   const gchar *filter)
227 {
228         GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
229         gchar * result = NULL;
230         if (list) {
231                 result = g_strdup(list->data);
232         }
233         g_list_free(list);
234         return result;
235 }
236 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
237 {
238         return filesel_select_file (title, path, TRUE, FALSE, NULL);
239 }
240
241 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
242                                             const gchar *filter)
243 {
244         return filesel_select_file (title, path, TRUE, FALSE, filter);
245 }
246
247 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
248 {
249         return filesel_select_file (title, path, FALSE, FALSE, NULL);
250 }
251
252 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
253 {
254         return filesel_select_file (title, path, TRUE, TRUE, NULL);
255 }
256
257 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
258 {
259         return filesel_select_file (title, path, FALSE, TRUE, NULL);
260 }
261