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