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