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