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