Fix CID 1596595: Resource leak
[claws.git] / src / foldersel.c
blobfab33691a678258013d67794f2dcfb0c45bee666
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 the Claws Mail team and Hiroyuki Yamamoto
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.
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.
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/>.
19 #include "config.h"
20 #include "defs.h"
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>
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 #include "folder_item_prefs.h"
47 enum {
48 FOLDERSEL_FOLDERNAME,
49 FOLDERSEL_FOLDERITEM,
50 FOLDERSEL_PIXBUF,
51 FOLDERSEL_PIXBUF_OPEN,
52 FOLDERSEL_FOREGROUND,
53 FOLDERSEL_BOLD,
54 N_FOLDERSEL_COLUMNS
57 typedef struct _FolderItemSearch FolderItemSearch;
59 struct _FolderItemSearch
61 FolderItem *item;
62 GtkTreePath *path;
63 GtkTreeIter iter;
66 static GdkPixbuf *folder_pixbuf = NULL;
67 static GdkPixbuf *folderopen_pixbuf = NULL;
68 static GdkPixbuf *foldernoselect_pixbuf = NULL;
69 static GdkPixbuf *foldernoselectopen_pixbuf = NULL;
71 static GtkWidget *window;
72 static GtkWidget *treeview;
73 static GtkWidget *statusbar;
74 static gint statusbar_cid;
75 static GtkWidget *ok_button;
76 static GtkWidget *cancel_button;
77 static GtkWidget *new_button;
78 static gboolean root_selectable;
80 static FolderItem *folder_item;
81 static FolderItem *selected_item;
83 static GtkTreeStore *tree_store;
85 static gboolean cancelled;
86 static gboolean finished;
88 static void foldersel_create (const gchar *title);
89 static void foldersel_init (void);
91 static void foldersel_append_item (GtkTreeStore *store,
92 FolderItem *item,
93 GtkTreeIter *iter,
94 GtkTreeIter *parent);
96 static void foldersel_set_tree (Folder *cur_folder,
97 FolderSelectionType type);
99 static gboolean foldersel_selected (GtkTreeSelection *selection,
100 GtkTreeModel *model,
101 GtkTreePath *path,
102 gboolean currently_selected,
103 gpointer data);
105 static void foldersel_ok (GtkButton *button,
106 gpointer data);
107 static void foldersel_cancel (GtkButton *button,
108 gpointer data);
109 static void foldersel_new_folder (GtkButton *button,
110 gpointer data);
112 static void foldersel_tree_activated (GtkTreeView *treeview,
113 GtkTreePath *path,
114 GtkTreeViewColumn *column,
115 gpointer data);
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);
124 static gint foldersel_folder_name_compare (GtkTreeModel *model,
125 GtkTreeIter *a,
126 GtkTreeIter *b,
127 gpointer context);
129 static gboolean tree_view_folder_item_func (GtkTreeModel *model,
130 GtkTreePath *path,
131 GtkTreeIter *iter,
132 FolderItemSearch *data);
134 FolderItem *foldersel_folder_sel(Folder *cur_folder, FolderSelectionType type,
135 const gchar *default_folder, gboolean can_sel_mailbox,
136 const gchar *title)
138 selected_item = NULL;
139 root_selectable = can_sel_mailbox;
141 if (!window) {
142 foldersel_create(title);
143 foldersel_init();
146 foldersel_set_tree(cur_folder, type);
148 /* select current */
149 if (folder_item) {
150 FolderItemSearch fis;
152 fis.item = folder_item;
153 fis.path = NULL;
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);
161 if (fis.path) {
162 GtkTreeSelection *selection;
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);
179 gtk_widget_grab_focus(ok_button);
180 gtk_widget_grab_focus(treeview);
182 gtk_window_present(GTK_WINDOW(window));
183 manage_window_set_transient(GTK_WINDOW(window));
184 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
186 cancelled = finished = FALSE;
188 while (finished == FALSE)
189 gtk_main_iteration();
191 gtk_widget_destroy(window);
192 window = NULL;
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;
202 static gboolean foldersel_search_name_func(GtkTreeModel *model, gint column,
203 const gchar *key, GtkTreeIter *iter, gpointer search_data)
205 gchar *store_string = NULL;
206 FolderItem *item;
207 gboolean retval;
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);
218 if (!store_string)
219 return FALSE;
221 if (!key)
222 retval = FALSE;
223 else
224 retval = (strcasestr(store_string, key) == NULL);
226 g_free(store_string);
228 return retval;
231 static void foldersel_size_allocate_cb(GtkWidget *widget,
232 GtkAllocation *allocation)
234 cm_return_if_fail(allocation != NULL);
236 gtk_window_get_size(GTK_WINDOW(widget),
237 &prefs_common.folderselwin_width, &prefs_common.folderselwin_height);
240 static void foldersel_create(const gchar *title)
242 GtkWidget *vbox;
243 GtkWidget *scrolledwin;
244 GtkWidget *confirm_area;
245 GtkWidget *label;
246 GtkTreeViewColumn *column;
247 GtkCellRenderer *renderer;
248 GtkTreeSelection *selection;
249 static GdkGeometry geometry;
251 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "foldersel");
252 gtk_window_set_title(GTK_WINDOW(window),_("Select folder"));
253 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
254 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
255 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
256 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
258 gtk_widget_realize(window);
259 g_signal_connect(G_OBJECT(window), "delete_event",
260 G_CALLBACK(delete_event), NULL);
261 g_signal_connect(G_OBJECT(window), "key_press_event",
262 G_CALLBACK(key_pressed), NULL);
263 g_signal_connect(G_OBJECT(window), "size_allocate",
264 G_CALLBACK(foldersel_size_allocate_cb), NULL);
265 MANAGE_WINDOW_SIGNALS_CONNECT(window);
267 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
268 gtk_container_add(GTK_CONTAINER(window), vbox);
270 if (title != NULL) {
271 label = gtk_label_new(title);
272 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
273 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
276 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
277 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
278 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
279 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
280 GTK_SHADOW_IN);
281 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
283 tree_store = gtk_tree_store_new(N_FOLDERSEL_COLUMNS,
284 G_TYPE_STRING,
285 G_TYPE_POINTER,
286 GDK_TYPE_PIXBUF,
287 GDK_TYPE_PIXBUF,
288 GDK_TYPE_RGBA,
289 G_TYPE_INT);
290 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(tree_store),
291 FOLDERSEL_FOLDERNAME,
292 foldersel_folder_name_compare,
293 NULL, NULL);
295 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
296 g_object_unref(G_OBJECT(tree_store));
297 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
298 gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview), FALSE);
299 gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview),
300 FOLDERSEL_FOLDERNAME);
301 if (prefs_common.folder_search_wildcard)
302 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(treeview),
303 foldersel_search_name_func, NULL, NULL);
305 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
306 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
307 gtk_tree_selection_set_select_function(selection, foldersel_selected,
308 NULL, NULL);
310 g_signal_connect(G_OBJECT(treeview), "row-activated",
311 G_CALLBACK(foldersel_tree_activated), NULL);
312 gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
314 column = gtk_tree_view_column_new();
315 gtk_tree_view_column_set_spacing(column, 2);
316 renderer = gtk_cell_renderer_pixbuf_new();
317 gtk_tree_view_column_pack_start(column, renderer, FALSE);
318 gtk_tree_view_column_set_attributes
319 (column, renderer,
320 "pixbuf", FOLDERSEL_PIXBUF,
321 "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN,
322 "pixbuf-expander-closed", FOLDERSEL_PIXBUF,
323 NULL);
325 /* create text renderer */
326 renderer = gtk_cell_renderer_text_new();
327 gtk_cell_renderer_set_padding(renderer, 0, 0);
328 gtk_tree_view_column_pack_start(column, renderer, TRUE);
329 gtk_tree_view_column_set_attributes
330 (column, renderer,
331 "text", FOLDERSEL_FOLDERNAME,
332 "foreground-rgba", FOLDERSEL_FOREGROUND,
333 "weight", FOLDERSEL_BOLD,
334 NULL);
335 g_object_set(G_OBJECT(renderer), "weight", PANGO_WEIGHT_BOLD, NULL);
336 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
338 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
340 statusbar = gtk_statusbar_new();
341 statusbar_cid = gtk_statusbar_get_context_id(
342 GTK_STATUSBAR(statusbar), "Select Folder Dialog" );
343 gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
345 gtkut_stock_button_set_create(&confirm_area,
346 &new_button, NULL, _("_New"),
347 &cancel_button, NULL, _("_Cancel"),
348 &ok_button, NULL, _("_OK"));
350 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
351 gtk_widget_grab_default(ok_button);
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);
360 if (!geometry.min_height) {
361 geometry.min_width = 300;
362 geometry.min_height = 360;
365 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
366 GDK_HINT_MIN_SIZE);
367 gtk_window_set_default_size(GTK_WINDOW(window),
368 prefs_common.folderselwin_width,
369 prefs_common.folderselwin_height);
371 gtk_widget_show_all(vbox);
374 static void foldersel_init(void)
376 stock_pixbuf_gdk(STOCK_PIXMAP_DIR_CLOSE, &folder_pixbuf);
377 stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN, &folderopen_pixbuf);
378 stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_CLOSE, &foldernoselect_pixbuf);
379 stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_OPEN, &foldernoselectopen_pixbuf);
382 void foldersel_reflect_prefs_pixmap_theme(void)
384 if (folder_pixbuf)
385 g_object_unref(folder_pixbuf);
386 if (folderopen_pixbuf)
387 g_object_unref(folderopen_pixbuf);
388 if (foldernoselect_pixbuf)
389 g_object_unref(foldernoselect_pixbuf);
390 if (foldernoselectopen_pixbuf)
391 g_object_unref(foldernoselectopen_pixbuf);
392 foldersel_init();
395 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
396 GtkTreeIter *iter, GtkTreeIter *parent)
398 gchar *name, *tmpname;
399 GdkPixbuf *pixbuf, *pixbuf_open;
400 gboolean use_color;
401 PangoWeight weight = PANGO_WEIGHT_NORMAL;
402 GdkRGBA *foreground = NULL;
403 GdkRGBA color_noselect = {COLOR_DIM, COLOR_DIM, COLOR_DIM, 1.0};
405 name = folder_item_get_name(item);
407 if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
408 tmpname = g_strdup_printf("%s (%d)", name, item->total_msgs);
409 } else if (item->unread_msgs > 0) {
410 tmpname = g_strdup_printf("%s (%d)", name, item->unread_msgs);
411 } else
412 tmpname = g_strdup(name);
414 g_free(name);
415 name = tmpname;
417 pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
418 pixbuf_open =
419 item->no_select ? foldernoselectopen_pixbuf : folderopen_pixbuf;
421 if (folder_has_parent_of_type(item, F_DRAFT) ||
422 folder_has_parent_of_type(item, F_OUTBOX) ||
423 folder_has_parent_of_type(item, F_TRASH)) {
424 use_color = FALSE;
425 } else if (folder_has_parent_of_type(item, F_QUEUE)) {
426 use_color = (item->total_msgs > 0);
427 if (item->total_msgs > 0)
428 weight = PANGO_WEIGHT_BOLD;
429 } else {
430 if (item->unread_msgs > 0)
431 weight = PANGO_WEIGHT_BOLD;
432 use_color = (item->new_msgs > 0);
435 if (item->no_select)
436 foreground = &color_noselect;
437 else if (use_color)
438 foreground = &prefs_common.color[COL_NEW];
440 /* insert this node */
441 gtk_tree_store_append(store, iter, parent);
442 gtk_tree_store_set(store, iter,
443 FOLDERSEL_FOLDERNAME, name,
444 FOLDERSEL_FOLDERITEM, item,
445 FOLDERSEL_PIXBUF, pixbuf,
446 FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
447 FOLDERSEL_FOREGROUND, foreground,
448 FOLDERSEL_BOLD, weight,
449 -1);
451 g_free(name);
454 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
455 GtkTreeIter *parent)
457 FolderItem *item;
458 GtkTreeIter child;
459 GNode *iter;
461 cm_return_if_fail(node != NULL);
462 cm_return_if_fail(node->data != NULL);
463 cm_return_if_fail(store != NULL);
465 item = FOLDER_ITEM(node->data);
466 foldersel_append_item(store, item, &child, parent);
468 /* insert its children (this node as parent) */
469 for (iter = node->children; iter != NULL; iter = iter->next)
470 foldersel_insert_gnode_in_store(store, iter, &child);
473 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
475 Folder *folder;
476 GList *list;
478 for (list = folder_get_list(); list != NULL; list = list->next) {
479 folder = FOLDER(list->data);
480 cm_return_if_fail(folder != NULL);
482 if (type != FOLDER_SEL_ALL) {
483 if (FOLDER_TYPE(folder) == F_NEWS)
484 continue;
487 if (cur_folder && (cur_folder->klass != folder->klass
488 && g_strcmp0(cur_folder->name, folder->name) != 0))
489 continue;
491 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
494 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
495 FOLDERSEL_FOLDERNAME,
496 GTK_SORT_ASCENDING);
498 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
501 #include "localfolder.h"
502 static gboolean foldersel_selected(GtkTreeSelection *selection,
503 GtkTreeModel *model, GtkTreePath *path,
504 gboolean currently_selected, gpointer data)
506 GtkTreeIter iter;
507 FolderItem *item = NULL;
509 if (currently_selected)
510 return TRUE;
512 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
513 return TRUE;
515 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
516 FOLDERSEL_FOLDERITEM, &item, -1);
518 selected_item = item;
519 if (selected_item && selected_item->path) {
520 gchar *id;
521 id = folder_item_get_identifier(selected_item);
522 gtk_statusbar_push(GTK_STATUSBAR(statusbar), statusbar_cid, id);
523 g_free(id);
524 } else if (root_selectable && selected_item && selected_item->folder &&
525 (FOLDER_TYPE(selected_item->folder) == F_MH ||
526 FOLDER_TYPE(selected_item->folder) == F_MBOX ||
527 FOLDER_TYPE(selected_item->folder) == F_IMAP)) {
528 gchar *id = folder_get_identifier(selected_item->folder);
529 gtk_statusbar_push(GTK_STATUSBAR(statusbar), statusbar_cid, id);
530 g_free(id);
531 } else
532 gtk_statusbar_push(GTK_STATUSBAR(statusbar), statusbar_cid, "");
534 return TRUE;
537 static void foldersel_ok(GtkButton *button, gpointer data)
539 finished = TRUE;
542 static void foldersel_cancel(GtkButton *button, gpointer data)
544 cancelled = TRUE;
545 finished = TRUE;
548 static void foldersel_new_folder(GtkButton *button, gpointer data)
550 FolderItem *new_item;
551 gchar *new_folder;
552 gchar *disp_name;
553 gchar *p;
554 GtkTreeIter selected, new_child;
555 GtkTreePath *selected_p, *new_child_p;
556 GtkTreeStore *store;
557 GtkTreeModel *model;
558 GtkTreeSelection *selection;
560 if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
561 return;
563 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
564 if (!gtk_tree_selection_get_selected(selection, &model, &selected))
565 return;
566 store = GTK_TREE_STORE(model);
568 new_folder = input_dialog_with_checkbtn(_("New folder"),
569 _("Input the name of new folder:"),
570 _("NewFolder"),
571 _("Inherit properties and processing rules from parent folder"),
572 &(prefs_common.inherit_folder_props));
574 if (!new_folder) return;
575 AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
577 p = strchr(new_folder, G_DIR_SEPARATOR);
578 if ((p && FOLDER_TYPE(selected_item->folder) != F_IMAP) ||
579 (p && FOLDER_TYPE(selected_item->folder) == F_IMAP &&
580 *(p + 1) != '\0')) {
581 alertpanel_error(_("'%c' can't be included in folder name."),
582 G_DIR_SEPARATOR);
583 return;
586 if (FOLDER_TYPE(selected_item->folder) != F_IMAP &&
587 FOLDER_TYPE(selected_item->folder) != F_NEWS &&
588 !folder_local_name_ok(new_folder))
589 return;
591 disp_name = trim_string(new_folder, 32);
592 AUTORELEASE_STR(disp_name, {g_free(disp_name); return;});
594 /* find whether the directory already exists */
595 if (folder_find_child_item_by_name(selected_item, new_folder)) {
596 alertpanel_error(_("The folder '%s' already exists."),
597 disp_name);
598 return;
601 new_item = folder_create_folder(selected_item, new_folder);
602 if (!new_item) {
603 alertpanel_error(_("Can't create the folder '%s'."), disp_name);
604 return;
607 if (prefs_common.inherit_folder_props) {
608 folder_item_prefs_copy_prefs(selected_item, new_item);
611 /* add new child */
612 foldersel_append_item(store, new_item, &new_child, &selected);
614 selected_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &selected);
615 new_child_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store),
616 &new_child);
618 gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), selected_p, FALSE);
619 gtk_tree_selection_select_iter(selection, &new_child);
620 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), new_child_p,
621 NULL, TRUE, 0.5, 0.0);
622 gtk_tree_path_free(new_child_p);
623 gtk_tree_path_free(selected_p);
625 folder_write_list();
628 static void foldersel_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
629 GtkTreeViewColumn *column, gpointer data)
631 gtk_button_clicked(GTK_BUTTON(ok_button));
634 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
636 foldersel_cancel(NULL, NULL);
637 return TRUE;
640 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
642 if (!event)
643 return FALSE;
645 if (event->keyval == GDK_KEY_Escape) {
646 foldersel_cancel(NULL, NULL);
647 return TRUE;
650 GtkTreePath *path = NULL;
651 gtk_tree_view_get_cursor(GTK_TREE_VIEW(treeview), &path, NULL);
652 if (path == NULL)
653 return FALSE;
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);
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);
672 return TRUE;
673 break;
676 gtk_tree_path_free(path);
678 return FALSE;
681 static gint foldersel_folder_name_compare(GtkTreeModel *model, GtkTreeIter *a,
682 GtkTreeIter *b, gpointer context)
684 gchar *str_a = NULL, *str_b = NULL;
685 gint val = 0;
686 FolderItem *item_a = NULL, *item_b = NULL;
687 GtkTreeIter parent;
689 gtk_tree_model_get(model, a, FOLDERSEL_FOLDERITEM, &item_a, -1);
690 gtk_tree_model_get(model, b, FOLDERSEL_FOLDERITEM, &item_b, -1);
692 /* no sort for root folder */
693 if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model), &parent, a))
694 return 0;
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;
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;
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;
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);
717 /* otherwise just compare the folder names */
718 val = g_utf8_collate(str_a, str_b);
720 g_free(str_a);
721 g_free(str_b);
723 return val;
726 static gboolean tree_view_folder_item_func(GtkTreeModel *model,
727 GtkTreePath *path,
728 GtkTreeIter *iter,
729 FolderItemSearch *data)
731 FolderItem *item = NULL;
733 gtk_tree_model_get(model, iter, FOLDERSEL_FOLDERITEM, &item, -1);
735 if (data->item == item) {
736 data->path = gtk_tree_path_copy(path);
737 data->iter = *iter;
738 return TRUE;
741 return FALSE;