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