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