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