2005-02-03 [paul] 1.0.0cvs25.3
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkwidget.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkctree.h>
27 #include <gtk/gtkcontainer.h>
28 #include <gtk/gtkclist.h>
29 #include <gtk/gtkstyle.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkstatusbar.h>
33 #include <gtk/gtkmenu.h>
34 #include <gtk/gtkmenuitem.h>
35 #include <gtk/gtkitemfactory.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "summary_search.h"
46 #include "inputdialog.h"
47 #include "manage_window.h"
48 #include "alertpanel.h"
49 #include "menu.h"
50 #include "stock_pixmap.h"
51 #include "procmsg.h"
52 #include "utils.h"
53 #include "gtkutils.h"
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56 #include "prefs_filtering.h"
57 #include "prefs_folder_item.h"
58 #include "account.h"
59 #include "folder.h"
60 #include "foldersel.h"
61 #include "inc.h"
62 #include "statusbar.h"
63 #include "hooks.h"
64 #include "folderutils.h"
65 #include "partial_download.h"
66
67 typedef enum
68 {
69         COL_FOLDER      = 0,
70         COL_NEW         = 1,
71         COL_UNREAD      = 2,
72         COL_TOTAL       = 3
73 } FolderColumnPos;
74
75 #define N_FOLDER_COLS           4
76 #define COL_FOLDER_WIDTH        150
77 #define COL_NUM_WIDTH           32
78
79 static GList *folderview_list = NULL;
80
81 static GtkStyle *normal_style;
82 static GtkStyle *normal_color_style;
83 static GtkStyle *bold_style;
84 static GtkStyle *bold_color_style;
85 static GtkStyle *bold_tgtfold_style;
86
87 static GdkBitmap *inboxxpm;
88 static GdkBitmap *inboxxpmmask;
89 static GdkPixmap *inboxhrmxpm;
90 static GdkBitmap *inboxhrmxpmmask;
91 static GdkPixmap *inboxopenxpm;
92 static GdkBitmap *inboxopenxpmmask;
93 static GdkPixmap *inboxopenhrmxpm;
94 static GdkBitmap *inboxopenhrmxpmmask;
95 static GdkPixmap *outboxxpm;
96 static GdkBitmap *outboxxpmmask;
97 static GdkPixmap *outboxhrmxpm;
98 static GdkBitmap *outboxhrmxpmmask;
99 static GdkPixmap *outboxopenxpm;
100 static GdkBitmap *outboxopenxpmmask;
101 static GdkPixmap *outboxopenhrmxpm;
102 static GdkBitmap *outboxopenhrmxpmmask;
103 static GdkPixmap *folderxpm;
104 static GdkBitmap *folderxpmmask;
105 static GdkPixmap *folderhrmxpm;
106 static GdkBitmap *folderhrmxpmmask;
107 static GdkPixmap *folderopenxpm;
108 static GdkBitmap *folderopenxpmmask;
109 static GdkPixmap *folderopenhrmxpm;
110 static GdkBitmap *folderopenhrmxpmmask;
111 static GdkPixmap *trashopenxpm;
112 static GdkBitmap *trashopenxpmmask;
113 static GdkPixmap *trashopenhrmxpm;
114 static GdkBitmap *trashopenhrmxpmmask;
115 static GdkPixmap *trashxpm;
116 static GdkBitmap *trashxpmmask;
117 static GdkPixmap *trashhrmxpm;
118 static GdkBitmap *trashhrmxpmmask;
119 static GdkPixmap *queuexpm;
120 static GdkBitmap *queuexpmmask;
121 static GdkPixmap *queuehrmxpm;
122 static GdkBitmap *queuehrmxpmmask;
123 static GdkPixmap *queueopenxpm;
124 static GdkBitmap *queueopenxpmmask;
125 static GdkPixmap *queueopenhrmxpm;
126 static GdkBitmap *queueopenhrmxpmmask;
127 static GdkPixmap *newxpm;
128 static GdkBitmap *newxpmmask;
129 static GdkPixmap *unreadxpm;
130 static GdkBitmap *unreadxpmmask;
131 static GdkPixmap *readxpm;
132 static GdkBitmap *readxpmmask;
133 static GdkPixmap *draftsxpm;
134 static GdkBitmap *draftsxpmmask;
135 static GdkPixmap *draftsopenxpm;
136 static GdkBitmap *draftsopenxpmmask;
137
138 static void folderview_select_node       (FolderView    *folderview,
139                                           GtkCTreeNode  *node);
140 static void folderview_set_folders       (FolderView    *folderview);
141 static void folderview_sort_folders      (FolderView    *folderview,
142                                           GtkCTreeNode  *root,
143                                           Folder        *folder);
144 static void folderview_append_folder     (FolderView    *folderview,
145                                           Folder        *folder);
146 static void folderview_update_node       (FolderView    *folderview,
147                                           GtkCTreeNode  *node);
148
149 static gint folderview_clist_compare    (GtkCList       *clist,
150                                          gconstpointer   ptr1,
151                                          gconstpointer   ptr2);
152
153 /* callback functions */
154 static gboolean folderview_button_pressed       (GtkWidget      *ctree,
155                                                  GdkEventButton *event,
156                                                  FolderView     *folderview);
157 static gboolean folderview_button_released      (GtkWidget      *ctree,
158                                                  GdkEventButton *event,
159                                                  FolderView     *folderview);
160 static gboolean folderview_key_pressed  (GtkWidget      *widget,
161                                          GdkEventKey    *event,
162                                          FolderView     *folderview);
163 static void folderview_selected         (GtkCTree       *ctree,
164                                          GtkCTreeNode   *row,
165                                          gint            column,
166                                          FolderView     *folderview);
167 static void folderview_tree_expanded    (GtkCTree       *ctree,
168                                          GtkCTreeNode   *node,
169                                          FolderView     *folderview);
170 static void folderview_tree_collapsed   (GtkCTree       *ctree,
171                                          GtkCTreeNode   *node,
172                                          FolderView     *folderview);
173 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
174                                          FolderView     *folderview);
175 static void folderview_col_resized      (GtkCList       *clist,
176                                          gint            column,
177                                          gint            width,
178                                          FolderView     *folderview);
179
180 static void mark_all_read_cb            (FolderView    *folderview,
181                                          guint           action,
182                                          GtkWidget      *widget);
183
184 static void folderview_empty_trash_cb   (FolderView     *folderview,
185                                          guint           action,
186                                          GtkWidget      *widget);
187
188 static void folderview_search_cb        (FolderView     *folderview,
189                                          guint           action,
190                                          GtkWidget      *widget);
191
192 static void folderview_property_cb      (FolderView     *folderview,
193                                          guint           action,
194                                          GtkWidget      *widget);
195
196 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
197                                           GdkDragContext *context,
198                                           gint            x,
199                                           gint            y,
200                                           guint           time,
201                                           FolderView     *folderview);
202 static void folderview_drag_leave_cb     (GtkWidget        *widget,
203                                           GdkDragContext   *context,
204                                           guint             time,
205                                           FolderView       *folderview);
206 static void folderview_drag_received_cb  (GtkWidget        *widget,
207                                           GdkDragContext   *drag_context,
208                                           gint              x,
209                                           gint              y,
210                                           GtkSelectionData *data,
211                                           guint             info,
212                                           guint             time,
213                                           FolderView       *folderview);
214 static void folderview_start_drag        (GtkWidget *widget, gint button, GdkEvent *event,
215                                           FolderView       *folderview);
216 static void folderview_drag_data_get     (GtkWidget        *widget,
217                                           GdkDragContext   *drag_context,
218                                           GtkSelectionData *selection_data,
219                                           guint             info,
220                                           guint             time,
221                                           FolderView       *folderview);
222 static void folderview_drag_end_cb       (GtkWidget        *widget,
223                                           GdkDragContext   *drag_context,
224                                           FolderView       *folderview);
225
226 void folderview_create_folder_node       (FolderView       *folderview, 
227                                           FolderItem       *item);
228 gboolean folderview_update_folder        (gpointer          source,
229                                           gpointer          userdata);
230 gboolean folderview_update_item_claws    (gpointer          source,
231                                           gpointer          data);
232 static void folderview_processing_cb(FolderView *folderview, guint action,
233                                      GtkWidget *widget);
234
235 GHashTable *folderview_popups;
236
237 static GtkItemFactoryEntry folderview_common_popup_entries[] =
238 {
239         {N_("/Mark all _read"),         NULL, mark_all_read_cb, 0, NULL},
240         {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
241         {N_("/_Properties..."),         NULL, folderview_property_cb, 0, NULL},
242         {N_("/Pr_ocessing..."),         NULL, folderview_processing_cb, 0, NULL},
243 };
244
245 static GtkItemFactoryEntry folder_view_trash_popup_entries[] = {
246         {N_("/------"),                 NULL, NULL, 0, "<Separator>"},
247         {N_("/Empty trash..."),         NULL, folderview_empty_trash_cb, 0, NULL},
248 };
249
250
251 GtkTargetEntry folderview_drag_types[] =
252 {
253         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
254 };
255
256 void folderview_initialize(void)
257 {
258         FolderViewPopup *fpopup;
259         guint i, n_entries;
260         GSList *entries = NULL;
261
262         fpopup = g_new0(FolderViewPopup, 1);
263
264         n_entries = sizeof(folderview_common_popup_entries) /
265                 sizeof(folderview_common_popup_entries[0]);
266         for (i = 0; i < n_entries; i++)
267                 entries = g_slist_append(entries, &folderview_common_popup_entries[i]);
268
269         fpopup->klass = "common";
270         fpopup->path = "<CommonFolder>";
271         fpopup->entries = entries;
272         fpopup->set_sensitivity = NULL;
273
274         folderview_popups = g_hash_table_new(g_str_hash, g_str_equal);
275         g_hash_table_insert(folderview_popups, "common", fpopup);
276 }
277
278 static GtkItemFactory *create_ifactory(FolderView *folderview, FolderViewPopup *fpopup)
279 {
280         GSList *entries;
281         GtkItemFactory *factory;
282         FolderViewPopup *fpopup_common;
283         GtkWidget *popup;
284
285         factory = gtk_item_factory_new(GTK_TYPE_MENU, fpopup->path, NULL);
286         gtk_item_factory_set_translate_func(factory, menu_translate,
287                                             NULL, NULL);
288
289         for (entries = fpopup->entries; entries != NULL; entries = g_slist_next(entries))
290                 gtk_item_factory_create_item(factory, entries->data, folderview, 1);
291
292         fpopup_common = g_hash_table_lookup(folderview_popups, "common");
293         if (fpopup_common != fpopup)
294                 for (entries = fpopup_common->entries; entries != NULL; entries = g_slist_next(entries))
295                         gtk_item_factory_create_item(factory, entries->data, folderview, 1);
296
297         popup = gtk_item_factory_get_widget(factory, fpopup->path);
298         g_signal_connect(G_OBJECT(popup), "selection_done",
299                          G_CALLBACK(folderview_popup_close),
300                          folderview);
301
302         return factory;
303 }
304
305 static void create_ifactories(gpointer key, gpointer value, gpointer data)
306 {
307         FolderView *folderview = data;
308         FolderViewPopup *fpopup = value;
309         GtkItemFactory *factory;
310
311         factory = create_ifactory(folderview, fpopup);
312         g_hash_table_insert(folderview->popups, fpopup->klass, factory);
313 }
314
315 FolderView *folderview_create(void)
316 {
317         FolderView *folderview;
318         GtkWidget *scrolledwin;
319         GtkWidget *ctree;
320         gchar *titles[N_FOLDER_COLS];
321         gint i;
322
323         debug_print("Creating folder view...\n");
324         folderview = g_new0(FolderView, 1);
325
326         titles[COL_FOLDER] = _("Folder");
327         titles[COL_NEW]    = _("New");
328         titles[COL_UNREAD] = _("Unread");
329         titles[COL_TOTAL]  = _("#");
330
331         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
332         gtk_scrolled_window_set_policy
333                 (GTK_SCROLLED_WINDOW(scrolledwin),
334                  GTK_POLICY_AUTOMATIC,
335                  prefs_common.folderview_vscrollbar_policy);
336         gtk_widget_set_size_request(scrolledwin,
337                              prefs_common.folderview_width,
338                              prefs_common.folderview_height);
339
340         ctree = gtk_sctree_new_with_titles(N_FOLDER_COLS, COL_FOLDER, titles);
341         
342         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
343         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
344         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_NEW,
345                                            GTK_JUSTIFY_RIGHT);
346         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_UNREAD,
347                                            GTK_JUSTIFY_RIGHT);
348         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_TOTAL,
349                                            GTK_JUSTIFY_RIGHT);
350         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_FOLDER,
351                                    prefs_common.folder_col_folder);
352         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_NEW,
353                                    prefs_common.folder_col_new);
354         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_UNREAD,        
355                                    prefs_common.folder_col_unread);
356         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_TOTAL,
357                                    prefs_common.folder_col_total);
358         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
359         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
360                                      GTK_CTREE_EXPANDER_SQUARE);
361         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
362         gtk_clist_set_compare_func(GTK_CLIST(ctree), folderview_clist_compare);
363
364         /* create popup factories */
365         folderview->popups = g_hash_table_new(g_str_hash, g_str_equal);
366         g_hash_table_foreach(folderview_popups, create_ifactories, folderview);
367
368         /* don't let title buttons take key focus */
369         for (i = 0; i < N_FOLDER_COLS; i++)
370                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
371                                        GTK_CAN_FOCUS);
372
373
374         g_signal_connect(G_OBJECT(ctree), "key_press_event",
375                          G_CALLBACK(folderview_key_pressed),
376                          folderview);
377         g_signal_connect(G_OBJECT(ctree), "button_press_event",
378                          G_CALLBACK(folderview_button_pressed),
379                          folderview);
380         g_signal_connect(G_OBJECT(ctree), "button_release_event",
381                          G_CALLBACK(folderview_button_released),
382                          folderview);
383         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
384                          G_CALLBACK(folderview_selected), folderview);
385         g_signal_connect(G_OBJECT(ctree), "start_drag",
386                          G_CALLBACK(folderview_start_drag), folderview);
387         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
388                          G_CALLBACK(folderview_drag_data_get),
389                          folderview);
390
391         g_signal_connect_after(G_OBJECT(ctree), "tree_expand",
392                                G_CALLBACK(folderview_tree_expanded),
393                                folderview);
394         g_signal_connect_after(G_OBJECT(ctree), "tree_collapse",
395                                G_CALLBACK(folderview_tree_collapsed),
396                                folderview);
397
398         g_signal_connect(G_OBJECT(ctree), "resize_column",
399                          G_CALLBACK(folderview_col_resized),
400                          folderview);
401
402         /* drop callback */
403         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
404                           summary_drag_types, 1,
405                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
406         g_signal_connect(G_OBJECT(ctree), "drag_motion",
407                          G_CALLBACK(folderview_drag_motion_cb),
408                          folderview);
409         g_signal_connect(G_OBJECT(ctree), "drag_leave",
410                          G_CALLBACK(folderview_drag_leave_cb),
411                          folderview);
412         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
413                          G_CALLBACK(folderview_drag_received_cb),
414                          folderview);
415         g_signal_connect(G_OBJECT(ctree), "drag_end",
416                          G_CALLBACK(folderview_drag_end_cb),
417                          folderview);
418
419         folderview->scrolledwin  = scrolledwin;
420         folderview->ctree        = ctree;
421
422         folderview->folder_update_callback_id =
423                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST, folderview_update_folder, (gpointer) folderview);
424         folderview->folder_item_update_callback_id =
425                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST, folderview_update_item_claws, (gpointer) folderview);
426
427         gtk_widget_show_all(scrolledwin);
428
429         folderview->target_list = gtk_target_list_new(folderview_drag_types, 1);
430         folderview_list = g_list_append(folderview_list, folderview);
431
432         return folderview;
433 }
434
435 void folderview_init(FolderView *folderview)
436 {
437         GtkWidget *ctree = folderview->ctree;
438         GtkWidget *label_new;
439         GtkWidget *label_unread;
440         GtkWidget *label_total;
441         GtkWidget *hbox_new;
442         GtkWidget *hbox_unread;
443         GtkWidget *hbox_total;
444                 
445         gtk_widget_realize(ctree);
446         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
447         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
448         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
449         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
450         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
451         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
452         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
453         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
454         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
455         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
456         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
457         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
458         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
459         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
460         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
461         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
462         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
463         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
464         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
465         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
466         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE, &draftsxpm, &draftsxpmmask);
467         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN, &draftsopenxpm, &draftsopenxpmmask);
468
469         /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
470          * instead text (text overflows making them unreadable and ugly) */
471         stock_pixmap_gdk(ctree, STOCK_PIXMAP_NEW,
472                          &newxpm, &newxpmmask);
473         stock_pixmap_gdk(ctree, STOCK_PIXMAP_UNREAD,
474                          &unreadxpm, &unreadxpmmask);
475         stock_pixmap_gdk(ctree, STOCK_PIXMAP_READ,
476                          &readxpm, &readxpmmask);
477                 
478         label_new = gtk_pixmap_new(newxpm, newxpmmask);
479         label_unread = gtk_pixmap_new(unreadxpm, unreadxpmmask);
480         label_total = gtk_pixmap_new(readxpm, readxpmmask);
481
482         hbox_new = gtk_hbox_new(FALSE, 4);
483         hbox_unread = gtk_hbox_new(FALSE, 4);
484         hbox_total = gtk_hbox_new(FALSE, 4);
485
486         /* left justified */
487         gtk_box_pack_start(GTK_BOX(hbox_new), label_new, TRUE, TRUE, 0);
488         gtk_misc_set_alignment (GTK_MISC (label_new), 1, 0.5);
489         gtk_box_pack_start(GTK_BOX(hbox_unread), label_unread, TRUE, TRUE, 0);
490         gtk_misc_set_alignment (GTK_MISC (label_unread), 1, 0.5);
491         gtk_box_pack_start(GTK_BOX(hbox_total), label_total, TRUE, TRUE, 0);
492         gtk_misc_set_alignment (GTK_MISC (label_total), 1, 0.5);
493
494         gtk_widget_show_all(hbox_new);
495         gtk_widget_show_all(hbox_unread);
496         gtk_widget_show_all(hbox_total);
497
498         gtk_clist_set_column_widget(GTK_CLIST(ctree),COL_NEW,hbox_new);
499         gtk_clist_set_column_widget(GTK_CLIST(ctree),COL_UNREAD,hbox_unread);
500         gtk_clist_set_column_widget(GTK_CLIST(ctree),COL_TOTAL,hbox_total);
501                         
502         if (!normal_style) {
503                 PangoFontDescription *font_desc;
504                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
505                 font_desc = pango_font_description_from_string(NORMAL_FONT);
506                 if (font_desc) {
507                         if (normal_style->font_desc)
508                                 pango_font_description_free
509                                         (normal_style->font_desc);
510                         normal_style->font_desc = font_desc;
511                 }
512                 normal_color_style = gtk_style_copy(normal_style);
513                 normal_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
514
515                 gtk_widget_set_style(ctree, normal_style);
516         }
517
518         if (!bold_style) {
519                 PangoFontDescription *font_desc;
520                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
521                 font_desc = pango_font_description_from_string(BOLD_FONT);
522                 if (font_desc) {
523                         if (bold_style->font_desc)
524                                 pango_font_description_free
525                                         (bold_style->font_desc);
526                         bold_style->font_desc = font_desc;
527                 }
528                 pango_font_description_set_weight
529                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
530                 bold_color_style = gtk_style_copy(bold_style);
531                 bold_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
532
533                 bold_tgtfold_style = gtk_style_copy(bold_style);
534                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
535         }
536 }
537
538 void folderview_set(FolderView *folderview)
539 {
540         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
541         MainWindow *mainwin = folderview->mainwin;
542
543         debug_print("Setting folder info...\n");
544         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
545
546         main_window_cursor_wait(mainwin);
547
548         folderview->selected = NULL;
549         folderview->opened = NULL;
550
551         gtk_clist_freeze(GTK_CLIST(ctree));
552         gtk_clist_clear(GTK_CLIST(ctree));
553         gtk_clist_thaw(GTK_CLIST(ctree));
554         gtk_clist_freeze(GTK_CLIST(ctree));
555
556         folderview_set_folders(folderview);
557
558         gtk_clist_thaw(GTK_CLIST(ctree));
559         main_window_cursor_normal(mainwin);
560         STATUSBAR_POP(mainwin);
561 }
562
563 void folderview_set_all(void)
564 {
565         GList *list;
566
567         for (list = folderview_list; list != NULL; list = list->next)
568                 folderview_set((FolderView *)list->data);
569 }
570
571 void folderview_select(FolderView *folderview, FolderItem *item)
572 {
573         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
574         GtkCTreeNode *node;
575         GtkCTreeNode *old_selected = folderview->selected;
576
577         if (!item) return;
578
579         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
580         if (node) folderview_select_node(folderview, node);
581
582         if (old_selected != node)
583                 folder_update_op_count();
584 }
585
586 static void mark_all_read_cb(FolderView *folderview, guint action,
587                              GtkWidget *widget)
588 {
589         FolderItem *item;
590
591         item = folderview_get_selected_item(folderview);
592         if (item == NULL)
593                 return;
594
595         folderutils_mark_all_read(item);
596 }
597
598 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
599 {
600         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
601
602         g_return_if_fail(node != NULL);
603
604         folderview->open_folder = TRUE;
605         gtkut_ctree_set_focus_row(ctree, node);
606         gtk_ctree_select(ctree, node);
607         if (folderview->summaryview->folder_item &&
608             folderview->summaryview->folder_item->total_msgs > 0)
609                 gtk_widget_grab_focus(folderview->summaryview->ctree);
610         else
611                 gtk_widget_grab_focus(folderview->ctree);
612
613         gtkut_ctree_expand_parent_all(ctree, node);
614 }
615
616 void folderview_unselect(FolderView *folderview)
617 {
618         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
619                 gtk_ctree_collapse
620                         (GTK_CTREE(folderview->ctree), folderview->opened);
621
622         folderview->selected = folderview->opened = NULL;
623 }
624
625 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
626                                                  GtkCTreeNode *node)
627 {
628         FolderItem *item;
629
630         if (node)
631                 node = gtkut_ctree_node_next(ctree, node);
632         else
633                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
634
635         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
636                 item = gtk_ctree_node_get_row_data(ctree, node);
637                 if (item && item->unread_msgs > 0 && item->stype != F_TRASH)
638                         return node;
639         }
640
641         return NULL;
642 }
643
644 void folderview_select_next_unread(FolderView *folderview)
645 {
646         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
647         GtkCTreeNode *node = NULL;
648
649         if ((node = folderview_find_next_unread(ctree, folderview->opened))
650             != NULL) {
651                 folderview_select_node(folderview, node);
652                 return;
653         }
654
655         if (!folderview->opened ||
656             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
657                 return;
658         /* search again from the first node */
659         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
660                 folderview_select_node(folderview, node);
661 }
662
663 FolderItem *folderview_get_selected_item(FolderView *folderview)
664 {
665         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
666
667         if (!folderview->selected) return NULL;
668         return gtk_ctree_node_get_row_data(ctree, folderview->selected);
669 }
670
671 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
672 {
673         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
674         static GtkCTreeNode *prev_row = NULL;
675         FolderItem *item;
676         gint new, unread, total;
677         gchar *new_str, *unread_str, *total_str;
678
679         if (!row) return;
680
681         item = gtk_ctree_node_get_row_data(ctree, row);
682         if (!item) return;
683
684         gtk_ctree_node_get_text(ctree, row, COL_NEW, &new_str);
685         gtk_ctree_node_get_text(ctree, row, COL_UNREAD, &unread_str);
686         gtk_ctree_node_get_text(ctree, row, COL_TOTAL, &total_str);
687         new = atoi(new_str);
688         unread = atoi(unread_str);
689         total = atoi(total_str);
690
691         prev_row = row;
692
693         folderview_update_node(folderview, row);
694 }
695
696 void folderview_append_item(FolderItem *item)
697 {
698         GList *list;
699
700         g_return_if_fail(item != NULL);
701         g_return_if_fail(item->folder != NULL);
702         if (folder_item_parent(item)) return;
703
704         for (list = folderview_list; list != NULL; list = list->next) {
705                 FolderView *folderview = (FolderView *)list->data;
706                 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
707                 GtkCTreeNode *node, *child;
708
709                 node = gtk_ctree_find_by_row_data(ctree, NULL, 
710                                                   folder_item_parent(item));
711                 if (node) {
712                         child = gtk_ctree_find_by_row_data(ctree, node, item);
713                         if (!child) {
714                                 gchar *text[N_FOLDER_COLS] =
715                                         {NULL, "0", "0", "0"};
716
717                                 gtk_clist_freeze(GTK_CLIST(ctree));
718
719                                 text[COL_FOLDER] = item->name;
720                                 child = gtk_ctree_insert_node
721                                         (ctree, node, NULL, text,
722                                          FOLDER_SPACING,
723                                          folderxpm, folderxpmmask,
724                                          folderopenxpm, folderopenxpmmask,
725                                          FALSE, FALSE);
726                                 gtk_ctree_node_set_row_data(ctree, child, item);
727                                 gtk_ctree_expand(ctree, node);
728                                 folderview_update_node(folderview, child);
729                                 folderview_sort_folders(folderview, node,
730                                                         item->folder);
731
732                                 gtk_clist_thaw(GTK_CLIST(ctree));
733                         }
734                 }
735         }
736 }
737
738 static void folderview_set_folders(FolderView *folderview)
739 {
740         GList *list;
741
742         list = folder_get_list();
743
744         for (; list != NULL; list = list->next)
745                 folderview_append_folder(folderview, FOLDER(list->data));
746 }
747
748 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
749                                       gpointer data)
750 {
751         GList *list;
752
753         for (list = folderview_list; list != NULL; list = list->next) {
754                 FolderView *folderview = (FolderView *)list->data;
755                 MainWindow *mainwin = folderview->mainwin;
756                 gchar *str;
757
758                 if (item->path)
759                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
760                                               item->folder->name, G_DIR_SEPARATOR,
761                                               item->path);
762                 else
763                         str = g_strdup_printf(_("Scanning folder %s ..."),
764                                               item->folder->name);
765
766                 STATUSBAR_PUSH(mainwin, str);
767                 STATUSBAR_POP(mainwin);
768                 g_free(str);
769         }
770 }
771
772 static GtkWidget *label_window_create(const gchar *str)
773 {
774         GtkWidget *window;
775         GtkWidget *label;
776
777         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
778         gtk_widget_set_size_request(window, 380, 60);
779         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
780         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
781         gtk_window_set_title(GTK_WINDOW(window), str);
782         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
783         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
784         manage_window_set_transient(GTK_WINDOW(window));
785
786         label = gtk_label_new(str);
787         gtk_container_add(GTK_CONTAINER(window), label);
788         gtk_widget_show(label);
789
790         gtk_widget_show_now(window);
791
792         return window;
793 }
794
795 void folderview_rescan_tree(Folder *folder)
796 {
797         GtkWidget *window;
798
799         g_return_if_fail(folder != NULL);
800
801         if (!folder->klass->scan_tree) return;
802
803         inc_lock();
804         window = label_window_create(_("Rebuilding folder tree..."));
805
806         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
807         folder_scan_tree(folder);
808         folder_set_ui_func(folder, NULL, NULL);
809
810         folderview_set_all();
811
812         gtk_widget_destroy(window);
813         inc_unlock();
814 }
815
816 /** folderview_check_new()
817  *  Scan and update the folder and return the 
818  *  count the number of new messages since last check. 
819  *  \param folder the folder to check for new messages
820  *  \return the number of new messages since last check
821  */
822 gint folderview_check_new(Folder *folder)
823 {
824         GList *list;
825         FolderItem *item;
826         FolderView *folderview;
827         GtkCTree *ctree;
828         GtkCTreeNode *node;
829         gint new_msgs = 0;
830         gint former_new_msgs = 0;
831         gint former_new = 0;
832
833         for (list = folderview_list; list != NULL; list = list->next) {
834                 folderview = (FolderView *)list->data;
835                 ctree = GTK_CTREE(folderview->ctree);
836
837                 inc_lock();
838                 main_window_lock(folderview->mainwin);
839                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
840
841                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
842                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
843                         item = gtk_ctree_node_get_row_data(ctree, node);
844                         if (!item || !item->path || !item->folder) continue;
845                         if (item->no_select) continue;
846                         if (folder && folder != item->folder) continue;
847                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
848                         if (!item->prefs->newmailcheck) continue;
849
850                         folderview_scan_tree_func(item->folder, item, NULL);
851                         former_new = item->new_msgs;
852                         if (folder_item_scan(item) < 0) {
853                                 if (folder && !FOLDER_IS_LOCAL(folder))
854                                         break;
855                         }
856                         folderview_update_node(folderview, node);
857                         new_msgs += item->new_msgs;
858                         former_new_msgs += former_new;
859                 }
860
861                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
862                 main_window_unlock(folderview->mainwin);
863                 inc_unlock();
864         }
865
866         folder_write_list();
867         /* Number of new messages since last check is the just the difference 
868          * between former_new_msgs and new_msgs. If new_msgs is less than
869          * former_new_msgs, that would mean another session accessed the folder
870          * and the result is not well defined.
871          */
872         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
873         return new_msgs;
874 }
875
876 void folderview_check_new_all(void)
877 {
878         GList *list;
879         GtkWidget *window;
880         FolderView *folderview;
881
882         folderview = (FolderView *)folderview_list->data;
883
884         inc_lock();
885         main_window_lock(folderview->mainwin);
886         window = label_window_create
887                 (_("Checking for new messages in all folders..."));
888
889         list = folder_get_list();
890         for (; list != NULL; list = list->next) {
891                 Folder *folder = list->data;
892
893                 folderview_check_new(folder);
894         }
895
896         folder_write_list();
897         folderview_set_all();
898
899         gtk_widget_destroy(window);
900         main_window_unlock(folderview->mainwin);
901         inc_unlock();
902 }
903
904 static gboolean folderview_search_new_recursive(GtkCTree *ctree,
905                                                 GtkCTreeNode *node)
906 {
907         FolderItem *item;
908
909         if (node) {
910                 item = gtk_ctree_node_get_row_data(ctree, node);
911                 if (item) {
912                         if (item->new_msgs > 0 ||
913                             (item->stype == F_QUEUE && item->total_msgs > 0))
914                                 return TRUE;
915                 }
916                 node = GTK_CTREE_ROW(node)->children;
917         } else
918                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
919
920         while (node) {
921                 if (folderview_search_new_recursive(ctree, node) == TRUE)
922                         return TRUE;
923                 node = GTK_CTREE_ROW(node)->sibling;
924         }
925
926         return FALSE;
927 }
928
929 static gboolean folderview_have_new_children(FolderView *folderview,
930                                              GtkCTreeNode *node)
931 {
932         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
933
934         if (!node)
935                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
936         if (!node)
937                 return FALSE;
938
939         node = GTK_CTREE_ROW(node)->children;
940
941         while (node) {
942                 if (folderview_search_new_recursive(ctree, node) == TRUE)
943                         return TRUE;
944                 node = GTK_CTREE_ROW(node)->sibling;
945         }
946
947         return FALSE;
948 }
949
950 static gboolean folderview_search_unread_recursive(GtkCTree *ctree,
951                                                    GtkCTreeNode *node)
952 {
953         FolderItem *item;
954
955         if (node) {
956                 item = gtk_ctree_node_get_row_data(ctree, node);
957                 if (item) {
958                         if (item->unread_msgs > 0 ||
959                             (item->stype == F_QUEUE && item->total_msgs > 0))
960                                 return TRUE;
961                 }
962                 node = GTK_CTREE_ROW(node)->children;
963         } else
964                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
965
966         while (node) {
967                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
968                         return TRUE;
969                 node = GTK_CTREE_ROW(node)->sibling;
970         }
971
972         return FALSE;
973 }
974
975 static gboolean folderview_have_unread_children(FolderView *folderview,
976                                                 GtkCTreeNode *node)
977 {
978         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
979
980         if (!node)
981                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
982         if (!node)
983                 return FALSE;
984
985         node = GTK_CTREE_ROW(node)->children;
986
987         while (node) {
988                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
989                         return TRUE;
990                 node = GTK_CTREE_ROW(node)->sibling;
991         }
992
993         return FALSE;
994 }
995
996 static gboolean folderview_search_matching_recursive(GtkCTree *ctree,
997                                                    GtkCTreeNode *node)
998 {
999         FolderItem *item;
1000
1001         if (node) {
1002                 item = gtk_ctree_node_get_row_data(ctree, node);
1003                 if (item) {
1004                         if (item->search_match)
1005                                 return TRUE;
1006                 }
1007                 node = GTK_CTREE_ROW(node)->children;
1008         } else
1009                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1010
1011         while (node) {
1012                 if (folderview_search_matching_recursive(ctree, node) == TRUE)
1013                         return TRUE;
1014                 node = GTK_CTREE_ROW(node)->sibling;
1015         }
1016
1017         return FALSE;
1018 }
1019
1020 static gboolean folderview_have_matching_children(FolderView *folderview,
1021                                                 GtkCTreeNode *node)
1022 {
1023         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1024
1025         if (!node)
1026                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1027         if (!node)
1028                 return FALSE;
1029
1030         node = GTK_CTREE_ROW(node)->children;
1031
1032         while (node) {
1033                 if (folderview_search_matching_recursive(ctree, node) == TRUE)
1034                         return TRUE;
1035                 node = GTK_CTREE_ROW(node)->sibling;
1036         }
1037
1038         return FALSE;
1039 }
1040
1041 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1042 {
1043         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1044         GtkStyle *style = NULL;
1045         GtkStyle *color_style = NULL;
1046         FolderItem *item;
1047         GdkPixmap *xpm, *openxpm;
1048         GdkBitmap *mask, *openmask;
1049         static GdkPixmap *searchicon;
1050         static GdkBitmap *searchmask;
1051
1052         gchar *name;
1053         gchar *str;
1054         gboolean add_unread_mark;
1055         gboolean add_sub_match_mark;
1056         gboolean use_bold, use_color;
1057
1058         item = gtk_ctree_node_get_row_data(ctree, node);
1059         g_return_if_fail(item != NULL);
1060
1061         switch (item->stype) {
1062         case F_INBOX:
1063                 if (item->hide_read_msgs) {
1064                         xpm = inboxhrmxpm;
1065                         mask = inboxhrmxpmmask;
1066                         openxpm = inboxopenhrmxpm;
1067                         openmask = inboxopenhrmxpmmask;
1068                 } else {
1069                         xpm = inboxxpm;
1070                         mask = inboxxpmmask;
1071                         openxpm = inboxopenxpm;
1072                         openmask = inboxopenxpmmask;
1073                 }
1074                 break;
1075         case F_OUTBOX:
1076                 if (item->hide_read_msgs) {
1077                         xpm = outboxhrmxpm;
1078                         mask = outboxhrmxpmmask;
1079                         openxpm = outboxopenhrmxpm;
1080                         openmask = outboxopenhrmxpmmask;
1081                 } else {
1082                         xpm = outboxxpm;
1083                         mask = outboxxpmmask;
1084                         openxpm = outboxopenxpm;
1085                         openmask = outboxopenxpmmask;
1086                 }
1087                 break;
1088         case F_QUEUE:
1089                 if (item->hide_read_msgs) {
1090                         xpm = queuehrmxpm;
1091                         mask = queuehrmxpmmask;
1092                         openxpm = queueopenhrmxpm;
1093                         openmask = queueopenhrmxpmmask;
1094                 } else {
1095                         xpm = queuexpm;
1096                         mask = queuexpmmask;
1097                         openxpm = queueopenxpm;
1098                         openmask = queueopenxpmmask;
1099                 }
1100                 break;
1101         case F_TRASH:
1102                 if (item->hide_read_msgs) {
1103                         xpm = trashhrmxpm;
1104                         mask = trashhrmxpmmask;
1105                         openxpm = trashopenhrmxpm;
1106                         openmask = trashopenhrmxpmmask;
1107                 } else {
1108                         xpm = trashxpm;
1109                         mask = trashxpmmask;
1110                         openxpm = trashopenxpm;
1111                         openmask = trashopenxpmmask;
1112                 }
1113                 break;
1114         case F_DRAFT:
1115                 xpm = draftsxpm;
1116                 mask = draftsxpmmask;
1117                 openxpm = draftsopenxpm;
1118                 openmask = draftsopenxpmmask;
1119                 break;
1120         default:
1121                 if (item->hide_read_msgs) {
1122                         xpm = folderhrmxpm;
1123                         mask = folderhrmxpmmask;
1124                         openxpm = folderopenhrmxpm;
1125                         openmask = folderopenhrmxpmmask;
1126                 } else {
1127                         xpm = folderxpm;
1128                         mask = folderxpmmask;
1129                         openxpm = folderopenxpm;
1130                         openmask = folderopenxpmmask;
1131                 }
1132         }
1133         name = folder_item_get_name(item);
1134
1135         if (!GTK_CTREE_ROW(node)->expanded) {
1136                 add_unread_mark = folderview_have_unread_children(
1137                                         folderview, node);
1138                 add_sub_match_mark = folderview_have_matching_children(
1139                                         folderview, node);
1140         } else {
1141                 add_unread_mark = FALSE;
1142                 add_sub_match_mark = FALSE;
1143         }
1144
1145         if (item->search_match) {
1146                 if (!searchicon) {
1147                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1148                          &searchicon, &searchmask);
1149                 }
1150                 xpm = openxpm = searchicon;
1151                 mask = openmask = searchmask;
1152         }
1153
1154         if (item->stype == F_QUEUE && item->total_msgs > 0 &&
1155             prefs_common.display_folder_unread) {
1156                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1157                                       add_unread_mark ? "+" : "");
1158                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1159                                         xpm, mask, openxpm, openmask,
1160                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1161                 g_free(str);
1162         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1163                     prefs_common.display_folder_unread) 
1164                    || add_sub_match_mark) {
1165
1166                 if (item->unread_msgs > 0)
1167                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1168                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1169                                               item->unreadmarked_msgs > 0 ? "!":"");
1170                 else
1171                         str = g_strdup_printf("%s (+)", name);
1172                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1173                                         xpm, mask, openxpm, openmask,
1174                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1175                 g_free(str);
1176         } else {
1177                 str = g_strdup_printf("%s%s", name, 
1178                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1179         
1180                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1181                                         xpm, mask, openxpm, openmask,
1182                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1183                 g_free(str);
1184         }
1185         g_free(name);
1186
1187         if (!folder_item_parent(item)) {
1188                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1189                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1190                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1191         } else {
1192                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new_msgs));
1193                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread_msgs));
1194                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total_msgs));
1195         }
1196
1197         if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1198             item->stype == F_TRASH) {
1199                 use_bold = use_color = FALSE;
1200         } else if (item->stype == F_QUEUE) {
1201                 /* highlight queue folder if there are any messages */
1202                 use_bold = use_color = (item->total_msgs > 0);
1203         } else {
1204                 /* if unread messages exist, print with bold font */
1205                 use_bold = (item->unread_msgs > 0) || add_unread_mark;
1206                 /* if new messages exist, print with colored letter */
1207                 use_color =
1208                         (item->new_msgs > 0) ||
1209                         (add_unread_mark &&
1210                          folderview_have_new_children(folderview, node));       
1211         }
1212
1213         gtk_ctree_node_set_foreground(ctree, node, NULL);
1214
1215         if (use_bold) {
1216                 if (item->prefs->color > 0 && !use_color) {
1217                         GdkColor gdk_color;
1218
1219                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1220                         color_style = gtk_style_copy(bold_style);
1221                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1222                         style = color_style;
1223                 } else if (use_color)
1224                         style = bold_color_style;
1225                 else
1226                         style = bold_style;
1227                 if (item->op_count > 0) {
1228                         style = bold_tgtfold_style;
1229                 }
1230         } else if (use_color) {
1231                 style = normal_color_style;
1232                 gtk_ctree_node_set_foreground(ctree, node,
1233                                               &folderview->color_new);
1234         } else if (item->op_count > 0) {
1235                 style = bold_tgtfold_style;
1236         } else if (item->prefs->color > 0) {
1237                 GdkColor gdk_color;
1238
1239                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1240                 color_style = gtk_style_copy(normal_style);
1241                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1242                 style = color_style;
1243         } else {
1244                 style = normal_style;
1245         }
1246
1247         gtk_ctree_node_set_row_style(ctree, node, style);
1248
1249         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1250                 folderview_update_node(folderview, node);
1251 }
1252
1253 #if !CLAWS /* keep it here for syncs */
1254 void folderview_update_item(FolderItem *item, gboolean update_summary)
1255 {
1256         GList *list;
1257         FolderView *folderview;
1258         GtkCTree *ctree;
1259         GtkCTreeNode *node;
1260
1261         g_return_if_fail(item != NULL);
1262
1263         for (list = folderview_list; list != NULL; list = list->next) {
1264                 folderview = (FolderView *)list->data;
1265                 ctree = GTK_CTREE(folderview->ctree);
1266
1267                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1268                 if (node) {
1269                         folderview_update_node(folderview, node);
1270                         if (update_summary && folderview->opened == node)
1271                                 summary_show(folderview->summaryview,
1272                                              item, FALSE);
1273                 }
1274         }
1275 }
1276 #endif
1277
1278 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1279 {
1280         GList *list;
1281         FolderView *folderview;
1282         GtkCTree *ctree;
1283         GtkCTreeNode *node;
1284
1285         g_return_if_fail(item != NULL);
1286
1287         for (list = folderview_list; list != NULL; list = list->next) {
1288                 folderview = (FolderView *)list->data;
1289                 ctree = GTK_CTREE(folderview->ctree);
1290
1291                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1292                 if (node) {
1293                         item->search_match = matches;
1294                         folderview_update_node(folderview, node);
1295                 }
1296         }
1297 }
1298
1299 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1300 {
1301         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1302         FolderView *folderview = (FolderView *)data;
1303         GtkCTree *ctree;
1304         GtkCTreeNode *node;
1305
1306         g_return_val_if_fail(update_info != NULL, TRUE);
1307         g_return_val_if_fail(update_info->item != NULL, TRUE);
1308         g_return_val_if_fail(folderview != NULL, FALSE);
1309
1310         ctree = GTK_CTREE(folderview->ctree);
1311
1312         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1313         if (node) {
1314                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1315                         folderview_update_node(folderview, node);
1316                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1317                         summary_show(folderview->summaryview, update_info->item);
1318         }
1319         
1320         return FALSE;
1321 }
1322
1323 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1324                                                 gpointer data)
1325 {
1326         /* CLAWS: share this joy with other hook functions ... */
1327         folder_item_update((FolderItem *)key, 
1328                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1329 }
1330
1331 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1332 {
1333         GList *list;
1334         FolderItemUpdateFlags flags;
1335         
1336         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1337                 : 0;
1338         for (list = folderview_list; list != NULL; list = list->next)
1339                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1340                                      GINT_TO_POINTER(flags));
1341 }
1342
1343 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1344                                       GNode *gnode, GtkCTreeNode *cnode,
1345                                       gpointer data)
1346 {
1347         FolderView *folderview = (FolderView *)data;
1348         FolderItem *item = FOLDER_ITEM(gnode->data);
1349
1350         g_return_val_if_fail(item != NULL, FALSE);
1351
1352         gtk_ctree_node_set_row_data(ctree, cnode, item);
1353         folderview_update_node(folderview, cnode);
1354
1355         return TRUE;
1356 }
1357
1358 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1359                                    gpointer data)
1360 {
1361         FolderView *folderview = (FolderView *)data;
1362         FolderItem *item;
1363
1364         if (GTK_CTREE_ROW(node)->children) {
1365                 item = gtk_ctree_node_get_row_data(ctree, node);
1366                 g_return_if_fail(item != NULL);
1367
1368                 if (!item->collapsed)
1369                         gtk_ctree_expand(ctree, node);
1370                 else
1371                         folderview_update_node(folderview, node);
1372         }
1373 }
1374
1375 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1376                                GtkCTreeNode *root, GtkCTreeNode **prev)
1377 {
1378         if (item) {
1379                 GtkCTreeNode *node, *parent, *sibling;
1380
1381                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1382                 if (!node)
1383                         g_warning("%s not found.\n", item->path);
1384                 else {
1385                         parent = GTK_CTREE_ROW(node)->parent;
1386                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1387                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1388                         else
1389                                 sibling = GTK_CTREE_ROW(parent)->children;
1390                         while (sibling) {
1391                                 FolderItem *tmp;
1392
1393                                 tmp = gtk_ctree_node_get_row_data
1394                                         (ctree, sibling);
1395                                 if (tmp->stype != F_NORMAL)
1396                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1397                                 else
1398                                         break;
1399                         }
1400                         if (node != sibling)
1401                                 gtk_ctree_move(ctree, node, parent, sibling);
1402                 }
1403
1404                 *prev = node;
1405         }
1406 }
1407
1408 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1409                                     Folder *folder)
1410 {
1411         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1412         GtkCTreeNode *prev = NULL;
1413
1414         gtk_sctree_sort_recursive(ctree, root);
1415
1416         if (root && GTK_CTREE_ROW(root)->parent) return;
1417
1418         set_special_folder(ctree, folder->inbox, root, &prev);
1419         set_special_folder(ctree, folder->outbox, root, &prev);
1420         set_special_folder(ctree, folder->draft, root, &prev);
1421         set_special_folder(ctree, folder->queue, root, &prev);
1422         set_special_folder(ctree, folder->trash, root, &prev);
1423 }
1424
1425 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1426 {
1427         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1428         GtkCTreeNode *root;
1429
1430         g_return_if_fail(folder != NULL);
1431
1432         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1433                                       folderview_gnode_func, folderview);
1434         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1435                                 folderview);
1436         folderview_sort_folders(folderview, root, folder);
1437 }
1438
1439 /* callback functions */
1440
1441 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1442                                           FolderView *folderview)
1443 {
1444         GtkCList *clist = GTK_CLIST(ctree);
1445         gint prev_row = -1, row = -1, column = -1;
1446         FolderItem *item;
1447         Folder *folder;
1448         FolderViewPopup *fpopup;
1449         GtkItemFactory *fpopup_factory;
1450         GtkWidget *popup;
1451
1452         if (!event) return FALSE;
1453
1454         if (event->button == 1) {
1455                 folderview->open_folder = TRUE;
1456                 return FALSE;
1457         }
1458
1459         if (event->button == 2 || event->button == 3) {
1460                 /* right clicked */
1461                 if (clist->selection) {
1462                         GtkCTreeNode *node;
1463
1464                         node = GTK_CTREE_NODE(clist->selection->data);
1465                         if (node)
1466                                 prev_row = gtkut_ctree_get_nth_from_node
1467                                         (GTK_CTREE(ctree), node);
1468                 }
1469
1470                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1471                                                   &row, &column))
1472                         return FALSE;
1473                 if (prev_row != row) {
1474                         gtk_clist_unselect_all(clist);
1475                         if (event->button == 2)
1476                                 folderview_select_node
1477                                         (folderview,
1478                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1479                                                             row));
1480                         else
1481                                 gtk_clist_select_row(clist, row, column);
1482                 }
1483         }
1484
1485         if (event->button != 3) return FALSE;
1486
1487         item = gtk_clist_get_row_data(clist, row);
1488         g_return_val_if_fail(item != NULL, FALSE);
1489         g_return_val_if_fail(item->folder != NULL, FALSE);
1490         folder = item->folder;
1491
1492         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1493         if (fpopup != NULL)
1494                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1495         else {
1496                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1497                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1498         }
1499
1500         if (fpopup->set_sensitivity != NULL)
1501                 fpopup->set_sensitivity(fpopup_factory, item);
1502
1503         if (item == folder->trash &&
1504             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1505                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1506                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1507         } else if (item != folder->trash) {
1508                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1509                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1510         }
1511         
1512 #define SET_SENS(name, sens) \
1513         menu_set_sensitive(fpopup_factory, name, sens)
1514
1515         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1516         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1517                  folderview->selected == folderview->opened);
1518         SET_SENS("/Properties...", item->node->parent != NULL);
1519         SET_SENS("/Processing...", item->node->parent != NULL);
1520         if (item == folder->trash)
1521                 SET_SENS("/Empty trash...", folder_item_get_msg_list(item) != NULL);
1522 #undef SET_SENS
1523
1524         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1525         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1526                        event->button, event->time);
1527
1528         return FALSE;
1529 }
1530
1531 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1532                                            FolderView *folderview)
1533 {
1534         if (!event) return FALSE;
1535
1536         if (event->button == 1 && folderview->open_folder == FALSE &&
1537             folderview->opened != NULL) {
1538                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1539                                           folderview->opened);
1540                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1541         }
1542
1543         return FALSE;
1544 }
1545
1546 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1547                                        FolderView *folderview)
1548 {
1549         if (!event) return FALSE;
1550
1551         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1552                 return FALSE;
1553
1554         switch (event->keyval) {
1555         case GDK_Return:
1556                 if (folderview->selected) {
1557                         folderview_select_node(folderview,
1558                                                folderview->selected);
1559                 }
1560                 break;
1561         case GDK_space:
1562                 if (folderview->selected) {
1563                         if (folderview->opened == folderview->selected &&
1564                             (!folderview->summaryview->folder_item ||
1565                              folderview->summaryview->folder_item->total_msgs == 0))
1566                                 folderview_select_next_unread(folderview);
1567                         else
1568                                 folderview_select_node(folderview,
1569                                                        folderview->selected);
1570                 }
1571                 break;
1572         default:
1573                 break;
1574         }
1575
1576         return FALSE;
1577 }
1578
1579 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1580                                 gint column, FolderView *folderview)
1581 {
1582         static gboolean can_select = TRUE;      /* exclusive lock */
1583         gboolean opened;
1584         FolderItem *item;
1585         gchar *buf;
1586
1587         folderview->selected = row;
1588
1589         if (folderview->opened == row) {
1590                 folderview->open_folder = FALSE;
1591                 return;
1592         }
1593
1594         if (!can_select || summary_is_locked(folderview->summaryview)) {
1595                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1596                 gtk_ctree_select(ctree, folderview->opened);
1597                 return;
1598         }
1599
1600         if (!folderview->open_folder) return;
1601
1602         item = gtk_ctree_node_get_row_data(ctree, row);
1603         if (!item) return;
1604
1605         can_select = FALSE;
1606
1607         /* Save cache for old folder */
1608         /* We don't want to lose all caches if sylpheed crashed */
1609         if (folderview->opened) {
1610                 FolderItem *olditem;
1611                 
1612                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1613                 if (olditem) {
1614                         /* will be null if we just moved the previously opened folder */
1615                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1616                         folder_item_close(olditem);
1617                 }
1618         }
1619
1620         /* CLAWS: set compose button type: news folder items 
1621          * always have a news folder as parent */
1622         if (item->folder) 
1623                 toolbar_set_compose_button
1624                         (folderview->mainwin->toolbar,
1625                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1626                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1627
1628         if (item->path)
1629                 debug_print("Folder %s is selected\n", item->path);
1630
1631         if (!GTK_CTREE_ROW(row)->children)
1632                 gtk_ctree_expand(ctree, row);
1633         if (folderview->opened &&
1634             !GTK_CTREE_ROW(folderview->opened)->children)
1635                 gtk_ctree_collapse(ctree, folderview->opened);
1636
1637         /* ungrab the mouse event */
1638         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1639                 gtk_grab_remove(GTK_WIDGET(ctree));
1640                 if (gdk_pointer_is_grabbed())
1641                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1642         }
1643
1644         /* Open Folder */
1645         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1646                                         item->path : "(null)");
1647         debug_print("%s\n", buf);
1648         STATUSBAR_PUSH(folderview->mainwin, buf);
1649         g_free(buf);
1650
1651         main_window_cursor_wait(folderview->mainwin);
1652
1653         if (folder_item_open(item) != 0) {
1654                 main_window_cursor_normal(folderview->mainwin);
1655                 STATUSBAR_POP(folderview->mainwin);
1656
1657                 alertpanel_error(_("Folder could not be opened."));
1658
1659                 folderview->open_folder = FALSE;
1660                 can_select = TRUE;
1661
1662                 return;
1663         }
1664
1665         main_window_cursor_normal(folderview->mainwin);
1666
1667         /* Show messages */
1668         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1669         opened = summary_show(folderview->summaryview, item);
1670         
1671         folder_clean_cache_memory();
1672
1673         if (!opened) {
1674                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1675                 gtk_ctree_select(ctree, folderview->opened);
1676         } else {
1677                 folderview->opened = row;
1678                 if (gtk_ctree_node_is_visible(ctree, row)
1679                     != GTK_VISIBILITY_FULL)
1680                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1681         }
1682
1683         STATUSBAR_POP(folderview->mainwin);
1684
1685         folderview->open_folder = FALSE;
1686         can_select = TRUE;
1687 }
1688
1689 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1690                                      FolderView *folderview)
1691 {
1692         FolderItem *item;
1693
1694         item = gtk_ctree_node_get_row_data(ctree, node);
1695         g_return_if_fail(item != NULL);
1696         item->collapsed = FALSE;
1697         folderview_update_node(folderview, node);
1698 }
1699
1700 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1701                                       FolderView *folderview)
1702 {
1703         FolderItem *item;
1704
1705         item = gtk_ctree_node_get_row_data(ctree, node);
1706         g_return_if_fail(item != NULL);
1707         item->collapsed = TRUE;
1708         folderview_update_node(folderview, node);
1709 }
1710
1711 static void folderview_popup_close(GtkMenuShell *menu_shell,
1712                                    FolderView *folderview)
1713 {
1714         if (!folderview->opened) return;
1715
1716         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1717                                   folderview->opened);
1718         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1719 }
1720
1721 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1722                                    FolderView *folderview)
1723 {
1724         switch (column) {
1725         case COL_FOLDER:
1726                 prefs_common.folder_col_folder = width;
1727                 break;
1728         case COL_NEW:
1729                 prefs_common.folder_col_new = width;
1730                 break;
1731         case COL_UNREAD:
1732                 prefs_common.folder_col_unread = width;
1733                 break;
1734         case COL_TOTAL:
1735                 prefs_common.folder_col_total = width;
1736                 break;
1737         default:
1738                 break;
1739         }
1740 }
1741
1742 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1743 {
1744         GNode *srcnode;
1745
1746         folderview_create_folder_node(folderview, item);
1747         
1748         srcnode = item->folder->node;   
1749         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1750         srcnode = srcnode->children;
1751         while (srcnode != NULL) {
1752                 if (srcnode && srcnode->data) {
1753                         FolderItem *next_item = (FolderItem*) srcnode->data;
1754                         folderview_create_folder_node_recursive(folderview, next_item);
1755                 }
1756                 srcnode = srcnode->next;
1757         }
1758 }
1759
1760 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1761 {
1762         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1763         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1764         GtkCTreeNode *node, *parent_node;
1765         
1766         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
1767         if (parent_node == NULL)
1768                 return;
1769
1770         gtk_clist_freeze(GTK_CLIST(ctree));
1771
1772         text[COL_FOLDER] = item->name;
1773         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1774                                      FOLDER_SPACING,
1775                                      folderxpm, folderxpmmask,
1776                                      folderopenxpm, folderopenxpmmask,
1777                                      FALSE, FALSE);
1778         gtk_ctree_expand(ctree, parent_node);
1779         gtk_ctree_node_set_row_data(ctree, node, item);
1780         if (normal_style)
1781                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1782         folderview_sort_folders(folderview, folderview->selected, item->folder);
1783
1784         gtk_clist_thaw(GTK_CLIST(ctree));
1785 }
1786
1787 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
1788                                       GtkWidget *widget)
1789 {
1790         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1791         FolderItem *item;
1792         GSList *mlist = NULL;
1793         GSList *cur = NULL;
1794         if (!folderview->selected) return;
1795         
1796         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1797         g_return_if_fail(item != NULL);
1798         g_return_if_fail(item->folder != NULL);
1799         if (item != item->folder->trash) return;
1800         
1801         if (prefs_common.ask_on_clean) {
1802                 if (alertpanel(_("Empty trash"),
1803                                _("Empty all messages in trash?"),
1804                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
1805                         return;
1806         }
1807         
1808         mlist = folder_item_get_msg_list(item);
1809         
1810         for (cur = mlist ; cur != NULL ; cur = cur->next) {
1811                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1812                 /* is it partially received? (partial_recv isn't cached) */
1813                 if (msginfo->total_size != 0 && 
1814                     msginfo->size != (off_t)msginfo->total_size)
1815                         partial_mark_for_delete(msginfo);
1816         }
1817         procmsg_msg_list_free(mlist);
1818
1819         folder_item_remove_all_msg(item);
1820 }
1821
1822 static void folderview_search_cb(FolderView *folderview, guint action,
1823                                  GtkWidget *widget)
1824 {
1825         summary_search(folderview->summaryview);
1826 }
1827
1828 static void folderview_property_cb(FolderView *folderview, guint action,
1829                                    GtkWidget *widget)
1830 {
1831         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1832         FolderItem *item;
1833
1834         if (!folderview->selected) return;
1835
1836         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1837         g_return_if_fail(item != NULL);
1838         g_return_if_fail(item->folder != NULL);
1839
1840         if (folder_item_parent(item) == NULL)
1841                 return;
1842
1843         prefs_folder_item_open(item);
1844 }
1845
1846 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
1847 {
1848         GSList *list = NULL;
1849         GSList *done = NULL;
1850         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1851         
1852         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
1853                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
1854                 &&  list->data != node) {
1855                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
1856                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
1857                 }
1858         }
1859         for (list = done; list != NULL; list = g_slist_next(list)) {
1860                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
1861                                                                  list->data);
1862         }
1863         g_slist_free(done);
1864 }
1865
1866 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
1867                             FolderItem *to_folder)
1868 {
1869         FolderItem *from_parent = NULL;
1870         FolderItem *new_folder = NULL;
1871         GtkCTreeNode *src_node = NULL;
1872         gchar *buf;
1873         gint status;
1874
1875         g_return_if_fail(folderview != NULL);
1876         g_return_if_fail(from_folder != NULL);
1877         g_return_if_fail(to_folder != NULL);
1878
1879         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
1880         from_parent = folder_item_parent(from_folder);
1881         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
1882         STATUSBAR_PUSH(folderview->mainwin, buf);
1883         g_free(buf);
1884         summary_clear_all(folderview->summaryview);
1885         folderview->opened = NULL;
1886         folderview->selected = NULL;
1887         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
1888         inc_lock();
1889         main_window_cursor_wait(folderview->mainwin);
1890         statusbar_verbosity_set(TRUE);
1891         folder_item_update_freeze();
1892         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
1893                 statusbar_verbosity_set(FALSE);
1894                 main_window_cursor_normal(folderview->mainwin);
1895                 STATUSBAR_POP(folderview->mainwin);
1896                 folder_item_update_thaw();
1897                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
1898
1899                 folderview_sort_folders(folderview, 
1900                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
1901                                 NULL, folder_item_parent(new_folder)), new_folder->folder);
1902                 folderview_select(folderview, new_folder);
1903         } else {
1904                 statusbar_verbosity_set(FALSE);         
1905                 main_window_cursor_normal(folderview->mainwin);
1906                 STATUSBAR_POP(folderview->mainwin);
1907                 folder_item_update_thaw();
1908                 switch (status) {
1909                 case F_MOVE_FAILED_DEST_IS_PARENT:
1910                         alertpanel_error(_("Source and destination are the same."));
1911                         break;
1912                 case F_MOVE_FAILED_DEST_IS_CHILD:
1913                         alertpanel_error(_("Can't move a folder to one of its children."));
1914                         break;
1915                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
1916                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
1917                         break;
1918                 default:
1919                         alertpanel_error(_("Move failed!"));
1920                         break;
1921                 }
1922         }       
1923         inc_unlock();           
1924         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
1925 }
1926
1927 static gint folderview_clist_compare(GtkCList *clist,
1928                                      gconstpointer ptr1, gconstpointer ptr2)
1929 {
1930         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
1931         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
1932
1933         if (!item1->name)
1934                 return (item2->name != NULL);
1935         if (!item2->name)
1936                 return -1;
1937
1938         return g_utf8_collate(item1->name, item2->name);
1939 }
1940
1941 static void folderview_processing_cb(FolderView *folderview, guint action,
1942                                      GtkWidget *widget)
1943 {
1944         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1945         FolderItem *item;
1946
1947         if (!folderview->selected) return;
1948
1949         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1950         g_return_if_fail(item != NULL);
1951         g_return_if_fail(item->folder != NULL);
1952
1953         prefs_filtering_open(&item->prefs->processing,
1954                              _("Processing configuration"), NULL, NULL);
1955 }
1956
1957 void folderview_set_target_folder_color(gint color_op) 
1958 {
1959         gint firstone = 1;
1960         GList *list;
1961         FolderView *folderview;
1962
1963         for (list = folderview_list; list != NULL; list = list->next) {
1964                 folderview = (FolderView *)list->data;
1965                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
1966                 if (firstone) {
1967                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
1968                                 folderview->color_op;
1969                         firstone = 0;
1970                 }
1971         }
1972 }
1973
1974 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
1975 {
1976         folderview_init(folderview);
1977         folderview_set_all();
1978 }
1979
1980 static void drag_state_stop(FolderView *folderview)
1981 {
1982         if (folderview->drag_timer)
1983                 gtk_timeout_remove(folderview->drag_timer);
1984         folderview->drag_timer = 0;
1985         folderview->drag_node = NULL;
1986 }
1987
1988 static gint folderview_defer_expand(FolderView *folderview)
1989 {
1990         if (folderview->drag_node) {
1991                 folderview_recollapse_nodes(folderview, folderview->drag_node);
1992                 if (folderview->drag_item->collapsed) {
1993                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
1994                         folderview->nodes_to_recollapse = g_slist_append
1995                                 (folderview->nodes_to_recollapse, folderview->drag_node);
1996                 }
1997         }
1998         folderview->drag_item  = NULL;
1999         folderview->drag_timer = 0;
2000         return FALSE;
2001 }
2002
2003 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2004 {
2005         /* the idea is that we call drag_state_start() whenever we want expansion to
2006          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2007          * we need to call drag_state_stop() */
2008         drag_state_stop(folderview);
2009         /* request expansion */
2010         if (0 != (folderview->drag_timer = gtk_timeout_add
2011                         (prefs_common.hover_timeout, 
2012                          (GtkFunction)folderview_defer_expand,
2013                          folderview))) {
2014                 folderview->drag_node = node;
2015                 folderview->drag_item = item;
2016         }                        
2017 }
2018
2019 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2020                                   FolderView       *folderview)
2021 {
2022         GdkDragContext *context;
2023
2024         g_return_if_fail(folderview != NULL);
2025         if (folderview->selected == NULL) return;
2026         if (folderview->nodes_to_recollapse) 
2027                 g_slist_free(folderview->nodes_to_recollapse);
2028         folderview->nodes_to_recollapse = NULL;
2029         context = gtk_drag_begin(widget, folderview->target_list,
2030                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2031         gtk_drag_set_icon_default(context);
2032 }
2033
2034 static void folderview_drag_data_get(GtkWidget        *widget,
2035                                      GdkDragContext   *drag_context,
2036                                      GtkSelectionData *selection_data,
2037                                      guint             info,
2038                                      guint             time,
2039                                      FolderView       *folderview)
2040 {
2041         FolderItem *item;
2042         GList *cur;
2043         gchar *source = NULL;
2044
2045         for (cur = GTK_CLIST(folderview->ctree)->selection;
2046              cur != NULL; cur = cur->next) {
2047                 item = gtk_ctree_node_get_row_data
2048                         (GTK_CTREE(folderview->ctree), 
2049                          GTK_CTREE_NODE(cur->data));
2050                 if (item) {
2051                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2052                         gtk_selection_data_set(selection_data,
2053                                                selection_data->target, 8,
2054                                                source, strlen(source));
2055                         break;
2056                 } else
2057                         return;
2058         }
2059 }
2060
2061 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2062 {
2063         FolderUpdateData *hookdata;
2064         FolderView *folderview;
2065         GtkWidget *ctree;
2066
2067         hookdata = source;
2068         folderview = (FolderView *) userdata;   
2069         g_return_val_if_fail(hookdata != NULL, FALSE);
2070         g_return_val_if_fail(folderview != NULL, FALSE);
2071
2072         ctree = folderview->ctree;
2073         g_return_val_if_fail(ctree != NULL, FALSE);
2074
2075         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2076                 folderview_create_folder_node(folderview, hookdata->item);
2077         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2078                 GtkCTreeNode *node;
2079
2080                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2081                 if (node != NULL)
2082                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2083         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2084                 folderview_set(folderview);
2085
2086         return FALSE;
2087 }
2088
2089 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2090                                           GdkDragContext *context,
2091                                           gint            x,
2092                                           gint            y,
2093                                           guint           time,
2094                                           FolderView     *folderview)
2095 {
2096         gint row, column;
2097         FolderItem *item, *src_item = NULL;
2098         GtkCTreeNode *node = NULL;
2099         gboolean acceptable = FALSE;
2100         gint height = folderview->ctree->allocation.height;
2101         gint total_height = folderview->ctree->requisition.height;
2102         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2103                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2104         gfloat vpos = pos->value;
2105
2106         if (gtk_clist_get_selection_info
2107                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2108                 GtkWidget *srcwidget;
2109
2110                 if (y > height - 24 && height + vpos < total_height)
2111                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2112
2113                 if (y < 24 && y > 0)
2114                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2115
2116                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2117                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2118                 src_item = folderview->summaryview->folder_item;
2119
2120                 srcwidget = gtk_drag_get_source_widget(context);
2121                 if (srcwidget == folderview->summaryview->ctree) {
2122                         /* comes from summaryview */
2123                         /* we are copying messages, so only accept folder items that are not
2124                            the source item, are no root items and can copy messages */
2125                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2126                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2127                                 acceptable = TRUE;
2128                 } else if (srcwidget == folderview->ctree) {
2129                         /* comes from folderview */
2130                         /* we are moving folder items, only accept folders that are not
2131                            the source items and can copy messages and create folder items */
2132                         if (item && item->folder && src_item && src_item != item &&
2133                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2134                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2135                                 acceptable = TRUE;
2136                 }
2137         }
2138
2139         if (acceptable || (src_item && src_item == item))
2140                 drag_state_start(folderview, node, item);
2141         
2142         if (acceptable) {
2143                 g_signal_handlers_block_by_func
2144                         (G_OBJECT(widget),
2145                          G_CALLBACK(folderview_selected), folderview);
2146                 gtk_ctree_select(GTK_CTREE(widget), node);
2147                 g_signal_handlers_unblock_by_func
2148                         (G_OBJECT(widget),
2149                          G_CALLBACK(folderview_selected), folderview);
2150                 gdk_drag_status(context, 
2151                                         (context->actions == GDK_ACTION_COPY ?
2152                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2153         } else {
2154                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2155                 gdk_drag_status(context, 0, time);
2156         }
2157
2158         return acceptable;
2159 }
2160
2161 static void folderview_drag_leave_cb(GtkWidget      *widget,
2162                                      GdkDragContext *context,
2163                                      guint           time,
2164                                      FolderView     *folderview)
2165 {
2166         drag_state_stop(folderview);
2167         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2168 }
2169
2170 static void folderview_drag_received_cb(GtkWidget        *widget,
2171                                         GdkDragContext   *drag_context,
2172                                         gint              x,
2173                                         gint              y,
2174                                         GtkSelectionData *data,
2175                                         guint             info,
2176                                         guint             time,
2177                                         FolderView       *folderview)
2178 {
2179         gint row, column;
2180         FolderItem *item, *src_item;
2181         GtkCTreeNode *node;
2182
2183         drag_state_stop(folderview);
2184         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2185                 /* comes from summaryview */
2186                 if (gtk_clist_get_selection_info
2187                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2188                         return;
2189
2190                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2191                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2192                 src_item = folderview->summaryview->folder_item;
2193                 
2194                 /* re-check (due to acceptable possibly set for folder moves */
2195                 if (!(item && item->folder && item->path &&
2196                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2197                         return;
2198                 }
2199                 if (item && src_item) {
2200                         switch (drag_context->action) {
2201                         case GDK_ACTION_COPY:
2202                                 summary_copy_selected_to(folderview->summaryview, item);
2203                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2204                                 break;
2205                         case GDK_ACTION_MOVE:
2206                         case GDK_ACTION_DEFAULT:
2207                         default:
2208                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2209                                         summary_copy_selected_to(folderview->summaryview, item);
2210                                 else
2211                                         summary_move_selected_to(folderview->summaryview, item);
2212                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2213                         }
2214                 } else
2215                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2216         } else {
2217                 /* comes from folderview */
2218                 char *source;
2219                 
2220                 source = data->data + 17;
2221                 if (gtk_clist_get_selection_info
2222                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2223                     || *source == 0) {
2224                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2225                         return;
2226                 }
2227                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2228                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2229                 src_item = folder_find_item_from_identifier(source);
2230
2231                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2232                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2233                         return;
2234                 }
2235
2236                 folderview_move_folder(folderview, src_item, item);
2237                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2238         }
2239         folderview->nodes_to_recollapse = NULL;
2240 }
2241
2242 static void folderview_drag_end_cb(GtkWidget        *widget, 
2243                                    GdkDragContext   *drag_context,
2244                                    FolderView       *folderview)
2245 {
2246         drag_state_stop(folderview);
2247         g_slist_free(folderview->nodes_to_recollapse);
2248         folderview->nodes_to_recollapse = NULL;
2249 }
2250
2251 void folderview_register_popup(FolderViewPopup *fpopup)
2252 {
2253         GList *folderviews;
2254
2255         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2256                 FolderView *folderview = folderviews->data;
2257                 GtkItemFactory *factory;
2258
2259                 factory = create_ifactory(folderview, fpopup);
2260                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2261         }       
2262         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2263 }
2264
2265 void folderview_unregister_popup(FolderViewPopup *fpopup)
2266 {
2267         GList *folderviews;
2268
2269         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2270                 FolderView *folderview = folderviews->data;
2271
2272                 g_hash_table_remove(folderview->popups, fpopup->klass);
2273         }       
2274         g_hash_table_remove(folderview_popups, fpopup->klass);
2275 }