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