Sync Spanish manual
[claws.git] / src / foldersel.c
blobfa89056624cbe38fd687917d5234f9de37aa2d64
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 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_rules_hint(GTK_TREE_VIEW(treeview),
299 prefs_common.use_stripes_everywhere);
300 gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview), FALSE);
301 gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview),
302 FOLDERSEL_FOLDERNAME);
303 if (prefs_common.folder_search_wildcard)
304 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(treeview),
305 foldersel_search_name_func, NULL, NULL);
307 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
308 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
309 gtk_tree_selection_set_select_function(selection, foldersel_selected,
310 NULL, NULL);
312 g_signal_connect(G_OBJECT(treeview), "row-activated",
313 G_CALLBACK(foldersel_tree_activated), NULL);
314 gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
316 column = gtk_tree_view_column_new();
317 gtk_tree_view_column_set_spacing(column, 2);
318 renderer = gtk_cell_renderer_pixbuf_new();
319 gtk_tree_view_column_pack_start(column, renderer, FALSE);
320 gtk_tree_view_column_set_attributes
321 (column, renderer,
322 "pixbuf", FOLDERSEL_PIXBUF,
323 "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN,
324 "pixbuf-expander-closed", FOLDERSEL_PIXBUF,
325 NULL);
327 /* create text renderer */
328 renderer = gtk_cell_renderer_text_new();
329 gtk_cell_renderer_set_padding(renderer, 0, 0);
330 gtk_tree_view_column_pack_start(column, renderer, TRUE);
331 gtk_tree_view_column_set_attributes
332 (column, renderer,
333 "text", FOLDERSEL_FOLDERNAME,
334 "foreground-rgba", FOLDERSEL_FOREGROUND,
335 "weight", FOLDERSEL_BOLD,
336 NULL);
337 g_object_set(G_OBJECT(renderer), "weight", PANGO_WEIGHT_BOLD, NULL);
338 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
340 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
342 statusbar = gtk_statusbar_new();
343 statusbar_cid = gtk_statusbar_get_context_id(
344 GTK_STATUSBAR(statusbar), "Select Folder Dialog" );
345 gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
347 gtkut_stock_button_set_create(&confirm_area,
348 &new_button, NULL, _("_New"),
349 &cancel_button, NULL, _("_Cancel"),
350 &ok_button, NULL, _("_OK"));
352 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
353 gtk_widget_grab_default(ok_button);
355 g_signal_connect(G_OBJECT(ok_button), "clicked",
356 G_CALLBACK(foldersel_ok), NULL);
357 g_signal_connect(G_OBJECT(cancel_button), "clicked",
358 G_CALLBACK(foldersel_cancel), NULL);
359 g_signal_connect(G_OBJECT(new_button), "clicked",
360 G_CALLBACK(foldersel_new_folder), NULL);
362 if (!geometry.min_height) {
363 geometry.min_width = 300;
364 geometry.min_height = 360;
367 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
368 GDK_HINT_MIN_SIZE);
369 gtk_window_set_default_size(GTK_WINDOW(window),
370 prefs_common.folderselwin_width,
371 prefs_common.folderselwin_height);
373 gtk_widget_show_all(vbox);
376 static void foldersel_init(void)
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);
384 void foldersel_reflect_prefs_pixmap_theme(void)
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();
397 static void foldersel_append_item(GtkTreeStore *store, FolderItem *item,
398 GtkTreeIter *iter, GtkTreeIter *parent)
400 gchar *name, *tmpname;
401 GdkPixbuf *pixbuf, *pixbuf_open;
402 gboolean use_color;
403 PangoWeight weight = PANGO_WEIGHT_NORMAL;
404 GdkRGBA *foreground = NULL;
405 GdkRGBA color_noselect = {COLOR_DIM, COLOR_DIM, COLOR_DIM, 1.0};
407 name = folder_item_get_name(item);
409 if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0) {
410 tmpname = g_strdup_printf("%s (%d)", name, item->total_msgs);
411 } else if (item->unread_msgs > 0) {
412 tmpname = g_strdup_printf("%s (%d)", name, item->unread_msgs);
413 } else
414 tmpname = g_strdup(name);
416 g_free(name);
417 name = tmpname;
419 pixbuf = item->no_select ? foldernoselect_pixbuf : folder_pixbuf;
420 pixbuf_open =
421 item->no_select ? foldernoselectopen_pixbuf : folderopen_pixbuf;
423 if (folder_has_parent_of_type(item, F_DRAFT) ||
424 folder_has_parent_of_type(item, F_OUTBOX) ||
425 folder_has_parent_of_type(item, F_TRASH)) {
426 use_color = FALSE;
427 } else if (folder_has_parent_of_type(item, F_QUEUE)) {
428 use_color = (item->total_msgs > 0);
429 if (item->total_msgs > 0)
430 weight = PANGO_WEIGHT_BOLD;
431 } else {
432 if (item->unread_msgs > 0)
433 weight = PANGO_WEIGHT_BOLD;
434 use_color = (item->new_msgs > 0);
437 if (item->no_select)
438 foreground = &color_noselect;
439 else if (use_color)
440 foreground = &prefs_common.color[COL_NEW];
442 /* insert this node */
443 gtk_tree_store_append(store, iter, parent);
444 gtk_tree_store_set(store, iter,
445 FOLDERSEL_FOLDERNAME, name,
446 FOLDERSEL_FOLDERITEM, item,
447 FOLDERSEL_PIXBUF, pixbuf,
448 FOLDERSEL_PIXBUF_OPEN, pixbuf_open,
449 FOLDERSEL_FOREGROUND, foreground,
450 FOLDERSEL_BOLD, weight,
451 -1);
453 g_free(name);
456 static void foldersel_insert_gnode_in_store(GtkTreeStore *store, GNode *node,
457 GtkTreeIter *parent)
459 FolderItem *item;
460 GtkTreeIter child;
461 GNode *iter;
463 cm_return_if_fail(node != NULL);
464 cm_return_if_fail(node->data != NULL);
465 cm_return_if_fail(store != NULL);
467 item = FOLDER_ITEM(node->data);
468 foldersel_append_item(store, item, &child, parent);
470 /* insert its children (this node as parent) */
471 for (iter = node->children; iter != NULL; iter = iter->next)
472 foldersel_insert_gnode_in_store(store, iter, &child);
475 static void foldersel_set_tree(Folder *cur_folder, FolderSelectionType type)
477 Folder *folder;
478 GList *list;
480 for (list = folder_get_list(); list != NULL; list = list->next) {
481 folder = FOLDER(list->data);
482 cm_return_if_fail(folder != NULL);
484 if (type != FOLDER_SEL_ALL) {
485 if (FOLDER_TYPE(folder) == F_NEWS)
486 continue;
489 if (cur_folder && (cur_folder->klass != folder->klass
490 && g_strcmp0(cur_folder->name, folder->name) != 0))
491 continue;
493 foldersel_insert_gnode_in_store(tree_store, folder->node, NULL);
496 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store),
497 FOLDERSEL_FOLDERNAME,
498 GTK_SORT_ASCENDING);
500 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview));
503 #include "localfolder.h"
504 static gboolean foldersel_selected(GtkTreeSelection *selection,
505 GtkTreeModel *model, GtkTreePath *path,
506 gboolean currently_selected, gpointer data)
508 GtkTreeIter iter;
509 FolderItem *item = NULL;
511 if (currently_selected)
512 return TRUE;
514 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
515 return TRUE;
517 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter,
518 FOLDERSEL_FOLDERITEM, &item, -1);
520 selected_item = item;
521 if (selected_item && selected_item->path) {
522 gchar *id;
523 id = folder_item_get_identifier(selected_item);
524 gtk_statusbar_push(GTK_STATUSBAR(statusbar), statusbar_cid, id);
525 g_free(id);
526 } else 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_statusbar_push(GTK_STATUSBAR(statusbar), statusbar_cid, id);
532 g_free(id);
533 } else
534 gtk_statusbar_push(GTK_STATUSBAR(statusbar), statusbar_cid, "");
536 return TRUE;
539 static void foldersel_ok(GtkButton *button, gpointer data)
541 finished = TRUE;
544 static void foldersel_cancel(GtkButton *button, gpointer data)
546 cancelled = TRUE;
547 finished = TRUE;
550 static void foldersel_new_folder(GtkButton *button, gpointer data)
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;
562 if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
563 return;
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);
570 new_folder = input_dialog_with_checkbtn(_("New folder"),
571 _("Input the name of new folder:"),
572 _("NewFolder"),
573 _("Inherit properties and processing rules from parent folder"),
574 &(prefs_common.inherit_folder_props));
576 if (!new_folder) return;
577 AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
579 p = strchr(new_folder, G_DIR_SEPARATOR);
580 if ((p && FOLDER_TYPE(selected_item->folder) != F_IMAP) ||
581 (p && FOLDER_TYPE(selected_item->folder) == F_IMAP &&
582 *(p + 1) != '\0')) {
583 alertpanel_error(_("'%c' can't be included in folder name."),
584 G_DIR_SEPARATOR);
585 return;
588 if (FOLDER_TYPE(selected_item->folder) != F_IMAP &&
589 FOLDER_TYPE(selected_item->folder) != F_NEWS &&
590 !folder_local_name_ok(new_folder))
591 return;
593 disp_name = trim_string(new_folder, 32);
594 AUTORELEASE_STR(disp_name, {g_free(disp_name); return;});
596 /* find whether the directory already exists */
597 if (folder_find_child_item_by_name(selected_item, new_folder)) {
598 alertpanel_error(_("The folder '%s' already exists."),
599 disp_name);
600 return;
603 new_item = folder_create_folder(selected_item, new_folder);
604 if (!new_item) {
605 alertpanel_error(_("Can't create the folder '%s'."), disp_name);
606 return;
609 if (prefs_common.inherit_folder_props) {
610 folder_item_prefs_copy_prefs(selected_item, new_item);
613 /* add new child */
614 foldersel_append_item(store, new_item, &new_child, &selected);
616 selected_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &selected);
617 new_child_p = gtk_tree_model_get_path(GTK_TREE_MODEL(store),
618 &new_child);
620 gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), selected_p, FALSE);
621 gtk_tree_selection_select_iter(selection, &new_child);
622 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), new_child_p,
623 NULL, TRUE, 0.5, 0.0);
624 gtk_tree_path_free(new_child_p);
625 gtk_tree_path_free(selected_p);
627 folder_write_list();
630 static void foldersel_tree_activated(GtkTreeView *treeview, GtkTreePath *path,
631 GtkTreeViewColumn *column, gpointer data)
633 gtk_button_clicked(GTK_BUTTON(ok_button));
636 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
638 foldersel_cancel(NULL, NULL);
639 return TRUE;
642 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
644 if (!event)
645 return FALSE;
647 if (event->keyval == GDK_KEY_Escape) {
648 foldersel_cancel(NULL, NULL);
649 return TRUE;
652 GtkTreePath *path = NULL;
653 gtk_tree_view_get_cursor(GTK_TREE_VIEW(treeview), &path, NULL);
654 if (path == NULL)
655 return FALSE;
657 switch (event->keyval) {
658 case GDK_KEY_Left:
659 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(treeview), path)) {
660 gtk_tree_view_collapse_row(GTK_TREE_VIEW(treeview), path);
661 } else {
662 gtk_tree_path_up(path);
663 gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), path, NULL, FALSE);
665 return TRUE;
666 break;
667 case GDK_KEY_Right:
668 if (!gtk_tree_view_row_expanded(GTK_TREE_VIEW(treeview), path)) {
669 gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), path, FALSE);
670 } else {
671 gtk_tree_path_down(path);
672 gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), path, NULL, FALSE);
674 return TRUE;
675 break;
678 gtk_tree_path_free(path);
680 return FALSE;
683 static gint foldersel_folder_name_compare(GtkTreeModel *model, GtkTreeIter *a,
684 GtkTreeIter *b, gpointer context)
686 gchar *str_a = NULL, *str_b = NULL;
687 gint val = 0;
688 FolderItem *item_a = NULL, *item_b = NULL;
689 GtkTreeIter parent;
691 gtk_tree_model_get(model, a, FOLDERSEL_FOLDERITEM, &item_a, -1);
692 gtk_tree_model_get(model, b, FOLDERSEL_FOLDERITEM, &item_b, -1);
694 /* no sort for root folder */
695 if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model), &parent, a))
696 return 0;
698 /* if both a and b are special folders, sort them according to
699 * their types (which is in-order). Note that this assumes that
700 * there are no multiple folders of a special type. */
701 if (item_a->stype != F_NORMAL && item_b->stype != F_NORMAL)
702 return item_a->stype - item_b->stype;
704 /* if b is normal folder, and a is not, b is smaller (ends up
705 * lower in the list) */
706 if (item_a->stype != F_NORMAL && item_b->stype == F_NORMAL)
707 return item_b->stype - item_a->stype;
709 /* if b is special folder, and a is not, b is larger (ends up
710 * higher in the list) */
711 if (item_a->stype == F_NORMAL && item_b->stype != F_NORMAL)
712 return item_b->stype - item_a->stype;
714 /* XXX g_utf8_collate_key() comparisons may speed things
715 * up when having large lists of folders */
716 gtk_tree_model_get(model, a, FOLDERSEL_FOLDERNAME, &str_a, -1);
717 gtk_tree_model_get(model, b, FOLDERSEL_FOLDERNAME, &str_b, -1);
719 /* otherwise just compare the folder names */
720 val = g_utf8_collate(str_a, str_b);
722 g_free(str_a);
723 g_free(str_b);
725 return val;
728 static gboolean tree_view_folder_item_func(GtkTreeModel *model,
729 GtkTreePath *path,
730 GtkTreeIter *iter,
731 FolderItemSearch *data)
733 FolderItem *item = NULL;
735 gtk_tree_model_get(model, iter, FOLDERSEL_FOLDERITEM, &item, -1);
737 if (data->item == item) {
738 data->path = gtk_tree_path_copy(path);
739 data->iter = *iter;
740 return TRUE;
743 return FALSE;