840695d8829b59654a8a50eba4ffc1176f52bd47
[claws.git] / src / 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 "main.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
63         filesel_create(title, FALSE);
64
65         manage_window_set_transient(GTK_WINDOW(filesel));
66
67         if (filename) {
68                 g_free(filename);
69                 filename = NULL;
70         }
71
72         if (!cwd)
73                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
74
75         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
76
77         if (file) {
78                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
79                                                 file);
80                 filesel_oldfilename = g_strdup(file);
81         } else {
82                 filesel_oldfilename = NULL;
83         }
84
85         gtk_widget_show(filesel);
86
87         filesel_ack = filesel_fin = FALSE;
88
89         while (filesel_fin == FALSE)
90                 gtk_main_iteration();
91
92         if (filesel_ack) {
93                 gchar *str;
94
95                 str = gtk_file_selection_get_filename
96                         (GTK_FILE_SELECTION(filesel));
97                 if (str && str[0] != '\0') {
98                         gchar *dir;
99
100                         filename = g_strdup(str);
101                         dir = g_dirname(str);
102                         g_free(cwd);
103                         cwd = g_strconcat(dir, G_DIR_SEPARATOR_S, NULL);
104                         g_free(dir);
105                 }
106         }
107
108         if (filesel_oldfilename) 
109                 g_free(filesel_oldfilename);
110
111         manage_window_focus_out(filesel, NULL, NULL);
112         gtk_widget_destroy(filesel);
113         GTK_EVENTS_FLUSH();
114
115         return filename;
116 }
117
118 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
119 {
120         /* ALF - sorry for the exuberant code duping... need to 
121          * be cleaned up. */
122         static gchar *filename = NULL;
123         static gchar *cwd = NULL;
124         GList        *list = NULL;
125
126         filesel_create(title, TRUE);
127
128         manage_window_set_transient(GTK_WINDOW(filesel));
129
130         if (filename) {
131                 g_free(filename);
132                 filename = NULL;
133         }
134
135         if (!cwd)
136                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
137
138         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
139
140         if (file)
141                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
142                                                 file);
143         gtk_widget_show(filesel);
144
145         gtk_main();
146
147         if (filesel_ack) {
148                 gchar *fname = NULL;
149
150                 list = filesel_get_multiple_filenames();
151
152                 if (!list) {
153                         fname = gtk_file_selection_get_filename (GTK_FILE_SELECTION(filesel));
154                         list = g_list_append (list, g_strdup(fname));
155                 }
156         }
157
158         manage_window_focus_out(filesel, NULL, NULL);
159         gtk_widget_destroy(filesel);
160         GTK_EVENTS_FLUSH();
161
162         return list;
163 }
164
165 static void filesel_create(const gchar *title, gboolean multiple_files)
166 {
167         filesel = gtk_file_selection_new(title);
168         gtk_window_set_position(GTK_WINDOW(filesel), GTK_WIN_POS_CENTER);
169         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
170         gtk_window_set_wmclass
171                 (GTK_WINDOW(filesel), "file_selection", "Sylpheed");
172
173         gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
174                            "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
175                            NULL);
176         gtk_signal_connect
177                 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
178                  "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
179                  NULL);
180         gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
181                            GTK_SIGNAL_FUNC(delete_event), NULL);
182         gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
183                            GTK_SIGNAL_FUNC(key_pressed), NULL);
184         MANAGE_WINDOW_SIGNALS_CONNECT(filesel);
185
186         if (multiple_files) {
187                 gtk_clist_set_selection_mode
188                         (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
189                          GTK_SELECTION_MULTIPLE);
190                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
191                                    "select_row", 
192                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
193                                    NULL);
194                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
195                                    "unselect_row",
196                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
197                                    NULL);
198                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
199                                    "select_row",
200                                    GTK_SIGNAL_FUNC(filesel_dir_list_select_row_multi),
201                                    NULL);
202         } else {
203                 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
204                                          "select_row", 
205                                          GTK_SIGNAL_FUNC(filesel_file_list_select_row_single),
206                                          NULL);
207                 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
208                                          "select_row",
209                                          GTK_SIGNAL_FUNC(filesel_dir_list_select_row_single),
210                                          NULL);
211         }
212 }
213
214 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
215 {
216         filesel_ack = TRUE;
217         filesel_fin = TRUE;
218 }
219
220 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
221 {
222         filesel_ack = FALSE;
223         filesel_fin = TRUE;
224 }
225
226 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
227 {
228         filesel_cancel_cb(NULL, NULL);
229         if (filesel_oldfilename) {
230                 g_free(filesel_oldfilename);
231                 filesel_oldfilename = NULL;
232         }
233         return TRUE;
234 }
235
236 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
237 {
238         if (event && event->keyval == GDK_Escape)
239                 filesel_cancel_cb(NULL, NULL);
240 }
241
242 /* handle both "select_row" and "unselect_row". note that we're using the
243  * entry box to put there the selected file names in. we're not using these
244  * entry box to get the selected file names. instead we use the clist selection.
245  * the entry box is used only to retrieve dir name. */
246 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
247                                                GdkEventButton *event, gpointer userdata)
248 {
249         /* simple implementation in which we clear the file entry and refill it */
250         GList    *list  = clist->selection;
251         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
252
253         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
254
255 #define INVALID_FILENAME_CHARS     " "
256         for (; list; list = list->next) {
257                 gint row = GPOINTER_TO_INT(list->data);
258                 gchar *text = NULL, *tmp;
259
260                 if (!gtk_clist_get_text(clist, row, 0, &text))
261                         break;
262
263                 /* NOTE: quick glance in source code of GtkCList
264                  * reveals we should not free the returned 'text' */
265                 
266                 tmp = g_strconcat(text, " ", NULL);
267                 text = tmp;
268                 gtk_entry_append_text(entry, text); 
269                 g_free(text);
270         }
271 #undef INVALID_FILENAME_CHARS
272 }
273
274 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
275                                               GdkEventButton *event, gpointer userdata)
276 {
277         GtkEntry *entry     = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
278         GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
279
280         /* if dir list is selected we clean everything */
281         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
282         gtk_clist_unselect_all(file_list);
283 }
284
285 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
286                                          GdkEventButton *event, gpointer userdata)
287 {
288         gchar *text;
289
290         if(gtk_clist_get_text(clist, row, 0, &text)) {
291                 filesel_oldfilename = g_strdup(text);
292                 debug_print("%s\n", filesel_oldfilename);
293         } else {
294                 filesel_oldfilename = NULL;
295         }
296 }
297
298 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
299                                         GdkEventButton *event, gpointer userdata)
300 {
301         gchar *buf;
302         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
303
304         buf = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
305         if(filesel_oldfilename && !(*buf)) {
306                 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
307                 gtk_entry_append_text(entry, filesel_oldfilename);
308         }
309         g_free(buf);
310 }
311
312 static GList *filesel_get_multiple_filenames(void)
313 {
314         /* as noted before we are not using the entry text when selecting
315          * multiple files. to much hassle to parse out invalid chars (chars
316          * that need to be escaped). instead we use the file_list. the
317          * entry is only useful for extracting the current directory. */
318         GtkCList *file_list  = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
319         GList    *list = NULL, *sel_list;
320         gchar    *cwd, *tmp;     
321         gboolean  separator;
322
323         g_return_val_if_fail(file_list->selection != NULL, NULL);
324
325         tmp = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
326         tmp = g_strdup(tmp);
327         cwd = g_dirname(tmp);
328         g_free(tmp);
329         g_return_val_if_fail(cwd != NULL, NULL);
330
331         /* only quick way to check the end of a multi byte string for our
332          * separator... */
333         g_strreverse(cwd);
334         separator = 0 == g_strncasecmp(cwd, G_DIR_SEPARATOR_S, strlen(G_DIR_SEPARATOR_S))
335                 ?  TRUE : FALSE;
336         g_strreverse(cwd);
337
338         /* fetch the selected file names */
339         for (sel_list = file_list->selection; sel_list; sel_list = sel_list->next) {
340                 gint   sel = GPOINTER_TO_INT(sel_list->data);
341                 gchar *sel_text = NULL;
342                 gchar *fname = NULL;
343                 
344                 gtk_clist_get_text(file_list, sel, 0, &sel_text);
345                 if (!sel_text) continue;
346                 sel_text = g_strdup(sel_text);
347
348                 if (separator)
349                         fname = g_strconcat(cwd, sel_text, NULL);
350                 else
351                         fname = g_strconcat(cwd, G_DIR_SEPARATOR_S, sel_text, NULL);
352                 
353                 list = g_list_append(list, fname);
354                 g_free(sel_text);
355         }
356         
357         g_free(cwd);
358         
359         return list;
360 }
361