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