2009-01-17 [colin] 3.7.0cvs37
[claws.git] / src / foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include <errno.h>
33
34 #include "main.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "stock_pixmap.h"
38 #include "foldersel.h"
39 #include "alertpanel.h"
40 #include "manage_window.h"
41 #include "folderview.h"
42 #include "inputdialog.h"
43 #include "folder.h"
44 #include "prefs_common.h"
45
46 enum {
47         FOLDERSEL_FOLDERNAME,
48         FOLDERSEL_FOLDERITEM,
49         FOLDERSEL_PIXBUF,
50         FOLDERSEL_PIXBUF_OPEN,
51         FOLDERSEL_FOREGROUND,
52         FOLDERSEL_BOLD,
53         N_FOLDERSEL_COLUMNS
54 };
55
56 typedef struct _FolderItemSearch        FolderItemSearch;
57
58 struct _FolderItemSearch
59 {
60         FolderItem *item;
61         GtkTreePath *path;
62         GtkTreeIter iter;
63 };
64
65 static GdkPixbuf *folder_pixbuf = NULL;
66 static GdkPixbuf *folderopen_pixbuf = NULL;
67 static GdkPixbuf *foldernoselect_pixbuf = NULL;
68
69 static GtkWidget *window;
70 static GtkWidget *treeview;
71 static GtkWidget *entry;
72 static GtkWidget *ok_button;
73 static GtkWidget *cancel_button;
74 static GtkWidget *new_button;
75 static gboolean   root_selectable;
76
77 static FolderItem *folder_item;
78 static FolderItem *selected_item;
79
80 static GtkTreeStore *tree_store;
81
82 static gboolean cancelled;
83 static gboolean finished;
84
85 static void foldersel_create            (void);
86 static void foldersel_init              (void);
87
88 static void foldersel_append_item       (GtkTreeStore   *store,
89                                          FolderItem     *item,
90                                          GtkTreeIter    *iter,
91                                          GtkTreeIter    *parent);
92
93 static void foldersel_set_tree          (Folder                 *cur_folder,
94                                          FolderSelectionType     type);
95
96 static gboolean foldersel_selected      (GtkTreeSelection *selection,
97                                          GtkTreeModel     *model,
98                                          GtkTreePath      *path,
99                                          gboolean          currently_selected,
100                                          gpointer          data);
101
102 static void foldersel_ok                (GtkButton      *button,
103                                          gpointer        data);
104 static void foldersel_cancel            (GtkButton      *button,
105                                          gpointer        data);
106 static void foldersel_new_folder        (GtkButton      *button,
107                                          gpointer        data);
108
109 static void foldersel_entry_activated   (GtkEntry       *entry,
110                                          gpointer        data);
111
112 static void foldersel_tree_activated    (GtkTreeView            *treeview,
113                                          GtkTreePath            *path,
114                                          GtkTreeViewColumn      *column,
115                                          gpointer                data);
116
117 static gint delete_event                (GtkWidget      *widget,
118                                          GdkEventAny    *event,
119                                          gpointer        data);
120 static gboolean key_pressed             (GtkWidget      *widget,
121                                          GdkEventKey    *event,
122                                          gpointer        data);
123
124 static gint foldersel_folder_name_compare       (GtkTreeModel   *model,
125                                                  GtkTreeIter    *a,
126                                                  GtkTreeIter    *b,
127                                                  gpointer        context);
128
129 static gboolean tree_view_folder_item_func      (GtkTreeModel     *model,
130                                                  GtkTreePath      *path,
131                                                  GtkTreeIter      *iter,
132                                                  FolderItemSearch *data);
133
134 FolderItem *foldersel_folder_sel(Folder *cur_folder, FolderSelectionType type,
135                                  const gchar *default_folder, gboolean can_sel_mailbox)
136 {
137         selected_item = NULL;
138         root_selectable = can_sel_mailbox;
139
140         if (!window) {
141                 foldersel_create();
142                 foldersel_init();
143         }
144
145         foldersel_set_tree(cur_folder, type);
146
147         /* select current */
148         if (folder_item) {
149                 FolderItemSearch fis;
150
151                 fis.item = folder_item;
152                 fis.path = NULL;
153
154                 /* find matching model entry */
155                 gtk_tree_model_foreach
156                         (GTK_TREE_MODEL(tree_store),
157                          (GtkTreeModelForeachFunc)tree_view_folder_item_func,
158                          &fis);
159
160                 if (fis.path) {
161                         GtkTreeSelection *selection;
162
163                         selection = gtk_tree_view_get_selection
164                                 (GTK_TREE_VIEW(treeview));
165                         gtk_tree_selection_select_iter(selection, &fis.iter);
166                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview),
167                                                  fis.path, NULL, FALSE);
168                         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview),
169                                                      fis.path,
170                                                      NULL, TRUE, 0.5, 0.0);
171                         gtk_tree_path_free(fis.path);
172                 } else
173                         gtk_tree_view_scroll_to_point
174                                 (GTK_TREE_VIEW(treeview), 0, 0);
175         } else
176                 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(treeview), 0, 0);
177
178         gtk_widget_grab_focus(ok_button);
179         gtk_widget_grab_focus(treeview);
180
181         gtk_widget_show(window);
182         manage_window_set_transient(GTK_WINDOW(window));
183
184         cancelled = finished = FALSE;
185
186         while (finished == FALSE)
187                 gtk_main_iteration();
188
189         gtk_widget_hide(window);
190         gtk_entry_set_text(GTK_ENTRY(entry), "");
191         gtk_tree_store_clear(tree_store);
192
193         if (!cancelled &&
194             selected_item && (selected_item->path || root_selectable)) {
195                 folder_item = selected_item;
196                 return folder_item;
197         } else
198                 return NULL;
199 }
200
201 static void foldersel_size_allocate_cb(GtkWidget *widget,
202                                          GtkAllocation *allocation)
203 {
204         g_return_if_fail(allocation != NULL);
205
206         prefs_common.folderselwin_width = allocation->width;
207         prefs_common.folderselwin_height = allocation->height;
208 }
209
210 static void foldersel_create(void)
211 {
212         GtkWidget *vbox;
213         GtkWidget *scrolledwin;
214         GtkWidget *confirm_area;
215         GtkTreeViewColumn *column;
216         GtkCellRenderer *renderer;
217         GtkTreeSelection *selection;
218         static GdkGeometry geometry;
219
220         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "foldersel");
221         gtk_window_set_title(GTK_WINDOW(window), _("Select folder"));
222         gtk_container_set_border_width(GTK_CONTAINER(window), 4);
223         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
224         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
225         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
226
227         gtk_widget_realize(window);
228         g_signal_connect(G_OBJECT(window), "delete_event",
229                          G_CALLBACK(delete_event), NULL);
230         g_signal_connect(G_OBJECT(window), "key_press_event",
231                          G_CALLBACK(key_pressed), NULL);
232         g_signal_connect(G_OBJECT(window), "size_allocate",
233                          G_CALLBACK(foldersel_size_allocate_cb), NULL);
234         MANAGE_WINDOW_SIGNALS_CONNECT(window);
235
236         vbox = gtk_vbox_new(FALSE, 4);
237         gtk_container_add(GTK_CONTAINER(window), vbox);
238
239         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
240         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
241                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
242         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
243                                             GTK_SHADOW_IN);
244         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
245
246         tree_store = gtk_tree_store_new(N_FOLDERSEL_COLUMNS,
247                                         G_TYPE_STRING,
248                                         G_TYPE_POINTER,
249                                         GDK_TYPE_PIXBUF,
250                                         GDK_TYPE_PIXBUF,
251                                         GDK_TYPE_COLOR,
252                                         G_TYPE_INT);
253         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(tree_store),
254                                         FOLDERSEL_FOLDERNAME,
255                                         foldersel_folder_name_compare,
256                                         NULL, NULL);
257
258         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
259         g_object_unref(G_OBJECT(tree_store));
260         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
261         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
262                                      prefs_common.use_stripes_everywhere);
263 #if GTK_CHECK_VERSION(2,10,0)
264         gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview),
265                                                         prefs_common.enable_dotted_lines);
266 #endif
267         gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview),
268                                         FOLDERSEL_FOLDERNAME);
269
270         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
271         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
272         gtk_tree_selection_set_select_function(selection, foldersel_selected,
273                                                NULL, NULL);
274
275         g_signal_connect(G_OBJECT(treeview), "row-activated",
276                          G_CALLBACK(foldersel_tree_activated), NULL);
277         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
278
279         column = gtk_tree_view_column_new();
280         gtk_tree_view_column_set_spacing(column, 2);
281         renderer = gtk_cell_renderer_pixbuf_new();
282         gtk_tree_view_column_pack_start(column, renderer, FALSE);
283         gtk_tree_view_column_set_attributes
284                 (column, renderer,
285                  "pixbuf", FOLDERSEL_PIXBUF,
286                  "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN,
287                  "pixbuf-expander-closed", FOLDERSEL_PIXBUF,
288                  NULL);
289
290         /* create text renderer */
291         renderer = gtk_cell_renderer_text_new();
292         gtk_tree_view_column_pack_start(column, renderer, TRUE);
293         gtk_tree_view_column_set_attributes
294                 (column, renderer,
295                  "text", FOLDERSEL_FOLDERNAME,
296                  "foreground-gdk", FOLDERSEL_FOREGROUND,
297                  "weight", FOLDERSEL_BOLD,
298                  NULL);
299         g_object_set(G_OBJECT(renderer), "weight", PANGO_WEIGHT_BOLD, NULL);
300         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
301
302         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
303
304         entry = gtk_entry_new();
305         gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
306         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
307         g_signal_connect(G_OBJECT(entry), "activate",
308                          G_CALLBACK(foldersel_entry_activated), NULL);
309
310         gtkut_stock_button_set_create(&confirm_area,
311                                       &new_button,    GTK_STOCK_NEW,
312                                       &cancel_button, GTK_STOCK_CANCEL,
313                                       &ok_button,     GTK_STOCK_OK);
314
315         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
316         gtk_widget_grab_default(ok_button);
317
318         g_signal_connect(G_OBJECT(ok_button), "clicked",
319                          G_CALLBACK(foldersel_ok), NULL);
320         g_signal_connect(G_OBJECT(cancel_button), "clicked",
321                          G_CALLBACK(foldersel_cancel), NULL);
322         g_signal_connect(G_OBJECT(new_button), "clicked",
323                          G_CALLBACK(foldersel_new_folder), NULL);
324
325         if (!geometry.min_height) {
326                 geometry.min_width = 300;
327                 geometry.min_height = 360;
328         }
329
330         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
331                                       GDK_HINT_MIN_SIZE);
332         gtk_widget_set_size_request(window, prefs_common.folderselwin_width,
333                                     prefs_common.folderselwin_height);
334
335         gtk_widget_show_all(vbox);
336 }
337
338 static void foldersel_init(void)
339 {
340         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_CLOSE,
341                          &folder_pixbuf);
342         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_OPEN,
343                          &folderopen_pixbuf);
344         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_NOSELECT,
345                          &foldernoselect_pixbuf);
346 }
347
348 void foldersel_reflect_prefs_pixmap_theme(void)
349 {
350         if (folder_pixbuf)
351                 g_object_unref(folder_pixbuf);
352         if (folderopen_pixbuf)
353                 g_object_unref(folderopen_pixbuf);
354         if (foldernoselect_pixbuf)
355                 g_object_unref(foldernoselect_pixbuf);
356         foldersel_init();
357 }
358
359 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
360                                   GtkTreeIter *iter, GtkTreeIter *parent)
361 {
362         gchar *name, *tmpname;
363         GdkPixbuf *pixbuf, *pixbuf_open;
364         gboolean use_color;
365         PangoWeight weight = PANGO_WEIGHT_NORMAL;
366         GdkColor *foreground = NULL;
367         static GdkColor color_noselect = {0, COLOR_DIM, COLOR_DIM, COLOR_DIM};
368         static GdkColor color_new;
369
370         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &color_new);
371
372         name = tmpname = folder_item_get_name(item);
373
374         if (item->stype != F_NORMAL && FOLDER_IS_LOCAL(item->folder)) {
375                 switch (item->stype) {
376                 case F_INBOX:
377                         if (!strcmp2(item->name, INBOX_DIR))
378                                 name = _("Inbox");
379                         break;
380                 case F_OUTBOX:
381                         if (!strcmp2(item->name, OUTBOX_DIR))
382                                 name = _("Sent");
383                         break;
384                 case F_QUEUE:
385                         if (!strcmp2(item->name, QUEUE_DIR))
386                                 name = _("Queue");
387                         break;
388                 case F_TRASH:
389                         if (!strcmp2(item->name, TRASH_DIR))
390                                 name = _("Trash");
391                         break;
392                 case F_DRAFT:
393                         if (!strcmp2(item->name, DRAFT_DIR))
394                                 name = _("Drafts");
395                         break;
396                 default:
397                         break;
398                 }
399         }
400
401         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
402                 name = g_strdup_printf("%s (%d)", name, item->total_msgs);
403         } else if (item->unread_msgs > 0) {
404                 name = g_strdup_printf("%s (%d)", name, item->unread_msgs);
405         } else
406                 name = g_strdup(name);
407
408         pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
409         pixbuf_open =
410                 item->no_select ? foldernoselect_pixbuf : folderopen_pixbuf;
411
412         if (folder_has_parent_of_type(item, F_DRAFT) ||
413             folder_has_parent_of_type(item, F_OUTBOX) ||
414             folder_has_parent_of_type(item, F_TRASH)) {
415                 use_color = FALSE;
416         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
417                 use_color = (item->total_msgs > 0);
418                 if (item->total_msgs > 0)
419                         weight = PANGO_WEIGHT_BOLD;
420         } else {
421                 if (item->unread_msgs > 0)
422                         weight = PANGO_WEIGHT_BOLD;
423                 use_color = (item->new_msgs > 0);
424         }
425
426         if (item->no_select)
427                 foreground = &color_noselect;
428         else if (use_color)
429                 foreground = &color_new;
430
431         /* insert this node */
432         gtk_tree_store_append(store, iter, parent);
433         gtk_tree_store_set(store, iter,
434                            FOLDERSEL_FOLDERNAME, name,
435                            FOLDERSEL_FOLDERITEM, item,
436                            FOLDERSEL_PIXBUF, pixbuf,
437                            FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
438                            FOLDERSEL_FOREGROUND, foreground,
439                            FOLDERSEL_BOLD, weight,
440                            -1);
441         
442         g_free(tmpname);
443 }
444
445 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
446                                             GtkTreeIter *parent)
447 {
448         FolderItem *item;
449         GtkTreeIter child;
450         GNode *iter;
451
452         g_return_if_fail(node != NULL);
453         g_return_if_fail(node->data != NULL);
454         g_return_if_fail(store != NULL);
455
456         item = FOLDER_ITEM(node->data);
457         foldersel_append_item(store, item, &child, parent);
458
459         /* insert its children (this node as parent) */
460         for (iter = node->children; iter != NULL; iter = iter->next)
461                 foldersel_insert_gnode_in_store(store, iter, &child);
462 }
463
464 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
465 {
466         Folder *folder;
467         GList *list;
468
469         for (list = folder_get_list(); list != NULL; list = list->next) {
470                 folder = FOLDER(list->data);
471                 g_return_if_fail(folder != NULL);
472
473                 if (type != FOLDER_SEL_ALL) {
474                         if (FOLDER_TYPE(folder) == F_NEWS)
475                                 continue;
476                 }
477
478                 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
479         }
480
481         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
482                                              FOLDERSEL_FOLDERNAME,
483                                              GTK_SORT_ASCENDING);
484
485         gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
486 }
487
488 #include "localfolder.h"
489 static gboolean foldersel_selected(GtkTreeSelection *selection,
490                                    GtkTreeModel *model, GtkTreePath *path,
491                                    gboolean currently_selected, gpointer data)
492 {
493         GtkTreeIter iter;
494         FolderItem *item = NULL;
495
496         if (currently_selected)
497                 return TRUE;
498
499         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
500                 return TRUE;
501
502         gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
503                            FOLDERSEL_FOLDERITEM, &item, -1);
504
505         selected_item = item;
506         if (selected_item && selected_item->path) {
507                 gchar *id;
508                 id = folder_item_get_identifier(selected_item);
509                 gtk_entry_set_text(GTK_ENTRY(entry), id);
510                 g_free(id);
511         } else
512         if (root_selectable && selected_item && selected_item->folder &&
513                         (FOLDER_TYPE(selected_item->folder) == F_MH ||
514                          FOLDER_TYPE(selected_item->folder) == F_MBOX ||
515                          FOLDER_TYPE(selected_item->folder) == F_IMAP)) {
516                 gchar *id = folder_get_identifier(selected_item->folder);
517                 gtk_entry_set_text(GTK_ENTRY(entry), id);
518                 g_free(id);
519         } else
520                 gtk_entry_set_text(GTK_ENTRY(entry), "");
521
522         return TRUE;
523 }
524
525 static void foldersel_ok(GtkButton *button, gpointer data)
526 {
527         finished = TRUE;
528 }
529
530 static void foldersel_cancel(GtkButton *button, gpointer data)
531 {
532         cancelled = TRUE;
533         finished = TRUE;
534 }
535
536 static void foldersel_new_folder(GtkButton *button, gpointer data)
537 {
538         FolderItem *new_item;
539         gchar *new_folder;
540         gchar *disp_name;
541         gchar *p;
542         GtkTreeIter selected, new_child;
543         GtkTreePath *selected_p, *new_child_p;
544         GtkTreeStore *store;
545         GtkTreeModel *model;
546         GtkTreeSelection *selection;
547
548         if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
549                 return;
550
551         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
552         if (!gtk_tree_selection_get_selected(selection, &model, &selected))
553                 return;
554         store = GTK_TREE_STORE(model);
555
556         new_folder = input_dialog(_("New folder"),
557                                   _("Input the name of new folder:"),
558                                   _("NewFolder"));
559         if (!new_folder) return;
560         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
561
562         p = strchr(new_folder, G_DIR_SEPARATOR);
563         if ((p && FOLDER_TYPE(selected_item->folder) != F_IMAP) ||
564             (p && FOLDER_TYPE(selected_item->folder) == F_IMAP &&
565              *(p + 1) != '\0')) {
566                 alertpanel_error(_("'%c' can't be included in folder name."),
567                                 G_DIR_SEPARATOR);
568                 return;
569         }
570
571         disp_name = trim_string(new_folder, 32);
572         AUTORELEASE_STR(disp_name, {g_free(disp_name); return;});
573
574         /* find whether the directory already exists */
575         if (folder_find_child_item_by_name(selected_item, new_folder)) {
576                 alertpanel_error(_("The folder '%s' already exists."),
577                                  disp_name);
578                 return;
579         }
580
581         new_item = folder_create_folder(selected_item, new_folder);
582         if (!new_item) {
583                 alertpanel_error(_("Can't create the folder '%s'."), disp_name);
584                 return;
585         }
586
587         /* add new child */
588         foldersel_append_item(store, new_item, &new_child, &selected);
589         
590         selected_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &selected);
591         new_child_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store),
592                                               &new_child);
593
594         gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), selected_p, FALSE);
595         gtk_tree_selection_select_iter(selection, &new_child);
596         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), new_child_p,
597                                      NULL, TRUE, 0.5, 0.0);
598         gtk_tree_path_free(new_child_p);
599         gtk_tree_path_free(selected_p);
600
601         folder_write_list();
602 }
603
604 static void foldersel_entry_activated(GtkEntry *entry, gpointer data)
605 {
606         gtk_button_clicked(GTK_BUTTON(ok_button));
607 }
608
609 static void foldersel_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
610                                      GtkTreeViewColumn *column, gpointer data)
611 {
612         gtk_button_clicked(GTK_BUTTON(ok_button));
613 }
614
615 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
616 {
617         foldersel_cancel(NULL, NULL);
618         return TRUE;
619 }
620
621 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
622 {
623         if (event && event->keyval == GDK_Escape)
624                 foldersel_cancel(NULL, NULL);
625         return FALSE;
626 }
627
628 static gint foldersel_folder_name_compare(GtkTreeModel *model, GtkTreeIter *a,
629                                           GtkTreeIter *b, gpointer context)
630 {
631         gchar *str_a = NULL, *str_b = NULL;
632         gint val = 0;
633         FolderItem *item_a = NULL, *item_b = NULL;
634         GtkTreeIter parent;
635
636         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERITEM, &item_a, -1);
637         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERITEM, &item_b, -1);
638
639         /* no sort for root folder */
640         if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model), &parent, a))
641                 return 0;
642
643         /* if both a and b are special folders, sort them according to
644          * their types (which is in-order). Note that this assumes that
645          * there are no multiple folders of a special type. */
646         if (item_a->stype != F_NORMAL && item_b->stype != F_NORMAL)
647                 return item_a->stype - item_b->stype;
648
649         /* if b is normal folder, and a is not, b is smaller (ends up
650          * lower in the list) */
651         if (item_a->stype != F_NORMAL && item_b->stype == F_NORMAL)
652                 return item_b->stype - item_a->stype;
653
654         /* if b is special folder, and a is not, b is larger (ends up
655          * higher in the list) */
656         if (item_a->stype == F_NORMAL && item_b->stype != F_NORMAL)
657                 return item_b->stype - item_a->stype;
658
659         /* XXX g_utf8_collate_key() comparisons may speed things
660          * up when having large lists of folders */
661         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERNAME, &str_a, -1);
662         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERNAME, &str_b, -1);
663
664         /* otherwise just compare the folder names */
665         val = g_utf8_collate(str_a, str_b);
666
667         g_free(str_a);
668         g_free(str_b);
669
670         return val;
671 }
672
673 static gboolean tree_view_folder_item_func(GtkTreeModel *model,
674                                            GtkTreePath *path,
675                                            GtkTreeIter *iter,
676                                            FolderItemSearch *data)
677 {
678         FolderItem *item = NULL;
679
680         gtk_tree_model_get(model, iter, FOLDERSEL_FOLDERITEM, &item, -1);
681
682         if (data->item == item) {
683                 data->path = gtk_tree_path_copy(path);
684                 data->iter = *iter;
685                 return TRUE;
686         }
687
688         return FALSE;
689 }