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