2006-02-09 [paul] 2.0.0cvs37
[claws.git] / src / gtk / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 "sylpheed.h"
38 #include "filesel.h"
39 #include "manage_window.h"
40 #include "gtkutils.h"
41 #include "utils.h"
42 #include "codeconv.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;
50         GdkPixbuf *pixbuf;
51         gboolean have_preview;
52
53         preview = GTK_WIDGET (data);
54         filename = gtk_file_chooser_get_preview_filename (file_chooser);
55
56         if (filename == NULL)
57                 return;
58
59         pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
60         have_preview = (pixbuf != NULL);
61         g_free (filename);
62
63         gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
64         if (pixbuf)
65                 gdk_pixbuf_unref (pixbuf);
66
67         gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
68 }
69         
70 static GList *filesel_create(const gchar *title, const gchar *path,
71                              gboolean multiple_files,
72                              gboolean open, gboolean folder_mode,
73                              const gchar *filter)
74 {
75         GSList *slist = NULL, *slist_orig = NULL;
76         GList *list = NULL;
77
78         gint action = (open == TRUE) ? 
79                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
80                                                GTK_FILE_CHOOSER_ACTION_OPEN):
81                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
82                                                GTK_FILE_CHOOSER_ACTION_SAVE);
83                         
84         gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
85         GtkWidget *chooser = gtk_file_chooser_dialog_new (title, NULL, action, 
86                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
87                                 action_btn, GTK_RESPONSE_ACCEPT, 
88                                 NULL);
89         if (filter != NULL) {
90                 GtkFileFilter *file_filter = gtk_file_filter_new();
91                 gtk_file_filter_add_pattern(file_filter, filter);
92                 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
93                                             file_filter);
94         }
95
96         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
97                 GtkImage *preview;
98                 preview = gtk_image_new ();
99                 gtk_file_chooser_set_preview_widget (chooser, preview);
100                 g_signal_connect (chooser, "update-preview",
101                             G_CALLBACK (update_preview_cb), preview);
102
103         }
104
105         manage_window_set_transient (GTK_WINDOW(chooser));
106         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
107
108         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
109
110         if (path && strlen(path) > 0) {
111                 char *filename = NULL;
112                 char *realpath = strdup(path);
113                 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
114                         filename = "";
115                 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
116                         filename++;
117                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
118                 } else {
119                         filename = (char *) path;
120                         free(realpath); 
121                         realpath = strdup(get_home_dir());
122                 }
123                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), realpath);
124                 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
125                         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), filename);
126                 free(realpath);
127         } else {
128                 if (!prefs_common.attach_load_dir)
129                         prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
130
131                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), prefs_common.attach_load_dir);
132         }
133
134         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) 
135                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
136         
137         manage_window_focus_out(chooser, NULL, NULL);
138         gtk_widget_destroy (chooser);
139
140         slist_orig = slist;
141         
142         if (slist) {
143                 gchar *tmp = strdup(slist->data);
144
145                 if (!path && prefs_common.attach_load_dir)
146                         g_free(prefs_common.attach_load_dir);
147                 
148                 if (strrchr(tmp, G_DIR_SEPARATOR))
149                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
150
151                 if (!path)
152                         prefs_common.attach_load_dir = g_strdup(tmp);
153
154                 g_free(tmp);
155         }
156
157         while (slist) {
158                 list = g_list_append(list, slist->data);
159                 slist = slist->next;
160         }
161         
162         if (slist_orig)
163                 g_slist_free(slist_orig);
164         
165         return list;
166 }
167
168 /**
169  * This function lets the user select multiple files.
170  * This opens an Open type dialog.
171  * @param title the title of the dialog
172  */
173 GList *filesel_select_multiple_files_open(const gchar *title)
174 {
175         return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
176 }
177
178 GList *filesel_select_multiple_files_open_with_filter(  const gchar *title,
179                                                         const gchar *path,
180                                                         const gchar *filter)
181 {
182         return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
183 }
184
185 /**
186  * This function lets the user select one file.
187  * This opens an Open type dialog if "file" is NULL, 
188  * Save dialog if "file" contains a path.
189  * @param title the title of the dialog
190  * @param path the optional path to save to
191  */
192 static gchar *filesel_select_file(const gchar *title, const gchar *path,
193                                   gboolean open, gboolean folder_mode,
194                                   const gchar *filter)
195 {
196         GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
197         gchar * result = NULL;
198         if (list) {
199                 result = strdup(list->data);
200         }
201         g_list_free(list);
202         return result;
203 }
204 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
205 {
206         return filesel_select_file (title, path, TRUE, FALSE, NULL);
207 }
208
209 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
210                                             const gchar *filter)
211 {
212         return filesel_select_file (title, path, TRUE, FALSE, filter);
213 }
214
215 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
216 {
217         return filesel_select_file (title, path, FALSE, FALSE, NULL);
218 }
219
220 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
221 {
222         return filesel_select_file (title, path, TRUE, TRUE, NULL);
223 }
224
225 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
226 {
227         return filesel_select_file (title, path, FALSE, TRUE, NULL);
228 }
229