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