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