db508ac478ecf0519adc991ebe3d8eceef5854eb
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkscrolledwindow.h>
27 #include <gtk/gtkctree.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtkclist.h>
30 #include <gtk/gtkstyle.h>
31 #include <gtk/gtksignal.h>
32 #include <gtk/gtkmain.h>
33 #include <gtk/gtkstatusbar.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkitemfactory.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 #include "main.h"
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "summary_search.h"
46 #include "inputdialog.h"
47 #include "manage_window.h"
48 #include "alertpanel.h"
49 #include "menu.h"
50 #include "stock_pixmap.h"
51 #include "procmsg.h"
52 #include "utils.h"
53 #include "gtkutils.h"
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56 #include "prefs_filtering.h"
57 #include "prefs_folder_item.h"
58 #include "account.h"
59 #include "folder.h"
60 #include "foldersel.h"
61 #include "inc.h"
62 #include "statusbar.h"
63 #include "hooks.h"
64 #include "folderutils.h"
65 #include "partial_download.h"
66 #include "prefs_folder_column.h"
67 #include "filtering.h"
68
69 #define COL_FOLDER_WIDTH        150
70 #define COL_NUM_WIDTH           32
71
72 static GList *folderview_list = NULL;
73
74 static GtkStyle *normal_style;
75 static GtkStyle *normal_color_style;
76 static GtkStyle *bold_style;
77 static GtkStyle *bold_color_style;
78 static GtkStyle *bold_tgtfold_style;
79
80 static GdkPixmap *inboxxpm;
81 static GdkBitmap *inboxxpmmask;
82 static GdkPixmap *inboxhrmxpm;
83 static GdkBitmap *inboxhrmxpmmask;
84 static GdkPixmap *inboxopenxpm;
85 static GdkBitmap *inboxopenxpmmask;
86 static GdkPixmap *inboxopenhrmxpm;
87 static GdkBitmap *inboxopenhrmxpmmask;
88 static GdkPixmap *outboxxpm;
89 static GdkBitmap *outboxxpmmask;
90 static GdkPixmap *outboxhrmxpm;
91 static GdkBitmap *outboxhrmxpmmask;
92 static GdkPixmap *outboxopenxpm;
93 static GdkBitmap *outboxopenxpmmask;
94 static GdkPixmap *outboxopenhrmxpm;
95 static GdkBitmap *outboxopenhrmxpmmask;
96 static GdkPixmap *folderxpm;
97 static GdkBitmap *folderxpmmask;
98 static GdkPixmap *folderhrmxpm;
99 static GdkBitmap *folderhrmxpmmask;
100 static GdkPixmap *folderopenxpm;
101 static GdkBitmap *folderopenxpmmask;
102 static GdkPixmap *folderopenhrmxpm;
103 static GdkBitmap *folderopenhrmxpmmask;
104 static GdkPixmap *trashopenxpm;
105 static GdkBitmap *trashopenxpmmask;
106 static GdkPixmap *trashopenhrmxpm;
107 static GdkBitmap *trashopenhrmxpmmask;
108 static GdkPixmap *trashxpm;
109 static GdkBitmap *trashxpmmask;
110 static GdkPixmap *trashhrmxpm;
111 static GdkBitmap *trashhrmxpmmask;
112 static GdkPixmap *queuexpm;
113 static GdkBitmap *queuexpmmask;
114 static GdkPixmap *queuehrmxpm;
115 static GdkBitmap *queuehrmxpmmask;
116 static GdkPixmap *queueopenxpm;
117 static GdkBitmap *queueopenxpmmask;
118 static GdkPixmap *queueopenhrmxpm;
119 static GdkBitmap *queueopenhrmxpmmask;
120 static GdkPixmap *draftsxpm;
121 static GdkBitmap *draftsxpmmask;
122 static GdkPixmap *draftsopenxpm;
123 static GdkBitmap *draftsopenxpmmask;
124 static GdkPixmap *noselectxpm;
125 static GdkBitmap *noselectxpmmask;
126
127 static GdkPixmap *m_inboxxpm;
128 static GdkBitmap *m_inboxxpmmask;
129 static GdkPixmap *m_inboxhrmxpm;
130 static GdkBitmap *m_inboxhrmxpmmask;
131 static GdkPixmap *m_inboxopenxpm;
132 static GdkBitmap *m_inboxopenxpmmask;
133 static GdkPixmap *m_inboxopenhrmxpm;
134 static GdkBitmap *m_inboxopenhrmxpmmask;
135 static GdkPixmap *m_outboxxpm;
136 static GdkBitmap *m_outboxxpmmask;
137 static GdkPixmap *m_outboxhrmxpm;
138 static GdkBitmap *m_outboxhrmxpmmask;
139 static GdkPixmap *m_outboxopenxpm;
140 static GdkBitmap *m_outboxopenxpmmask;
141 static GdkPixmap *m_outboxopenhrmxpm;
142 static GdkBitmap *m_outboxopenhrmxpmmask;
143 static GdkPixmap *m_folderxpm;
144 static GdkBitmap *m_folderxpmmask;
145 static GdkPixmap *m_folderhrmxpm;
146 static GdkBitmap *m_folderhrmxpmmask;
147 static GdkPixmap *m_folderopenxpm;
148 static GdkBitmap *m_folderopenxpmmask;
149 static GdkPixmap *m_folderopenhrmxpm;
150 static GdkBitmap *m_folderopenhrmxpmmask;
151 static GdkPixmap *m_trashopenxpm;
152 static GdkBitmap *m_trashopenxpmmask;
153 static GdkPixmap *m_trashopenhrmxpm;
154 static GdkBitmap *m_trashopenhrmxpmmask;
155 static GdkPixmap *m_trashxpm;
156 static GdkBitmap *m_trashxpmmask;
157 static GdkPixmap *m_trashhrmxpm;
158 static GdkBitmap *m_trashhrmxpmmask;
159 static GdkPixmap *m_queuexpm;
160 static GdkBitmap *m_queuexpmmask;
161 static GdkPixmap *m_queuehrmxpm;
162 static GdkBitmap *m_queuehrmxpmmask;
163 static GdkPixmap *m_queueopenxpm;
164 static GdkBitmap *m_queueopenxpmmask;
165 static GdkPixmap *m_queueopenhrmxpm;
166 static GdkBitmap *m_queueopenhrmxpmmask;
167 static GdkPixmap *m_draftsxpm;
168 static GdkBitmap *m_draftsxpmmask;
169 static GdkPixmap *m_draftsopenxpm;
170 static GdkBitmap *m_draftsopenxpmmask;
171
172 static GdkPixmap *newxpm;
173 static GdkBitmap *newxpmmask;
174 static GdkPixmap *unreadxpm;
175 static GdkBitmap *unreadxpmmask;
176 static GdkPixmap *readxpm;
177 static GdkBitmap *readxpmmask;
178
179
180 static void folderview_select_node       (FolderView    *folderview,
181                                           GtkCTreeNode  *node);
182 static void folderview_set_folders       (FolderView    *folderview);
183 static void folderview_sort_folders      (FolderView    *folderview,
184                                           GtkCTreeNode  *root,
185                                           Folder        *folder);
186 static void folderview_append_folder     (FolderView    *folderview,
187                                           Folder        *folder);
188 static void folderview_update_node       (FolderView    *folderview,
189                                           GtkCTreeNode  *node);
190
191 static gint folderview_clist_compare    (GtkCList       *clist,
192                                          gconstpointer   ptr1,
193                                          gconstpointer   ptr2);
194
195 /* callback functions */
196 static gboolean folderview_button_pressed       (GtkWidget      *ctree,
197                                                  GdkEventButton *event,
198                                                  FolderView     *folderview);
199 static gboolean folderview_button_released      (GtkWidget      *ctree,
200                                                  GdkEventButton *event,
201                                                  FolderView     *folderview);
202 static gboolean folderview_key_pressed  (GtkWidget      *widget,
203                                          GdkEventKey    *event,
204                                          FolderView     *folderview);
205 static void folderview_selected         (GtkCTree       *ctree,
206                                          GtkCTreeNode   *row,
207                                          gint            column,
208                                          FolderView     *folderview);
209 static void folderview_tree_expanded    (GtkCTree       *ctree,
210                                          GtkCTreeNode   *node,
211                                          FolderView     *folderview);
212 static void folderview_tree_collapsed   (GtkCTree       *ctree,
213                                          GtkCTreeNode   *node,
214                                          FolderView     *folderview);
215 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
216                                          FolderView     *folderview);
217 static void folderview_col_resized      (GtkCList       *clist,
218                                          gint            column,
219                                          gint            width,
220                                          FolderView     *folderview);
221
222 static void mark_all_read_cb            (FolderView    *folderview,
223                                          guint           action,
224                                          GtkWidget      *widget);
225
226 static void folderview_empty_trash_cb   (FolderView     *folderview,
227                                          guint           action,
228                                          GtkWidget      *widget);
229
230 static void folderview_search_cb        (FolderView     *folderview,
231                                          guint           action,
232                                          GtkWidget      *widget);
233
234 static void folderview_property_cb      (FolderView     *folderview,
235                                          guint           action,
236                                          GtkWidget      *widget);
237
238 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
239                                           GdkDragContext *context,
240                                           gint            x,
241                                           gint            y,
242                                           guint           time,
243                                           FolderView     *folderview);
244 static void folderview_drag_leave_cb     (GtkWidget        *widget,
245                                           GdkDragContext   *context,
246                                           guint             time,
247                                           FolderView       *folderview);
248 static void folderview_drag_received_cb  (GtkWidget        *widget,
249                                           GdkDragContext   *drag_context,
250                                           gint              x,
251                                           gint              y,
252                                           GtkSelectionData *data,
253                                           guint             info,
254                                           guint             time,
255                                           FolderView       *folderview);
256 static void folderview_start_drag        (GtkWidget *widget, gint button, GdkEvent *event,
257                                           FolderView       *folderview);
258 static void folderview_drag_data_get     (GtkWidget        *widget,
259                                           GdkDragContext   *drag_context,
260                                           GtkSelectionData *selection_data,
261                                           guint             info,
262                                           guint             time,
263                                           FolderView       *folderview);
264 static void folderview_drag_end_cb       (GtkWidget        *widget,
265                                           GdkDragContext   *drag_context,
266                                           FolderView       *folderview);
267
268 void folderview_create_folder_node       (FolderView       *folderview, 
269                                           FolderItem       *item);
270 gboolean folderview_update_folder        (gpointer          source,
271                                           gpointer          userdata);
272 gboolean folderview_update_item_claws    (gpointer          source,
273                                           gpointer          data);
274 static void folderview_processing_cb(FolderView *folderview, guint action,
275                                      GtkWidget *widget);
276
277 GHashTable *folderview_popups;
278
279 static GtkItemFactoryEntry folderview_common_popup_entries[] =
280 {
281         {N_("/Mark all re_ad"),         NULL, mark_all_read_cb, 0, NULL},
282         {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
283         {N_("/_Properties..."),         NULL, folderview_property_cb, 0, NULL},
284         {N_("/Process_ing..."),         NULL, folderview_processing_cb, 0, NULL},
285 };
286
287 static GtkItemFactoryEntry folder_view_trash_popup_entries[] = {
288         {N_("/------"),                 NULL, NULL, 0, "<Separator>"},
289         {N_("/Empty _trash..."),        NULL, folderview_empty_trash_cb, 0, NULL},
290 };
291
292
293 GtkTargetEntry folderview_drag_types[] =
294 {
295         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
296 };
297
298 void folderview_initialize(void)
299 {
300         FolderViewPopup *fpopup;
301         guint i, n_entries;
302         GSList *entries = NULL;
303
304         fpopup = g_new0(FolderViewPopup, 1);
305
306         n_entries = sizeof(folderview_common_popup_entries) /
307                 sizeof(folderview_common_popup_entries[0]);
308         for (i = 0; i < n_entries; i++)
309                 entries = g_slist_append(entries, &folderview_common_popup_entries[i]);
310
311         fpopup->klass = "common";
312         fpopup->path = "<CommonFolder>";
313         fpopup->entries = entries;
314         fpopup->set_sensitivity = NULL;
315
316         folderview_popups = g_hash_table_new(g_str_hash, g_str_equal);
317         g_hash_table_insert(folderview_popups, "common", fpopup);
318 }
319
320 static GtkItemFactory *create_ifactory(FolderView *folderview, FolderViewPopup *fpopup)
321 {
322         GSList *entries;
323         GtkItemFactory *factory;
324         FolderViewPopup *fpopup_common;
325         GtkWidget *popup;
326
327         factory = gtk_item_factory_new(GTK_TYPE_MENU, fpopup->path, NULL);
328         gtk_item_factory_set_translate_func(factory, menu_translate,
329                                             NULL, NULL);
330
331         for (entries = fpopup->entries; entries != NULL; entries = g_slist_next(entries))
332                 gtk_item_factory_create_item(factory, entries->data, folderview, 1);
333
334         fpopup_common = g_hash_table_lookup(folderview_popups, "common");
335         if (fpopup_common != fpopup)
336                 for (entries = fpopup_common->entries; entries != NULL; entries = g_slist_next(entries))
337                         gtk_item_factory_create_item(factory, entries->data, folderview, 1);
338
339         popup = gtk_item_factory_get_widget(factory, fpopup->path);
340         g_signal_connect(G_OBJECT(popup), "selection_done",
341                          G_CALLBACK(folderview_popup_close),
342                          folderview);
343
344         return factory;
345 }
346
347 static void create_ifactories(gpointer key, gpointer value, gpointer data)
348 {
349         FolderView *folderview = data;
350         FolderViewPopup *fpopup = value;
351         GtkItemFactory *factory;
352
353         factory = create_ifactory(folderview, fpopup);
354         g_hash_table_insert(folderview->popups, fpopup->klass, factory);
355 }
356
357 static void folderview_column_set_titles(FolderView *folderview)
358 {
359         GtkWidget *ctree = folderview->ctree;
360         GtkWidget *label_new;
361         GtkWidget *label_unread;
362         GtkWidget *label_total;
363         GtkWidget *hbox_new;
364         GtkWidget *hbox_unread;
365         GtkWidget *hbox_total;
366         gint *col_pos = folderview->col_pos;
367         
368         debug_print("setting titles...\n");
369         gtk_widget_realize(folderview->ctree);
370         gtk_widget_show_all(folderview->scrolledwin);
371         
372         /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
373          * instead text (text overflows making them unreadable and ugly) */
374         stock_pixmap_gdk(ctree, STOCK_PIXMAP_NEW,
375                          &newxpm, &newxpmmask);
376         stock_pixmap_gdk(ctree, STOCK_PIXMAP_UNREAD,
377                          &unreadxpm, &unreadxpmmask);
378         stock_pixmap_gdk(ctree, STOCK_PIXMAP_READ,
379                          &readxpm, &readxpmmask);
380                 
381         label_new = gtk_pixmap_new(newxpm, newxpmmask);
382         label_unread = gtk_pixmap_new(unreadxpm, unreadxpmmask);
383         label_total = gtk_pixmap_new(readxpm, readxpmmask);
384
385         hbox_new = gtk_hbox_new(FALSE, 4);
386         hbox_unread = gtk_hbox_new(FALSE, 4);
387         hbox_total = gtk_hbox_new(FALSE, 4);
388
389         /* left justified */
390         gtk_box_pack_start(GTK_BOX(hbox_new), label_new, TRUE, TRUE, 0);
391         gtk_misc_set_alignment (GTK_MISC (label_new), 1, 0.5);
392         gtk_box_pack_start(GTK_BOX(hbox_unread), label_unread, TRUE, TRUE, 0);
393         gtk_misc_set_alignment (GTK_MISC (label_unread), 1, 0.5);
394         gtk_box_pack_start(GTK_BOX(hbox_total), label_total, TRUE, TRUE, 0);
395         gtk_misc_set_alignment (GTK_MISC (label_total), 1, 0.5);
396
397         gtk_widget_show_all(hbox_new);
398         gtk_widget_show_all(hbox_unread);
399         gtk_widget_show_all(hbox_total);
400
401         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_NEW],hbox_new);
402         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_UNREAD],hbox_unread);
403         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_TOTAL],hbox_total);
404 }
405
406 GtkWidget *folderview_ctree_create(FolderView *folderview)
407 {
408         GtkWidget *ctree;
409         gint *col_pos;
410         FolderColumnState *col_state;
411         FolderColumnType type;
412         gchar *titles[N_FOLDER_COLS];
413         gint i;
414         GtkWidget *scrolledwin = folderview->scrolledwin;
415
416         debug_print("creating tree...\n");
417         memset(titles, 0, sizeof(titles));
418
419         col_state = prefs_folder_column_get_config();
420         memset(titles, 0, sizeof(titles));
421
422         col_pos = folderview->col_pos;
423
424         for (i = 0; i < N_FOLDER_COLS; i++) {
425                 folderview->col_state[i] = col_state[i];
426                 type = col_state[i].type;
427                 col_pos[type] = i;
428         }
429
430         titles[col_pos[F_COL_FOLDER]] = _("Folder");
431         titles[col_pos[F_COL_NEW]]    = _("New");
432         titles[col_pos[F_COL_UNREAD]] = _("Unread");
433         titles[col_pos[F_COL_TOTAL]]  = _("#");
434
435         ctree = gtk_sctree_new_with_titles(N_FOLDER_COLS, col_pos[F_COL_FOLDER],
436                                            titles);
437         
438         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
439         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[F_COL_NEW],
440                                            GTK_JUSTIFY_RIGHT);
441         gtk_clist_set_column_justification(GTK_CLIST(ctree),
442                                            col_pos[F_COL_UNREAD],
443                                            GTK_JUSTIFY_RIGHT);
444         gtk_clist_set_column_justification(GTK_CLIST(ctree), 
445                                            col_pos[F_COL_TOTAL],
446                                            GTK_JUSTIFY_RIGHT);
447         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
448         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
449                                      GTK_CTREE_EXPANDER_SQUARE);
450         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
451         gtk_clist_set_compare_func(GTK_CLIST(ctree), folderview_clist_compare);
452
453         /* don't let title buttons take key focus */
454         for (i = 0; i < N_FOLDER_COLS; i++) {
455                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
456                                        GTK_CAN_FOCUS);
457                 gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[i],
458                                    prefs_common.folder_col_size[i]);
459                 gtk_clist_set_column_visibility
460                         (GTK_CLIST(ctree), i, col_state[i].visible);
461         }
462
463         g_signal_connect(G_OBJECT(ctree), "key_press_event",
464                          G_CALLBACK(folderview_key_pressed),
465                          folderview);
466         g_signal_connect(G_OBJECT(ctree), "button_press_event",
467                          G_CALLBACK(folderview_button_pressed),
468                          folderview);
469         g_signal_connect(G_OBJECT(ctree), "button_release_event",
470                          G_CALLBACK(folderview_button_released),
471                          folderview);
472         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
473                          G_CALLBACK(folderview_selected), folderview);
474         g_signal_connect(G_OBJECT(ctree), "start_drag",
475                          G_CALLBACK(folderview_start_drag), folderview);
476         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
477                          G_CALLBACK(folderview_drag_data_get),
478                          folderview);
479
480         g_signal_connect_after(G_OBJECT(ctree), "tree_expand",
481                                G_CALLBACK(folderview_tree_expanded),
482                                folderview);
483         g_signal_connect_after(G_OBJECT(ctree), "tree_collapse",
484                                G_CALLBACK(folderview_tree_collapsed),
485                                folderview);
486
487         g_signal_connect(G_OBJECT(ctree), "resize_column",
488                          G_CALLBACK(folderview_col_resized),
489                          folderview);
490
491         /* drop callback */
492         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
493                           summary_drag_types, 1,
494                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
495         g_signal_connect(G_OBJECT(ctree), "drag_motion",
496                          G_CALLBACK(folderview_drag_motion_cb),
497                          folderview);
498         g_signal_connect(G_OBJECT(ctree), "drag_leave",
499                          G_CALLBACK(folderview_drag_leave_cb),
500                          folderview);
501         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
502                          G_CALLBACK(folderview_drag_received_cb),
503                          folderview);
504         g_signal_connect(G_OBJECT(ctree), "drag_end",
505                          G_CALLBACK(folderview_drag_end_cb),
506                          folderview);
507
508         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
509
510         return ctree;
511 }
512
513 void folderview_set_column_order(FolderView *folderview)
514 {
515         GtkWidget *ctree;
516         FolderItem *item = folderview_get_selected_item(folderview);
517         GtkWidget *scrolledwin = folderview->scrolledwin;
518
519         debug_print("recreating tree...\n");
520         gtk_widget_destroy(folderview->ctree);
521
522         folderview->ctree = ctree = folderview_ctree_create(folderview);
523         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
524                                             GTK_CLIST(ctree)->hadjustment);
525         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
526                                             GTK_CLIST(ctree)->vadjustment);
527         gtk_widget_show(ctree);
528         
529         folderview_set(folderview);
530         folderview_column_set_titles(folderview);
531
532         folderview_select(folderview,item);
533 }
534
535 FolderView *folderview_create(void)
536 {
537         FolderView *folderview;
538         GtkWidget *scrolledwin;
539         GtkWidget *ctree;
540
541         debug_print("Creating folder view...\n");
542         folderview = g_new0(FolderView, 1);
543
544         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
545         gtk_scrolled_window_set_policy
546                 (GTK_SCROLLED_WINDOW(scrolledwin),
547                  GTK_POLICY_AUTOMATIC,
548                  prefs_common.folderview_vscrollbar_policy);
549         gtk_widget_set_size_request(scrolledwin,
550                              prefs_common.folderview_width,
551                              prefs_common.folderview_height);
552
553         folderview->scrolledwin  = scrolledwin;
554         ctree = folderview_ctree_create(folderview);
555         
556         /* create popup factories */
557         folderview->popups = g_hash_table_new(g_str_hash, g_str_equal);
558         g_hash_table_foreach(folderview_popups, create_ifactories, folderview);
559
560         folderview->ctree        = ctree;
561
562         folderview->folder_update_callback_id =
563                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST, folderview_update_folder, (gpointer) folderview);
564         folderview->folder_item_update_callback_id =
565                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST, folderview_update_item_claws, (gpointer) folderview);
566
567         gtk_widget_show_all(scrolledwin);
568         
569         folderview->target_list = gtk_target_list_new(folderview_drag_types, 1);
570         folderview_list = g_list_append(folderview_list, folderview);
571
572         return folderview;
573 }
574
575 void folderview_init(FolderView *folderview)
576 {
577         GtkWidget *ctree = folderview->ctree;
578
579         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
580         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
581         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
582         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
583         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
584         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
585         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
586         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
587         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
588         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
589         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
590         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
591         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
592         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
593         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
594         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
595         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
596         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
597         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
598         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
599         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE, &draftsxpm, &draftsxpmmask);
600         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN, &draftsopenxpm, &draftsopenxpmmask);
601         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_NOSELECT, &noselectxpm, &noselectxpmmask);
602
603         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_MARK, &m_inboxxpm, &m_inboxxpmmask);
604         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM_MARK, &m_inboxhrmxpm, &m_inboxhrmxpmmask);
605         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_MARK, &m_inboxopenxpm, &m_inboxopenxpmmask);
606         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM_MARK, &m_inboxopenhrmxpm, &m_inboxopenhrmxpmmask);
607         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_MARK, &m_outboxxpm, &m_outboxxpmmask);
608         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM_MARK, &m_outboxhrmxpm, &m_outboxhrmxpmmask);
609         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_MARK, &m_outboxopenxpm, &m_outboxopenxpmmask);
610         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM_MARK, &m_outboxopenhrmxpm, &m_outboxopenhrmxpmmask);
611         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_MARK, &m_folderxpm, &m_folderxpmmask);
612         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM_MARK, &m_folderhrmxpm, &m_folderhrmxpmmask);
613         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_MARK, &m_folderopenxpm, &m_folderopenxpmmask);
614         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM_MARK, &m_folderopenhrmxpm, &m_folderopenhrmxpmmask);
615         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_MARK, &m_trashopenxpm, &m_trashopenxpmmask);
616         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM_MARK, &m_trashopenhrmxpm, &m_trashopenhrmxpmmask);
617         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_MARK, &m_trashxpm, &m_trashxpmmask);
618         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM_MARK, &m_trashhrmxpm, &m_trashhrmxpmmask);
619         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_MARK, &m_queuexpm, &m_queuexpmmask);
620         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM_MARK, &m_queuehrmxpm, &m_queuehrmxpmmask);
621         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_MARK, &m_queueopenxpm, &m_queueopenxpmmask);
622         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM_MARK, &m_queueopenhrmxpm, &m_queueopenhrmxpmmask);
623         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE_MARK, &m_draftsxpm, &m_draftsxpmmask);
624         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN_MARK, &m_draftsopenxpm, &m_draftsopenxpmmask);
625                         
626         if (!normal_style) {
627                 PangoFontDescription *font_desc;
628                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
629                 font_desc = pango_font_description_from_string(NORMAL_FONT);
630                 if (font_desc) {
631                         if (normal_style->font_desc)
632                                 pango_font_description_free
633                                         (normal_style->font_desc);
634                         normal_style->font_desc = font_desc;
635                 }
636                 normal_color_style = gtk_style_copy(normal_style);
637                 normal_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
638
639                 gtk_widget_set_style(ctree, normal_style);
640         }
641
642         if (!bold_style) {
643                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
644                 pango_font_description_set_weight
645                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
646                 bold_color_style = gtk_style_copy(bold_style);
647                 bold_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
648
649                 bold_tgtfold_style = gtk_style_copy(bold_style);
650                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
651         }
652 }
653
654 void folderview_set(FolderView *folderview)
655 {
656         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
657         MainWindow *mainwin = folderview->mainwin;
658
659         debug_print("Setting folder info...\n");
660         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
661
662         main_window_cursor_wait(mainwin);
663
664         folderview->selected = NULL;
665         folderview->opened = NULL;
666
667         gtk_clist_freeze(GTK_CLIST(ctree));
668         gtk_clist_clear(GTK_CLIST(ctree));
669         gtk_clist_thaw(GTK_CLIST(ctree));
670         gtk_clist_freeze(GTK_CLIST(ctree));
671
672         folderview_set_folders(folderview);
673
674         gtk_clist_thaw(GTK_CLIST(ctree));
675         main_window_cursor_normal(mainwin);
676         STATUSBAR_POP(mainwin);
677 }
678
679 void folderview_set_all(void)
680 {
681         GList *list;
682
683         for (list = folderview_list; list != NULL; list = list->next)
684                 folderview_set((FolderView *)list->data);
685 }
686
687 void folderview_select(FolderView *folderview, FolderItem *item)
688 {
689         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
690         GtkCTreeNode *node;
691         GtkCTreeNode *old_selected = folderview->selected;
692
693         if (!item) return;
694
695         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
696         if (node) folderview_select_node(folderview, node);
697
698         if (old_selected != node)
699                 folder_update_op_count();
700 }
701
702 static void mark_all_read_cb(FolderView *folderview, guint action,
703                              GtkWidget *widget)
704 {
705         FolderItem *item;
706         AlertValue val;
707         
708         item = folderview_get_selected_item(folderview);
709         if (item == NULL)
710                 return;
711
712         if (prefs_common.ask_mark_all_read) {
713                 val = alertpanel_full(_("Mark all as read"),
714                         _("Do you really want to mark all mails in this "
715                           "folder as read ?"), GTK_STOCK_YES, GTK_STOCK_NO, NULL,
716                           TRUE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
717
718                 if (val == G_ALERTALTERNATE ||
719                     val == (G_ALERTALTERNATE|G_ALERTDISABLE))
720                         return;
721                 else if (val == (G_ALERTDEFAULT|G_ALERTDISABLE)) 
722                         prefs_common.ask_mark_all_read = FALSE;
723         }
724         
725         summary_lock(folderview->summaryview);
726         folder_item_update_freeze();
727         if (folderview->summaryview->folder_item == item)
728                 gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
729         folderutils_mark_all_read(item);
730         if (folderview->summaryview->folder_item == item)
731                 gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
732         folder_item_update_thaw();
733         summary_unlock(folderview->summaryview);
734 }
735
736 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
737 {
738         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
739
740         g_return_if_fail(node != NULL);
741
742         folderview->open_folder = TRUE;
743         gtkut_ctree_set_focus_row(ctree, node);
744         gtk_ctree_select(ctree, node);
745         if (folderview->summaryview->folder_item &&
746             folderview->summaryview->folder_item->total_msgs > 0)
747                 gtk_widget_grab_focus(folderview->summaryview->ctree);
748         else
749                 gtk_widget_grab_focus(folderview->ctree);
750
751         gtkut_ctree_expand_parent_all(ctree, node);
752 }
753
754 void folderview_unselect(FolderView *folderview)
755 {
756         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
757                 gtk_ctree_collapse
758                         (GTK_CTREE(folderview->ctree), folderview->opened);
759
760         folderview->selected = folderview->opened = NULL;
761 }
762
763 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
764                                                  GtkCTreeNode *node)
765 {
766         FolderItem *item;
767
768         if (node)
769                 node = gtkut_ctree_node_next(ctree, node);
770         else
771                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
772
773         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
774                 item = gtk_ctree_node_get_row_data(ctree, node);
775                 if (item && item->unread_msgs > 0 && item->stype != F_TRASH)
776                         return node;
777         }
778
779         return NULL;
780 }
781
782 void folderview_select_next_unread(FolderView *folderview)
783 {
784         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
785         GtkCTreeNode *node = NULL;
786
787         if ((node = folderview_find_next_unread(ctree, folderview->opened))
788             != NULL) {
789                 folderview_select_node(folderview, node);
790                 return;
791         }
792
793         if (!folderview->opened ||
794             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
795                 return;
796         /* search again from the first node */
797         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
798                 folderview_select_node(folderview, node);
799 }
800
801 static GtkCTreeNode *folderview_find_next_new(GtkCTree *ctree,
802                                                  GtkCTreeNode *node)
803 {
804         FolderItem *item;
805
806         if (node)
807                 node = gtkut_ctree_node_next(ctree, node);
808         else
809                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
810
811         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
812                 item = gtk_ctree_node_get_row_data(ctree, node);
813                 if (item && item->new_msgs > 0 && item->stype != F_TRASH)
814                         return node;
815         }
816
817         return NULL;
818 }
819
820 void folderview_select_next_new(FolderView *folderview)
821 {
822         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
823         GtkCTreeNode *node = NULL;
824
825         if ((node = folderview_find_next_new(ctree, folderview->opened))
826             != NULL) {
827                 folderview_select_node(folderview, node);
828                 return;
829         }
830
831         if (!folderview->opened ||
832             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
833                 return;
834         /* search again from the first node */
835         if ((node = folderview_find_next_new(ctree, NULL)) != NULL)
836                 folderview_select_node(folderview, node);
837 }
838
839 FolderItem *folderview_get_selected_item(FolderView *folderview)
840 {
841         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
842
843         if (!folderview->selected) return NULL;
844         return gtk_ctree_node_get_row_data(ctree, folderview->selected);
845 }
846
847 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
848 {
849         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
850         static GtkCTreeNode *prev_row = NULL;
851         FolderItem *item;
852         gint new, unread, total;
853         gchar *new_str, *unread_str, *total_str;
854         gint *col_pos = folderview->col_pos;
855
856         if (!row) return;
857
858         item = gtk_ctree_node_get_row_data(ctree, row);
859         if (!item) return;
860
861         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_NEW], &new_str);
862         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_UNREAD], &unread_str);
863         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_TOTAL], &total_str);
864         new = atoi(new_str);
865         unread = atoi(unread_str);
866         total = atoi(total_str);
867
868         prev_row = row;
869
870         folderview_update_node(folderview, row);
871 }
872
873 void folderview_append_item(FolderItem *item)
874 {
875         GList *list;
876         
877         g_return_if_fail(item != NULL);
878         g_return_if_fail(item->folder != NULL);
879         if (folder_item_parent(item)) return;
880
881         for (list = folderview_list; list != NULL; list = list->next) {
882                 FolderView *folderview = (FolderView *)list->data;
883                 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
884                 GtkCTreeNode *node, *child;
885                 gint *col_pos = folderview->col_pos;
886
887                 node = gtk_ctree_find_by_row_data(ctree, NULL, 
888                                                   folder_item_parent(item));
889                 if (node) {
890                         child = gtk_ctree_find_by_row_data(ctree, node, item);
891                         if (!child) {
892                                 gchar *text[N_FOLDER_COLS] =
893                                         {NULL, "0", "0", "0"};
894
895                                 gtk_clist_freeze(GTK_CLIST(ctree));
896
897                                 text[col_pos[F_COL_FOLDER]] = item->name;
898                                 child = gtk_sctree_insert_node
899                                         (ctree, node, NULL, text,
900                                          FOLDER_SPACING,
901                                          folderxpm, folderxpmmask,
902                                          folderopenxpm, folderopenxpmmask,
903                                          FALSE, FALSE);
904                                 gtk_ctree_node_set_row_data(ctree, child, item);
905                                 gtk_ctree_expand(ctree, node);
906                                 folderview_update_node(folderview, child);
907                                 folderview_sort_folders(folderview, node,
908                                                         item->folder);
909
910                                 gtk_clist_thaw(GTK_CLIST(ctree));
911                         }
912                 }
913         }
914 }
915
916 static void folderview_set_folders(FolderView *folderview)
917 {
918         GList *list;
919
920         list = folder_get_list();
921
922         for (; list != NULL; list = list->next)
923                 folderview_append_folder(folderview, FOLDER(list->data));
924 }
925
926 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
927                                       gpointer data)
928 {
929         GList *list;
930
931         for (list = folderview_list; list != NULL; list = list->next) {
932                 FolderView *folderview = (FolderView *)list->data;
933                 MainWindow *mainwin = folderview->mainwin;
934                 gchar *str;
935
936                 if (item->path)
937                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
938                                               item->folder->name, G_DIR_SEPARATOR,
939                                               item->path);
940                 else
941                         str = g_strdup_printf(_("Scanning folder %s ..."),
942                                               item->folder->name);
943
944                 STATUSBAR_PUSH(mainwin, str);
945                 STATUSBAR_POP(mainwin);
946                 g_free(str);
947         }
948 }
949
950 static GtkWidget *label_window_create(const gchar *str)
951 {
952         GtkWidget *window;
953         GtkWidget *label;
954
955         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
956         gtk_widget_set_size_request(window, 380, 60);
957         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
958         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
959         gtk_window_set_title(GTK_WINDOW(window), str);
960         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
961         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
962         manage_window_set_transient(GTK_WINDOW(window));
963
964         label = gtk_label_new(str);
965         gtk_container_add(GTK_CONTAINER(window), label);
966         gtk_widget_show(label);
967
968         gtk_widget_show_now(window);
969
970         return window;
971 }
972
973 void folderview_rescan_tree(Folder *folder, gboolean rebuild)
974 {
975         GtkWidget *window;
976
977         g_return_if_fail(folder != NULL);
978
979         if (!folder->klass->scan_tree) return;
980
981         if (rebuild && 
982             alertpanel_full(_("Rebuild folder tree"), 
983                          _("Rebuilding the folder tree will remove "
984                            "local caches. Do you want to continue?"),
985                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
986                          NULL, ALERT_WARNING, G_ALERTALTERNATE) 
987                 != G_ALERTDEFAULT) {
988                 return;
989         }
990
991         inc_lock();
992         if (rebuild)
993                 window = label_window_create(_("Rebuilding folder tree..."));
994         else 
995                 window = label_window_create(_("Scanning folder tree..."));
996
997         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
998         folder_scan_tree(folder, rebuild);
999         folder_set_ui_func(folder, NULL, NULL);
1000
1001         folderview_set_all();
1002
1003         gtk_widget_destroy(window);
1004         inc_unlock();
1005 }
1006
1007 /** folderview_check_new()
1008  *  Scan and update the folder and return the 
1009  *  count the number of new messages since last check. 
1010  *  \param folder the folder to check for new messages
1011  *  \return the number of new messages since last check
1012  */
1013 gint folderview_check_new(Folder *folder)
1014 {
1015         GList *list;
1016         FolderItem *item;
1017         FolderView *folderview;
1018         GtkCTree *ctree;
1019         GtkCTreeNode *node;
1020         gint new_msgs = 0;
1021         gint former_new_msgs = 0;
1022         gint former_new = 0, former_unread = 0, former_total;
1023
1024         for (list = folderview_list; list != NULL; list = list->next) {
1025                 folderview = (FolderView *)list->data;
1026                 ctree = GTK_CTREE(folderview->ctree);
1027
1028                 inc_lock();
1029                 main_window_lock(folderview->mainwin);
1030
1031                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1032                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1033                         item = gtk_ctree_node_get_row_data(ctree, node);
1034                         if (!item || !item->path || !item->folder) continue;
1035                         if (item->no_select) continue;
1036                         if (folder && folder != item->folder) continue;
1037                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1038                         if (!item->prefs->newmailcheck) continue;
1039
1040                         folderview_scan_tree_func(item->folder, item, NULL);
1041                         former_new    = item->new_msgs;
1042                         former_unread = item->unread_msgs;
1043                         former_total  = item->total_msgs;
1044
1045                         if (folder_item_scan(item) < 0) {
1046                                 summaryview_unlock(folderview->summaryview, item);
1047                                 if (folder && !FOLDER_IS_LOCAL(folder))
1048                                         break;
1049                         }
1050
1051                         if (former_new    != item->new_msgs ||
1052                             former_unread != item->unread_msgs ||
1053                             former_total  != item->total_msgs)
1054                                 folderview_update_node(folderview, node);
1055
1056                         new_msgs += item->new_msgs;
1057                         former_new_msgs += former_new;
1058                 }
1059
1060                 main_window_unlock(folderview->mainwin);
1061                 inc_unlock();
1062         }
1063
1064         folder_write_list();
1065         /* Number of new messages since last check is the just the difference 
1066          * between former_new_msgs and new_msgs. If new_msgs is less than
1067          * former_new_msgs, that would mean another session accessed the folder
1068          * and the result is not well defined.
1069          */
1070         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1071         return new_msgs;
1072 }
1073
1074 void folderview_check_new_all(void)
1075 {
1076         GList *list;
1077         GtkWidget *window;
1078         FolderView *folderview;
1079
1080         folderview = (FolderView *)folderview_list->data;
1081
1082         inc_lock();
1083         main_window_lock(folderview->mainwin);
1084         window = label_window_create
1085                 (_("Checking for new messages in all folders..."));
1086
1087         list = folder_get_list();
1088         for (; list != NULL; list = list->next) {
1089                 Folder *folder = list->data;
1090
1091                 folderview_check_new(folder);
1092         }
1093
1094         folder_write_list();
1095         folderview_set_all();
1096
1097         gtk_widget_destroy(window);
1098         main_window_unlock(folderview->mainwin);
1099         inc_unlock();
1100 }
1101
1102 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1103                                                  FolderItem *item,
1104                                                  gboolean in_sub)
1105 {
1106         GNode *node = NULL;
1107         
1108         if (!item || !item->folder || !item->folder->node)
1109                 return FALSE;
1110                 
1111         node = item->folder->node;
1112         
1113         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1114         node = node->children;
1115
1116         if (in_sub &&
1117             (item->new_msgs > 0 ||
1118             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1119                 return TRUE;
1120         }
1121
1122         while (node != NULL) {
1123                 if (node && node->data) {
1124                         FolderItem *next_item = (FolderItem*) node->data;
1125                         node = node->next;
1126                         if (folderview_have_new_children_sub(folderview, 
1127                                                              next_item, TRUE))
1128                                 return TRUE;
1129                 }
1130         }
1131
1132         return FALSE;
1133 }
1134
1135 static gboolean folderview_have_new_children(FolderView *folderview,
1136                                              FolderItem *item)
1137 {
1138         return folderview_have_new_children_sub(folderview, item, FALSE);
1139 }
1140
1141 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1142                                                     FolderItem *item, 
1143                                                     gboolean in_sub)
1144 {
1145         GNode *node = NULL;
1146         
1147         if (!item || !item->folder || !item->folder->node)
1148                 return FALSE;
1149         
1150         node = item->folder->node;
1151         
1152         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1153         node = node->children;
1154
1155         if (in_sub &&
1156             (item->unread_msgs > 0 ||
1157             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1158                 return TRUE;
1159         }
1160
1161         while (node != NULL) {
1162                 if (node && node->data) {
1163                         FolderItem *next_item = (FolderItem*) node->data;
1164                         node = node->next;
1165                         if (folderview_have_unread_children_sub(folderview, 
1166                                                                 next_item, 
1167                                                                 TRUE))
1168                                 return TRUE;
1169                 }
1170         }
1171
1172         return FALSE;
1173 }
1174
1175 static gboolean folderview_have_unread_children(FolderView *folderview,
1176                                                 FolderItem *item)
1177 {
1178         return folderview_have_unread_children_sub(folderview, item, FALSE);
1179 }
1180
1181 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1182                                                       FolderItem *item,
1183                                                       gboolean in_sub)
1184 {
1185         GNode *node = NULL;
1186
1187         if (!item || !item->folder || !item->folder->node)
1188                 return FALSE;
1189
1190         node = item->folder->node;
1191         
1192         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1193         node = node->children;
1194
1195         if (in_sub && item->search_match){
1196                 return TRUE;
1197         }
1198
1199         while (node != NULL) {
1200                 if (node && node->data) {
1201                         FolderItem *next_item = (FolderItem*) node->data;
1202                         node = node->next;
1203                         if (folderview_have_matching_children_sub(folderview, 
1204                                                                   next_item, 
1205                                                                   TRUE))
1206                                 return TRUE;
1207                 }
1208         }
1209
1210         return FALSE;
1211 }
1212
1213 static gboolean folderview_have_matching_children(FolderView *folderview,
1214                                                   FolderItem *item)
1215 {
1216         return folderview_have_matching_children_sub(folderview, item, FALSE);
1217 }
1218
1219 static gboolean folderview_have_marked_children_sub(FolderView *folderview,
1220                                                     FolderItem *item,
1221                                                     gboolean in_sub)
1222 {
1223         GNode *node = NULL;
1224         
1225         if (!item || !item->folder || !item->folder->node)
1226                 return FALSE;
1227                 
1228         node = item->folder->node;
1229         
1230         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1231         node = node->children;
1232
1233         if (item->marked_msgs != 0) {
1234                 return TRUE;
1235         }
1236
1237         while (node != NULL) {
1238                 if (node && node->data) {
1239                         FolderItem *next_item = (FolderItem*) node->data;
1240                         node = node->next;
1241                         if (folderview_have_marked_children_sub(folderview,
1242                                                                 next_item, TRUE))
1243                                 return TRUE;
1244                 }
1245         }
1246
1247         return FALSE;
1248 }
1249
1250 static gboolean folderview_have_marked_children(FolderView *folderview,
1251                                              FolderItem *item)
1252 {
1253         return folderview_have_marked_children_sub(folderview, item, FALSE);
1254 }
1255
1256 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1257 {
1258         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1259         GtkStyle *style = NULL;
1260         GtkStyle *color_style = NULL;
1261         FolderItem *item;
1262         GdkPixmap *xpm, *openxpm;
1263         GdkBitmap *mask, *openmask;
1264         static GdkPixmap *searchicon;
1265         static GdkBitmap *searchmask;
1266         gboolean mark = FALSE;
1267         gchar *name;
1268         gchar *str;
1269         gboolean add_unread_mark;
1270         gboolean add_sub_match_mark;
1271         gboolean use_bold, use_color;
1272         gint *col_pos = folderview->col_pos;
1273         SpecialFolderItemType stype;
1274         
1275         item = gtk_ctree_node_get_row_data(ctree, node);
1276         g_return_if_fail(item != NULL);
1277
1278         if (!GTK_CTREE_ROW(node)->expanded)
1279                 mark = folderview_have_marked_children(folderview, item);
1280         else
1281                 mark = (item->marked_msgs != 0);
1282
1283         stype = item->stype;
1284         if (stype == F_NORMAL) {
1285                 if (folder_has_parent_of_type(item, F_TRASH))
1286                         stype = F_TRASH;
1287                 else if (folder_has_parent_of_type(item, F_DRAFT))
1288                         stype = F_DRAFT;
1289                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1290                         stype = F_OUTBOX;
1291                 else if (folder_has_parent_of_type(item, F_QUEUE))
1292                         stype = F_QUEUE;
1293         }
1294         switch (stype) {
1295         case F_INBOX:
1296                 if (item->hide_read_msgs) {
1297                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1298                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1299                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1300                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1301                 } else {
1302                         xpm = mark?m_inboxxpm:inboxxpm;
1303                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1304                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1305                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1306                 }
1307                 break;
1308         case F_OUTBOX:
1309                 if (item->hide_read_msgs) {
1310                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1311                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1312                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1313                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1314                 } else {
1315                         xpm = mark?m_outboxxpm:outboxxpm;
1316                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1317                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1318                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1319                 }
1320                 break;
1321         case F_QUEUE:
1322                 if (item->hide_read_msgs) {
1323                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1324                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1325                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1326                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1327                 } else {
1328                         xpm = mark?m_queuexpm:queuexpm;
1329                         mask = mark?m_queuexpmmask:queuexpmmask;
1330                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1331                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1332                 }
1333                 break;
1334         case F_TRASH:
1335                 if (item->hide_read_msgs) {
1336                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1337                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1338                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1339                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1340                 } else {
1341                         xpm = mark?m_trashxpm:trashxpm;
1342                         mask = mark?m_trashxpmmask:trashxpmmask;
1343                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1344                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1345                 }
1346                 break;
1347         case F_DRAFT:
1348                 xpm = mark?m_draftsxpm:draftsxpm;
1349                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1350                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1351                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1352                 break;
1353         default:
1354                 if (item->hide_read_msgs) {
1355                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1356                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1357                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1358                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1359                 } else {
1360                         xpm = mark?m_folderxpm:folderxpm;
1361                         mask = mark?m_folderxpmmask:folderxpmmask;
1362                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1363                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1364                 }
1365         }
1366         
1367         if (item->no_select) {
1368                 xpm = openxpm = noselectxpm;
1369                 mask = openmask = noselectxpmmask;
1370         }
1371
1372         name = folder_item_get_name(item);
1373
1374         if (!GTK_CTREE_ROW(node)->expanded) {
1375                 add_unread_mark = folderview_have_unread_children(
1376                                         folderview, item);
1377                 add_sub_match_mark = folderview_have_matching_children(
1378                                         folderview, item);
1379         } else {
1380                 add_unread_mark = FALSE;
1381                 add_sub_match_mark = FALSE;
1382         }
1383
1384         if (item->search_match) {
1385                 if (!searchicon) {
1386                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1387                          &searchicon, &searchmask);
1388                 }
1389                 xpm = openxpm = searchicon;
1390                 mask = openmask = searchmask;
1391         }
1392
1393         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1394             prefs_common.display_folder_unread) {
1395                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1396                                       add_unread_mark ? "+" : "");
1397                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1398                                         xpm, mask, openxpm, openmask,
1399                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1400                 g_free(str);
1401         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1402                     prefs_common.display_folder_unread) 
1403                    || add_sub_match_mark) {
1404
1405                 if (item->unread_msgs > 0)
1406                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1407                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1408                                               item->unreadmarked_msgs > 0 ? "!":"");
1409                 else
1410                         str = g_strdup_printf("%s (+)", name);
1411                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1412                                         xpm, mask, openxpm, openmask,
1413                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1414                 g_free(str);
1415         } else {
1416                 str = g_strdup_printf("%s%s", name, 
1417                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1418         
1419                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1420                                         xpm, mask, openxpm, openmask,
1421                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1422                 g_free(str);
1423         }
1424         g_free(name);
1425
1426         if (!folder_item_parent(item)) {
1427                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1428                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1429                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1430         } else {
1431                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1432                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1433                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1434         }
1435
1436         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1437             folder_has_parent_of_type(item, F_DRAFT) ||
1438             folder_has_parent_of_type(item, F_TRASH)) {
1439                 use_bold = use_color = FALSE;
1440         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1441                 /* highlight queue folder if there are any messages */
1442                 use_bold = use_color = (item->total_msgs > 0);
1443         } else {
1444                 /* if unread messages exist, print with bold font */
1445                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1446                                 || add_unread_mark;
1447                 /* if new messages exist, print with colored letter */
1448                 use_color =
1449                         (item->new_msgs > 0) ||
1450                         (add_unread_mark &&
1451                          folderview_have_new_children(folderview, item));       
1452         }
1453
1454         gtk_ctree_node_set_foreground(ctree, node, NULL);
1455
1456         if (use_bold) {
1457                 if (item->prefs->color > 0 && !use_color) {
1458                         GdkColor gdk_color;
1459
1460                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1461                         color_style = gtk_style_copy(bold_style);
1462                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1463                         style = color_style;
1464                 } else if (use_color)
1465                         style = bold_color_style;
1466                 else
1467                         style = bold_style;
1468                 if (item->op_count > 0) {
1469                         style = bold_tgtfold_style;
1470                 }
1471         } else if (use_color) {
1472                 style = normal_color_style;
1473                 gtk_ctree_node_set_foreground(ctree, node,
1474                                               &folderview->color_new);
1475         } else if (item->op_count > 0) {
1476                 style = bold_tgtfold_style;
1477         } else if (item->prefs->color > 0) {
1478                 GdkColor gdk_color;
1479                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1480                 color_style = gtk_style_copy(normal_style);
1481                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1482                 style = color_style;
1483         } else {
1484                 style = normal_style;
1485         }
1486
1487         gtk_ctree_node_set_row_style(ctree, node, style);
1488
1489         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1490                 folderview_update_node(folderview, node);
1491 }
1492
1493 #if !CLAWS /* keep it here for syncs */
1494 void folderview_update_item(FolderItem *item, gboolean update_summary)
1495 {
1496         GList *list;
1497         FolderView *folderview;
1498         GtkCTree *ctree;
1499         GtkCTreeNode *node;
1500
1501         g_return_if_fail(item != NULL);
1502
1503         for (list = folderview_list; list != NULL; list = list->next) {
1504                 folderview = (FolderView *)list->data;
1505                 ctree = GTK_CTREE(folderview->ctree);
1506
1507                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1508                 if (node) {
1509                         folderview_update_node(folderview, node);
1510                         if (update_summary && folderview->opened == node)
1511                                 summary_show(folderview->summaryview,
1512                                              item, FALSE);
1513                 }
1514         }
1515 }
1516 #endif
1517
1518 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1519 {
1520         GList *list;
1521         FolderView *folderview;
1522         GtkCTree *ctree;
1523         GtkCTreeNode *node;
1524
1525         g_return_if_fail(item != NULL);
1526
1527         for (list = folderview_list; list != NULL; list = list->next) {
1528                 folderview = (FolderView *)list->data;
1529                 ctree = GTK_CTREE(folderview->ctree);
1530
1531                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1532                 if (node) {
1533                         item->search_match = matches;
1534                         folderview_update_node(folderview, node);
1535                 }
1536         }
1537 }
1538
1539 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1540 {
1541         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1542         FolderView *folderview = (FolderView *)data;
1543         GtkCTree *ctree;
1544         GtkCTreeNode *node;
1545         g_return_val_if_fail(update_info != NULL, TRUE);
1546         g_return_val_if_fail(update_info->item != NULL, TRUE);
1547         g_return_val_if_fail(folderview != NULL, FALSE);
1548
1549         ctree = GTK_CTREE(folderview->ctree);
1550
1551         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1552         if (node) {
1553                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1554                         folderview_update_node(folderview, node);
1555                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1556                         summary_show(folderview->summaryview, update_info->item);
1557         }
1558         
1559         return FALSE;
1560 }
1561
1562 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1563                                                 gpointer data)
1564 {
1565         /* CLAWS: share this joy with other hook functions ... */
1566         folder_item_update((FolderItem *)key, 
1567                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1568 }
1569
1570 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1571 {
1572         GList *list;
1573         FolderItemUpdateFlags flags;
1574         
1575         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1576                 : 0;
1577         for (list = folderview_list; list != NULL; list = list->next)
1578                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1579                                      GINT_TO_POINTER(flags));
1580 }
1581
1582 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1583                                       GNode *gnode, GtkCTreeNode *cnode,
1584                                       gpointer data)
1585 {
1586         FolderView *folderview = (FolderView *)data;
1587         FolderItem *item = FOLDER_ITEM(gnode->data);
1588
1589         g_return_val_if_fail(item != NULL, FALSE);
1590
1591         gtk_ctree_node_set_row_data(ctree, cnode, item);
1592         folderview_update_node(folderview, cnode);
1593
1594         return TRUE;
1595 }
1596
1597 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1598                                    gpointer data)
1599 {
1600         FolderView *folderview = (FolderView *)data;
1601         FolderItem *item;
1602
1603         if (GTK_CTREE_ROW(node)->children) {
1604                 item = gtk_ctree_node_get_row_data(ctree, node);
1605                 g_return_if_fail(item != NULL);
1606
1607                 if (!item->collapsed)
1608                         gtk_ctree_expand(ctree, node);
1609                 else
1610                         folderview_update_node(folderview, node);
1611         }
1612 }
1613
1614 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1615                                GtkCTreeNode *root, GtkCTreeNode **prev)
1616 {
1617         if (item) {
1618                 GtkCTreeNode *node, *parent, *sibling;
1619
1620                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1621                 if (!node)
1622                         g_warning("%s not found.\n", item->path);
1623                 else {
1624                         parent = GTK_CTREE_ROW(node)->parent;
1625                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1626                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1627                         else
1628                                 sibling = GTK_CTREE_ROW(parent)->children;
1629                         while (sibling) {
1630                                 FolderItem *tmp;
1631
1632                                 tmp = gtk_ctree_node_get_row_data
1633                                         (ctree, sibling);
1634                                 if (tmp->stype != F_NORMAL)
1635                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1636                                 else
1637                                         break;
1638                         }
1639                         if (node != sibling)
1640                                 gtk_ctree_move(ctree, node, parent, sibling);
1641                 }
1642
1643                 *prev = node;
1644         }
1645 }
1646
1647 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1648                                     Folder *folder)
1649 {
1650         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1651         GtkCTreeNode *prev = NULL;
1652
1653         gtk_sctree_sort_recursive(ctree, root);
1654
1655         if (root && GTK_CTREE_ROW(root)->parent) return;
1656
1657         set_special_folder(ctree, folder->inbox, root, &prev);
1658         set_special_folder(ctree, folder->outbox, root, &prev);
1659         set_special_folder(ctree, folder->draft, root, &prev);
1660         set_special_folder(ctree, folder->queue, root, &prev);
1661         set_special_folder(ctree, folder->trash, root, &prev);
1662 }
1663
1664 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1665 {
1666         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1667         GtkCTreeNode *root;
1668
1669         g_return_if_fail(folder != NULL);
1670
1671         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1672                                       folderview_gnode_func, folderview);
1673         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1674                                 folderview);
1675         folderview_sort_folders(folderview, root, folder);
1676 }
1677
1678 /* callback functions */
1679
1680 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1681                                           FolderView *folderview)
1682 {
1683         GtkCList *clist = GTK_CLIST(ctree);
1684         gint prev_row = -1, row = -1, column = -1;
1685         FolderItem *item;
1686         Folder *folder;
1687         FolderViewPopup *fpopup;
1688         GtkItemFactory *fpopup_factory;
1689         GtkWidget *popup;
1690         FolderItem *special_trash = NULL;
1691         PrefsAccount *ac;
1692
1693         if (!event) return FALSE;
1694
1695         if (event->button == 1) {
1696                 folderview->open_folder = TRUE;
1697
1698                 if (event->type == GDK_2BUTTON_PRESS) {
1699                         if (clist->selection) {
1700                                 GtkCTreeNode *node;
1701
1702                                 node = GTK_CTREE_NODE(clist->selection->data);
1703                                 if (node)
1704                                         gtk_ctree_toggle_expansion(
1705                                                 GTK_CTREE(ctree),
1706                                                 node);
1707                         }
1708                 }
1709                 return FALSE;
1710         }
1711
1712         if (event->button == 2 || event->button == 3) {
1713                 /* right clicked */
1714                 if (clist->selection) {
1715                         GtkCTreeNode *node;
1716
1717                         node = GTK_CTREE_NODE(clist->selection->data);
1718                         if (node)
1719                                 prev_row = gtkut_ctree_get_nth_from_node
1720                                         (GTK_CTREE(ctree), node);
1721                 }
1722
1723                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1724                                                   &row, &column))
1725                         return FALSE;
1726                 if (prev_row != row) {
1727                         gtk_clist_unselect_all(clist);
1728                         if (event->button == 2)
1729                                 folderview_select_node
1730                                         (folderview,
1731                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1732                                                             row));
1733                         else
1734                                 gtk_clist_select_row(clist, row, column);
1735                 }
1736         }
1737
1738         if (event->button != 3) return FALSE;
1739
1740         item = gtk_clist_get_row_data(clist, row);
1741         g_return_val_if_fail(item != NULL, FALSE);
1742         g_return_val_if_fail(item->folder != NULL, FALSE);
1743         folder = item->folder;
1744
1745         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1746         if (fpopup != NULL)
1747                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1748         else {
1749                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1750                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1751         }
1752
1753         if (fpopup->set_sensitivity != NULL)
1754                 fpopup->set_sensitivity(fpopup_factory, item);
1755
1756         if (NULL != (ac = account_find_from_item(item)))
1757                 special_trash = account_get_special_folder(ac, F_TRASH);
1758
1759         if ((item == folder->trash || item == special_trash
1760              || folder_has_parent_of_type(item, F_TRASH)) &&
1761             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1762                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1763                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1764         } else if (item != folder->trash && (special_trash == NULL || item != special_trash)
1765                 && !folder_has_parent_of_type(item, F_TRASH)) {
1766                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1767                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1768         }
1769         
1770 #define SET_SENS(name, sens) \
1771         menu_set_sensitive(fpopup_factory, name, sens)
1772
1773         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1774         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1775                  folderview->selected == folderview->opened);
1776         SET_SENS("/Properties...", item->node->parent != NULL);
1777         SET_SENS("/Processing...", item->node->parent != NULL);
1778         if (item == folder->trash || item == special_trash
1779             || folder_has_parent_of_type(item, F_TRASH)) {
1780                 GSList *msglist = folder_item_get_msg_list(item);
1781                 SET_SENS("/Empty trash...", msglist != NULL);
1782                 procmsg_msg_list_free(msglist);
1783         }
1784 #undef SET_SENS
1785
1786         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1787         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1788                        event->button, event->time);
1789
1790         return FALSE;
1791 }
1792
1793 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1794                                            FolderView *folderview)
1795 {
1796         if (!event) return FALSE;
1797
1798         if (event->button == 1 && folderview->open_folder == FALSE &&
1799             folderview->opened != NULL) {
1800                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1801                                           folderview->opened);
1802                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1803         }
1804
1805         return FALSE;
1806 }
1807
1808 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1809                                        FolderView *folderview)
1810 {
1811         if (!event) return FALSE;
1812
1813         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1814                 return FALSE;
1815
1816         switch (event->keyval) {
1817         case GDK_Return:
1818                 if (folderview->selected) {
1819                         folderview_select_node(folderview,
1820                                                folderview->selected);
1821                 }
1822                 break;
1823         case GDK_space:
1824                 if (folderview->selected) {
1825                         if (folderview->opened == folderview->selected &&
1826                             (!folderview->summaryview->folder_item ||
1827                              folderview->summaryview->folder_item->total_msgs == 0))
1828                                 folderview_select_next_unread(folderview);
1829                         else
1830                                 folderview_select_node(folderview,
1831                                                        folderview->selected);
1832                 }
1833                 break;
1834         default:
1835                 break;
1836         }
1837
1838         return FALSE;
1839 }
1840
1841 static void summary_freeze_for_proc(gpointer data)
1842 {
1843         FolderView *folderview = (FolderView *)data;
1844         debug_print("freezing during processing...\n");
1845         gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
1846 }
1847
1848 static void summary_thaw_for_proc(gpointer data)
1849 {
1850         FolderView *folderview = (FolderView *)data;
1851         debug_print("thawing after processing\n");
1852         gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
1853 }
1854
1855 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1856                                 gint column, FolderView *folderview)
1857 {
1858         static gboolean can_select = TRUE;      /* exclusive lock */
1859         gboolean opened;
1860         FolderItem *item;
1861         gchar *buf;
1862
1863         folderview->selected = row;
1864
1865         if (folderview->opened == row) {
1866                 folderview->open_folder = FALSE;
1867                 return;
1868         }
1869         
1870         if (!can_select || summary_is_locked(folderview->summaryview)) {
1871                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1872                 gtk_ctree_select(ctree, folderview->opened);
1873                 return;
1874         }
1875
1876         if (!folderview->open_folder) return;
1877
1878         item = gtk_ctree_node_get_row_data(ctree, row);
1879         if (!item || item->no_select) return;
1880
1881         can_select = FALSE;
1882
1883         /* Save cache for old folder */
1884         /* We don't want to lose all caches if sylpheed crashed */
1885         if (folderview->opened) {
1886                 FolderItem *olditem;
1887                 
1888                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1889                 if (olditem) {
1890                         /* will be null if we just moved the previously opened folder */
1891                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1892                         summary_show(folderview->summaryview, NULL);
1893                         folder_item_close(olditem);
1894                 }
1895         }
1896
1897         /* CLAWS: set compose button type: news folder items 
1898          * always have a news folder as parent */
1899         if (item->folder) 
1900                 toolbar_set_compose_button
1901                         (folderview->mainwin->toolbar,
1902                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1903                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1904
1905         if (item->path)
1906                 debug_print("Folder %s is selected\n", item->path);
1907
1908         if (!GTK_CTREE_ROW(row)->children)
1909                 gtk_ctree_expand(ctree, row);
1910         if (folderview->opened &&
1911             !GTK_CTREE_ROW(folderview->opened)->children)
1912                 gtk_ctree_collapse(ctree, folderview->opened);
1913
1914         /* ungrab the mouse event */
1915         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1916                 gtk_grab_remove(GTK_WIDGET(ctree));
1917                 if (gdk_pointer_is_grabbed())
1918                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1919         }
1920
1921         /* Open Folder */
1922         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1923                                         item->path : "(null)");
1924         debug_print("%s\n", buf);
1925         STATUSBAR_PUSH(folderview->mainwin, buf);
1926         g_free(buf);
1927
1928         main_window_cursor_wait(folderview->mainwin);
1929
1930         if (folder_item_open(item) != 0) {
1931                 main_window_cursor_normal(folderview->mainwin);
1932                 STATUSBAR_POP(folderview->mainwin);
1933
1934                 alertpanel_error(_("Folder could not be opened."));
1935
1936                 folderview->open_folder = FALSE;
1937                 can_select = TRUE;
1938
1939                 return;
1940         }
1941         
1942         main_window_cursor_normal(folderview->mainwin);
1943
1944         /* Show messages */
1945         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1946         opened = summary_show(folderview->summaryview, item);
1947         
1948         folder_clean_cache_memory();
1949
1950         if (!opened) {
1951                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1952                 gtk_ctree_select(ctree, folderview->opened);
1953         } else {
1954                 folderview->opened = row;
1955                 if (gtk_ctree_node_is_visible(ctree, row)
1956                     != GTK_VISIBILITY_FULL)
1957                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1958         }
1959
1960         STATUSBAR_POP(folderview->mainwin);
1961
1962         folderview->open_folder = FALSE;
1963         can_select = TRUE;
1964 }
1965
1966 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1967                                      FolderView *folderview)
1968 {
1969         FolderItem *item;
1970
1971         item = gtk_ctree_node_get_row_data(ctree, node);
1972         g_return_if_fail(item != NULL);
1973         item->collapsed = FALSE;
1974         folderview_update_node(folderview, node);
1975 }
1976
1977 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1978                                       FolderView *folderview)
1979 {
1980         FolderItem *item;
1981
1982         item = gtk_ctree_node_get_row_data(ctree, node);
1983         g_return_if_fail(item != NULL);
1984         item->collapsed = TRUE;
1985         folderview_update_node(folderview, node);
1986 }
1987
1988 static void folderview_popup_close(GtkMenuShell *menu_shell,
1989                                    FolderView *folderview)
1990 {
1991         if (!folderview->opened) return;
1992
1993         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1994 }
1995
1996 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1997                                    FolderView *folderview)
1998 {
1999         FolderColumnType type = folderview->col_state[column].type;
2000
2001         prefs_common.folder_col_size[type] = width;
2002 }
2003
2004 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
2005 {
2006         GNode *srcnode;
2007
2008         folderview_create_folder_node(folderview, item);
2009
2010         if (!item || !item->folder || !item->folder->node)
2011                 return;
2012
2013         srcnode = item->folder->node;   
2014         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2015         srcnode = srcnode->children;
2016         while (srcnode != NULL) {
2017                 if (srcnode && srcnode->data) {
2018                         FolderItem *next_item = (FolderItem*) srcnode->data;
2019                         folderview_create_folder_node_recursive(folderview, next_item);
2020                 }
2021                 srcnode = srcnode->next;
2022         }
2023 }
2024
2025 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2026 {
2027         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2028         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2029         GtkCTreeNode *node, *parent_node;
2030         gint *col_pos = folderview->col_pos;
2031         FolderItemUpdateData hookdata;
2032
2033         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2034         if (parent_node == NULL)
2035                 return;
2036
2037         gtk_clist_freeze(GTK_CLIST(ctree));
2038
2039         text[col_pos[F_COL_FOLDER]] = item->name;
2040         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2041                                      FOLDER_SPACING,
2042                                      folderxpm, folderxpmmask,
2043                                      folderopenxpm, folderopenxpmmask,
2044                                      FALSE, FALSE);
2045         gtk_ctree_expand(ctree, parent_node);
2046         gtk_ctree_node_set_row_data(ctree, node, item);
2047         if (normal_style)
2048                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
2049         folderview_sort_folders(folderview, parent_node, item->folder);
2050
2051         hookdata.item = item;
2052         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2053         hookdata.msg = NULL;
2054         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2055
2056         gtk_clist_thaw(GTK_CLIST(ctree));
2057 }
2058
2059 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
2060                                       GtkWidget *widget)
2061 {
2062         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2063         FolderItem *item;
2064         GSList *mlist = NULL;
2065         GSList *cur = NULL;
2066         FolderItem *special_trash = NULL;
2067         PrefsAccount *ac;
2068
2069         if (!folderview->selected) return;
2070         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2071         g_return_if_fail(item != NULL);
2072         g_return_if_fail(item->folder != NULL);
2073
2074         if (NULL != (ac = account_find_from_item(item)))
2075                 special_trash = account_get_special_folder(ac, F_TRASH);
2076
2077         if (item != item->folder->trash && item != special_trash
2078         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2079         
2080         if (prefs_common.ask_on_clean) {
2081                 if (alertpanel(_("Empty trash"),
2082                                _("Delete all messages in trash?"),
2083                                GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
2084                         return;
2085         }
2086         
2087         mlist = folder_item_get_msg_list(item);
2088         
2089         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2090                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2091                 if (MSG_IS_LOCKED(msginfo->flags))
2092                         continue;
2093                 /* is it partially received? (partial_recv isn't cached) */
2094                 if (msginfo->total_size != 0 && 
2095                     msginfo->size != (off_t)msginfo->total_size)
2096                         partial_mark_for_delete(msginfo);
2097         }
2098         procmsg_msg_list_free(mlist);
2099
2100         folder_item_remove_all_msg(item);
2101 }
2102
2103 static void folderview_search_cb(FolderView *folderview, guint action,
2104                                  GtkWidget *widget)
2105 {
2106         summary_search(folderview->summaryview);
2107 }
2108
2109 static void folderview_property_cb(FolderView *folderview, guint action,
2110                                    GtkWidget *widget)
2111 {
2112         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2113         FolderItem *item;
2114
2115         if (!folderview->selected) return;
2116
2117         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2118         g_return_if_fail(item != NULL);
2119         g_return_if_fail(item->folder != NULL);
2120
2121         if (folder_item_parent(item) == NULL)
2122                 return;
2123
2124         prefs_folder_item_open(item);
2125 }
2126
2127 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2128 {
2129         GSList *list = NULL;
2130         GSList *done = NULL;
2131         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2132         
2133         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2134                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2135                 &&  list->data != node) {
2136                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2137                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2138                 }
2139         }
2140         for (list = done; list != NULL; list = g_slist_next(list)) {
2141                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2142                                                                  list->data);
2143         }
2144         g_slist_free(done);
2145 }
2146
2147 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2148                             FolderItem *to_folder)
2149 {
2150         FolderItem *from_parent = NULL;
2151         FolderItem *new_folder = NULL;
2152         GtkCTreeNode *src_node = NULL;
2153         gchar *buf;
2154         gint status;
2155
2156         g_return_if_fail(folderview != NULL);
2157         g_return_if_fail(from_folder != NULL);
2158         g_return_if_fail(to_folder != NULL);
2159
2160         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2161         from_parent = folder_item_parent(from_folder);
2162         
2163         if (prefs_common.warn_dnd) {
2164                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2165                                         "sub-folder of '%s' ?"), from_folder->name,
2166                                         to_folder->name);
2167                 status = alertpanel_full(_("Move folder"), buf,
2168                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, TRUE,
2169                                          NULL, ALERT_QUESTION, G_ALERTALTERNATE);
2170                 g_free(buf);
2171
2172                 if (status != G_ALERTDEFAULT
2173                  && status != (G_ALERTDEFAULT | G_ALERTDISABLE))
2174                         return;
2175                 if (status & G_ALERTDISABLE)
2176                         prefs_common.warn_dnd = FALSE;
2177         }
2178
2179         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2180         STATUSBAR_PUSH(folderview->mainwin, buf);
2181         g_free(buf);
2182         summary_clear_all(folderview->summaryview);
2183         folderview->opened = NULL;
2184         folderview->selected = NULL;
2185         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2186         inc_lock();
2187         main_window_cursor_wait(folderview->mainwin);
2188         statusbar_verbosity_set(TRUE);
2189         folder_item_update_freeze();
2190         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2191                 statusbar_verbosity_set(FALSE);
2192                 main_window_cursor_normal(folderview->mainwin);
2193                 STATUSBAR_POP(folderview->mainwin);
2194                 folder_item_update_thaw();
2195                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2196
2197                 folderview_sort_folders(folderview, 
2198                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2199                                 NULL, to_folder), new_folder->folder);
2200                 folderview_select(folderview, new_folder);
2201         } else {
2202                 statusbar_verbosity_set(FALSE);         
2203                 main_window_cursor_normal(folderview->mainwin);
2204                 STATUSBAR_POP(folderview->mainwin);
2205                 folder_item_update_thaw();
2206                 switch (status) {
2207                 case F_MOVE_FAILED_DEST_IS_PARENT:
2208                         alertpanel_error(_("Source and destination are the same."));
2209                         break;
2210                 case F_MOVE_FAILED_DEST_IS_CHILD:
2211                         alertpanel_error(_("Can't move a folder to one of its children."));
2212                         break;
2213                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2214                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2215                         break;
2216                 default:
2217                         alertpanel_error(_("Move failed!"));
2218                         break;
2219                 }
2220         }       
2221         inc_unlock();           
2222         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2223 }
2224
2225 static gint folderview_clist_compare(GtkCList *clist,
2226                                      gconstpointer ptr1, gconstpointer ptr2)
2227 {
2228         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2229         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2230
2231         if (!item1->name)
2232                 return (item2->name != NULL);
2233         if (!item2->name)
2234                 return -1;
2235
2236         return g_utf8_collate(item1->name, item2->name);
2237 }
2238
2239 static void folderview_processing_cb(FolderView *folderview, guint action,
2240                                      GtkWidget *widget)
2241 {
2242         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2243         FolderItem *item;
2244         gchar *id, *title;
2245
2246         if (!folderview->selected) return;
2247
2248         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2249         g_return_if_fail(item != NULL);
2250         g_return_if_fail(item->folder != NULL);
2251
2252         id = folder_item_get_identifier(item);
2253         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2254         g_free (id);
2255
2256         prefs_filtering_open(&item->prefs->processing, title, NULL, NULL);
2257         g_free (title);
2258 }
2259
2260 void folderview_set_target_folder_color(gint color_op) 
2261 {
2262         gint firstone = 1;
2263         GList *list;
2264         FolderView *folderview;
2265
2266         for (list = folderview_list; list != NULL; list = list->next) {
2267                 folderview = (FolderView *)list->data;
2268                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2269                 if (firstone) {
2270                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2271                                 folderview->color_op;
2272                         firstone = 0;
2273                 }
2274         }
2275 }
2276
2277 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2278 {
2279 }
2280
2281 void folderview_reflect_prefs(void)
2282 {
2283         static gchar *last_font = NULL;
2284         gboolean update_font = TRUE;
2285         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2286         FolderItem *item = folderview_get_selected_item(folderview);
2287         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2288                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2289         gint height = pos->value;
2290
2291         if (last_font && !strcmp(last_font, NORMAL_FONT))
2292                 update_font = FALSE;
2293
2294         if (last_font)
2295                 g_free(last_font);
2296         
2297         last_font = g_strdup(NORMAL_FONT);
2298
2299         if (update_font) {              
2300                 normal_style = normal_color_style = bold_style = 
2301                         bold_color_style = bold_tgtfold_style = NULL;
2302
2303                 folderview_init(folderview);
2304         }
2305         gtk_clist_freeze(GTK_CLIST(folderview->ctree));
2306         folderview_column_set_titles(folderview);
2307         folderview_set_all();
2308
2309         g_signal_handlers_block_by_func
2310                 (G_OBJECT(folderview->ctree),
2311                  G_CALLBACK(folderview_selected), folderview);
2312
2313         if (item)
2314                 folderview_select(folderview, item);
2315
2316         g_signal_handlers_unblock_by_func
2317                 (G_OBJECT(folderview->ctree),
2318                  G_CALLBACK(folderview_selected), folderview);
2319
2320         pos = gtk_scrolled_window_get_vadjustment(
2321                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2322         gtk_adjustment_set_value(pos, height);
2323         gtk_clist_thaw(GTK_CLIST(folderview->ctree));
2324 }
2325
2326 static void drag_state_stop(FolderView *folderview)
2327 {
2328         if (folderview->drag_timer)
2329                 gtk_timeout_remove(folderview->drag_timer);
2330         folderview->drag_timer = 0;
2331         folderview->drag_node = NULL;
2332 }
2333
2334 static gint folderview_defer_expand(FolderView *folderview)
2335 {
2336         if (folderview->drag_node) {
2337                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2338                 if (folderview->drag_item->collapsed) {
2339                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2340                         folderview->nodes_to_recollapse = g_slist_append
2341                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2342                 }
2343         }
2344         folderview->drag_item  = NULL;
2345         folderview->drag_timer = 0;
2346         return FALSE;
2347 }
2348
2349 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2350 {
2351         /* the idea is that we call drag_state_start() whenever we want expansion to
2352          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2353          * we need to call drag_state_stop() */
2354         drag_state_stop(folderview);
2355         /* request expansion */
2356         if (0 != (folderview->drag_timer = gtk_timeout_add
2357                         (prefs_common.hover_timeout, 
2358                          (GtkFunction)folderview_defer_expand,
2359                          folderview))) {
2360                 folderview->drag_node = node;
2361                 folderview->drag_item = item;
2362         }                        
2363 }
2364
2365 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2366                                   FolderView       *folderview)
2367 {
2368         GdkDragContext *context;
2369
2370         g_return_if_fail(folderview != NULL);
2371         if (folderview->selected == NULL) return;
2372         if (folderview->nodes_to_recollapse) 
2373                 g_slist_free(folderview->nodes_to_recollapse);
2374         folderview->nodes_to_recollapse = NULL;
2375         context = gtk_drag_begin(widget, folderview->target_list,
2376                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2377         gtk_drag_set_icon_default(context);
2378 }
2379
2380 static void folderview_drag_data_get(GtkWidget        *widget,
2381                                      GdkDragContext   *drag_context,
2382                                      GtkSelectionData *selection_data,
2383                                      guint             info,
2384                                      guint             time,
2385                                      FolderView       *folderview)
2386 {
2387         FolderItem *item;
2388         GList *cur;
2389         gchar *source = NULL;
2390
2391         for (cur = GTK_CLIST(folderview->ctree)->selection;
2392              cur != NULL; cur = cur->next) {
2393                 item = gtk_ctree_node_get_row_data
2394                         (GTK_CTREE(folderview->ctree), 
2395                          GTK_CTREE_NODE(cur->data));
2396                 if (item) {
2397                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2398                         gtk_selection_data_set(selection_data,
2399                                                selection_data->target, 8,
2400                                                source, strlen(source));
2401                         break;
2402                 } else
2403                         return;
2404         }
2405 }
2406
2407 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2408 {
2409         FolderUpdateData *hookdata;
2410         FolderView *folderview;
2411         GtkWidget *ctree;
2412
2413         hookdata = source;
2414         folderview = (FolderView *) userdata;   
2415         g_return_val_if_fail(hookdata != NULL, FALSE);
2416         g_return_val_if_fail(folderview != NULL, FALSE);
2417
2418         ctree = folderview->ctree;
2419         g_return_val_if_fail(ctree != NULL, FALSE);
2420
2421         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2422                 folderview_create_folder_node(folderview, hookdata->item);
2423         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2424                 GtkCTreeNode *node;
2425
2426                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2427                 if (node != NULL)
2428                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2429         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2430                 folderview_set(folderview);
2431
2432         return FALSE;
2433 }
2434
2435 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2436                                           GdkDragContext *context,
2437                                           gint            x,
2438                                           gint            y,
2439                                           guint           time,
2440                                           FolderView     *folderview)
2441 {
2442         gint row, column;
2443         FolderItem *item = NULL, *src_item = NULL;
2444         GtkCTreeNode *node = NULL;
2445         gboolean acceptable = FALSE;
2446         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2447                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2448         int height = (int)pos->page_size;
2449         int total_height = (int)pos->upper;
2450         int vpos = (int) pos->value;
2451
2452         if (gtk_clist_get_selection_info
2453                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2454                 GtkWidget *srcwidget;
2455
2456                 if (y > height - 24 && height + vpos < total_height)
2457                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2458
2459                 if (y < 48 && y > 0)
2460                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2461
2462                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2463                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2464                 src_item = folderview->summaryview->folder_item;
2465
2466                 srcwidget = gtk_drag_get_source_widget(context);
2467                 if (srcwidget == folderview->summaryview->ctree) {
2468                         /* comes from summaryview */
2469                         /* we are copying messages, so only accept folder items that are not
2470                            the source item, are no root items and can copy messages */
2471                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2472                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2473                                 acceptable = TRUE;
2474                 } else if (srcwidget == folderview->ctree) {
2475                         /* comes from folderview */
2476                         /* we are moving folder items, only accept folders that are not
2477                            the source items and can copy messages and create folder items */
2478                         if (item && item->folder && src_item && src_item != item &&
2479                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2480                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2481                                 acceptable = TRUE;
2482                 }
2483         }
2484
2485         if (acceptable || (src_item && src_item == item))
2486                 drag_state_start(folderview, node, item);
2487         
2488         if (acceptable) {
2489                 g_signal_handlers_block_by_func
2490                         (G_OBJECT(widget),
2491                          G_CALLBACK(folderview_selected), folderview);
2492                 gtk_ctree_select(GTK_CTREE(widget), node);
2493                 g_signal_handlers_unblock_by_func
2494                         (G_OBJECT(widget),
2495                          G_CALLBACK(folderview_selected), folderview);
2496                 gdk_drag_status(context, 
2497                                         (context->actions == GDK_ACTION_COPY ?
2498                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2499         } else {
2500                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2501                 gdk_drag_status(context, 0, time);
2502         }
2503
2504         return acceptable;
2505 }
2506
2507 static void folderview_drag_leave_cb(GtkWidget      *widget,
2508                                      GdkDragContext *context,
2509                                      guint           time,
2510                                      FolderView     *folderview)
2511 {
2512         drag_state_stop(folderview);
2513         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2514 }
2515
2516 static void folderview_drag_received_cb(GtkWidget        *widget,
2517                                         GdkDragContext   *drag_context,
2518                                         gint              x,
2519                                         gint              y,
2520                                         GtkSelectionData *data,
2521                                         guint             info,
2522                                         guint             time,
2523                                         FolderView       *folderview)
2524 {
2525         gint row, column;
2526         FolderItem *item, *src_item;
2527         GtkCTreeNode *node;
2528
2529         drag_state_stop(folderview);
2530         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2531                 /* comes from summaryview */
2532                 if (gtk_clist_get_selection_info
2533                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2534                         return;
2535
2536                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2537                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2538                 src_item = folderview->summaryview->folder_item;
2539                 
2540                 /* re-check (due to acceptable possibly set for folder moves */
2541                 if (!(item && item->folder && item->path && !item->no_select && 
2542                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2543                         return;
2544                 }
2545                 if (item && src_item) {
2546                         switch (drag_context->action) {
2547                         case GDK_ACTION_COPY:
2548                                 summary_copy_selected_to(folderview->summaryview, item);
2549                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2550                                 break;
2551                         case GDK_ACTION_MOVE:
2552                         case GDK_ACTION_DEFAULT:
2553                         default:
2554                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2555                                         summary_copy_selected_to(folderview->summaryview, item);
2556                                 else
2557                                         summary_move_selected_to(folderview->summaryview, item);
2558                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2559                         }
2560                 } else
2561                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2562         } else {
2563                 /* comes from folderview */
2564                 char *source;
2565                 gboolean folder_is_normal = TRUE;
2566                 
2567                 source = data->data + 17;
2568                 if (gtk_clist_get_selection_info
2569                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2570                     || *source == 0) {
2571                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2572                         return;
2573                 }
2574                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2575                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2576                 src_item = folder_find_item_from_identifier(source);
2577
2578                 folder_is_normal = 
2579                         src_item != NULL &&
2580                         src_item->stype == F_NORMAL &&
2581                         !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2582                         !folder_has_parent_of_type(src_item, F_DRAFT) &&
2583                         !folder_has_parent_of_type(src_item, F_QUEUE) &&
2584                         !folder_has_parent_of_type(src_item, F_TRASH);
2585                 if (!item || item->no_select || !src_item 
2586                 ||  !folder_is_normal) {
2587                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2588                         return;
2589                 }
2590
2591                 folderview_move_folder(folderview, src_item, item);
2592                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2593         }
2594         folderview->nodes_to_recollapse = NULL;
2595 }
2596
2597 static void folderview_drag_end_cb(GtkWidget        *widget, 
2598                                    GdkDragContext   *drag_context,
2599                                    FolderView       *folderview)
2600 {
2601         drag_state_stop(folderview);
2602         g_slist_free(folderview->nodes_to_recollapse);
2603         folderview->nodes_to_recollapse = NULL;
2604 }
2605
2606 void folderview_register_popup(FolderViewPopup *fpopup)
2607 {
2608         GList *folderviews;
2609
2610         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2611                 FolderView *folderview = folderviews->data;
2612                 GtkItemFactory *factory;
2613
2614                 factory = create_ifactory(folderview, fpopup);
2615                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2616         }       
2617         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2618 }
2619
2620 void folderview_unregister_popup(FolderViewPopup *fpopup)
2621 {
2622         GList *folderviews;
2623
2624         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2625                 FolderView *folderview = folderviews->data;
2626
2627                 g_hash_table_remove(folderview->popups, fpopup->klass);
2628         }       
2629         g_hash_table_remove(folderview_popups, fpopup->klass);
2630 }