2006-09-06 [wwp] 2.4.0cvs154
[claws.git] / src / foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkmain.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtktreestore.h>
28 #include <gtk/gtktreeview.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkscrolledwindow.h>
32 #include <gtk/gtkentry.h>
33 #include <gtk/gtkhbbox.h>
34 #include <gtk/gtksignal.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <errno.h>
42
43 #include "main.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "stock_pixmap.h"
47 #include "foldersel.h"
48 #include "alertpanel.h"
49 #include "manage_window.h"
50 #include "folderview.h"
51 #include "inputdialog.h"
52 #include "folder.h"
53 #include "prefs_common.h"
54
55 enum {
56         FOLDERSEL_FOLDERNAME,
57         FOLDERSEL_FOLDERITEM,
58         FOLDERSEL_PIXBUF,
59         FOLDERSEL_PIXBUF_OPEN,
60         FOLDERSEL_FOREGROUND,
61         FOLDERSEL_BOLD,
62         N_FOLDERSEL_COLUMNS
63 };
64
65 typedef struct _FolderItemSearch        FolderItemSearch;
66
67 struct _FolderItemSearch
68 {
69         FolderItem *item;
70         GtkTreePath *path;
71         GtkTreeIter iter;
72 };
73
74 static GdkPixbuf *folder_pixbuf;
75 static GdkPixbuf *folderopen_pixbuf;
76 static GdkPixbuf *foldernoselect_pixbuf;
77
78 static GtkWidget *window;
79 static GtkWidget *treeview;
80 static GtkWidget *entry;
81 static GtkWidget *ok_button;
82 static GtkWidget *cancel_button;
83 static GtkWidget *new_button;
84
85 static FolderItem *folder_item;
86 static FolderItem *selected_item;
87
88 static GtkTreeStore *tree_store;
89
90 static gboolean cancelled;
91 static gboolean finished;
92
93 static void foldersel_create            (void);
94 static void foldersel_init              (void);
95
96 static void foldersel_append_item       (GtkTreeStore   *store,
97                                          FolderItem     *item,
98                                          GtkTreeIter    *iter,
99                                          GtkTreeIter    *parent);
100
101 static void foldersel_set_tree          (Folder                 *cur_folder,
102                                          FolderSelectionType     type);
103
104 static gboolean foldersel_selected      (GtkTreeSelection *selection,
105                                          GtkTreeModel     *model,
106                                          GtkTreePath      *path,
107                                          gboolean          currently_selected,
108                                          gpointer          data);
109
110 static void foldersel_ok                (GtkButton      *button,
111                                          gpointer        data);
112 static void foldersel_cancel            (GtkButton      *button,
113                                          gpointer        data);
114 static void foldersel_new_folder        (GtkButton      *button,
115                                          gpointer        data);
116
117 static void foldersel_entry_activated   (GtkEntry       *entry,
118                                          gpointer        data);
119
120 static void foldersel_tree_activated    (GtkTreeView            *treeview,
121                                          GtkTreePath            *path,
122                                          GtkTreeViewColumn      *column,
123                                          gpointer                data);
124
125 static gint delete_event                (GtkWidget      *widget,
126                                          GdkEventAny    *event,
127                                          gpointer        data);
128 static gboolean key_pressed             (GtkWidget      *widget,
129                                          GdkEventKey    *event,
130                                          gpointer        data);
131
132 static gint foldersel_folder_name_compare       (GtkTreeModel   *model,
133                                                  GtkTreeIter    *a,
134                                                  GtkTreeIter    *b,
135                                                  gpointer        context);
136
137 static gboolean tree_view_folder_item_func      (GtkTreeModel     *model,
138                                                  GtkTreePath      *path,
139                                                  GtkTreeIter      *iter,
140                                                  FolderItemSearch *data);
141
142 FolderItem *foldersel_folder_sel(Folder *cur_folder, FolderSelectionType type,
143                                  const gchar *default_folder)
144 {
145         selected_item = NULL;
146
147         if (!window) {
148                 foldersel_create();
149                 foldersel_init();
150         }
151
152         foldersel_set_tree(cur_folder, type);
153
154         /* select current */
155         if (folder_item) {
156                 FolderItemSearch fis;
157
158                 fis.item = folder_item;
159                 fis.path = NULL;
160
161                 /* find matching model entry */
162                 gtk_tree_model_foreach
163                         (GTK_TREE_MODEL(tree_store),
164                          (GtkTreeModelForeachFunc)tree_view_folder_item_func,
165                          &fis);
166
167                 if (fis.path) {
168                         GtkTreeSelection *selection;
169
170                         selection = gtk_tree_view_get_selection
171                                 (GTK_TREE_VIEW(treeview));
172                         gtk_tree_selection_select_iter(selection, &fis.iter);
173                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview),
174                                                  fis.path, NULL, FALSE);
175                         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview),
176                                                      fis.path,
177                                                      NULL, TRUE, 0.5, 0.0);
178                         gtk_tree_path_free(fis.path);
179                 } else
180                         gtk_tree_view_scroll_to_point
181                                 (GTK_TREE_VIEW(treeview), 0, 0);
182         } else
183                 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(treeview), 0, 0);
184
185         gtk_widget_grab_focus(ok_button);
186         gtk_widget_grab_focus(treeview);
187
188         gtk_widget_show(window);
189         manage_window_set_transient(GTK_WINDOW(window));
190
191         cancelled = finished = FALSE;
192
193         while (finished == FALSE)
194                 gtk_main_iteration();
195
196         gtk_widget_hide(window);
197         gtk_entry_set_text(GTK_ENTRY(entry), "");
198         gtk_tree_store_clear(tree_store);
199
200         if (!cancelled &&
201             selected_item && selected_item->path && !selected_item->no_select) {
202                 folder_item = selected_item;
203                 return folder_item;
204         } else
205                 return NULL;
206 }
207
208 static void foldersel_size_allocate_cb(GtkWidget *widget,
209                                          GtkAllocation *allocation)
210 {
211         g_return_if_fail(allocation != NULL);
212
213         prefs_common.folderselwin_width = allocation->width;
214         prefs_common.folderselwin_height = allocation->height;
215 }
216
217 static void foldersel_create(void)
218 {
219         GtkWidget *vbox;
220         GtkWidget *scrolledwin;
221         GtkWidget *confirm_area;
222         GtkTreeViewColumn *column;
223         GtkCellRenderer *renderer;
224         GtkTreeSelection *selection;
225         static GdkGeometry geometry;
226
227         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
228         gtk_window_set_title(GTK_WINDOW(window), _("Select folder"));
229         gtk_container_set_border_width(GTK_CONTAINER(window), 4);
230         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
231         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
232         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
233         gtk_window_set_wmclass
234                 (GTK_WINDOW(window), "folder_selection", "Sylpheed-Claws");
235         gtk_widget_realize(window);
236         g_signal_connect(G_OBJECT(window), "delete_event",
237                          G_CALLBACK(delete_event), NULL);
238         g_signal_connect(G_OBJECT(window), "key_press_event",
239                          G_CALLBACK(key_pressed), NULL);
240         g_signal_connect(G_OBJECT(window), "size_allocate",
241                          G_CALLBACK(foldersel_size_allocate_cb), NULL);
242         MANAGE_WINDOW_SIGNALS_CONNECT(window);
243
244         vbox = gtk_vbox_new(FALSE, 4);
245         gtk_container_add(GTK_CONTAINER(window), vbox);
246
247         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
248         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
249                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
250         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
251                                             GTK_SHADOW_IN);
252         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
253
254         tree_store = gtk_tree_store_new(N_FOLDERSEL_COLUMNS,
255                                         G_TYPE_STRING,
256                                         G_TYPE_POINTER,
257                                         GDK_TYPE_PIXBUF,
258                                         GDK_TYPE_PIXBUF,
259                                         GDK_TYPE_COLOR,
260                                         G_TYPE_INT);
261         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(tree_store),
262                                         FOLDERSEL_FOLDERNAME,
263                                         foldersel_folder_name_compare,
264                                         NULL, NULL);
265
266         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
267         g_object_unref(G_OBJECT(tree_store));
268         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
269         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
270                                      prefs_common.use_stripes_everywhere);
271         gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview),
272                                         FOLDERSEL_FOLDERNAME);
273
274         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
275         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
276         gtk_tree_selection_set_select_function(selection, foldersel_selected,
277                                                NULL, NULL);
278
279         g_signal_connect(G_OBJECT(treeview), "row-activated",
280                          G_CALLBACK(foldersel_tree_activated), NULL);
281         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
282
283         column = gtk_tree_view_column_new();
284         gtk_tree_view_column_set_spacing(column, 2);
285         renderer = gtk_cell_renderer_pixbuf_new();
286         gtk_tree_view_column_pack_start(column, renderer, FALSE);
287         gtk_tree_view_column_set_attributes
288                 (column, renderer,
289                  "pixbuf", FOLDERSEL_PIXBUF,
290                  "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN,
291                  "pixbuf-expander-closed", FOLDERSEL_PIXBUF,
292                  NULL);
293
294         /* create text renderer */
295         renderer = gtk_cell_renderer_text_new();
296         gtk_tree_view_column_pack_start(column, renderer, TRUE);
297         gtk_tree_view_column_set_attributes
298                 (column, renderer,
299                  "text", FOLDERSEL_FOLDERNAME,
300                  "foreground-gdk", FOLDERSEL_FOREGROUND,
301                  "weight", FOLDERSEL_BOLD,
302                  NULL);
303         g_object_set(G_OBJECT(renderer), "weight", PANGO_WEIGHT_BOLD, NULL);
304         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
305
306         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
307
308         entry = gtk_entry_new();
309         gtk_entry_set_editable(GTK_ENTRY(entry), FALSE);
310         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
311         g_signal_connect(G_OBJECT(entry), "activate",
312                          G_CALLBACK(foldersel_entry_activated), NULL);
313
314         gtkut_stock_button_set_create(&confirm_area,
315                                       &new_button,    GTK_STOCK_NEW,
316                                       &cancel_button, GTK_STOCK_CANCEL,
317                                       &ok_button,     GTK_STOCK_OK);
318
319         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
320         gtk_widget_grab_default(ok_button);
321
322         g_signal_connect(G_OBJECT(ok_button), "clicked",
323                          G_CALLBACK(foldersel_ok), NULL);
324         g_signal_connect(G_OBJECT(cancel_button), "clicked",
325                          G_CALLBACK(foldersel_cancel), NULL);
326         g_signal_connect(G_OBJECT(new_button), "clicked",
327                          G_CALLBACK(foldersel_new_folder), NULL);
328
329         if (!geometry.min_height) {
330                 geometry.min_width = 300;
331                 geometry.min_height = 360;
332         }
333
334         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
335                                       GDK_HINT_MIN_SIZE);
336         gtk_widget_set_size_request(window, prefs_common.folderselwin_width,
337                                     prefs_common.folderselwin_height);
338
339         gtk_widget_show_all(vbox);
340 }
341
342 static void foldersel_init(void)
343 {
344         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_CLOSE,
345                          &folder_pixbuf);
346         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_OPEN,
347                          &folderopen_pixbuf);
348         stock_pixbuf_gdk(treeview, STOCK_PIXMAP_DIR_NOSELECT,
349                          &foldernoselect_pixbuf);
350 }
351
352 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
353                                   GtkTreeIter *iter, GtkTreeIter *parent)
354 {
355         gchar *name, *tmpname;
356         GdkPixbuf *pixbuf, *pixbuf_open;
357         gboolean use_color;
358         PangoWeight weight = PANGO_WEIGHT_NORMAL;
359         GdkColor *foreground = NULL;
360         static GdkColor color_noselect = {0, COLOR_DIM, COLOR_DIM, COLOR_DIM};
361         static GdkColor color_new;
362
363         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &color_new);
364
365         name = tmpname = folder_item_get_name(item);
366
367         if (item->stype != F_NORMAL && FOLDER_IS_LOCAL(item->folder)) {
368                 switch (item->stype) {
369                 case F_INBOX:
370                         if (!strcmp2(item->name, INBOX_DIR))
371                                 name = _("Inbox");
372                         break;
373                 case F_OUTBOX:
374                         if (!strcmp2(item->name, OUTBOX_DIR))
375                                 name = _("Sent");
376                         break;
377                 case F_QUEUE:
378                         if (!strcmp2(item->name, QUEUE_DIR))
379                                 name = _("Queue");
380                         break;
381                 case F_TRASH:
382                         if (!strcmp2(item->name, TRASH_DIR))
383                                 name = _("Trash");
384                         break;
385                 case F_DRAFT:
386                         if (!strcmp2(item->name, DRAFT_DIR))
387                                 name = _("Drafts");
388                         break;
389                 default:
390                         break;
391                 }
392         }
393
394         /* CLAWS: already adds the UI string to the folder name */
395 #ifndef CLAWS         
396         if (!item->parent) {
397                 switch (FOLDER_TYPE(item->folder)) {
398                 case F_MH:
399                         Xstrcat_a(name, name, " (MH)", ); break;
400                 case F_IMAP:
401                         Xstrcat_a(name, name, " (IMAP4)", ); break;
402                 case F_NEWS:
403                         Xstrcat_a(name, name, " (News)", ); break;
404                 default:
405                         break;
406                 }
407         }
408 #endif        
409
410         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
411                 name = g_strdup_printf("%s (%d)", name, item->total_msgs);
412         } else if (item->unread_msgs > 0) {
413                 name = g_strdup_printf("%s (%d)", name, item->unread_msgs);
414         } else
415                 name = g_strdup(name);
416
417         pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
418         pixbuf_open =
419                 item->no_select ? foldernoselect_pixbuf : folderopen_pixbuf;
420
421         if (folder_has_parent_of_type(item, F_DRAFT) ||
422             folder_has_parent_of_type(item, F_OUTBOX) ||
423             folder_has_parent_of_type(item, F_TRASH)) {
424                 use_color = FALSE;
425         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
426                 use_color = (item->total_msgs > 0);
427                 if (item->total_msgs > 0)
428                         weight = PANGO_WEIGHT_BOLD;
429         } else {
430                 if (item->unread_msgs > 0)
431                         weight = PANGO_WEIGHT_BOLD;
432                 use_color = (item->new_msgs > 0);
433         }
434
435         if (item->no_select)
436                 foreground = &color_noselect;
437         else if (use_color)
438                 foreground = &color_new;
439
440         /* insert this node */
441         gtk_tree_store_append(store, iter, parent);
442         gtk_tree_store_set(store, iter,
443                            FOLDERSEL_FOLDERNAME, name,
444                            FOLDERSEL_FOLDERITEM, item,
445                            FOLDERSEL_PIXBUF, pixbuf,
446                            FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
447                            FOLDERSEL_FOREGROUND, foreground,
448                            FOLDERSEL_BOLD, weight,
449                            -1);
450         
451         g_free(tmpname);
452 }
453
454 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
455                                             GtkTreeIter *parent)
456 {
457         FolderItem *item;
458         GtkTreeIter child;
459         GNode *iter;
460
461         g_return_if_fail(node != NULL);
462         g_return_if_fail(node->data != NULL);
463         g_return_if_fail(store != NULL);
464
465         item = FOLDER_ITEM(node->data);
466         foldersel_append_item(store, item, &child, parent);
467
468         /* insert its children (this node as parent) */
469         for (iter = node->children; iter != NULL; iter = iter->next)
470                 foldersel_insert_gnode_in_store(store, iter, &child);
471 }
472
473 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
474 {
475         Folder *folder;
476         GList *list;
477
478         for (list = folder_get_list(); list != NULL; list = list->next) {
479                 folder = FOLDER(list->data);
480                 g_return_if_fail(folder != NULL);
481
482                 if (type != FOLDER_SEL_ALL) {
483                         if (FOLDER_TYPE(folder) == F_NEWS)
484                                 continue;
485                 }
486
487                 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
488         }
489
490         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
491                                              FOLDERSEL_FOLDERNAME,
492                                              GTK_SORT_ASCENDING);
493
494         gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
495 }
496
497 static gboolean foldersel_selected(GtkTreeSelection *selection,
498                                    GtkTreeModel *model, GtkTreePath *path,
499                                    gboolean currently_selected, gpointer data)
500 {
501         GtkTreeIter iter;
502         FolderItem *item = NULL;
503
504         if (currently_selected)
505                 return TRUE;
506
507         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
508                 return TRUE;
509
510         gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
511                            FOLDERSEL_FOLDERITEM, &item, -1);
512
513         selected_item = item;
514         if (selected_item && selected_item->path && !selected_item->no_select) {
515                 gchar *id;
516                 id = folder_item_get_identifier(selected_item);
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 }