Rename a misnamed function, remove debug statements.
[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_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_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 (folderview->summaryview->folder_item != item &&
880             prefs_common.ask_mark_all_read) {
881                 val = alertpanel_full(title, message,
882                           GTK_STOCK_NO, GTK_STOCK_YES, NULL,
883                           TRUE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
884
885                 if ((val & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
886                         return;
887                 else if (val & G_ALERTDISABLE)
888                         prefs_common.ask_mark_all_read = FALSE;
889         }
890         
891         folder_item_update_freeze();
892         if (folderview->summaryview->folder_item != item && !recursive)
893                 summary_lock(folderview->summaryview);
894         else
895                 summary_freeze(folderview->summaryview);
896                 
897         if (read) {
898                 if (recursive)
899                         folderutils_mark_all_read_recursive(item);
900                 else
901                         folderutils_mark_all_read(item);
902         } else {
903                 if (recursive)
904                         folderutils_mark_all_unread_recursive(item);
905                 else
906                         folderutils_mark_all_unread(item);
907         }
908         if (folderview->summaryview->folder_item != item && !recursive)
909                 summary_unlock(folderview->summaryview);
910         else
911                 summary_thaw(folderview->summaryview);
912         folder_item_update_thaw();
913 }
914
915 static void folderview_select_node(FolderView *folderview, GtkCMCTreeNode *node)
916 {
917         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
918
919         cm_return_if_fail(node != NULL);
920
921         if (folderview->open_folder) {
922                 return;
923         }
924
925         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
926         gtkut_ctree_expand_parent_all(ctree, node);
927
928         folderview->open_folder = TRUE;
929         gtkut_ctree_set_focus_row(ctree, node);
930         gtk_cmctree_select(ctree, node);
931         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
932         if ((folderview->summaryview->folder_item &&
933             folderview->summaryview->folder_item->total_msgs > 0) ||
934              prefs_common.layout_mode == SMALL_LAYOUT)
935                 summary_select_node(folderview->summaryview,
936                                     folderview->summaryview->selected, -1);
937         else
938                 gtk_widget_grab_focus(folderview->ctree);
939 }
940
941 void folderview_unselect(FolderView *folderview)
942 {
943         if (folderview->opened && !GTK_CMCTREE_ROW(folderview->opened)->children)
944                 gtk_cmctree_collapse
945                         (GTK_CMCTREE(folderview->ctree), folderview->opened);
946
947         folderview->selected = folderview->opened = NULL;
948 }
949
950 static GtkCMCTreeNode *folderview_find_next_with_flag(GtkCMCTree *ctree,
951                                                       GtkCMCTreeNode *node,
952                                                       MsgPermFlags flag)
953 {
954         FolderItem *item;
955
956         if (node)
957                 node = gtkut_ctree_node_next(ctree, node);
958         else
959                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
960
961         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
962                 item = gtk_cmctree_node_get_row_data(ctree, node);
963                 if (!item)
964                         continue;
965                 if (item->stype == F_TRASH || item->stype == F_DRAFT)
966                         continue;
967                 switch (flag) {
968                 case MSG_UNREAD:
969                         if(item->unread_msgs > 0)
970                                 return node;
971                         break;
972                 case MSG_NEW:
973                         if(item->new_msgs > 0)
974                                 return node;
975                         break;
976                 case MSG_MARKED:
977                         if(item->marked_msgs > 0)
978                                 return node;
979                         break;
980                 default:
981                         if(item->total_msgs > 0)
982                                 return node;
983                         break;
984                 }
985         }
986
987         return NULL;
988 }
989
990 void folderview_select_next_with_flag(FolderView *folderview,
991                                       MsgPermFlags flag)
992 {
993         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
994         GtkCMCTreeNode *node = NULL;
995         EntryAction last_summary_select_prio = prefs_common.summary_select_prio[0];
996         
997         switch (flag) {
998         case MSG_UNREAD:
999                 prefs_common.summary_select_prio[0] = ACTION_UNREAD;
1000                 break;
1001         case MSG_NEW:
1002                 prefs_common.summary_select_prio[0] = ACTION_NEW;
1003                 break;
1004         case MSG_MARKED:
1005                 prefs_common.summary_select_prio[0] = ACTION_MARKED;
1006                 break;
1007         default:
1008                 prefs_common.summary_select_prio[0] = ACTION_FIRST_LIST;
1009                 break;
1010         }
1011
1012         node = folderview_find_next_with_flag(ctree, folderview->opened, flag);
1013         if (node != NULL) {
1014                 folderview_select_node(folderview, node);
1015                 goto out;
1016         }
1017
1018         if (!folderview->opened ||
1019             folderview->opened == GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list)) {
1020                 goto out;
1021         }
1022
1023         /* search again from the first node */
1024         node = folderview_find_next_with_flag(ctree, NULL, flag);
1025         if (node != NULL)
1026                 folderview_select_node(folderview, node);
1027
1028 out:
1029         prefs_common.summary_select_prio[0] = last_summary_select_prio;
1030 }
1031
1032 FolderItem *folderview_get_selected_item(FolderView *folderview)
1033 {
1034         g_return_val_if_fail(folderview != NULL, NULL);
1035         g_return_val_if_fail(folderview->ctree != NULL, NULL);
1036
1037         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1038
1039         if (!folderview->selected) return NULL;
1040         return gtk_cmctree_node_get_row_data(ctree, folderview->selected);
1041 }
1042
1043 FolderItem *folderview_get_opened_item(FolderView *folderview)
1044 {
1045         g_return_val_if_fail(folderview != NULL, NULL);
1046         g_return_val_if_fail(folderview->ctree != NULL, NULL);
1047
1048         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1049
1050         if (!folderview->opened) return NULL;
1051         return gtk_cmctree_node_get_row_data(ctree, folderview->opened);
1052 }
1053
1054 static void folderview_set_folders(FolderView *folderview)
1055 {
1056         GList *list;
1057         list = folder_get_list();
1058
1059         for (; list != NULL; list = list->next) {
1060                 folderview_append_folder(folderview, FOLDER(list->data));
1061         }
1062 }
1063
1064 static gchar *get_scan_str(FolderItem *item)
1065 {
1066         if (item->path)
1067                 return g_strdup_printf(_("Scanning folder %s/%s..."),
1068                                       item->folder->name, item->path);
1069         else
1070                 return g_strdup_printf(_("Scanning folder %s..."),
1071                                       item->folder->name);      
1072 }
1073 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
1074                                       gpointer data)
1075 {
1076         GList *list;
1077         for (list = folderview_list; list != NULL; list = list->next) {
1078                 FolderView *folderview = (FolderView *)list->data;
1079                 MainWindow *mainwin = folderview->mainwin;
1080                 gchar *str = get_scan_str(item);
1081
1082                 STATUSBAR_PUSH(mainwin, str);
1083                 STATUSBAR_POP(mainwin);
1084                 g_free(str);
1085         }
1086 }
1087
1088 void folderview_rescan_tree(Folder *folder, gboolean rebuild)
1089 {
1090         GtkWidget *window;
1091         MainWindow *mainwin = mainwindow_get_mainwindow();
1092         FolderView *folderview = NULL;
1093         GtkAdjustment *pos = NULL;
1094         gint height = 0;
1095
1096         cm_return_if_fail(folder != NULL);
1097
1098         if (!folder->klass->scan_tree) return;
1099
1100         if (rebuild && 
1101             alertpanel_full(_("Rebuild folder tree"), 
1102                          _("Rebuilding the folder tree will remove "
1103                            "local caches. Do you want to continue?"),
1104                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, FALSE,
1105                          NULL, ALERT_WARNING, G_ALERTDEFAULT) 
1106                 != G_ALERTALTERNATE) {
1107                 return;
1108         }
1109
1110         inc_lock();
1111         if (rebuild)
1112                 window = label_window_create(_("Rebuilding folder tree..."));
1113         else 
1114                 window = label_window_create(_("Scanning folder tree..."));
1115
1116         if (mainwin)
1117                 folderview = mainwin->folderview;
1118         
1119         if (folderview) {
1120                 pos = gtk_scrolled_window_get_vadjustment(
1121                                         GTK_SCROLLED_WINDOW(folderview->scrolledwin));
1122                 height = gtk_adjustment_get_value(pos);
1123         }
1124
1125         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
1126         folder_scan_tree(folder, rebuild);
1127         folder_set_ui_func(folder, NULL, NULL);
1128
1129         folderview_set_all();
1130
1131         if (folderview) {
1132                 pos = gtk_scrolled_window_get_vadjustment(
1133                                         GTK_SCROLLED_WINDOW(folderview->scrolledwin));
1134                 gtk_adjustment_set_value(pos, height);
1135                 gtk_adjustment_changed(pos);
1136         }
1137         label_window_destroy(window);
1138         inc_unlock();
1139 }
1140
1141 /** folderview_check_new()
1142  *  Scan and update the folder and return the 
1143  *  count the number of new messages since last check. 
1144  *  \param folder the folder to check for new messages
1145  *  \return the number of new messages since last check
1146  */
1147 gint folderview_check_new(Folder *folder)
1148 {
1149         GList *list;
1150         FolderItem *item;
1151         FolderView *folderview;
1152         GtkCMCTree *ctree;
1153         GtkCMCTreeNode *node;
1154         gint new_msgs = 0;
1155         gint former_new_msgs = 0;
1156         gint former_new = 0, former_unread = 0, former_total;
1157
1158         for (list = folderview_list; list != NULL; list = list->next) {
1159                 folderview = (FolderView *)list->data;
1160                 ctree = GTK_CMCTREE(folderview->ctree);
1161                 folderview->scanning_folder = folder;
1162                 inc_lock();
1163                 main_window_lock(folderview->mainwin);
1164
1165                 for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
1166                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1167                         gchar *str = NULL;
1168                         item = gtk_cmctree_node_get_row_data(ctree, node);
1169                         if (!item || !item->path || !item->folder) continue;
1170                         if (item->no_select) continue;
1171                         if (folder && folder != item->folder) continue;
1172                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1173                         if (!item->prefs->newmailcheck) continue;
1174                         if (item->processing_pending == TRUE) {
1175                                 debug_print("skipping %s, processing pending\n",
1176                                         item->path ? item->path : item->name);
1177                                 continue;
1178                         }
1179                         if (item->scanning != ITEM_NOT_SCANNING) {
1180                                 debug_print("skipping %s, scanning\n",
1181                                         item->path ? item->path : item->name);
1182                                 continue;
1183                         }
1184
1185                         str = get_scan_str(item);
1186
1187                         STATUSBAR_PUSH(folderview->mainwin, str);
1188                         GTK_EVENTS_FLUSH();
1189                         g_free(str);
1190
1191                         folderview_scan_tree_func(item->folder, item, NULL);
1192                         former_new    = item->new_msgs;
1193                         former_unread = item->unread_msgs;
1194                         former_total  = item->total_msgs;
1195
1196                         if (item->folder->klass->scan_required &&
1197                             (item->folder->klass->scan_required(item->folder, item) ||
1198                              item->folder->inbox == item ||
1199                              item->opened == TRUE ||
1200                              item->processing_pending == TRUE)) {
1201                                 if (folder_item_scan(item) < 0) {
1202                                         if (folder) {
1203                                                 summaryview_unlock(folderview->summaryview, item);
1204                                                 if (FOLDER_TYPE(item->folder) == F_NEWS || FOLDER_IS_LOCAL(folder)) {
1205                                                         log_error(LOG_PROTOCOL, _("Couldn't scan folder %s\n"),
1206                                                                 item->path ? item->path:item->name);
1207                                                         STATUSBAR_POP(folderview->mainwin);
1208                                                         continue;
1209                                                 } else if (!FOLDER_IS_LOCAL(folder)) {
1210                                                         STATUSBAR_POP(folderview->mainwin);
1211                                                         break;
1212                                                 }
1213                                         }
1214                                 }
1215                         } else if (!item->folder->klass->scan_required) {
1216                                 if (folder_item_scan(item) < 0) {
1217                                         summaryview_unlock(folderview->summaryview, item);
1218                                         if (folder && !FOLDER_IS_LOCAL(folder)) {
1219                                                 STATUSBAR_POP(folderview->mainwin);
1220                                                 break;
1221                                         }
1222                                 }
1223                         }
1224                         if (former_new    != item->new_msgs ||
1225                             former_unread != item->unread_msgs ||
1226                             former_total  != item->total_msgs)
1227                                 folderview_update_node(folderview, node);
1228
1229                         new_msgs += item->new_msgs;
1230                         former_new_msgs += former_new;
1231                         STATUSBAR_POP(folderview->mainwin);
1232                 }
1233                 folderview->scanning_folder = NULL;
1234                 main_window_unlock(folderview->mainwin);
1235                 inc_unlock();
1236         }
1237
1238         folder_write_list();
1239         /* Number of new messages since last check is the just the difference 
1240          * between former_new_msgs and new_msgs. If new_msgs is less than
1241          * former_new_msgs, that would mean another session accessed the folder
1242          * and the result is not well defined.
1243          */
1244         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1245         return new_msgs;
1246 }
1247
1248 void folderview_check_new_all(void)
1249 {
1250         GList *list;
1251         GtkWidget *window;
1252         FolderView *folderview;
1253
1254         folderview = (FolderView *)folderview_list->data;
1255
1256         inc_lock();
1257         main_window_lock(folderview->mainwin);
1258         window = label_window_create
1259                 (_("Checking for new messages in all folders..."));
1260
1261         list = folder_get_list();
1262         for (; list != NULL; list = list->next) {
1263                 Folder *folder = list->data;
1264
1265                 folderview_check_new(folder);
1266         }
1267
1268         folder_write_list();
1269         folderview_set_all();
1270
1271         label_window_destroy(window);
1272         main_window_unlock(folderview->mainwin);
1273         inc_unlock();
1274 }
1275
1276 static gboolean folderview_have_children_sub(FolderView *folderview,
1277                                              FolderItem *item,
1278                                              gboolean in_sub)
1279 {
1280         GNode *node = NULL;
1281         
1282         if (!item || !item->folder || !item->folder->node)
1283                 return FALSE;
1284                 
1285         node = item->folder->node;
1286         
1287         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1288         node = node->children;
1289
1290         if (in_sub && item->total_msgs > 0) {
1291                 return TRUE;
1292         }
1293
1294         while (node != NULL) {
1295                 if (node && node->data) {
1296                         FolderItem *next_item = (FolderItem*) node->data;
1297                         node = node->next;
1298                         if (folderview_have_children_sub(folderview, 
1299                                                          next_item, TRUE))
1300                                 return TRUE;
1301                 }
1302         }
1303
1304         return FALSE;
1305 }
1306
1307 static gboolean folderview_have_children(FolderView *folderview,
1308                                          FolderItem *item)
1309 {
1310         return folderview_have_children_sub(folderview, item, FALSE);
1311 }
1312
1313 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1314                                                  FolderItem *item,
1315                                                  gboolean in_sub)
1316 {
1317         GNode *node = NULL;
1318         
1319         if (!item || !item->folder || !item->folder->node)
1320                 return FALSE;
1321                 
1322         node = item->folder->node;
1323         
1324         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1325         node = node->children;
1326
1327         if (in_sub &&
1328             (item->new_msgs > 0 ||
1329             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1330                 return TRUE;
1331         }
1332
1333         while (node != NULL) {
1334                 if (node && node->data) {
1335                         FolderItem *next_item = (FolderItem*) node->data;
1336                         node = node->next;
1337                         if (folderview_have_new_children_sub(folderview, 
1338                                                              next_item, TRUE))
1339                                 return TRUE;
1340                 }
1341         }
1342
1343         return FALSE;
1344 }
1345
1346 static gboolean folderview_have_new_children(FolderView *folderview,
1347                                              FolderItem *item)
1348 {
1349         return folderview_have_new_children_sub(folderview, item, FALSE);
1350 }
1351
1352 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1353                                                     FolderItem *item, 
1354                                                     gboolean in_sub)
1355 {
1356         GNode *node = NULL;
1357         
1358         if (!item || !item->folder || !item->folder->node)
1359                 return FALSE;
1360         
1361         node = item->folder->node;
1362         
1363         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1364         node = node->children;
1365
1366         if (in_sub &&
1367             (item->unread_msgs > 0 ||
1368             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1369                 return TRUE;
1370         }
1371
1372         while (node != NULL) {
1373                 if (node && node->data) {
1374                         FolderItem *next_item = (FolderItem*) node->data;
1375                         node = node->next;
1376                         if (folderview_have_unread_children_sub(folderview, 
1377                                                                 next_item, 
1378                                                                 TRUE))
1379                                 return TRUE;
1380                 }
1381         }
1382
1383         return FALSE;
1384 }
1385
1386 static gboolean folderview_have_unread_children(FolderView *folderview,
1387                                                 FolderItem *item)
1388 {
1389         return folderview_have_unread_children_sub(folderview, item, FALSE);
1390 }
1391
1392 static gboolean folderview_have_read_children_sub(FolderView *folderview,
1393                                                     FolderItem *item, 
1394                                                     gboolean in_sub)
1395 {
1396         GNode *node = NULL;
1397         
1398         if (!item || !item->folder || !item->folder->node) {
1399                 return FALSE;
1400         }                       
1401
1402         node = item->folder->node;
1403         
1404         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1405         node = node->children;
1406
1407         if (in_sub &&
1408             (((item->total_msgs > 0) &&
1409                 (item->unread_msgs != (item->total_msgs - item->ignored_msgs))))) {
1410                 return TRUE;
1411         }
1412
1413         while (node != NULL) {
1414                 if (node && node->data) {
1415                         FolderItem *next_item = (FolderItem*) node->data;
1416                         node = node->next;
1417                         if (folderview_have_read_children_sub(folderview, 
1418                                                                 next_item, 
1419                                                                 TRUE)) {
1420                                 return TRUE;
1421                         }
1422                 }
1423         }
1424
1425         return FALSE;
1426 }
1427
1428 static gboolean folderview_have_read_children(FolderView *folderview,
1429                                                 FolderItem *item)
1430 {
1431         return folderview_have_read_children_sub(folderview, item, FALSE);
1432 }
1433
1434 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1435                                                       FolderItem *item,
1436                                                       gboolean in_sub)
1437 {
1438         GNode *node = NULL;
1439
1440         if (!item || !item->folder || !item->folder->node)
1441                 return FALSE;
1442
1443         node = item->folder->node;
1444         
1445         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1446         node = node->children;
1447
1448         if (in_sub && item->search_match){
1449                 return TRUE;
1450         }
1451
1452         while (node != NULL) {
1453                 if (node && node->data) {
1454                         FolderItem *next_item = (FolderItem*) node->data;
1455                         node = node->next;
1456                         if (folderview_have_matching_children_sub(folderview, 
1457                                                                   next_item, 
1458                                                                   TRUE))
1459                                 return TRUE;
1460                 }
1461         }
1462
1463         return FALSE;
1464 }
1465
1466 static gboolean folderview_have_matching_children(FolderView *folderview,
1467                                                   FolderItem *item)
1468 {
1469         return folderview_have_matching_children_sub(folderview, item, FALSE);
1470 }
1471
1472 static gboolean folderview_have_marked_children_sub(FolderView *folderview,
1473                                                     FolderItem *item,
1474                                                     gboolean in_sub)
1475 {
1476         GNode *node = NULL;
1477         
1478         if (!item || !item->folder || !item->folder->node)
1479                 return FALSE;
1480                 
1481         node = item->folder->node;
1482         
1483         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1484         node = node->children;
1485
1486         if (item->marked_msgs != 0) {
1487                 return TRUE;
1488         }
1489
1490         while (node != NULL) {
1491                 if (node && node->data) {
1492                         FolderItem *next_item = (FolderItem*) node->data;
1493                         node = node->next;
1494                         if (folderview_have_marked_children_sub(folderview,
1495                                                                 next_item, TRUE))
1496                                 return TRUE;
1497                 }
1498         }
1499
1500         return FALSE;
1501 }
1502
1503 static gboolean folderview_have_marked_children(FolderView *folderview,
1504                                              FolderItem *item)
1505 {
1506         return folderview_have_marked_children_sub(folderview, item, FALSE);
1507 }
1508
1509 static void folderview_update_node(FolderView *folderview, GtkCMCTreeNode *node)
1510 {
1511         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1512         GtkStyle *style = NULL;
1513         GtkStyle *color_style = NULL;
1514         FolderItem *item;
1515         GdkPixbuf *xpm, *openxpm;
1516         static GdkPixbuf *searchicon;
1517         gboolean mark = FALSE;
1518         gchar *name;
1519         gchar *str;
1520         gboolean add_unread_mark;
1521         gboolean add_sub_match_mark;
1522         gboolean use_bold, use_color;
1523         gint *col_pos = folderview->col_pos;
1524         SpecialFolderItemType stype;
1525         
1526         item = gtk_cmctree_node_get_row_data(ctree, node);
1527         cm_return_if_fail(item != NULL);
1528
1529         if (!GTK_CMCTREE_ROW(node)->expanded)
1530                 mark = folderview_have_marked_children(folderview, item);
1531         else
1532                 mark = (item->marked_msgs != 0);
1533
1534         stype = item->stype;
1535         if (stype == F_NORMAL) {
1536                 if (folder_has_parent_of_type(item, F_TRASH))
1537                         stype = F_TRASH;
1538                 else if (folder_has_parent_of_type(item, F_DRAFT))
1539                         stype = F_DRAFT;
1540                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1541                         stype = F_OUTBOX;
1542                 else if (folder_has_parent_of_type(item, F_QUEUE))
1543                         stype = F_QUEUE;
1544         }
1545         switch (stype) {
1546         case F_INBOX:
1547                 if (item->hide_read_msgs || item->hide_read_threads) {
1548                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1549                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1550                 } else {
1551                         xpm = mark?m_inboxxpm:inboxxpm;
1552                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1553                 }
1554                 break;
1555         case F_OUTBOX:
1556                 if (item->hide_read_msgs || item->hide_read_threads) {
1557                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1558                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1559                 } else {
1560                         xpm = mark?m_outboxxpm:outboxxpm;
1561                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1562                 }
1563                 break;
1564         case F_QUEUE:
1565                 if (item->hide_read_msgs || item->hide_read_threads) {
1566                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1567                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1568                 } else {
1569                         xpm = mark?m_queuexpm:queuexpm;
1570                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1571                 }
1572                 break;
1573         case F_TRASH:
1574                 if (item->hide_read_msgs || item->hide_read_threads) {
1575                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1576                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1577                 } else {
1578                         xpm = mark?m_trashxpm:trashxpm;
1579                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1580                 }
1581                 break;
1582         case F_DRAFT:
1583                 xpm = mark?m_draftsxpm:draftsxpm;
1584                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1585                 break;
1586         default:
1587                 if (!item->path &&
1588                     FOLDER_TYPE(item->folder) == F_IMAP &&
1589                     item->folder->account->imap_subsonly) {
1590                         xpm = mark?m_foldersubsxpm:foldersubsxpm;
1591                         openxpm = foldersubsopenxpm;
1592                 } else if (item->no_select) {
1593                         xpm = mark?m_foldernoselectxpm:foldernoselectxpm;
1594                         openxpm = foldernoselectopenxpm;
1595                 } else if (item->hide_read_msgs || item->hide_read_threads) {
1596                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1597                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1598                 } else {
1599                         xpm = mark?m_folderxpm:folderxpm;
1600                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1601                 }
1602         }
1603
1604         name = folder_item_get_name(item);
1605
1606         if (!GTK_CMCTREE_ROW(node)->expanded) {
1607                 add_unread_mark = folderview_have_unread_children(
1608                                         folderview, item);
1609                 add_sub_match_mark = folderview_have_matching_children(
1610                                         folderview, item);
1611         } else {
1612                 add_unread_mark = FALSE;
1613                 add_sub_match_mark = FALSE;
1614         }
1615
1616         if (item->search_match) {
1617                 if (!searchicon) {
1618                         stock_pixbuf_gdk(STOCK_PIXMAP_QUICKSEARCH,
1619                          &searchicon);
1620                 }
1621                 xpm = openxpm = searchicon;
1622         }
1623
1624         str = NULL;
1625         if (prefs_common.display_folder_unread) {
1626                 if (folder_has_parent_of_type(item, F_QUEUE)) {
1627                         /* only total_msgs matters here */
1628                         if (item->total_msgs > 0) {
1629                                 /* show total number (should be equal to the unread number)
1630                                    and signs if any */
1631                                 str = g_strdup_printf("%s (%d%s%s)",
1632                                                         name, item->total_msgs,
1633                                                         (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1634                                                         (item->unreadmarked_msgs > 0) ? "!" : "");
1635                         }
1636                 } else {
1637                         if (prefs_common.display_folder_unread == 1) {
1638                                 if (item->unread_msgs > 0) {
1639                                         /* show unread number and signs */
1640                                         str = g_strdup_printf("%s (%d%s%s)",
1641                                                                 name, item->unread_msgs,
1642                                                                 (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1643                                                                 (item->unreadmarked_msgs > 0) ? "!" : "");
1644                                 }
1645                         } else {
1646                                 if (item->total_msgs > 0) {
1647                                         /* show unread number, total number and signs if any */
1648                                         str = g_strdup_printf("%s (%d/%d%s%s)",
1649                                                                 name, item->unread_msgs, item->total_msgs,
1650                                                                 (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1651                                                                 (item->unreadmarked_msgs > 0) ? "!" : "");
1652                                 }
1653                         }
1654                 }
1655                 if ((str == NULL) &&
1656                         (add_unread_mark || add_sub_match_mark || (item->unreadmarked_msgs > 0))) {
1657                         /* no unread/total numbers, but at least one sign */
1658                         str = g_strdup_printf("%s (%s%s)",
1659                                                 name,
1660                                                 (add_unread_mark || add_sub_match_mark) ? "+" : "", 
1661                                                 (item->unreadmarked_msgs > 0) ? "!" : "");
1662                 }
1663         }
1664         if (str == NULL) {
1665                 /* last fallback, folder name only or with +! sign */
1666                 if (item->unreadmarked_msgs > 0 && add_sub_match_mark) {
1667                         str = g_strdup_printf("%s%s",
1668                                                 name, " (+!)");
1669                 } else if (item->unreadmarked_msgs > 0) {
1670                         str = g_strdup_printf("%s%s",
1671                                                 name, " (!)");
1672                 } else if (add_sub_match_mark) {
1673                         str = g_strdup_printf("%s%s",
1674                                                 name, " (+)");
1675                 } else {
1676                         str = g_strdup_printf("%s", name);
1677                 }
1678         }
1679         gtk_cmctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1680                                 xpm, openxpm, 
1681                                 FALSE, GTK_CMCTREE_ROW(node)->expanded);
1682         g_free(str);
1683         g_free(name);
1684
1685         if (!folder_item_parent(item)) {
1686                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1687                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1688                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1689         } else {
1690                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    item->new_msgs    > 0 ? itos(item->new_msgs)    : prefs_common.zero_replacement);
1691                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], item->unread_msgs > 0 ? itos(item->unread_msgs) : prefs_common.zero_replacement);
1692                 gtk_cmctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  item->total_msgs  > 0 ? itos(item->total_msgs)  : prefs_common.zero_replacement);
1693         }
1694
1695         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1696             folder_has_parent_of_type(item, F_TRASH)) {
1697                 use_bold = use_color = FALSE;
1698         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1699                 GSList *list = folder_item_get_msg_list(item);
1700                 GSList *cur;
1701                 use_bold = use_color = FALSE;
1702                 for (cur = list; cur; cur = cur->next) {
1703                         MsgInfo *msginfo = (MsgInfo *)cur->data;
1704                         if (!MSG_IS_DELETED(msginfo->flags)) {
1705                                 /* highlight queue folder if there are any messages */
1706                                 use_bold = use_color = TRUE;
1707                                 break;
1708                         }
1709                 }
1710                 if (!GTK_CMCTREE_ROW(node)->expanded &&
1711                     use_bold == FALSE &&
1712                     folderview_have_children(folderview, item))
1713                         use_bold = use_color = TRUE;
1714                 procmsg_msg_list_free(list);
1715         } else {
1716                 /* if unread messages exist, print with bold font */
1717                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1718                                 || add_unread_mark;
1719                 /* if new messages exist, print with colored letter */
1720                 use_color =
1721                         (item->new_msgs > 0) ||
1722                         (add_unread_mark &&
1723                          folderview_have_new_children(folderview, item));       
1724         }
1725
1726         gtk_cmctree_node_set_foreground(ctree, node, NULL);
1727
1728         if (use_bold) {
1729                 GdkColor gdk_color;
1730
1731                 if (item->prefs->color > 0 && !use_color) {
1732                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1733                         color_style = gtk_style_copy(bold_style);
1734                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1735                         style = color_style;
1736                 } else if (use_color) {
1737                         style = bold_color_style;
1738                 } else
1739                         style = bold_style;
1740                 if (item->op_count > 0) {
1741                         style = bold_tgtfold_style;
1742                 }
1743         } else if (use_color) {
1744                 style = normal_color_style;
1745                 gtk_cmctree_node_set_foreground(ctree, node,
1746                                               &folderview->color_new);
1747         } else if (item->op_count > 0) {
1748                 style = bold_tgtfold_style;
1749         } else if (item->prefs->color > 0) {
1750                 GdkColor gdk_color;
1751                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1752                 color_style = gtk_style_copy(normal_style);
1753                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1754                 style = color_style;
1755         } else {
1756                 style = normal_style;
1757         }
1758
1759         gtk_cmctree_node_set_row_style(ctree, node, style);
1760
1761         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1762                 folderview_update_node(folderview, node);
1763 }
1764
1765 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1766 {
1767         GList *list;
1768         FolderView *folderview;
1769         GtkCMCTree *ctree;
1770         GtkCMCTreeNode *node;
1771
1772         cm_return_if_fail(item != NULL);
1773
1774         for (list = folderview_list; list != NULL; list = list->next) {
1775                 folderview = (FolderView *)list->data;
1776                 ctree = GTK_CMCTREE(folderview->ctree);
1777
1778                 node = gtk_cmctree_find_by_row_data(ctree, NULL, item);
1779                 if (node && item->search_match != matches) {
1780                         item->search_match = matches;
1781                         folderview_update_node(folderview, node);
1782                 }
1783         }
1784 }
1785
1786 static gboolean folderview_update_item_claws(gpointer source, gpointer data)
1787 {
1788         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1789         FolderView *folderview = (FolderView *)data;
1790         GtkCMCTree *ctree;
1791         GtkCMCTreeNode *node;
1792         cm_return_val_if_fail(update_info != NULL, TRUE);
1793         cm_return_val_if_fail(update_info->item != NULL, TRUE);
1794         cm_return_val_if_fail(folderview != NULL, FALSE);
1795
1796         ctree = GTK_CMCTREE(folderview->ctree);
1797
1798         node = gtk_cmctree_find_by_row_data(ctree, NULL, update_info->item);
1799
1800         if (node) {
1801                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1802                         folderview_update_node(folderview, node);
1803
1804                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && 
1805                      update_info->item == folderview->summaryview->folder_item &&
1806                      update_info->item != NULL)
1807                         if (!quicksearch_has_sat_predicate(folderview->summaryview->quicksearch))
1808                                 summary_show(folderview->summaryview, update_info->item);
1809         }
1810         
1811         return FALSE;
1812 }
1813
1814 static gboolean folderview_gnode_func(GtkCMCTree *ctree, guint depth,
1815                                       GNode *gnode, GtkCMCTreeNode *cnode,
1816                                       gpointer data)
1817 {
1818         FolderView *folderview = (FolderView *)data;
1819         FolderItem *item = FOLDER_ITEM(gnode->data);
1820
1821         cm_return_val_if_fail(item != NULL, FALSE);
1822
1823         gtk_cmctree_node_set_row_data(ctree, cnode, item);
1824         folderview_update_node(folderview, cnode);
1825
1826         return TRUE;
1827 }
1828
1829 static void folderview_expand_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
1830                                    gpointer data)
1831 {
1832         FolderView *folderview = (FolderView *)data;
1833         FolderItem *item;
1834
1835         if (GTK_CMCTREE_ROW(node)->children) {
1836                 item = gtk_cmctree_node_get_row_data(ctree, node);
1837                 cm_return_if_fail(item != NULL);
1838
1839                 if (!item->collapsed)
1840                         gtk_cmctree_expand(ctree, node);
1841                 else
1842                         folderview_update_node(folderview, node);
1843         }
1844 }
1845
1846 static void set_special_folder(GtkCMCTree *ctree, FolderItem *item,
1847                                GtkCMCTreeNode *root, GtkCMCTreeNode **prev)
1848 {
1849         if (item) {
1850                 GtkCMCTreeNode *node, *parent, *sibling;
1851
1852                 node = gtk_cmctree_find_by_row_data(ctree, root, item);
1853                 if (!node)
1854                         g_warning("%s not found.", item->path);
1855                 else {
1856                         parent = GTK_CMCTREE_ROW(node)->parent;
1857                         if (*prev && parent == GTK_CMCTREE_ROW(*prev)->parent)
1858                                 sibling = GTK_CMCTREE_ROW(*prev)->sibling;
1859                         else
1860                                 sibling = GTK_CMCTREE_ROW(parent)->children;
1861                         while (sibling) {
1862                                 FolderItem *tmp;
1863
1864                                 tmp = gtk_cmctree_node_get_row_data
1865                                         (ctree, sibling);
1866                                 if (tmp && tmp->stype != F_NORMAL)
1867                                         sibling = GTK_CMCTREE_ROW(sibling)->sibling;
1868                                 else
1869                                         break;
1870                         }
1871                         if (node != sibling)
1872                                 gtk_cmctree_move(ctree, node, parent, sibling);
1873                 }
1874
1875                 *prev = node;
1876         }
1877 }
1878
1879 static void folderview_sort_folders(FolderView *folderview, GtkCMCTreeNode *root,
1880                                     Folder *folder)
1881 {
1882         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1883         GtkCMCTreeNode *prev = NULL;
1884
1885         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
1886         gtk_sctree_sort_recursive(ctree, root);
1887         if (root && GTK_CMCTREE_ROW(root)->parent) {
1888                 gtk_cmclist_thaw(GTK_CMCLIST(ctree));
1889                 return;
1890         }
1891         set_special_folder(ctree, folder->inbox, root, &prev);
1892         set_special_folder(ctree, folder->outbox, root, &prev);
1893         set_special_folder(ctree, folder->draft, root, &prev);
1894         set_special_folder(ctree, folder->queue, root, &prev);
1895         set_special_folder(ctree, folder->trash, root, &prev);
1896         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
1897 }
1898
1899 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1900 {
1901         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
1902         GtkCMCTreeNode *root;
1903
1904         cm_return_if_fail(folder != NULL);
1905
1906         root = gtk_sctree_insert_gnode(ctree, NULL, NULL, folder->node,
1907                                       folderview_gnode_func, folderview);
1908         gtk_cmctree_pre_recursive(ctree, root, folderview_expand_func,
1909                                 folderview);
1910         folderview_sort_folders(folderview, root, folder);
1911 }
1912
1913 /* callback functions */
1914 static void folderview_set_sens_and_popup_menu(FolderView *folderview, gint row, 
1915                                 GdkEventButton *event)
1916 {
1917         FolderItem *item;
1918         Folder *folder;
1919         FolderViewPopup *fpopup;
1920         GtkActionGroup *action_group;
1921         GtkWidget *popup;
1922         FolderItem *special_trash = NULL, *special_queue = NULL;
1923         PrefsAccount *ac;
1924         GtkUIManager *ui_manager = gtk_ui_manager_new();
1925         
1926         if (folderview->ui_manager)
1927                 g_object_unref(folderview->ui_manager);
1928
1929         folderview->ui_manager = ui_manager;
1930         item = folderview_get_selected_item(folderview);
1931
1932         cm_return_if_fail(item != NULL);
1933         cm_return_if_fail(item->folder != NULL);
1934         folder = item->folder;
1935
1936         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1937
1938         if (fpopup != NULL)
1939                 action_group = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1940         else {
1941                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1942                 action_group = g_hash_table_lookup(folderview->popups, "common");
1943         }
1944         
1945         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
1946         MENUITEM_ADDUI_MANAGER(ui_manager, "/", "Popup", "Popup", GTK_UI_MANAGER_MENUBAR)
1947         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup", "FolderViewPopup", "FolderViewPopup", GTK_UI_MANAGER_MENU)
1948         
1949         if (fpopup->add_menuitems)      
1950                 fpopup->add_menuitems(ui_manager, item);
1951
1952         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MarkAllRead", "FolderViewPopup/MarkAllRead", GTK_UI_MANAGER_MENUITEM)
1953         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MarkAllUnread", "FolderViewPopup/MarkAllUnread", GTK_UI_MANAGER_MENUITEM)
1954         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MarkAllReadRec", "FolderViewPopup/MarkAllReadRec", GTK_UI_MANAGER_MENUITEM)
1955         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MarkAllUnreadRec", "FolderViewPopup/MarkAllUnreadRec", GTK_UI_MANAGER_MENUITEM)
1956         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Separator1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1957         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RunProcessing", "FolderViewPopup/RunProcessing", GTK_UI_MANAGER_MENUITEM)
1958         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SearchFolder", "FolderViewPopup/SearchFolder", GTK_UI_MANAGER_MENUITEM)
1959         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Properties", "FolderViewPopup/Properties", GTK_UI_MANAGER_MENUITEM)
1960         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Processing", "FolderViewPopup/Processing", GTK_UI_MANAGER_MENUITEM)
1961
1962         if (fpopup->set_sensitivity != NULL)
1963                 fpopup->set_sensitivity(ui_manager, item);
1964
1965         if (NULL != (ac = account_find_from_item(item))) {
1966                 special_trash = account_get_special_folder(ac, F_TRASH);
1967                 special_queue = account_get_special_folder(ac, F_QUEUE);
1968         }
1969         
1970         if ((item == folder->trash || item == special_trash
1971              || folder_has_parent_of_type(item, F_TRASH))) {
1972                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorTrash", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1973                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "EmptyTrash", "FolderViewPopup/EmptyTrash", GTK_UI_MANAGER_MENUITEM)
1974         } 
1975         
1976         if ((item == folder->queue || item == special_queue
1977              || folder_has_parent_of_type(item, F_QUEUE))) {
1978                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorQueue", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1979                 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SendQueue", "FolderViewPopup/SendQueue", GTK_UI_MANAGER_MENUITEM)
1980         } 
1981         
1982 #define SET_SENS(name, sens) \
1983         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
1984
1985         SET_SENS("FolderViewPopup/MarkAllRead", item->unread_msgs > 0);
1986         SET_SENS("FolderViewPopup/MarkAllUnread", (item->total_msgs > 0) &&
1987                 (item->unread_msgs != (item->total_msgs - item->ignored_msgs)));
1988         SET_SENS("FolderViewPopup/MarkAllReadRec", folderview_have_unread_children(folderview,item));
1989         SET_SENS("FolderViewPopup/MarkAllUnreadRec", folderview_have_read_children(folderview,item));
1990         SET_SENS("FolderViewPopup/SearchFolder", item->total_msgs > 0 && 
1991                  folderview->selected == folderview->opened);
1992         SET_SENS("FolderViewPopup/Properties", TRUE);
1993
1994         SET_SENS("FolderViewPopup/RunProcessing", item->prefs->processing &&
1995                  item->total_msgs >= 1 && !item->processing_pending);
1996         SET_SENS("FolderViewPopup/Processing", item->node->parent != NULL && 
1997                 !item->no_select && !item->processing_pending);
1998
1999         if (item == folder->trash || item == special_trash
2000             || folder_has_parent_of_type(item, F_TRASH)) {
2001                 GSList *msglist = folder_item_get_msg_list(item);
2002                 SET_SENS("FolderViewPopup/EmptyTrash", msglist != NULL);
2003                 procmsg_msg_list_free(msglist);
2004         }
2005         if (item == folder->queue || item == special_queue
2006             || folder_has_parent_of_type(item, F_QUEUE)) {
2007                 GSList *msglist = folder_item_get_msg_list(item);
2008                 SET_SENS("FolderViewPopup/SendQueue", msglist != NULL);
2009                 procmsg_msg_list_free(msglist);
2010         }
2011 #undef SET_SENS
2012
2013         popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
2014                         gtk_ui_manager_get_widget(ui_manager, "/Popup/FolderViewPopup")) );
2015         g_signal_connect(G_OBJECT(popup), "selection_done",
2016                          G_CALLBACK(folderview_popup_close),
2017                          folderview);
2018         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
2019                        event->button, event->time);
2020 }
2021
2022 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2023                                           FolderView *folderview)
2024 {
2025         GtkCMCList *clist = GTK_CMCLIST(ctree);
2026         gint prev_row = -1, row = -1, column = -1;
2027
2028         if (!event) return FALSE;
2029
2030         if (event->button == 1 || event->button == 2) {
2031                 if (!gtk_sctree_is_hot_spot (GTK_SCTREE(clist), event->x, event->y))
2032                         folderview->open_folder = TRUE;
2033
2034                 if (event->type == GDK_2BUTTON_PRESS) {
2035                         if (clist->selection) {
2036                                 GtkCMCTreeNode *node;
2037
2038                                 node = GTK_CMCTREE_NODE(clist->selection->data);
2039                                 if (node) {
2040                                         gtk_cmctree_toggle_expansion(
2041                                                 GTK_CMCTREE(ctree),
2042                                                 node);
2043                                         folderview->open_folder = FALSE;
2044                                 }
2045                         }
2046                 }
2047                 return FALSE;
2048         }
2049
2050         if (event->button == 2 || event->button == 3) {
2051                 /* right clicked */
2052                 if (clist->selection) {
2053                         GtkCMCTreeNode *node;
2054
2055                         node = GTK_CMCTREE_NODE(clist->selection->data);
2056                         if (node)
2057                                 prev_row = gtkut_ctree_get_nth_from_node
2058                                         (GTK_CMCTREE(ctree), node);
2059                 }
2060
2061                 if (!gtk_cmclist_get_selection_info(clist, event->x, event->y,
2062                                                   &row, &column))
2063                         return FALSE;
2064                 if (prev_row != row) {
2065                         gtk_cmclist_unselect_all(clist);
2066                         if (event->button == 2)
2067                                 folderview_select_node
2068                                         (folderview,
2069                                          gtk_cmctree_node_nth(GTK_CMCTREE(ctree),
2070                                                             row));
2071                         else
2072                                 gtk_cmclist_select_row(clist, row, column);
2073                 }
2074         }
2075
2076         if (event->button != 3) return FALSE;
2077
2078         folderview_set_sens_and_popup_menu(folderview, row, event);
2079         return FALSE;
2080 }
2081
2082 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
2083                                            FolderView *folderview)
2084 {
2085         int row = -1, column = -1;
2086
2087         if (!event) return FALSE;
2088
2089         if (!gtk_cmclist_get_selection_info(GTK_CMCLIST(ctree), event->x, event->y,
2090                                           &row, &column))
2091                 return FALSE;
2092         if (event->button == 1 && folderview->open_folder == FALSE &&
2093             folderview->opened != NULL) {
2094                 gtkut_ctree_set_focus_row(GTK_CMCTREE(ctree),
2095                                           folderview->opened);
2096                 gtk_cmctree_select(GTK_CMCTREE(ctree), folderview->opened);
2097         }
2098
2099         return FALSE;
2100 }
2101
2102 #define BREAK_ON_MODIFIER_KEY() \
2103         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2104
2105 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2106                                        FolderView *folderview)
2107 {
2108         GtkCMCTreeNode *node;
2109         FolderItem *item;
2110
2111         if (!event) return FALSE;
2112
2113         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
2114                 return FALSE;
2115
2116         switch (event->keyval) {
2117         case GDK_KEY_Right:
2118                 if (folderview->selected) {
2119                         if (GTK_CMCTREE_ROW(folderview->selected)->children != NULL
2120                                         && !GTK_CMCTREE_ROW(folderview->selected)->expanded)
2121                                 gtk_cmctree_expand(GTK_CMCTREE(folderview->ctree),
2122                                                 folderview->selected);
2123                         else
2124                                 folderview_select_node(folderview,
2125                                                        folderview->selected);
2126                 }
2127                 break;
2128 #ifdef GENERIC_UMPC
2129         case GDK_KEY_Return:
2130                 if (folderview->selected && GTK_CMCTREE_ROW(folderview->selected)->children) {
2131                         gtk_cmctree_toggle_expansion(
2132                                 GTK_CMCTREE(folderview->ctree),
2133                                 folderview->selected);
2134                 }
2135                 break;  
2136 #else
2137         case GDK_KEY_Return:
2138         case GDK_KEY_KP_Enter:
2139                 if (folderview->selected)
2140                         folderview_select_node(folderview, folderview->selected);
2141                 break;
2142 #endif
2143         case GDK_KEY_space:
2144                 BREAK_ON_MODIFIER_KEY();
2145                 if (folderview->selected) {
2146                         if (folderview->opened == folderview->selected &&
2147                             (!folderview->summaryview->folder_item ||
2148                              folderview->summaryview->folder_item->total_msgs == 0))
2149                                 folderview_select_next_with_flag(folderview, MSG_UNREAD);
2150                         else
2151                                 folderview_select_node(folderview,
2152                                                        folderview->selected);
2153                 }
2154                 break;
2155         case GDK_KEY_Left:
2156                 if (folderview->selected) {
2157                         if (GTK_CMCTREE_ROW(folderview->selected)->expanded) {
2158                                 gtk_cmctree_collapse(GTK_CMCTREE(folderview->ctree),
2159                                                 folderview->selected);
2160                         } else {
2161                                 if ((item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree),
2162                                                 folderview->selected))) {
2163                                         if ((node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(folderview->ctree),
2164                                                         NULL, folder_item_parent(item)))) {
2165                                                 gtk_sctree_select(GTK_SCTREE(folderview->ctree), node);
2166                                                 if (!gtk_cmctree_node_is_visible(GTK_CMCTREE(folderview->ctree), node))
2167                                                         gtk_cmctree_node_moveto(GTK_CMCTREE(folderview->ctree),
2168                                                                         node, -1, 0, 0);
2169                                         }
2170                                 }
2171                         }
2172                 }
2173                 break;
2174         case GDK_KEY_Home:
2175         case GDK_KEY_End:
2176                 if (event->keyval == GDK_KEY_Home)
2177                         node = gtk_cmctree_node_nth(GTK_CMCTREE(folderview->ctree), 0);
2178                 else
2179                         node = gtk_cmctree_last(GTK_CMCTREE(folderview->ctree),
2180                                         gtk_cmctree_node_nth(GTK_CMCTREE(folderview->ctree), 0));
2181
2182                 gtk_sctree_select(GTK_SCTREE(folderview->ctree), node);
2183
2184                 if (!gtk_cmctree_node_is_visible(GTK_CMCTREE(folderview->ctree), node))
2185                         gtk_cmctree_node_moveto(GTK_CMCTREE(folderview->ctree),
2186                                         node, -1, 0, 0);
2187                 break;
2188         default:
2189                 break;
2190         }
2191
2192         return FALSE;
2193 }
2194
2195 typedef struct _PostponedSelectData
2196 {
2197         GtkCMCTree *ctree;
2198         GtkCMCTreeNode *row;
2199         gint column;
2200         FolderView *folderview;
2201 } PostponedSelectData;
2202
2203 static gboolean postpone_select(void *data)
2204 {
2205         PostponedSelectData *psdata = (PostponedSelectData *)data;
2206         debug_print("trying again\n");
2207
2208         psdata->folderview->postpone_select_id = 0;
2209         psdata->folderview->open_folder = TRUE;
2210         main_window_cursor_normal(psdata->folderview->mainwin);
2211         STATUSBAR_POP(psdata->folderview->mainwin);
2212         folderview_selected(psdata->ctree, psdata->row,
2213                             psdata->column, psdata->folderview);
2214         g_free(psdata);
2215         return FALSE;
2216 }
2217
2218 void folderview_close_opened(FolderView *folderview, gboolean dirty)
2219 {
2220         if (folderview->opened) {
2221                 if (dirty) {
2222                         folderview->opened = NULL;
2223                         return;
2224                 }
2225
2226                 FolderItem *olditem =
2227                         gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree), 
2228                                                       folderview->opened);
2229                 if (olditem) {
2230                         gchar *buf = g_strdup_printf(_("Closing folder %s..."), 
2231                                 olditem->path ? olditem->path:olditem->name);
2232                         /* will be null if we just moved the previously opened folder */
2233                         STATUSBAR_PUSH(folderview->mainwin, buf);
2234                         main_window_cursor_wait(folderview->mainwin);
2235                         g_free(buf);
2236                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
2237                         summary_show(folderview->summaryview, NULL);
2238                         folder_item_close(olditem);
2239                         main_window_cursor_normal(folderview->mainwin);
2240                         STATUSBAR_POP(folderview->mainwin);
2241                         if (olditem->folder->klass->item_closed)
2242                                 olditem->folder->klass->item_closed(olditem);
2243
2244                 }
2245         }
2246
2247         if (folderview->opened &&
2248             !GTK_CMCTREE_ROW(folderview->opened)->children)
2249                 gtk_cmctree_collapse(GTK_CMCTREE(folderview->ctree), folderview->opened);
2250
2251         folderview->opened = NULL;
2252 }
2253 static void folderview_selected(GtkCMCTree *ctree, GtkCMCTreeNode *row,
2254                                 gint column, FolderView *folderview)
2255 {
2256         static gboolean can_select = TRUE;      /* exclusive lock */
2257         gboolean opened;
2258         FolderItem *item;
2259         gchar *buf;
2260         int res = 0;
2261         GtkCMCTreeNode *old_opened = folderview->opened;
2262         START_TIMING("");
2263         folderview->selected = row;
2264
2265         debug_print("newly selected %p, opened %p\n", folderview->selected, 
2266                         folderview->opened);
2267         if (folderview->opened == row) {
2268                 folderview->open_folder = FALSE;
2269                 END_TIMING();
2270                 return;
2271         }
2272         
2273         item = gtk_cmctree_node_get_row_data(ctree, row);
2274         if (!item) {
2275                 END_TIMING();
2276                 folderview->open_folder = FALSE;
2277                 return;
2278         }
2279
2280         if (!can_select || summary_is_locked(folderview->summaryview)) {
2281                 if (folderview->opened) {
2282                         gtkut_ctree_set_focus_row(ctree, folderview->opened);
2283                         gtk_cmctree_select(ctree, folderview->opened);
2284                 }
2285                 folderview->open_folder = FALSE;
2286                 END_TIMING();
2287                 return;
2288         }
2289
2290         if (!folderview->open_folder) {
2291                 END_TIMING();
2292                 return;
2293         }
2294
2295         can_select = FALSE;
2296
2297         /* Save cache for old folder */
2298         /* We don't want to lose all caches if sylpheed crashed */
2299         /* resets folderview->opened to NULL */
2300         folderview_close_opened(folderview, FALSE);
2301         
2302         /* CLAWS: set compose button type: news folder items 
2303          * always have a news folder as parent */
2304         if (item->folder) 
2305                 toolbar_set_compose_button
2306                         (folderview->mainwin->toolbar,
2307                          FOLDER_TYPE(item->folder) == F_NEWS ? 
2308                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
2309
2310         if (item->path)
2311                 debug_print("Folder %s is selected\n", item->path);
2312
2313         if (!GTK_CMCTREE_ROW(row)->children)
2314                 gtk_cmctree_expand(ctree, row);
2315
2316         /* ungrab the mouse event */
2317         if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
2318                 gtk_grab_remove(GTK_WIDGET(ctree));
2319                 if (gdk_pointer_is_grabbed())
2320                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
2321         }
2322
2323         /* Open Folder */
2324         /* TODO: wwp: avoid displaying (null) in the status bar */
2325         buf = g_strdup_printf(_("Opening folder %s..."), item->path ? 
2326                                         item->path : "(null)");
2327         debug_print("%s\n", buf);
2328         STATUSBAR_PUSH(folderview->mainwin, buf);
2329         g_free(buf);
2330
2331         main_window_cursor_wait(folderview->mainwin);
2332
2333         if (folderview->scanning_folder == item->folder) {
2334                 res = -2;
2335         } else {
2336                 res = folder_item_open(item);
2337         }
2338
2339         if (res == -1 && item->no_select == FALSE) {
2340                 main_window_cursor_normal(folderview->mainwin);
2341                 STATUSBAR_POP(folderview->mainwin);
2342
2343                 alertpanel_error(_("Folder could not be opened."));
2344
2345                 folderview->open_folder = FALSE;
2346                 can_select = TRUE;
2347                 END_TIMING();
2348                 return;
2349         } else if (res == -2 && item->no_select == FALSE) {
2350                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2351                 data->ctree = ctree;
2352                 data->row = row;
2353                 data->column = column;
2354                 data->folderview = folderview;
2355                 debug_print("postponing open of %s till end of scan\n",
2356                         item->path ? item->path:item->name);
2357                 folderview->open_folder = FALSE;
2358                 can_select = TRUE;
2359                 if (folderview->postpone_select_id != 0)
2360                         g_source_remove(folderview->postpone_select_id);
2361                 folderview->postpone_select_id = g_timeout_add(500, postpone_select, data);
2362                 END_TIMING();
2363                 return;
2364         }
2365         
2366         main_window_cursor_normal(folderview->mainwin);
2367
2368         /* Show messages */
2369         summary_set_prefs_from_folderitem(folderview->summaryview, item);
2370         opened = summary_show(folderview->summaryview, item);
2371         
2372         folder_clean_cache_memory(item);
2373
2374         if (!opened) {
2375                 gtkut_ctree_set_focus_row(ctree, old_opened);
2376                 gtk_cmctree_select(ctree, old_opened);
2377                 folderview->opened = old_opened;
2378         } else {
2379                 folderview->opened = row;
2380                 if (gtk_cmctree_node_is_visible(ctree, row)
2381                     != GTK_VISIBILITY_FULL)
2382                         gtk_cmctree_node_moveto(ctree, row, -1, 0.5, 0);
2383         }
2384
2385         STATUSBAR_POP(folderview->mainwin);
2386
2387         folderview->open_folder = FALSE;
2388         can_select = TRUE;
2389         END_TIMING();
2390 }
2391
2392 static void folderview_tree_expanded(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2393                                      FolderView *folderview)
2394 {
2395         FolderItem *item;
2396
2397         item = gtk_cmctree_node_get_row_data(ctree, node);
2398         cm_return_if_fail(item != NULL);
2399         item->collapsed = FALSE;
2400         folderview_update_node(folderview, node);
2401 }
2402
2403 static void folderview_tree_collapsed(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2404                                       FolderView *folderview)
2405 {
2406         FolderItem *item;
2407
2408         item = gtk_cmctree_node_get_row_data(ctree, node);
2409         cm_return_if_fail(item != NULL);
2410         item->collapsed = TRUE;
2411         folderview_update_node(folderview, node);
2412 }
2413
2414 static void folderview_popup_close(GtkMenuShell *menu_shell,
2415                                    FolderView *folderview)
2416 {
2417         if (!folderview->opened) return;
2418
2419         gtk_cmctree_select(GTK_CMCTREE(folderview->ctree), folderview->opened);
2420 }
2421
2422 static void folderview_col_resized(GtkCMCList *clist, gint column, gint width,
2423                                    FolderView *folderview)
2424 {
2425         FolderColumnType type = folderview->col_state[column].type;
2426
2427         prefs_common.folder_col_size[type] = width;
2428 }
2429
2430 static void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2431 {
2432         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2433         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2434         GtkCMCTreeNode *node, *parent_node;
2435         gint *col_pos = folderview->col_pos;
2436         FolderItemUpdateData hookdata;
2437
2438         parent_node = gtk_cmctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2439         if (parent_node == NULL)
2440                 return;
2441
2442         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
2443
2444         text[col_pos[F_COL_FOLDER]] = item->name;
2445         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2446                                      FOLDER_SPACING,
2447                                      folderxpm,
2448                                      folderopenxpm,
2449                                      FALSE, FALSE);
2450         gtk_cmctree_expand(ctree, parent_node);
2451         gtk_cmctree_node_set_row_data(ctree, node, item);
2452         if (normal_style)
2453                 gtk_cmctree_node_set_row_style(ctree, node, normal_style);
2454         folderview_sort_folders(folderview, parent_node, item->folder);
2455
2456         hookdata.item = item;
2457         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2458         hookdata.msg = NULL;
2459         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2460
2461         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
2462 }
2463
2464 static void folderview_empty_trash_cb(GtkAction *action, gpointer data)
2465 {
2466         FolderView *folderview = (FolderView *)data;
2467         FolderItem *item;
2468         GSList *mlist = NULL;
2469         GSList *cur = NULL;
2470         FolderItem *special_trash = NULL;
2471         PrefsAccount *ac;
2472
2473         if (!folderview->selected) return;
2474         item = folderview_get_selected_item(folderview);
2475         cm_return_if_fail(item != NULL);
2476         cm_return_if_fail(item->folder != NULL);
2477
2478         if (NULL != (ac = account_find_from_item(item)))
2479                 special_trash = account_get_special_folder(ac, F_TRASH);
2480
2481         if (item != item->folder->trash && item != special_trash
2482         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2483         
2484         if (prefs_common.ask_on_clean) {
2485                 if (alertpanel(_("Empty trash"),
2486                                _("Delete all messages in trash?"),
2487                                GTK_STOCK_CANCEL, g_strconcat("+", _("_Empty trash"), NULL), NULL) != G_ALERTALTERNATE)
2488                         return;
2489         }
2490         
2491         mlist = folder_item_get_msg_list(item);
2492         
2493         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2494                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2495                 if (MSG_IS_LOCKED(msginfo->flags))
2496                         continue;
2497                 /* is it partially received? (partial_recv isn't cached) */
2498                 if (msginfo->total_size != 0 && 
2499                     msginfo->size != (off_t)msginfo->total_size)
2500                         partial_mark_for_delete(msginfo);
2501         }
2502         procmsg_msg_list_free(mlist);
2503
2504         folder_item_remove_all_msg(item);
2505 }
2506
2507 static void folderview_send_queue_cb(GtkAction *action, gpointer data)
2508 {
2509         FolderView *folderview = (FolderView *)data;
2510         FolderItem *item;
2511         FolderItem *special_queue = NULL;
2512         PrefsAccount *ac;
2513         gchar *errstr = NULL;
2514
2515         if (!folderview->selected) return;
2516         item = folderview_get_selected_item(folderview);
2517         cm_return_if_fail(item != NULL);
2518         cm_return_if_fail(item->folder != NULL);
2519
2520         if (NULL != (ac = account_find_from_item(item)))
2521                 special_queue = account_get_special_folder(ac, F_QUEUE);
2522
2523         if (item != item->folder->queue && item != special_queue
2524         &&  !folder_has_parent_of_type(item, F_QUEUE)) return;
2525         
2526         if (procmsg_queue_is_empty(item))
2527                 return;
2528
2529         if (prefs_common.work_offline)
2530                 if (alertpanel(_("Offline warning"), 
2531                                _("You're working offline. Override?"),
2532                                GTK_STOCK_NO, GTK_STOCK_YES,
2533                                NULL) != G_ALERTALTERNATE)
2534                 return;
2535
2536         /* ask for confirmation before sending queued messages only
2537            in online mode and if there is at least one message queued
2538            in any of the folder queue
2539         */
2540         if (prefs_common.confirm_send_queued_messages) {
2541                 if (!prefs_common.work_offline) {
2542                         if (alertpanel(_("Send queued messages"), 
2543                                    _("Send all queued messages?"),
2544                                    GTK_STOCK_CANCEL, _("_Send"),
2545                                    NULL) != G_ALERTALTERNATE)
2546                                 return;
2547                 }
2548         }
2549
2550         if (procmsg_send_queue(item, prefs_common.savemsg, &errstr) < 0) {
2551                 if (!errstr)
2552                         alertpanel_error_log(_("Some errors occurred while "
2553                                            "sending queued messages."));
2554                 else {
2555                         alertpanel_error_log(_("Some errors occurred "
2556                                         "while sending queued messages:\n%s"), errstr);
2557                         g_free(errstr);
2558                 }
2559         }
2560 }
2561
2562 static void folderview_search_cb(GtkAction *action, gpointer data)
2563 {
2564         FolderView *folderview = (FolderView *)data;
2565         summary_search(folderview->summaryview);
2566 }
2567
2568 static void folderview_run_processing_cb(GtkAction *action, gpointer data)
2569 {
2570         FolderView *folderview = (FolderView *)data;
2571         FolderItem *item;
2572
2573         if (!folderview->selected) return;
2574
2575         item = folderview_get_selected_item(folderview);
2576         cm_return_if_fail(item != NULL);
2577         cm_return_if_fail(item->folder != NULL);
2578
2579         item->processing_pending = TRUE;
2580         folder_item_apply_processing(item);
2581         item->processing_pending = FALSE;
2582 }
2583
2584 static void folderview_property_cb(GtkAction *action, gpointer data)
2585 {
2586         FolderView *folderview = (FolderView *)data;
2587         FolderItem *item;
2588
2589         if (!folderview->selected) return;
2590
2591         item = folderview_get_selected_item(folderview);
2592         cm_return_if_fail(item != NULL);
2593         cm_return_if_fail(item->folder != NULL);
2594
2595         prefs_folder_item_open(item);
2596 }
2597
2598 static void folderview_recollapse_nodes(FolderView *folderview, GtkCMCTreeNode *node)
2599 {
2600         GSList *list = NULL;
2601         GSList *done = NULL;
2602         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
2603         
2604         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2605                 if (!gtkut_ctree_node_is_parent(GTK_CMCTREE_NODE(list->data), node)
2606                 &&  list->data != node) {
2607                         gtk_cmctree_collapse(ctree, GTK_CMCTREE_NODE(list->data));
2608                         done = g_slist_append(done, GTK_CMCTREE_NODE(list->data));
2609                 }
2610         }
2611         for (list = done; list != NULL; list = g_slist_next(list)) {
2612                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2613                                                                  list->data);
2614         }
2615         g_slist_free(done);
2616 }
2617
2618 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2619                             FolderItem *to_folder, gboolean copy)
2620 {
2621         FolderItem *new_folder = NULL;
2622         gchar *buf;
2623         gint status;
2624
2625         cm_return_if_fail(folderview != NULL);
2626         cm_return_if_fail(from_folder != NULL);
2627         cm_return_if_fail(to_folder != NULL);
2628
2629         if (prefs_common.warn_dnd) {
2630                 buf = g_strdup_printf(copy ? _("Do you really want to copy folder '%s' in '%s'?"):
2631                                              _("Do you really want to make folder '%s' a subfolder of '%s'?"), 
2632                                         from_folder->name, to_folder->name);
2633                 status = alertpanel_full(copy ? _("Copy folder"):_("Move folder"), buf,
2634                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, TRUE,
2635                                          NULL, ALERT_QUESTION, G_ALERTDEFAULT);
2636                 g_free(buf);
2637
2638                 if ((status & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
2639                         return;
2640                 else if (status & G_ALERTDISABLE)
2641                         prefs_common.warn_dnd = FALSE;
2642         }
2643
2644         buf = g_strdup_printf(copy ? _("Copying %s to %s..."):_("Moving %s to %s..."), 
2645                                 from_folder->name, to_folder->name);
2646         STATUSBAR_PUSH(folderview->mainwin, buf);
2647         g_free(buf);
2648         summary_clear_all(folderview->summaryview);
2649         folderview->opened = NULL;
2650         folderview->selected = NULL;
2651         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2652         inc_lock();
2653         main_window_cursor_wait(folderview->mainwin);
2654
2655         statusbar_verbosity_set(FALSE);
2656         folder_item_update_freeze();
2657         gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
2658         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder, copy)) == F_MOVE_OK) {
2659                 statusbar_verbosity_set(FALSE);
2660                 main_window_cursor_normal(folderview->mainwin);
2661                 STATUSBAR_POP(folderview->mainwin);
2662                 folder_item_update_thaw();
2663                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2664
2665                 folderview_sort_folders(folderview, 
2666                         gtk_cmctree_find_by_row_data(GTK_CMCTREE(folderview->ctree), 
2667                                 NULL, to_folder), new_folder->folder);
2668                 folderview_select(folderview, new_folder);
2669                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2670         } else {
2671                 statusbar_verbosity_set(FALSE);         
2672                 main_window_cursor_normal(folderview->mainwin);
2673                 STATUSBAR_POP(folderview->mainwin);
2674                 gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2675                 folder_item_update_thaw();
2676                 switch (status) {
2677                 case F_MOVE_FAILED_DEST_IS_PARENT:
2678                         alertpanel_error(_("Source and destination are the same."));
2679                         break;
2680                 case F_MOVE_FAILED_DEST_IS_CHILD:
2681                         alertpanel_error(copy ? _("Can't copy a folder to one of its children."):
2682                                                 _("Can't move a folder to one of its children."));
2683                         break;
2684                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2685                         alertpanel_error(_("A folder cannot be moved between different mailboxes."));
2686                         break;
2687                 default:
2688                         alertpanel_error(copy ? _("Copy failed!"):_("Move failed!"));
2689                         break;
2690                 }
2691         }       
2692         inc_unlock();           
2693         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2694 }
2695
2696 static gint folderview_clist_compare(GtkCMCList *clist,
2697                                      gconstpointer ptr1, gconstpointer ptr2)
2698 {
2699         FolderItem *item1 = ((GtkCMCListRow *)ptr1)->data;
2700         FolderItem *item2 = ((GtkCMCListRow *)ptr2)->data;
2701
2702         if (item1->order > 0 && item2->order > 0)  // if we have an order item, use it
2703         {
2704                 return item1->order - item2->order;
2705         }
2706
2707         // if only one folder has an order it comes first
2708         if (item1->order > 0)
2709         {
2710                 return -1;
2711         }
2712         if (item2->order > 0)
2713         {
2714                 return 1;
2715         }
2716
2717         if (!item1->name)
2718                 return (item2->name != NULL);
2719         if (!item2->name)
2720                 return -1;
2721
2722         return g_utf8_collate(item1->name, item2->name);
2723 }
2724
2725 static void folderview_processing_cb(GtkAction *action, gpointer data)
2726 {
2727         FolderView *folderview = (FolderView *)data;
2728         FolderItem *item;
2729         gchar *id, *title;
2730
2731         if (!folderview->selected) return;
2732
2733         item = folderview_get_selected_item(folderview);
2734         cm_return_if_fail(item != NULL);
2735         cm_return_if_fail(item->folder != NULL);
2736
2737         id = folder_item_get_identifier(item);
2738         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2739         g_free (id);
2740
2741         prefs_filtering_open(&item->prefs->processing, title,
2742                         MANUAL_ANCHOR_PROCESSING, NULL, NULL, FALSE);
2743         g_free (title);
2744 }
2745
2746 void folderview_set_target_folder_color(gint color_op) 
2747 {
2748         gint firstone = 1;
2749         GList *list;
2750         FolderView *folderview;
2751
2752         for (list = folderview_list; list != NULL; list = list->next) {
2753                 folderview = (FolderView *)list->data;
2754                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2755                 if (firstone) {
2756                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2757                                 folderview->color_op;
2758                         firstone = 0;
2759                 }
2760         }
2761 }
2762
2763 static gchar *last_smallfont = NULL;
2764 static gchar *last_normalfont = NULL;
2765 static gchar *last_boldfont = NULL;
2766 static gboolean last_derive = 0;
2767
2768 void folderview_reinit_fonts(FolderView *folderview)
2769 {
2770         /* force reinit */
2771         g_free(last_smallfont);
2772         last_smallfont = NULL;
2773         g_free(last_normalfont);
2774         last_normalfont = NULL;
2775         g_free(last_boldfont);
2776         last_boldfont = NULL;
2777 }
2778
2779 void folderview_reflect_prefs(void)
2780 {
2781         gboolean update_font = FALSE;
2782         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2783         FolderItem *item = folderview_get_selected_item(folderview);
2784         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2785                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2786         gint height = gtk_adjustment_get_value(pos);
2787
2788         if (!last_smallfont || strcmp(last_smallfont, SMALL_FONT) ||
2789                         !last_normalfont || strcmp(last_normalfont, NORMAL_FONT) ||
2790                         !last_boldfont || strcmp(last_boldfont, BOLD_FONT) ||
2791                         last_derive != prefs_common.derive_from_normal_font)
2792                 update_font = TRUE;
2793
2794         if (!update_font)
2795                 return;
2796
2797         g_free(last_smallfont);
2798         last_smallfont = g_strdup(SMALL_FONT);
2799         g_free(last_normalfont);
2800         last_normalfont = g_strdup(NORMAL_FONT);
2801         g_free(last_boldfont);
2802         last_boldfont = g_strdup(BOLD_FONT);
2803         last_derive = prefs_common.derive_from_normal_font;
2804
2805 #define STYLE_FREE(s)                   \
2806         if (s != NULL) {                \
2807                 g_object_unref(s);      \
2808                 s = NULL;               \
2809         }
2810
2811         STYLE_FREE(normal_style);
2812         STYLE_FREE(normal_color_style);
2813         STYLE_FREE(bold_style);
2814         STYLE_FREE(bold_color_style);
2815         STYLE_FREE(bold_tgtfold_style);
2816
2817 #undef STYLE_FREE
2818
2819         folderview_init(folderview);
2820         gtk_cmclist_freeze(GTK_CMCLIST(folderview->ctree));
2821         folderview_column_set_titles(folderview);
2822         folderview_set_all();
2823
2824         g_signal_handlers_block_by_func
2825                 (G_OBJECT(folderview->ctree),
2826                  G_CALLBACK(folderview_selected), folderview);
2827
2828         if (item) {
2829                 GtkCMCTreeNode *node = gtk_cmctree_find_by_row_data(
2830                         GTK_CMCTREE(folderview->ctree), NULL, item);
2831
2832                 folderview_select(folderview, item);
2833                 folderview->open_folder = FALSE;
2834                 folderview->selected = node;
2835         }
2836
2837         g_signal_handlers_unblock_by_func
2838                 (G_OBJECT(folderview->ctree),
2839                  G_CALLBACK(folderview_selected), folderview);
2840
2841         pos = gtk_scrolled_window_get_vadjustment(
2842                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2843         gtk_adjustment_set_value(pos, height);
2844         gtk_adjustment_changed(pos);
2845         gtk_cmclist_thaw(GTK_CMCLIST(folderview->ctree));
2846 }
2847
2848 static void drag_state_stop(FolderView *folderview)
2849 {
2850         if (folderview->drag_timer_id)
2851                 g_source_remove(folderview->drag_timer_id);
2852         folderview->drag_timer_id = 0;
2853         folderview->drag_node = NULL;
2854 }
2855
2856 static gboolean folderview_defer_expand(FolderView *folderview)
2857 {
2858         if (folderview->drag_node) {
2859                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2860                 if (folderview->drag_item->collapsed) {
2861                         gtk_cmctree_expand(GTK_CMCTREE(folderview->ctree), folderview->drag_node);
2862                         folderview->nodes_to_recollapse = g_slist_append
2863                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2864                 }
2865         }
2866         folderview->drag_item  = NULL;
2867         folderview->drag_timer_id = 0;
2868         return FALSE;
2869 }
2870
2871 static void drag_state_start(FolderView *folderview, GtkCMCTreeNode *node, FolderItem *item)
2872 {
2873         /* the idea is that we call drag_state_start() whenever we want expansion to
2874          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2875          * we need to call drag_state_stop() */
2876         drag_state_stop(folderview);
2877         /* request expansion */
2878         if (0 != (folderview->drag_timer_id = g_timeout_add
2879                         (prefs_common.hover_timeout, 
2880                          (GSourceFunc)folderview_defer_expand,
2881                          folderview))) {
2882                 folderview->drag_node = node;
2883                 folderview->drag_item = item;
2884         }                        
2885 }
2886 #ifndef GENERIC_UMPC
2887 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2888                                   FolderView       *folderview)
2889 {
2890         GdkDragContext *context;
2891
2892         cm_return_if_fail(folderview != NULL);
2893         if (folderview->selected == NULL) return;
2894         if (folderview->nodes_to_recollapse) 
2895                 g_slist_free(folderview->nodes_to_recollapse);
2896         folderview->nodes_to_recollapse = NULL;
2897         context = gtk_drag_begin(widget, folderview->target_list,
2898                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2899         gtk_drag_set_icon_default(context);
2900 }
2901 #endif
2902 static void folderview_drag_data_get(GtkWidget        *widget,
2903                                      GdkDragContext   *drag_context,
2904                                      GtkSelectionData *selection_data,
2905                                      guint             info,
2906                                      guint             time,
2907                                      FolderView       *folderview)
2908 {
2909         FolderItem *item;
2910         GList *sel;
2911         gchar *source = NULL;
2912         if (info == TARGET_DUMMY) {
2913                 sel = GTK_CMCLIST(folderview->ctree)->selection;
2914                 if (!sel)
2915                         return;
2916
2917                 item = gtk_cmctree_node_get_row_data
2918                         (GTK_CMCTREE(folderview->ctree),
2919                          GTK_CMCTREE_NODE(sel->data));
2920                 if (item) {
2921                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2922                         gtk_selection_data_set(selection_data,
2923                                                gtk_selection_data_get_target(selection_data), 8,
2924                                                source, strlen(source));
2925                 }
2926         } else {
2927                 g_warning("unknown info %d", info);
2928         }
2929 }
2930
2931 static gboolean folderview_update_folder(gpointer source, gpointer userdata)
2932 {
2933         FolderUpdateData *hookdata;
2934         FolderView *folderview;
2935         GtkWidget *ctree;
2936
2937         hookdata = source;
2938         folderview = (FolderView *) userdata;   
2939         cm_return_val_if_fail(hookdata != NULL, FALSE);
2940         cm_return_val_if_fail(folderview != NULL, FALSE);
2941
2942         ctree = folderview->ctree;
2943         cm_return_val_if_fail(ctree != NULL, FALSE);
2944
2945         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2946                 folderview_create_folder_node(folderview, hookdata->item);
2947         else if (hookdata->update_flags & FOLDER_RENAME_FOLDERITEM) {
2948                 GtkCMCTreeNode *node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree),
2949                                 NULL, folder_item_parent(hookdata->item));
2950                 folderview_sort_folders(folderview, node, hookdata->folder);
2951         } else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2952                 GtkCMCTreeNode *node;
2953
2954                 node = gtk_cmctree_find_by_row_data(GTK_CMCTREE(ctree), NULL, hookdata->item);
2955                 if (node != NULL) {
2956                         gtk_cmctree_remove_node(GTK_CMCTREE(ctree), node);
2957                         if (folderview->selected == node)
2958                                 folderview->selected = NULL;
2959                         if (folderview->opened == node)
2960                                 folderview->opened = NULL;
2961                 }
2962         } else if (hookdata->update_flags & FOLDER_MOVE_FOLDERITEM) {
2963                 /* do nothing, it's done by the ADD and REMOVE) */
2964         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2965                 folderview_set(folderview);
2966
2967         return FALSE;
2968 }
2969
2970 static gboolean folderview_dnd_scroll_cb(gpointer data)
2971 {
2972         FolderView *folderview = (FolderView *)data;
2973         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2974                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2975         gint new_val = (int)gtk_adjustment_get_value(pos) + folderview->scroll_value;
2976         gint max = (int)gtk_adjustment_get_upper(pos) -
2977                (int)gtk_adjustment_get_page_size(pos);
2978
2979         if (folderview->scroll_value == 0) {
2980                 folderview->scroll_timeout_id = 0;
2981                 return FALSE;
2982         }
2983
2984         if (folderview->scroll_value > 0 && new_val > max) {
2985                 new_val = max;
2986         } else if (folderview->scroll_value < 0 && new_val < 0) {
2987                 new_val = 0;
2988         }
2989         gtk_adjustment_set_value(pos, new_val);
2990         
2991         return TRUE;
2992 }
2993
2994 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2995                                           GdkDragContext *context,
2996                                           gint            x,
2997                                           gint            y,
2998                                           guint           time,
2999                                           FolderView     *folderview)
3000 {
3001         gint row, column;
3002         FolderItem *item = NULL, *src_item = NULL;
3003         GtkCMCTreeNode *node = NULL;
3004         gboolean acceptable = FALSE;
3005         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
3006                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
3007         int height = (int)gtk_adjustment_get_page_size(pos);
3008         int total_height = (int)gtk_adjustment_get_upper(pos);
3009         int vpos = (int)gtk_adjustment_get_value(pos);
3010         int offset = prefs_common.show_col_headers ? 24:0;
3011         int dist;
3012
3013         if (gtk_cmclist_get_selection_info
3014                 (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column)) {
3015                 GtkWidget *srcwidget;
3016
3017                 if (y > height - (48 - offset) && height + vpos < total_height) {
3018                         dist = -(height - (48 - offset) - y);
3019                         folderview->scroll_value = 1.41f * (1+(dist / 6));
3020                 } else if (y < 72 - (24 - offset) && y >= 0) {
3021                         dist = 72 - (24 - offset) - y;
3022                         folderview->scroll_value = -1.41f * (1+(dist / 6));
3023                 } else {
3024                         folderview->scroll_value = 0;
3025                 }
3026                 if (folderview->scroll_value != 0 && folderview->scroll_timeout_id == 0) {
3027                         folderview->scroll_timeout_id = 
3028                                 g_timeout_add(30, folderview_dnd_scroll_cb,
3029                                               folderview);
3030                 }
3031
3032                 node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3033                 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3034                 src_item = folderview->summaryview->folder_item;
3035
3036                 srcwidget = gtk_drag_get_source_widget(context);
3037                 if (srcwidget == summary_get_main_widget(folderview->summaryview)) {
3038                         /* comes from summaryview */
3039                         /* we are copying messages, so only accept folder items that are not
3040                            the source item, are no root items and can copy messages */
3041                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
3042                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL &&
3043                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
3044                                 acceptable = TRUE;
3045                 } else if (srcwidget == folderview->ctree) {
3046                         /* comes from folderview */
3047                         /* we are moving folder items, only accept folders that are not
3048                            the source items and can copy messages and create folder items */
3049                         if (item && item->folder && src_item && src_item != item &&
3050                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
3051                             FOLDER_CLASS(item->folder)->create_folder != NULL &&
3052                             ((FOLDER_TYPE(item->folder) != F_UNKNOWN &&  FOLDER_TYPE(src_item->folder) != F_UNKNOWN)
3053                              || item->folder == src_item->folder))
3054                                 acceptable = TRUE;
3055                 } else {
3056                         /* comes from another app */
3057                         /* we are adding messages, so only accept folder items that are 
3058                            no root items and can copy messages */
3059                         if (item && item->folder && folder_item_parent(item) != NULL
3060                             && FOLDER_CLASS(item->folder)->add_msg != NULL &&
3061                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
3062                                 acceptable = TRUE;
3063                 }
3064         }
3065
3066         if (acceptable || (src_item && src_item == item))
3067                 drag_state_start(folderview, node, item);
3068         
3069         if (acceptable) {
3070                 g_signal_handlers_block_by_func
3071                         (G_OBJECT(widget),
3072                          G_CALLBACK(folderview_selected), folderview);
3073                 gtk_cmctree_select(GTK_CMCTREE(widget), node);
3074                 g_signal_handlers_unblock_by_func
3075                         (G_OBJECT(widget),
3076                          G_CALLBACK(folderview_selected), folderview);
3077                 gdk_drag_status(context, 
3078                                         (gdk_drag_context_get_actions(context) == GDK_ACTION_COPY ?
3079                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
3080         } else {
3081                 if (folderview->opened)
3082                         gtk_cmctree_select(GTK_CMCTREE(widget), folderview->opened);
3083                 gdk_drag_status(context, 0, time);
3084         }
3085
3086         return acceptable;
3087 }
3088
3089 static void folderview_drag_leave_cb(GtkWidget      *widget,
3090                                      GdkDragContext *context,
3091                                      guint           time,
3092                                      FolderView     *folderview)
3093 {
3094         drag_state_stop(folderview);
3095         folderview->scroll_value = 0;
3096         gtk_cmctree_select(GTK_CMCTREE(widget), folderview->opened);
3097 }
3098
3099 static void free_info (gpointer stuff, gpointer data)
3100 {
3101         g_free(stuff);
3102 }
3103
3104 void folderview_finish_dnd(const gchar *data, GdkDragContext *drag_context,
3105                            guint time, FolderItem *item)
3106 {
3107         GList *list, *tmp;
3108         GSList *msglist = NULL;
3109         list = uri_list_extract_filenames(data);
3110         if (!(item && item->folder && folder_item_parent(item) != NULL
3111                     && FOLDER_CLASS(item->folder)->add_msg != NULL))
3112         {
3113                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3114                 debug_print("item doesn't fit\n");                      
3115                 return;
3116         }       
3117         if (!list) {
3118                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3119                 debug_print("list is empty\n");                 
3120                 return;
3121         }
3122         for (tmp = list; tmp != NULL; tmp = tmp->next) {
3123                 MsgFileInfo *info = NULL;
3124
3125                 if (file_is_email((gchar *)tmp->data)) {
3126                         info = g_new0(MsgFileInfo, 1);
3127                         info->msginfo = NULL;
3128                         info->file = (gchar *)tmp->data;
3129                         msglist = g_slist_prepend(msglist, info);
3130                         debug_print("file is a mail\n");
3131                 } else {
3132                         debug_print("file isn't a mail\n");
3133                 }
3134         }
3135         if (msglist) {
3136                 msglist = g_slist_reverse(msglist);
3137                 folder_item_add_msgs(item, msglist, FALSE);
3138                 g_slist_foreach(msglist, free_info, NULL);
3139                 g_slist_free(msglist);
3140                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
3141         } else {
3142                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3143         }
3144         list_free_strings(list);
3145         g_list_free(list);
3146 }
3147
3148 static void folderview_drag_received_cb(GtkWidget        *widget,
3149                                         GdkDragContext   *drag_context,
3150                                         gint              x,
3151                                         gint              y,
3152                                         GtkSelectionData *data,
3153                                         guint             info,
3154                                         guint             time,
3155                                         FolderView       *folderview)
3156 {
3157         gint row, column;
3158         FolderItem *item = NULL, *src_item;
3159         GtkCMCTreeNode *node;
3160         int offset = prefs_common.show_col_headers ? 24:0;
3161
3162         folderview->scroll_value = 0;
3163
3164         if (info == TARGET_DUMMY) {
3165                 drag_state_stop(folderview);
3166                 const gchar *ddata = (const gchar *)gtk_selection_data_get_data(data);
3167                 if ((gchar *)strstr(ddata, "FROM_OTHER_FOLDER") != ddata) {
3168                         /* comes from summaryview */
3169                         if (gtk_cmclist_get_selection_info
3170                                 (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0)
3171                                 return;
3172
3173                         node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3174                         item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3175                         src_item = folderview->summaryview->folder_item;
3176
3177                         if (item->no_select) {
3178                                 alertpanel_error(_("The destination folder can only be used to "
3179                                                    "store subfolders."));
3180                                 return;
3181                         }
3182                         /* re-check (due to acceptable possibly set for folder moves */
3183                         if (!(item && item->folder && item->path && !item->no_select && 
3184                               src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
3185                                 return;
3186                         }
3187                         if (item && src_item) {
3188                                 switch (gdk_drag_context_get_selected_action(drag_context)) {
3189                                 case GDK_ACTION_COPY:
3190                                         summary_copy_selected_to(folderview->summaryview, item);
3191                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
3192                                         break;
3193                                 case GDK_ACTION_MOVE:
3194                                 case GDK_ACTION_DEFAULT:
3195                                 default:
3196                                         if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
3197                                                 summary_copy_selected_to(folderview->summaryview, item);
3198                                         else
3199                                                 summary_move_selected_to(folderview->summaryview, item);
3200                                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3201                                 }
3202                         } else
3203                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3204                 } else {
3205                         /* comes from folderview */
3206                         char *source;
3207                         gboolean folder_is_normal = TRUE;
3208                         gboolean copy = (GDK_ACTION_COPY ==
3209                                 gdk_drag_context_get_selected_action(drag_context));
3210
3211                         source = (char *)gtk_selection_data_get_data(data) + 17;
3212                         if (gtk_cmclist_get_selection_info
3213                             (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0
3214                             || *source == 0) {
3215                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3216                                 return;
3217                         }
3218                         node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3219                         item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3220                         src_item = folder_find_item_from_identifier(source);
3221
3222                         folder_is_normal = 
3223                                 src_item != NULL &&
3224                                 src_item->stype == F_NORMAL &&
3225                                 !folder_has_parent_of_type(src_item, F_OUTBOX) &&
3226                                 !folder_has_parent_of_type(src_item, F_DRAFT) &&
3227                                 !folder_has_parent_of_type(src_item, F_QUEUE) &&
3228                                 !folder_has_parent_of_type(src_item, F_TRASH);
3229                         if (!item || !src_item || !folder_is_normal) {
3230                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3231                                 return;
3232                         }
3233
3234                         folderview_move_folder(folderview, src_item, item, copy);
3235                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3236                 }
3237                 folderview->nodes_to_recollapse = NULL;
3238         } else if (info == TARGET_MAIL_URI_LIST) {
3239                 if (gtk_cmclist_get_selection_info
3240                         (GTK_CMCLIST(widget), x - offset, y - offset, &row, &column) == 0)
3241                         return;
3242
3243                 node = gtk_cmctree_node_nth(GTK_CMCTREE(widget), row);
3244                 if (!node) {
3245                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
3246                         debug_print("no node\n");               
3247                         return;
3248                 }
3249                 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(widget), node);
3250                 if (!item) {
3251                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3252                         debug_print("no item\n");
3253                         return;
3254                 }
3255                 folderview_finish_dnd(gtk_selection_data_get_data(data),
3256                         drag_context, time, item);
3257         }
3258 }
3259
3260 static void folderview_drag_end_cb(GtkWidget        *widget, 
3261                                    GdkDragContext   *drag_context,
3262                                    FolderView       *folderview)
3263 {