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