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