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