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