2006-01-09 [paul] 1.9.100cvs128
[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         GdkColor gdk_color;
579
580         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
581         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
582         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
583         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
584         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
585         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
586         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
587         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
588         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
589         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
590         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
591         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
592         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
593         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
594         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
595         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
596         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
597         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
598         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
599         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
600         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE, &draftsxpm, &draftsxpmmask);
601         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN, &draftsopenxpm, &draftsopenxpmmask);
602         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_NOSELECT, &noselectxpm, &noselectxpmmask);
603
604         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_MARK, &m_inboxxpm, &m_inboxxpmmask);
605         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM_MARK, &m_inboxhrmxpm, &m_inboxhrmxpmmask);
606         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_MARK, &m_inboxopenxpm, &m_inboxopenxpmmask);
607         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM_MARK, &m_inboxopenhrmxpm, &m_inboxopenhrmxpmmask);
608         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_MARK, &m_outboxxpm, &m_outboxxpmmask);
609         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM_MARK, &m_outboxhrmxpm, &m_outboxhrmxpmmask);
610         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_MARK, &m_outboxopenxpm, &m_outboxopenxpmmask);
611         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM_MARK, &m_outboxopenhrmxpm, &m_outboxopenhrmxpmmask);
612         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_MARK, &m_folderxpm, &m_folderxpmmask);
613         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM_MARK, &m_folderhrmxpm, &m_folderhrmxpmmask);
614         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_MARK, &m_folderopenxpm, &m_folderopenxpmmask);
615         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM_MARK, &m_folderopenhrmxpm, &m_folderopenhrmxpmmask);
616         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_MARK, &m_trashopenxpm, &m_trashopenxpmmask);
617         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM_MARK, &m_trashopenhrmxpm, &m_trashopenhrmxpmmask);
618         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_MARK, &m_trashxpm, &m_trashxpmmask);
619         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM_MARK, &m_trashhrmxpm, &m_trashhrmxpmmask);
620         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_MARK, &m_queuexpm, &m_queuexpmmask);
621         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM_MARK, &m_queuehrmxpm, &m_queuehrmxpmmask);
622         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_MARK, &m_queueopenxpm, &m_queueopenxpmmask);
623         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM_MARK, &m_queueopenhrmxpm, &m_queueopenhrmxpmmask);
624         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE_MARK, &m_draftsxpm, &m_draftsxpmmask);
625         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN_MARK, &m_draftsopenxpm, &m_draftsopenxpmmask);
626                         
627         if (!normal_style) {
628                 PangoFontDescription *font_desc;
629                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
630                 font_desc = pango_font_description_from_string(NORMAL_FONT);
631                 if (font_desc) {
632                         if (normal_style->font_desc)
633                                 pango_font_description_free
634                                         (normal_style->font_desc);
635                         normal_style->font_desc = font_desc;
636                 }
637                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
638                 normal_color_style = gtk_style_copy(normal_style);
639                 normal_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
640
641                 gtk_widget_set_style(ctree, normal_style);
642         }
643
644         if (!bold_style) {
645                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
646                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
647                 pango_font_description_set_weight
648                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
649                 bold_color_style = gtk_style_copy(bold_style);
650                 bold_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
651
652                 bold_tgtfold_style = gtk_style_copy(bold_style);
653                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
654         }
655 }
656
657 void folderview_set(FolderView *folderview)
658 {
659         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
660         MainWindow *mainwin = folderview->mainwin;
661
662         debug_print("Setting folder info...\n");
663         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
664
665         main_window_cursor_wait(mainwin);
666
667         folderview->selected = NULL;
668         folderview->opened = NULL;
669
670         gtk_clist_freeze(GTK_CLIST(ctree));
671         gtk_clist_clear(GTK_CLIST(ctree));
672         gtk_clist_thaw(GTK_CLIST(ctree));
673         gtk_clist_freeze(GTK_CLIST(ctree));
674
675         folderview_set_folders(folderview);
676
677         gtk_clist_thaw(GTK_CLIST(ctree));
678         main_window_cursor_normal(mainwin);
679         STATUSBAR_POP(mainwin);
680 }
681
682 void folderview_set_all(void)
683 {
684         GList *list;
685
686         for (list = folderview_list; list != NULL; list = list->next)
687                 folderview_set((FolderView *)list->data);
688 }
689
690 void folderview_select(FolderView *folderview, FolderItem *item)
691 {
692         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
693         GtkCTreeNode *node;
694         GtkCTreeNode *old_selected = folderview->selected;
695
696         if (!item) return;
697
698         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
699         if (node) folderview_select_node(folderview, node);
700
701         if (old_selected != node)
702                 folder_update_op_count();
703 }
704
705 static void mark_all_read_cb(FolderView *folderview, guint action,
706                              GtkWidget *widget)
707 {
708         FolderItem *item;
709         AlertValue val;
710         
711         item = folderview_get_selected_item(folderview);
712         if (item == NULL)
713                 return;
714
715         if (prefs_common.ask_mark_all_read) {
716                 val = alertpanel_full(_("Mark all as read"),
717                         _("Do you really want to mark all mails in this "
718                           "folder as read ?"), GTK_STOCK_YES, GTK_STOCK_NO, NULL,
719                           TRUE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
720
721                 if (val == G_ALERTALTERNATE ||
722                     val == (G_ALERTALTERNATE|G_ALERTDISABLE))
723                         return;
724                 else if (val == (G_ALERTDEFAULT|G_ALERTDISABLE)) 
725                         prefs_common.ask_mark_all_read = FALSE;
726         }
727         
728         summary_lock(folderview->summaryview);
729         folder_item_update_freeze();
730         if (folderview->summaryview->folder_item == item)
731                 gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
732         folderutils_mark_all_read(item);
733         if (folderview->summaryview->folder_item == item)
734                 gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
735         folder_item_update_thaw();
736         summary_unlock(folderview->summaryview);
737 }
738
739 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
740 {
741         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
742
743         g_return_if_fail(node != NULL);
744
745         folderview->open_folder = TRUE;
746         gtkut_ctree_set_focus_row(ctree, node);
747         gtk_ctree_select(ctree, node);
748         if (folderview->summaryview->folder_item &&
749             folderview->summaryview->folder_item->total_msgs > 0)
750                 gtk_widget_grab_focus(folderview->summaryview->ctree);
751         else
752                 gtk_widget_grab_focus(folderview->ctree);
753
754         gtkut_ctree_expand_parent_all(ctree, node);
755 }
756
757 void folderview_unselect(FolderView *folderview)
758 {
759         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
760                 gtk_ctree_collapse
761                         (GTK_CTREE(folderview->ctree), folderview->opened);
762
763         folderview->selected = folderview->opened = NULL;
764 }
765
766 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
767                                                  GtkCTreeNode *node)
768 {
769         FolderItem *item;
770
771         if (node)
772                 node = gtkut_ctree_node_next(ctree, node);
773         else
774                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
775
776         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
777                 item = gtk_ctree_node_get_row_data(ctree, node);
778                 if (item && item->unread_msgs > 0 && item->stype != F_TRASH)
779                         return node;
780         }
781
782         return NULL;
783 }
784
785 void folderview_select_next_unread(FolderView *folderview)
786 {
787         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
788         GtkCTreeNode *node = NULL;
789
790         if ((node = folderview_find_next_unread(ctree, folderview->opened))
791             != NULL) {
792                 folderview_select_node(folderview, node);
793                 return;
794         }
795
796         if (!folderview->opened ||
797             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
798                 return;
799         /* search again from the first node */
800         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
801                 folderview_select_node(folderview, node);
802 }
803
804 static GtkCTreeNode *folderview_find_next_new(GtkCTree *ctree,
805                                                  GtkCTreeNode *node)
806 {
807         FolderItem *item;
808
809         if (node)
810                 node = gtkut_ctree_node_next(ctree, node);
811         else
812                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
813
814         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
815                 item = gtk_ctree_node_get_row_data(ctree, node);
816                 if (item && item->new_msgs > 0 && item->stype != F_TRASH)
817                         return node;
818         }
819
820         return NULL;
821 }
822
823 void folderview_select_next_new(FolderView *folderview)
824 {
825         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
826         GtkCTreeNode *node = NULL;
827
828         if ((node = folderview_find_next_new(ctree, folderview->opened))
829             != NULL) {
830                 folderview_select_node(folderview, node);
831                 return;
832         }
833
834         if (!folderview->opened ||
835             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
836                 return;
837         /* search again from the first node */
838         if ((node = folderview_find_next_new(ctree, NULL)) != NULL)
839                 folderview_select_node(folderview, node);
840 }
841
842 FolderItem *folderview_get_selected_item(FolderView *folderview)
843 {
844         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
845
846         if (!folderview->selected) return NULL;
847         return gtk_ctree_node_get_row_data(ctree, folderview->selected);
848 }
849
850 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
851 {
852         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
853         static GtkCTreeNode *prev_row = NULL;
854         FolderItem *item;
855         gint new, unread, total;
856         gchar *new_str, *unread_str, *total_str;
857         gint *col_pos = folderview->col_pos;
858
859         if (!row) return;
860
861         item = gtk_ctree_node_get_row_data(ctree, row);
862         if (!item) return;
863
864         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_NEW], &new_str);
865         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_UNREAD], &unread_str);
866         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_TOTAL], &total_str);
867         new = atoi(new_str);
868         unread = atoi(unread_str);
869         total = atoi(total_str);
870
871         prev_row = row;
872
873         folderview_update_node(folderview, row);
874 }
875
876 void folderview_append_item(FolderItem *item)
877 {
878         GList *list;
879         
880         g_return_if_fail(item != NULL);
881         g_return_if_fail(item->folder != NULL);
882         if (folder_item_parent(item)) return;
883
884         for (list = folderview_list; list != NULL; list = list->next) {
885                 FolderView *folderview = (FolderView *)list->data;
886                 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
887                 GtkCTreeNode *node, *child;
888                 gint *col_pos = folderview->col_pos;
889
890                 node = gtk_ctree_find_by_row_data(ctree, NULL, 
891                                                   folder_item_parent(item));
892                 if (node) {
893                         child = gtk_ctree_find_by_row_data(ctree, node, item);
894                         if (!child) {
895                                 gchar *text[N_FOLDER_COLS] =
896                                         {NULL, "0", "0", "0"};
897
898                                 gtk_clist_freeze(GTK_CLIST(ctree));
899
900                                 text[col_pos[F_COL_FOLDER]] = item->name;
901                                 child = gtk_sctree_insert_node
902                                         (ctree, node, NULL, text,
903                                          FOLDER_SPACING,
904                                          folderxpm, folderxpmmask,
905                                          folderopenxpm, folderopenxpmmask,
906                                          FALSE, FALSE);
907                                 gtk_ctree_node_set_row_data(ctree, child, item);
908                                 gtk_ctree_expand(ctree, node);
909                                 folderview_update_node(folderview, child);
910                                 folderview_sort_folders(folderview, node,
911                                                         item->folder);
912
913                                 gtk_clist_thaw(GTK_CLIST(ctree));
914                         }
915                 }
916         }
917 }
918
919 static void folderview_set_folders(FolderView *folderview)
920 {
921         GList *list;
922
923         list = folder_get_list();
924
925         for (; list != NULL; list = list->next)
926                 folderview_append_folder(folderview, FOLDER(list->data));
927 }
928
929 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
930                                       gpointer data)
931 {
932         GList *list;
933
934         for (list = folderview_list; list != NULL; list = list->next) {
935                 FolderView *folderview = (FolderView *)list->data;
936                 MainWindow *mainwin = folderview->mainwin;
937                 gchar *str;
938
939                 if (item->path)
940                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
941                                               item->folder->name, G_DIR_SEPARATOR,
942                                               item->path);
943                 else
944                         str = g_strdup_printf(_("Scanning folder %s ..."),
945                                               item->folder->name);
946
947                 STATUSBAR_PUSH(mainwin, str);
948                 STATUSBAR_POP(mainwin);
949                 g_free(str);
950         }
951 }
952
953 void folderview_rescan_tree(Folder *folder, gboolean rebuild)
954 {
955         GtkWidget *window;
956
957         g_return_if_fail(folder != NULL);
958
959         if (!folder->klass->scan_tree) return;
960
961         if (rebuild && 
962             alertpanel_full(_("Rebuild folder tree"), 
963                          _("Rebuilding the folder tree will remove "
964                            "local caches. Do you want to continue?"),
965                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
966                          NULL, ALERT_WARNING, G_ALERTALTERNATE) 
967                 != G_ALERTDEFAULT) {
968                 return;
969         }
970
971         inc_lock();
972         if (rebuild)
973                 window = label_window_create(_("Rebuilding folder tree..."));
974         else 
975                 window = label_window_create(_("Scanning folder tree..."));
976
977         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
978         folder_scan_tree(folder, rebuild);
979         folder_set_ui_func(folder, NULL, NULL);
980
981         folderview_set_all();
982
983         gtk_widget_destroy(window);
984         inc_unlock();
985 }
986
987 /** folderview_check_new()
988  *  Scan and update the folder and return the 
989  *  count the number of new messages since last check. 
990  *  \param folder the folder to check for new messages
991  *  \return the number of new messages since last check
992  */
993 gint folderview_check_new(Folder *folder)
994 {
995         GList *list;
996         FolderItem *item;
997         FolderView *folderview;
998         GtkCTree *ctree;
999         GtkCTreeNode *node;
1000         gint new_msgs = 0;
1001         gint former_new_msgs = 0;
1002         gint former_new = 0, former_unread = 0, former_total;
1003
1004         for (list = folderview_list; list != NULL; list = list->next) {
1005                 folderview = (FolderView *)list->data;
1006                 ctree = GTK_CTREE(folderview->ctree);
1007
1008                 inc_lock();
1009                 main_window_lock(folderview->mainwin);
1010
1011                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1012                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1013                         item = gtk_ctree_node_get_row_data(ctree, node);
1014                         if (!item || !item->path || !item->folder) continue;
1015                         if (item->no_select) continue;
1016                         if (folder && folder != item->folder) continue;
1017                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1018                         if (!item->prefs->newmailcheck) continue;
1019
1020                         folderview_scan_tree_func(item->folder, item, NULL);
1021                         former_new    = item->new_msgs;
1022                         former_unread = item->unread_msgs;
1023                         former_total  = item->total_msgs;
1024
1025                         if (folder_item_scan(item) < 0) {
1026                                 summaryview_unlock(folderview->summaryview, item);
1027                                 if (folder && !FOLDER_IS_LOCAL(folder))
1028                                         break;
1029                         }
1030
1031                         if (former_new    != item->new_msgs ||
1032                             former_unread != item->unread_msgs ||
1033                             former_total  != item->total_msgs)
1034                                 folderview_update_node(folderview, node);
1035
1036                         new_msgs += item->new_msgs;
1037                         former_new_msgs += former_new;
1038                 }
1039
1040                 main_window_unlock(folderview->mainwin);
1041                 inc_unlock();
1042         }
1043
1044         folder_write_list();
1045         /* Number of new messages since last check is the just the difference 
1046          * between former_new_msgs and new_msgs. If new_msgs is less than
1047          * former_new_msgs, that would mean another session accessed the folder
1048          * and the result is not well defined.
1049          */
1050         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1051         return new_msgs;
1052 }
1053
1054 void folderview_check_new_all(void)
1055 {
1056         GList *list;
1057         GtkWidget *window;
1058         FolderView *folderview;
1059
1060         folderview = (FolderView *)folderview_list->data;
1061
1062         inc_lock();
1063         main_window_lock(folderview->mainwin);
1064         window = label_window_create
1065                 (_("Checking for new messages in all folders..."));
1066
1067         list = folder_get_list();
1068         for (; list != NULL; list = list->next) {
1069                 Folder *folder = list->data;
1070
1071                 folderview_check_new(folder);
1072         }
1073
1074         folder_write_list();
1075         folderview_set_all();
1076
1077         gtk_widget_destroy(window);
1078         main_window_unlock(folderview->mainwin);
1079         inc_unlock();
1080 }
1081
1082 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1083                                                  FolderItem *item,
1084                                                  gboolean in_sub)
1085 {
1086         GNode *node = NULL;
1087         
1088         if (!item || !item->folder || !item->folder->node)
1089                 return FALSE;
1090                 
1091         node = item->folder->node;
1092         
1093         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1094         node = node->children;
1095
1096         if (in_sub &&
1097             (item->new_msgs > 0 ||
1098             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1099                 return TRUE;
1100         }
1101
1102         while (node != NULL) {
1103                 if (node && node->data) {
1104                         FolderItem *next_item = (FolderItem*) node->data;
1105                         node = node->next;
1106                         if (folderview_have_new_children_sub(folderview, 
1107                                                              next_item, TRUE))
1108                                 return TRUE;
1109                 }
1110         }
1111
1112         return FALSE;
1113 }
1114
1115 static gboolean folderview_have_new_children(FolderView *folderview,
1116                                              FolderItem *item)
1117 {
1118         return folderview_have_new_children_sub(folderview, item, FALSE);
1119 }
1120
1121 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1122                                                     FolderItem *item, 
1123                                                     gboolean in_sub)
1124 {
1125         GNode *node = NULL;
1126         
1127         if (!item || !item->folder || !item->folder->node)
1128                 return FALSE;
1129         
1130         node = item->folder->node;
1131         
1132         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1133         node = node->children;
1134
1135         if (in_sub &&
1136             (item->unread_msgs > 0 ||
1137             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1138                 return TRUE;
1139         }
1140
1141         while (node != NULL) {
1142                 if (node && node->data) {
1143                         FolderItem *next_item = (FolderItem*) node->data;
1144                         node = node->next;
1145                         if (folderview_have_unread_children_sub(folderview, 
1146                                                                 next_item, 
1147                                                                 TRUE))
1148                                 return TRUE;
1149                 }
1150         }
1151
1152         return FALSE;
1153 }
1154
1155 static gboolean folderview_have_unread_children(FolderView *folderview,
1156                                                 FolderItem *item)
1157 {
1158         return folderview_have_unread_children_sub(folderview, item, FALSE);
1159 }
1160
1161 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1162                                                       FolderItem *item,
1163                                                       gboolean in_sub)
1164 {
1165         GNode *node = NULL;
1166
1167         if (!item || !item->folder || !item->folder->node)
1168                 return FALSE;
1169
1170         node = item->folder->node;
1171         
1172         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1173         node = node->children;
1174
1175         if (in_sub && item->search_match){
1176                 return TRUE;
1177         }
1178
1179         while (node != NULL) {
1180                 if (node && node->data) {
1181                         FolderItem *next_item = (FolderItem*) node->data;
1182                         node = node->next;
1183                         if (folderview_have_matching_children_sub(folderview, 
1184                                                                   next_item, 
1185                                                                   TRUE))
1186                                 return TRUE;
1187                 }
1188         }
1189
1190         return FALSE;
1191 }
1192
1193 static gboolean folderview_have_matching_children(FolderView *folderview,
1194                                                   FolderItem *item)
1195 {
1196         return folderview_have_matching_children_sub(folderview, item, FALSE);
1197 }
1198
1199 static gboolean folderview_have_marked_children_sub(FolderView *folderview,
1200                                                     FolderItem *item,
1201                                                     gboolean in_sub)
1202 {
1203         GNode *node = NULL;
1204         
1205         if (!item || !item->folder || !item->folder->node)
1206                 return FALSE;
1207                 
1208         node = item->folder->node;
1209         
1210         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1211         node = node->children;
1212
1213         if (item->marked_msgs != 0) {
1214                 return TRUE;
1215         }
1216
1217         while (node != NULL) {
1218                 if (node && node->data) {
1219                         FolderItem *next_item = (FolderItem*) node->data;
1220                         node = node->next;
1221                         if (folderview_have_marked_children_sub(folderview,
1222                                                                 next_item, TRUE))
1223                                 return TRUE;
1224                 }
1225         }
1226
1227         return FALSE;
1228 }
1229
1230 static gboolean folderview_have_marked_children(FolderView *folderview,
1231                                              FolderItem *item)
1232 {
1233         return folderview_have_marked_children_sub(folderview, item, FALSE);
1234 }
1235
1236 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1237 {
1238         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1239         GtkStyle *style = NULL;
1240         GtkStyle *color_style = NULL;
1241         FolderItem *item;
1242         GdkPixmap *xpm, *openxpm;
1243         GdkBitmap *mask, *openmask;
1244         static GdkPixmap *searchicon;
1245         static GdkBitmap *searchmask;
1246         gboolean mark = FALSE;
1247         gchar *name;
1248         gchar *str;
1249         gboolean add_unread_mark;
1250         gboolean add_sub_match_mark;
1251         gboolean use_bold, use_color;
1252         gint *col_pos = folderview->col_pos;
1253         SpecialFolderItemType stype;
1254         
1255         item = gtk_ctree_node_get_row_data(ctree, node);
1256         g_return_if_fail(item != NULL);
1257
1258         if (!GTK_CTREE_ROW(node)->expanded)
1259                 mark = folderview_have_marked_children(folderview, item);
1260         else
1261                 mark = (item->marked_msgs != 0);
1262
1263         stype = item->stype;
1264         if (stype == F_NORMAL) {
1265                 if (folder_has_parent_of_type(item, F_TRASH))
1266                         stype = F_TRASH;
1267                 else if (folder_has_parent_of_type(item, F_DRAFT))
1268                         stype = F_DRAFT;
1269                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1270                         stype = F_OUTBOX;
1271                 else if (folder_has_parent_of_type(item, F_QUEUE))
1272                         stype = F_QUEUE;
1273         }
1274         switch (stype) {
1275         case F_INBOX:
1276                 if (item->hide_read_msgs) {
1277                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1278                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1279                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1280                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1281                 } else {
1282                         xpm = mark?m_inboxxpm:inboxxpm;
1283                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1284                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1285                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1286                 }
1287                 break;
1288         case F_OUTBOX:
1289                 if (item->hide_read_msgs) {
1290                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1291                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1292                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1293                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1294                 } else {
1295                         xpm = mark?m_outboxxpm:outboxxpm;
1296                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1297                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1298                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1299                 }
1300                 break;
1301         case F_QUEUE:
1302                 if (item->hide_read_msgs) {
1303                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1304                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1305                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1306                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1307                 } else {
1308                         xpm = mark?m_queuexpm:queuexpm;
1309                         mask = mark?m_queuexpmmask:queuexpmmask;
1310                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1311                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1312                 }
1313                 break;
1314         case F_TRASH:
1315                 if (item->hide_read_msgs) {
1316                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1317                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1318                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1319                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1320                 } else {
1321                         xpm = mark?m_trashxpm:trashxpm;
1322                         mask = mark?m_trashxpmmask:trashxpmmask;
1323                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1324                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1325                 }
1326                 break;
1327         case F_DRAFT:
1328                 xpm = mark?m_draftsxpm:draftsxpm;
1329                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1330                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1331                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1332                 break;
1333         default:
1334                 if (item->hide_read_msgs) {
1335                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1336                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1337                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1338                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1339                 } else {
1340                         xpm = mark?m_folderxpm:folderxpm;
1341                         mask = mark?m_folderxpmmask:folderxpmmask;
1342                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1343                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1344                 }
1345         }
1346         
1347         if (item->no_select) {
1348                 xpm = openxpm = noselectxpm;
1349                 mask = openmask = noselectxpmmask;
1350         }
1351
1352         name = folder_item_get_name(item);
1353
1354         if (!GTK_CTREE_ROW(node)->expanded) {
1355                 add_unread_mark = folderview_have_unread_children(
1356                                         folderview, item);
1357                 add_sub_match_mark = folderview_have_matching_children(
1358                                         folderview, item);
1359         } else {
1360                 add_unread_mark = FALSE;
1361                 add_sub_match_mark = FALSE;
1362         }
1363
1364         if (item->search_match) {
1365                 if (!searchicon) {
1366                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1367                          &searchicon, &searchmask);
1368                 }
1369                 xpm = openxpm = searchicon;
1370                 mask = openmask = searchmask;
1371         }
1372
1373         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1374             prefs_common.display_folder_unread) {
1375                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1376                                       add_unread_mark ? "+" : "");
1377                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1378                                         xpm, mask, openxpm, openmask,
1379                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1380                 g_free(str);
1381         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1382                     prefs_common.display_folder_unread) 
1383                    || add_sub_match_mark) {
1384
1385                 if (item->unread_msgs > 0)
1386                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1387                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1388                                               item->unreadmarked_msgs > 0 ? "!":"");
1389                 else
1390                         str = g_strdup_printf("%s (+)", name);
1391                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1392                                         xpm, mask, openxpm, openmask,
1393                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1394                 g_free(str);
1395         } else {
1396                 str = g_strdup_printf("%s%s", name, 
1397                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1398         
1399                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1400                                         xpm, mask, openxpm, openmask,
1401                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1402                 g_free(str);
1403         }
1404         g_free(name);
1405
1406         if (!folder_item_parent(item)) {
1407                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1408                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1409                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1410         } else {
1411                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1412                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1413                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1414         }
1415
1416         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1417             folder_has_parent_of_type(item, F_DRAFT) ||
1418             folder_has_parent_of_type(item, F_TRASH)) {
1419                 use_bold = use_color = FALSE;
1420         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1421                 /* highlight queue folder if there are any messages */
1422                 use_bold = use_color = (item->total_msgs > 0);
1423         } else {
1424                 /* if unread messages exist, print with bold font */
1425                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1426                                 || add_unread_mark;
1427                 /* if new messages exist, print with colored letter */
1428                 use_color =
1429                         (item->new_msgs > 0) ||
1430                         (add_unread_mark &&
1431                          folderview_have_new_children(folderview, item));       
1432         }
1433
1434         gtk_ctree_node_set_foreground(ctree, node, NULL);
1435
1436         if (use_bold) {
1437                 GdkColor gdk_color;
1438
1439                 if (item->prefs->color > 0 && !use_color) {
1440                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1441                         color_style = gtk_style_copy(bold_style);
1442                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1443                         style = color_style;
1444                 } else if (use_color) {
1445                         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
1446                         bold_color_style = gtk_style_copy(bold_style);
1447                         bold_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1448                         style = bold_color_style;
1449                 } else
1450                         style = bold_style;
1451                 if (item->op_count > 0) {
1452                         style = bold_tgtfold_style;
1453                 }
1454         } else if (use_color) {
1455                 GdkColor gdk_color;
1456
1457                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
1458                 style = gtk_style_copy(normal_color_style);
1459                 style->fg[GTK_STATE_NORMAL] = gdk_color;
1460                 gtk_ctree_node_set_foreground(ctree, node,
1461                                               &folderview->color_new);
1462         } else if (item->op_count > 0) {
1463                 style = bold_tgtfold_style;
1464         } else if (item->prefs->color > 0) {
1465                 GdkColor gdk_color;
1466                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1467                 color_style = gtk_style_copy(normal_style);
1468                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1469                 style = color_style;
1470         } else {
1471                 style = normal_style;
1472         }
1473
1474         gtk_ctree_node_set_row_style(ctree, node, style);
1475
1476         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1477                 folderview_update_node(folderview, node);
1478 }
1479
1480 #if !CLAWS /* keep it here for syncs */
1481 void folderview_update_item(FolderItem *item, gboolean update_summary)
1482 {
1483         GList *list;
1484         FolderView *folderview;
1485         GtkCTree *ctree;
1486         GtkCTreeNode *node;
1487
1488         g_return_if_fail(item != NULL);
1489
1490         for (list = folderview_list; list != NULL; list = list->next) {
1491                 folderview = (FolderView *)list->data;
1492                 ctree = GTK_CTREE(folderview->ctree);
1493
1494                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1495                 if (node) {
1496                         folderview_update_node(folderview, node);
1497                         if (update_summary && folderview->opened == node)
1498                                 summary_show(folderview->summaryview,
1499                                              item, FALSE);
1500                 }
1501         }
1502 }
1503 #endif
1504
1505 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1506 {
1507         GList *list;
1508         FolderView *folderview;
1509         GtkCTree *ctree;
1510         GtkCTreeNode *node;
1511
1512         g_return_if_fail(item != NULL);
1513
1514         for (list = folderview_list; list != NULL; list = list->next) {
1515                 folderview = (FolderView *)list->data;
1516                 ctree = GTK_CTREE(folderview->ctree);
1517
1518                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1519                 if (node) {
1520                         item->search_match = matches;
1521                         folderview_update_node(folderview, node);
1522                 }
1523         }
1524 }
1525
1526 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1527 {
1528         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1529         FolderView *folderview = (FolderView *)data;
1530         GtkCTree *ctree;
1531         GtkCTreeNode *node;
1532         g_return_val_if_fail(update_info != NULL, TRUE);
1533         g_return_val_if_fail(update_info->item != NULL, TRUE);
1534         g_return_val_if_fail(folderview != NULL, FALSE);
1535
1536         ctree = GTK_CTREE(folderview->ctree);
1537
1538         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1539         if (node) {
1540                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1541                         folderview_update_node(folderview, node);
1542                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1543                         summary_show(folderview->summaryview, update_info->item);
1544         }
1545         
1546         return FALSE;
1547 }
1548
1549 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1550                                                 gpointer data)
1551 {
1552         /* CLAWS: share this joy with other hook functions ... */
1553         folder_item_update((FolderItem *)key, 
1554                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1555 }
1556
1557 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1558 {
1559         GList *list;
1560         FolderItemUpdateFlags flags;
1561         
1562         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1563                 : 0;
1564         for (list = folderview_list; list != NULL; list = list->next)
1565                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1566                                      GINT_TO_POINTER(flags));
1567 }
1568
1569 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1570                                       GNode *gnode, GtkCTreeNode *cnode,
1571                                       gpointer data)
1572 {
1573         FolderView *folderview = (FolderView *)data;
1574         FolderItem *item = FOLDER_ITEM(gnode->data);
1575
1576         g_return_val_if_fail(item != NULL, FALSE);
1577
1578         gtk_ctree_node_set_row_data(ctree, cnode, item);
1579         folderview_update_node(folderview, cnode);
1580
1581         return TRUE;
1582 }
1583
1584 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1585                                    gpointer data)
1586 {
1587         FolderView *folderview = (FolderView *)data;
1588         FolderItem *item;
1589
1590         if (GTK_CTREE_ROW(node)->children) {
1591                 item = gtk_ctree_node_get_row_data(ctree, node);
1592                 g_return_if_fail(item != NULL);
1593
1594                 if (!item->collapsed)
1595                         gtk_ctree_expand(ctree, node);
1596                 else
1597                         folderview_update_node(folderview, node);
1598         }
1599 }
1600
1601 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1602                                GtkCTreeNode *root, GtkCTreeNode **prev)
1603 {
1604         if (item) {
1605                 GtkCTreeNode *node, *parent, *sibling;
1606
1607                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1608                 if (!node)
1609                         g_warning("%s not found.\n", item->path);
1610                 else {
1611                         parent = GTK_CTREE_ROW(node)->parent;
1612                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1613                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1614                         else
1615                                 sibling = GTK_CTREE_ROW(parent)->children;
1616                         while (sibling) {
1617                                 FolderItem *tmp;
1618
1619                                 tmp = gtk_ctree_node_get_row_data
1620                                         (ctree, sibling);
1621                                 if (tmp->stype != F_NORMAL)
1622                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1623                                 else
1624                                         break;
1625                         }
1626                         if (node != sibling)
1627                                 gtk_ctree_move(ctree, node, parent, sibling);
1628                 }
1629
1630                 *prev = node;
1631         }
1632 }
1633
1634 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1635                                     Folder *folder)
1636 {
1637         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1638         GtkCTreeNode *prev = NULL;
1639
1640         gtk_sctree_sort_recursive(ctree, root);
1641
1642         if (root && GTK_CTREE_ROW(root)->parent) return;
1643
1644         set_special_folder(ctree, folder->inbox, root, &prev);
1645         set_special_folder(ctree, folder->outbox, root, &prev);
1646         set_special_folder(ctree, folder->draft, root, &prev);
1647         set_special_folder(ctree, folder->queue, root, &prev);
1648         set_special_folder(ctree, folder->trash, root, &prev);
1649 }
1650
1651 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1652 {
1653         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1654         GtkCTreeNode *root;
1655
1656         g_return_if_fail(folder != NULL);
1657
1658         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1659                                       folderview_gnode_func, folderview);
1660         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1661                                 folderview);
1662         folderview_sort_folders(folderview, root, folder);
1663 }
1664
1665 /* callback functions */
1666
1667 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1668                                           FolderView *folderview)
1669 {
1670         GtkCList *clist = GTK_CLIST(ctree);
1671         gint prev_row = -1, row = -1, column = -1;
1672         FolderItem *item;
1673         Folder *folder;
1674         FolderViewPopup *fpopup;
1675         GtkItemFactory *fpopup_factory;
1676         GtkWidget *popup;
1677         FolderItem *special_trash = NULL;
1678         PrefsAccount *ac;
1679
1680         if (!event) return FALSE;
1681
1682         if (event->button == 1 || event->button == 2) {
1683                 folderview->open_folder = TRUE;
1684
1685                 if (event->type == GDK_2BUTTON_PRESS) {
1686                         if (clist->selection) {
1687                                 GtkCTreeNode *node;
1688
1689                                 node = GTK_CTREE_NODE(clist->selection->data);
1690                                 if (node)
1691                                         gtk_ctree_toggle_expansion(
1692                                                 GTK_CTREE(ctree),
1693                                                 node);
1694                         }
1695                 }
1696                 return FALSE;
1697         }
1698
1699         if (event->button == 2 || event->button == 3) {
1700                 /* right clicked */
1701                 if (clist->selection) {
1702                         GtkCTreeNode *node;
1703
1704                         node = GTK_CTREE_NODE(clist->selection->data);
1705                         if (node)
1706                                 prev_row = gtkut_ctree_get_nth_from_node
1707                                         (GTK_CTREE(ctree), node);
1708                 }
1709
1710                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1711                                                   &row, &column))
1712                         return FALSE;
1713                 if (prev_row != row) {
1714                         gtk_clist_unselect_all(clist);
1715                         if (event->button == 2)
1716                                 folderview_select_node
1717                                         (folderview,
1718                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1719                                                             row));
1720                         else
1721                                 gtk_clist_select_row(clist, row, column);
1722                 }
1723         }
1724
1725         if (event->button != 3) return FALSE;
1726
1727         item = gtk_clist_get_row_data(clist, row);
1728         g_return_val_if_fail(item != NULL, FALSE);
1729         g_return_val_if_fail(item->folder != NULL, FALSE);
1730         folder = item->folder;
1731
1732         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1733         if (fpopup != NULL)
1734                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1735         else {
1736                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1737                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1738         }
1739
1740         if (fpopup->set_sensitivity != NULL)
1741                 fpopup->set_sensitivity(fpopup_factory, item);
1742
1743         if (NULL != (ac = account_find_from_item(item)))
1744                 special_trash = account_get_special_folder(ac, F_TRASH);
1745
1746         if ((item == folder->trash || item == special_trash
1747              || folder_has_parent_of_type(item, F_TRASH)) &&
1748             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1749                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1750                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1751         } else if (item != folder->trash && (special_trash == NULL || item != special_trash)
1752                 && !folder_has_parent_of_type(item, F_TRASH)) {
1753                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1754                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1755         }
1756         
1757 #define SET_SENS(name, sens) \
1758         menu_set_sensitive(fpopup_factory, name, sens)
1759
1760         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1761         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1762                  folderview->selected == folderview->opened);
1763         SET_SENS("/Properties...", item->node->parent != NULL);
1764         SET_SENS("/Processing...", item->node->parent != NULL);
1765         if (item == folder->trash || item == special_trash
1766             || folder_has_parent_of_type(item, F_TRASH)) {
1767                 GSList *msglist = folder_item_get_msg_list(item);
1768                 SET_SENS("/Empty trash...", msglist != NULL);
1769                 procmsg_msg_list_free(msglist);
1770         }
1771 #undef SET_SENS
1772
1773         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1774         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1775                        event->button, event->time);
1776
1777         return FALSE;
1778 }
1779
1780 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1781                                            FolderView *folderview)
1782 {
1783         if (!event) return FALSE;
1784
1785         if (event->button == 1 && folderview->open_folder == FALSE &&
1786             folderview->opened != NULL) {
1787                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1788                                           folderview->opened);
1789                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1790         }
1791
1792         return FALSE;
1793 }
1794
1795 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1796                                        FolderView *folderview)
1797 {
1798         if (!event) return FALSE;
1799
1800         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1801                 return FALSE;
1802
1803         switch (event->keyval) {
1804         case GDK_Return:
1805                 if (folderview->selected) {
1806                         folderview_select_node(folderview,
1807                                                folderview->selected);
1808                 }
1809                 break;
1810         case GDK_space:
1811                 if (folderview->selected) {
1812                         if (folderview->opened == folderview->selected &&
1813                             (!folderview->summaryview->folder_item ||
1814                              folderview->summaryview->folder_item->total_msgs == 0))
1815                                 folderview_select_next_unread(folderview);
1816                         else
1817                                 folderview_select_node(folderview,
1818                                                        folderview->selected);
1819                 }
1820                 break;
1821         default:
1822                 break;
1823         }
1824
1825         return FALSE;
1826 }
1827
1828 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1829                                 gint column, FolderView *folderview)
1830 {
1831         static gboolean can_select = TRUE;      /* exclusive lock */
1832         gboolean opened;
1833         FolderItem *item;
1834         gchar *buf;
1835
1836         folderview->selected = row;
1837
1838         if (folderview->opened == row) {
1839                 folderview->open_folder = FALSE;
1840                 return;
1841         }
1842         
1843         if (!can_select || summary_is_locked(folderview->summaryview)) {
1844                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1845                 gtk_ctree_select(ctree, folderview->opened);
1846                 return;
1847         }
1848
1849         if (!folderview->open_folder) return;
1850
1851         item = gtk_ctree_node_get_row_data(ctree, row);
1852         if (!item || item->no_select) return;
1853
1854         can_select = FALSE;
1855
1856         /* Save cache for old folder */
1857         /* We don't want to lose all caches if sylpheed crashed */
1858         if (folderview->opened) {
1859                 FolderItem *olditem;
1860                 
1861                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1862                 if (olditem) {
1863                         /* will be null if we just moved the previously opened folder */
1864                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1865                         summary_show(folderview->summaryview, NULL);
1866                         folder_item_close(olditem);
1867                 }
1868         }
1869
1870         /* CLAWS: set compose button type: news folder items 
1871          * always have a news folder as parent */
1872         if (item->folder) 
1873                 toolbar_set_compose_button
1874                         (folderview->mainwin->toolbar,
1875                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1876                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1877
1878         if (item->path)
1879                 debug_print("Folder %s is selected\n", item->path);
1880
1881         if (!GTK_CTREE_ROW(row)->children)
1882                 gtk_ctree_expand(ctree, row);
1883         if (folderview->opened &&
1884             !GTK_CTREE_ROW(folderview->opened)->children)
1885                 gtk_ctree_collapse(ctree, folderview->opened);
1886
1887         /* ungrab the mouse event */
1888         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1889                 gtk_grab_remove(GTK_WIDGET(ctree));
1890                 if (gdk_pointer_is_grabbed())
1891                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1892         }
1893
1894         /* Open Folder */
1895         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1896                                         item->path : "(null)");
1897         debug_print("%s\n", buf);
1898         STATUSBAR_PUSH(folderview->mainwin, buf);
1899         g_free(buf);
1900
1901         main_window_cursor_wait(folderview->mainwin);
1902
1903         if (folder_item_open(item) != 0) {
1904                 main_window_cursor_normal(folderview->mainwin);
1905                 STATUSBAR_POP(folderview->mainwin);
1906
1907                 alertpanel_error(_("Folder could not be opened."));
1908
1909                 folderview->open_folder = FALSE;
1910                 can_select = TRUE;
1911
1912                 return;
1913         }
1914         
1915         main_window_cursor_normal(folderview->mainwin);
1916
1917         /* Show messages */
1918         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1919         opened = summary_show(folderview->summaryview, item);
1920         
1921         folder_clean_cache_memory();
1922
1923         if (!opened) {
1924                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1925                 gtk_ctree_select(ctree, folderview->opened);
1926         } else {
1927                 folderview->opened = row;
1928                 if (gtk_ctree_node_is_visible(ctree, row)
1929                     != GTK_VISIBILITY_FULL)
1930                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1931         }
1932
1933         STATUSBAR_POP(folderview->mainwin);
1934
1935         folderview->open_folder = FALSE;
1936         can_select = TRUE;
1937 }
1938
1939 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1940                                      FolderView *folderview)
1941 {
1942         FolderItem *item;
1943
1944         item = gtk_ctree_node_get_row_data(ctree, node);
1945         g_return_if_fail(item != NULL);
1946         item->collapsed = FALSE;
1947         folderview_update_node(folderview, node);
1948 }
1949
1950 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1951                                       FolderView *folderview)
1952 {
1953         FolderItem *item;
1954
1955         item = gtk_ctree_node_get_row_data(ctree, node);
1956         g_return_if_fail(item != NULL);
1957         item->collapsed = TRUE;
1958         folderview_update_node(folderview, node);
1959 }
1960
1961 static void folderview_popup_close(GtkMenuShell *menu_shell,
1962                                    FolderView *folderview)
1963 {
1964         if (!folderview->opened) return;
1965
1966         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1967 }
1968
1969 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1970                                    FolderView *folderview)
1971 {
1972         FolderColumnType type = folderview->col_state[column].type;
1973
1974         prefs_common.folder_col_size[type] = width;
1975 }
1976
1977 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1978 {
1979         GNode *srcnode;
1980
1981         folderview_create_folder_node(folderview, item);
1982
1983         if (!item || !item->folder || !item->folder->node)
1984                 return;
1985
1986         srcnode = item->folder->node;   
1987         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1988         srcnode = srcnode->children;
1989         while (srcnode != NULL) {
1990                 if (srcnode && srcnode->data) {
1991                         FolderItem *next_item = (FolderItem*) srcnode->data;
1992                         folderview_create_folder_node_recursive(folderview, next_item);
1993                 }
1994                 srcnode = srcnode->next;
1995         }
1996 }
1997
1998 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1999 {
2000         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2001         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2002         GtkCTreeNode *node, *parent_node;
2003         gint *col_pos = folderview->col_pos;
2004         FolderItemUpdateData hookdata;
2005
2006         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2007         if (parent_node == NULL)
2008                 return;
2009
2010         gtk_clist_freeze(GTK_CLIST(ctree));
2011
2012         text[col_pos[F_COL_FOLDER]] = item->name;
2013         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2014                                      FOLDER_SPACING,
2015                                      folderxpm, folderxpmmask,
2016                                      folderopenxpm, folderopenxpmmask,
2017                                      FALSE, FALSE);
2018         gtk_ctree_expand(ctree, parent_node);
2019         gtk_ctree_node_set_row_data(ctree, node, item);
2020         if (normal_style)
2021                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
2022         folderview_sort_folders(folderview, parent_node, item->folder);
2023
2024         hookdata.item = item;
2025         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2026         hookdata.msg = NULL;
2027         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2028
2029         gtk_clist_thaw(GTK_CLIST(ctree));
2030 }
2031
2032 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
2033                                       GtkWidget *widget)
2034 {
2035         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2036         FolderItem *item;
2037         GSList *mlist = NULL;
2038         GSList *cur = NULL;
2039         FolderItem *special_trash = NULL;
2040         PrefsAccount *ac;
2041
2042         if (!folderview->selected) return;
2043         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2044         g_return_if_fail(item != NULL);
2045         g_return_if_fail(item->folder != NULL);
2046
2047         if (NULL != (ac = account_find_from_item(item)))
2048                 special_trash = account_get_special_folder(ac, F_TRASH);
2049
2050         if (item != item->folder->trash && item != special_trash
2051         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2052         
2053         if (prefs_common.ask_on_clean) {
2054                 if (alertpanel(_("Empty trash"),
2055                                _("Delete all messages in trash?"),
2056                                GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
2057                         return;
2058         }
2059         
2060         mlist = folder_item_get_msg_list(item);
2061         
2062         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2063                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2064                 if (MSG_IS_LOCKED(msginfo->flags))
2065                         continue;
2066                 /* is it partially received? (partial_recv isn't cached) */
2067                 if (msginfo->total_size != 0 && 
2068                     msginfo->size != (off_t)msginfo->total_size)
2069                         partial_mark_for_delete(msginfo);
2070         }
2071         procmsg_msg_list_free(mlist);
2072
2073         folder_item_remove_all_msg(item);
2074 }
2075
2076 static void folderview_search_cb(FolderView *folderview, guint action,
2077                                  GtkWidget *widget)
2078 {
2079         summary_search(folderview->summaryview);
2080 }
2081
2082 static void folderview_property_cb(FolderView *folderview, guint action,
2083                                    GtkWidget *widget)
2084 {
2085         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2086         FolderItem *item;
2087
2088         if (!folderview->selected) return;
2089
2090         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2091         g_return_if_fail(item != NULL);
2092         g_return_if_fail(item->folder != NULL);
2093
2094         if (folder_item_parent(item) == NULL)
2095                 return;
2096
2097         prefs_folder_item_open(item);
2098 }
2099
2100 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2101 {
2102         GSList *list = NULL;
2103         GSList *done = NULL;
2104         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2105         
2106         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2107                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2108                 &&  list->data != node) {
2109                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2110                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2111                 }
2112         }
2113         for (list = done; list != NULL; list = g_slist_next(list)) {
2114                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2115                                                                  list->data);
2116         }
2117         g_slist_free(done);
2118 }
2119
2120 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2121                             FolderItem *to_folder)
2122 {
2123         FolderItem *from_parent = NULL;
2124         FolderItem *new_folder = NULL;
2125         GtkCTreeNode *src_node = NULL;
2126         gchar *buf;
2127         gint status;
2128
2129         g_return_if_fail(folderview != NULL);
2130         g_return_if_fail(from_folder != NULL);
2131         g_return_if_fail(to_folder != NULL);
2132
2133         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2134         from_parent = folder_item_parent(from_folder);
2135         
2136         if (prefs_common.warn_dnd) {
2137                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2138                                         "sub-folder of '%s' ?"), from_folder->name,
2139                                         to_folder->name);
2140                 status = alertpanel_full(_("Move folder"), buf,
2141                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, TRUE,
2142                                          NULL, ALERT_QUESTION, G_ALERTALTERNATE);
2143                 g_free(buf);
2144
2145                 if (status != G_ALERTDEFAULT
2146                  && status != (G_ALERTDEFAULT | G_ALERTDISABLE))
2147                         return;
2148                 if (status & G_ALERTDISABLE)
2149                         prefs_common.warn_dnd = FALSE;
2150         }
2151
2152         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2153         STATUSBAR_PUSH(folderview->mainwin, buf);
2154         g_free(buf);
2155         summary_clear_all(folderview->summaryview);
2156         folderview->opened = NULL;
2157         folderview->selected = NULL;
2158         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2159         inc_lock();
2160         main_window_cursor_wait(folderview->mainwin);
2161         statusbar_verbosity_set(TRUE);
2162         folder_item_update_freeze();
2163         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2164                 statusbar_verbosity_set(FALSE);
2165                 main_window_cursor_normal(folderview->mainwin);
2166                 STATUSBAR_POP(folderview->mainwin);
2167                 folder_item_update_thaw();
2168                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2169
2170                 folderview_sort_folders(folderview, 
2171                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2172                                 NULL, to_folder), new_folder->folder);
2173                 folderview_select(folderview, new_folder);
2174         } else {
2175                 statusbar_verbosity_set(FALSE);         
2176                 main_window_cursor_normal(folderview->mainwin);
2177                 STATUSBAR_POP(folderview->mainwin);
2178                 folder_item_update_thaw();
2179                 switch (status) {
2180                 case F_MOVE_FAILED_DEST_IS_PARENT:
2181                         alertpanel_error(_("Source and destination are the same."));
2182                         break;
2183                 case F_MOVE_FAILED_DEST_IS_CHILD:
2184                         alertpanel_error(_("Can't move a folder to one of its children."));
2185                         break;
2186                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2187                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2188                         break;
2189                 default:
2190                         alertpanel_error(_("Move failed!"));
2191                         break;
2192                 }
2193         }       
2194         inc_unlock();           
2195         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2196 }
2197
2198 static gint folderview_clist_compare(GtkCList *clist,
2199                                      gconstpointer ptr1, gconstpointer ptr2)
2200 {
2201         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2202         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2203
2204         if (!item1->name)
2205                 return (item2->name != NULL);
2206         if (!item2->name)
2207                 return -1;
2208
2209         return g_utf8_collate(item1->name, item2->name);
2210 }
2211
2212 static void folderview_processing_cb(FolderView *folderview, guint action,
2213                                      GtkWidget *widget)
2214 {
2215         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2216         FolderItem *item;
2217         gchar *id, *title;
2218
2219         if (!folderview->selected) return;
2220
2221         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2222         g_return_if_fail(item != NULL);
2223         g_return_if_fail(item->folder != NULL);
2224
2225         id = folder_item_get_identifier(item);
2226         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2227         g_free (id);
2228
2229         prefs_filtering_open(&item->prefs->processing, title, NULL, NULL);
2230         g_free (title);
2231 }
2232
2233 void folderview_set_target_folder_color(gint color_op) 
2234 {
2235         gint firstone = 1;
2236         GList *list;
2237         FolderView *folderview;
2238
2239         for (list = folderview_list; list != NULL; list = list->next) {
2240                 folderview = (FolderView *)list->data;
2241                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2242                 if (firstone) {
2243                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2244                                 folderview->color_op;
2245                         firstone = 0;
2246                 }
2247         }
2248 }
2249
2250 static gchar *last_font = NULL;
2251 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2252 {
2253         /* force reinit */
2254         if (last_font) 
2255                 g_free(last_font);
2256         last_font = NULL;
2257         
2258 }
2259
2260 void folderview_reflect_prefs(void)
2261 {
2262         gboolean update_font = TRUE;
2263         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2264         FolderItem *item = folderview_get_selected_item(folderview);
2265         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2266                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2267         gint height = pos->value;
2268
2269         if (last_font && !strcmp(last_font, NORMAL_FONT))
2270                 update_font = FALSE;
2271
2272         if (last_font)
2273                 g_free(last_font);
2274         
2275         last_font = g_strdup(NORMAL_FONT);
2276
2277         if (update_font) {              
2278                 normal_style = normal_color_style = bold_style = 
2279                         bold_color_style = bold_tgtfold_style = NULL;
2280
2281                 folderview_init(folderview);
2282         }
2283         gtk_clist_freeze(GTK_CLIST(folderview->ctree));
2284         folderview_column_set_titles(folderview);
2285         folderview_set_all();
2286
2287         g_signal_handlers_block_by_func
2288                 (G_OBJECT(folderview->ctree),
2289                  G_CALLBACK(folderview_selected), folderview);
2290
2291         if (item) {
2292                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(
2293                         GTK_CTREE(folderview->ctree), NULL, item);
2294
2295                 folderview_select(folderview, item);
2296                 folderview->selected = node;
2297         }
2298
2299         g_signal_handlers_unblock_by_func
2300                 (G_OBJECT(folderview->ctree),
2301                  G_CALLBACK(folderview_selected), folderview);
2302
2303         pos = gtk_scrolled_window_get_vadjustment(
2304                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2305         gtk_adjustment_set_value(pos, height);
2306         gtk_clist_thaw(GTK_CLIST(folderview->ctree));
2307 }
2308
2309 static void drag_state_stop(FolderView *folderview)
2310 {
2311         if (folderview->drag_timer)
2312                 gtk_timeout_remove(folderview->drag_timer);
2313         folderview->drag_timer = 0;
2314         folderview->drag_node = NULL;
2315 }
2316
2317 static gint folderview_defer_expand(FolderView *folderview)
2318 {
2319         if (folderview->drag_node) {
2320                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2321                 if (folderview->drag_item->collapsed) {
2322                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2323                         folderview->nodes_to_recollapse = g_slist_append
2324                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2325                 }
2326         }
2327         folderview->drag_item  = NULL;
2328         folderview->drag_timer = 0;
2329         return FALSE;
2330 }
2331
2332 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2333 {
2334         /* the idea is that we call drag_state_start() whenever we want expansion to
2335          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2336          * we need to call drag_state_stop() */
2337         drag_state_stop(folderview);
2338         /* request expansion */
2339         if (0 != (folderview->drag_timer = gtk_timeout_add
2340                         (prefs_common.hover_timeout, 
2341                          (GtkFunction)folderview_defer_expand,
2342                          folderview))) {
2343                 folderview->drag_node = node;
2344                 folderview->drag_item = item;
2345         }                        
2346 }
2347
2348 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2349                                   FolderView       *folderview)
2350 {
2351         GdkDragContext *context;
2352
2353         g_return_if_fail(folderview != NULL);
2354         if (folderview->selected == NULL) return;
2355         if (folderview->nodes_to_recollapse) 
2356                 g_slist_free(folderview->nodes_to_recollapse);
2357         folderview->nodes_to_recollapse = NULL;
2358         context = gtk_drag_begin(widget, folderview->target_list,
2359                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2360         gtk_drag_set_icon_default(context);
2361 }
2362
2363 static void folderview_drag_data_get(GtkWidget        *widget,
2364                                      GdkDragContext   *drag_context,
2365                                      GtkSelectionData *selection_data,
2366                                      guint             info,
2367                                      guint             time,
2368                                      FolderView       *folderview)
2369 {
2370         FolderItem *item;
2371         GList *cur;
2372         gchar *source = NULL;
2373
2374         for (cur = GTK_CLIST(folderview->ctree)->selection;
2375              cur != NULL; cur = cur->next) {
2376                 item = gtk_ctree_node_get_row_data
2377                         (GTK_CTREE(folderview->ctree), 
2378                          GTK_CTREE_NODE(cur->data));
2379                 if (item) {
2380                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2381                         gtk_selection_data_set(selection_data,
2382                                                selection_data->target, 8,
2383                                                source, strlen(source));
2384                         break;
2385                 } else
2386                         return;
2387         }
2388 }
2389
2390 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2391 {
2392         FolderUpdateData *hookdata;
2393         FolderView *folderview;
2394         GtkWidget *ctree;
2395
2396         hookdata = source;
2397         folderview = (FolderView *) userdata;   
2398         g_return_val_if_fail(hookdata != NULL, FALSE);
2399         g_return_val_if_fail(folderview != NULL, FALSE);
2400
2401         ctree = folderview->ctree;
2402         g_return_val_if_fail(ctree != NULL, FALSE);
2403
2404         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2405                 folderview_create_folder_node(folderview, hookdata->item);
2406         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2407                 GtkCTreeNode *node;
2408
2409                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2410                 if (node != NULL)
2411                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2412         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2413                 folderview_set(folderview);
2414
2415         return FALSE;
2416 }
2417
2418 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2419                                           GdkDragContext *context,
2420                                           gint            x,
2421                                           gint            y,
2422                                           guint           time,
2423                                           FolderView     *folderview)
2424 {
2425         gint row, column;
2426         FolderItem *item = NULL, *src_item = NULL;
2427         GtkCTreeNode *node = NULL;
2428         gboolean acceptable = FALSE;
2429         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2430                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2431         int height = (int)pos->page_size;
2432         int total_height = (int)pos->upper;
2433         int vpos = (int) pos->value;
2434
2435         if (gtk_clist_get_selection_info
2436                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2437                 GtkWidget *srcwidget;
2438
2439                 if (y > height - 24 && height + vpos < total_height)
2440                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2441
2442                 if (y < 48 && y > 0)
2443                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2444
2445                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2446                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2447                 src_item = folderview->summaryview->folder_item;
2448
2449                 srcwidget = gtk_drag_get_source_widget(context);
2450                 if (srcwidget == folderview->summaryview->ctree) {
2451                         /* comes from summaryview */
2452                         /* we are copying messages, so only accept folder items that are not
2453                            the source item, are no root items and can copy messages */
2454                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2455                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2456                                 acceptable = TRUE;
2457                 } else if (srcwidget == folderview->ctree) {
2458                         /* comes from folderview */
2459                         /* we are moving folder items, only accept folders that are not
2460                            the source items and can copy messages and create folder items */
2461                         if (item && item->folder && src_item && src_item != item &&
2462                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2463                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2464                                 acceptable = TRUE;
2465                 }
2466         }
2467
2468         if (acceptable || (src_item && src_item == item))
2469                 drag_state_start(folderview, node, item);
2470         
2471         if (acceptable) {
2472                 g_signal_handlers_block_by_func
2473                         (G_OBJECT(widget),
2474                          G_CALLBACK(folderview_selected), folderview);
2475                 gtk_ctree_select(GTK_CTREE(widget), node);
2476                 g_signal_handlers_unblock_by_func
2477                         (G_OBJECT(widget),
2478                          G_CALLBACK(folderview_selected), folderview);
2479                 gdk_drag_status(context, 
2480                                         (context->actions == GDK_ACTION_COPY ?
2481                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2482         } else {
2483                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2484                 gdk_drag_status(context, 0, time);
2485         }
2486
2487         return acceptable;
2488 }
2489
2490 static void folderview_drag_leave_cb(GtkWidget      *widget,
2491                                      GdkDragContext *context,
2492                                      guint           time,
2493                                      FolderView     *folderview)
2494 {
2495         drag_state_stop(folderview);
2496         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2497 }
2498
2499 static void folderview_drag_received_cb(GtkWidget        *widget,
2500                                         GdkDragContext   *drag_context,
2501                                         gint              x,
2502                                         gint              y,
2503                                         GtkSelectionData *data,
2504                                         guint             info,
2505                                         guint             time,
2506                                         FolderView       *folderview)
2507 {
2508         gint row, column;
2509         FolderItem *item, *src_item;
2510         GtkCTreeNode *node;
2511
2512         drag_state_stop(folderview);
2513         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2514                 /* comes from summaryview */
2515                 if (gtk_clist_get_selection_info
2516                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2517                         return;
2518
2519                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2520                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2521                 src_item = folderview->summaryview->folder_item;
2522                 
2523                 /* re-check (due to acceptable possibly set for folder moves */
2524                 if (!(item && item->folder && item->path && !item->no_select && 
2525                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2526                         return;
2527                 }
2528                 if (item && src_item) {
2529                         switch (drag_context->action) {
2530                         case GDK_ACTION_COPY:
2531                                 summary_copy_selected_to(folderview->summaryview, item);
2532                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2533                                 break;
2534                         case GDK_ACTION_MOVE:
2535                         case GDK_ACTION_DEFAULT:
2536                         default:
2537                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2538                                         summary_copy_selected_to(folderview->summaryview, item);
2539                                 else
2540                                         summary_move_selected_to(folderview->summaryview, item);
2541                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2542                         }
2543                 } else
2544                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2545         } else {
2546                 /* comes from folderview */
2547                 char *source;
2548                 gboolean folder_is_normal = TRUE;
2549                 
2550                 source = data->data + 17;
2551                 if (gtk_clist_get_selection_info
2552                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2553                     || *source == 0) {
2554                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2555                         return;
2556                 }
2557                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2558                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2559                 src_item = folder_find_item_from_identifier(source);
2560
2561                 folder_is_normal = 
2562                         src_item != NULL &&
2563                         src_item->stype == F_NORMAL &&
2564                         !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2565                         !folder_has_parent_of_type(src_item, F_DRAFT) &&
2566                         !folder_has_parent_of_type(src_item, F_QUEUE) &&
2567                         !folder_has_parent_of_type(src_item, F_TRASH);
2568                 if (!item || item->no_select || !src_item 
2569                 ||  !folder_is_normal) {
2570                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2571                         return;
2572                 }
2573
2574                 folderview_move_folder(folderview, src_item, item);
2575                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2576         }
2577         folderview->nodes_to_recollapse = NULL;
2578 }
2579
2580 static void folderview_drag_end_cb(GtkWidget        *widget, 
2581                                    GdkDragContext   *drag_context,
2582                                    FolderView       *folderview)
2583 {
2584         drag_state_stop(folderview);
2585         g_slist_free(folderview->nodes_to_recollapse);
2586         folderview->nodes_to_recollapse = NULL;
2587 }
2588
2589 void folderview_register_popup(FolderViewPopup *fpopup)
2590 {
2591         GList *folderviews;
2592
2593         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2594                 FolderView *folderview = folderviews->data;
2595                 GtkItemFactory *factory;
2596
2597                 factory = create_ifactory(folderview, fpopup);
2598                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2599         }       
2600         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2601 }
2602
2603 void folderview_unregister_popup(FolderViewPopup *fpopup)
2604 {
2605         GList *folderviews;
2606
2607         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2608                 FolderView *folderview = folderviews->data;
2609
2610                 g_hash_table_remove(folderview->popups, fpopup->klass);
2611         }       
2612         g_hash_table_remove(folderview_popups, fpopup->klass);
2613 }