c830613bd7fec7e059f501258146cba807b06501
[claws.git] / src / gtk / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkfilesel.h>
24 #include <gtk/gtkentry.h>
25 #include <gtk/gtkmain.h>
26 #include <gtk/gtksignal.h>
27 #include <gtk/gtkeditable.h>
28
29 #include "sylpheed.h"
30 #include "filesel.h"
31 #include "manage_window.h"
32 #include "gtkutils.h"
33 #include "utils.h"
34
35 static GtkWidget *filesel;
36 static gboolean filesel_ack;
37 static gboolean filesel_fin;
38 static gchar *filesel_oldfilename;
39
40 static void filesel_create(const gchar *title, gboolean multiple_files);
41 static void filesel_ok_cb(GtkWidget *widget, gpointer data);
42 static void filesel_cancel_cb(GtkWidget *widget, gpointer data);
43 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
44 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
45
46 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
47                                                GdkEventButton *event, gpointer userdata);
48 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
49                                                 GdkEventButton *event, gpointer userdata);
50
51 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
52                                               GdkEventButton *event, gpointer userdata);
53 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
54                                                GdkEventButton *event, gpointer userdata);
55
56 static GList *filesel_get_multiple_filenames(void);
57
58 gchar *filesel_select_file(const gchar *title, const gchar *file)
59 {
60         static gchar *filename = NULL;
61         static gchar *cwd = NULL;
62         gchar *startdir = NULL;
63         gchar *realfile = NULL;
64
65         filesel_create(title, FALSE);
66
67         manage_window_set_transient(GTK_WINDOW(filesel));
68
69         if (filename) {
70                 g_free(filename);
71                 filename = NULL;
72         }
73
74         /* try to extract the directory and filename from file */
75         if (file) {
76                 startdir = g_dirname(file);
77                 realfile = g_basename(file);
78         } else {
79                 realfile = file;
80         }
81
82         /* use sylpheed's startup directory if we can't find anything useful */
83         if (!startdir || !strcmp(startdir, ".")) {
84                 cwd = g_strconcat(sylpheed_get_startup_dir(), G_DIR_SEPARATOR_S, NULL);
85         } else {
86                 cwd = g_strconcat(startdir, G_DIR_SEPARATOR_S, NULL);
87         }
88
89         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
90
91         if (realfile) {
92                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
93                                                 realfile);
94                 filesel_oldfilename = g_strdup(realfile);
95         } else {
96                 filesel_oldfilename = NULL;
97         }
98
99         gtk_widget_show(filesel);
100
101         filesel_ack = filesel_fin = FALSE;
102
103         while (filesel_fin == FALSE)
104                 gtk_main_iteration();
105
106         if (filesel_ack) {
107                 gchar *str;
108
109                 str = gtk_file_selection_get_filename
110                         (GTK_FILE_SELECTION(filesel));
111                 if (str && str[0] != '\0') {
112                         gchar *dir;
113
114                         filename = g_strdup(str);
115                         dir = g_dirname(str);
116                         g_free(cwd);
117                         cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
118                         g_free(dir);
119                 }
120         }
121
122         if (filesel_oldfilename) 
123                 g_free(filesel_oldfilename);
124
125         if (startdir)
126                 g_free(startdir);
127
128         manage_window_focus_out(filesel, NULL, NULL);
129         gtk_widget_destroy(filesel);
130         GTK_EVENTS_FLUSH();
131
132         return filename;
133 }
134
135 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
136 {
137         /* ALF - sorry for the exuberant code duping... need to 
138          * be cleaned up. */
139         static gchar *filename = NULL;
140         static gchar *cwd = NULL;
141         GList        *list = NULL;
142
143         filesel_create(title, TRUE);
144
145         manage_window_set_transient(GTK_WINDOW(filesel));
146
147         if (filename) {
148                 g_free(filename);
149                 filename = NULL;
150         }
151
152         if (!cwd)
153                 cwd = g_strconcat(sylpheed_get_startup_dir(), G_DIR_SEPARATOR_S, NULL);
154
155         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
156
157         if (file)
158                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
159                                                 file);
160         gtk_widget_show(filesel);
161
162         filesel_ack = filesel_fin = FALSE;
163
164         while (filesel_fin == FALSE)
165                 gtk_main_iteration();
166
167         if (filesel_ack) {
168                 gchar *fname = NULL;
169
170                 list = filesel_get_multiple_filenames();
171
172                 if (!list) {
173                         fname = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
174                         list = g_list_append(list, g_strdup(fname));
175                 }
176                 /* store dir of first entry */
177                 if (NULL != (fname = list->data) && *fname) {
178                         gchar *dir;
179                         if (NULL != (dir = g_dirname(fname))) {
180                                 g_free(cwd);
181                                 cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
182                                 g_free(dir); 
183                         }
184                 }
185         }
186
187         manage_window_focus_out(filesel, NULL, NULL);
188         gtk_widget_destroy(filesel);
189         GTK_EVENTS_FLUSH();
190
191         return list;
192 }
193
194 static void filesel_create(const gchar *title, gboolean multiple_files)
195 {
196         filesel = gtk_file_selection_new(title);
197         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
198         gtk_window_set_wmclass
199                 (GTK_WINDOW(filesel), "file_selection", "Sylpheed");
200
201         gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
202                            "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
203                            NULL);
204         gtk_signal_connect
205                 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
206                  "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
207                  NULL);
208         gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
209                            GTK_SIGNAL_FUNC(delete_event), NULL);
210         gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
211                            GTK_SIGNAL_FUNC(key_pressed), NULL);
212         MANAGE_WINDOW_SIGNALS_CONNECT(filesel);
213
214         if (multiple_files) {
215                 gtk_clist_set_selection_mode
216                         (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
217                          GTK_SELECTION_MULTIPLE);
218                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
219                                    "select_row", 
220                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
221                                    NULL);
222                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
223                                    "unselect_row",
224                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
225                                    NULL);
226                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
227                                    "select_row",
228                                    GTK_SIGNAL_FUNC(filesel_dir_list_select_row_multi),
229                                    NULL);
230         } else {
231                 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
232                                          "select_row", 
233                                          GTK_SIGNAL_FUNC(filesel_file_list_select_row_single),
234                                          NULL);
235                 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
236                                          "select_row",
237                                          GTK_SIGNAL_FUNC(filesel_dir_list_select_row_single),
238                                          NULL);
239         }
240 }
241
242 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
243 {
244         filesel_ack = TRUE;
245         filesel_fin = TRUE;
246 }
247
248 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
249 {
250         filesel_ack = FALSE;
251         filesel_fin = TRUE;
252 }
253
254 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
255 {
256         filesel_cancel_cb(NULL, NULL);
257         if (filesel_oldfilename) {
258                 g_free(filesel_oldfilename);
259                 filesel_oldfilename = NULL;
260         }
261         return TRUE;
262 }
263
264 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
265 {
266         if (event && event->keyval == GDK_Escape)
267                 filesel_cancel_cb(NULL, NULL);
268 }
269
270 /* handle both "select_row" and "unselect_row". note that we're using the
271  * entry box to put there the selected file names in. we're not using these
272  * entry box to get the selected file names. instead we use the clist selection.
273  * the entry box is used only to retrieve dir name. */
274 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
275                                                GdkEventButton *event, gpointer userdata)
276 {
277         /* simple implementation in which we clear the file entry and refill it */
278         GList    *list  = clist->selection;
279         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
280
281         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
282
283 #define INVALID_FILENAME_CHARS     " "
284         for (; list; list = list->next) {
285                 gint row = GPOINTER_TO_INT(list->data);
286                 gchar *text = NULL, *tmp;
287
288                 if (!gtk_clist_get_text(clist, row, 0, &text))
289                         break;
290
291                 /* NOTE: quick glance in source code of GtkCList
292                  * reveals we should not free the returned 'text' */
293                 
294                 tmp = g_strconcat(text, " ", NULL);
295                 text = tmp;
296                 gtk_entry_append_text(entry, text); 
297                 g_free(text);
298         }
299 #undef INVALID_FILENAME_CHARS
300 }
301
302 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
303                                               GdkEventButton *event, gpointer userdata)
304 {
305         GtkEntry *entry     = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
306         GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
307
308         /* if dir list is selected we clean everything */
309         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
310         gtk_clist_unselect_all(file_list);
311 }
312
313 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
314                                          GdkEventButton *event, gpointer userdata)
315 {
316         gchar *text;
317
318         if(gtk_clist_get_text(clist, row, 0, &text)) {
319                 filesel_oldfilename = g_strdup(text);
320                 debug_print("%s\n", filesel_oldfilename);
321         } else {
322                 filesel_oldfilename = NULL;
323         }
324 }
325
326 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
327                                         GdkEventButton *event, gpointer userdata)
328 {
329         gchar *buf;
330         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
331
332         buf = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
333         if(filesel_oldfilename && !(*buf)) {
334                 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
335                 gtk_entry_append_text(entry, filesel_oldfilename);
336         }
337         g_free(buf);
338 }
339
340 static GList *filesel_get_multiple_filenames(void)
341 {
342         /* as noted before we are not using the entry text when selecting
343          * multiple files. to much hassle to parse out invalid chars (chars
344          * that need to be escaped). instead we use the file_list. the
345          * entry is only useful for extracting the current directory. */
346         GtkCList *file_list  = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
347         GList    *list = NULL, *sel_list;
348         gchar    *cwd, *tmp;     
349         gboolean  separator;
350
351         g_return_val_if_fail(file_list->selection != NULL, NULL);
352
353         tmp = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
354         tmp = g_strdup(tmp);
355         cwd = g_dirname(tmp);
356         g_free(tmp);
357         g_return_val_if_fail(cwd != NULL, NULL);
358
359         /* only quick way to check the end of a multi byte string for our
360          * separator... */
361         g_strreverse(cwd);
362         separator = 0 == g_strncasecmp(cwd, G_DIR_SEPARATOR_S, strlen(G_DIR_SEPARATOR_S))
363                 ?  TRUE : FALSE;
364         g_strreverse(cwd);
365
366         /* fetch the selected file names */
367         for (sel_list = file_list->selection; sel_list; sel_list = sel_list->next) {
368                 gint   sel = GPOINTER_TO_INT(sel_list->data);
369                 gchar *sel_text = NULL;
370                 gchar *fname = NULL;
371                 
372                 gtk_clist_get_text(file_list, sel, 0, &sel_text);
373                 if (!sel_text) continue;
374                 sel_text = g_strdup(sel_text);
375
376                 if (separator)
377                         fname = g_strconcat(cwd, sel_text, NULL);
378                 else
379                         fname = g_strconcat(cwd, G_DIR_SEPARATOR_S, sel_text, NULL);
380                 
381                 list = g_list_append(list, fname);
382                 g_free(sel_text);
383         }
384         
385         g_free(cwd);
386         
387         return list;
388 }
389