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