2012-11-16 [colin] 3.9.0cvs10
[claws.git] / src / foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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_window_present(window);
182         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
183         manage_window_set_transient(GTK_WINDOW(window));
184
185         cancelled = finished = FALSE;
186
187         while (finished == FALSE)
188                 gtk_main_iteration();
189
190         gtk_widget_destroy(window);
191         window = NULL;
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         cm_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_resizable(GTK_WINDOW(window), TRUE);
225
226         gtk_widget_realize(window);
227         g_signal_connect(G_OBJECT(window), "delete_event",
228                          G_CALLBACK(delete_event), NULL);
229         g_signal_connect(G_OBJECT(window), "key_press_event",
230                          G_CALLBACK(key_pressed), NULL);
231         g_signal_connect(G_OBJECT(window), "size_allocate",
232                          G_CALLBACK(foldersel_size_allocate_cb), NULL);
233         MANAGE_WINDOW_SIGNALS_CONNECT(window);
234
235         vbox = gtk_vbox_new(FALSE, 4);
236         gtk_container_add(GTK_CONTAINER(window), vbox);
237
238         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
239         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
240                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
241         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
242                                             GTK_SHADOW_IN);
243         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
244
245         tree_store = gtk_tree_store_new(N_FOLDERSEL_COLUMNS,
246                                         G_TYPE_STRING,
247                                         G_TYPE_POINTER,
248                                         GDK_TYPE_PIXBUF,
249                                         GDK_TYPE_PIXBUF,
250                                         GDK_TYPE_COLOR,
251                                         G_TYPE_INT);
252         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(tree_store),
253                                         FOLDERSEL_FOLDERNAME,
254                                         foldersel_folder_name_compare,
255                                         NULL, NULL);
256
257         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
258         g_object_unref(G_OBJECT(tree_store));
259         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
260         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
261                                      prefs_common.use_stripes_everywhere);
262         gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview), FALSE);
263         gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview),
264                                         FOLDERSEL_FOLDERNAME);
265
266         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
267         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
268         gtk_tree_selection_set_select_function(selection, foldersel_selected,
269                                                NULL, NULL);
270
271         g_signal_connect(G_OBJECT(treeview), "row-activated",
272                          G_CALLBACK(foldersel_tree_activated), NULL);
273         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
274
275         column = gtk_tree_view_column_new();
276         gtk_tree_view_column_set_spacing(column, 2);
277         renderer = gtk_cell_renderer_pixbuf_new();
278         gtk_tree_view_column_pack_start(column, renderer, FALSE);
279         gtk_tree_view_column_set_attributes
280                 (column, renderer,
281                  "pixbuf", FOLDERSEL_PIXBUF,
282                  "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN,
283                  "pixbuf-expander-closed", FOLDERSEL_PIXBUF,
284                  NULL);
285
286         /* create text renderer */
287         renderer = gtk_cell_renderer_text_new();
288         gtk_tree_view_column_pack_start(column, renderer, TRUE);
289         gtk_tree_view_column_set_attributes
290                 (column, renderer,
291                  "text", FOLDERSEL_FOLDERNAME,
292                  "foreground-gdk", FOLDERSEL_FOREGROUND,
293                  "weight", FOLDERSEL_BOLD,
294                  NULL);
295         g_object_set(G_OBJECT(renderer), "weight", PANGO_WEIGHT_BOLD, NULL);
296         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
297
298         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
299
300         entry = gtk_entry_new();
301         gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
302         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
303         g_signal_connect(G_OBJECT(entry), "activate",
304                          G_CALLBACK(foldersel_entry_activated), NULL);
305
306         gtkut_stock_button_set_create(&confirm_area,
307                                       &new_button,    GTK_STOCK_NEW,
308                                       &cancel_button, GTK_STOCK_CANCEL,
309                                       &ok_button,     GTK_STOCK_OK);
310
311         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
312         gtk_widget_grab_default(ok_button);
313
314         g_signal_connect(G_OBJECT(ok_button), "clicked",
315                          G_CALLBACK(foldersel_ok), NULL);
316         g_signal_connect(G_OBJECT(cancel_button), "clicked",
317                          G_CALLBACK(foldersel_cancel), NULL);
318         g_signal_connect(G_OBJECT(new_button), "clicked",
319                          G_CALLBACK(foldersel_new_folder), NULL);
320
321         if (!geometry.min_height) {
322                 geometry.min_width = 300;
323                 geometry.min_height = 360;
324         }
325
326         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
327                                       GDK_HINT_MIN_SIZE);
328         gtk_widget_set_size_request(window, prefs_common.folderselwin_width,
329                                     prefs_common.folderselwin_height);
330
331         gtk_widget_show_all(vbox);
332 }
333
334 static void foldersel_init(void)
335 {
336         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_CLOSE,
337                          &folder_pixbuf);
338         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_OPEN,
339                          &folderopen_pixbuf);
340         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_NOSELECT,
341                          &foldernoselect_pixbuf);
342 }
343
344 void foldersel_reflect_prefs_pixmap_theme(void)
345 {
346         if (folder_pixbuf)
347                 g_object_unref(folder_pixbuf);
348         if (folderopen_pixbuf)
349                 g_object_unref(folderopen_pixbuf);
350         if (foldernoselect_pixbuf)
351                 g_object_unref(foldernoselect_pixbuf);
352         foldersel_init();
353 }
354
355 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
356                                   GtkTreeIter *iter, GtkTreeIter *parent)
357 {
358         gchar *name, *tmpname;
359         GdkPixbuf *pixbuf, *pixbuf_open;
360         gboolean use_color;
361         PangoWeight weight = PANGO_WEIGHT_NORMAL;
362         GdkColor *foreground = NULL;
363         static GdkColor color_noselect = {0, COLOR_DIM, COLOR_DIM, COLOR_DIM};
364         static GdkColor color_new;
365
366         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &color_new);
367
368         name = tmpname = folder_item_get_name(item);
369
370         if (item->stype != F_NORMAL && FOLDER_IS_LOCAL(item->folder)) {
371                 switch (item->stype) {
372                 case F_INBOX:
373                         if (!strcmp2(item->name, INBOX_DIR))
374                                 name = _("Inbox");
375                         break;
376                 case F_OUTBOX:
377                         if (!strcmp2(item->name, OUTBOX_DIR))
378                                 name = _("Sent");
379                         break;
380                 case F_QUEUE:
381                         if (!strcmp2(item->name, QUEUE_DIR))
382                                 name = _("Queue");
383                         break;
384                 case F_TRASH:
385                         if (!strcmp2(item->name, TRASH_DIR))
386                                 name = _("Trash");
387                         break;
388                 case F_DRAFT:
389                         if (!strcmp2(item->name, DRAFT_DIR))
390                                 name = _("Drafts");
391                         break;
392                 default:
393                         break;
394                 }
395         }
396
397         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
398                 name = g_strdup_printf("%s (%d)", name, item->total_msgs);
399         } else if (item->unread_msgs > 0) {
400                 name = g_strdup_printf("%s (%d)", name, item->unread_msgs);
401         } else
402                 name = g_strdup(name);
403
404         pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
405         pixbuf_open =
406                 item->no_select ? foldernoselect_pixbuf : folderopen_pixbuf;
407
408         if (folder_has_parent_of_type(item, F_DRAFT) ||
409             folder_has_parent_of_type(item, F_OUTBOX) ||
410             folder_has_parent_of_type(item, F_TRASH)) {
411                 use_color = FALSE;
412         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
413                 use_color = (item->total_msgs > 0);
414                 if (item->total_msgs > 0)
415                         weight = PANGO_WEIGHT_BOLD;
416         } else {
417                 if (item->unread_msgs > 0)
418                         weight = PANGO_WEIGHT_BOLD;
419                 use_color = (item->new_msgs > 0);
420         }
421
422         if (item->no_select)
423                 foreground = &color_noselect;
424         else if (use_color)
425                 foreground = &color_new;
426
427         /* insert this node */
428         gtk_tree_store_append(store, iter, parent);
429         gtk_tree_store_set(store, iter,
430                            FOLDERSEL_FOLDERNAME, name,
431                            FOLDERSEL_FOLDERITEM, item,
432                            FOLDERSEL_PIXBUF, pixbuf,
433                            FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
434                            FOLDERSEL_FOREGROUND, foreground,
435                            FOLDERSEL_BOLD, weight,
436                            -1);
437         
438         g_free(tmpname);
439 }
440
441 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
442                                             GtkTreeIter *parent)
443 {
444         FolderItem *item;
445         GtkTreeIter child;
446         GNode *iter;
447
448         cm_return_if_fail(node != NULL);
449         cm_return_if_fail(node->data != NULL);
450         cm_return_if_fail(store != NULL);
451
452         item = FOLDER_ITEM(node->data);
453         foldersel_append_item(store, item, &child, parent);
454
455         /* insert its children (this node as parent) */
456         for (iter = node->children; iter != NULL; iter = iter->next)
457                 foldersel_insert_gnode_in_store(store, iter, &child);
458 }
459
460 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
461 {
462         Folder *folder;
463         GList *list;
464
465         for (list = folder_get_list(); list != NULL; list = list->next) {
466                 folder = FOLDER(list->data);
467                 cm_return_if_fail(folder != NULL);
468
469                 if (type != FOLDER_SEL_ALL) {
470                         if (FOLDER_TYPE(folder) == F_NEWS)
471                                 continue;
472                 }
473
474                 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
475         }
476
477         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
478                                              FOLDERSEL_FOLDERNAME,
479                                              GTK_SORT_ASCENDING);
480
481         gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
482 }
483
484 #include "localfolder.h"
485 static gboolean foldersel_selected(GtkTreeSelection *selection,
486                                    GtkTreeModel *model, GtkTreePath *path,
487                                    gboolean currently_selected, gpointer data)
488 {
489         GtkTreeIter iter;
490         FolderItem *item = NULL;
491
492         if (currently_selected)
493                 return TRUE;
494
495         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
496                 return TRUE;
497
498         gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
499                            FOLDERSEL_FOLDERITEM, &item, -1);
500
501         selected_item = item;
502         if (selected_item && selected_item->path) {
503                 gchar *id;
504                 id = folder_item_get_identifier(selected_item);
505                 gtk_entry_set_text(GTK_ENTRY(entry), id);
506                 g_free(id);
507         } else
508         if (root_selectable && selected_item && selected_item->folder &&
509                         (FOLDER_TYPE(selected_item->folder) == F_MH ||
510                          FOLDER_TYPE(selected_item->folder) == F_MBOX ||
511                          FOLDER_TYPE(selected_item->folder) == F_IMAP)) {
512                 gchar *id = folder_get_identifier(selected_item->folder);
513                 gtk_entry_set_text(GTK_ENTRY(entry), id);
514                 g_free(id);
515         } else
516                 gtk_entry_set_text(GTK_ENTRY(entry), "");
517
518         return TRUE;
519 }
520
521 static void foldersel_ok(GtkButton *button, gpointer data)
522 {
523         finished = TRUE;
524 }
525
526 static void foldersel_cancel(GtkButton *button, gpointer data)
527 {
528         cancelled = TRUE;
529         finished = TRUE;
530 }
531
532 static void foldersel_new_folder(GtkButton *button, gpointer data)
533 {
534         FolderItem *new_item;
535         gchar *new_folder;
536         gchar *disp_name;
537         gchar *p;
538         GtkTreeIter selected, new_child;
539         GtkTreePath *selected_p, *new_child_p;
540         GtkTreeStore *store;
541         GtkTreeModel *model;
542         GtkTreeSelection *selection;
543
544         if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
545                 return;
546
547         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
548         if (!gtk_tree_selection_get_selected(selection, &model, &selected))
549                 return;
550         store = GTK_TREE_STORE(model);
551
552         new_folder = input_dialog(_("New folder"),
553                                   _("Input the name of new folder:"),
554                                   _("NewFolder"));
555         if (!new_folder) return;
556         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
557
558         p = strchr(new_folder, G_DIR_SEPARATOR);
559         if ((p && FOLDER_TYPE(selected_item->folder) != F_IMAP) ||
560             (p && FOLDER_TYPE(selected_item->folder) == F_IMAP &&
561              *(p + 1) != '\0')) {
562                 alertpanel_error(_("'%c' can't be included in folder name."),
563                                 G_DIR_SEPARATOR);
564                 return;
565         }
566
567         disp_name = trim_string(new_folder, 32);
568         AUTORELEASE_STR(disp_name, {g_free(disp_name); return;});
569
570         /* find whether the directory already exists */
571         if (folder_find_child_item_by_name(selected_item, new_folder)) {
572                 alertpanel_error(_("The folder '%s' already exists."),
573                                  disp_name);
574                 return;
575         }
576
577         new_item = folder_create_folder(selected_item, new_folder);
578         if (!new_item) {
579                 alertpanel_error(_("Can't create the folder '%s'."), disp_name);
580                 return;
581         }
582
583         /* add new child */
584         foldersel_append_item(store, new_item, &new_child, &selected);
585         
586         selected_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &selected);
587         new_child_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store),
588                                               &new_child);
589
590         gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), selected_p, FALSE);
591         gtk_tree_selection_select_iter(selection, &new_child);
592         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), new_child_p,
593                                      NULL, TRUE, 0.5, 0.0);
594         gtk_tree_path_free(new_child_p);
595         gtk_tree_path_free(selected_p);
596
597         folder_write_list();
598 }
599
600 static void foldersel_entry_activated(GtkEntry *entry, gpointer data)
601 {
602         gtk_button_clicked(GTK_BUTTON(ok_button));
603 }
604
605 static void foldersel_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
606                                      GtkTreeViewColumn *column, gpointer data)
607 {
608         gtk_button_clicked(GTK_BUTTON(ok_button));
609 }
610
611 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
612 {
613         foldersel_cancel(NULL, NULL);
614         return TRUE;
615 }
616
617 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
618 {
619         if (event && event->keyval == GDK_KEY_Escape)
620                 foldersel_cancel(NULL, NULL);
621         return FALSE;
622 }
623
624 static gint foldersel_folder_name_compare(GtkTreeModel *model, GtkTreeIter *a,
625                                           GtkTreeIter *b, gpointer context)
626 {
627         gchar *str_a = NULL, *str_b = NULL;
628         gint val = 0;
629         FolderItem *item_a = NULL, *item_b = NULL;
630         GtkTreeIter parent;
631
632         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERITEM, &item_a, -1);
633         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERITEM, &item_b, -1);
634
635         /* no sort for root folder */
636         if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model), &parent, a))
637                 return 0;
638
639         /* if both a and b are special folders, sort them according to
640          * their types (which is in-order). Note that this assumes that
641          * there are no multiple folders of a special type. */
642         if (item_a->stype != F_NORMAL && item_b->stype != F_NORMAL)
643                 return item_a->stype - item_b->stype;
644
645         /* if b is normal folder, and a is not, b is smaller (ends up
646          * lower in the list) */
647         if (item_a->stype != F_NORMAL && item_b->stype == F_NORMAL)
648                 return item_b->stype - item_a->stype;
649
650         /* if b is special folder, and a is not, b is larger (ends up
651          * higher in the list) */
652         if (item_a->stype == F_NORMAL && item_b->stype != F_NORMAL)
653                 return item_b->stype - item_a->stype;
654
655         /* XXX g_utf8_collate_key() comparisons may speed things
656          * up when having large lists of folders */
657         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERNAME, &str_a, -1);
658         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERNAME, &str_b, -1);
659
660         /* otherwise just compare the folder names */
661         val = g_utf8_collate(str_a, str_b);
662
663         g_free(str_a);
664         g_free(str_b);
665
666         return val;
667 }
668
669 static gboolean tree_view_folder_item_func(GtkTreeModel *model,
670                                            GtkTreePath *path,
671                                            GtkTreeIter *iter,
672                                            FolderItemSearch *data)
673 {
674         FolderItem *item = NULL;
675
676         gtk_tree_model_get(model, iter, FOLDERSEL_FOLDERITEM, &item, -1);
677
678         if (data->item == item) {
679                 data->path = gtk_tree_path_copy(path);
680                 data->iter = *iter;
681                 return TRUE;
682         }
683
684         return FALSE;
685 }