2009-01-09 [colin] 3.7.0cvs25
[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         if (folder_pixbuf)
350                 g_object_unref(folder_pixbuf);
351         if (folderopen_pixbuf)
352                 g_object_unref(folderopen_pixbuf);
353         if (foldernoselect_pixbuf)
354                 g_object_unref(foldernoselect_pixbuf);
355         foldersel_init();
356 }
357
358 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
359                                   GtkTreeIter *iter, GtkTreeIter *parent)
360 {
361         gchar *name, *tmpname;
362         GdkPixbuf *pixbuf, *pixbuf_open;
363         gboolean use_color;
364         PangoWeight weight = PANGO_WEIGHT_NORMAL;
365         GdkColor *foreground = NULL;
366         static GdkColor color_noselect = {0, COLOR_DIM, COLOR_DIM, COLOR_DIM};
367         static GdkColor color_new;
368
369         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &color_new);
370
371         name = tmpname = folder_item_get_name(item);
372
373         if (item->stype != F_NORMAL && FOLDER_IS_LOCAL(item->folder)) {
374                 switch (item->stype) {
375                 case F_INBOX:
376                         if (!strcmp2(item->name, INBOX_DIR))
377                                 name = _("Inbox");
378                         break;
379                 case F_OUTBOX:
380                         if (!strcmp2(item->name, OUTBOX_DIR))
381                                 name = _("Sent");
382                         break;
383                 case F_QUEUE:
384                         if (!strcmp2(item->name, QUEUE_DIR))
385                                 name = _("Queue");
386                         break;
387                 case F_TRASH:
388                         if (!strcmp2(item->name, TRASH_DIR))
389                                 name = _("Trash");
390                         break;
391                 case F_DRAFT:
392                         if (!strcmp2(item->name, DRAFT_DIR))
393                                 name = _("Drafts");
394                         break;
395                 default:
396                         break;
397                 }
398         }
399
400         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
401                 name = g_strdup_printf("%s (%d)", name, item->total_msgs);
402         } else if (item->unread_msgs > 0) {
403                 name = g_strdup_printf("%s (%d)", name, item->unread_msgs);
404         } else
405                 name = g_strdup(name);
406
407         pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
408         pixbuf_open =
409                 item->no_select ? foldernoselect_pixbuf : folderopen_pixbuf;
410
411         if (folder_has_parent_of_type(item, F_DRAFT) ||
412             folder_has_parent_of_type(item, F_OUTBOX) ||
413             folder_has_parent_of_type(item, F_TRASH)) {
414                 use_color = FALSE;
415         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
416                 use_color = (item->total_msgs > 0);
417                 if (item->total_msgs > 0)
418                         weight = PANGO_WEIGHT_BOLD;
419         } else {
420                 if (item->unread_msgs > 0)
421                         weight = PANGO_WEIGHT_BOLD;
422                 use_color = (item->new_msgs > 0);
423         }
424
425         if (item->no_select)
426                 foreground = &color_noselect;
427         else if (use_color)
428                 foreground = &color_new;
429
430         /* insert this node */
431         gtk_tree_store_append(store, iter, parent);
432         gtk_tree_store_set(store, iter,
433                            FOLDERSEL_FOLDERNAME, name,
434                            FOLDERSEL_FOLDERITEM, item,
435                            FOLDERSEL_PIXBUF, pixbuf,
436                            FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
437                            FOLDERSEL_FOREGROUND, foreground,
438                            FOLDERSEL_BOLD, weight,
439                            -1);
440         
441         g_free(tmpname);
442 }
443
444 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
445                                             GtkTreeIter *parent)
446 {
447         FolderItem *item;
448         GtkTreeIter child;
449         GNode *iter;
450
451         g_return_if_fail(node != NULL);
452         g_return_if_fail(node->data != NULL);
453         g_return_if_fail(store != NULL);
454
455         item = FOLDER_ITEM(node->data);
456         foldersel_append_item(store, item, &child, parent);
457
458         /* insert its children (this node as parent) */
459         for (iter = node->children; iter != NULL; iter = iter->next)
460                 foldersel_insert_gnode_in_store(store, iter, &child);
461 }
462
463 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
464 {
465         Folder *folder;
466         GList *list;
467
468         for (list = folder_get_list(); list != NULL; list = list->next) {
469                 folder = FOLDER(list->data);
470                 g_return_if_fail(folder != NULL);
471
472                 if (type != FOLDER_SEL_ALL) {
473                         if (FOLDER_TYPE(folder) == F_NEWS)
474                                 continue;
475                 }
476
477                 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
478         }
479
480         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
481                                              FOLDERSEL_FOLDERNAME,
482                                              GTK_SORT_ASCENDING);
483
484         gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
485 }
486
487 #include "localfolder.h"
488 static gboolean foldersel_selected(GtkTreeSelection *selection,
489                                    GtkTreeModel *model, GtkTreePath *path,
490                                    gboolean currently_selected, gpointer data)
491 {
492         GtkTreeIter iter;
493         FolderItem *item = NULL;
494
495         if (currently_selected)
496                 return TRUE;
497
498         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
499                 return TRUE;
500
501         gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
502                            FOLDERSEL_FOLDERITEM, &item, -1);
503
504         selected_item = item;
505         if (selected_item && selected_item->path) {
506                 gchar *id;
507                 id = folder_item_get_identifier(selected_item);
508                 gtk_entry_set_text(GTK_ENTRY(entry), id);
509                 g_free(id);
510         } else
511         if (root_selectable && selected_item && selected_item->folder &&
512                         (FOLDER_TYPE(selected_item->folder) == F_MH ||
513                          FOLDER_TYPE(selected_item->folder) == F_MBOX ||
514                          FOLDER_TYPE(selected_item->folder) == F_IMAP)) {
515                 gchar *id = folder_get_identifier(selected_item->folder);
516                 gtk_entry_set_text(GTK_ENTRY(entry), id);
517                 g_free(id);
518         } else
519                 gtk_entry_set_text(GTK_ENTRY(entry), "");
520
521         return TRUE;
522 }
523
524 static void foldersel_ok(GtkButton *button, gpointer data)
525 {
526         finished = TRUE;
527 }
528
529 static void foldersel_cancel(GtkButton *button, gpointer data)
530 {
531         cancelled = TRUE;
532         finished = TRUE;
533 }
534
535 static void foldersel_new_folder(GtkButton *button, gpointer data)
536 {
537         FolderItem *new_item;
538         gchar *new_folder;
539         gchar *disp_name;
540         gchar *p;
541         GtkTreeIter selected, new_child;
542         GtkTreePath *selected_p, *new_child_p;
543         GtkTreeStore *store;
544         GtkTreeModel *model;
545         GtkTreeSelection *selection;
546
547         if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
548                 return;
549
550         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
551         if (!gtk_tree_selection_get_selected(selection, &model, &selected))
552                 return;
553         store = GTK_TREE_STORE(model);
554
555         new_folder = input_dialog(_("New folder"),
556                                   _("Input the name of new folder:"),
557                                   _("NewFolder"));
558         if (!new_folder) return;
559         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
560
561         p = strchr(new_folder, G_DIR_SEPARATOR);
562         if ((p && FOLDER_TYPE(selected_item->folder) != F_IMAP) ||
563             (p && FOLDER_TYPE(selected_item->folder) == F_IMAP &&
564              *(p + 1) != '\0')) {
565                 alertpanel_error(_("'%c' can't be included in folder name."),
566                                 G_DIR_SEPARATOR);
567                 return;
568         }
569
570         disp_name = trim_string(new_folder, 32);
571         AUTORELEASE_STR(disp_name, {g_free(disp_name); return;});
572
573         /* find whether the directory already exists */
574         if (folder_find_child_item_by_name(selected_item, new_folder)) {
575                 alertpanel_error(_("The folder '%s' already exists."),
576                                  disp_name);
577                 return;
578         }
579
580         new_item = folder_create_folder(selected_item, new_folder);
581         if (!new_item) {
582                 alertpanel_error(_("Can't create the folder '%s'."), disp_name);
583                 return;
584         }
585
586         /* add new child */
587         foldersel_append_item(store, new_item, &new_child, &selected);
588         
589         selected_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &selected);
590         new_child_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store),
591                                               &new_child);
592
593         gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), selected_p, FALSE);
594         gtk_tree_selection_select_iter(selection, &new_child);
595         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), new_child_p,
596                                      NULL, TRUE, 0.5, 0.0);
597         gtk_tree_path_free(new_child_p);
598         gtk_tree_path_free(selected_p);
599
600         folder_write_list();
601 }
602
603 static void foldersel_entry_activated(GtkEntry *entry, gpointer data)
604 {
605         gtk_button_clicked(GTK_BUTTON(ok_button));
606 }
607
608 static void foldersel_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
609                                      GtkTreeViewColumn *column, gpointer data)
610 {
611         gtk_button_clicked(GTK_BUTTON(ok_button));
612 }
613
614 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
615 {
616         foldersel_cancel(NULL, NULL);
617         return TRUE;
618 }
619
620 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
621 {
622         if (event && event->keyval == GDK_Escape)
623                 foldersel_cancel(NULL, NULL);
624         return FALSE;
625 }
626
627 static gint foldersel_folder_name_compare(GtkTreeModel *model, GtkTreeIter *a,
628                                           GtkTreeIter *b, gpointer context)
629 {
630         gchar *str_a = NULL, *str_b = NULL;
631         gint val = 0;
632         FolderItem *item_a = NULL, *item_b = NULL;
633         GtkTreeIter parent;
634
635         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERITEM, &item_a, -1);
636         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERITEM, &item_b, -1);
637
638         /* no sort for root folder */
639         if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model), &parent, a))
640                 return 0;
641
642         /* if both a and b are special folders, sort them according to
643          * their types (which is in-order). Note that this assumes that
644          * there are no multiple folders of a special type. */
645         if (item_a->stype != F_NORMAL && item_b->stype != F_NORMAL)
646                 return item_a->stype - item_b->stype;
647
648         /* if b is normal folder, and a is not, b is smaller (ends up
649          * lower in the list) */
650         if (item_a->stype != F_NORMAL && item_b->stype == F_NORMAL)
651                 return item_b->stype - item_a->stype;
652
653         /* if b is special folder, and a is not, b is larger (ends up
654          * higher in the list) */
655         if (item_a->stype == F_NORMAL && item_b->stype != F_NORMAL)
656                 return item_b->stype - item_a->stype;
657
658         /* XXX g_utf8_collate_key() comparisons may speed things
659          * up when having large lists of folders */
660         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERNAME, &str_a, -1);
661         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERNAME, &str_b, -1);
662
663         /* otherwise just compare the folder names */
664         val = g_utf8_collate(str_a, str_b);
665
666         g_free(str_a);
667         g_free(str_b);
668
669         return val;
670 }
671
672 static gboolean tree_view_folder_item_func(GtkTreeModel *model,
673                                            GtkTreePath *path,
674                                            GtkTreeIter *iter,
675                                            FolderItemSearch *data)
676 {
677         FolderItem *item = NULL;
678
679         gtk_tree_model_get(model, iter, FOLDERSEL_FOLDERITEM, &item, -1);
680
681         if (data->item == item) {
682                 data->path = gtk_tree_path_copy(path);
683                 data->iter = *iter;
684                 return TRUE;
685         }
686
687         return FALSE;
688 }