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