2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
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/>.
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
30 #include <sys/types.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"
44 #include "prefs_common.h"
50 FOLDERSEL_PIXBUF_OPEN
,
56 typedef struct _FolderItemSearch FolderItemSearch
;
58 struct _FolderItemSearch
65 static GdkPixbuf
*folder_pixbuf
= NULL
;
66 static GdkPixbuf
*folderopen_pixbuf
= NULL
;
67 static GdkPixbuf
*foldernoselect_pixbuf
= NULL
;
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
;
77 static FolderItem
*folder_item
;
78 static FolderItem
*selected_item
;
80 static GtkTreeStore
*tree_store
;
82 static gboolean cancelled
;
83 static gboolean finished
;
85 static void foldersel_create (void);
86 static void foldersel_init (void);
88 static void foldersel_append_item (GtkTreeStore
*store
,
93 static void foldersel_set_tree (Folder
*cur_folder
,
94 FolderSelectionType type
);
96 static gboolean
foldersel_selected (GtkTreeSelection
*selection
,
99 gboolean currently_selected
,
102 static void foldersel_ok (GtkButton
*button
,
104 static void foldersel_cancel (GtkButton
*button
,
106 static void foldersel_new_folder (GtkButton
*button
,
109 static void foldersel_entry_activated (GtkEntry
*entry
,
112 static void foldersel_tree_activated (GtkTreeView
*treeview
,
114 GtkTreeViewColumn
*column
,
117 static gint
delete_event (GtkWidget
*widget
,
120 static gboolean
key_pressed (GtkWidget
*widget
,
124 static gint
foldersel_folder_name_compare (GtkTreeModel
*model
,
129 static gboolean
tree_view_folder_item_func (GtkTreeModel
*model
,
132 FolderItemSearch
*data
);
134 FolderItem
*foldersel_folder_sel(Folder
*cur_folder
, FolderSelectionType type
,
135 const gchar
*default_folder
, gboolean can_sel_mailbox
)
137 selected_item
= NULL
;
138 root_selectable
= can_sel_mailbox
;
145 foldersel_set_tree(cur_folder
, type
);
149 FolderItemSearch fis
;
151 fis
.item
= folder_item
;
154 /* find matching model entry */
155 gtk_tree_model_foreach
156 (GTK_TREE_MODEL(tree_store
),
157 (GtkTreeModelForeachFunc
)tree_view_folder_item_func
,
161 GtkTreeSelection
*selection
;
163 selection
= gtk_tree_view_get_selection
164 (GTK_TREE_VIEW(treeview
));
165 gtk_tree_selection_select_iter(selection
, &fis
.iter
);
166 gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview
),
167 fis
.path
, NULL
, FALSE
);
168 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview
),
170 NULL
, TRUE
, 0.5, 0.0);
171 gtk_tree_path_free(fis
.path
);
173 gtk_tree_view_scroll_to_point
174 (GTK_TREE_VIEW(treeview
), 0, 0);
176 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(treeview
), 0, 0);
178 gtk_widget_grab_focus(ok_button
);
179 gtk_widget_grab_focus(treeview
);
181 gtk_widget_show(window
);
182 manage_window_set_transient(GTK_WINDOW(window
));
184 cancelled
= finished
= FALSE
;
186 while (finished
== FALSE
)
187 gtk_main_iteration();
189 gtk_widget_hide(window
);
190 gtk_entry_set_text(GTK_ENTRY(entry
), "");
191 gtk_tree_store_clear(tree_store
);
194 selected_item
&& (selected_item
->path
|| root_selectable
)) {
195 folder_item
= selected_item
;
201 static void foldersel_size_allocate_cb(GtkWidget
*widget
,
202 GtkAllocation
*allocation
)
204 cm_return_if_fail(allocation
!= NULL
);
206 prefs_common
.folderselwin_width
= allocation
->width
;
207 prefs_common
.folderselwin_height
= allocation
->height
;
210 static void foldersel_create(void)
213 GtkWidget
*scrolledwin
;
214 GtkWidget
*confirm_area
;
215 GtkTreeViewColumn
*column
;
216 GtkCellRenderer
*renderer
;
217 GtkTreeSelection
*selection
;
218 static GdkGeometry geometry
;
220 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "foldersel");
221 gtk_window_set_title(GTK_WINDOW(window
), _("Select folder"));
222 gtk_container_set_border_width(GTK_CONTAINER(window
), 4);
223 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
224 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
225 gtk_window_set_resizable(GTK_WINDOW(window
), TRUE
);
227 gtk_widget_realize(window
);
228 g_signal_connect(G_OBJECT(window
), "delete_event",
229 G_CALLBACK(delete_event
), NULL
);
230 g_signal_connect(G_OBJECT(window
), "key_press_event",
231 G_CALLBACK(key_pressed
), NULL
);
232 g_signal_connect(G_OBJECT(window
), "size_allocate",
233 G_CALLBACK(foldersel_size_allocate_cb
), NULL
);
234 MANAGE_WINDOW_SIGNALS_CONNECT(window
);
236 vbox
= gtk_vbox_new(FALSE
, 4);
237 gtk_container_add(GTK_CONTAINER(window
), vbox
);
239 scrolledwin
= gtk_scrolled_window_new(NULL
, NULL
);
240 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin
),
241 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
242 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin
),
244 gtk_box_pack_start(GTK_BOX(vbox
), scrolledwin
, TRUE
, TRUE
, 0);
246 tree_store
= gtk_tree_store_new(N_FOLDERSEL_COLUMNS
,
253 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(tree_store
),
254 FOLDERSEL_FOLDERNAME
,
255 foldersel_folder_name_compare
,
258 treeview
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store
));
259 g_object_unref(G_OBJECT(tree_store
));
260 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview
), FALSE
);
261 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview
),
262 prefs_common
.use_stripes_everywhere
);
263 #if GTK_CHECK_VERSION(2,10,0)
264 gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview
),
265 prefs_common
.enable_dotted_lines
);
267 gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview
),
268 FOLDERSEL_FOLDERNAME
);
270 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
271 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_BROWSE
);
272 gtk_tree_selection_set_select_function(selection
, foldersel_selected
,
275 g_signal_connect(G_OBJECT(treeview
), "row-activated",
276 G_CALLBACK(foldersel_tree_activated
), NULL
);
277 gtk_container_add(GTK_CONTAINER(scrolledwin
), treeview
);
279 column
= gtk_tree_view_column_new();
280 gtk_tree_view_column_set_spacing(column
, 2);
281 renderer
= gtk_cell_renderer_pixbuf_new();
282 gtk_tree_view_column_pack_start(column
, renderer
, FALSE
);
283 gtk_tree_view_column_set_attributes
285 "pixbuf", FOLDERSEL_PIXBUF
,
286 "pixbuf-expander-open", FOLDERSEL_PIXBUF_OPEN
,
287 "pixbuf-expander-closed", FOLDERSEL_PIXBUF
,
290 /* create text renderer */
291 renderer
= gtk_cell_renderer_text_new();
292 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
293 gtk_tree_view_column_set_attributes
295 "text", FOLDERSEL_FOLDERNAME
,
296 "foreground-gdk", FOLDERSEL_FOREGROUND
,
297 "weight", FOLDERSEL_BOLD
,
299 g_object_set(G_OBJECT(renderer
), "weight", PANGO_WEIGHT_BOLD
, NULL
);
300 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
302 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview
), column
);
304 entry
= gtk_entry_new();
305 gtk_editable_set_editable(GTK_EDITABLE(entry
), FALSE
);
306 gtk_box_pack_start(GTK_BOX(vbox
), entry
, FALSE
, FALSE
, 0);
307 g_signal_connect(G_OBJECT(entry
), "activate",
308 G_CALLBACK(foldersel_entry_activated
), NULL
);
310 gtkut_stock_button_set_create(&confirm_area
,
311 &new_button
, GTK_STOCK_NEW
,
312 &cancel_button
, GTK_STOCK_CANCEL
,
313 &ok_button
, GTK_STOCK_OK
);
315 gtk_box_pack_end(GTK_BOX(vbox
), confirm_area
, FALSE
, FALSE
, 0);
316 gtk_widget_grab_default(ok_button
);
318 g_signal_connect(G_OBJECT(ok_button
), "clicked",
319 G_CALLBACK(foldersel_ok
), NULL
);
320 g_signal_connect(G_OBJECT(cancel_button
), "clicked",
321 G_CALLBACK(foldersel_cancel
), NULL
);
322 g_signal_connect(G_OBJECT(new_button
), "clicked",
323 G_CALLBACK(foldersel_new_folder
), NULL
);
325 if (!geometry
.min_height
) {
326 geometry
.min_width
= 300;
327 geometry
.min_height
= 360;
330 gtk_window_set_geometry_hints(GTK_WINDOW(window
), NULL
, &geometry
,
332 gtk_widget_set_size_request(window
, prefs_common
.folderselwin_width
,
333 prefs_common
.folderselwin_height
);
335 gtk_widget_show_all(vbox
);
338 static void foldersel_init(void)
340 stock_pixbuf_gdk(treeview
, STOCK_PIXMAP_DIR_CLOSE
,
342 stock_pixbuf_gdk(treeview
, STOCK_PIXMAP_DIR_OPEN
,
344 stock_pixbuf_gdk(treeview
, STOCK_PIXMAP_DIR_NOSELECT
,
345 &foldernoselect_pixbuf
);
348 void foldersel_reflect_prefs_pixmap_theme(void)
351 g_object_unref(folder_pixbuf
);
352 if (folderopen_pixbuf
)
353 g_object_unref(folderopen_pixbuf
);
354 if (foldernoselect_pixbuf
)
355 g_object_unref(foldernoselect_pixbuf
);
359 static void foldersel_append_item(GtkTreeStore
*store
, FolderItem
*item
,
360 GtkTreeIter
*iter
, GtkTreeIter
*parent
)
362 gchar
*name
, *tmpname
;
363 GdkPixbuf
*pixbuf
, *pixbuf_open
;
365 PangoWeight weight
= PANGO_WEIGHT_NORMAL
;
366 GdkColor
*foreground
= NULL
;
367 static GdkColor color_noselect
= {0, COLOR_DIM
, COLOR_DIM
, COLOR_DIM
};
368 static GdkColor color_new
;
370 gtkut_convert_int_to_gdk_color(prefs_common
.color_new
, &color_new
);
372 name
= tmpname
= folder_item_get_name(item
);
374 if (item
->stype
!= F_NORMAL
&& FOLDER_IS_LOCAL(item
->folder
)) {
375 switch (item
->stype
) {
377 if (!strcmp2(item
->name
, INBOX_DIR
))
381 if (!strcmp2(item
->name
, OUTBOX_DIR
))
385 if (!strcmp2(item
->name
, QUEUE_DIR
))
389 if (!strcmp2(item
->name
, TRASH_DIR
))
393 if (!strcmp2(item
->name
, DRAFT_DIR
))
401 if (folder_has_parent_of_type(item
, F_QUEUE
) && item
->total_msgs
> 0) {
402 name
= g_strdup_printf("%s (%d)", name
, item
->total_msgs
);
403 } else if (item
->unread_msgs
> 0) {
404 name
= g_strdup_printf("%s (%d)", name
, item
->unread_msgs
);
406 name
= g_strdup(name
);
408 pixbuf
= item
->no_select
? foldernoselect_pixbuf
: folder_pixbuf
;
410 item
->no_select
? foldernoselect_pixbuf
: folderopen_pixbuf
;
412 if (folder_has_parent_of_type(item
, F_DRAFT
) ||
413 folder_has_parent_of_type(item
, F_OUTBOX
) ||
414 folder_has_parent_of_type(item
, F_TRASH
)) {
416 } else if (folder_has_parent_of_type(item
, F_QUEUE
)) {
417 use_color
= (item
->total_msgs
> 0);
418 if (item
->total_msgs
> 0)
419 weight
= PANGO_WEIGHT_BOLD
;
421 if (item
->unread_msgs
> 0)
422 weight
= PANGO_WEIGHT_BOLD
;
423 use_color
= (item
->new_msgs
> 0);
427 foreground
= &color_noselect
;
429 foreground
= &color_new
;
431 /* insert this node */
432 gtk_tree_store_append(store
, iter
, parent
);
433 gtk_tree_store_set(store
, iter
,
434 FOLDERSEL_FOLDERNAME
, name
,
435 FOLDERSEL_FOLDERITEM
, item
,
436 FOLDERSEL_PIXBUF
, pixbuf
,
437 FOLDERSEL_PIXBUF_OPEN
, pixbuf_open
,
438 FOLDERSEL_FOREGROUND
, foreground
,
439 FOLDERSEL_BOLD
, weight
,
445 static void foldersel_insert_gnode_in_store(GtkTreeStore
*store
, GNode
*node
,
452 cm_return_if_fail(node
!= NULL
);
453 cm_return_if_fail(node
->data
!= NULL
);
454 cm_return_if_fail(store
!= NULL
);
456 item
= FOLDER_ITEM(node
->data
);
457 foldersel_append_item(store
, item
, &child
, parent
);
459 /* insert its children (this node as parent) */
460 for (iter
= node
->children
; iter
!= NULL
; iter
= iter
->next
)
461 foldersel_insert_gnode_in_store(store
, iter
, &child
);
464 static void foldersel_set_tree(Folder
*cur_folder
, FolderSelectionType type
)
469 for (list
= folder_get_list(); list
!= NULL
; list
= list
->next
) {
470 folder
= FOLDER(list
->data
);
471 cm_return_if_fail(folder
!= NULL
);
473 if (type
!= FOLDER_SEL_ALL
) {
474 if (FOLDER_TYPE(folder
) == F_NEWS
)
478 foldersel_insert_gnode_in_store(tree_store
, folder
->node
, NULL
);
481 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store
),
482 FOLDERSEL_FOLDERNAME
,
485 gtk_tree_view_expand_all(GTK_TREE_VIEW(treeview
));
488 #include "localfolder.h"
489 static gboolean
foldersel_selected(GtkTreeSelection
*selection
,
490 GtkTreeModel
*model
, GtkTreePath
*path
,
491 gboolean currently_selected
, gpointer data
)
494 FolderItem
*item
= NULL
;
496 if (currently_selected
)
499 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model
), &iter
, path
))
502 gtk_tree_model_get(GTK_TREE_MODEL(tree_store
), &iter
,
503 FOLDERSEL_FOLDERITEM
, &item
, -1);
505 selected_item
= item
;
506 if (selected_item
&& selected_item
->path
) {
508 id
= folder_item_get_identifier(selected_item
);
509 gtk_entry_set_text(GTK_ENTRY(entry
), id
);
512 if (root_selectable
&& selected_item
&& selected_item
->folder
&&
513 (FOLDER_TYPE(selected_item
->folder
) == F_MH
||
514 FOLDER_TYPE(selected_item
->folder
) == F_MBOX
||
515 FOLDER_TYPE(selected_item
->folder
) == F_IMAP
)) {
516 gchar
*id
= folder_get_identifier(selected_item
->folder
);
517 gtk_entry_set_text(GTK_ENTRY(entry
), id
);
520 gtk_entry_set_text(GTK_ENTRY(entry
), "");
525 static void foldersel_ok(GtkButton
*button
, gpointer data
)
530 static void foldersel_cancel(GtkButton
*button
, gpointer data
)
536 static void foldersel_new_folder(GtkButton
*button
, gpointer data
)
538 FolderItem
*new_item
;
542 GtkTreeIter selected
, new_child
;
543 GtkTreePath
*selected_p
, *new_child_p
;
546 GtkTreeSelection
*selection
;
548 if (!selected_item
|| FOLDER_TYPE(selected_item
->folder
) == F_NEWS
)
551 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
552 if (!gtk_tree_selection_get_selected(selection
, &model
, &selected
))
554 store
= GTK_TREE_STORE(model
);
556 new_folder
= input_dialog(_("New folder"),
557 _("Input the name of new folder:"),
559 if (!new_folder
) return;
560 AUTORELEASE_STR(new_folder
, {g_free(new_folder
); return;});
562 p
= strchr(new_folder
, G_DIR_SEPARATOR
);
563 if ((p
&& FOLDER_TYPE(selected_item
->folder
) != F_IMAP
) ||
564 (p
&& FOLDER_TYPE(selected_item
->folder
) == F_IMAP
&&
566 alertpanel_error(_("'%c' can't be included in folder name."),
571 disp_name
= trim_string(new_folder
, 32);
572 AUTORELEASE_STR(disp_name
, {g_free(disp_name
); return;});
574 /* find whether the directory already exists */
575 if (folder_find_child_item_by_name(selected_item
, new_folder
)) {
576 alertpanel_error(_("The folder '%s' already exists."),
581 new_item
= folder_create_folder(selected_item
, new_folder
);
583 alertpanel_error(_("Can't create the folder '%s'."), disp_name
);
588 foldersel_append_item(store
, new_item
, &new_child
, &selected
);
590 selected_p
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &selected
);
591 new_child_p
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
),
594 gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview
), selected_p
, FALSE
);
595 gtk_tree_selection_select_iter(selection
, &new_child
);
596 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview
), new_child_p
,
597 NULL
, TRUE
, 0.5, 0.0);
598 gtk_tree_path_free(new_child_p
);
599 gtk_tree_path_free(selected_p
);
604 static void foldersel_entry_activated(GtkEntry
*entry
, gpointer data
)
606 gtk_button_clicked(GTK_BUTTON(ok_button
));
609 static void foldersel_tree_activated(GtkTreeView
*treeview
, GtkTreePath
*path
,
610 GtkTreeViewColumn
*column
, gpointer data
)
612 gtk_button_clicked(GTK_BUTTON(ok_button
));
615 static gint
delete_event(GtkWidget
*widget
, GdkEventAny
*event
, gpointer data
)
617 foldersel_cancel(NULL
, NULL
);
621 static gboolean
key_pressed(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
623 if (event
&& event
->keyval
== GDK_Escape
)
624 foldersel_cancel(NULL
, NULL
);
628 static gint
foldersel_folder_name_compare(GtkTreeModel
*model
, GtkTreeIter
*a
,
629 GtkTreeIter
*b
, gpointer context
)
631 gchar
*str_a
= NULL
, *str_b
= NULL
;
633 FolderItem
*item_a
= NULL
, *item_b
= NULL
;
636 gtk_tree_model_get(model
, a
, FOLDERSEL_FOLDERITEM
, &item_a
, -1);
637 gtk_tree_model_get(model
, b
, FOLDERSEL_FOLDERITEM
, &item_b
, -1);
639 /* no sort for root folder */
640 if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(model
), &parent
, a
))
643 /* if both a and b are special folders, sort them according to
644 * their types (which is in-order). Note that this assumes that
645 * there are no multiple folders of a special type. */
646 if (item_a
->stype
!= F_NORMAL
&& item_b
->stype
!= F_NORMAL
)
647 return item_a
->stype
- item_b
->stype
;
649 /* if b is normal folder, and a is not, b is smaller (ends up
650 * lower in the list) */
651 if (item_a
->stype
!= F_NORMAL
&& item_b
->stype
== F_NORMAL
)
652 return item_b
->stype
- item_a
->stype
;
654 /* if b is special folder, and a is not, b is larger (ends up
655 * higher in the list) */
656 if (item_a
->stype
== F_NORMAL
&& item_b
->stype
!= F_NORMAL
)
657 return item_b
->stype
- item_a
->stype
;
659 /* XXX g_utf8_collate_key() comparisons may speed things
660 * up when having large lists of folders */
661 gtk_tree_model_get(model
, a
, FOLDERSEL_FOLDERNAME
, &str_a
, -1);
662 gtk_tree_model_get(model
, b
, FOLDERSEL_FOLDERNAME
, &str_b
, -1);
664 /* otherwise just compare the folder names */
665 val
= g_utf8_collate(str_a
, str_b
);
673 static gboolean
tree_view_folder_item_func(GtkTreeModel
*model
,
676 FolderItemSearch
*data
)
678 FolderItem
*item
= NULL
;
680 gtk_tree_model_get(model
, iter
, FOLDERSEL_FOLDERITEM
, &item
, -1);
682 if (data
->item
== item
) {
683 data
->path
= gtk_tree_path_copy(path
);