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