ecc06825e26070fa28dab7a06f735d5fba05a031
[claws.git] / src / foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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 "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkwindow.h>
27 #include <gtk/gtkvbox.h>
28 #include <gtk/gtkscrolledwindow.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtkhbbox.h>
31 #include <gtk/gtksignal.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "utils.h"
43 #include "gtkutils.h"
44 #include "gtksctree.h"
45 #include "stock_pixmap.h"
46 #include "foldersel.h"
47 #include "alertpanel.h"
48 #include "manage_window.h"
49 #include "folder.h"
50
51 static GdkPixmap *folderxpm;
52 static GdkBitmap *folderxpmmask;
53 static GdkPixmap *folderopenxpm;
54 static GdkBitmap *folderopenxpmmask;
55
56 static GtkWidget *window;
57 static GtkWidget *ctree;
58 static GtkWidget *entry;
59 static GtkWidget *ok_button;
60 static GtkWidget *cancel_button;
61
62 static FolderItem *folder_item;
63 static FolderItem *selected_item;
64
65 static gboolean cancelled;
66 static gboolean finished;
67
68 static void foldersel_create    (void);
69 static void foldersel_init      (void);
70 static void foldersel_set_tree  (Folder                 *cur_folder,
71                                  FolderSelectionType     type);
72
73 static void foldersel_selected  (GtkCList       *clist,
74                                  gint            row,
75                                  gint            column,
76                                  GdkEvent       *event,
77                                  gpointer        data);
78
79 static void foldersel_ok        (GtkButton      *button,
80                                  gpointer        data);
81 static void foldersel_cancel    (GtkButton      *button,
82                                  gpointer        data);
83 static void foldersel_activated (void);
84 static gint delete_event        (GtkWidget      *widget,
85                                  GdkEventAny    *event,
86                                  gpointer        data);
87 static gboolean key_pressed     (GtkWidget      *widget,
88                                  GdkEventKey    *event,
89                                  gpointer        data);
90
91 static gint foldersel_clist_compare     (GtkCList       *clist,
92                                          gconstpointer   ptr1,
93                                          gconstpointer   ptr2);
94
95 FolderItem *foldersel_folder_sel(Folder *cur_folder,
96                                  FolderSelectionType type,
97                                  const gchar *default_folder)
98 {
99         GtkCTreeNode *node;
100
101         selected_item = NULL;
102
103         if (!window) {
104                 foldersel_create();
105                 foldersel_init();
106         } else
107                 gtk_widget_show(window);
108         manage_window_set_transient(GTK_WINDOW(window));
109
110         foldersel_set_tree(cur_folder, type);
111
112         if (folder_item) {
113                 node = gtk_ctree_find_by_row_data
114                         (GTK_CTREE(ctree), NULL, folder_item);
115                 if (node) {
116                         gint row;
117
118                         row = gtkut_ctree_get_nth_from_node
119                                 (GTK_CTREE(ctree), node);
120                         gtk_clist_select_row(GTK_CLIST(ctree), row, -1);
121                         gtkut_clist_set_focus_row(GTK_CLIST(ctree), row);
122                         gtk_ctree_node_moveto(GTK_CTREE(ctree), node, -1,
123                                               0.5, 0);
124                 }
125         }
126         gtk_widget_grab_focus(ok_button);
127         gtk_widget_grab_focus(ctree);
128
129         cancelled = finished = FALSE;
130
131         while (finished == FALSE)
132                 gtk_main_iteration();
133
134         gtk_widget_hide(window);
135         gtk_entry_set_text(GTK_ENTRY(entry), "");
136         gtk_clist_clear(GTK_CLIST(ctree));
137
138         if (!cancelled &&
139             selected_item && selected_item->path) {
140                 folder_item = selected_item;
141                 return folder_item;
142         } else
143                 return NULL;
144 }
145
146 static void foldersel_create(void)
147 {
148         GtkWidget *vbox;
149         GtkWidget *scrolledwin;
150         GtkWidget *confirm_area;
151
152         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
153         gtk_window_set_title(GTK_WINDOW(window), _("Select folder"));
154         gtk_container_set_border_width(GTK_CONTAINER(window), 4);
155         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
156         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
157         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
158         gtk_window_set_wmclass
159                 (GTK_WINDOW(window), "folder_selection", "Sylpheed");
160         g_signal_connect(G_OBJECT(window), "delete_event",
161                          G_CALLBACK(delete_event), NULL);
162         g_signal_connect(G_OBJECT(window), "key_press_event",
163                          G_CALLBACK(key_pressed), NULL);
164         MANAGE_WINDOW_SIGNALS_CONNECT(window);
165
166         vbox = gtk_vbox_new(FALSE, 4);
167         gtk_container_add(GTK_CONTAINER(window), vbox);
168
169         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
170         gtk_widget_set_size_request(window, 300, 360);
171         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
172                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
173         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
174
175         ctree = gtk_ctree_new(1, 0);
176         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
177                                             GTK_CLIST(ctree)->vadjustment);
178         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
179         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
180         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
181         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
182                                      GTK_CTREE_EXPANDER_SQUARE);
183         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
184         gtk_clist_set_compare_func(GTK_CLIST(ctree), foldersel_clist_compare);
185         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[0].button,
186                                GTK_CAN_FOCUS);
187         /* g_signal_connect(G_OBJECT(ctree), "tree_select_row",
188                             G_CALLBACK(foldersel_selected), NULL); */
189         g_signal_connect(G_OBJECT(ctree), "select_row",
190                          G_CALLBACK(foldersel_selected), NULL);
191
192         entry = gtk_entry_new();
193         gtk_entry_set_editable(GTK_ENTRY(entry), FALSE);
194         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
195         g_signal_connect(G_OBJECT(entry), "activate",
196                          G_CALLBACK(foldersel_activated), NULL);
197
198         gtkut_button_set_create(&confirm_area,
199                                 &ok_button,     _("OK"),
200                                 &cancel_button, _("Cancel"),
201                                 NULL,           NULL);
202
203         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
204         gtk_widget_grab_default(ok_button);
205
206         g_signal_connect(G_OBJECT(ok_button), "clicked",
207                          G_CALLBACK(foldersel_ok), NULL);
208         g_signal_connect(G_OBJECT(cancel_button), "clicked",
209                          G_CALLBACK(foldersel_cancel), NULL);
210
211         gtk_widget_show_all(window);
212 }
213
214 static void foldersel_init(void)
215 {
216         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE,
217                          &folderxpm, &folderxpmmask);
218         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN,
219                          &folderopenxpm, &folderopenxpmmask);
220 }
221
222 static gboolean foldersel_gnode_func(GtkCTree *ctree, guint depth,
223                                      GNode *gnode, GtkCTreeNode *cnode,
224                                      gpointer data)
225 {
226         FolderItem *item = FOLDER_ITEM(gnode->data);
227         gchar *name;
228
229         name = folder_item_get_name(item);
230
231         gtk_ctree_node_set_row_data(ctree, cnode, item);
232         gtk_ctree_set_node_info(ctree, cnode, name,
233                                 FOLDER_SPACING,
234                                 folderxpm, folderxpmmask,
235                                 folderopenxpm, folderopenxpmmask,
236                                 FALSE, FALSE);
237
238         g_free(name);
239
240         return TRUE;
241 }
242
243 static void foldersel_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
244                                   gpointer data)
245 {
246         if (GTK_CTREE_ROW(node)->children)
247                 gtk_ctree_expand(ctree, node);
248 }
249
250 #define SET_SPECIAL_FOLDER(item) \
251 { \
252         if (item) { \
253                 GtkCTreeNode *node_, *parent, *sibling; \
254  \
255                 node_ = gtk_ctree_find_by_row_data \
256                         (GTK_CTREE(ctree), node, item); \
257                 if (!node_) \
258                         g_warning("%s not found.\n", item->path); \
259                 else { \
260                         parent = GTK_CTREE_ROW(node_)->parent; \
261                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
262                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
263                         else \
264                                 sibling = GTK_CTREE_ROW(parent)->children; \
265                         if (node_ != sibling) \
266                                 gtk_ctree_move(GTK_CTREE(ctree), \
267                                                node_, parent, sibling); \
268                 } \
269  \
270                 prev = node_; \
271         } \
272 }
273
274 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
275 {
276         Folder *folder;
277         GtkCTreeNode *node;
278         GList *list;
279
280         list = folder_get_list();
281
282         gtk_clist_freeze(GTK_CLIST(ctree));
283
284         for (; list != NULL; list = list->next) {
285                 GtkCTreeNode *prev = NULL;
286
287                 folder = FOLDER(list->data);
288                 g_return_if_fail(folder != NULL);
289
290                 if (type != FOLDER_SEL_ALL) {
291                         if (FOLDER_TYPE(folder) == F_NEWS)
292                                 continue;
293                 }
294
295                 node = gtk_ctree_insert_gnode(GTK_CTREE(ctree), NULL, NULL,
296                                               folder->node,
297                                               foldersel_gnode_func,
298                                               NULL);
299                 gtk_sctree_sort_recursive(GTK_CTREE(ctree), node);
300                 SET_SPECIAL_FOLDER(folder->inbox);
301                 SET_SPECIAL_FOLDER(folder->outbox);
302                 SET_SPECIAL_FOLDER(folder->draft);
303                 SET_SPECIAL_FOLDER(folder->queue);
304                 SET_SPECIAL_FOLDER(folder->trash);
305                 gtk_ctree_pre_recursive(GTK_CTREE(ctree), node,
306                                         foldersel_expand_func,
307                                         NULL);
308         }
309
310         gtk_clist_thaw(GTK_CLIST(ctree));
311 }
312
313 static void foldersel_selected(GtkCList *clist, gint row, gint column,
314                                GdkEvent *event, gpointer data)
315 {
316         GdkEventButton *ev = (GdkEventButton *)event;
317
318         selected_item = gtk_clist_get_row_data(clist, row);
319         if (selected_item && selected_item->path) {
320                 gchar *id;
321                 id = folder_item_get_identifier(selected_item);
322                 gtk_entry_set_text(GTK_ENTRY(entry), id);
323                 g_free(id);
324         } else
325                 gtk_entry_set_text(GTK_ENTRY(entry), "");
326
327         if (ev && GDK_2BUTTON_PRESS == ev->type)
328                 gtk_button_clicked(GTK_BUTTON(ok_button));
329 }
330
331 static void foldersel_ok(GtkButton *button, gpointer data)
332 {
333         finished = TRUE;
334 }
335
336 static void foldersel_cancel(GtkButton *button, gpointer data)
337 {
338         cancelled = TRUE;
339         finished = TRUE;
340 }
341
342 static void foldersel_activated(void)
343 {
344         gtk_button_clicked(GTK_BUTTON(ok_button));
345 }
346
347 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
348 {
349         foldersel_cancel(NULL, NULL);
350         return TRUE;
351 }
352
353 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
354 {
355         if (event && event->keyval == GDK_Escape)
356                 foldersel_cancel(NULL, NULL);
357         return FALSE;
358 }
359
360 static gint foldersel_clist_compare(GtkCList *clist,
361                                     gconstpointer ptr1, gconstpointer ptr2)
362 {
363         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
364         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
365
366         if (!item1->name)
367                 return (item2->name != NULL);
368         if (!item2->name)
369                 return -1;
370
371         return g_strcasecmp(item1->name, item2->name);
372 }