Make Left and Right keys collapse and expand item under cursor if appropriate.
[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 = gtk_cmctree_node_get_row_data(GTK_CMCTREE(ctree), folderview->selected);
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 = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
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.\n", 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 #ifndef GENERIC_UMPC
2009         case GDK_KEY_Return:
2010         case GDK_KEY_KP_Enter:
2011 #endif
2012                 if (folderview->selected) {
2013                         if (!GTK_CMCTREE_ROW(folderview->selected)->expanded)
2014                                 gtk_cmctree_expand(GTK_CMCTREE(folderview->ctree),
2015                                                 folderview->selected);
2016                         else
2017                                 folderview_select_node(folderview,
2018                                                        folderview->selected);
2019                 }
2020                 break;
2021 #ifdef GENERIC_UMPC
2022         case GDK_KEY_Return:
2023                 if (folderview->selected && GTK_CMCTREE_ROW(folderview->selected)->children) {
2024                         gtk_cmctree_toggle_expansion(
2025                                 GTK_CMCTREE(folderview->ctree),
2026                                 folderview->selected);
2027                 }
2028                 break;  
2029 #endif
2030         case GDK_KEY_space:
2031                 BREAK_ON_MODIFIER_KEY();
2032                 if (folderview->selected) {
2033                         if (folderview->opened == folderview->selected &&
2034                             (!folderview->summaryview->folder_item ||
2035                              folderview->summaryview->folder_item->total_msgs == 0))
2036                                 folderview_select_next_with_flag(folderview, MSG_UNREAD, TRUE);
2037                         else
2038                                 folderview_select_node(folderview,
2039                                                        folderview->selected);
2040                 }
2041                 break;
2042         case GDK_KEY_Left:
2043                 if (folderview->selected) {
2044                         if (GTK_CMCTREE_ROW(folderview->selected)->expanded) {
2045                                 gtk_cmctree_collapse(GTK_CMCTREE(folderview->ctree),
2046                                                 folderview->selected);
2047                         } else {
2048                                 if ((item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree),
2049                                                 folderview->selected))) {
2050                                         if ((node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(folderview->ctree),
2051                                                         NULL, folder_item_parent(item)))) {
2052                                                 gtk_cmctree_select(GTK_CMCTREE(folderview->ctree), node);
2053                                                 if (!gtk_cmctree_node_is_visible(GTK_CMCTREE(folderview->ctree), node))
2054                                                         gtk_cmctree_node_moveto(GTK_CMCTREE(folderview->ctree),
2055                                                                         node, -1, 0, 0);
2056                                         }
2057                                 }
2058                         }
2059                 }
2060                 break;
2061         case GDK_KEY_Home:
2062         case GDK_KEY_End:
2063                 if (event->keyval == GDK_KEY_Home)
2064                         node = gtk_cmctree_node_nth(GTK_CMCTREE(folderview->ctree), 0);
2065                 else
2066                         node = gtk_cmctree_last(GTK_CMCTREE(folderview->ctree),
2067                                         gtk_cmctree_node_nth(GTK_CMCTREE(folderview->ctree), 0));
2068
2069                 gtk_cmctree_select(GTK_CMCTREE(folderview->ctree), node);
2070
2071                 if (!gtk_cmctree_node_is_visible(GTK_CMCTREE(folderview->ctree), node))
2072                         gtk_cmctree_node_moveto(GTK_CMCTREE(folderview->ctree),
2073                                         node, -1, 0, 0);
2074                 break;
2075         default:
2076                 break;
2077         }
2078
2079         return FALSE;
2080 }
2081
2082 typedef struct _PostponedSelectData
2083 {
2084         GtkCMCTree *ctree;
2085         GtkCMCTreeNode *row;
2086         gint column;
2087         FolderView *folderview;
2088 } PostponedSelectData;
2089
2090 static gboolean postpone_select(void *data)
2091 {
2092         PostponedSelectData *psdata = (PostponedSelectData *)data;
2093         debug_print("trying again\n");
2094
2095         psdata->folderview->postpone_select_id = 0;
2096         psdata->folderview->open_folder = TRUE;
2097         main_window_cursor_normal(psdata->folderview->mainwin);
2098         STATUSBAR_POP(psdata->folderview->mainwin);
2099         folderview_selected(psdata->ctree, psdata->row,
2100                             psdata->column, psdata->folderview);
2101         g_free(psdata);
2102         return FALSE;
2103 }
2104
2105 void folderview_close_opened(FolderView *folderview)
2106 {
2107         if (folderview->opened) {
2108                 FolderItem *olditem;
2109                 
2110                 olditem = gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree), 
2111                                                       folderview->opened);
2112                 if (olditem) {
2113                         gchar *buf = g_strdup_printf(_("Closing folder %s..."), 
2114                                 olditem->path ? olditem->path:olditem->name);
2115                         /* will be null if we just moved the previously opened folder */
2116                         STATUSBAR_PUSH(folderview->mainwin, buf);
2117                         main_window_cursor_wait(folderview->mainwin);
2118                         g_free(buf);
2119                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
2120                         summary_show(folderview->summaryview, NULL);
2121                         folder_item_close(olditem);
2122                         main_window_cursor_normal(folderview->mainwin);
2123                         STATUSBAR_POP(folderview->mainwin);
2124                         if (olditem->folder->klass->item_closed)
2125                                 olditem->folder->klass->item_closed(olditem);
2126
2127                 }
2128         }
2129
2130         if (folderview->opened &&
2131             !GTK_CMCTREE_ROW(folderview->opened)->children)
2132                 gtk_cmctree_collapse(GTK_CMCTREE(folderview->ctree), folderview->opened);
2133
2134         folderview->opened = NULL;
2135 }
2136 static void folderview_selected(GtkCMCTree *ctree, GtkCMCTreeNode *row,
2137                                 gint column, FolderView *folderview)
2138 {
2139         static gboolean can_select = TRUE;      /* exclusive lock */
2140         gboolean opened;
2141         FolderItem *item;
2142         gchar *buf;
2143         int res = 0;
2144         GtkCMCTreeNode *old_opened = folderview->opened;
2145         START_TIMING("");
2146         folderview->selected = row;
2147
2148         debug_print("newly selected %p, opened %p\n", folderview->selected, 
2149                         folderview->opened);
2150         if (folderview->opened == row) {
2151                 folderview->open_folder = FALSE;
2152                 END_TIMING();
2153                 return;
2154         }
2155         
2156         item = gtk_cmctree_node_get_row_data(ctree, row);
2157         if (!item) {
2158                 END_TIMING();
2159                 folderview->open_folder = FALSE;
2160                 return;
2161         }
2162
2163         if (!can_select || summary_is_locked(folderview->summaryview)) {
2164                 if (folderview->opened) {
2165                         gtkut_ctree_set_focus_row(ctree, folderview->opened);
2166                         gtk_cmctree_select(ctree, folderview->opened);
2167                 }
2168                 folderview->open_folder = FALSE;
2169                 END_TIMING();
2170                 return;
2171         }
2172
2173         if (!folderview->open_folder) {
2174                 END_TIMING();
2175                 return;
2176         }
2177
2178         can_select = FALSE;
2179
2180         /* Save cache for old folder */
2181         /* We don't want to lose all caches if sylpheed crashed */
2182         /* resets folderview->opened to NULL */
2183         folderview_close_opened(folderview);
2184         
2185         /* CLAWS: set compose button type: news folder items 
2186          * always have a news folder as parent */
2187         if (item->folder) 
2188                 toolbar_set_compose_button
2189                         (folderview->mainwin->toolbar,
2190                          FOLDER_TYPE(item->folder) == F_NEWS ? 
2191                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
2192
2193         if (item->path)
2194                 debug_print("Folder %s is selected\n", item->path);
2195
2196         if (!GTK_CMCTREE_ROW(row)->children)
2197                 gtk_cmctree_expand(ctree, row);
2198
2199         /* ungrab the mouse event */
2200         if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
2201                 gtk_grab_remove(GTK_WIDGET(ctree));
2202                 if (gdk_pointer_is_grabbed())
2203                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
2204         }
2205
2206         /* Open Folder */
2207         /* TODO: wwp: avoid displaying (null) in the status bar */
2208         buf = g_strdup_printf(_("Opening folder %s..."), item->path ? 
2209                                         item->path : "(null)");
2210         debug_print("%s\n", buf);
2211         STATUSBAR_PUSH(folderview->mainwin, buf);
2212         g_free(buf);
2213
2214         main_window_cursor_wait(folderview->mainwin);
2215
2216         if (folderview->scanning_folder == item->folder) {
2217                 res = -2;
2218         } else {
2219                 res = folder_item_open(item);
2220         }
2221
2222         if (res == -1 && item->no_select == FALSE) {
2223                 main_window_cursor_normal(folderview->mainwin);
2224                 STATUSBAR_POP(folderview->mainwin);
2225
2226                 alertpanel_error(_("Folder could not be opened."));
2227
2228                 folderview->open_folder = FALSE;
2229                 can_select = TRUE;
2230                 END_TIMING();
2231                 return;
2232         } else if (res == -2 && item->no_select == FALSE) {
2233                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2234                 data->ctree = ctree;
2235                 data->row = row;
2236                 data->column = column;
2237                 data->folderview = folderview;
2238                 debug_print("postponing open of %s till end of scan\n",
2239                         item->path ? item->path:item->name);
2240                 folderview->open_folder = FALSE;
2241                 can_select = TRUE;
2242                 if (folderview->postpone_select_id != 0)
2243                         g_source_remove(folderview->postpone_select_id);
2244                 folderview->postpone_select_id = g_timeout_add(500, postpone_select, data);
2245                 END_TIMING();
2246                 return;
2247         }
2248         
2249         main_window_cursor_normal(folderview->mainwin);
2250
2251         /* Show messages */
2252         summary_set_prefs_from_folderitem(folderview->summaryview, item);
2253         opened = summary_show(folderview->summaryview, item);
2254         
2255         folder_clean_cache_memory(item);
2256
2257         if (!opened) {
2258                 gtkut_ctree_set_focus_row(ctree, old_opened);
2259                 gtk_cmctree_select(ctree, old_opened);
2260                 folderview->opened = old_opened;
2261         } else {
2262                 folderview->opened = row;
2263                 if (gtk_cmctree_node_is_visible(ctree, row)
2264                     != GTK_VISIBILITY_FULL)
2265                         gtk_cmctree_node_moveto(ctree, row, -1, 0.5, 0);
2266         }
2267
2268         STATUSBAR_POP(folderview->mainwin);
2269
2270         folderview->open_folder = FALSE;
2271         can_select = TRUE;
2272         END_TIMING();
2273 }
2274
2275 static void folderview_tree_expanded(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2276                                      FolderView *folderview)
2277 {
2278         FolderItem *item;
2279
2280         item = gtk_cmctree_node_get_row_data(ctree, node);
2281         cm_return_if_fail(item != NULL);
2282         item->collapsed = FALSE;
2283         folderview_update_node(folderview, node);
2284 }
2285
2286 static void folderview_tree_collapsed(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2287                                       FolderView *folderview)
2288 {
2289         FolderItem *item;
2290
2291         item = gtk_cmctree_node_get_row_data(ctree, node);
2292         cm_return_if_fail(item != NULL);
2293         item->collapsed = TRUE;
2294         folderview_update_node(folderview, node);
2295 }
2296
2297 static void folderview_popup_close(GtkMenuShell *menu_shell,
2298                                    FolderView *folderview)
2299 {
2300         if (!folderview->opened) return;
2301
2302         gtk_cmctree_select(GTK_CMCTREE(folderview->ctree), folderview->opened);
2303 }
2304
2305 static void folderview_col_resized(GtkCMCList *clist, gint column, gint width,
2306                                    FolderView *folderview)
2307 {
2308         FolderColumnType type = folderview->col_state[column].type;
2309
2310         prefs_common.folder_col_size[type] = width;
2311 }
2312
2313 static void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2314 {
2315         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2316         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2317         GtkCMCTreeNode *node, *parent_node;
2318         gint *col_pos = folderview->col_pos;
2319         FolderItemUpdateData hookdata;
2320
2321         parent_node = gtk_cmctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2322         if (parent_node == NULL)
2323                 return;
2324
2325         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
2326
2327         text[col_pos[F_COL_FOLDER]] = item->name;
2328         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2329                                      FOLDER_SPACING,
2330                                      folderxpm,
2331                                      folderopenxpm,
2332                                      FALSE, FALSE);
2333         gtk_cmctree_expand(ctree, parent_node);
2334         gtk_cmctree_node_set_row_data(ctree, node, item);
2335         if (normal_style)
2336                 gtk_cmctree_node_set_row_style(ctree, node, normal_style);
2337         folderview_sort_folders(folderview, parent_node, item->folder);
2338
2339         hookdata.item = item;
2340         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2341         hookdata.msg = NULL;
2342         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2343
2344         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
2345 }
2346
2347 static void folderview_empty_trash_cb(GtkAction *action, gpointer data)
2348 {
2349         FolderView *folderview = (FolderView *)data;
2350         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2351         FolderItem *item;
2352         GSList *mlist = NULL;
2353         GSList *cur = NULL;
2354         FolderItem *special_trash = NULL;
2355         PrefsAccount *ac;
2356
2357         if (!folderview->selected) return;
2358         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
2359         cm_return_if_fail(item != NULL);
2360         cm_return_if_fail(item->folder != NULL);
2361
2362         if (NULL != (ac = account_find_from_item(item)))
2363                 special_trash = account_get_special_folder(ac, F_TRASH);
2364
2365         if (item != item->folder->trash && item != special_trash
2366         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2367         
2368         if (prefs_common.ask_on_clean) {
2369                 if (alertpanel(_("Empty trash"),
2370                                _("Delete all messages in trash?"),
2371                                GTK_STOCK_CANCEL, _("+_Empty trash"), NULL) != G_ALERTALTERNATE)
2372                         return;
2373         }
2374         
2375         mlist = folder_item_get_msg_list(item);
2376         
2377         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2378                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2379                 if (MSG_IS_LOCKED(msginfo->flags))
2380                         continue;
2381                 /* is it partially received? (partial_recv isn't cached) */
2382                 if (msginfo->total_size != 0 && 
2383                     msginfo->size != (off_t)msginfo->total_size)
2384                         partial_mark_for_delete(msginfo);
2385         }
2386         procmsg_msg_list_free(mlist);
2387
2388         folder_item_remove_all_msg(item);
2389 }
2390
2391 static void folderview_send_queue_cb(GtkAction *action, gpointer data)
2392 {
2393         FolderView *folderview = (FolderView *)data;
2394         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2395         FolderItem *item;
2396         FolderItem *special_queue = NULL;
2397         PrefsAccount *ac;
2398         gchar *errstr = NULL;
2399
2400         if (!folderview->selected) return;
2401         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
2402         cm_return_if_fail(item != NULL);
2403         cm_return_if_fail(item->folder != NULL);
2404
2405         if (NULL != (ac = account_find_from_item(item)))
2406                 special_queue = account_get_special_folder(ac, F_QUEUE);
2407
2408         if (item != item->folder->queue && item != special_queue
2409         &&  !folder_has_parent_of_type(item, F_QUEUE)) return;
2410         
2411         if (procmsg_queue_is_empty(item))
2412                 return;
2413
2414         if (prefs_common.work_offline)
2415                 if (alertpanel(_("Offline warning"), 
2416                                _("You're working offline. Override?"),
2417                                GTK_STOCK_NO, GTK_STOCK_YES,
2418                                NULL) != G_ALERTALTERNATE)
2419                 return;
2420
2421         /* ask for confirmation before sending queued messages only
2422            in online mode and if there is at least one message queued
2423            in any of the folder queue
2424         */
2425         if (prefs_common.confirm_send_queued_messages) {
2426                 if (!prefs_common.work_offline) {
2427                         if (alertpanel(_("Send queued messages"), 
2428                                    _("Send all queued messages?"),
2429                                    GTK_STOCK_CANCEL, _("_Send"),
2430                                    NULL) != G_ALERTALTERNATE)
2431                                 return;
2432                 }
2433         }
2434
2435         if (procmsg_send_queue(item, prefs_common.savemsg, &errstr) < 0) {
2436                 if (!errstr)
2437                         alertpanel_error_log(_("Some errors occurred while "
2438                                            "sending queued messages."));
2439                 else {
2440                         alertpanel_error_log(_("Some errors occurred "
2441                                         "while sending queued messages:\n%s"), errstr);
2442                         g_free(errstr);
2443                 }
2444         }
2445 }
2446
2447 static void folderview_search_cb(GtkAction *action, gpointer data)
2448 {
2449         FolderView *folderview = (FolderView *)data;
2450         summary_search(folderview->summaryview);
2451 }
2452
2453 static void folderview_run_processing_cb(GtkAction *action, gpointer data)
2454 {
2455         FolderView *folderview = (FolderView *)data;
2456         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2457         FolderItem *item;
2458
2459         if (!folderview->selected) return;
2460
2461         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
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         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2474         FolderItem *item;
2475
2476         if (!folderview->selected) return;
2477
2478         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
2479         cm_return_if_fail(item != NULL);
2480         cm_return_if_fail(item->folder != NULL);
2481
2482         prefs_folder_item_open(item);
2483 }
2484
2485 static void folderview_recollapse_nodes(FolderView *folderview, GtkCMCTreeNode *node)
2486 {
2487         GSList *list = NULL;
2488         GSList *done = NULL;
2489         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2490         
2491         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2492                 if (!gtkut_ctree_node_is_parent(GTK_CMCTREE_NODE(list->data), node)
2493                 &&  list->data != node) {
2494                         gtk_cmctree_collapse(ctree, GTK_CMCTREE_NODE(list->data));
2495                         done = g_slist_append(done, GTK_CMCTREE_NODE(list->data));
2496                 }
2497         }
2498         for (list = done; list != NULL; list = g_slist_next(list)) {
2499                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2500                                                                  list->data);
2501         }
2502         g_slist_free(done);
2503 }
2504
2505 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2506                             FolderItem *to_folder, gboolean copy)
2507 {
2508         FolderItem *new_folder = NULL;
2509         gchar *buf;
2510         gint status;
2511
2512         cm_return_if_fail(folderview != NULL);
2513         cm_return_if_fail(from_folder != NULL);
2514         cm_return_if_fail(to_folder != NULL);
2515
2516         if (prefs_common.warn_dnd) {
2517                 buf = g_strdup_printf(copy ? _("Do you really want to copy folder '%s' in '%s'?"):
2518                                              _("Do you really want to make folder '%s' a subfolder of '%s'?"), 
2519                                         from_folder->name, to_folder->name);
2520                 status = alertpanel_full(copy ? _("Copy folder"):_("Move folder"), buf,
2521                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, TRUE,
2522                                          NULL, ALERT_QUESTION, G_ALERTDEFAULT);
2523                 g_free(buf);
2524
2525                 if ((status & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
2526                         return;
2527                 else if (status & G_ALERTDISABLE)
2528                         prefs_common.warn_dnd = FALSE;
2529         }
2530
2531         buf = g_strdup_printf(copy ? _("Copying %s to %s..."):_("Moving %s to %s..."), 
2532                                 from_folder->name, to_folder->name);
2533         STATUSBAR_PUSH(folderview->mainwin, buf);
2534         g_free(buf);
2535         summary_clear_all(folderview->summaryview);
2536         folderview->opened = NULL;
2537         folderview->selected = NULL;
2538         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2539         inc_lock();
2540         main_window_cursor_wait(folderview->mainwin);
2541
2542         statusbar_verbosity_set(FALSE);
2543         folder_item_update_freeze();
2544         gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
2545         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder, copy)) == F_MOVE_OK) {
2546                 statusbar_verbosity_set(FALSE);
2547                 main_window_cursor_normal(folderview->mainwin);
2548                 STATUSBAR_POP(folderview->mainwin);
2549                 folder_item_update_thaw();
2550                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2551
2552                 folderview_sort_folders(folderview, 
2553                         gtk_cmctree_find_by_row_data(GTK_CMCTREE(folderview->ctree), 
2554                                 NULL, to_folder), new_folder->folder);
2555                 folderview_select(folderview, new_folder);
2556                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2557         } else {
2558                 statusbar_verbosity_set(FALSE);         
2559                 main_window_cursor_normal(folderview->mainwin);
2560                 STATUSBAR_POP(folderview->mainwin);
2561                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2562                 folder_item_update_thaw();
2563                 switch (status) {
2564                 case F_MOVE_FAILED_DEST_IS_PARENT:
2565                         alertpanel_error(_("Source and destination are the same."));
2566                         break;
2567                 case F_MOVE_FAILED_DEST_IS_CHILD:
2568                         alertpanel_error(copy ? _("Can't copy a folder to one of its children."):
2569                                                 _("Can't move a folder to one of its children."));
2570                         break;
2571                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2572                         alertpanel_error(_("A folder cannot be moved between different mailboxes."));
2573                         break;
2574                 default:
2575                         alertpanel_error(copy ? _("Copy failed!"):_("Move failed!"));
2576                         break;
2577                 }
2578         }       
2579         inc_unlock();           
2580         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2581 }
2582
2583 static gint folderview_clist_compare(GtkCMCList *clist,
2584                                      gconstpointer ptr1, gconstpointer ptr2)
2585 {
2586         FolderItem *item1 = ((GtkCMCListRow *)ptr1)->data;
2587         FolderItem *item2 = ((GtkCMCListRow *)ptr2)->data;
2588
2589         if (item1->order > 0 && item2->order > 0)  // if we have an order item, use it
2590         {
2591                 return item1->order - item2->order;
2592         }
2593
2594         // if only one folder has an order it comes first
2595         if (item1->order > 0)
2596         {
2597                 return -1;
2598         }
2599         if (item2->order > 0)
2600         {
2601                 return 1;
2602         }
2603
2604         if (!item1->name)
2605                 return (item2->name != NULL);
2606         if (!item2->name)
2607                 return -1;
2608
2609         return g_utf8_collate(item1->name, item2->name);
2610 }
2611
2612 static void folderview_processing_cb(GtkAction *action, gpointer data)
2613 {
2614         FolderView *folderview = (FolderView *)data;
2615         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2616         FolderItem *item;
2617         gchar *id, *title;
2618
2619         if (!folderview->selected) return;
2620
2621         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
2622         cm_return_if_fail(item != NULL);
2623         cm_return_if_fail(item->folder != NULL);
2624
2625         id = folder_item_get_identifier(item);
2626         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2627         g_free (id);
2628
2629         prefs_filtering_open(&item->prefs->processing, title,
2630                         MANUAL_ANCHOR_PROCESSING, NULL, NULL, FALSE);
2631         g_free (title);
2632 }
2633
2634 void folderview_set_target_folder_color(gint color_op) 
2635 {
2636         gint firstone = 1;
2637         GList *list;
2638         FolderView *folderview;
2639
2640         for (list = folderview_list; list != NULL; list = list->next) {
2641                 folderview = (FolderView *)list->data;
2642                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2643                 if (firstone) {
2644                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2645                                 folderview->color_op;
2646                         firstone = 0;
2647                 }
2648         }
2649 }
2650
2651 static gchar *last_smallfont = NULL;
2652 static gchar *last_normalfont = NULL;
2653 static gchar *last_boldfont = NULL;
2654 static gboolean last_derive = 0;
2655
2656 void folderview_reinit_fonts(FolderView *folderview)
2657 {
2658         /* force reinit */
2659         g_free(last_smallfont);
2660         last_smallfont = NULL;
2661         g_free(last_normalfont);
2662         last_normalfont = NULL;
2663         g_free(last_boldfont);
2664         last_boldfont = NULL;
2665 }
2666
2667 void folderview_reflect_prefs(void)
2668 {
2669         gboolean update_font = FALSE;
2670         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2671         FolderItem *item = folderview_get_selected_item(folderview);
2672         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2673                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2674         gint height = gtk_adjustment_get_value(pos);
2675
2676         if (!last_smallfont || strcmp(last_smallfont, SMALL_FONT) ||
2677                         !last_normalfont || strcmp(last_normalfont, NORMAL_FONT) ||
2678                         !last_boldfont || strcmp(last_boldfont, BOLD_FONT) ||
2679                         last_derive != prefs_common.derive_from_normal_font)
2680                 update_font = TRUE;
2681
2682         g_free(last_smallfont);
2683         last_smallfont = g_strdup(SMALL_FONT);
2684         g_free(last_normalfont);
2685         last_normalfont = g_strdup(NORMAL_FONT);
2686         g_free(last_boldfont);
2687         last_boldfont = g_strdup(BOLD_FONT);
2688         last_derive = prefs_common.derive_from_normal_font;
2689
2690         if (update_font) {              
2691                 normal_style = normal_color_style = bold_style = 
2692                         bold_color_style = bold_tgtfold_style = NULL;
2693
2694                 folderview_init(folderview);
2695         }
2696         gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
2697         folderview_column_set_titles(folderview);
2698         folderview_set_all();
2699
2700         g_signal_handlers_block_by_func
2701                 (G_OBJECT(folderview->ctree),
2702                  G_CALLBACK(folderview_selected), folderview);
2703
2704         if (item) {
2705                 GtkCMCTreeNode *node = gtk_cmctree_find_by_row_data(
2706                         GTK_CMCTREE(folderview->ctree), NULL, item);
2707
2708                 folderview_select(folderview, item);
2709                 folderview->open_folder = FALSE;
2710                 folderview->selected = node;
2711         }
2712
2713         g_signal_handlers_unblock_by_func
2714                 (G_OBJECT(folderview->ctree),
2715                  G_CALLBACK(folderview_selected), folderview);
2716
2717         pos = gtk_scrolled_window_get_vadjustment(
2718                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2719         gtk_adjustment_set_value(pos, height);
2720         gtk_adjustment_changed(pos);
2721         gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2722 }
2723
2724 static void drag_state_stop(FolderView *folderview)
2725 {
2726         if (folderview->drag_timer_id)
2727                 g_source_remove(folderview->drag_timer_id);
2728         folderview->drag_timer_id = 0;
2729         folderview->drag_node = NULL;
2730 }
2731
2732 static gboolean folderview_defer_expand(FolderView *folderview)
2733 {
2734         if (folderview->drag_node) {
2735                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2736                 if (folderview->drag_item->collapsed) {
2737                         gtk_cmctree_expand(GTK_CMCTREE(folderview->ctree), folderview->drag_node);
2738                         folderview->nodes_to_recollapse = g_slist_append
2739                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2740                 }
2741         }
2742         folderview->drag_item  = NULL;
2743         folderview->drag_timer_id = 0;
2744         return FALSE;
2745 }
2746
2747 static void drag_state_start(FolderView *folderview, GtkCMCTreeNode *node, FolderItem *item)
2748 {
2749         /* the idea is that we call drag_state_start() whenever we want expansion to
2750          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2751          * we need to call drag_state_stop() */
2752         drag_state_stop(folderview);
2753         /* request expansion */
2754         if (0 != (folderview->drag_timer_id = g_timeout_add
2755                         (prefs_common.hover_timeout, 
2756                          (GSourceFunc)folderview_defer_expand,
2757                          folderview))) {
2758                 folderview->drag_node = node;
2759                 folderview->drag_item = item;
2760         }                        
2761 }
2762 #ifndef GENERIC_UMPC
2763 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2764                                   FolderView       *folderview)
2765 {
2766         GdkDragContext *context;
2767
2768         cm_return_if_fail(folderview != NULL);
2769         if (folderview->selected == NULL) return;
2770         if (folderview->nodes_to_recollapse) 
2771                 g_slist_free(folderview->nodes_to_recollapse);
2772         folderview->nodes_to_recollapse = NULL;
2773         context = gtk_drag_begin(widget, folderview->target_list,
2774                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2775         gtk_drag_set_icon_default(context);
2776 }
2777 #endif
2778 static void folderview_drag_data_get(GtkWidget        *widget,
2779                                      GdkDragContext   *drag_context,
2780                                      GtkSelectionData *selection_data,
2781                                      guint             info,
2782                                      guint             time,
2783                                      FolderView       *folderview)
2784 {
2785         FolderItem *item;
2786         GList *sel;
2787         gchar *source = NULL;
2788         if (info == TARGET_DUMMY) {
2789                 sel = GTK_CMCLIST(folderview->ctree)->selection;
2790                 if (!sel)
2791                         return;
2792
2793                 item = gtk_cmctree_node_get_row_data
2794                         (GTK_CMCTREE(folderview->ctree),
2795                          GTK_CMCTREE_NODE(sel->data));
2796                 if (item) {
2797                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2798                         gtk_selection_data_set(selection_data,
2799                                                gtk_selection_data_get_target(selection_data), 8,
2800                                                source, strlen(source));
2801                 }
2802         } else {
2803                 g_warning("unknown info %d\n", info);
2804         }
2805 }
2806
2807 static gboolean folderview_update_folder(gpointer source, gpointer userdata)
2808 {
2809         FolderUpdateData *hookdata;
2810         FolderView *folderview;
2811         GtkWidget *ctree;
2812
2813         hookdata = source;
2814         folderview = (FolderView *) userdata;   
2815         cm_return_val_if_fail(hookdata != NULL, FALSE);
2816         cm_return_val_if_fail(folderview != NULL, FALSE);
2817
2818         ctree = folderview->ctree;
2819         cm_return_val_if_fail(ctree != NULL, FALSE);
2820
2821         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2822                 folderview_create_folder_node(folderview, hookdata->item);
2823         else if (hookdata->update_flags & FOLDER_RENAME_FOLDERITEM) {
2824                 GtkCMCTreeNode *node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree),
2825                                 NULL, folder_item_parent(hookdata->item));
2826                 folderview_sort_folders(folderview, node, hookdata->folder);
2827         } else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2828                 GtkCMCTreeNode *node;
2829
2830                 node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree), NULL, hookdata->item);
2831                 if (node != NULL) {
2832                         gtk_cmctree_remove_node(GTK_CMCTREE(ctree), node);
2833                         if (folderview->selected == node)
2834                                 folderview->selected = NULL;
2835                         if (folderview->opened == node)
2836                                 folderview->opened = NULL;
2837                 }
2838         } else if (hookdata->update_flags & FOLDER_MOVE_FOLDERITEM) {
2839                 /* do nothing, it's done by the ADD and REMOVE) */
2840         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2841                 folderview_set(folderview);
2842
2843         return FALSE;
2844 }
2845
2846 static gboolean folderview_dnd_scroll_cb(gpointer data)
2847 {
2848         FolderView *folderview = (FolderView *)data;
2849         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2850                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2851         gint new_val = (int)gtk_adjustment_get_value(pos) + folderview->scroll_value;
2852         gint max = (int)gtk_adjustment_get_upper(pos) -
2853                (int)gtk_adjustment_get_page_size(pos);
2854
2855         if (folderview->scroll_value == 0) {
2856                 folderview->scroll_timeout_id = 0;
2857                 return FALSE;
2858         }
2859
2860         if (folderview->scroll_value > 0 && new_val > max) {
2861                 new_val = max;
2862         } else if (folderview->scroll_value < 0 && new_val < 0) {
2863                 new_val = 0;
2864         }
2865         gtk_adjustment_set_value(pos, new_val);
2866         
2867         return TRUE;
2868 }
2869
2870 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2871                                           GdkDragContext *context,
2872                                           gint            x,
2873                                           gint            y,
2874                                           guint           time,
2875                                           FolderView     *folderview)
2876 {
2877         gint row, column;
2878         FolderItem *item = NULL, *src_item = NULL;
2879         GtkCMCTreeNode *node = NULL;
2880         gboolean acceptable = FALSE;
2881         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2882                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2883         int height = (int)gtk_adjustment_get_page_size(pos);
2884         int total_height = (int)gtk_adjustment_get_upper(pos);
2885         int vpos = (int)gtk_adjustment_get_value(pos);
2886         int offset = prefs_common.show_col_headers ? 24:0;
2887         int dist;
2888
2889         if (gtk_cmclist_get_selection_info
2890                 (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column)) {
2891                 GtkWidget *srcwidget;
2892
2893                 if (y > height - (48 - offset) && height + vpos < total_height) {
2894                         dist = -(height - (48 - offset) - y);
2895                         folderview->scroll_value = 1.41f * (1+(dist / 6));
2896                 } else if (y < 72 - (24 - offset) && y >= 0) {
2897                         dist = 72 - (24 - offset) - y;
2898                         folderview->scroll_value = -1.41f * (1+(dist / 6));
2899                 } else {
2900                         folderview->scroll_value = 0;
2901                 }
2902                 if (folderview->scroll_value != 0 && folderview->scroll_timeout_id == 0) {
2903                         folderview->scroll_timeout_id = 
2904                                 g_timeout_add(30, folderview_dnd_scroll_cb,
2905                                               folderview);
2906                 }
2907
2908                 node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
2909                 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
2910                 src_item = folderview->summaryview->folder_item;
2911
2912                 srcwidget = gtk_drag_get_source_widget(context);
2913                 if (srcwidget == summary_get_main_widget(folderview->summaryview)) {
2914                         /* comes from summaryview */
2915                         /* we are copying messages, so only accept folder items that are not
2916                            the source item, are no root items and can copy messages */
2917                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2918                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2919                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
2920                                 acceptable = TRUE;
2921                 } else if (srcwidget == folderview->ctree) {
2922                         /* comes from folderview */
2923                         /* we are moving folder items, only accept folders that are not
2924                            the source items and can copy messages and create folder items */
2925                         if (item && item->folder && src_item && src_item != item &&
2926                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2927                             FOLDER_CLASS(item->folder)->create_folder != NULL &&
2928                             ((FOLDER_TYPE(item->folder) != F_UNKNOWN &&  FOLDER_TYPE(src_item->folder) != F_UNKNOWN)
2929                              || item->folder == src_item->folder))
2930                                 acceptable = TRUE;
2931                 } else {
2932                         /* comes from another app */
2933                         /* we are adding messages, so only accept folder items that are 
2934                            no root items and can copy messages */
2935                         if (item && item->folder && folder_item_parent(item) != NULL
2936                             && FOLDER_CLASS(item->folder)->add_msg != NULL &&
2937                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
2938                                 acceptable = TRUE;
2939                 }
2940         }
2941
2942         if (acceptable || (src_item && src_item == item))
2943                 drag_state_start(folderview, node, item);
2944         
2945         if (acceptable) {
2946                 g_signal_handlers_block_by_func
2947                         (G_OBJECT(widget),
2948                          G_CALLBACK(folderview_selected), folderview);
2949                 gtk_cmctree_select(GTK_CMCTREE(widget), node);
2950                 g_signal_handlers_unblock_by_func
2951                         (G_OBJECT(widget),
2952                          G_CALLBACK(folderview_selected), folderview);
2953                 gdk_drag_status(context, 
2954                                         (gdk_drag_context_get_actions(context) == GDK_ACTION_COPY ?
2955                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2956         } else {
2957                 if (folderview->opened)
2958                         gtk_cmctree_select(GTK_CMCTREE(widget), folderview->opened);
2959                 gdk_drag_status(context, 0, time);
2960         }
2961
2962         return acceptable;
2963 }
2964
2965 static void folderview_drag_leave_cb(GtkWidget      *widget,
2966                                      GdkDragContext *context,
2967                                      guint           time,
2968                                      FolderView     *folderview)
2969 {
2970         drag_state_stop(folderview);
2971         folderview->scroll_value = 0;
2972         gtk_cmctree_select(GTK_CMCTREE(widget), folderview->opened);
2973 }
2974
2975 static void free_info (gpointer stuff, gpointer data)
2976 {
2977         g_free(stuff);
2978 }
2979
2980 void folderview_finish_dnd(const gchar *data, GdkDragContext *drag_context,
2981                            guint time, FolderItem *item)
2982 {
2983         GList *list, *tmp;
2984         GSList *msglist = NULL;
2985         list = uri_list_extract_filenames(data);
2986         if (!(item && item->folder && folder_item_parent(item) != NULL
2987                     && FOLDER_CLASS(item->folder)->add_msg != NULL))
2988         {
2989                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2990                 debug_print("item doesn't fit\n");                      
2991                 return;
2992         }       
2993         if (!list) {
2994                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2995                 debug_print("list is empty\n");                 
2996                 return;
2997         }
2998         for (tmp = list; tmp != NULL; tmp = tmp->next) {
2999                 MsgFileInfo *info = NULL;
3000
3001                 if (file_is_email((gchar *)tmp->data)) {
3002                         info = g_new0(MsgFileInfo, 1);
3003                         info->msginfo = NULL;
3004                         info->file = (gchar *)tmp->data;
3005                         msglist = g_slist_prepend(msglist, info);
3006                         debug_print("file is a mail\n");
3007                 } else {
3008                         debug_print("file isn't a mail\n");
3009                 }
3010         }
3011         if (msglist) {
3012                 msglist = g_slist_reverse(msglist);
3013                 folder_item_add_msgs(item, msglist, FALSE);
3014                 g_slist_foreach(msglist, free_info, NULL);
3015                 g_slist_free(msglist);
3016                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
3017         } else {
3018                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3019         }
3020         list_free_strings(list);
3021         g_list_free(list);
3022 }
3023
3024 static void folderview_drag_received_cb(GtkWidget        *widget,
3025                                         GdkDragContext   *drag_context,
3026                                         gint              x,
3027                                         gint              y,
3028                                         GtkSelectionData *data,
3029                                         guint             info,
3030                                         guint             time,
3031                                         FolderView       *folderview)
3032 {
3033         gint row, column;
3034         FolderItem *item = NULL, *src_item;
3035         GtkCMCTreeNode *node;
3036         int offset = prefs_common.show_col_headers ? 24:0;
3037
3038         folderview->scroll_value = 0;
3039
3040         if (info == TARGET_DUMMY) {
3041                 drag_state_stop(folderview);
3042                 const gchar *ddata = (const gchar *)gtk_selection_data_get_data(data);
3043                 if ((gchar *)strstr(ddata, "FROM_OTHER_FOLDER") != ddata) {
3044                         /* comes from summaryview */
3045                         if (gtk_cmclist_get_selection_info
3046                                 (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0)
3047                                 return;
3048
3049                         node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3050                         item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3051                         src_item = folderview->summaryview->folder_item;
3052
3053                         if (item->no_select) {
3054                                 alertpanel_error(_("The destination folder can only be used to "
3055                                                    "store subfolders."));
3056                                 return;
3057                         }
3058                         /* re-check (due to acceptable possibly set for folder moves */
3059                         if (!(item && item->folder && item->path && !item->no_select && 
3060                               src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
3061                                 return;
3062                         }
3063                         if (item && src_item) {
3064                                 switch (gdk_drag_context_get_selected_action(drag_context)) {
3065                                 case GDK_ACTION_COPY:
3066                                         summary_copy_selected_to(folderview->summaryview, item);
3067                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
3068                                         break;
3069                                 case GDK_ACTION_MOVE:
3070                                 case GDK_ACTION_DEFAULT:
3071                                 default:
3072                                         if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
3073                                                 summary_copy_selected_to(folderview->summaryview, item);
3074                                         else
3075                                                 summary_move_selected_to(folderview->summaryview, item);
3076                                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3077                                 }
3078                         } else
3079                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3080                 } else {
3081                         /* comes from folderview */
3082                         char *source;
3083                         gboolean folder_is_normal = TRUE;
3084                         gboolean copy = (GDK_ACTION_COPY ==
3085                                 gdk_drag_context_get_selected_action(drag_context));
3086
3087                         source = (char *)gtk_selection_data_get_data(data) + 17;
3088                         if (gtk_cmclist_get_selection_info
3089                             (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0
3090                             || *source == 0) {
3091                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3092                                 return;
3093                         }
3094                         node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3095                         item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3096                         src_item = folder_find_item_from_identifier(source);
3097
3098                         folder_is_normal = 
3099                                 src_item != NULL &&
3100                                 src_item->stype == F_NORMAL &&
3101                                 !folder_has_parent_of_type(src_item, F_OUTBOX) &&
3102                                 !folder_has_parent_of_type(src_item, F_DRAFT) &&
3103                                 !folder_has_parent_of_type(src_item, F_QUEUE) &&
3104                                 !folder_has_parent_of_type(src_item, F_TRASH);
3105                         if (!item || !src_item || !folder_is_normal) {
3106                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3107                                 return;
3108                         }
3109
3110                         folderview_move_folder(folderview, src_item, item, copy);
3111                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3112                 }
3113                 folderview->nodes_to_recollapse = NULL;
3114         } else if (info == TARGET_MAIL_URI_LIST) {
3115                 if (gtk_cmclist_get_selection_info
3116                         (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0)
3117                         return;
3118
3119                 node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3120                 if (!node) {
3121                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
3122                         debug_print("no node\n");               
3123                         return;
3124                 }
3125                 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3126                 if (!item) {
3127                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3128                         debug_print("no item\n");
3129                         return;
3130                 }
3131                 folderview_finish_dnd(gtk_selection_data_get_data(data),
3132                         drag_context, time, item);
3133         }
3134 }
3135
3136 static void folderview_drag_end_cb(GtkWidget        *widget, 
3137                                    GdkDragContext   *drag_context,
3138                                    FolderView       *folderview)
3139 {
3140         drag_state_stop(folderview);
3141         folderview->scroll_value = 0;
3142         g_slist_free(folderview->nodes_to_recollapse);
3143         folderview->nodes_to_recollapse = NULL;
3144 }
3145
3146 void folderview_register_popup(FolderViewPopup *fpopup)
3147 {
3148         GList *folderviews;
3149
3150         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
3151                 FolderView *folderview = folderviews->data;
3152                 GtkActionGroup *factory;
3153
3154                 factory = create_action_group(folderview, fpopup);
3155                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
3156         }       
3157         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
3158 }
3159
3160 void folderview_unregister_popup(FolderViewPopup *fpopup)
3161 {
3162         GList *folderviews;
3163
3164
3165         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
3166                 FolderView *folderview = folderviews->data;
3167
3168                 g_hash_table_remove(folderview->popups, fpopup->klass);
3169         }       
3170         g_hash_table_remove(folderview_popups, fpopup->klass);
3171 }