faf8293193d1905aac871a7fe69abbd4ac9f655e
[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 static GdkPixbuf *foldernoselectopen_pixbuf = NULL;
69
70 static GtkWidget *window;
71 static GtkWidget *treeview;
72 static GtkWidget *entry;
73 static GtkWidget *ok_button;
74 static GtkWidget *cancel_button;
75 static GtkWidget *new_button;
76 static gboolean   root_selectable;
77
78 static FolderItem *folder_item;
79 static FolderItem *selected_item;
80
81 static GtkTreeStore *tree_store;
82
83 static gboolean cancelled;
84 static gboolean finished;
85
86 static void foldersel_create            (void);
87 static void foldersel_init              (void);
88
89 static void foldersel_append_item       (GtkTreeStore   *store,
90                                          FolderItem     *item,
91                                          GtkTreeIter    *iter,
92                                          GtkTreeIter    *parent);
93
94 static void foldersel_set_tree          (Folder                 *cur_folder,
95                                          FolderSelectionType     type);
96
97 static gboolean foldersel_selected      (GtkTreeSelection *selection,
98                                          GtkTreeModel     *model,
99                                          GtkTreePath      *path,
100                                          gboolean          currently_selected,
101                                          gpointer          data);
102
103 static void foldersel_ok                (GtkButton      *button,
104                                          gpointer        data);
105 static void foldersel_cancel            (GtkButton      *button,
106                                          gpointer        data);
107 static void foldersel_new_folder        (GtkButton      *button,
108                                          gpointer        data);
109
110 static void foldersel_entry_activated   (GtkEntry       *entry,
111                                          gpointer        data);
112
113 static void foldersel_tree_activated    (GtkTreeView            *treeview,
114                                          GtkTreePath            *path,
115                                          GtkTreeViewColumn      *column,
116                                          gpointer                data);
117
118 static gint delete_event                (GtkWidget      *widget,
119                                          GdkEventAny    *event,
120                                          gpointer        data);
121 static gboolean key_pressed             (GtkWidget      *widget,
122                                          GdkEventKey    *event,
123                                          gpointer        data);
124
125 static gint foldersel_folder_name_compare       (GtkTreeModel   *model,
126                                                  GtkTreeIter    *a,
127                                                  GtkTreeIter    *b,
128                                                  gpointer        context);
129
130 static gboolean tree_view_folder_item_func      (GtkTreeModel     *model,
131                                                  GtkTreePath      *path,
132                                                  GtkTreeIter      *iter,
133                                                  FolderItemSearch *data);
134
135 FolderItem *foldersel_folder_sel(Folder *cur_folder, FolderSelectionType type,
136                                  const gchar *default_folder, gboolean can_sel_mailbox)
137 {
138         selected_item = NULL;
139         root_selectable = can_sel_mailbox;
140
141         if (!window) {
142                 foldersel_create();
143                 foldersel_init();
144         }
145
146         foldersel_set_tree(cur_folder, type);
147
148         /* select current */
149         if (folder_item) {
150                 FolderItemSearch fis;
151
152                 fis.item = folder_item;
153                 fis.path = NULL;
154
155                 /* find matching model entry */
156                 gtk_tree_model_foreach
157                         (GTK_TREE_MODEL(tree_store),
158                          (GtkTreeModelForeachFunc)tree_view_folder_item_func,
159                          &fis);
160
161                 if (fis.path) {
162                         GtkTreeSelection *selection;
163
164                         selection = gtk_tree_view_get_selection
165                                 (GTK_TREE_VIEW(treeview));
166                         gtk_tree_selection_select_iter(selection, &fis.iter);
167                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview),
168                                                  fis.path, NULL, FALSE);
169                         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview),
170                                                      fis.path,
171                                                      NULL, TRUE, 0.5, 0.0);
172                         gtk_tree_path_free(fis.path);
173                 } else
174                         gtk_tree_view_scroll_to_point
175                                 (GTK_TREE_VIEW(treeview), 0, 0);
176         } else
177                 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(treeview), 0, 0);
178
179         gtk_widget_grab_focus(ok_button);
180         gtk_widget_grab_focus(treeview);
181
182         gtk_window_present(GTK_WINDOW(window));
183         manage_window_set_transient(GTK_WINDOW(window));
184         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
185
186         cancelled = finished = FALSE;
187
188         while (finished == FALSE)
189                 gtk_main_iteration();
190
191         gtk_widget_destroy(window);
192         window = NULL;
193
194         if (!cancelled &&
195             selected_item && (selected_item->path || root_selectable)) {
196                 folder_item = selected_item;
197                 return folder_item;
198         } else
199                 return NULL;
200 }
201
202 static gboolean foldersel_search_name_func(GtkTreeModel *model, gint column,
203                 const gchar *key, GtkTreeIter *iter, gpointer search_data)
204 {
205         gchar *store_string = NULL;
206         FolderItem *item;
207         gboolean retval;
208
209         if (column == FOLDERSEL_FOLDERNAME) {
210                 /* get the name of the FolderItem, not the displayed string */
211                 gtk_tree_model_get(model, iter,
212                            FOLDERSEL_FOLDERITEM, &item, -1);
213                 store_string = folder_item_get_name(item);
214         } else {
215                 gtk_tree_model_get(model, iter, column, &store_string, -1);
216         }
217
218         if (!store_string || !key)
219                 return FALSE;
220
221         retval = (strcasestr(store_string, key) == NULL);
222
223         g_free(store_string);
224
225         return retval;
226 }
227
228 static void foldersel_size_allocate_cb(GtkWidget *widget,
229                                          GtkAllocation *allocation)
230 {
231         cm_return_if_fail(allocation != NULL);
232
233         prefs_common.folderselwin_width = allocation->width;
234         prefs_common.folderselwin_height = allocation->height;
235 }
236
237 static void foldersel_create(void)
238 {
239         GtkWidget *vbox;
240         GtkWidget *scrolledwin;
241         GtkWidget *confirm_area;
242         GtkTreeViewColumn *column;
243         GtkCellRenderer *renderer;
244         GtkTreeSelection *selection;
245         static GdkGeometry geometry;
246
247         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "foldersel");
248         gtk_window_set_title(GTK_WINDOW(window), _("Select folder"));
249         gtk_container_set_border_width(GTK_CONTAINER(window), 4);
250         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
251         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
252
253         gtk_widget_realize(window);
254         g_signal_connect(G_OBJECT(window), "delete_event",
255                          G_CALLBACK(delete_event), NULL);
256         g_signal_connect(G_OBJECT(window), "key_press_event",
257                          G_CALLBACK(key_pressed), NULL);
258         g_signal_connect(G_OBJECT(window), "size_allocate",
259                          G_CALLBACK(foldersel_size_allocate_cb), NULL);
260         MANAGE_WINDOW_SIGNALS_CONNECT(window);
261
262         vbox = gtk_vbox_new(FALSE, 4);
263         gtk_container_add(GTK_CONTAINER(window), vbox);
264
265         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
266         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
267                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
268         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
269                                             GTK_SHADOW_IN);
270         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
271
272         tree_store = gtk_tree_store_new(N_FOLDERSEL_COLUMNS,
273                                         G_TYPE_STRING,
274                                         G_TYPE_POINTER,
275                                         GDK_TYPE_PIXBUF,
276                                         GDK_TYPE_PIXBUF,
277                                         GDK_TYPE_COLOR,
278                                         G_TYPE_INT);
279         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(tree_store),
280                                         FOLDERSEL_FOLDERNAME,
281                                         foldersel_folder_name_compare,
282                                         NULL, NULL);
283
284         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
285         g_object_unref(G_OBJECT(tree_store));
286         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
287         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview),
288                                      prefs_common.use_stripes_everywhere);
289         gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview), FALSE);
290         gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview),
291                                         FOLDERSEL_FOLDERNAME);
292         if (prefs_common.folder_search_wildcard)
293                 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(treeview),
294                                 foldersel_search_name_func, NULL, NULL);
295
296         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
297         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
298         gtk_tree_selection_set_select_function(selection, foldersel_selected,
299                                                NULL, NULL);
300
301         g_signal_connect(G_OBJECT(treeview), "row-activated",
302                          G_CALLBACK(foldersel_tree_activated), NULL);
303         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
304
305         column = gtk_tree_view_column_new();
306         gtk_tree_view_column_set_spacing(column, 2);
307         renderer = gtk_cell_renderer_pixbuf_new();
308         gtk_tree_view_column_pack_start(column, renderer, FALSE);
309         gtk_tree_view_column_set_attributes
310                 (column, renderer,
311                  "pixbuf", FOLDERSEL_PIXBUF,
312                  "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN,
313                  "pixbuf-expander-closed", FOLDERSEL_PIXBUF,
314                  NULL);
315
316         /* create text renderer */
317         renderer = gtk_cell_renderer_text_new();
318         gtk_tree_view_column_pack_start(column, renderer, TRUE);
319         gtk_tree_view_column_set_attributes
320                 (column, renderer,
321                  "text", FOLDERSEL_FOLDERNAME,
322                  "foreground-gdk", FOLDERSEL_FOREGROUND,
323                  "weight", FOLDERSEL_BOLD,
324                  NULL);
325         g_object_set(G_OBJECT(renderer), "weight", PANGO_WEIGHT_BOLD, NULL);
326         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
327
328         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
329
330         entry = gtk_entry_new();
331         gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
332         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
333         g_signal_connect(G_OBJECT(entry), "activate",
334                          G_CALLBACK(foldersel_entry_activated), NULL);
335
336         gtkut_stock_button_set_create(&confirm_area,
337                                       &new_button,    GTK_STOCK_NEW,
338                                       &cancel_button, GTK_STOCK_CANCEL,
339                                       &ok_button,     GTK_STOCK_OK);
340
341         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
342         gtk_widget_grab_default(ok_button);
343
344         g_signal_connect(G_OBJECT(ok_button), "clicked",
345                          G_CALLBACK(foldersel_ok), NULL);
346         g_signal_connect(G_OBJECT(cancel_button), "clicked",
347                          G_CALLBACK(foldersel_cancel), NULL);
348         g_signal_connect(G_OBJECT(new_button), "clicked",
349                          G_CALLBACK(foldersel_new_folder), NULL);
350
351         if (!geometry.min_height) {
352                 geometry.min_width = 300;
353                 geometry.min_height = 360;
354         }
355
356         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
357                                       GDK_HINT_MIN_SIZE);
358         gtk_widget_set_size_request(window, prefs_common.folderselwin_width,
359                                     prefs_common.folderselwin_height);
360
361         gtk_widget_show_all(vbox);
362 }
363
364 static void foldersel_init(void)
365 {
366         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_CLOSE, &folder_pixbuf);
367         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN, &folderopen_pixbuf);
368         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_CLOSE, &foldernoselect_pixbuf);
369         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_OPEN, &foldernoselectopen_pixbuf);
370 }
371
372 void foldersel_reflect_prefs_pixmap_theme(void)
373 {
374         if (folder_pixbuf)
375                 g_object_unref(folder_pixbuf);
376         if (folderopen_pixbuf)
377                 g_object_unref(folderopen_pixbuf);
378         if (foldernoselect_pixbuf)
379                 g_object_unref(foldernoselect_pixbuf);
380         if (foldernoselectopen_pixbuf)
381                 g_object_unref(foldernoselectopen_pixbuf);
382         foldersel_init();
383 }
384
385 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
386                                   GtkTreeIter *iter, GtkTreeIter *parent)
387 {
388         gchar *name, *tmpname;
389         GdkPixbuf *pixbuf, *pixbuf_open;
390         gboolean use_color;
391         PangoWeight weight = PANGO_WEIGHT_NORMAL;
392         GdkColor *foreground = NULL;
393         static GdkColor color_noselect = {0, COLOR_DIM, COLOR_DIM, COLOR_DIM};
394         static GdkColor color_new;
395
396         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &color_new);
397
398         name = tmpname = folder_item_get_name(item);
399
400         if (item->stype != F_NORMAL && FOLDER_IS_LOCAL(item->folder)) {
401                 switch (item->stype) {
402                 case F_INBOX:
403                         if (!strcmp2(item->name, INBOX_DIR))
404                                 name = _("Inbox");
405                         break;
406                 case F_OUTBOX:
407                         if (!strcmp2(item->name, OUTBOX_DIR))
408                                 name = _("Sent");
409                         break;
410                 case F_QUEUE:
411                         if (!strcmp2(item->name, QUEUE_DIR))
412                                 name = _("Queue");
413                         break;
414                 case F_TRASH:
415                         if (!strcmp2(item->name, TRASH_DIR))
416                                 name = _("Trash");
417                         break;
418                 case F_DRAFT:
419                         if (!strcmp2(item->name, DRAFT_DIR))
420                                 name = _("Drafts");
421                         break;
422                 default:
423                         break;
424                 }
425         }
426
427         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
428                 name = g_strdup_printf("%s (%d)", name, item->total_msgs);
429         } else if (item->unread_msgs > 0) {
430                 name = g_strdup_printf("%s (%d)", name, item->unread_msgs);
431         } else
432                 name = g_strdup(name);
433
434         pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
435         pixbuf_open =
436                 item->no_select ? foldernoselectopen_pixbuf : folderopen_pixbuf;
437
438         if (folder_has_parent_of_type(item, F_DRAFT) ||
439             folder_has_parent_of_type(item, F_OUTBOX) ||
440             folder_has_parent_of_type(item, F_TRASH)) {
441                 use_color = FALSE;
442         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
443                 use_color = (item->total_msgs > 0);
444                 if (item->total_msgs > 0)
445                         weight = PANGO_WEIGHT_BOLD;
446         } else {
447                 if (item->unread_msgs > 0)
448                         weight = PANGO_WEIGHT_BOLD;
449                 use_color = (item->new_msgs > 0);
450         }
451
452         if (item->no_select)
453                 foreground = &color_noselect;
454         else if (use_color)
455                 foreground = &color_new;
456
457         /* insert this node */
458         gtk_tree_store_append(store, iter, parent);
459         gtk_tree_store_set(store, iter,
460                            FOLDERSEL_FOLDERNAME, name,
461                            FOLDERSEL_FOLDERITEM, item,
462                            FOLDERSEL_PIXBUF, pixbuf,
463                            FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
464                            FOLDERSEL_FOREGROUND, foreground,
465                            FOLDERSEL_BOLD, weight,
466                            -1);
467         
468         g_free(tmpname);
469 }
470
471 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
472                                             GtkTreeIter *parent)
473 {
474         FolderItem *item;
475         GtkTreeIter child;
476         GNode *iter;
477
478         cm_return_if_fail(node != NULL);
479         cm_return_if_fail(node->data != NULL);
480         cm_return_if_fail(store != NULL);
481
482         item = FOLDER_ITEM(node->data);
483         foldersel_append_item(store, item, &child, parent);
484
485         /* insert its children (this node as parent) */
486         for (iter = node->children; iter != NULL; iter = iter->next)
487                 foldersel_insert_gnode_in_store(store, iter, &child);
488 }
489
490 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
491 {
492         Folder *folder;
493         GList *list;
494
495         for (list = folder_get_list(); list != NULL; list = list->next) {
496                 folder = FOLDER(list->data);
497                 cm_return_if_fail(folder != NULL);
498
499                 if (type != FOLDER_SEL_ALL) {
500                         if (FOLDER_TYPE(folder) == F_NEWS)
501                                 continue;
502                 }
503                 
504                 if (cur_folder && (cur_folder->klass != folder->klass
505                     && strcmp2(cur_folder->name, folder->name) != 0))
506                     continue;
507                 
508                 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
509         }
510
511         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
512                                              FOLDERSEL_FOLDERNAME,
513                                              GTK_SORT_ASCENDING);
514
515         gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
516 }
517
518 #include "localfolder.h"
519 static gboolean foldersel_selected(GtkTreeSelection *selection,
520                                    GtkTreeModel *model, GtkTreePath *path,
521                                    gboolean currently_selected, gpointer data)
522 {
523         GtkTreeIter iter;
524         FolderItem *item = NULL;
525
526         if (currently_selected)
527                 return TRUE;
528
529         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
530                 return TRUE;
531
532         gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
533                            FOLDERSEL_FOLDERITEM, &item, -1);
534
535         selected_item = item;
536         if (selected_item && selected_item->path) {
537                 gchar *id;
538                 id = folder_item_get_identifier(selected_item);
539                 gtk_entry_set_text(GTK_ENTRY(entry), id);
540                 g_free(id);
541         } else
542         if (root_selectable && selected_item && selected_item->folder &&
543                         (FOLDER_TYPE(selected_item->folder) == F_MH ||
544                          FOLDER_TYPE(selected_item->folder) == F_MBOX ||
545                          FOLDER_TYPE(selected_item->folder) == F_IMAP)) {
546                 gchar *id = folder_get_identifier(selected_item->folder);
547                 gtk_entry_set_text(GTK_ENTRY(entry), id);
548                 g_free(id);
549         } else
550                 gtk_entry_set_text(GTK_ENTRY(entry), "");
551
552         return TRUE;
553 }
554
555 static void foldersel_ok(GtkButton *button, gpointer data)
556 {
557         finished = TRUE;
558 }
559
560 static void foldersel_cancel(GtkButton *button, gpointer data)
561 {
562         cancelled = TRUE;
563         finished = TRUE;
564 }
565
566 static void foldersel_new_folder(GtkButton *button, gpointer data)
567 {
568         FolderItem *new_item;
569         gchar *new_folder;
570         gchar *disp_name;
571         gchar *p;
572         GtkTreeIter selected, new_child;
573         GtkTreePath *selected_p, *new_child_p;
574         GtkTreeStore *store;
575         GtkTreeModel *model;
576         GtkTreeSelection *selection;
577
578         if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
579                 return;
580
581         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
582         if (!gtk_tree_selection_get_selected(selection, &model, &selected))
583                 return;
584         store = GTK_TREE_STORE(model);
585
586         new_folder = input_dialog(_("New folder"),
587                                   _("Input the name of new folder:"),
588                                   _("NewFolder"));
589         if (!new_folder) return;
590         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
591
592         p = strchr(new_folder, G_DIR_SEPARATOR);
593         if ((p && FOLDER_TYPE(selected_item->folder) != F_IMAP) ||
594             (p && FOLDER_TYPE(selected_item->folder) == F_IMAP &&
595              *(p + 1) != '\0')) {
596                 alertpanel_error(_("'%c' can't be included in folder name."),
597                                 G_DIR_SEPARATOR);
598                 return;
599         }
600
601         if (FOLDER_TYPE(selected_item->folder) != F_IMAP &&
602                         FOLDER_TYPE(selected_item->folder) != F_NEWS &&
603                         !folder_local_name_ok(new_folder))
604                 return;
605
606         disp_name = trim_string(new_folder, 32);
607         AUTORELEASE_STR(disp_name, {g_free(disp_name); return;});
608
609         /* find whether the directory already exists */
610         if (folder_find_child_item_by_name(selected_item, new_folder)) {
611                 alertpanel_error(_("The folder '%s' already exists."),
612                                  disp_name);
613                 return;
614         }
615
616         new_item = folder_create_folder(selected_item, new_folder);
617         if (!new_item) {
618                 alertpanel_error(_("Can't create the folder '%s'."), disp_name);
619                 return;
620         }
621
622         /* add new child */
623         foldersel_append_item(store, new_item, &new_child, &selected);
624         
625         selected_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &selected);
626         new_child_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store),
627                                               &new_child);
628
629         gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), selected_p, FALSE);
630         gtk_tree_selection_select_iter(selection, &new_child);
631         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), new_child_p,
632                                      NULL, TRUE, 0.5, 0.0);
633         gtk_tree_path_free(new_child_p);
634         gtk_tree_path_free(selected_p);
635
636         folder_write_list();
637 }
638
639 static void foldersel_entry_activated(GtkEntry *entry, gpointer data)
640 {
641         gtk_button_clicked(GTK_BUTTON(ok_button));
642 }
643
644 static void foldersel_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
645                                      GtkTreeViewColumn *column, gpointer data)
646 {
647         gtk_button_clicked(GTK_BUTTON(ok_button));
648 }
649
650 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
651 {
652         foldersel_cancel(NULL, NULL);
653         return TRUE;
654 }
655
656 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
657 {
658         if (!event)
659                 return FALSE;
660
661         if (event->keyval == GDK_KEY_Escape) {
662                 foldersel_cancel(NULL, NULL);
663                 return TRUE;
664         }
665
666         GtkTreePath *path = NULL;
667         gtk_tree_view_get_cursor(GTK_TREE_VIEW(treeview), &path, NULL);
668         if (path == NULL)
669                 return FALSE;
670
671         switch (event->keyval) {
672                 case GDK_KEY_Left:
673                         if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(treeview), path)) {
674                                 gtk_tree_view_collapse_row(GTK_TREE_VIEW(treeview), path);
675                         } else {
676                                 gtk_tree_path_up(path);
677                                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), path, NULL, FALSE);
678                         }
679                         return TRUE;
680                         break;
681                 case GDK_KEY_Right:
682                         if (!gtk_tree_view_row_expanded(GTK_TREE_VIEW(treeview), path)) {
683                                 gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), path, FALSE);
684                         } else {
685                                 gtk_tree_path_down(path);
686                                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), path, NULL, FALSE);
687                         }
688                         return TRUE;
689                         break;
690         }
691
692         gtk_tree_path_free(path);
693
694         return FALSE;
695 }
696
697 static gint foldersel_folder_name_compare(GtkTreeModel *model, GtkTreeIter *a,
698                                           GtkTreeIter *b, gpointer context)
699 {
700         gchar *str_a = NULL, *str_b = NULL;
701         gint val = 0;
702         FolderItem *item_a = NULL, *item_b = NULL;
703         GtkTreeIter parent;
704
705         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERITEM, &item_a, -1);
706         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERITEM, &item_b, -1);
707
708         /* no sort for root folder */
709         if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model), &parent, a))
710                 return 0;
711
712         /* if both a and b are special folders, sort them according to
713          * their types (which is in-order). Note that this assumes that
714          * there are no multiple folders of a special type. */
715         if (item_a->stype != F_NORMAL && item_b->stype != F_NORMAL)
716                 return item_a->stype - item_b->stype;
717
718         /* if b is normal folder, and a is not, b is smaller (ends up
719          * lower in the list) */
720         if (item_a->stype != F_NORMAL && item_b->stype == F_NORMAL)
721                 return item_b->stype - item_a->stype;
722
723         /* if b is special folder, and a is not, b is larger (ends up
724          * higher in the list) */
725         if (item_a->stype == F_NORMAL && item_b->stype != F_NORMAL)
726                 return item_b->stype - item_a->stype;
727
728         /* XXX g_utf8_collate_key() comparisons may speed things
729          * up when having large lists of folders */
730         gtk_tree_model_get(model, a, FOLDERSEL_FOLDERNAME, &str_a, -1);
731         gtk_tree_model_get(model, b, FOLDERSEL_FOLDERNAME, &str_b, -1);
732
733         /* otherwise just compare the folder names */
734         val = g_utf8_collate(str_a, str_b);
735
736         g_free(str_a);
737         g_free(str_b);
738
739         return val;
740 }
741
742 static gboolean tree_view_folder_item_func(GtkTreeModel *model,
743                                            GtkTreePath *path,
744                                            GtkTreeIter *iter,
745                                            FolderItemSearch *data)
746 {
747         FolderItem *item = NULL;
748
749         gtk_tree_model_get(model, iter, FOLDERSEL_FOLDERITEM, &item, -1);
750
751         if (data->item == item) {
752                 data->path = gtk_tree_path_copy(path);
753                 data->iter = *iter;
754                 return TRUE;
755         }
756
757         return FALSE;
758 }