0.7.2claws release
[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         if (filesel_oldfilename) 
105                 g_free(filesel_oldfilename);
106
107         manage_window_focus_out(filesel, NULL, NULL);
108         gtk_widget_destroy(filesel);
109         GTK_EVENTS_FLUSH();
110
111         return filename;
112 }
113
114 GList *filesel_select_multiple_files(const gchar *title, const gchar *file)
115 {
116         /* ALF - sorry for the exuberant code duping... need to 
117          * be cleaned up. */
118         static gchar *filename = NULL;
119         static gchar *cwd = NULL;
120         GList        *list = NULL;
121
122         filesel_create(title, TRUE);
123
124         manage_window_set_transient(GTK_WINDOW(filesel));
125
126         if (filename) {
127                 g_free(filename);
128                 filename = NULL;
129         }
130
131         if (!cwd)
132                 cwd = g_strconcat(startup_dir, G_DIR_SEPARATOR_S, NULL);
133
134         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), cwd);
135
136         if (file)
137                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),
138                                                 file);
139         gtk_widget_show(filesel);
140
141         gtk_main();
142
143         if (filesel_ack) {
144                 GtkWidget *entry;
145                 gchar *fname = NULL;
146
147                 list = filesel_get_multiple_filenames();
148
149                 if (!list) {
150                         entry = GTK_FILE_SELECTION(filesel)->selection_entry;
151                         fname = gtk_entry_get_text (GTK_ENTRY(entry));
152                         if (fname && fname[0] != '\0')
153                                 list = g_list_append (list, g_strdup(fname));
154                 }
155         }
156
157         manage_window_focus_out(filesel, NULL, NULL);
158         gtk_widget_destroy(filesel);
159         GTK_EVENTS_FLUSH();
160
161         return list;
162 }
163
164 static void filesel_create(const gchar *title, gboolean multiple_files)
165 {
166         filesel = gtk_file_selection_new(title);
167         gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
168                            "clicked", GTK_SIGNAL_FUNC(filesel_ok_cb),
169                            NULL);
170         gtk_signal_connect
171                 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
172                  "clicked", GTK_SIGNAL_FUNC(filesel_cancel_cb),
173                  NULL);
174         gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
175                            GTK_SIGNAL_FUNC(delete_event), NULL);
176         gtk_signal_connect(GTK_OBJECT(filesel), "key_press_event",
177                            GTK_SIGNAL_FUNC(key_pressed), NULL);
178         gtk_signal_connect(GTK_OBJECT(filesel), "focus_in_event",
179                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
180         gtk_signal_connect(GTK_OBJECT(filesel), "focus_out_event",
181                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
182
183         gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
184
185         if (multiple_files) {
186                 gtk_clist_set_selection_mode
187                         (GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list),
188                          GTK_SELECTION_MULTIPLE);
189                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
190                                    "select_row", 
191                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
192                                    NULL);
193                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
194                                    "unselect_row",
195                                    GTK_SIGNAL_FUNC(filesel_file_list_select_row_multi),
196                                    NULL);
197                 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
198                                    "select_row",
199                                    GTK_SIGNAL_FUNC(filesel_dir_list_select_row_multi),
200                                    NULL);
201         } else {
202                 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->file_list),
203                                          "select_row", 
204                                          GTK_SIGNAL_FUNC(filesel_file_list_select_row_single),
205                                          NULL);
206                 gtk_signal_connect_after(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->dir_list),
207                                          "select_row",
208                                          GTK_SIGNAL_FUNC(filesel_dir_list_select_row_single),
209                                          NULL);
210         }
211 }
212
213 static void filesel_ok_cb(GtkWidget *widget, gpointer data)
214 {
215         filesel_ack = TRUE;
216         gtk_main_quit();
217 }
218
219 static void filesel_cancel_cb(GtkWidget *widget, gpointer data)
220 {
221         filesel_ack = FALSE;
222         gtk_main_quit();
223 }
224
225 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
226 {
227         filesel_cancel_cb(NULL, NULL);
228         if (filesel_oldfilename) {
229                 g_free(filesel_oldfilename);
230                 filesel_oldfilename = NULL;
231         }
232         return TRUE;
233 }
234
235 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
236 {
237         if (event && event->keyval == GDK_Escape)
238                 filesel_cancel_cb(NULL, NULL);
239 }
240
241 /* handle both "select_row" and "unselect_row". note that we're using the
242  * entry box to put there the selected file names in. we're not using these
243  * entry box to get the selected file names. instead we use the clist selection.
244  * the entry box is used only to retrieve dir name. */
245 static void filesel_file_list_select_row_multi(GtkCList *clist, gint row, gint col,
246                                                GdkEventButton *event, gpointer userdata)
247 {
248         /* simple implementation in which we clear the file entry and refill it */
249         GList    *list  = clist->selection;
250         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
251
252         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
253
254 #define INVALID_FILENAME_CHARS     " "
255         for (; list; list = list->next) {
256                 gint row = GPOINTER_TO_INT(list->data);
257                 gchar *text = NULL, *tmp;
258
259                 if (!gtk_clist_get_text(clist, row, 0, &text))
260                         break;
261
262                 /* NOTE: quick glance in source code of GtkCList
263                  * reveals we should not free the returned 'text' */
264                 
265                 tmp = g_strconcat(text, " ", NULL);
266                 text = tmp;
267                 gtk_entry_append_text(entry, text); 
268                 g_free(text);
269         }
270 #undef INVALID_FILENAME_CHARS
271 }
272
273 static void filesel_dir_list_select_row_multi(GtkCList *clist, gint row, gint col,
274                                               GdkEventButton *event, gpointer userdata)
275 {
276         GtkEntry *entry     = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
277         GtkCList *file_list = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
278
279         /* if dir list is selected we clean everything */
280         gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
281         gtk_clist_unselect_all(file_list);
282 }
283
284 static void filesel_file_list_select_row_single(GtkCList *clist, gint row, gint col,
285                                          GdkEventButton *event, gpointer userdata)
286 {
287         gchar *text;
288
289         if(gtk_clist_get_text(clist, row, 0, &text)) {
290                 filesel_oldfilename = g_strdup(text);
291                 debug_print("%s\n", filesel_oldfilename);
292         } else {
293                 filesel_oldfilename = NULL;
294         }
295 }
296
297 static void filesel_dir_list_select_row_single(GtkCList *clist, gint row, gint col,
298                                         GdkEventButton *event, gpointer userdata)
299 {
300         gchar *buf;
301         GtkEntry *entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
302
303         buf = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
304         if(filesel_oldfilename && !(*buf)) {
305                 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
306                 gtk_entry_append_text(entry, filesel_oldfilename);
307         }
308         g_free(buf);
309 }
310
311 static GList *filesel_get_multiple_filenames(void)
312 {
313         /* as noted before we are not using the entry text when selecting
314          * multiple files. to much hassle to parse out invalid chars (chars
315          * that need to be escaped). instead we use the file_list. the
316          * entry is only useful for extracting the current directory. */
317         GtkCList *file_list  = GTK_CLIST(GTK_FILE_SELECTION(filesel)->file_list);
318         GtkEntry *file_entry = GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry);
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         return list;
358 }
359