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