more fixes for Go To actions not obeying 'always_show_msg' option
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 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
20 #include "defs.h"
21
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 <string.h>
28 #include <stdlib.h>
29
30 #include "main.h"
31 #include "mainwindow.h"
32 #include "folderview.h"
33 #include "summaryview.h"
34 #include "summary_search.h"
35 #include "inputdialog.h"
36 #include "manage_window.h"
37 #include "alertpanel.h"
38 #include "menu.h"
39 #include "stock_pixmap.h"
40 #include "procmsg.h"
41 #include "utils.h"
42 #include "gtkutils.h"
43 #include "prefs_common.h"
44 #include "prefs_account.h"
45 #include "prefs_filtering.h"
46 #include "prefs_folder_item.h"
47 #include "account.h"
48 #include "folder.h"
49 #include "foldersel.h"
50 #include "inc.h"
51 #include "statusbar.h"
52 #include "hooks.h"
53 #include "folderutils.h"
54 #include "partial_download.h"
55 #include "prefs_folder_column.h"
56 #include "filtering.h"
57 #include "quicksearch.h"
58 #include "manual.h"
59 #include "timing.h"
60 #include "log.h"
61 #include "gtkcmctree.h"
62
63 #define COL_FOLDER_WIDTH        150
64 #define COL_NUM_WIDTH           32
65
66 static GList *folderview_list = NULL;
67
68 static GtkStyle *normal_style;
69 static GtkStyle *normal_color_style;
70 static GtkStyle *bold_style;
71 static GtkStyle *bold_color_style;
72 static GtkStyle *bold_tgtfold_style;
73
74 static GdkPixbuf *inboxxpm;
75 static GdkPixbuf *inboxhrmxpm;
76 static GdkPixbuf *inboxopenxpm;
77 static GdkPixbuf *inboxopenhrmxpm;
78 static GdkPixbuf *outboxxpm;
79 static GdkPixbuf *outboxhrmxpm;
80 static GdkPixbuf *outboxopenxpm;
81 static GdkPixbuf *outboxopenhrmxpm;
82 static GdkPixbuf *folderxpm;
83 static GdkPixbuf *folderhrmxpm;
84 static GdkPixbuf *folderopenxpm;
85 static GdkPixbuf *folderopenhrmxpm;
86 static GdkPixbuf *trashopenxpm;
87 static GdkPixbuf *trashopenhrmxpm;
88 static GdkPixbuf *trashxpm;
89 static GdkPixbuf *trashhrmxpm;
90 static GdkPixbuf *queuexpm;
91 static GdkPixbuf *queuehrmxpm;
92 static GdkPixbuf *queueopenxpm;
93 static GdkPixbuf *queueopenhrmxpm;
94 static GdkPixbuf *draftsxpm;
95 static GdkPixbuf *draftsopenxpm;
96 static GdkPixbuf *foldersubsxpm;
97 static GdkPixbuf *foldersubsopenxpm;
98 static GdkPixbuf *foldernoselectxpm;
99 static GdkPixbuf *foldernoselectopenxpm;
100
101 static GdkPixbuf *m_inboxxpm;
102 static GdkPixbuf *m_inboxhrmxpm;
103 static GdkPixbuf *m_inboxopenxpm;
104 static GdkPixbuf *m_inboxopenhrmxpm;
105 static GdkPixbuf *m_outboxxpm;
106 static GdkPixbuf *m_outboxhrmxpm;
107 static GdkPixbuf *m_outboxopenxpm;
108 static GdkPixbuf *m_outboxopenhrmxpm;
109 static GdkPixbuf *m_folderxpm;
110 static GdkPixbuf *m_folderhrmxpm;
111 static GdkPixbuf *m_folderopenxpm;
112 static GdkPixbuf *m_folderopenhrmxpm;
113 static GdkPixbuf *m_trashopenxpm;
114 static GdkPixbuf *m_trashopenhrmxpm;
115 static GdkPixbuf *m_trashxpm;
116 static GdkPixbuf *m_trashhrmxpm;
117 static GdkPixbuf *m_queuexpm;
118 static GdkPixbuf *m_queuehrmxpm;
119 static GdkPixbuf *m_queueopenxpm;
120 static GdkPixbuf *m_queueopenhrmxpm;
121 static GdkPixbuf *m_draftsxpm;
122 static GdkPixbuf *m_draftsopenxpm;
123 static GdkPixbuf *m_foldersubsxpm;
124 static GdkPixbuf *m_foldernoselectxpm;
125
126 static GdkPixbuf *newxpm;
127 static GdkPixbuf *unreadxpm;
128 static GdkPixbuf *readxpm;
129
130 static void folderview_select_node       (FolderView    *folderview,
131                                           GtkCMCTreeNode        *node);
132 static void folderview_set_folders       (FolderView    *folderview);
133 static void folderview_sort_folders      (FolderView    *folderview,
134                                           GtkCMCTreeNode        *root,
135                                           Folder        *folder);
136 static void folderview_append_folder     (FolderView    *folderview,
137                                           Folder        *folder);
138 static void folderview_update_node       (FolderView    *folderview,
139                                           GtkCMCTreeNode        *node);
140
141 static gint folderview_clist_compare    (GtkCMCList     *clist,
142                                          gconstpointer   ptr1,
143                                          gconstpointer   ptr2);
144
145 /* callback functions */
146 static gboolean folderview_button_pressed       (GtkWidget      *ctree,
147                                                  GdkEventButton *event,
148                                                  FolderView     *folderview);
149 static gboolean folderview_button_released      (GtkWidget      *ctree,
150                                                  GdkEventButton *event,
151                                                  FolderView     *folderview);
152 static gboolean folderview_key_pressed  (GtkWidget      *widget,
153                                          GdkEventKey    *event,
154                                          FolderView     *folderview);
155 static void folderview_selected         (GtkCMCTree     *ctree,
156                                          GtkCMCTreeNode *row,
157                                          gint            column,
158                                          FolderView     *folderview);
159 static void folderview_tree_expanded    (GtkCMCTree     *ctree,
160                                          GtkCMCTreeNode *node,
161                                          FolderView     *folderview);
162 static void folderview_tree_collapsed   (GtkCMCTree     *ctree,
163                                          GtkCMCTreeNode *node,
164                                          FolderView     *folderview);
165 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
166                                          FolderView     *folderview);
167 static void folderview_col_resized      (GtkCMCList     *clist,
168                                          gint            column,
169                                          gint            width,
170                                          FolderView     *folderview);
171
172 static void mark_all_read_handler       (GtkAction      *action,
173                                          gpointer        data,
174                                          gboolean        recursive);
175
176 static void mark_all_read_cb            (GtkAction      *action,
177                                          gpointer        data);
178 static void mark_all_read_recursive_cb  (GtkAction      *action,
179                                          gpointer        data);
180
181 static void folderview_empty_trash_cb   (GtkAction      *action,
182                                          gpointer        data);
183
184 static void folderview_send_queue_cb    (GtkAction      *action,
185                                          gpointer        data);
186
187 static void folderview_search_cb        (GtkAction      *action,
188                                          gpointer        data);
189 static void folderview_run_processing_cb(GtkAction      *action,
190                                          gpointer        data);
191
192 static void folderview_property_cb      (GtkAction      *action,
193                                          gpointer        data);
194
195 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
196                                           GdkDragContext *context,
197                                           gint            x,
198                                           gint            y,
199                                           guint           time,
200                                           FolderView     *folderview);
201 static void folderview_drag_leave_cb     (GtkWidget        *widget,
202                                           GdkDragContext   *context,
203                                           guint             time,
204                                           FolderView       *folderview);
205 static void folderview_drag_received_cb  (GtkWidget        *widget,
206                                           GdkDragContext   *drag_context,
207                                           gint              x,
208                                           gint              y,
209                                           GtkSelectionData *data,
210                                           guint             info,
211                                           guint             time,
212                                           FolderView       *folderview);
213 #ifndef GENERIC_UMPC
214 static void folderview_start_drag        (GtkWidget *widget, gint button, GdkEvent *event,
215                                           FolderView       *folderview);
216 #endif
217 static void folderview_drag_data_get     (GtkWidget        *widget,
218                                           GdkDragContext   *drag_context,
219                                           GtkSelectionData *selection_data,
220                                           guint             info,
221                                           guint             time,
222                                           FolderView       *folderview);
223 static void folderview_drag_end_cb       (GtkWidget        *widget,
224                                           GdkDragContext   *drag_context,
225                                           FolderView       *folderview);
226
227 static void folderview_create_folder_node       (FolderView       *folderview, 
228                                           FolderItem       *item);
229 static gboolean folderview_update_folder         (gpointer          source,
230                                           gpointer          userdata);
231 static gboolean folderview_update_item_claws     (gpointer          source,
232                                           gpointer          data);
233 static void folderview_processing_cb(GtkAction *action, gpointer data);
234 static void folderview_set_sens_and_popup_menu(FolderView *folderview, gint row, 
235                                 GdkEventButton *event);
236
237 GHashTable *folderview_popups;
238
239 static GtkActionEntry folderview_common_popup_entries[] = 
240 {
241         {"FolderViewPopup",                     NULL, "FolderViewPopup" },
242         {"FolderViewPopup/MarkAllRead",         NULL, N_("Mark all re_ad"), NULL, NULL, G_CALLBACK(mark_all_read_cb) },
243         {"FolderViewPopup/MarkAllReadRec",      NULL, N_("Mark all read recursi_vely"), NULL, NULL, G_CALLBACK(mark_all_read_recursive_cb) },
244         {"FolderViewPopup/---",                 NULL, "---" },
245         {"FolderViewPopup/RunProcessing",       NULL, N_("R_un processing rules"), NULL, NULL, G_CALLBACK(folderview_run_processing_cb) },
246         {"FolderViewPopup/SearchFolder",        NULL, N_("_Search folder..."), NULL, NULL, G_CALLBACK(folderview_search_cb) },
247         {"FolderViewPopup/Properties",          NULL, N_("_Properties..."), NULL, NULL, G_CALLBACK(folderview_property_cb) },
248         {"FolderViewPopup/Processing",          NULL, N_("Process_ing..."), NULL, NULL, G_CALLBACK(folderview_processing_cb) },
249         {"FolderViewPopup/EmptyTrash",          NULL, N_("Empty _trash..."), NULL, NULL, G_CALLBACK(folderview_empty_trash_cb) },
250         {"FolderViewPopup/SendQueue",           NULL, N_("Send _queue..."), NULL, NULL, G_CALLBACK(folderview_send_queue_cb) },
251         
252 };
253
254 GtkTargetEntry folderview_drag_types[] =
255 {
256         {"claws-mail/internal", GTK_TARGET_SAME_APP, TARGET_DUMMY},
257         {"text/uri-list", 0, TARGET_MAIL_URI_LIST}
258 };
259
260 void folderview_initialize(void)
261 {
262         FolderViewPopup *fpopup;
263
264         fpopup = g_new0(FolderViewPopup, 1);
265
266         fpopup->klass = "common";
267         fpopup->path = "<CommonFolder>";
268         fpopup->entries = folderview_common_popup_entries;
269         fpopup->n_entries = G_N_ELEMENTS(folderview_common_popup_entries);
270         fpopup->set_sensitivity = NULL;
271
272         folderview_popups = g_hash_table_new(g_str_hash, g_str_equal);
273         g_hash_table_insert(folderview_popups, "common", fpopup);
274 }
275
276 static GtkActionGroup *create_action_group(FolderView *folderview, FolderViewPopup *fpopup)
277 {
278         FolderViewPopup *fpopup_common;
279         GtkActionGroup *action_group;
280         
281         action_group = cm_menu_create_action_group(
282                                 fpopup->path, 
283                                 fpopup->entries, fpopup->n_entries, 
284                                 (gpointer)folderview);
285
286         if (fpopup->toggle_entries)
287                 gtk_action_group_add_toggle_actions(action_group, fpopup->toggle_entries,
288                                 fpopup->n_toggle_entries,
289                                 (gpointer)folderview);
290         if (fpopup->radio_entries)
291                 gtk_action_group_add_radio_actions(action_group, fpopup->radio_entries,
292                                 fpopup->n_radio_entries, fpopup->radio_default, 
293                                 G_CALLBACK(fpopup->radio_callback), 
294                                 (gpointer)folderview);
295
296         fpopup_common = g_hash_table_lookup(folderview_popups, "common");
297         if (fpopup_common != fpopup) {
298                 gtk_action_group_add_actions(action_group, fpopup_common->entries,
299                                 fpopup_common->n_entries,
300                                 (gpointer)folderview);
301                 if (fpopup_common->toggle_entries)
302                         gtk_action_group_add_toggle_actions(action_group, fpopup_common->toggle_entries,
303                                         fpopup_common->n_toggle_entries,
304                                         (gpointer)folderview);
305                 if (fpopup_common->radio_entries)
306                         gtk_action_group_add_radio_actions(action_group, fpopup_common->radio_entries,
307                                         fpopup_common->n_radio_entries, fpopup_common->radio_default, 
308                                         G_CALLBACK(fpopup_common->radio_callback), 
309                                         (gpointer)folderview);
310         }
311
312         return action_group;
313 }
314
315 static void create_action_groups(gpointer key, gpointer value, gpointer data)
316 {
317         FolderView *folderview = data;
318         FolderViewPopup *fpopup = value;
319         GtkActionGroup *group;
320
321         group = create_action_group(folderview, fpopup);
322         g_hash_table_insert(folderview->popups, fpopup->klass, group);
323 }
324
325 static void folderview_column_set_titles(FolderView *folderview)
326 {
327         GtkWidget *ctree = folderview->ctree;
328         GtkWidget *label_folder;
329         GtkWidget *label_new;
330         GtkWidget *label_unread;
331         GtkWidget *label_total;
332         GtkWidget *hbox_folder;
333         GtkWidget *hbox_new;
334         GtkWidget *hbox_unread;
335         GtkWidget *hbox_total;
336         gint *col_pos = folderview->col_pos;
337
338         debug_print("setting titles...\n");
339         gtk_widget_realize(folderview->ctree);
340         gtk_widget_show_all(folderview->scrolledwin);
341         
342         /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
343          * instead text (text overflows making them unreadable and ugly) */
344         stock_pixbuf_gdk(STOCK_PIXMAP_NEW, &newxpm);
345         stock_pixbuf_gdk(STOCK_PIXMAP_UNREAD, &unreadxpm);
346         stock_pixbuf_gdk(STOCK_PIXMAP_READ, &readxpm);
347         
348         label_folder = gtk_label_new(_("Folder"));
349         label_new = gtk_image_new_from_pixbuf(newxpm);
350         label_unread = gtk_image_new_from_pixbuf(unreadxpm);
351         label_total = gtk_image_new_from_pixbuf(readxpm);
352         
353         gtk_cmclist_column_titles_active(GTK_CMCLIST(ctree));
354          
355         hbox_folder = gtk_hbox_new(FALSE, 4);
356         hbox_new = gtk_hbox_new(FALSE, 4);
357         hbox_unread = gtk_hbox_new(FALSE, 4);
358         hbox_total = gtk_hbox_new(FALSE, 4);
359
360         /* left justified */
361         gtk_box_pack_start(GTK_BOX(hbox_folder), label_folder, TRUE, TRUE, 0);
362         gtk_misc_set_alignment (GTK_MISC (label_folder), 0, 0.5);
363         gtk_box_pack_start(GTK_BOX(hbox_new), label_new, TRUE, TRUE, 0);
364         gtk_misc_set_alignment (GTK_MISC (label_new), 1, 0.5);
365         gtk_box_pack_start(GTK_BOX(hbox_unread), label_unread, TRUE, TRUE, 0);
366         gtk_misc_set_alignment (GTK_MISC (label_unread), 1, 0.5);
367         gtk_box_pack_start(GTK_BOX(hbox_total), label_total, TRUE, TRUE, 0);
368         gtk_misc_set_alignment (GTK_MISC (label_total), 1, 0.5);
369
370         gtk_widget_show_all(hbox_folder);
371         gtk_widget_show_all(hbox_new);
372         gtk_widget_show_all(hbox_unread);
373         gtk_widget_show_all(hbox_total);
374
375 #ifdef GENERIC_UMPC
376         gtk_widget_set_size_request(hbox_new, -1, 20);
377         gtk_widget_set_size_request(hbox_unread, -1, 20);
378         gtk_widget_set_size_request(hbox_total, -1, 20);
379 #endif
380
381         gtk_cmclist_set_column_widget(GTK_CMCLIST(ctree),col_pos[F_COL_FOLDER],hbox_folder);
382         gtk_cmclist_set_column_widget(GTK_CMCLIST(ctree),col_pos[F_COL_NEW],hbox_new);
383         gtk_cmclist_set_column_widget(GTK_CMCLIST(ctree),col_pos[F_COL_UNREAD],hbox_unread);
384         gtk_cmclist_set_column_widget(GTK_CMCLIST(ctree),col_pos[F_COL_TOTAL],hbox_total);
385
386 #ifdef GENERIC_UMPC
387         GTK_EVENTS_FLUSH();
388 #endif
389
390         gtk_sctree_set_column_tooltip(GTK_SCTREE(ctree), col_pos[F_COL_NEW], _("New"));
391         gtk_sctree_set_column_tooltip(GTK_SCTREE(ctree), col_pos[F_COL_UNREAD], _("Unread"));
392         gtk_sctree_set_column_tooltip(GTK_SCTREE(ctree), col_pos[F_COL_TOTAL], _("Total"));
393 }
394
395 static gboolean folderview_popup_menu(GtkWidget *widget, gpointer data)
396 {
397         FolderView *folderview = (FolderView *)data;
398         GdkEventButton event;
399         if (folderview_get_selected_item(folderview) == NULL)
400                 return FALSE;
401         
402         event.button = 3;
403         event.time = gtk_get_current_event_time();
404         
405         folderview_set_sens_and_popup_menu(folderview, -1, 
406                                 &event);
407
408         return TRUE;
409 }
410
411
412 static GtkWidget *folderview_ctree_create(FolderView *folderview)
413 {
414         GtkWidget *ctree;
415         gint *col_pos;
416         FolderColumnState *col_state;
417         FolderColumnType type;
418         gchar *titles[N_FOLDER_COLS];
419         gint i;
420         GtkWidget *scrolledwin = folderview->scrolledwin;
421
422         debug_print("creating tree...\n");
423         memset(titles, 0, sizeof(titles));
424
425         col_state = prefs_folder_column_get_config();
426         memset(titles, 0, sizeof(titles));
427
428         col_pos = folderview->col_pos;
429
430         for (i = 0; i < N_FOLDER_COLS; i++) {
431                 folderview->col_state[i] = col_state[i];
432                 type = col_state[i].type;
433                 col_pos[type] = i;
434         }
435
436         titles[col_pos[F_COL_FOLDER]] = _("Folder");
437         titles[col_pos[F_COL_NEW]]    = _("New");
438         titles[col_pos[F_COL_UNREAD]] = _("Unread");
439         /* TRANSLATORS: This in Number sign in American style */
440         titles[col_pos[F_COL_TOTAL]]  = _("#");
441
442         ctree = gtk_sctree_new_with_titles(N_FOLDER_COLS, col_pos[F_COL_FOLDER],
443                                            titles);
444
445         if (prefs_common.show_col_headers == FALSE)
446                 gtk_cmclist_column_titles_hide(GTK_CMCLIST(ctree));
447
448
449         gtk_cmclist_set_selection_mode(GTK_CMCLIST(ctree), GTK_SELECTION_BROWSE);
450         gtk_cmclist_set_column_justification(GTK_CMCLIST(ctree), col_pos[F_COL_NEW],
451                                            GTK_JUSTIFY_RIGHT);
452         gtk_cmclist_set_column_justification(GTK_CMCLIST(ctree),
453                                            col_pos[F_COL_UNREAD],
454                                            GTK_JUSTIFY_RIGHT);
455         gtk_cmclist_set_column_justification(GTK_CMCLIST(ctree), 
456                                            col_pos[F_COL_TOTAL],
457                                            GTK_JUSTIFY_RIGHT);
458         gtk_cmctree_set_line_style(GTK_CMCTREE(ctree), GTK_CMCTREE_LINES_NONE);
459         gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
460                              GTK_CMCTREE_EXPANDER_TRIANGLE);
461
462         gtk_sctree_set_stripes(GTK_SCTREE(ctree), prefs_common.use_stripes_in_summaries);
463         gtk_sctree_set_recursive_expand(GTK_SCTREE(ctree), FALSE);
464
465         gtk_cmctree_set_indent(GTK_CMCTREE(ctree), CTREE_INDENT);
466         gtk_cmclist_set_compare_func(GTK_CMCLIST(ctree), folderview_clist_compare);
467
468         /* don't let title buttons take key focus */
469         for (i = 0; i < N_FOLDER_COLS; i++) {
470                 gtkut_widget_set_can_focus(GTK_CMCLIST(ctree)->column[i].button, FALSE);
471                 gtk_cmclist_set_column_width(GTK_CMCLIST(ctree), col_pos[i],
472                                    prefs_common.folder_col_size[i]);
473                 gtk_cmclist_set_column_visibility
474                         (GTK_CMCLIST(ctree), i, col_state[i].visible);
475         }
476
477         g_signal_connect(G_OBJECT(ctree), "key_press_event",
478                          G_CALLBACK(folderview_key_pressed),
479                          folderview);
480         g_signal_connect(G_OBJECT(ctree), "button_press_event",
481                          G_CALLBACK(folderview_button_pressed),
482                          folderview);
483         g_signal_connect(G_OBJECT(ctree), "popup-menu",
484                          G_CALLBACK(folderview_popup_menu), folderview);
485         g_signal_connect(G_OBJECT(ctree), "button_release_event",
486                          G_CALLBACK(folderview_button_released),
487                          folderview);
488         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
489                          G_CALLBACK(folderview_selected), folderview);
490 #ifndef GENERIC_UMPC
491         /* drag-n-dropping folders on maemo is impractical as this 
492          * opens the folder almost everytime */
493         g_signal_connect(G_OBJECT(ctree), "start_drag",
494                          G_CALLBACK(folderview_start_drag), folderview);
495 #endif
496         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
497                          G_CALLBACK(folderview_drag_data_get),
498                          folderview);
499
500         g_signal_connect_after(G_OBJECT(ctree), "tree_expand",
501                                G_CALLBACK(folderview_tree_expanded),
502                                folderview);
503         g_signal_connect_after(G_OBJECT(ctree), "tree_collapse",
504                                G_CALLBACK(folderview_tree_collapsed),
505                                folderview);
506
507         g_signal_connect(G_OBJECT(ctree), "resize_column",
508                          G_CALLBACK(folderview_col_resized),
509                          folderview);
510
511         /* drop callback */
512         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
513                           folderview_drag_types, 2,
514                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
515         g_signal_connect(G_OBJECT(ctree), "drag_motion",
516                          G_CALLBACK(folderview_drag_motion_cb),
517                          folderview);
518         g_signal_connect(G_OBJECT(ctree), "drag_leave",
519                          G_CALLBACK(folderview_drag_leave_cb),
520                          folderview);
521         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
522                          G_CALLBACK(folderview_drag_received_cb),
523                          folderview);
524         g_signal_connect(G_OBJECT(ctree), "drag_end",
525                          G_CALLBACK(folderview_drag_end_cb),
526                          folderview);
527
528         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
529
530         return ctree;
531 }
532
533 void folderview_set_column_order(FolderView *folderview)
534 {
535         GtkWidget *ctree = folderview->ctree;
536         FolderItem *item = folderview_get_selected_item(folderview);
537         FolderItem *sel_item = NULL, *op_item = NULL;
538         GtkWidget *scrolledwin = folderview->scrolledwin;
539
540         if (folderview->drag_timer_id != 0) {
541                 g_source_remove(folderview->drag_timer_id);
542                 folderview->drag_timer_id = 0;
543         }
544         if (folderview->deferred_refresh_id != 0) {
545                 g_source_remove(folderview->deferred_refresh_id);
546                 folderview->deferred_refresh_id = 0;
547         }
548         if (folderview->scroll_timeout_id != 0) {
549                 g_source_remove(folderview->scroll_timeout_id);
550                 folderview->scroll_timeout_id = 0;
551         }
552         if (folderview->postpone_select_id != 0) {
553                 g_source_remove(folderview->postpone_select_id);
554                 folderview->postpone_select_id = 0;
555         }
556
557         if (folderview->selected)
558                 sel_item = folderview_get_selected_item(folderview);
559         if (folderview->opened)
560                 op_item = folderview_get_opened_item(folderview);
561
562         debug_print("recreating tree...\n");
563         gtk_widget_destroy(folderview->ctree);
564
565
566         folderview->ctree = ctree = folderview_ctree_create(folderview);
567         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
568                                             GTK_CMCLIST(ctree)->hadjustment);
569         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
570                                             GTK_CMCLIST(ctree)->vadjustment);
571         gtk_widget_show(ctree);
572         
573         if (sel_item)
574                 folderview->selected = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree), NULL, sel_item);
575         if (op_item)
576                 folderview->opened = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree), NULL, op_item);
577
578         folderview_set(folderview);
579         folderview_column_set_titles(folderview);
580
581         folderview_select(folderview,item);
582 }
583
584 FolderView *folderview_create(void)
585 {
586         FolderView *folderview;
587         GtkWidget *scrolledwin;
588         GtkWidget *ctree;
589
590         debug_print("Creating folder view...\n");
591         folderview = g_new0(FolderView, 1);
592
593         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
594         gtk_scrolled_window_set_policy
595                 (GTK_SCROLLED_WINDOW(scrolledwin),
596                  GTK_POLICY_AUTOMATIC,
597                  prefs_common.folderview_vscrollbar_policy);
598         gtk_widget_set_size_request(scrolledwin,
599                              prefs_common.folderview_width,
600                              prefs_common.folderview_height);
601
602         folderview->scrolledwin  = scrolledwin;
603         ctree = folderview_ctree_create(folderview);
604         
605         /* create popup factories */
606         folderview->popups = g_hash_table_new(g_str_hash, g_str_equal);
607         g_hash_table_foreach(folderview_popups, create_action_groups, folderview);
608
609         folderview->ctree        = ctree;
610
611         folderview->folder_update_callback_id =
612                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST, folderview_update_folder, (gpointer) folderview);
613         folderview->folder_item_update_callback_id =
614                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST, folderview_update_item_claws, (gpointer) folderview);
615
616         gtk_widget_show_all(scrolledwin);
617         
618         folderview->target_list = gtk_target_list_new(folderview_drag_types, 2);
619         folderview_list = g_list_append(folderview_list, folderview);
620
621         folderview->drag_timer_id       = 0;
622         folderview->deferred_refresh_id = 0;
623         folderview->scroll_timeout_id   = 0;
624         folderview->postpone_select_id  = 0;
625
626         return folderview;
627 }
628
629 void folderview_init(FolderView *folderview)
630 {
631         GtkWidget *ctree = folderview->ctree;
632         GdkColor gdk_color;
633         PangoFontDescription *normal_font;
634
635         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm);
636         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm);
637         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm);
638         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm);
639         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm);
640         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm);
641         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm);
642         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm);
643         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_CLOSE, &folderxpm);
644         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm);
645         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN, &folderopenxpm);
646         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm);
647         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm);
648         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm);
649         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_CLOSE, &trashxpm);
650         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm);
651         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm);
652         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm);
653         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm);
654         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm);
655         stock_pixbuf_gdk(STOCK_PIXMAP_DRAFTS_CLOSE, &draftsxpm);
656         stock_pixbuf_gdk(STOCK_PIXMAP_DRAFTS_OPEN, &draftsopenxpm);
657         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_SUBS_OPEN, &foldersubsopenxpm);
658         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_SUBS_CLOSE, &foldersubsxpm);
659         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_OPEN, &foldernoselectopenxpm);
660         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_CLOSE, &foldernoselectxpm);
661
662         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_CLOSE_MARK, &m_inboxxpm);
663         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_CLOSE_HRM_MARK, &m_inboxhrmxpm);
664         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_OPEN_MARK, &m_inboxopenxpm);
665         stock_pixbuf_gdk(STOCK_PIXMAP_INBOX_OPEN_HRM_MARK, &m_inboxopenhrmxpm);
666         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_CLOSE_MARK, &m_outboxxpm);
667         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_CLOSE_HRM_MARK, &m_outboxhrmxpm);
668         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_OPEN_MARK, &m_outboxopenxpm);
669         stock_pixbuf_gdk(STOCK_PIXMAP_OUTBOX_OPEN_HRM_MARK, &m_outboxopenhrmxpm);
670         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_CLOSE_MARK, &m_folderxpm);
671         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_CLOSE_HRM_MARK, &m_folderhrmxpm);
672         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN_MARK, &m_folderopenxpm);
673         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN_HRM_MARK, &m_folderopenhrmxpm);
674         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_OPEN_MARK, &m_trashopenxpm);
675         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_OPEN_HRM_MARK, &m_trashopenhrmxpm);
676         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_CLOSE_MARK, &m_trashxpm);
677         stock_pixbuf_gdk(STOCK_PIXMAP_TRASH_CLOSE_HRM_MARK, &m_trashhrmxpm);
678         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_CLOSE_MARK, &m_queuexpm);
679         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_CLOSE_HRM_MARK, &m_queuehrmxpm);
680         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_OPEN_MARK, &m_queueopenxpm);
681         stock_pixbuf_gdk(STOCK_PIXMAP_QUEUE_OPEN_HRM_MARK, &m_queueopenhrmxpm);
682         stock_pixbuf_gdk(STOCK_PIXMAP_DRAFTS_CLOSE_MARK, &m_draftsxpm);
683         stock_pixbuf_gdk(STOCK_PIXMAP_DRAFTS_OPEN_MARK, &m_draftsopenxpm);
684         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_SUBS_CLOSE_MARK, &m_foldersubsxpm);
685         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_NOSELECT_CLOSE_MARK, &m_foldernoselectxpm);
686
687         normal_font = pango_font_description_from_string(NORMAL_FONT);
688         if (normal_font) {
689                 gtk_widget_modify_font(ctree, normal_font);
690                 pango_font_description_free(normal_font);
691         }
692         gtk_cmclist_set_row_height(GTK_CMCLIST(ctree), 0);
693
694         if (!normal_style) {
695                 PangoFontDescription *font_desc;
696                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
697                 font_desc = pango_font_description_from_string(NORMAL_FONT);
698                 if (font_desc) {
699                         if (normal_style->font_desc)
700                                 pango_font_description_free
701                                         (normal_style->font_desc);
702                         normal_style->font_desc = font_desc;
703                 }
704                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
705                 normal_color_style = gtk_style_copy(normal_style);
706                 normal_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
707         }
708
709         if (!bold_style) {
710                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
711                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
712                 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
713                         PangoFontDescription *font_desc;
714                         font_desc = pango_font_description_from_string(NORMAL_FONT);
715                         if (font_desc) {
716                                 pango_font_description_free(bold_style->font_desc);
717                                 bold_style->font_desc = font_desc;
718                         }
719                         pango_font_description_set_weight
720                                 (bold_style->font_desc, PANGO_WEIGHT_BOLD);
721                 } else {
722                         PangoFontDescription *font_desc;
723                         font_desc = pango_font_description_from_string(BOLD_FONT);
724                         if (font_desc) {
725                                 if (bold_style->font_desc)
726                                         pango_font_description_free
727                                                 (bold_style->font_desc);
728                                 bold_style->font_desc = font_desc;
729                         }
730                 }
731                 bold_color_style = gtk_style_copy(bold_style);
732                 bold_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
733
734                 bold_tgtfold_style = gtk_style_copy(bold_style);
735                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
736         }
737 }
738
739 static gboolean folderview_defer_set(gpointer data)
740 {
741         FolderView *folderview = (FolderView *)data;
742         MainWindow *mainwin = folderview->mainwin;
743         
744         if (!mainwin)
745                 return FALSE;
746         if (mainwin->lock_count)
747                 return TRUE;
748                 
749         debug_print("doing deferred folderview_set now\n");
750         folderview_set(folderview);
751
752         folderview->deferred_refresh_id = 0;
753         return FALSE;
754 }
755
756 void folderview_set(FolderView *folderview)
757 {
758         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
759         MainWindow *mainwin = folderview->mainwin;
760         FolderItem *sel_item = NULL, *op_item = NULL;
761
762         if (!mainwin)
763                 return;
764
765         if (mainwin->lock_count) {
766                 if (folderview->deferred_refresh_id == 0)
767                         folderview->deferred_refresh_id = 
768                                 g_timeout_add(500, folderview_defer_set, folderview);
769                 debug_print("deferred folderview_set\n");
770                 return;
771         }
772
773         inc_lock();
774         debug_print("Setting folder info...\n");
775         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
776
777         main_window_cursor_wait(mainwin);
778
779         if (folderview->selected)
780                 sel_item = folderview_get_selected_item(folderview);
781         if (folderview->opened)
782                 op_item = folderview_get_opened_item(folderview);
783
784         folderview->selected = NULL;
785         folderview->opened = NULL;
786
787         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
788         gtk_cmclist_clear(GTK_CMCLIST(ctree));
789
790         folderview_set_folders(folderview);
791
792         if (sel_item)
793                 folderview->selected = gtk_cmctree_find_by_row_data(ctree, NULL, sel_item);
794         if (op_item)
795                 folderview->opened = gtk_cmctree_find_by_row_data(ctree, NULL, op_item);
796
797         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
798         main_window_cursor_normal(mainwin);
799         STATUSBAR_POP(mainwin);
800         inc_unlock();
801 }
802
803 void folderview_set_all(void)
804 {
805         GList *list;
806
807         for (list = folderview_list; list != NULL; list = list->next)
808                 folderview_set((FolderView *)list->data);
809 }
810
811 void folderview_select(FolderView *folderview, FolderItem *item)
812 {
813         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
814         GtkCMCTreeNode *node;
815         GtkCMCTreeNode *old_selected = folderview->selected;
816
817         if (!item) return;
818
819         node = gtk_cmctree_find_by_row_data(ctree, NULL, item);
820         if (node) folderview_select_node(folderview, node);
821
822         if (old_selected != node)
823                 folder_update_op_count();
824 }
825
826 static void mark_all_read_cb(GtkAction *action, gpointer data)
827 {
828         mark_all_read_handler(action, data, FALSE);
829 }
830
831 static void mark_all_read_recursive_cb(GtkAction *action, gpointer data)
832 {
833         mark_all_read_handler(action, data, TRUE);
834 }
835
836 static void mark_all_read_handler(GtkAction *action, gpointer data, gboolean recursive)
837 {
838         FolderView *folderview = (FolderView *)data;
839         FolderItem *item;
840         AlertValue val;
841         gchar *message;
842         
843         item = folderview_get_selected_item(folderview);
844         if (item == NULL)
845                 return;
846
847         message = recursive? _("Do you really want to mark all mails in this "
848                                "folder and its sub-folders as read?") :
849                              _("Do you really want to mark all mails in this "
850                                "folder as read?");
851         if (folderview->summaryview->folder_item != item &&
852             prefs_common.ask_mark_all_read) {
853                 val = alertpanel_full(_("Mark all as read"),
854                           message, GTK_STOCK_NO, GTK_STOCK_YES, NULL,
855                           TRUE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
856
857                 if ((val & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
858                         return;
859                 else if (val & G_ALERTDISABLE)
860                         prefs_common.ask_mark_all_read = FALSE;
861         }
862         
863         folder_item_update_freeze();
864         if (folderview->summaryview->folder_item != item && !recursive)
865                 summary_lock(folderview->summaryview);
866         else
867                 summary_freeze(folderview->summaryview);
868                 
869         if (recursive)
870                 folderutils_mark_all_read_recursive(item);
871         else
872                 folderutils_mark_all_read(item);
873         
874         if (folderview->summaryview->folder_item != item && !recursive)
875                 summary_unlock(folderview->summaryview);
876         else
877                 summary_thaw(folderview->summaryview);
878         folder_item_update_thaw();
879 }
880
881 static void folderview_select_node(FolderView *folderview, GtkCMCTreeNode *node)
882 {
883         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
884
885         cm_return_if_fail(node != NULL);
886
887         if (folderview->open_folder) {
888                 return;
889         }
890
891         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
892         gtkut_ctree_expand_parent_all(ctree, node);
893
894         folderview->open_folder = TRUE;
895         gtkut_ctree_set_focus_row(ctree, node);
896         gtk_cmctree_select(ctree, node);
897         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
898         if ((folderview->summaryview->folder_item &&
899             folderview->summaryview->folder_item->total_msgs > 0) ||
900              prefs_common.layout_mode == SMALL_LAYOUT)
901                 summary_grab_focus(folderview->summaryview);
902         else
903                 gtk_widget_grab_focus(folderview->ctree);
904 }
905
906 void folderview_unselect(FolderView *folderview)
907 {
908         if (folderview->opened && !GTK_CMCTREE_ROW(folderview->opened)->children)
909                 gtk_cmctree_collapse
910                         (GTK_CMCTREE(folderview->ctree), folderview->opened);
911
912         folderview->selected = folderview->opened = NULL;
913 }
914
915 static GtkCMCTreeNode *folderview_find_next_with_flag(GtkCMCTree *ctree,
916                                                       GtkCMCTreeNode *node,
917                                                       MsgPermFlags flag)
918 {
919         FolderItem *item;
920
921         if (node)
922                 node = gtkut_ctree_node_next(ctree, node);
923         else
924                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
925
926         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
927                 item = gtk_cmctree_node_get_row_data(ctree, node);
928                 if (!item)
929                         continue;
930                 if (item->stype == F_TRASH || item->stype == F_DRAFT)
931                         continue;
932                 switch (flag) {
933                 case MSG_UNREAD:
934                         if(item->unread_msgs > 0)
935                                 return node;
936                         break;
937                 case MSG_NEW:
938                         if(item->new_msgs > 0)
939                                 return node;
940                         break;
941                 case MSG_MARKED:
942                         if(item->marked_msgs > 0)
943                                 return node;
944                         break;
945                 default:
946                         if(item->total_msgs > 0)
947                                 return node;
948                         break;
949                 }
950         }
951
952         return NULL;
953 }
954
955 void folderview_select_next_with_flag(FolderView *folderview,
956                                       MsgPermFlags flag)
957 {
958         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
959         GtkCMCTreeNode *node = NULL;
960         EntryAction last_summary_select_prio = prefs_common.summary_select_prio[0];
961         
962         switch (flag) {
963         case MSG_UNREAD:
964                 prefs_common.summary_select_prio[0] = ACTION_UNREAD;
965                 break;
966         case MSG_NEW:
967                 prefs_common.summary_select_prio[0] = ACTION_NEW;
968                 break;
969         case MSG_MARKED:
970                 prefs_common.summary_select_prio[0] = ACTION_MARKED;
971                 break;
972         default:
973                 prefs_common.summary_select_prio[0] = ACTION_FIRST_LIST;
974                 break;
975         }
976
977         node = folderview_find_next_with_flag(ctree, folderview->opened, flag);
978         if (node != NULL) {
979                 folderview_select_node(folderview, node);
980                 goto out;
981         }
982
983         if (!folderview->opened ||
984             folderview->opened == GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list)) {
985                 goto out;
986         }
987
988         /* search again from the first node */
989         node = folderview_find_next_with_flag(ctree, NULL, flag);
990         if (node != NULL)
991                 folderview_select_node(folderview, node);
992
993 out:
994         prefs_common.summary_select_prio[0] = last_summary_select_prio;
995 }
996
997 FolderItem *folderview_get_selected_item(FolderView *folderview)
998 {
999         g_return_val_if_fail(folderview != NULL, NULL);
1000         g_return_val_if_fail(folderview->ctree != NULL, NULL);
1001
1002         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1003
1004         if (!folderview->selected) return NULL;
1005         return gtk_cmctree_node_get_row_data(ctree, folderview->selected);
1006 }
1007
1008 FolderItem *folderview_get_opened_item(FolderView *folderview)
1009 {
1010         g_return_val_if_fail(folderview != NULL, NULL);
1011         g_return_val_if_fail(folderview->ctree != NULL, NULL);
1012
1013         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1014
1015         if (!folderview->opened) return NULL;
1016         return gtk_cmctree_node_get_row_data(ctree, folderview->opened);
1017 }
1018
1019 static void folderview_set_folders(FolderView *folderview)
1020 {
1021         GList *list;
1022         list = folder_get_list();
1023
1024         for (; list != NULL; list = list->next) {
1025                 folderview_append_folder(folderview, FOLDER(list->data));
1026         }
1027 }
1028
1029 static gchar *get_scan_str(FolderItem *item)
1030 {
1031         if (item->path)
1032                 return g_strdup_printf(_("Scanning folder %s/%s..."),
1033                                       item->folder->name, item->path);
1034         else
1035                 return g_strdup_printf(_("Scanning folder %s..."),
1036                                       item->folder->name);      
1037 }
1038 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
1039                                       gpointer data)
1040 {
1041         GList *list;
1042         for (list = folderview_list; list != NULL; list = list->next) {
1043                 FolderView *folderview = (FolderView *)list->data;
1044                 MainWindow *mainwin = folderview->mainwin;
1045                 gchar *str = get_scan_str(item);
1046
1047                 STATUSBAR_PUSH(mainwin, str);
1048                 STATUSBAR_POP(mainwin);
1049                 g_free(str);
1050         }
1051 }
1052
1053 void folderview_rescan_tree(Folder *folder, gboolean rebuild)
1054 {
1055         GtkWidget *window;
1056         MainWindow *mainwin = mainwindow_get_mainwindow();
1057         FolderView *folderview = NULL;
1058         GtkAdjustment *pos = NULL;
1059         gint height = 0;
1060
1061         cm_return_if_fail(folder != NULL);
1062
1063         if (!folder->klass->scan_tree) return;
1064
1065         if (rebuild && 
1066             alertpanel_full(_("Rebuild folder tree"), 
1067                          _("Rebuilding the folder tree will remove "
1068                            "local caches. Do you want to continue?"),
1069                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, FALSE,
1070                          NULL, ALERT_WARNING, G_ALERTDEFAULT) 
1071                 != G_ALERTALTERNATE) {
1072                 return;
1073         }
1074
1075         inc_lock();
1076         if (rebuild)
1077                 window = label_window_create(_("Rebuilding folder tree..."));
1078         else 
1079                 window = label_window_create(_("Scanning folder tree..."));
1080
1081         if (mainwin)
1082                 folderview = mainwin->folderview;
1083         
1084         if (folderview) {
1085                 pos = gtk_scrolled_window_get_vadjustment(
1086                                         GTK_SCROLLED_WINDOW(folderview->scrolledwin));
1087                 height = gtk_adjustment_get_value(pos);
1088         }
1089
1090         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
1091         folder_scan_tree(folder, rebuild);
1092         folder_set_ui_func(folder, NULL, NULL);
1093
1094         folderview_set_all();
1095
1096         if (folderview) {
1097                 pos = gtk_scrolled_window_get_vadjustment(
1098                                         GTK_SCROLLED_WINDOW(folderview->scrolledwin));
1099                 gtk_adjustment_set_value(pos, height);
1100                 gtk_adjustment_changed(pos);
1101         }
1102         label_window_destroy(window);
1103         inc_unlock();
1104 }
1105
1106 /** folderview_check_new()
1107  *  Scan and update the folder and return the 
1108  *  count the number of new messages since last check. 
1109  *  \param folder the folder to check for new messages
1110  *  \return the number of new messages since last check
1111  */
1112 gint folderview_check_new(Folder *folder)
1113 {
1114         GList *list;
1115         FolderItem *item;
1116         FolderView *folderview;
1117         GtkCMCTree *ctree;
1118         GtkCMCTreeNode *node;
1119         gint new_msgs = 0;
1120         gint former_new_msgs = 0;
1121         gint former_new = 0, former_unread = 0, former_total;
1122
1123         for (list = folderview_list; list != NULL; list = list->next) {
1124                 folderview = (FolderView *)list->data;
1125                 ctree = GTK_CMCTREE(folderview->ctree);
1126                 folderview->scanning_folder = folder;
1127                 inc_lock();
1128                 main_window_lock(folderview->mainwin);
1129
1130                 for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
1131                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1132                         gchar *str = NULL;
1133                         item = gtk_cmctree_node_get_row_data(ctree, node);
1134                         if (!item || !item->path || !item->folder) continue;
1135                         if (item->no_select) continue;
1136                         if (folder && folder != item->folder) continue;
1137                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1138                         if (!item->prefs->newmailcheck) continue;
1139                         if (item->processing_pending == TRUE) {
1140                                 debug_print("skipping %s, processing pending\n",
1141                                         item->path ? item->path : item->name);
1142                                 continue;
1143                         }
1144                         if (item->scanning != ITEM_NOT_SCANNING) {
1145                                 debug_print("skipping %s, scanning\n",
1146                                         item->path ? item->path : item->name);
1147                                 continue;
1148                         }
1149
1150                         str = get_scan_str(item);
1151
1152                         STATUSBAR_PUSH(folderview->mainwin, str);
1153                         GTK_EVENTS_FLUSH();
1154                         g_free(str);
1155
1156                         folderview_scan_tree_func(item->folder, item, NULL);
1157                         former_new    = item->new_msgs;
1158                         former_unread = item->unread_msgs;
1159                         former_total  = item->total_msgs;
1160
1161                         if (item->folder->klass->scan_required &&
1162                             (item->folder->klass->scan_required(item->folder, item) ||
1163                              item->folder->inbox == item ||
1164                              item->opened == TRUE ||
1165                              item->processing_pending == TRUE)) {
1166                                 if (folder_item_scan(item) < 0) {
1167                                         if (folder) {
1168                                                 summaryview_unlock(folderview->summaryview, item);
1169                                                 if (FOLDER_TYPE(item->folder) == F_NEWS || FOLDER_IS_LOCAL(folder)) {
1170                                                         log_error(LOG_PROTOCOL, _("Couldn't scan folder %s\n"),
1171                                                                 item->path ? item->path:item->name);
1172                                                         STATUSBAR_POP(folderview->mainwin);
1173                                                         continue;
1174                                                 } else if (!FOLDER_IS_LOCAL(folder)) {
1175                                                         STATUSBAR_POP(folderview->mainwin);
1176                                                         break;
1177                                                 }
1178                                         }
1179                                 }
1180                         } else if (!item->folder->klass->scan_required) {
1181                                 if (folder_item_scan(item) < 0) {
1182                                         summaryview_unlock(folderview->summaryview, item);
1183                                         if (folder && !FOLDER_IS_LOCAL(folder)) {
1184                                                 STATUSBAR_POP(folderview->mainwin);
1185                                                 break;
1186                                         }
1187                                 }
1188                         }
1189                         if (former_new    != item->new_msgs ||
1190                             former_unread != item->unread_msgs ||
1191                             former_total  != item->total_msgs)
1192                                 folderview_update_node(folderview, node);
1193
1194                         new_msgs += item->new_msgs;
1195                         former_new_msgs += former_new;
1196                         STATUSBAR_POP(folderview->mainwin);
1197                 }
1198                 folderview->scanning_folder = NULL;
1199                 main_window_unlock(folderview->mainwin);
1200                 inc_unlock();
1201         }
1202
1203         folder_write_list();
1204         /* Number of new messages since last check is the just the difference 
1205          * between former_new_msgs and new_msgs. If new_msgs is less than
1206          * former_new_msgs, that would mean another session accessed the folder
1207          * and the result is not well defined.
1208          */
1209         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1210         return new_msgs;
1211 }
1212
1213 void folderview_check_new_all(void)
1214 {
1215         GList *list;
1216         GtkWidget *window;
1217         FolderView *folderview;
1218
1219         folderview = (FolderView *)folderview_list->data;
1220
1221         inc_lock();
1222         main_window_lock(folderview->mainwin);
1223         window = label_window_create
1224                 (_("Checking for new messages in all folders..."));
1225
1226         list = folder_get_list();
1227         for (; list != NULL; list = list->next) {
1228                 Folder *folder = list->data;
1229
1230                 folderview_check_new(folder);
1231         }
1232
1233         folder_write_list();
1234         folderview_set_all();
1235
1236         label_window_destroy(window);
1237         main_window_unlock(folderview->mainwin);
1238         inc_unlock();
1239 }
1240
1241 static gboolean folderview_have_children_sub(FolderView *folderview,
1242                                              FolderItem *item,
1243                                              gboolean in_sub)
1244 {
1245         GNode *node = NULL;
1246         
1247         if (!item || !item->folder || !item->folder->node)
1248                 return FALSE;
1249                 
1250         node = item->folder->node;
1251         
1252         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1253         node = node->children;
1254
1255         if (in_sub && item->total_msgs > 0) {
1256                 return TRUE;
1257         }
1258
1259         while (node != NULL) {
1260                 if (node && node->data) {
1261                         FolderItem *next_item = (FolderItem*) node->data;
1262                         node = node->next;
1263                         if (folderview_have_children_sub(folderview, 
1264                                                          next_item, TRUE))
1265                                 return TRUE;
1266                 }
1267         }
1268
1269         return FALSE;
1270 }
1271
1272 static gboolean folderview_have_children(FolderView *folderview,
1273                                          FolderItem *item)
1274 {
1275         return folderview_have_children_sub(folderview, item, FALSE);
1276 }
1277
1278 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1279                                                  FolderItem *item,
1280                                                  gboolean in_sub)
1281 {
1282         GNode *node = NULL;
1283         
1284         if (!item || !item->folder || !item->folder->node)
1285                 return FALSE;
1286                 
1287         node = item->folder->node;
1288         
1289         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1290         node = node->children;
1291
1292         if (in_sub &&
1293             (item->new_msgs > 0 ||
1294             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1295                 return TRUE;
1296         }
1297
1298         while (node != NULL) {
1299                 if (node && node->data) {
1300                         FolderItem *next_item = (FolderItem*) node->data;
1301                         node = node->next;
1302                         if (folderview_have_new_children_sub(folderview, 
1303                                                              next_item, TRUE))
1304                                 return TRUE;
1305                 }
1306         }
1307
1308         return FALSE;
1309 }
1310
1311 static gboolean folderview_have_new_children(FolderView *folderview,
1312                                              FolderItem *item)
1313 {
1314         return folderview_have_new_children_sub(folderview, item, FALSE);
1315 }
1316
1317 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1318                                                     FolderItem *item, 
1319                                                     gboolean in_sub)
1320 {
1321         GNode *node = NULL;
1322         
1323         if (!item || !item->folder || !item->folder->node)
1324                 return FALSE;
1325         
1326         node = item->folder->node;
1327         
1328         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1329         node = node->children;
1330
1331         if (in_sub &&
1332             (item->unread_msgs > 0 ||
1333             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1334                 return TRUE;
1335         }
1336
1337         while (node != NULL) {
1338                 if (node && node->data) {
1339                         FolderItem *next_item = (FolderItem*) node->data;
1340                         node = node->next;
1341                         if (folderview_have_unread_children_sub(folderview, 
1342                                                                 next_item, 
1343                                                                 TRUE))
1344                                 return TRUE;
1345                 }
1346         }
1347
1348         return FALSE;
1349 }
1350
1351 static gboolean folderview_have_unread_children(FolderView *folderview,
1352                                                 FolderItem *item)
1353 {
1354         return folderview_have_unread_children_sub(folderview, item, FALSE);
1355 }
1356
1357 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1358                                                       FolderItem *item,
1359                                                       gboolean in_sub)
1360 {
1361         GNode *node = NULL;
1362
1363         if (!item || !item->folder || !item->folder->node)
1364                 return FALSE;
1365
1366         node = item->folder->node;
1367         
1368         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1369         node = node->children;
1370
1371         if (in_sub && item->search_match){
1372                 return TRUE;
1373         }
1374
1375         while (node != NULL) {
1376                 if (node && node->data) {
1377                         FolderItem *next_item = (FolderItem*) node->data;
1378                         node = node->next;
1379                         if (folderview_have_matching_children_sub(folderview, 
1380                                                                   next_item, 
1381                                                                   TRUE))
1382                                 return TRUE;
1383                 }
1384         }
1385
1386         return FALSE;
1387 }
1388
1389 static gboolean folderview_have_matching_children(FolderView *folderview,
1390                                                   FolderItem *item)
1391 {
1392         return folderview_have_matching_children_sub(folderview, item, FALSE);
1393 }
1394
1395 static gboolean folderview_have_marked_children_sub(FolderView *folderview,
1396                                                     FolderItem *item,
1397                                                     gboolean in_sub)
1398 {
1399         GNode *node = NULL;
1400         
1401         if (!item || !item->folder || !item->folder->node)
1402                 return FALSE;
1403                 
1404         node = item->folder->node;
1405         
1406         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1407         node = node->children;
1408
1409         if (item->marked_msgs != 0) {
1410                 return TRUE;
1411         }
1412
1413         while (node != NULL) {
1414                 if (node && node->data) {
1415                         FolderItem *next_item = (FolderItem*) node->data;
1416                         node = node->next;
1417                         if (folderview_have_marked_children_sub(folderview,
1418                                                                 next_item, TRUE))
1419                                 return TRUE;
1420                 }
1421         }
1422
1423         return FALSE;
1424 }
1425
1426 static gboolean folderview_have_marked_children(FolderView *folderview,
1427                                              FolderItem *item)
1428 {
1429         return folderview_have_marked_children_sub(folderview, item, FALSE);
1430 }
1431
1432 static void folderview_update_node(FolderView *folderview, GtkCMCTreeNode *node)
1433 {
1434         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1435         GtkStyle *style = NULL;
1436         GtkStyle *color_style = NULL;
1437         FolderItem *item;
1438         GdkPixbuf *xpm, *openxpm;
1439         static GdkPixbuf *searchicon;
1440         gboolean mark = FALSE;
1441         gchar *name;
1442         gchar *str;
1443         gboolean add_unread_mark;
1444         gboolean add_sub_match_mark;
1445         gboolean use_bold, use_color;
1446         gint *col_pos = folderview->col_pos;
1447         SpecialFolderItemType stype;
1448         
1449         item = gtk_cmctree_node_get_row_data(ctree, node);
1450         cm_return_if_fail(item != NULL);
1451
1452         if (!GTK_CMCTREE_ROW(node)->expanded)
1453                 mark = folderview_have_marked_children(folderview, item);
1454         else
1455                 mark = (item->marked_msgs != 0);
1456
1457         stype = item->stype;
1458         if (stype == F_NORMAL) {
1459                 if (folder_has_parent_of_type(item, F_TRASH))
1460                         stype = F_TRASH;
1461                 else if (folder_has_parent_of_type(item, F_DRAFT))
1462                         stype = F_DRAFT;
1463                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1464                         stype = F_OUTBOX;
1465                 else if (folder_has_parent_of_type(item, F_QUEUE))
1466                         stype = F_QUEUE;
1467         }
1468         switch (stype) {
1469         case F_INBOX:
1470                 if (item->hide_read_msgs || item->hide_read_threads) {
1471                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1472                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1473                 } else {
1474                         xpm = mark?m_inboxxpm:inboxxpm;
1475                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1476                 }
1477                 break;
1478         case F_OUTBOX:
1479                 if (item->hide_read_msgs || item->hide_read_threads) {
1480                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1481                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1482                 } else {
1483                         xpm = mark?m_outboxxpm:outboxxpm;
1484                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1485                 }
1486                 break;
1487         case F_QUEUE:
1488                 if (item->hide_read_msgs || item->hide_read_threads) {
1489                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1490                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1491                 } else {
1492                         xpm = mark?m_queuexpm:queuexpm;
1493                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1494                 }
1495                 break;
1496         case F_TRASH:
1497                 if (item->hide_read_msgs || item->hide_read_threads) {
1498                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1499                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1500                 } else {
1501                         xpm = mark?m_trashxpm:trashxpm;
1502                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1503                 }
1504                 break;
1505         case F_DRAFT:
1506                 xpm = mark?m_draftsxpm:draftsxpm;
1507                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1508                 break;
1509         default:
1510                 if (!item->path &&
1511                     FOLDER_TYPE(item->folder) == F_IMAP &&
1512                     item->folder->account->imap_subsonly) {
1513                         xpm = mark?m_foldersubsxpm:foldersubsxpm;
1514                         openxpm = foldersubsopenxpm;
1515                 } else if (item->no_select) {
1516                         xpm = mark?m_foldernoselectxpm:foldernoselectxpm;
1517                         openxpm = foldernoselectopenxpm;
1518                 } else if (item->hide_read_msgs || item->hide_read_threads) {
1519                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1520                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1521                 } else {
1522                         xpm = mark?m_folderxpm:folderxpm;
1523                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1524                 }
1525         }
1526
1527         name = folder_item_get_name(item);
1528
1529         if (!GTK_CMCTREE_ROW(node)->expanded) {
1530                 add_unread_mark = folderview_have_unread_children(
1531                                         folderview, item);
1532                 add_sub_match_mark = folderview_have_matching_children(
1533                                         folderview, item);
1534         } else {
1535                 add_unread_mark = FALSE;
1536                 add_sub_match_mark = FALSE;
1537         }
1538
1539         if (item->search_match) {
1540                 if (!searchicon) {
1541                         stock_pixbuf_gdk(STOCK_PIXMAP_QUICKSEARCH,
1542                          &searchicon);
1543                 }
1544                 xpm = openxpm = searchicon;
1545         }
1546
1547         str = NULL;
1548         if (prefs_common.display_folder_unread) {
1549                 if (folder_has_parent_of_type(item, F_QUEUE)) {
1550                         /* only total_msgs matters here */
1551                         if (item->total_msgs > 0) {
1552                                 /* show total number (should be equal to the unread number)
1553                                    and signs if any */
1554                                 str = g_strdup_printf("%s (%d%s%s)",
1555                                                         name, item->total_msgs,
1556                                                         (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1557                                                         (item->unreadmarked_msgs > 0) ? "!" : "");
1558                         }
1559                 } else {
1560                         if (prefs_common.display_folder_unread == 1) {
1561                                 if (item->unread_msgs > 0) {
1562                                         /* show unread number and signs */
1563                                         str = g_strdup_printf("%s (%d%s%s)",
1564                                                                 name, item->unread_msgs,
1565                                                                 (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1566                                                                 (item->unreadmarked_msgs > 0) ? "!" : "");
1567                                 }
1568                         } else {
1569                                 if (item->total_msgs > 0) {
1570                                         /* show unread number, total number and signs if any */
1571                                         str = g_strdup_printf("%s (%d/%d%s%s)",
1572                                                                 name, item->unread_msgs, item->total_msgs,
1573                                                                 (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1574                                                                 (item->unreadmarked_msgs > 0) ? "!" : "");
1575                                 }
1576                         }
1577                 }
1578                 if ((str == NULL) &&
1579                         (add_unread_mark || add_sub_match_mark || (item->unreadmarked_msgs > 0))) {
1580                         /* no unread/total numbers, but at least one sign */
1581                         str = g_strdup_printf("%s (%s%s)",
1582                                                 name,
1583                                                 (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1584                                                 (item->unreadmarked_msgs > 0) ? "!" : "");
1585                 }
1586         }
1587         if (str == NULL) {
1588                 /* last fallback, folder name only or with +! sign */
1589                 if (item->unreadmarked_msgs > 0 && add_sub_match_mark) {
1590                         str = g_strdup_printf("%s%s",
1591                                                 name, " (+!)");
1592                 } else if (item->unreadmarked_msgs > 0) {
1593                         str = g_strdup_printf("%s%s",
1594                                                 name, " (!)");
1595                 } else if (add_sub_match_mark) {
1596                         str = g_strdup_printf("%s%s",
1597                                                 name, " (+)");
1598                 } else {
1599                         str = g_strdup_printf("%s", name);
1600                 }
1601         }
1602         gtk_cmctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1603                                 xpm, openxpm, 
1604                                 FALSE, GTK_CMCTREE_ROW(node)->expanded);
1605         g_free(str);
1606         g_free(name);
1607
1608         if (!folder_item_parent(item)) {
1609                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1610                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1611                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1612         } else {
1613                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    item->new_msgs    > 0 ? itos(item->new_msgs)    : prefs_common.zero_replacement);
1614                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], item->unread_msgs > 0 ? itos(item->unread_msgs) : prefs_common.zero_replacement);
1615                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  item->total_msgs  > 0 ? itos(item->total_msgs)  : prefs_common.zero_replacement);
1616         }
1617
1618         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1619             folder_has_parent_of_type(item, F_TRASH)) {
1620                 use_bold = use_color = FALSE;
1621         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1622                 GSList *list = folder_item_get_msg_list(item);
1623                 GSList *cur;
1624                 use_bold = use_color = FALSE;
1625                 for (cur = list; cur; cur = cur->next) {
1626                         MsgInfo *msginfo = (MsgInfo *)cur->data;
1627                         if (!MSG_IS_DELETED(msginfo->flags)) {
1628                                 /* highlight queue folder if there are any messages */
1629                                 use_bold = use_color = TRUE;
1630                                 break;
1631                         }
1632                 }
1633                 if (!GTK_CMCTREE_ROW(node)->expanded &&
1634                     use_bold == FALSE &&
1635                     folderview_have_children(folderview, item))
1636                         use_bold = use_color = TRUE;
1637                 procmsg_msg_list_free(list);
1638         } else {
1639                 /* if unread messages exist, print with bold font */
1640                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1641                                 || add_unread_mark;
1642                 /* if new messages exist, print with colored letter */
1643                 use_color =
1644                         (item->new_msgs > 0) ||
1645                         (add_unread_mark &&
1646                          folderview_have_new_children(folderview, item));       
1647         }
1648
1649         gtk_cmctree_node_set_foreground(ctree, node, NULL);
1650
1651         if (use_bold) {
1652                 GdkColor gdk_color;
1653
1654                 if (item->prefs->color > 0 && !use_color) {
1655                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1656                         color_style = gtk_style_copy(bold_style);
1657                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1658                         style = color_style;
1659                 } else if (use_color) {
1660                         style = bold_color_style;
1661                 } else
1662                         style = bold_style;
1663                 if (item->op_count > 0) {
1664                         style = bold_tgtfold_style;
1665                 }
1666         } else if (use_color) {
1667                 style = normal_color_style;
1668                 gtk_cmctree_node_set_foreground(ctree, node,
1669                                               &folderview->color_new);
1670         } else if (item->op_count > 0) {
1671                 style = bold_tgtfold_style;
1672         } else if (item->prefs->color > 0) {
1673                 GdkColor gdk_color;
1674                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1675                 color_style = gtk_style_copy(normal_style);
1676                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1677                 style = color_style;
1678         } else {
1679                 style = normal_style;
1680         }
1681
1682         gtk_cmctree_node_set_row_style(ctree, node, style);
1683
1684         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1685                 folderview_update_node(folderview, node);
1686 }
1687
1688 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1689 {
1690         GList *list;
1691         FolderView *folderview;
1692         GtkCMCTree *ctree;
1693         GtkCMCTreeNode *node;
1694
1695         cm_return_if_fail(item != NULL);
1696
1697         for (list = folderview_list; list != NULL; list = list->next) {
1698                 folderview = (FolderView *)list->data;
1699                 ctree = GTK_CMCTREE(folderview->ctree);
1700
1701                 node = gtk_cmctree_find_by_row_data(ctree, NULL, item);
1702                 if (node && item->search_match != matches) {
1703                         item->search_match = matches;
1704                         folderview_update_node(folderview, node);
1705                 }
1706         }
1707 }
1708
1709 static gboolean folderview_update_item_claws(gpointer source, gpointer data)
1710 {
1711         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1712         FolderView *folderview = (FolderView *)data;
1713         GtkCMCTree *ctree;
1714         GtkCMCTreeNode *node;
1715         cm_return_val_if_fail(update_info != NULL, TRUE);
1716         cm_return_val_if_fail(update_info->item != NULL, TRUE);
1717         cm_return_val_if_fail(folderview != NULL, FALSE);
1718
1719         ctree = GTK_CMCTREE(folderview->ctree);
1720
1721         node = gtk_cmctree_find_by_row_data(ctree, NULL, update_info->item);
1722
1723         if (node) {
1724                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1725                         folderview_update_node(folderview, node);
1726
1727                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && 
1728                      update_info->item == folderview->summaryview->folder_item &&
1729                      update_info->item != NULL)
1730                         if (!quicksearch_has_sat_predicate(folderview->summaryview->quicksearch))
1731                                 summary_show(folderview->summaryview, update_info->item);
1732         }
1733         
1734         return FALSE;
1735 }
1736
1737 static gboolean folderview_gnode_func(GtkCMCTree *ctree, guint depth,
1738                                       GNode *gnode, GtkCMCTreeNode *cnode,
1739                                       gpointer data)
1740 {
1741         FolderView *folderview = (FolderView *)data;
1742         FolderItem *item = FOLDER_ITEM(gnode->data);
1743
1744         cm_return_val_if_fail(item != NULL, FALSE);
1745
1746         gtk_cmctree_node_set_row_data(ctree, cnode, item);
1747         folderview_update_node(folderview, cnode);
1748
1749         return TRUE;
1750 }
1751
1752 static void folderview_expand_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
1753                                    gpointer data)
1754 {
1755         FolderView *folderview = (FolderView *)data;
1756         FolderItem *item;
1757
1758         if (GTK_CMCTREE_ROW(node)->children) {
1759                 item = gtk_cmctree_node_get_row_data(ctree, node);
1760                 cm_return_if_fail(item != NULL);
1761
1762                 if (!item->collapsed)
1763                         gtk_cmctree_expand(ctree, node);
1764                 else
1765                         folderview_update_node(folderview, node);
1766         }
1767 }
1768
1769 static void set_special_folder(GtkCMCTree *ctree, FolderItem *item,
1770                                GtkCMCTreeNode *root, GtkCMCTreeNode **prev)
1771 {
1772         if (item) {
1773                 GtkCMCTreeNode *node, *parent, *sibling;
1774
1775                 node = gtk_cmctree_find_by_row_data(ctree, root, item);
1776                 if (!node)
1777                         g_warning("%s not found.", item->path);
1778                 else {
1779                         parent = GTK_CMCTREE_ROW(node)->parent;
1780                         if (*prev && parent == GTK_CMCTREE_ROW(*prev)->parent)
1781                                 sibling = GTK_CMCTREE_ROW(*prev)->sibling;
1782                         else
1783                                 sibling = GTK_CMCTREE_ROW(parent)->children;
1784                         while (sibling) {
1785                                 FolderItem *tmp;
1786
1787                                 tmp = gtk_cmctree_node_get_row_data
1788                                         (ctree, sibling);
1789                                 if (tmp && tmp->stype != F_NORMAL)
1790                                         sibling = GTK_CMCTREE_ROW(sibling)->sibling;
1791                                 else
1792                                         break;
1793                         }
1794                         if (node != sibling)
1795                                 gtk_cmctree_move(ctree, node, parent, sibling);
1796                 }
1797
1798                 *prev = node;
1799         }
1800 }
1801
1802 static void folderview_sort_folders(FolderView *folderview, GtkCMCTreeNode *root,
1803                                     Folder *folder)
1804 {
1805         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1806         GtkCMCTreeNode *prev = NULL;
1807
1808         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
1809         gtk_sctree_sort_recursive(ctree, root);
1810         if (root && GTK_CMCTREE_ROW(root)->parent) {
1811                 gtk_cmclist_thaw(GTK_CMCLIST(ctree));
1812                 return;
1813         }
1814         set_special_folder(ctree, folder->inbox, root, &prev);
1815         set_special_folder(ctree, folder->outbox, root, &prev);
1816         set_special_folder(ctree, folder->draft, root, &prev);
1817         set_special_folder(ctree, folder->queue, root, &prev);
1818         set_special_folder(ctree, folder->trash, root, &prev);
1819         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
1820 }
1821
1822 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1823 {
1824         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1825         GtkCMCTreeNode *root;
1826
1827         cm_return_if_fail(folder != NULL);
1828
1829         root = gtk_sctree_insert_gnode(ctree, NULL, NULL, folder->node,
1830                                       folderview_gnode_func, folderview);
1831         gtk_cmctree_pre_recursive(ctree, root, folderview_expand_func,
1832                                 folderview);
1833         folderview_sort_folders(folderview, root, folder);
1834 }
1835
1836 /* callback functions */
1837 static void folderview_set_sens_and_popup_menu(FolderView *folderview, gint row, 
1838                                 GdkEventButton *event)
1839 {
1840         FolderItem *item;
1841         Folder *folder;
1842         FolderViewPopup *fpopup;
1843         GtkActionGroup *action_group;
1844         GtkWidget *popup;
1845         FolderItem *special_trash = NULL, *special_queue = NULL;
1846         PrefsAccount *ac;
1847         GtkUIManager *ui_manager = gtk_ui_manager_new();
1848         
1849         if (folderview->ui_manager)
1850                 g_object_unref(folderview->ui_manager);
1851
1852         folderview->ui_manager = ui_manager;
1853         item = folderview_get_selected_item(folderview);
1854
1855         cm_return_if_fail(item != NULL);
1856         cm_return_if_fail(item->folder != NULL);
1857         folder = item->folder;
1858
1859         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1860
1861         if (fpopup != NULL)
1862                 action_group = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1863         else {
1864                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1865                 action_group = g_hash_table_lookup(folderview->popups, "common");
1866         }
1867         
1868         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
1869         MENUITEM_ADDUI_MANAGER(ui_manager, "/", "Popup", "Popup", GTK_UI_MANAGER_MENUBAR)
1870         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup", "FolderViewPopup", "FolderViewPopup", GTK_UI_MANAGER_MENU)
1871         
1872         if (fpopup->add_menuitems)      
1873                 fpopup->add_menuitems(ui_manager, item);
1874
1875         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MarkAllRead", "FolderViewPopup/MarkAllRead", GTK_UI_MANAGER_MENUITEM)
1876         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MarkAllReadRec", "FolderViewPopup/MarkAllReadRec", GTK_UI_MANAGER_MENUITEM)
1877         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Separator1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1878         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RunProcessing", "FolderViewPopup/RunProcessing", GTK_UI_MANAGER_MENUITEM)
1879         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SearchFolder", "FolderViewPopup/SearchFolder", GTK_UI_MANAGER_MENUITEM)
1880         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Properties", "FolderViewPopup/Properties", GTK_UI_MANAGER_MENUITEM)
1881         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Processing", "FolderViewPopup/Processing", GTK_UI_MANAGER_MENUITEM)
1882
1883         if (fpopup->set_sensitivity != NULL)
1884                 fpopup->set_sensitivity(ui_manager, item);
1885
1886         if (NULL != (ac = account_find_from_item(item))) {
1887                 special_trash = account_get_special_folder(ac, F_TRASH);
1888                 special_queue = account_get_special_folder(ac, F_QUEUE);
1889         }
1890         
1891         if ((item == folder->trash || item == special_trash
1892              || folder_has_parent_of_type(item, F_TRASH))) {
1893                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorTrash", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1894                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "EmptyTrash", "FolderViewPopup/EmptyTrash", GTK_UI_MANAGER_MENUITEM)
1895         } 
1896         
1897         if ((item == folder->queue || item == special_queue
1898              || folder_has_parent_of_type(item, F_QUEUE))) {
1899                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorQueue", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1900                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SendQueue", "FolderViewPopup/SendQueue", GTK_UI_MANAGER_MENUITEM)
1901         } 
1902         
1903 #define SET_SENS(name, sens) \
1904         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
1905
1906         SET_SENS("FolderViewPopup/MarkAllRead", item->unread_msgs >= 1);
1907         SET_SENS("FolderViewPopup/MarkAllReadRec", folderview_have_unread_children(folderview,item));
1908         SET_SENS("FolderViewPopup/SearchFolder", item->total_msgs >= 1 && 
1909                  folderview->selected == folderview->opened);
1910         SET_SENS("FolderViewPopup/Properties", TRUE);
1911
1912         SET_SENS("FolderViewPopup/RunProcessing", item->prefs->processing &&
1913                  item->total_msgs >= 1 && !item->processing_pending);
1914         SET_SENS("FolderViewPopup/Processing", item->node->parent != NULL && 
1915                 !item->no_select && !item->processing_pending);
1916
1917         if (item == folder->trash || item == special_trash
1918             || folder_has_parent_of_type(item, F_TRASH)) {
1919                 GSList *msglist = folder_item_get_msg_list(item);
1920                 SET_SENS("FolderViewPopup/EmptyTrash", msglist != NULL);
1921                 procmsg_msg_list_free(msglist);
1922         }
1923         if (item == folder->queue || item == special_queue
1924             || folder_has_parent_of_type(item, F_QUEUE)) {
1925                 GSList *msglist = folder_item_get_msg_list(item);
1926                 SET_SENS("FolderViewPopup/SendQueue", msglist != NULL);
1927                 procmsg_msg_list_free(msglist);
1928         }
1929 #undef SET_SENS
1930
1931         popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1932                         gtk_ui_manager_get_widget(ui_manager, "/Popup/FolderViewPopup")) );
1933         g_signal_connect(G_OBJECT(popup), "selection_done",
1934                          G_CALLBACK(folderview_popup_close),
1935                          folderview);
1936         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1937                        event->button, event->time);
1938 }
1939
1940 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1941                                           FolderView *folderview)
1942 {
1943         GtkCMCList *clist = GTK_CMCLIST(ctree);
1944         gint prev_row = -1, row = -1, column = -1;
1945
1946         if (!event) return FALSE;
1947
1948         if (event->button == 1 || event->button == 2) {
1949                 if (!gtk_sctree_is_hot_spot (GTK_SCTREE(clist), event->x, event->y))
1950                         folderview->open_folder = TRUE;
1951
1952                 if (event->type == GDK_2BUTTON_PRESS) {
1953                         if (clist->selection) {
1954                                 GtkCMCTreeNode *node;
1955
1956                                 node = GTK_CMCTREE_NODE(clist->selection->data);
1957                                 if (node) {
1958                                         gtk_cmctree_toggle_expansion(
1959                                                 GTK_CMCTREE(ctree),
1960                                                 node);
1961                                         folderview->open_folder = FALSE;
1962                                 }
1963                         }
1964                 }
1965                 return FALSE;
1966         }
1967
1968         if (event->button == 2 || event->button == 3) {
1969                 /* right clicked */
1970                 if (clist->selection) {
1971                         GtkCMCTreeNode *node;
1972
1973                         node = GTK_CMCTREE_NODE(clist->selection->data);
1974                         if (node)
1975                                 prev_row = gtkut_ctree_get_nth_from_node
1976                                         (GTK_CMCTREE(ctree), node);
1977                 }
1978
1979                 if (!gtk_cmclist_get_selection_info(clist, event->x, event->y,
1980                                                   &row, &column))
1981                         return FALSE;
1982                 if (prev_row != row) {
1983                         gtk_cmclist_unselect_all(clist);
1984                         if (event->button == 2)
1985                                 folderview_select_node
1986                                         (folderview,
1987                                          gtk_cmctree_node_nth(GTK_CMCTREE(ctree),
1988                                                             row));
1989                         else
1990                                 gtk_cmclist_select_row(clist, row, column);
1991                 }
1992         }
1993
1994         if (event->button != 3) return FALSE;
1995
1996         folderview_set_sens_and_popup_menu(folderview, row, event);
1997         return FALSE;
1998 }
1999
2000 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
2001                                            FolderView *folderview)
2002 {
2003         int row = -1, column = -1;
2004
2005         if (!event) return FALSE;
2006
2007         if (!gtk_cmclist_get_selection_info(GTK_CMCLIST(ctree), event->x, event->y,
2008                                           &row, &column))
2009                 return FALSE;
2010         if (event->button == 1 && folderview->open_folder == FALSE &&
2011             folderview->opened != NULL) {
2012                 gtkut_ctree_set_focus_row(GTK_CMCTREE(ctree),
2013                                           folderview->opened);
2014                 gtk_cmctree_select(GTK_CMCTREE(ctree), folderview->opened);
2015         }
2016
2017         return FALSE;
2018 }
2019
2020 #define BREAK_ON_MODIFIER_KEY() \
2021         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2022
2023 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2024                                        FolderView *folderview)
2025 {
2026         GtkCMCTreeNode *node;
2027         FolderItem *item;
2028
2029         if (!event) return FALSE;
2030
2031         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
2032                 return FALSE;
2033
2034         switch (event->keyval) {
2035         case GDK_KEY_Right:
2036                 if (folderview->selected) {
2037                         if (GTK_CMCTREE_ROW(folderview->selected)->children != NULL
2038                                         && !GTK_CMCTREE_ROW(folderview->selected)->expanded)
2039                                 gtk_cmctree_expand(GTK_CMCTREE(folderview->ctree),
2040                                                 folderview->selected);
2041                         else
2042                                 folderview_select_node(folderview,
2043                                                        folderview->selected);
2044                 }
2045                 break;
2046 #ifdef GENERIC_UMPC
2047         case GDK_KEY_Return:
2048                 if (folderview->selected && GTK_CMCTREE_ROW(folderview->selected)->children) {
2049                         gtk_cmctree_toggle_expansion(
2050                                 GTK_CMCTREE(folderview->ctree),
2051                                 folderview->selected);
2052                 }
2053                 break;  
2054 #else
2055         case GDK_KEY_Return:
2056         case GDK_KEY_KP_Enter:
2057                 if (folderview->selected)
2058                         folderview_select_node(folderview, folderview->selected);
2059                 break;
2060 #endif
2061         case GDK_KEY_space:
2062                 BREAK_ON_MODIFIER_KEY();
2063                 if (folderview->selected) {
2064                         if (folderview->opened == folderview->selected &&
2065                             (!folderview->summaryview->folder_item ||
2066                              folderview->summaryview->folder_item->total_msgs == 0))
2067                                 folderview_select_next_with_flag(folderview, MSG_UNREAD);
2068                         else
2069                                 folderview_select_node(folderview,
2070                                                        folderview->selected);
2071                 }
2072                 break;
2073         case GDK_KEY_Left:
2074                 if (folderview->selected) {
2075                         if (GTK_CMCTREE_ROW(folderview->selected)->expanded) {
2076                                 gtk_cmctree_collapse(GTK_CMCTREE(folderview->ctree),
2077                                                 folderview->selected);
2078                         } else {
2079                                 if ((item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree),
2080                                                 folderview->selected))) {
2081                                         if ((node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(folderview->ctree),
2082                                                         NULL, folder_item_parent(item)))) {
2083                                                 gtk_sctree_select(GTK_SCTREE(folderview->ctree), node);
2084                                                 if (!gtk_cmctree_node_is_visible(GTK_CMCTREE(folderview->ctree), node))
2085                                                         gtk_cmctree_node_moveto(GTK_CMCTREE(folderview->ctree),
2086                                                                         node, -1, 0, 0);
2087                                         }
2088                                 }
2089                         }
2090                 }
2091                 break;
2092         case GDK_KEY_Home:
2093         case GDK_KEY_End:
2094                 if (event->keyval == GDK_KEY_Home)
2095                         node = gtk_cmctree_node_nth(GTK_CMCTREE(folderview->ctree), 0);
2096                 else
2097                         node = gtk_cmctree_last(GTK_CMCTREE(folderview->ctree),
2098                                         gtk_cmctree_node_nth(GTK_CMCTREE(folderview->ctree), 0));
2099
2100                 gtk_sctree_select(GTK_SCTREE(folderview->ctree), node);
2101
2102                 if (!gtk_cmctree_node_is_visible(GTK_CMCTREE(folderview->ctree), node))
2103                         gtk_cmctree_node_moveto(GTK_CMCTREE(folderview->ctree),
2104                                         node, -1, 0, 0);
2105                 break;
2106         default:
2107                 break;
2108         }
2109
2110         return FALSE;
2111 }
2112
2113 typedef struct _PostponedSelectData
2114 {
2115         GtkCMCTree *ctree;
2116         GtkCMCTreeNode *row;
2117         gint column;
2118         FolderView *folderview;
2119 } PostponedSelectData;
2120
2121 static gboolean postpone_select(void *data)
2122 {
2123         PostponedSelectData *psdata = (PostponedSelectData *)data;
2124         debug_print("trying again\n");
2125
2126         psdata->folderview->postpone_select_id = 0;
2127         psdata->folderview->open_folder = TRUE;
2128         main_window_cursor_normal(psdata->folderview->mainwin);
2129         STATUSBAR_POP(psdata->folderview->mainwin);
2130         folderview_selected(psdata->ctree, psdata->row,
2131                             psdata->column, psdata->folderview);
2132         g_free(psdata);
2133         return FALSE;
2134 }
2135
2136 void folderview_close_opened(FolderView *folderview, gboolean dirty)
2137 {
2138         if (folderview->opened) {
2139                 if (dirty) {
2140                         folderview->opened = NULL;
2141                         return;
2142                 }
2143
2144                 FolderItem *olditem =
2145                         gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree), 
2146                                                       folderview->opened);
2147                 if (olditem) {
2148                         gchar *buf = g_strdup_printf(_("Closing folder %s..."), 
2149                                 olditem->path ? olditem->path:olditem->name);
2150                         /* will be null if we just moved the previously opened folder */
2151                         STATUSBAR_PUSH(folderview->mainwin, buf);
2152                         main_window_cursor_wait(folderview->mainwin);
2153                         g_free(buf);
2154                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
2155                         summary_show(folderview->summaryview, NULL);
2156                         folder_item_close(olditem);
2157                         main_window_cursor_normal(folderview->mainwin);
2158                         STATUSBAR_POP(folderview->mainwin);
2159                         if (olditem->folder->klass->item_closed)
2160                                 olditem->folder->klass->item_closed(olditem);
2161
2162                 }
2163         }
2164
2165         if (folderview->opened &&
2166             !GTK_CMCTREE_ROW(folderview->opened)->children)
2167                 gtk_cmctree_collapse(GTK_CMCTREE(folderview->ctree), folderview->opened);
2168
2169         folderview->opened = NULL;
2170 }
2171 static void folderview_selected(GtkCMCTree *ctree, GtkCMCTreeNode *row,
2172                                 gint column, FolderView *folderview)
2173 {
2174         static gboolean can_select = TRUE;      /* exclusive lock */
2175         gboolean opened;
2176         FolderItem *item;
2177         gchar *buf;
2178         int res = 0;
2179         GtkCMCTreeNode *old_opened = folderview->opened;
2180         START_TIMING("");
2181         folderview->selected = row;
2182
2183         debug_print("newly selected %p, opened %p\n", folderview->selected, 
2184                         folderview->opened);
2185         if (folderview->opened == row) {
2186                 folderview->open_folder = FALSE;
2187                 END_TIMING();
2188                 return;
2189         }
2190         
2191         item = gtk_cmctree_node_get_row_data(ctree, row);
2192         if (!item) {
2193                 END_TIMING();
2194                 folderview->open_folder = FALSE;
2195                 return;
2196         }
2197
2198         if (!can_select || summary_is_locked(folderview->summaryview)) {
2199                 if (folderview->opened) {
2200                         gtkut_ctree_set_focus_row(ctree, folderview->opened);
2201                         gtk_cmctree_select(ctree, folderview->opened);
2202                 }
2203                 folderview->open_folder = FALSE;
2204                 END_TIMING();
2205                 return;
2206         }
2207
2208         if (!folderview->open_folder) {
2209                 END_TIMING();
2210                 return;
2211         }
2212
2213         can_select = FALSE;
2214
2215         /* Save cache for old folder */
2216         /* We don't want to lose all caches if sylpheed crashed */
2217         /* resets folderview->opened to NULL */
2218         folderview_close_opened(folderview, FALSE);
2219         
2220         /* CLAWS: set compose button type: news folder items 
2221          * always have a news folder as parent */
2222         if (item->folder) 
2223                 toolbar_set_compose_button
2224                         (folderview->mainwin->toolbar,
2225                          FOLDER_TYPE(item->folder) == F_NEWS ? 
2226                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
2227
2228         if (item->path)
2229                 debug_print("Folder %s is selected\n", item->path);
2230
2231         if (!GTK_CMCTREE_ROW(row)->children)
2232                 gtk_cmctree_expand(ctree, row);
2233
2234         /* ungrab the mouse event */
2235         if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
2236                 gtk_grab_remove(GTK_WIDGET(ctree));
2237                 if (gdk_pointer_is_grabbed())
2238                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
2239         }
2240
2241         /* Open Folder */
2242         /* TODO: wwp: avoid displaying (null) in the status bar */
2243         buf = g_strdup_printf(_("Opening folder %s..."), item->path ? 
2244                                         item->path : "(null)");
2245         debug_print("%s\n", buf);
2246         STATUSBAR_PUSH(folderview->mainwin, buf);
2247         g_free(buf);
2248
2249         main_window_cursor_wait(folderview->mainwin);
2250
2251         if (folderview->scanning_folder == item->folder) {
2252                 res = -2;
2253         } else {
2254                 res = folder_item_open(item);
2255         }
2256
2257         if (res == -1 && item->no_select == FALSE) {
2258                 main_window_cursor_normal(folderview->mainwin);
2259                 STATUSBAR_POP(folderview->mainwin);
2260
2261                 alertpanel_error(_("Folder could not be opened."));
2262
2263                 folderview->open_folder = FALSE;
2264                 can_select = TRUE;
2265                 END_TIMING();
2266                 return;
2267         } else if (res == -2 && item->no_select == FALSE) {
2268                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2269                 data->ctree = ctree;
2270                 data->row = row;
2271                 data->column = column;
2272                 data->folderview = folderview;
2273                 debug_print("postponing open of %s till end of scan\n",
2274                         item->path ? item->path:item->name);
2275                 folderview->open_folder = FALSE;
2276                 can_select = TRUE;
2277                 if (folderview->postpone_select_id != 0)
2278                         g_source_remove(folderview->postpone_select_id);
2279                 folderview->postpone_select_id = g_timeout_add(500, postpone_select, data);
2280                 END_TIMING();
2281                 return;
2282         }
2283         
2284         main_window_cursor_normal(folderview->mainwin);
2285
2286         /* Show messages */
2287         summary_set_prefs_from_folderitem(folderview->summaryview, item);
2288         opened = summary_show(folderview->summaryview, item);
2289         
2290         folder_clean_cache_memory(item);
2291
2292         if (!opened) {
2293                 gtkut_ctree_set_focus_row(ctree, old_opened);
2294                 gtk_cmctree_select(ctree, old_opened);
2295                 folderview->opened = old_opened;
2296         } else {
2297                 folderview->opened = row;
2298                 if (gtk_cmctree_node_is_visible(ctree, row)
2299                     != GTK_VISIBILITY_FULL)
2300                         gtk_cmctree_node_moveto(ctree, row, -1, 0.5, 0);
2301         }
2302
2303         STATUSBAR_POP(folderview->mainwin);
2304
2305         folderview->open_folder = FALSE;
2306         can_select = TRUE;
2307         END_TIMING();
2308 }
2309
2310 static void folderview_tree_expanded(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2311                                      FolderView *folderview)
2312 {
2313         FolderItem *item;
2314
2315         item = gtk_cmctree_node_get_row_data(ctree, node);
2316         cm_return_if_fail(item != NULL);
2317         item->collapsed = FALSE;
2318         folderview_update_node(folderview, node);
2319 }
2320
2321 static void folderview_tree_collapsed(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2322                                       FolderView *folderview)
2323 {
2324         FolderItem *item;
2325
2326         item = gtk_cmctree_node_get_row_data(ctree, node);
2327         cm_return_if_fail(item != NULL);
2328         item->collapsed = TRUE;
2329         folderview_update_node(folderview, node);
2330 }
2331
2332 static void folderview_popup_close(GtkMenuShell *menu_shell,
2333                                    FolderView *folderview)
2334 {
2335         if (!folderview->opened) return;
2336
2337         gtk_cmctree_select(GTK_CMCTREE(folderview->ctree), folderview->opened);
2338 }
2339
2340 static void folderview_col_resized(GtkCMCList *clist, gint column, gint width,
2341                                    FolderView *folderview)
2342 {
2343         FolderColumnType type = folderview->col_state[column].type;
2344
2345         prefs_common.folder_col_size[type] = width;
2346 }
2347
2348 static void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2349 {
2350         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2351         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2352         GtkCMCTreeNode *node, *parent_node;
2353         gint *col_pos = folderview->col_pos;
2354         FolderItemUpdateData hookdata;
2355
2356         parent_node = gtk_cmctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2357         if (parent_node == NULL)
2358                 return;
2359
2360         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
2361
2362         text[col_pos[F_COL_FOLDER]] = item->name;
2363         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2364                                      FOLDER_SPACING,
2365                                      folderxpm,
2366                                      folderopenxpm,
2367                                      FALSE, FALSE);
2368         gtk_cmctree_expand(ctree, parent_node);
2369         gtk_cmctree_node_set_row_data(ctree, node, item);
2370         if (normal_style)
2371                 gtk_cmctree_node_set_row_style(ctree, node, normal_style);
2372         folderview_sort_folders(folderview, parent_node, item->folder);
2373
2374         hookdata.item = item;
2375         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2376         hookdata.msg = NULL;
2377         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2378
2379         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
2380 }
2381
2382 static void folderview_empty_trash_cb(GtkAction *action, gpointer data)
2383 {
2384         FolderView *folderview = (FolderView *)data;
2385         FolderItem *item;
2386         GSList *mlist = NULL;
2387         GSList *cur = NULL;
2388         FolderItem *special_trash = NULL;
2389         PrefsAccount *ac;
2390
2391         if (!folderview->selected) return;
2392         item = folderview_get_selected_item(folderview);
2393         cm_return_if_fail(item != NULL);
2394         cm_return_if_fail(item->folder != NULL);
2395
2396         if (NULL != (ac = account_find_from_item(item)))
2397                 special_trash = account_get_special_folder(ac, F_TRASH);
2398
2399         if (item != item->folder->trash && item != special_trash
2400         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2401         
2402         if (prefs_common.ask_on_clean) {
2403                 if (alertpanel(_("Empty trash"),
2404                                _("Delete all messages in trash?"),
2405                                GTK_STOCK_CANCEL, g_strconcat("+", _("_Empty trash"), NULL), NULL) != G_ALERTALTERNATE)
2406                         return;
2407         }
2408         
2409         mlist = folder_item_get_msg_list(item);
2410         
2411         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2412                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2413                 if (MSG_IS_LOCKED(msginfo->flags))
2414                         continue;
2415                 /* is it partially received? (partial_recv isn't cached) */
2416                 if (msginfo->total_size != 0 && 
2417                     msginfo->size != (off_t)msginfo->total_size)
2418                         partial_mark_for_delete(msginfo);
2419         }
2420         procmsg_msg_list_free(mlist);
2421
2422         folder_item_remove_all_msg(item);
2423 }
2424
2425 static void folderview_send_queue_cb(GtkAction *action, gpointer data)
2426 {
2427         FolderView *folderview = (FolderView *)data;
2428         FolderItem *item;
2429         FolderItem *special_queue = NULL;
2430         PrefsAccount *ac;
2431         gchar *errstr = NULL;
2432
2433         if (!folderview->selected) return;
2434         item = folderview_get_selected_item(folderview);
2435         cm_return_if_fail(item != NULL);
2436         cm_return_if_fail(item->folder != NULL);
2437
2438         if (NULL != (ac = account_find_from_item(item)))
2439                 special_queue = account_get_special_folder(ac, F_QUEUE);
2440
2441         if (item != item->folder->queue && item != special_queue
2442         &&  !folder_has_parent_of_type(item, F_QUEUE)) return;
2443         
2444         if (procmsg_queue_is_empty(item))
2445                 return;
2446
2447         if (prefs_common.work_offline)
2448                 if (alertpanel(_("Offline warning"), 
2449                                _("You're working offline. Override?"),
2450                                GTK_STOCK_NO, GTK_STOCK_YES,
2451                                NULL) != G_ALERTALTERNATE)
2452                 return;
2453
2454         /* ask for confirmation before sending queued messages only
2455            in online mode and if there is at least one message queued
2456            in any of the folder queue
2457         */
2458         if (prefs_common.confirm_send_queued_messages) {
2459                 if (!prefs_common.work_offline) {
2460                         if (alertpanel(_("Send queued messages"), 
2461                                    _("Send all queued messages?"),
2462                                    GTK_STOCK_CANCEL, _("_Send"),
2463                                    NULL) != G_ALERTALTERNATE)
2464                                 return;
2465                 }
2466         }
2467
2468         if (procmsg_send_queue(item, prefs_common.savemsg, &errstr) < 0) {
2469                 if (!errstr)
2470                         alertpanel_error_log(_("Some errors occurred while "
2471                                            "sending queued messages."));
2472                 else {
2473                         alertpanel_error_log(_("Some errors occurred "
2474                                         "while sending queued messages:\n%s"), errstr);
2475                         g_free(errstr);
2476                 }
2477         }
2478 }
2479
2480 static void folderview_search_cb(GtkAction *action, gpointer data)
2481 {
2482         FolderView *folderview = (FolderView *)data;
2483         summary_search(folderview->summaryview);
2484 }
2485
2486 static void folderview_run_processing_cb(GtkAction *action, gpointer data)
2487 {
2488         FolderView *folderview = (FolderView *)data;
2489         FolderItem *item;
2490
2491         if (!folderview->selected) return;
2492
2493         item = folderview_get_selected_item(folderview);
2494         cm_return_if_fail(item != NULL);
2495         cm_return_if_fail(item->folder != NULL);
2496
2497         item->processing_pending = TRUE;
2498         folder_item_apply_processing(item);
2499         item->processing_pending = FALSE;
2500 }
2501
2502 static void folderview_property_cb(GtkAction *action, gpointer data)
2503 {
2504         FolderView *folderview = (FolderView *)data;
2505         FolderItem *item;
2506
2507         if (!folderview->selected) return;
2508
2509         item = folderview_get_selected_item(folderview);
2510         cm_return_if_fail(item != NULL);
2511         cm_return_if_fail(item->folder != NULL);
2512
2513         prefs_folder_item_open(item);
2514 }
2515
2516 static void folderview_recollapse_nodes(FolderView *folderview, GtkCMCTreeNode *node)
2517 {
2518         GSList *list = NULL;
2519         GSList *done = NULL;
2520         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2521         
2522         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2523                 if (!gtkut_ctree_node_is_parent(GTK_CMCTREE_NODE(list->data), node)
2524                 &&  list->data != node) {
2525                         gtk_cmctree_collapse(ctree, GTK_CMCTREE_NODE(list->data));
2526                         done = g_slist_append(done, GTK_CMCTREE_NODE(list->data));
2527                 }
2528         }
2529         for (list = done; list != NULL; list = g_slist_next(list)) {
2530                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2531                                                                  list->data);
2532         }
2533         g_slist_free(done);
2534 }
2535
2536 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2537                             FolderItem *to_folder, gboolean copy)
2538 {
2539         FolderItem *new_folder = NULL;
2540         gchar *buf;
2541         gint status;
2542
2543         cm_return_if_fail(folderview != NULL);
2544         cm_return_if_fail(from_folder != NULL);
2545         cm_return_if_fail(to_folder != NULL);
2546
2547         if (prefs_common.warn_dnd) {
2548                 buf = g_strdup_printf(copy ? _("Do you really want to copy folder '%s' in '%s'?"):
2549                                              _("Do you really want to make folder '%s' a subfolder of '%s'?"), 
2550                                         from_folder->name, to_folder->name);
2551                 status = alertpanel_full(copy ? _("Copy folder"):_("Move folder"), buf,
2552                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, TRUE,
2553                                          NULL, ALERT_QUESTION, G_ALERTDEFAULT);
2554                 g_free(buf);
2555
2556                 if ((status & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
2557                         return;
2558                 else if (status & G_ALERTDISABLE)
2559                         prefs_common.warn_dnd = FALSE;
2560         }
2561
2562         buf = g_strdup_printf(copy ? _("Copying %s to %s..."):_("Moving %s to %s..."), 
2563                                 from_folder->name, to_folder->name);
2564         STATUSBAR_PUSH(folderview->mainwin, buf);
2565         g_free(buf);
2566         summary_clear_all(folderview->summaryview);
2567         folderview->opened = NULL;
2568         folderview->selected = NULL;
2569         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2570         inc_lock();
2571         main_window_cursor_wait(folderview->mainwin);
2572
2573         statusbar_verbosity_set(FALSE);
2574         folder_item_update_freeze();
2575         gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
2576         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder, copy)) == F_MOVE_OK) {
2577                 statusbar_verbosity_set(FALSE);
2578                 main_window_cursor_normal(folderview->mainwin);
2579                 STATUSBAR_POP(folderview->mainwin);
2580                 folder_item_update_thaw();
2581                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2582
2583                 folderview_sort_folders(folderview, 
2584                         gtk_cmctree_find_by_row_data(GTK_CMCTREE(folderview->ctree), 
2585                                 NULL, to_folder), new_folder->folder);
2586                 folderview_select(folderview, new_folder);
2587                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2588         } else {
2589                 statusbar_verbosity_set(FALSE);         
2590                 main_window_cursor_normal(folderview->mainwin);
2591                 STATUSBAR_POP(folderview->mainwin);
2592                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2593                 folder_item_update_thaw();
2594                 switch (status) {
2595                 case F_MOVE_FAILED_DEST_IS_PARENT:
2596                         alertpanel_error(_("Source and destination are the same."));
2597                         break;
2598                 case F_MOVE_FAILED_DEST_IS_CHILD:
2599                         alertpanel_error(copy ? _("Can't copy a folder to one of its children."):
2600                                                 _("Can't move a folder to one of its children."));
2601                         break;
2602                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2603                         alertpanel_error(_("A folder cannot be moved between different mailboxes."));
2604                         break;
2605                 default:
2606                         alertpanel_error(copy ? _("Copy failed!"):_("Move failed!"));
2607                         break;
2608                 }
2609         }       
2610         inc_unlock();           
2611         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2612 }
2613
2614 static gint folderview_clist_compare(GtkCMCList *clist,
2615                                      gconstpointer ptr1, gconstpointer ptr2)
2616 {
2617         FolderItem *item1 = ((GtkCMCListRow *)ptr1)->data;
2618         FolderItem *item2 = ((GtkCMCListRow *)ptr2)->data;
2619
2620         if (item1->order > 0 && item2->order > 0)  // if we have an order item, use it
2621         {
2622                 return item1->order - item2->order;
2623         }
2624
2625         // if only one folder has an order it comes first
2626         if (item1->order > 0)
2627         {
2628                 return -1;
2629         }
2630         if (item2->order > 0)
2631         {
2632                 return 1;
2633         }
2634
2635         if (!item1->name)
2636                 return (item2->name != NULL);
2637         if (!item2->name)
2638                 return -1;
2639
2640         return g_utf8_collate(item1->name, item2->name);
2641 }
2642
2643 static void folderview_processing_cb(GtkAction *action, gpointer data)
2644 {
2645         FolderView *folderview = (FolderView *)data;
2646         FolderItem *item;
2647         gchar *id, *title;
2648
2649         if (!folderview->selected) return;
2650
2651         item = folderview_get_selected_item(folderview);
2652         cm_return_if_fail(item != NULL);
2653         cm_return_if_fail(item->folder != NULL);
2654
2655         id = folder_item_get_identifier(item);
2656         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2657         g_free (id);
2658
2659         prefs_filtering_open(&item->prefs->processing, title,
2660                         MANUAL_ANCHOR_PROCESSING, NULL, NULL, FALSE);
2661         g_free (title);
2662 }
2663
2664 void folderview_set_target_folder_color(gint color_op) 
2665 {
2666         gint firstone = 1;
2667         GList *list;
2668         FolderView *folderview;
2669
2670         for (list = folderview_list; list != NULL; list = list->next) {
2671                 folderview = (FolderView *)list->data;
2672                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2673                 if (firstone) {
2674                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2675                                 folderview->color_op;
2676                         firstone = 0;
2677                 }
2678         }
2679 }
2680
2681 static gchar *last_smallfont = NULL;
2682 static gchar *last_normalfont = NULL;
2683 static gchar *last_boldfont = NULL;
2684 static gboolean last_derive = 0;
2685
2686 void folderview_reinit_fonts(FolderView *folderview)
2687 {
2688         /* force reinit */
2689         g_free(last_smallfont);
2690         last_smallfont = NULL;
2691         g_free(last_normalfont);
2692         last_normalfont = NULL;
2693         g_free(last_boldfont);
2694         last_boldfont = NULL;
2695 }
2696
2697 void folderview_reflect_prefs(void)
2698 {
2699         gboolean update_font = FALSE;
2700         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2701         FolderItem *item = folderview_get_selected_item(folderview);
2702         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2703                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2704         gint height = gtk_adjustment_get_value(pos);
2705
2706         if (!last_smallfont || strcmp(last_smallfont, SMALL_FONT) ||
2707                         !last_normalfont || strcmp(last_normalfont, NORMAL_FONT) ||
2708                         !last_boldfont || strcmp(last_boldfont, BOLD_FONT) ||
2709                         last_derive != prefs_common.derive_from_normal_font)
2710                 update_font = TRUE;
2711
2712         if (!update_font)
2713                 return;
2714
2715         g_free(last_smallfont);
2716         last_smallfont = g_strdup(SMALL_FONT);
2717         g_free(last_normalfont);
2718         last_normalfont = g_strdup(NORMAL_FONT);
2719         g_free(last_boldfont);
2720         last_boldfont = g_strdup(BOLD_FONT);
2721         last_derive = prefs_common.derive_from_normal_font;
2722
2723 #define STYLE_FREE(s)                   \
2724         if (s != NULL) {                \
2725                 g_object_unref(s);      \
2726                 s = NULL;               \
2727         }
2728
2729         STYLE_FREE(normal_style);
2730         STYLE_FREE(normal_color_style);
2731         STYLE_FREE(bold_style);
2732         STYLE_FREE(bold_color_style);
2733         STYLE_FREE(bold_tgtfold_style);
2734
2735 #undef STYLE_FREE
2736
2737         folderview_init(folderview);
2738         gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
2739         folderview_column_set_titles(folderview);
2740         folderview_set_all();
2741
2742         g_signal_handlers_block_by_func
2743                 (G_OBJECT(folderview->ctree),
2744                  G_CALLBACK(folderview_selected), folderview);
2745
2746         if (item) {
2747                 GtkCMCTreeNode *node = gtk_cmctree_find_by_row_data(
2748                         GTK_CMCTREE(folderview->ctree), NULL, item);
2749
2750                 folderview_select(folderview, item);
2751                 folderview->open_folder = FALSE;
2752                 folderview->selected = node;
2753         }
2754
2755         g_signal_handlers_unblock_by_func
2756                 (G_OBJECT(folderview->ctree),
2757                  G_CALLBACK(folderview_selected), folderview);
2758
2759         pos = gtk_scrolled_window_get_vadjustment(
2760                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2761         gtk_adjustment_set_value(pos, height);
2762         gtk_adjustment_changed(pos);
2763         gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2764 }
2765
2766 static void drag_state_stop(FolderView *folderview)
2767 {
2768         if (folderview->drag_timer_id)
2769                 g_source_remove(folderview->drag_timer_id);
2770         folderview->drag_timer_id = 0;
2771         folderview->drag_node = NULL;
2772 }
2773
2774 static gboolean folderview_defer_expand(FolderView *folderview)
2775 {
2776         if (folderview->drag_node) {
2777                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2778                 if (folderview->drag_item->collapsed) {
2779                         gtk_cmctree_expand(GTK_CMCTREE(folderview->ctree), folderview->drag_node);
2780                         folderview->nodes_to_recollapse = g_slist_append
2781                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2782                 }
2783         }
2784         folderview->drag_item  = NULL;
2785         folderview->drag_timer_id = 0;
2786         return FALSE;
2787 }
2788
2789 static void drag_state_start(FolderView *folderview, GtkCMCTreeNode *node, FolderItem *item)
2790 {
2791         /* the idea is that we call drag_state_start() whenever we want expansion to
2792          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2793          * we need to call drag_state_stop() */
2794         drag_state_stop(folderview);
2795         /* request expansion */
2796         if (0 != (folderview->drag_timer_id = g_timeout_add
2797                         (prefs_common.hover_timeout, 
2798                          (GSourceFunc)folderview_defer_expand,
2799                          folderview))) {
2800                 folderview->drag_node = node;
2801                 folderview->drag_item = item;
2802         }                        
2803 }
2804 #ifndef GENERIC_UMPC
2805 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2806                                   FolderView       *folderview)
2807 {
2808         GdkDragContext *context;
2809
2810         cm_return_if_fail(folderview != NULL);
2811         if (folderview->selected == NULL) return;
2812         if (folderview->nodes_to_recollapse) 
2813                 g_slist_free(folderview->nodes_to_recollapse);
2814         folderview->nodes_to_recollapse = NULL;
2815         context = gtk_drag_begin(widget, folderview->target_list,
2816                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2817         gtk_drag_set_icon_default(context);
2818 }
2819 #endif
2820 static void folderview_drag_data_get(GtkWidget        *widget,
2821                                      GdkDragContext   *drag_context,
2822                                      GtkSelectionData *selection_data,
2823                                      guint             info,
2824                                      guint             time,
2825                                      FolderView       *folderview)
2826 {
2827         FolderItem *item;
2828         GList *sel;
2829         gchar *source = NULL;
2830         if (info == TARGET_DUMMY) {
2831                 sel = GTK_CMCLIST(folderview->ctree)->selection;
2832                 if (!sel)
2833                         return;
2834
2835                 item = gtk_cmctree_node_get_row_data
2836                         (GTK_CMCTREE(folderview->ctree),
2837                          GTK_CMCTREE_NODE(sel->data));
2838                 if (item) {
2839                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2840                         gtk_selection_data_set(selection_data,
2841                                                gtk_selection_data_get_target(selection_data), 8,
2842                                                source, strlen(source));
2843                 }
2844         } else {
2845                 g_warning("unknown info %d", info);
2846         }
2847 }
2848
2849 static gboolean folderview_update_folder(gpointer source, gpointer userdata)
2850 {
2851         FolderUpdateData *hookdata;
2852         FolderView *folderview;
2853         GtkWidget *ctree;
2854
2855         hookdata = source;
2856         folderview = (FolderView *) userdata;   
2857         cm_return_val_if_fail(hookdata != NULL, FALSE);
2858         cm_return_val_if_fail(folderview != NULL, FALSE);
2859
2860         ctree = folderview->ctree;
2861         cm_return_val_if_fail(ctree != NULL, FALSE);
2862
2863         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2864                 folderview_create_folder_node(folderview, hookdata->item);
2865         else if (hookdata->update_flags & FOLDER_RENAME_FOLDERITEM) {
2866                 GtkCMCTreeNode *node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree),
2867                                 NULL, folder_item_parent(hookdata->item));
2868                 folderview_sort_folders(folderview, node, hookdata->folder);
2869         } else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2870                 GtkCMCTreeNode *node;
2871
2872                 node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree), NULL, hookdata->item);
2873                 if (node != NULL) {
2874                         gtk_cmctree_remove_node(GTK_CMCTREE(ctree), node);
2875                         if (folderview->selected == node)
2876                                 folderview->selected = NULL;
2877                         if (folderview->opened == node)
2878                                 folderview->opened = NULL;
2879                 }
2880         } else if (hookdata->update_flags & FOLDER_MOVE_FOLDERITEM) {
2881                 /* do nothing, it's done by the ADD and REMOVE) */
2882         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2883                 folderview_set(folderview);
2884
2885         return FALSE;
2886 }
2887
2888 static gboolean folderview_dnd_scroll_cb(gpointer data)
2889 {
2890         FolderView *folderview = (FolderView *)data;
2891         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2892                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2893         gint new_val = (int)gtk_adjustment_get_value(pos) + folderview->scroll_value;
2894         gint max = (int)gtk_adjustment_get_upper(pos) -
2895                (int)gtk_adjustment_get_page_size(pos);
2896
2897         if (folderview->scroll_value == 0) {
2898                 folderview->scroll_timeout_id = 0;
2899                 return FALSE;
2900         }
2901
2902         if (folderview->scroll_value > 0 && new_val > max) {
2903                 new_val = max;
2904         } else if (folderview->scroll_value < 0 && new_val < 0) {
2905                 new_val = 0;
2906         }
2907         gtk_adjustment_set_value(pos, new_val);
2908         
2909         return TRUE;
2910 }
2911
2912 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2913                                           GdkDragContext *context,
2914                                           gint            x,
2915                                           gint            y,
2916                                           guint           time,
2917                                           FolderView     *folderview)
2918 {
2919         gint row, column;
2920         FolderItem *item = NULL, *src_item = NULL;
2921         GtkCMCTreeNode *node = NULL;
2922         gboolean acceptable = FALSE;
2923         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2924                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2925         int height = (int)gtk_adjustment_get_page_size(pos);
2926         int total_height = (int)gtk_adjustment_get_upper(pos);
2927         int vpos = (int)gtk_adjustment_get_value(pos);
2928         int offset = prefs_common.show_col_headers ? 24:0;
2929         int dist;
2930
2931         if (gtk_cmclist_get_selection_info
2932                 (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column)) {
2933                 GtkWidget *srcwidget;
2934
2935                 if (y > height - (48 - offset) && height + vpos < total_height) {
2936                         dist = -(height - (48 - offset) - y);
2937                         folderview->scroll_value = 1.41f * (1+(dist / 6));
2938                 } else if (y < 72 - (24 - offset) && y >= 0) {
2939                         dist = 72 - (24 - offset) - y;
2940                         folderview->scroll_value = -1.41f * (1+(dist / 6));
2941                 } else {
2942                         folderview->scroll_value = 0;
2943                 }
2944                 if (folderview->scroll_value != 0 && folderview->scroll_timeout_id == 0) {
2945                         folderview->scroll_timeout_id = 
2946                                 g_timeout_add(30, folderview_dnd_scroll_cb,
2947                                               folderview);
2948                 }
2949
2950                 node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
2951                 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
2952                 src_item = folderview->summaryview->folder_item;
2953
2954                 srcwidget = gtk_drag_get_source_widget(context);
2955                 if (srcwidget == summary_get_main_widget(folderview->summaryview)) {
2956                         /* comes from summaryview */
2957                         /* we are copying messages, so only accept folder items that are not
2958                            the source item, are no root items and can copy messages */
2959                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2960                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2961                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
2962                                 acceptable = TRUE;
2963                 } else if (srcwidget == folderview->ctree) {
2964                         /* comes from folderview */
2965                         /* we are moving folder items, only accept folders that are not
2966                            the source items and can copy messages and create folder items */
2967                         if (item && item->folder && src_item && src_item != item &&
2968                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2969                             FOLDER_CLASS(item->folder)->create_folder != NULL &&
2970                             ((FOLDER_TYPE(item->folder) != F_UNKNOWN &&  FOLDER_TYPE(src_item->folder) != F_UNKNOWN)
2971                              || item->folder == src_item->folder))
2972                                 acceptable = TRUE;
2973                 } else {
2974                         /* comes from another app */
2975                         /* we are adding messages, so only accept folder items that are 
2976                            no root items and can copy messages */
2977                         if (item && item->folder && folder_item_parent(item) != NULL
2978                             && FOLDER_CLASS(item->folder)->add_msg != NULL &&
2979                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
2980                                 acceptable = TRUE;
2981                 }
2982         }
2983
2984         if (acceptable || (src_item && src_item == item))
2985                 drag_state_start(folderview, node, item);
2986         
2987         if (acceptable) {
2988                 g_signal_handlers_block_by_func
2989                         (G_OBJECT(widget),
2990                          G_CALLBACK(folderview_selected), folderview);
2991                 gtk_cmctree_select(GTK_CMCTREE(widget), node);
2992                 g_signal_handlers_unblock_by_func
2993                         (G_OBJECT(widget),
2994                          G_CALLBACK(folderview_selected), folderview);
2995                 gdk_drag_status(context, 
2996                                         (gdk_drag_context_get_actions(context) == GDK_ACTION_COPY ?
2997                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2998         } else {
2999                 if (folderview->opened)
3000                         gtk_cmctree_select(GTK_CMCTREE(widget), folderview->opened);
3001                 gdk_drag_status(context, 0, time);
3002         }
3003
3004         return acceptable;
3005 }
3006
3007 static void folderview_drag_leave_cb(GtkWidget      *widget,
3008                                      GdkDragContext *context,
3009                                      guint           time,
3010                                      FolderView     *folderview)
3011 {
3012         drag_state_stop(folderview);
3013         folderview->scroll_value = 0;
3014         gtk_cmctree_select(GTK_CMCTREE(widget), folderview->opened);
3015 }
3016
3017 static void free_info (gpointer stuff, gpointer data)
3018 {
3019         g_free(stuff);
3020 }
3021
3022 void folderview_finish_dnd(const gchar *data, GdkDragContext *drag_context,
3023                            guint time, FolderItem *item)
3024 {
3025         GList *list, *tmp;
3026         GSList *msglist = NULL;
3027         list = uri_list_extract_filenames(data);
3028         if (!(item && item->folder && folder_item_parent(item) != NULL
3029                     && FOLDER_CLASS(item->folder)->add_msg != NULL))
3030         {
3031                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3032                 debug_print("item doesn't fit\n");                      
3033                 return;
3034         }       
3035         if (!list) {
3036                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3037                 debug_print("list is empty\n");                 
3038                 return;
3039         }
3040         for (tmp = list; tmp != NULL; tmp = tmp->next) {
3041                 MsgFileInfo *info = NULL;
3042
3043                 if (file_is_email((gchar *)tmp->data)) {
3044                         info = g_new0(MsgFileInfo, 1);
3045                         info->msginfo = NULL;
3046                         info->file = (gchar *)tmp->data;
3047                         msglist = g_slist_prepend(msglist, info);
3048                         debug_print("file is a mail\n");
3049                 } else {
3050                         debug_print("file isn't a mail\n");
3051                 }
3052         }
3053         if (msglist) {
3054                 msglist = g_slist_reverse(msglist);
3055                 folder_item_add_msgs(item, msglist, FALSE);
3056                 g_slist_foreach(msglist, free_info, NULL);
3057                 g_slist_free(msglist);
3058                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
3059         } else {
3060                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3061         }
3062         list_free_strings(list);
3063         g_list_free(list);
3064 }
3065
3066 static void folderview_drag_received_cb(GtkWidget        *widget,
3067                                         GdkDragContext   *drag_context,
3068                                         gint              x,
3069                                         gint              y,
3070                                         GtkSelectionData *data,
3071                                         guint             info,
3072                                         guint             time,
3073                                         FolderView       *folderview)
3074 {
3075         gint row, column;
3076         FolderItem *item = NULL, *src_item;
3077         GtkCMCTreeNode *node;
3078         int offset = prefs_common.show_col_headers ? 24:0;
3079
3080         folderview->scroll_value = 0;
3081
3082         if (info == TARGET_DUMMY) {
3083                 drag_state_stop(folderview);
3084                 const gchar *ddata = (const gchar *)gtk_selection_data_get_data(data);
3085                 if ((gchar *)strstr(ddata, "FROM_OTHER_FOLDER") != ddata) {
3086                         /* comes from summaryview */
3087                         if (gtk_cmclist_get_selection_info
3088                                 (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0)
3089                                 return;
3090
3091                         node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3092                         item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3093                         src_item = folderview->summaryview->folder_item;
3094
3095                         if (item->no_select) {
3096                                 alertpanel_error(_("The destination folder can only be used to "
3097                                                    "store subfolders."));
3098                                 return;
3099                         }
3100                         /* re-check (due to acceptable possibly set for folder moves */
3101                         if (!(item && item->folder && item->path && !item->no_select && 
3102                               src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
3103                                 return;
3104                         }
3105                         if (item && src_item) {
3106                                 switch (gdk_drag_context_get_selected_action(drag_context)) {
3107                                 case GDK_ACTION_COPY:
3108                                         summary_copy_selected_to(folderview->summaryview, item);
3109                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
3110                                         break;
3111                                 case GDK_ACTION_MOVE:
3112                                 case GDK_ACTION_DEFAULT:
3113                                 default:
3114                                         if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
3115                                                 summary_copy_selected_to(folderview->summaryview, item);
3116                                         else
3117                                                 summary_move_selected_to(folderview->summaryview, item);
3118                                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3119                                 }
3120                         } else
3121                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3122                 } else {
3123                         /* comes from folderview */
3124                         char *source;
3125                         gboolean folder_is_normal = TRUE;
3126                         gboolean copy = (GDK_ACTION_COPY ==
3127                                 gdk_drag_context_get_selected_action(drag_context));
3128
3129                         source = (char *)gtk_selection_data_get_data(data) + 17;
3130                         if (gtk_cmclist_get_selection_info
3131                             (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0
3132                             || *source == 0) {
3133                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3134                                 return;
3135                         }
3136                         node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3137                         item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3138                         src_item = folder_find_item_from_identifier(source);
3139
3140                         folder_is_normal = 
3141                                 src_item != NULL &&
3142                                 src_item->stype == F_NORMAL &&
3143                                 !folder_has_parent_of_type(src_item, F_OUTBOX) &&
3144                                 !folder_has_parent_of_type(src_item, F_DRAFT) &&
3145                                 !folder_has_parent_of_type(src_item, F_QUEUE) &&
3146                                 !folder_has_parent_of_type(src_item, F_TRASH);
3147                         if (!item || !src_item || !folder_is_normal) {
3148                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3149                                 return;
3150                         }
3151
3152                         folderview_move_folder(folderview, src_item, item, copy);
3153                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3154                 }
3155                 folderview->nodes_to_recollapse = NULL;
3156         } else if (info == TARGET_MAIL_URI_LIST) {
3157                 if (gtk_cmclist_get_selection_info
3158                         (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0)
3159                         return;
3160
3161                 node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3162                 if (!node) {
3163                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
3164                         debug_print("no node\n");               
3165                         return;
3166                 }
3167                 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3168                 if (!item) {
3169                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3170                         debug_print("no item\n");
3171                         return;
3172                 }
3173                 folderview_finish_dnd(gtk_selection_data_get_data(data),
3174                         drag_context, time, item);
3175         }
3176 }
3177
3178 static void folderview_drag_end_cb(GtkWidget        *widget, 
3179                                    GdkDragContext   *drag_context,
3180                                    FolderView       *folderview)
3181 {
3182         drag_state_stop(folderview);
3183         folderview->scroll_value = 0;
3184         g_slist_free(folderview->nodes_to_recollapse);
3185         folderview->nodes_to_recollapse = NULL;
3186 }
3187
3188 void folderview_register_popup(FolderViewPopup *fpopup)
3189 {
3190         GList *folderviews;
3191
3192         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
3193                 FolderView *folderview = folderviews->data;
3194                 GtkActionGroup *factory;
3195
3196                 factory = create_action_group(folderview, fpopup);
3197                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
3198         }       
3199         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
3200 }
3201
3202 void folderview_unregister_popup(FolderViewPopup *fpopup)
3203 {
3204         GList *folderviews;
3205
3206
3207         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
3208                 FolderView *folderview = folderviews->data;
3209
3210                 g_hash_table_remove(folderview->popups, fpopup->klass);
3211         }       
3212         g_hash_table_remove(folderview_popups, fpopup->klass);
3213 }
3214
3215 void folderview_remove_item(FolderView *folderview, FolderItem *item)
3216 {
3217         g_return_if_fail(folderview != NULL);
3218         g_return_if_fail(item != NULL);
3219
3220         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
3221         g_return_if_fail(ctree != NULL);
3222
3223         GtkCMCTreeNode *node =
3224                 gtk_cmctree_find_by_row_data(ctree, NULL, item);
3225         g_return_if_fail(node != NULL);
3226
3227         gtk_cmctree_remove_node(ctree, node);
3228 }
3229
3230 void folderview_freeze(FolderView *folderview)
3231 {
3232         if (folderview)
3233                 gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
3234 }
3235
3236 void folderview_thaw(FolderView *folderview)
3237 {
3238         if (folderview)
3239                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
3240 }
3241
3242 void folderview_grab_focus(FolderView *folderview)
3243 {
3244          if (folderview)
3245                  gtk_widget_grab_focus(folderview->ctree);
3246 }