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