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