2004-11-23 [colin] 0.9.12cvs166.1
[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         gchar *name;
1003         gchar *str;
1004         gboolean add_unread_mark;
1005         gboolean use_bold, use_color;
1006
1007         item = gtk_ctree_node_get_row_data(ctree, node);
1008         g_return_if_fail(item != NULL);
1009
1010         switch (item->stype) {
1011         case F_INBOX:
1012                 if (item->hide_read_msgs) {
1013                         xpm = inboxhrmxpm;
1014                         mask = inboxhrmxpmmask;
1015                         openxpm = inboxopenhrmxpm;
1016                         openmask = inboxopenhrmxpmmask;
1017                 } else {
1018                         xpm = inboxxpm;
1019                         mask = inboxxpmmask;
1020                         openxpm = inboxopenxpm;
1021                         openmask = inboxopenxpmmask;
1022                 }
1023                 break;
1024         case F_OUTBOX:
1025                 if (item->hide_read_msgs) {
1026                         xpm = outboxhrmxpm;
1027                         mask = outboxhrmxpmmask;
1028                         openxpm = outboxopenhrmxpm;
1029                         openmask = outboxopenhrmxpmmask;
1030                 } else {
1031                         xpm = outboxxpm;
1032                         mask = outboxxpmmask;
1033                         openxpm = outboxopenxpm;
1034                         openmask = outboxopenxpmmask;
1035                 }
1036                 break;
1037         case F_QUEUE:
1038                 if (item->hide_read_msgs) {
1039                         xpm = queuehrmxpm;
1040                         mask = queuehrmxpmmask;
1041                         openxpm = queueopenhrmxpm;
1042                         openmask = queueopenhrmxpmmask;
1043                 } else {
1044                         xpm = queuexpm;
1045                         mask = queuexpmmask;
1046                         openxpm = queueopenxpm;
1047                         openmask = queueopenxpmmask;
1048                 }
1049                 break;
1050         case F_TRASH:
1051                 if (item->hide_read_msgs) {
1052                         xpm = trashhrmxpm;
1053                         mask = trashhrmxpmmask;
1054                         openxpm = trashopenhrmxpm;
1055                         openmask = trashopenhrmxpmmask;
1056                 } else {
1057                         xpm = trashxpm;
1058                         mask = trashxpmmask;
1059                         openxpm = trashopenxpm;
1060                         openmask = trashopenxpmmask;
1061                 }
1062                 break;
1063         case F_DRAFT:
1064                 xpm = draftsxpm;
1065                 mask = draftsxpmmask;
1066                 openxpm = draftsopenxpm;
1067                 openmask = draftsopenxpmmask;
1068                 break;
1069         default:
1070                 if (item->hide_read_msgs) {
1071                         xpm = folderhrmxpm;
1072                         mask = folderhrmxpmmask;
1073                         openxpm = folderopenhrmxpm;
1074                         openmask = folderopenhrmxpmmask;
1075                 } else {
1076                         xpm = folderxpm;
1077                         mask = folderxpmmask;
1078                         openxpm = folderopenxpm;
1079                         openmask = folderopenxpmmask;
1080                 }
1081         }
1082         name = folder_item_get_name(item);
1083
1084         if (!GTK_CTREE_ROW(node)->expanded &&
1085             folderview_have_unread_children(folderview, node))
1086                 add_unread_mark = TRUE;
1087         else
1088                 add_unread_mark = FALSE;
1089
1090         if (item->stype == F_QUEUE && item->total_msgs > 0 &&
1091             prefs_common.display_folder_unread) {
1092                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1093                                       add_unread_mark ? "+" : "");
1094                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1095                                         xpm, mask, openxpm, openmask,
1096                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1097                 g_free(str);
1098         } else if ((item->unread_msgs > 0 || add_unread_mark) &&
1099                  prefs_common.display_folder_unread) {
1100
1101                 if (item->unread_msgs > 0)
1102                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1103                                               add_unread_mark ? "+" : "", 
1104                                               item->unreadmarked_msgs > 0 ? "!":"");
1105                 else
1106                         str = g_strdup_printf("%s (+)", name);
1107                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1108                                         xpm, mask, openxpm, openmask,
1109                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1110                 g_free(str);
1111         } else {
1112                 str = g_strdup_printf("%s%s", name, 
1113                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1114         
1115                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1116                                         xpm, mask, openxpm, openmask,
1117                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1118                 g_free(str);
1119         }
1120         g_free(name);
1121
1122         if (!folder_item_parent(item)) {
1123                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1124                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1125                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1126         } else {
1127                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new_msgs));
1128                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread_msgs));
1129                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total_msgs));
1130         }
1131
1132         if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1133             item->stype == F_TRASH) {
1134                 use_bold = use_color = FALSE;
1135         } else if (item->stype == F_QUEUE) {
1136                 /* highlight queue folder if there are any messages */
1137                 use_bold = use_color = (item->total_msgs > 0);
1138         } else {
1139                 /* if unread messages exist, print with bold font */
1140                 use_bold = (item->unread_msgs > 0) || add_unread_mark;
1141                 /* if new messages exist, print with colored letter */
1142                 use_color =
1143                         (item->new_msgs > 0) ||
1144                         (add_unread_mark &&
1145                          folderview_have_new_children(folderview, node));       
1146         }
1147
1148         gtk_ctree_node_set_foreground(ctree, node, NULL);
1149
1150         if (use_bold) {
1151                 if (item->prefs->color > 0 && !use_color) {
1152                         GdkColor gdk_color;
1153
1154                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1155                         color_style = gtk_style_copy(bold_style);
1156                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1157                         style = color_style;
1158                 } else if (use_color)
1159                         style = bold_color_style;
1160                 else
1161                         style = bold_style;
1162                 if (item->op_count > 0) {
1163                         style = bold_tgtfold_style;
1164                 }
1165         } else if (use_color) {
1166                 style = normal_color_style;
1167                 gtk_ctree_node_set_foreground(ctree, node,
1168                                               &folderview->color_new);
1169         } else if (item->op_count > 0) {
1170                 style = bold_tgtfold_style;
1171         } else if (item->prefs->color > 0) {
1172                 GdkColor gdk_color;
1173
1174                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1175                 color_style = gtk_style_copy(normal_style);
1176                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1177                 style = color_style;
1178         } else {
1179                 style = normal_style;
1180         }
1181
1182         gtk_ctree_node_set_row_style(ctree, node, style);
1183
1184         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1185                 folderview_update_node(folderview, node);
1186 }
1187
1188 #if !CLAWS /* keep it here for syncs */
1189 void folderview_update_item(FolderItem *item, gboolean update_summary)
1190 {
1191         GList *list;
1192         FolderView *folderview;
1193         GtkCTree *ctree;
1194         GtkCTreeNode *node;
1195
1196         g_return_if_fail(item != NULL);
1197
1198         for (list = folderview_list; list != NULL; list = list->next) {
1199                 folderview = (FolderView *)list->data;
1200                 ctree = GTK_CTREE(folderview->ctree);
1201
1202                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1203                 if (node) {
1204                         folderview_update_node(folderview, node);
1205                         if (update_summary && folderview->opened == node)
1206                                 summary_show(folderview->summaryview,
1207                                              item, FALSE);
1208                 }
1209         }
1210 }
1211 #endif
1212
1213 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1214 {
1215         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1216         FolderView *folderview = (FolderView *)data;
1217         GtkCTree *ctree;
1218         GtkCTreeNode *node;
1219
1220         g_return_val_if_fail(update_info != NULL, TRUE);
1221         g_return_val_if_fail(update_info->item != NULL, TRUE);
1222         g_return_val_if_fail(folderview != NULL, FALSE);
1223
1224         ctree = GTK_CTREE(folderview->ctree);
1225
1226         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1227         if (node) {
1228                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1229                         folderview_update_node(folderview, node);
1230                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1231                         summary_show(folderview->summaryview, update_info->item);
1232         }
1233         
1234         return FALSE;
1235 }
1236
1237 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1238                                                 gpointer data)
1239 {
1240         /* CLAWS: share this joy with other hook functions ... */
1241         folder_item_update((FolderItem *)key, 
1242                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1243 }
1244
1245 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1246 {
1247         GList *list;
1248         FolderItemUpdateFlags flags;
1249         
1250         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1251                 : 0;
1252         for (list = folderview_list; list != NULL; list = list->next)
1253                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1254                                      GINT_TO_POINTER(flags));
1255 }
1256
1257 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1258                                       GNode *gnode, GtkCTreeNode *cnode,
1259                                       gpointer data)
1260 {
1261         FolderView *folderview = (FolderView *)data;
1262         FolderItem *item = FOLDER_ITEM(gnode->data);
1263
1264         g_return_val_if_fail(item != NULL, FALSE);
1265
1266         gtk_ctree_node_set_row_data(ctree, cnode, item);
1267         folderview_update_node(folderview, cnode);
1268
1269         return TRUE;
1270 }
1271
1272 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1273                                    gpointer data)
1274 {
1275         FolderView *folderview = (FolderView *)data;
1276         FolderItem *item;
1277
1278         if (GTK_CTREE_ROW(node)->children) {
1279                 item = gtk_ctree_node_get_row_data(ctree, node);
1280                 g_return_if_fail(item != NULL);
1281
1282                 if (!item->collapsed)
1283                         gtk_ctree_expand(ctree, node);
1284                 else
1285                         folderview_update_node(folderview, node);
1286         }
1287 }
1288
1289 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1290                                GtkCTreeNode *root, GtkCTreeNode **prev)
1291 {
1292         if (item) {
1293                 GtkCTreeNode *node, *parent, *sibling;
1294
1295                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1296                 if (!node)
1297                         g_warning("%s not found.\n", item->path);
1298                 else {
1299                         parent = GTK_CTREE_ROW(node)->parent;
1300                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1301                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1302                         else
1303                                 sibling = GTK_CTREE_ROW(parent)->children;
1304                         while (sibling) {
1305                                 FolderItem *tmp;
1306
1307                                 tmp = gtk_ctree_node_get_row_data
1308                                         (ctree, sibling);
1309                                 if (tmp->stype != F_NORMAL)
1310                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1311                                 else
1312                                         break;
1313                         }
1314                         if (node != sibling)
1315                                 gtk_ctree_move(ctree, node, parent, sibling);
1316                 }
1317
1318                 *prev = node;
1319         }
1320 }
1321
1322 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1323                                     Folder *folder)
1324 {
1325         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1326         GtkCTreeNode *prev = NULL;
1327
1328         gtk_sctree_sort_recursive(ctree, root);
1329
1330         if (root && GTK_CTREE_ROW(root)->parent) return;
1331
1332         set_special_folder(ctree, folder->inbox, root, &prev);
1333         set_special_folder(ctree, folder->outbox, root, &prev);
1334         set_special_folder(ctree, folder->draft, root, &prev);
1335         set_special_folder(ctree, folder->queue, root, &prev);
1336         set_special_folder(ctree, folder->trash, root, &prev);
1337 }
1338
1339 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1340 {
1341         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1342         GtkCTreeNode *root;
1343
1344         g_return_if_fail(folder != NULL);
1345
1346         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1347                                       folderview_gnode_func, folderview);
1348         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1349                                 folderview);
1350         folderview_sort_folders(folderview, root, folder);
1351 }
1352
1353 /* callback functions */
1354
1355 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1356                                           FolderView *folderview)
1357 {
1358         GtkCList *clist = GTK_CLIST(ctree);
1359         gint prev_row = -1, row = -1, column = -1;
1360         FolderItem *item;
1361         Folder *folder;
1362         FolderViewPopup *fpopup;
1363         GtkItemFactory *fpopup_factory;
1364         GtkWidget *popup;
1365
1366         if (!event) return FALSE;
1367
1368         if (event->button == 1) {
1369                 folderview->open_folder = TRUE;
1370                 return FALSE;
1371         }
1372
1373         if (event->button == 2 || event->button == 3) {
1374                 /* right clicked */
1375                 if (clist->selection) {
1376                         GtkCTreeNode *node;
1377
1378                         node = GTK_CTREE_NODE(clist->selection->data);
1379                         if (node)
1380                                 prev_row = gtkut_ctree_get_nth_from_node
1381                                         (GTK_CTREE(ctree), node);
1382                 }
1383
1384                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1385                                                   &row, &column))
1386                         return FALSE;
1387                 if (prev_row != row) {
1388                         gtk_clist_unselect_all(clist);
1389                         if (event->button == 2)
1390                                 folderview_select_node
1391                                         (folderview,
1392                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1393                                                             row));
1394                         else
1395                                 gtk_clist_select_row(clist, row, column);
1396                 }
1397         }
1398
1399         if (event->button != 3) return FALSE;
1400
1401         item = gtk_clist_get_row_data(clist, row);
1402         g_return_val_if_fail(item != NULL, FALSE);
1403         g_return_val_if_fail(item->folder != NULL, FALSE);
1404         folder = item->folder;
1405
1406         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1407         if (fpopup != NULL)
1408                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1409         else {
1410                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1411                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1412         }
1413
1414         if (fpopup->set_sensitivity != NULL)
1415                 fpopup->set_sensitivity(fpopup_factory, item);
1416
1417         if (item == folder->trash &&
1418             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1419                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1420                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1421         } else if (item != folder->trash) {
1422                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1423                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1424         }
1425         
1426 #define SET_SENS(name, sens) \
1427         menu_set_sensitive(fpopup_factory, name, sens)
1428
1429         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1430         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1431                  folderview->selected == folderview->opened);
1432         SET_SENS("/Properties...", TRUE);
1433         SET_SENS("/Processing...", item->node->parent != NULL);
1434         if (item == folder->trash)
1435                 SET_SENS("/Empty trash...", folder_item_get_msg_list(item) != NULL);
1436 #undef SET_SENS
1437
1438         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1439         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1440                        event->button, event->time);
1441
1442         return FALSE;
1443 }
1444
1445 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1446                                            FolderView *folderview)
1447 {
1448         if (!event) return FALSE;
1449
1450         if (event->button == 1 && folderview->open_folder == FALSE &&
1451             folderview->opened != NULL) {
1452                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1453                                           folderview->opened);
1454                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1455         }
1456
1457         return FALSE;
1458 }
1459
1460 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1461                                        FolderView *folderview)
1462 {
1463         if (!event) return FALSE;
1464
1465         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1466                 return FALSE;
1467
1468         switch (event->keyval) {
1469         case GDK_Return:
1470                 if (folderview->selected) {
1471                         folderview_select_node(folderview,
1472                                                folderview->selected);
1473                 }
1474                 break;
1475         case GDK_space:
1476                 if (folderview->selected) {
1477                         if (folderview->opened == folderview->selected &&
1478                             (!folderview->summaryview->folder_item ||
1479                              folderview->summaryview->folder_item->total_msgs == 0))
1480                                 folderview_select_next_unread(folderview);
1481                         else
1482                                 folderview_select_node(folderview,
1483                                                        folderview->selected);
1484                 }
1485                 break;
1486         default:
1487                 break;
1488         }
1489
1490         return FALSE;
1491 }
1492
1493 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1494                                 gint column, FolderView *folderview)
1495 {
1496         static gboolean can_select = TRUE;      /* exclusive lock */
1497         gboolean opened;
1498         FolderItem *item;
1499         gchar *buf;
1500
1501         folderview->selected = row;
1502
1503         if (folderview->opened == row) {
1504                 folderview->open_folder = FALSE;
1505                 return;
1506         }
1507
1508         if (!can_select || summary_is_locked(folderview->summaryview)) {
1509                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1510                 gtk_ctree_select(ctree, folderview->opened);
1511                 return;
1512         }
1513
1514         if (!folderview->open_folder) return;
1515
1516         item = gtk_ctree_node_get_row_data(ctree, row);
1517         if (!item) return;
1518
1519         can_select = FALSE;
1520
1521         /* Save cache for old folder */
1522         /* We don't want to lose all caches if sylpheed crashed */
1523         if (folderview->opened) {
1524                 FolderItem *olditem;
1525                 
1526                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1527                 if (olditem) {
1528                         /* will be null if we just moved the previously opened folder */
1529                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1530                         folder_item_close(olditem);
1531                 }
1532         }
1533
1534         /* CLAWS: set compose button type: news folder items 
1535          * always have a news folder as parent */
1536         if (item->folder) 
1537                 toolbar_set_compose_button
1538                         (folderview->mainwin->toolbar,
1539                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1540                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1541
1542         if (item->path)
1543                 debug_print("Folder %s is selected\n", item->path);
1544
1545         if (!GTK_CTREE_ROW(row)->children)
1546                 gtk_ctree_expand(ctree, row);
1547         if (folderview->opened &&
1548             !GTK_CTREE_ROW(folderview->opened)->children)
1549                 gtk_ctree_collapse(ctree, folderview->opened);
1550
1551         /* ungrab the mouse event */
1552         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1553                 gtk_grab_remove(GTK_WIDGET(ctree));
1554                 if (gdk_pointer_is_grabbed())
1555                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1556         }
1557
1558         /* Open Folder */
1559         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1560                                         item->path : "(null)");
1561         debug_print("%s\n", buf);
1562         STATUSBAR_PUSH(folderview->mainwin, buf);
1563         g_free(buf);
1564
1565         main_window_cursor_wait(folderview->mainwin);
1566
1567         if (folder_item_open(item) != 0) {
1568                 main_window_cursor_normal(folderview->mainwin);
1569                 STATUSBAR_POP(folderview->mainwin);
1570
1571                 alertpanel_error(_("Folder could not be opened."));
1572
1573                 folderview->open_folder = FALSE;
1574                 can_select = TRUE;
1575
1576                 return;
1577         }
1578
1579         main_window_cursor_normal(folderview->mainwin);
1580
1581         /* Show messages */
1582         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1583         opened = summary_show(folderview->summaryview, item);
1584         
1585         folder_clean_cache_memory();
1586
1587         if (!opened) {
1588                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1589                 gtk_ctree_select(ctree, folderview->opened);
1590         } else {
1591                 folderview->opened = row;
1592                 if (gtk_ctree_node_is_visible(ctree, row)
1593                     != GTK_VISIBILITY_FULL)
1594                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1595         }
1596
1597         STATUSBAR_POP(folderview->mainwin);
1598
1599         folderview->open_folder = FALSE;
1600         can_select = TRUE;
1601 }
1602
1603 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1604                                      FolderView *folderview)
1605 {
1606         FolderItem *item;
1607
1608         item = gtk_ctree_node_get_row_data(ctree, node);
1609         g_return_if_fail(item != NULL);
1610         item->collapsed = FALSE;
1611         folderview_update_node(folderview, node);
1612 }
1613
1614 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1615                                       FolderView *folderview)
1616 {
1617         FolderItem *item;
1618
1619         item = gtk_ctree_node_get_row_data(ctree, node);
1620         g_return_if_fail(item != NULL);
1621         item->collapsed = TRUE;
1622         folderview_update_node(folderview, node);
1623 }
1624
1625 static void folderview_popup_close(GtkMenuShell *menu_shell,
1626                                    FolderView *folderview)
1627 {
1628         if (!folderview->opened) return;
1629
1630         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1631                                   folderview->opened);
1632         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1633 }
1634
1635 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1636                                    FolderView *folderview)
1637 {
1638         switch (column) {
1639         case COL_FOLDER:
1640                 prefs_common.folder_col_folder = width;
1641                 break;
1642         case COL_NEW:
1643                 prefs_common.folder_col_new = width;
1644                 break;
1645         case COL_UNREAD:
1646                 prefs_common.folder_col_unread = width;
1647                 break;
1648         case COL_TOTAL:
1649                 prefs_common.folder_col_total = width;
1650                 break;
1651         default:
1652                 break;
1653         }
1654 }
1655
1656 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1657 {
1658         GNode *srcnode;
1659
1660         folderview_create_folder_node(folderview, item);
1661         
1662         srcnode = item->folder->node;   
1663         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1664         srcnode = srcnode->children;
1665         while (srcnode != NULL) {
1666                 if (srcnode && srcnode->data) {
1667                         FolderItem *next_item = (FolderItem*) srcnode->data;
1668                         folderview_create_folder_node_recursive(folderview, next_item);
1669                 }
1670                 srcnode = srcnode->next;
1671         }
1672 }
1673
1674 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1675 {
1676         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1677         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1678         GtkCTreeNode *node, *parent_node;
1679         
1680         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
1681         if (parent_node == NULL)
1682                 return;
1683
1684         gtk_clist_freeze(GTK_CLIST(ctree));
1685
1686         text[COL_FOLDER] = item->name;
1687         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1688                                      FOLDER_SPACING,
1689                                      folderxpm, folderxpmmask,
1690                                      folderopenxpm, folderopenxpmmask,
1691                                      FALSE, FALSE);
1692         gtk_ctree_expand(ctree, parent_node);
1693         gtk_ctree_node_set_row_data(ctree, node, item);
1694         if (normal_style)
1695                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1696         folderview_sort_folders(folderview, folderview->selected, item->folder);
1697
1698         gtk_clist_thaw(GTK_CLIST(ctree));
1699 }
1700
1701 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
1702                                       GtkWidget *widget)
1703 {
1704         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1705         FolderItem *item;
1706         GSList *mlist = NULL;
1707         GSList *cur = NULL;
1708         if (!folderview->selected) return;
1709         
1710         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1711         g_return_if_fail(item != NULL);
1712         g_return_if_fail(item->folder != NULL);
1713         if (item != item->folder->trash) return;
1714         
1715         if (prefs_common.ask_on_clean) {
1716                 if (alertpanel(_("Empty trash"),
1717                                _("Empty all messages in trash?"),
1718                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
1719                         return;
1720         }
1721         
1722         mlist = folder_item_get_msg_list(item);
1723         
1724         for (cur = mlist ; cur != NULL ; cur = cur->next) {
1725                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1726                 partial_mark_for_delete(msginfo);
1727                 procmsg_msginfo_free(msginfo);
1728         }
1729
1730         folder_item_remove_all_msg(item);
1731 }
1732
1733 static void folderview_search_cb(FolderView *folderview, guint action,
1734                                  GtkWidget *widget)
1735 {
1736         summary_search(folderview->summaryview);
1737 }
1738
1739 static void folderview_property_cb(FolderView *folderview, guint action,
1740                                    GtkWidget *widget)
1741 {
1742         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1743         FolderItem *item;
1744
1745         if (!folderview->selected) return;
1746
1747         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1748         g_return_if_fail(item != NULL);
1749         g_return_if_fail(item->folder != NULL);
1750
1751         if (folder_item_parent(item) == NULL && item->folder->account)
1752                 account_open(item->folder->account);
1753         else {
1754                 prefs_folder_item_open(item);
1755         }
1756 }
1757
1758 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
1759 {
1760         GSList *list = NULL;
1761         GSList *done = NULL;
1762         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1763         
1764         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
1765                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
1766                 &&  list->data != node) {
1767                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
1768                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
1769                 }
1770         }
1771         for (list = done; list != NULL; list = g_slist_next(list)) {
1772                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
1773                                                                  list->data);
1774         }
1775         g_slist_free(done);
1776 }
1777
1778 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
1779                             FolderItem *to_folder)
1780 {
1781         FolderItem *from_parent = NULL;
1782         FolderItem *new_folder = NULL;
1783         GtkCTreeNode *src_node = NULL;
1784         gchar *buf;
1785         gint status;
1786
1787         g_return_if_fail(folderview != NULL);
1788         g_return_if_fail(from_folder != NULL);
1789         g_return_if_fail(to_folder != NULL);
1790
1791         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
1792         from_parent = folder_item_parent(from_folder);
1793         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
1794         STATUSBAR_PUSH(folderview->mainwin, buf);
1795         g_free(buf);
1796         summary_clear_all(folderview->summaryview);
1797         folderview->opened = NULL;
1798         folderview->selected = NULL;
1799         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
1800         inc_lock();
1801         main_window_cursor_wait(folderview->mainwin);
1802         statusbar_verbosity_set(TRUE);
1803         folder_item_update_freeze();
1804         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
1805                 statusbar_verbosity_set(FALSE);
1806                 main_window_cursor_normal(folderview->mainwin);
1807                 STATUSBAR_POP(folderview->mainwin);
1808                 folder_item_update_thaw();
1809                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
1810
1811                 folderview_sort_folders(folderview, 
1812                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
1813                                 NULL, folder_item_parent(new_folder)), new_folder->folder);
1814                 folderview_select(folderview, new_folder);
1815         } else {
1816                 statusbar_verbosity_set(FALSE);         
1817                 main_window_cursor_normal(folderview->mainwin);
1818                 STATUSBAR_POP(folderview->mainwin);
1819                 folder_item_update_thaw();
1820                 switch (status) {
1821                 case F_MOVE_FAILED_DEST_IS_PARENT:
1822                         alertpanel_error(_("Source and destination are the same."));
1823                         break;
1824                 case F_MOVE_FAILED_DEST_IS_CHILD:
1825                         alertpanel_error(_("Can't move a folder to one of its children."));
1826                         break;
1827                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
1828                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
1829                         break;
1830                 default:
1831                         alertpanel_error(_("Move failed!"));
1832                         break;
1833                 }
1834         }       
1835         inc_unlock();           
1836         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
1837 }
1838
1839 static gint folderview_clist_compare(GtkCList *clist,
1840                                      gconstpointer ptr1, gconstpointer ptr2)
1841 {
1842         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
1843         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
1844
1845         if (!item1->name)
1846                 return (item2->name != NULL);
1847         if (!item2->name)
1848                 return -1;
1849
1850         return g_utf8_collate(item1->name, item2->name);
1851 }
1852
1853 static void folderview_processing_cb(FolderView *folderview, guint action,
1854                                      GtkWidget *widget)
1855 {
1856         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1857         FolderItem *item;
1858
1859         if (!folderview->selected) return;
1860
1861         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1862         g_return_if_fail(item != NULL);
1863         g_return_if_fail(item->folder != NULL);
1864
1865         prefs_filtering_open(&item->prefs->processing,
1866                              _("Processing configuration"), NULL, NULL);
1867 }
1868
1869 void folderview_set_target_folder_color(gint color_op) 
1870 {
1871         gint firstone = 1;
1872         GList *list;
1873         FolderView *folderview;
1874
1875         for (list = folderview_list; list != NULL; list = list->next) {
1876                 folderview = (FolderView *)list->data;
1877                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
1878                 if (firstone) {
1879                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
1880                                 folderview->color_op;
1881                         firstone = 0;
1882                 }
1883         }
1884 }
1885
1886 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
1887 {
1888         folderview_init(folderview);
1889         folderview_set_all();
1890 }
1891
1892 static void drag_state_stop(FolderView *folderview)
1893 {
1894         if (folderview->drag_timer)
1895                 gtk_timeout_remove(folderview->drag_timer);
1896         folderview->drag_timer = 0;
1897         folderview->drag_node = NULL;
1898 }
1899
1900 static gint folderview_defer_expand(FolderView *folderview)
1901 {
1902         if (folderview->drag_node) {
1903                 folderview_recollapse_nodes(folderview, folderview->drag_node);
1904                 if (folderview->drag_item->collapsed) {
1905                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
1906                         folderview->nodes_to_recollapse = g_slist_append
1907                                 (folderview->nodes_to_recollapse, folderview->drag_node);
1908                 }
1909         }
1910         folderview->drag_item  = NULL;
1911         folderview->drag_timer = 0;
1912         return FALSE;
1913 }
1914
1915 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
1916 {
1917         /* the idea is that we call drag_state_start() whenever we want expansion to
1918          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
1919          * we need to call drag_state_stop() */
1920         drag_state_stop(folderview);
1921         /* request expansion */
1922         if (0 != (folderview->drag_timer = gtk_timeout_add
1923                         (prefs_common.hover_timeout, 
1924                          (GtkFunction)folderview_defer_expand,
1925                          folderview))) {
1926                 folderview->drag_node = node;
1927                 folderview->drag_item = item;
1928         }                        
1929 }
1930
1931 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
1932                                   FolderView       *folderview)
1933 {
1934         GdkDragContext *context;
1935
1936         g_return_if_fail(folderview != NULL);
1937         if (folderview->selected == NULL) return;
1938         if (folderview->nodes_to_recollapse) 
1939                 g_slist_free(folderview->nodes_to_recollapse);
1940         folderview->nodes_to_recollapse = NULL;
1941         context = gtk_drag_begin(widget, folderview->target_list,
1942                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
1943         gtk_drag_set_icon_default(context);
1944 }
1945
1946 static void folderview_drag_data_get(GtkWidget        *widget,
1947                                      GdkDragContext   *drag_context,
1948                                      GtkSelectionData *selection_data,
1949                                      guint             info,
1950                                      guint             time,
1951                                      FolderView       *folderview)
1952 {
1953         FolderItem *item;
1954         GList *cur;
1955         gchar *source = NULL;
1956
1957         for (cur = GTK_CLIST(folderview->ctree)->selection;
1958              cur != NULL; cur = cur->next) {
1959                 item = gtk_ctree_node_get_row_data
1960                         (GTK_CTREE(folderview->ctree), 
1961                          GTK_CTREE_NODE(cur->data));
1962                 if (item) {
1963                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
1964                         gtk_selection_data_set(selection_data,
1965                                                selection_data->target, 8,
1966                                                source, strlen(source));
1967                         break;
1968                 } else
1969                         return;
1970         }
1971 }
1972
1973 gboolean folderview_update_folder(gpointer source, gpointer userdata)
1974 {
1975         FolderUpdateData *hookdata;
1976         FolderView *folderview;
1977         GtkWidget *ctree;
1978
1979         hookdata = source;
1980         folderview = (FolderView *) userdata;   
1981         g_return_val_if_fail(hookdata != NULL, FALSE);
1982         g_return_val_if_fail(folderview != NULL, FALSE);
1983
1984         ctree = folderview->ctree;
1985         g_return_val_if_fail(ctree != NULL, FALSE);
1986
1987         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
1988                 folderview_create_folder_node(folderview, hookdata->item);
1989         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
1990                 GtkCTreeNode *node;
1991
1992                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
1993                 if (node != NULL)
1994                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
1995         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
1996                 folderview_set(folderview);
1997
1998         return FALSE;
1999 }
2000
2001 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2002                                           GdkDragContext *context,
2003                                           gint            x,
2004                                           gint            y,
2005                                           guint           time,
2006                                           FolderView     *folderview)
2007 {
2008         gint row, column;
2009         FolderItem *item, *src_item = NULL;
2010         GtkCTreeNode *node = NULL;
2011         gboolean acceptable = FALSE;
2012         gint height = folderview->ctree->allocation.height;
2013         gint total_height = folderview->ctree->requisition.height;
2014         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2015                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2016         gfloat vpos = pos->value;
2017
2018         if (gtk_clist_get_selection_info
2019                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2020                 GtkWidget *srcwidget;
2021
2022                 if (y > height - 24 && height + vpos < total_height)
2023                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2024
2025                 if (y < 24 && y > 0)
2026                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2027
2028                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2029                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2030                 src_item = folderview->summaryview->folder_item;
2031
2032                 srcwidget = gtk_drag_get_source_widget(context);
2033                 if (srcwidget == folderview->summaryview->ctree) {
2034                         /* comes from summaryview */
2035                         /* we are copying messages, so only accept folder items that are not
2036                            the source item, are no root items and can copy messages */
2037                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2038                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2039                                 acceptable = TRUE;
2040                 } else if (srcwidget == folderview->ctree) {
2041                         /* comes from folderview */
2042                         /* we are moving folder items, only accept folders that are not
2043                            the source items and can copy messages and create folder items */
2044                         if (item && item->folder && src_item && src_item != item &&
2045                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2046                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2047                                 acceptable = TRUE;
2048                 }
2049         }
2050
2051         if (acceptable || (src_item && src_item == item))
2052                 drag_state_start(folderview, node, item);
2053         
2054         if (acceptable) {
2055                 g_signal_handlers_block_by_func
2056                         (G_OBJECT(widget),
2057                          G_CALLBACK(folderview_selected), folderview);
2058                 gtk_ctree_select(GTK_CTREE(widget), node);
2059                 g_signal_handlers_unblock_by_func
2060                         (G_OBJECT(widget),
2061                          G_CALLBACK(folderview_selected), folderview);
2062                 gdk_drag_status(context, 
2063                                         (context->actions == GDK_ACTION_COPY ?
2064                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2065         } else {
2066                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2067                 gdk_drag_status(context, 0, time);
2068         }
2069
2070         return acceptable;
2071 }
2072
2073 static void folderview_drag_leave_cb(GtkWidget      *widget,
2074                                      GdkDragContext *context,
2075                                      guint           time,
2076                                      FolderView     *folderview)
2077 {
2078         drag_state_stop(folderview);
2079         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2080 }
2081
2082 static void folderview_drag_received_cb(GtkWidget        *widget,
2083                                         GdkDragContext   *drag_context,
2084                                         gint              x,
2085                                         gint              y,
2086                                         GtkSelectionData *data,
2087                                         guint             info,
2088                                         guint             time,
2089                                         FolderView       *folderview)
2090 {
2091         gint row, column;
2092         FolderItem *item, *src_item;
2093         GtkCTreeNode *node;
2094
2095         drag_state_stop(folderview);
2096         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2097                 /* comes from summaryview */
2098                 if (gtk_clist_get_selection_info
2099                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2100                         return;
2101
2102                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2103                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2104                 src_item = folderview->summaryview->folder_item;
2105                 
2106                 /* re-check (due to acceptable possibly set for folder moves */
2107                 if (!(item && item->folder && item->path &&
2108                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2109                         return;
2110                 }
2111                 if (item && src_item) {
2112                         switch (drag_context->action) {
2113                         case GDK_ACTION_COPY:
2114                                 summary_copy_selected_to(folderview->summaryview, item);
2115                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2116                                 break;
2117                         case GDK_ACTION_MOVE:
2118                         case GDK_ACTION_DEFAULT:
2119                         default:
2120                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2121                                         summary_copy_selected_to(folderview->summaryview, item);
2122                                 else
2123                                         summary_move_selected_to(folderview->summaryview, item);
2124                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2125                         }
2126                 } else
2127                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2128         } else {
2129                 /* comes from folderview */
2130                 char *source;
2131                 
2132                 source = data->data + 17;
2133                 if (gtk_clist_get_selection_info
2134                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2135                     || *source == 0) {
2136                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2137                         return;
2138                 }
2139                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2140                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2141                 src_item = folder_find_item_from_identifier(source);
2142
2143                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2144                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2145                         return;
2146                 }
2147
2148                 folderview_move_folder(folderview, src_item, item);
2149                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2150         }
2151         folderview->nodes_to_recollapse = NULL;
2152 }
2153
2154 static void folderview_drag_end_cb(GtkWidget        *widget, 
2155                                    GdkDragContext   *drag_context,
2156                                    FolderView       *folderview)
2157 {
2158         drag_state_stop(folderview);
2159         g_slist_free(folderview->nodes_to_recollapse);
2160         folderview->nodes_to_recollapse = NULL;
2161 }
2162
2163 void folderview_register_popup(FolderViewPopup *fpopup)
2164 {
2165         GList *folderviews;
2166
2167         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2168                 FolderView *folderview = folderviews->data;
2169                 GtkItemFactory *factory;
2170
2171                 factory = create_ifactory(folderview, fpopup);
2172                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2173         }       
2174         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2175 }
2176
2177 void folderview_unregister_popup(FolderViewPopup *fpopup)
2178 {
2179         GList *folderviews;
2180
2181         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2182                 FolderView *folderview = folderviews->data;
2183
2184                 g_hash_table_remove(folderview->popups, fpopup->klass);
2185         }       
2186         g_hash_table_remove(folderview_popups, fpopup->klass);
2187 }