5e6fc35bc38a266027f57b09c437617a91c3a564
[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_ctree_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 void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1214 {
1215         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1216         GtkStyle *style = NULL;
1217         GtkStyle *color_style = NULL;
1218         FolderItem *item;
1219         GdkPixmap *xpm, *openxpm;
1220         GdkBitmap *mask, *openmask;
1221         static GdkPixmap *searchicon;
1222         static GdkBitmap *searchmask;
1223         gboolean mark = FALSE;
1224         gchar *name;
1225         gchar *str;
1226         gboolean add_unread_mark;
1227         gboolean add_sub_match_mark;
1228         gboolean use_bold, use_color;
1229         gint *col_pos = folderview->col_pos;
1230         SpecialFolderItemType stype;
1231         
1232         item = gtk_ctree_node_get_row_data(ctree, node);
1233         g_return_if_fail(item != NULL);
1234
1235         mark = (item->marked_msgs != 0);
1236
1237         stype = item->stype;
1238         if (stype == F_NORMAL) {
1239                 if (folder_has_parent_of_type(item, F_TRASH))
1240                         stype = F_TRASH;
1241                 else if (folder_has_parent_of_type(item, F_DRAFT))
1242                         stype = F_DRAFT;
1243                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1244                         stype = F_OUTBOX;
1245                 else if (folder_has_parent_of_type(item, F_QUEUE))
1246                         stype = F_QUEUE;
1247         }
1248         switch (stype) {
1249         case F_INBOX:
1250                 if (item->hide_read_msgs) {
1251                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1252                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1253                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1254                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1255                 } else {
1256                         xpm = mark?m_inboxxpm:inboxxpm;
1257                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1258                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1259                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1260                 }
1261                 break;
1262         case F_OUTBOX:
1263                 if (item->hide_read_msgs) {
1264                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1265                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1266                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1267                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1268                 } else {
1269                         xpm = mark?m_outboxxpm:outboxxpm;
1270                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1271                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1272                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1273                 }
1274                 break;
1275         case F_QUEUE:
1276                 if (item->hide_read_msgs) {
1277                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1278                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1279                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1280                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1281                 } else {
1282                         xpm = mark?m_queuexpm:queuexpm;
1283                         mask = mark?m_queuexpmmask:queuexpmmask;
1284                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1285                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1286                 }
1287                 break;
1288         case F_TRASH:
1289                 if (item->hide_read_msgs) {
1290                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1291                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1292                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1293                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1294                 } else {
1295                         xpm = mark?m_trashxpm:trashxpm;
1296                         mask = mark?m_trashxpmmask:trashxpmmask;
1297                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1298                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1299                 }
1300                 break;
1301         case F_DRAFT:
1302                 xpm = mark?m_draftsxpm:draftsxpm;
1303                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1304                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1305                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1306                 break;
1307         default:
1308                 if (item->hide_read_msgs) {
1309                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1310                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1311                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1312                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1313                 } else {
1314                         xpm = mark?m_folderxpm:folderxpm;
1315                         mask = mark?m_folderxpmmask:folderxpmmask;
1316                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1317                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1318                 }
1319         }
1320         
1321         if (item->no_select) {
1322                 xpm = openxpm = noselectxpm;
1323                 mask = openmask = noselectxpmmask;
1324         }
1325
1326         name = folder_item_get_name(item);
1327
1328         if (!GTK_CTREE_ROW(node)->expanded) {
1329                 add_unread_mark = folderview_have_unread_children(
1330                                         folderview, item);
1331                 add_sub_match_mark = folderview_have_matching_children(
1332                                         folderview, item);
1333         } else {
1334                 add_unread_mark = FALSE;
1335                 add_sub_match_mark = FALSE;
1336         }
1337
1338         if (item->search_match) {
1339                 if (!searchicon) {
1340                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1341                          &searchicon, &searchmask);
1342                 }
1343                 xpm = openxpm = searchicon;
1344                 mask = openmask = searchmask;
1345         }
1346
1347         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1348             prefs_common.display_folder_unread) {
1349                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1350                                       add_unread_mark ? "+" : "");
1351                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1352                                         xpm, mask, openxpm, openmask,
1353                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1354                 g_free(str);
1355         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1356                     prefs_common.display_folder_unread) 
1357                    || add_sub_match_mark) {
1358
1359                 if (item->unread_msgs > 0)
1360                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1361                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1362                                               item->unreadmarked_msgs > 0 ? "!":"");
1363                 else
1364                         str = g_strdup_printf("%s (+)", name);
1365                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1366                                         xpm, mask, openxpm, openmask,
1367                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1368                 g_free(str);
1369         } else {
1370                 str = g_strdup_printf("%s%s", name, 
1371                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1372         
1373                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1374                                         xpm, mask, openxpm, openmask,
1375                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1376                 g_free(str);
1377         }
1378         g_free(name);
1379
1380         if (!folder_item_parent(item)) {
1381                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1382                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1383                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1384         } else {
1385                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1386                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1387                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1388         }
1389
1390         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1391             folder_has_parent_of_type(item, F_DRAFT) ||
1392             folder_has_parent_of_type(item, F_TRASH)) {
1393                 use_bold = use_color = FALSE;
1394         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1395                 /* highlight queue folder if there are any messages */
1396                 use_bold = use_color = (item->total_msgs > 0);
1397         } else {
1398                 /* if unread messages exist, print with bold font */
1399                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1400                                 || add_unread_mark;
1401                 /* if new messages exist, print with colored letter */
1402                 use_color =
1403                         (item->new_msgs > 0) ||
1404                         (add_unread_mark &&
1405                          folderview_have_new_children(folderview, item));       
1406         }
1407
1408         gtk_ctree_node_set_foreground(ctree, node, NULL);
1409
1410         if (use_bold) {
1411                 if (item->prefs->color > 0 && !use_color) {
1412                         GdkColor gdk_color;
1413
1414                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1415                         color_style = gtk_style_copy(bold_style);
1416                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1417                         style = color_style;
1418                 } else if (use_color)
1419                         style = bold_color_style;
1420                 else
1421                         style = bold_style;
1422                 if (item->op_count > 0) {
1423                         style = bold_tgtfold_style;
1424                 }
1425         } else if (use_color) {
1426                 style = normal_color_style;
1427                 gtk_ctree_node_set_foreground(ctree, node,
1428                                               &folderview->color_new);
1429         } else if (item->op_count > 0) {
1430                 style = bold_tgtfold_style;
1431         } else if (item->prefs->color > 0) {
1432                 GdkColor gdk_color;
1433                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1434                 color_style = gtk_style_copy(normal_style);
1435                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1436                 style = color_style;
1437         } else {
1438                 style = normal_style;
1439         }
1440
1441         gtk_ctree_node_set_row_style(ctree, node, style);
1442
1443         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1444                 folderview_update_node(folderview, node);
1445 }
1446
1447 #if !CLAWS /* keep it here for syncs */
1448 void folderview_update_item(FolderItem *item, gboolean update_summary)
1449 {
1450         GList *list;
1451         FolderView *folderview;
1452         GtkCTree *ctree;
1453         GtkCTreeNode *node;
1454
1455         g_return_if_fail(item != NULL);
1456
1457         for (list = folderview_list; list != NULL; list = list->next) {
1458                 folderview = (FolderView *)list->data;
1459                 ctree = GTK_CTREE(folderview->ctree);
1460
1461                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1462                 if (node) {
1463                         folderview_update_node(folderview, node);
1464                         if (update_summary && folderview->opened == node)
1465                                 summary_show(folderview->summaryview,
1466                                              item, FALSE);
1467                 }
1468         }
1469 }
1470 #endif
1471
1472 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1473 {
1474         GList *list;
1475         FolderView *folderview;
1476         GtkCTree *ctree;
1477         GtkCTreeNode *node;
1478
1479         g_return_if_fail(item != NULL);
1480
1481         for (list = folderview_list; list != NULL; list = list->next) {
1482                 folderview = (FolderView *)list->data;
1483                 ctree = GTK_CTREE(folderview->ctree);
1484
1485                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1486                 if (node) {
1487                         item->search_match = matches;
1488                         folderview_update_node(folderview, node);
1489                 }
1490         }
1491 }
1492
1493 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1494 {
1495         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1496         FolderView *folderview = (FolderView *)data;
1497         GtkCTree *ctree;
1498         GtkCTreeNode *node;
1499         g_return_val_if_fail(update_info != NULL, TRUE);
1500         g_return_val_if_fail(update_info->item != NULL, TRUE);
1501         g_return_val_if_fail(folderview != NULL, FALSE);
1502
1503         ctree = GTK_CTREE(folderview->ctree);
1504
1505         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1506         if (node) {
1507                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1508                         folderview_update_node(folderview, node);
1509                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1510                         summary_show(folderview->summaryview, update_info->item);
1511         }
1512         
1513         return FALSE;
1514 }
1515
1516 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1517                                                 gpointer data)
1518 {
1519         /* CLAWS: share this joy with other hook functions ... */
1520         folder_item_update((FolderItem *)key, 
1521                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1522 }
1523
1524 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1525 {
1526         GList *list;
1527         FolderItemUpdateFlags flags;
1528         
1529         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1530                 : 0;
1531         for (list = folderview_list; list != NULL; list = list->next)
1532                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1533                                      GINT_TO_POINTER(flags));
1534 }
1535
1536 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1537                                       GNode *gnode, GtkCTreeNode *cnode,
1538                                       gpointer data)
1539 {
1540         FolderView *folderview = (FolderView *)data;
1541         FolderItem *item = FOLDER_ITEM(gnode->data);
1542
1543         g_return_val_if_fail(item != NULL, FALSE);
1544
1545         gtk_ctree_node_set_row_data(ctree, cnode, item);
1546         folderview_update_node(folderview, cnode);
1547
1548         return TRUE;
1549 }
1550
1551 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1552                                    gpointer data)
1553 {
1554         FolderView *folderview = (FolderView *)data;
1555         FolderItem *item;
1556
1557         if (GTK_CTREE_ROW(node)->children) {
1558                 item = gtk_ctree_node_get_row_data(ctree, node);
1559                 g_return_if_fail(item != NULL);
1560
1561                 if (!item->collapsed)
1562                         gtk_ctree_expand(ctree, node);
1563                 else
1564                         folderview_update_node(folderview, node);
1565         }
1566 }
1567
1568 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1569                                GtkCTreeNode *root, GtkCTreeNode **prev)
1570 {
1571         if (item) {
1572                 GtkCTreeNode *node, *parent, *sibling;
1573
1574                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1575                 if (!node)
1576                         g_warning("%s not found.\n", item->path);
1577                 else {
1578                         parent = GTK_CTREE_ROW(node)->parent;
1579                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1580                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1581                         else
1582                                 sibling = GTK_CTREE_ROW(parent)->children;
1583                         while (sibling) {
1584                                 FolderItem *tmp;
1585
1586                                 tmp = gtk_ctree_node_get_row_data
1587                                         (ctree, sibling);
1588                                 if (tmp->stype != F_NORMAL)
1589                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1590                                 else
1591                                         break;
1592                         }
1593                         if (node != sibling)
1594                                 gtk_ctree_move(ctree, node, parent, sibling);
1595                 }
1596
1597                 *prev = node;
1598         }
1599 }
1600
1601 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1602                                     Folder *folder)
1603 {
1604         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1605         GtkCTreeNode *prev = NULL;
1606
1607         gtk_sctree_sort_recursive(ctree, root);
1608
1609         if (root && GTK_CTREE_ROW(root)->parent) return;
1610
1611         set_special_folder(ctree, folder->inbox, root, &prev);
1612         set_special_folder(ctree, folder->outbox, root, &prev);
1613         set_special_folder(ctree, folder->draft, root, &prev);
1614         set_special_folder(ctree, folder->queue, root, &prev);
1615         set_special_folder(ctree, folder->trash, root, &prev);
1616 }
1617
1618 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1619 {
1620         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1621         GtkCTreeNode *root;
1622
1623         g_return_if_fail(folder != NULL);
1624
1625         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1626                                       folderview_gnode_func, folderview);
1627         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1628                                 folderview);
1629         folderview_sort_folders(folderview, root, folder);
1630 }
1631
1632 /* callback functions */
1633
1634 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1635                                           FolderView *folderview)
1636 {
1637         GtkCList *clist = GTK_CLIST(ctree);
1638         gint prev_row = -1, row = -1, column = -1;
1639         FolderItem *item;
1640         Folder *folder;
1641         FolderViewPopup *fpopup;
1642         GtkItemFactory *fpopup_factory;
1643         GtkWidget *popup;
1644         FolderItem *special_trash = NULL;
1645         PrefsAccount *ac;
1646
1647         if (!event) return FALSE;
1648
1649         if (event->button == 1) {
1650                 folderview->open_folder = TRUE;
1651
1652                 if (event->type == GDK_2BUTTON_PRESS) {
1653                         if (clist->selection) {
1654                                 GtkCTreeNode *node;
1655
1656                                 node = GTK_CTREE_NODE(clist->selection->data);
1657                                 if (node)
1658                                         gtk_ctree_toggle_expansion(
1659                                                 GTK_CTREE(ctree),
1660                                                 node);
1661                         }
1662                 }
1663                 return FALSE;
1664         }
1665
1666         if (event->button == 2 || event->button == 3) {
1667                 /* right clicked */
1668                 if (clist->selection) {
1669                         GtkCTreeNode *node;
1670
1671                         node = GTK_CTREE_NODE(clist->selection->data);
1672                         if (node)
1673                                 prev_row = gtkut_ctree_get_nth_from_node
1674                                         (GTK_CTREE(ctree), node);
1675                 }
1676
1677                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1678                                                   &row, &column))
1679                         return FALSE;
1680                 if (prev_row != row) {
1681                         gtk_clist_unselect_all(clist);
1682                         if (event->button == 2)
1683                                 folderview_select_node
1684                                         (folderview,
1685                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1686                                                             row));
1687                         else
1688                                 gtk_clist_select_row(clist, row, column);
1689                 }
1690         }
1691
1692         if (event->button != 3) return FALSE;
1693
1694         item = gtk_clist_get_row_data(clist, row);
1695         g_return_val_if_fail(item != NULL, FALSE);
1696         g_return_val_if_fail(item->folder != NULL, FALSE);
1697         folder = item->folder;
1698
1699         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1700         if (fpopup != NULL)
1701                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1702         else {
1703                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1704                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1705         }
1706
1707         if (fpopup->set_sensitivity != NULL)
1708                 fpopup->set_sensitivity(fpopup_factory, item);
1709
1710         if (NULL != (ac = account_find_from_item(item)))
1711                 special_trash = account_get_special_folder(ac, F_TRASH);
1712
1713         if ((item == folder->trash || item == special_trash
1714              || folder_has_parent_of_type(item, F_TRASH)) &&
1715             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1716                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1717                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1718         } else if (item != folder->trash && (special_trash == NULL || item != special_trash)
1719                 && !folder_has_parent_of_type(item, F_TRASH)) {
1720                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1721                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1722         }
1723         
1724 #define SET_SENS(name, sens) \
1725         menu_set_sensitive(fpopup_factory, name, sens)
1726
1727         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1728         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1729                  folderview->selected == folderview->opened);
1730         SET_SENS("/Properties...", item->node->parent != NULL);
1731         SET_SENS("/Processing...", item->node->parent != NULL);
1732         if (item == folder->trash || item == special_trash
1733             || folder_has_parent_of_type(item, F_TRASH)) {
1734                 GSList *msglist = folder_item_get_msg_list(item);
1735                 SET_SENS("/Empty trash...", msglist != NULL);
1736                 procmsg_msg_list_free(msglist);
1737         }
1738 #undef SET_SENS
1739
1740         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1741         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1742                        event->button, event->time);
1743
1744         return FALSE;
1745 }
1746
1747 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1748                                            FolderView *folderview)
1749 {
1750         if (!event) return FALSE;
1751
1752         if (event->button == 1 && folderview->open_folder == FALSE &&
1753             folderview->opened != NULL) {
1754                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1755                                           folderview->opened);
1756                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1757         }
1758
1759         return FALSE;
1760 }
1761
1762 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1763                                        FolderView *folderview)
1764 {
1765         if (!event) return FALSE;
1766
1767         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1768                 return FALSE;
1769
1770         switch (event->keyval) {
1771         case GDK_Return:
1772                 if (folderview->selected) {
1773                         folderview_select_node(folderview,
1774                                                folderview->selected);
1775                 }
1776                 break;
1777         case GDK_space:
1778                 if (folderview->selected) {
1779                         if (folderview->opened == folderview->selected &&
1780                             (!folderview->summaryview->folder_item ||
1781                              folderview->summaryview->folder_item->total_msgs == 0))
1782                                 folderview_select_next_unread(folderview);
1783                         else
1784                                 folderview_select_node(folderview,
1785                                                        folderview->selected);
1786                 }
1787                 break;
1788         default:
1789                 break;
1790         }
1791
1792         return FALSE;
1793 }
1794
1795 static void summary_freeze_for_proc(gpointer data)
1796 {
1797         FolderView *folderview = (FolderView *)data;
1798         debug_print("freezing during processing...\n");
1799         gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
1800 }
1801
1802 static void summary_thaw_for_proc(gpointer data)
1803 {
1804         FolderView *folderview = (FolderView *)data;
1805         debug_print("thawing after processing\n");
1806         gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
1807 }
1808
1809 void folderview_process_open(FolderView *folderview, FolderItem *item)
1810 {
1811         if (!item)
1812                 return;
1813
1814         folder_item_update_freeze();
1815         folder_item_process_open(item, 
1816                 summary_freeze_for_proc,
1817                 summary_thaw_for_proc,
1818                 folderview);
1819         folder_item_update_thaw();
1820         
1821         return; 
1822 }
1823
1824 gboolean folderview_process_open_cb(gpointer data)
1825 {
1826         FolderView *folderview = (FolderView *)data;
1827         FolderItem *item = NULL;
1828         
1829         if (!folderview || !folderview->opened) {
1830                 debug_print("opened NULL\n");
1831                 return FALSE;
1832         }
1833         item = gtk_ctree_node_get_row_data(
1834                 GTK_CTREE(folderview->ctree), 
1835                 folderview->opened);
1836         
1837         if (!item)
1838                 return FALSE;
1839
1840         folderview_process_open(folderview, item);
1841         
1842         return FALSE;   
1843 }
1844
1845 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1846                                 gint column, FolderView *folderview)
1847 {
1848         static gboolean can_select = TRUE;      /* exclusive lock */
1849         gboolean opened;
1850         FolderItem *item;
1851         gchar *buf;
1852
1853         folderview->selected = row;
1854
1855         if (folderview->opened == row) {
1856                 folderview->open_folder = FALSE;
1857                 return;
1858         }
1859         
1860         if (!can_select || summary_is_locked(folderview->summaryview)) {
1861                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1862                 gtk_ctree_select(ctree, folderview->opened);
1863                 return;
1864         }
1865
1866         if (!folderview->open_folder) return;
1867
1868         item = gtk_ctree_node_get_row_data(ctree, row);
1869         if (!item || item->no_select) return;
1870
1871         can_select = FALSE;
1872
1873         /* Save cache for old folder */
1874         /* We don't want to lose all caches if sylpheed crashed */
1875         if (folderview->opened) {
1876                 FolderItem *olditem;
1877                 
1878                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1879                 if (olditem) {
1880                         /* will be null if we just moved the previously opened folder */
1881                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1882                         summary_show(folderview->summaryview, NULL);
1883                         folder_item_close(olditem);
1884                 }
1885         }
1886
1887         /* CLAWS: set compose button type: news folder items 
1888          * always have a news folder as parent */
1889         if (item->folder) 
1890                 toolbar_set_compose_button
1891                         (folderview->mainwin->toolbar,
1892                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1893                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1894
1895         if (item->path)
1896                 debug_print("Folder %s is selected\n", item->path);
1897
1898         if (!GTK_CTREE_ROW(row)->children)
1899                 gtk_ctree_expand(ctree, row);
1900         if (folderview->opened &&
1901             !GTK_CTREE_ROW(folderview->opened)->children)
1902                 gtk_ctree_collapse(ctree, folderview->opened);
1903
1904         /* ungrab the mouse event */
1905         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1906                 gtk_grab_remove(GTK_WIDGET(ctree));
1907                 if (gdk_pointer_is_grabbed())
1908                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1909         }
1910
1911         /* Open Folder */
1912         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1913                                         item->path : "(null)");
1914         debug_print("%s\n", buf);
1915         STATUSBAR_PUSH(folderview->mainwin, buf);
1916         g_free(buf);
1917
1918         main_window_cursor_wait(folderview->mainwin);
1919
1920         if (folder_item_open(item) != 0) {
1921                 main_window_cursor_normal(folderview->mainwin);
1922                 STATUSBAR_POP(folderview->mainwin);
1923
1924                 alertpanel_error(_("Folder could not be opened."));
1925
1926                 folderview->open_folder = FALSE;
1927                 can_select = TRUE;
1928
1929                 return;
1930         }
1931         
1932         if (pre_global_processing || item->prefs->processing 
1933             || post_global_processing)
1934                 folderview_process_open(folderview, item);
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         if (!pre_global_processing && !item->prefs->processing 
1955             && !post_global_processing)
1956                 g_timeout_add(0, folderview_process_open_cb, folderview);
1957
1958         STATUSBAR_POP(folderview->mainwin);
1959
1960         folderview->open_folder = FALSE;
1961         can_select = TRUE;
1962 }
1963
1964 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1965                                      FolderView *folderview)
1966 {
1967         FolderItem *item;
1968
1969         item = gtk_ctree_node_get_row_data(ctree, node);
1970         g_return_if_fail(item != NULL);
1971         item->collapsed = FALSE;
1972         folderview_update_node(folderview, node);
1973 }
1974
1975 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1976                                       FolderView *folderview)
1977 {
1978         FolderItem *item;
1979
1980         item = gtk_ctree_node_get_row_data(ctree, node);
1981         g_return_if_fail(item != NULL);
1982         item->collapsed = TRUE;
1983         folderview_update_node(folderview, node);
1984 }
1985
1986 static void folderview_popup_close(GtkMenuShell *menu_shell,
1987                                    FolderView *folderview)
1988 {
1989         if (!folderview->opened) return;
1990
1991         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1992 }
1993
1994 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1995                                    FolderView *folderview)
1996 {
1997         FolderColumnType type = folderview->col_state[column].type;
1998
1999         prefs_common.folder_col_size[type] = width;
2000 }
2001
2002 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
2003 {
2004         GNode *srcnode;
2005
2006         folderview_create_folder_node(folderview, item);
2007
2008         if (!item || !item->folder || !item->folder->node)
2009                 return;
2010
2011         srcnode = item->folder->node;   
2012         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2013         srcnode = srcnode->children;
2014         while (srcnode != NULL) {
2015                 if (srcnode && srcnode->data) {
2016                         FolderItem *next_item = (FolderItem*) srcnode->data;
2017                         folderview_create_folder_node_recursive(folderview, next_item);
2018                 }
2019                 srcnode = srcnode->next;
2020         }
2021 }
2022
2023 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2024 {
2025         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2026         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2027         GtkCTreeNode *node, *parent_node;
2028         gint *col_pos = folderview->col_pos;
2029         FolderItemUpdateData hookdata;
2030
2031         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2032         if (parent_node == NULL)
2033                 return;
2034
2035         gtk_clist_freeze(GTK_CLIST(ctree));
2036
2037         text[col_pos[F_COL_FOLDER]] = item->name;
2038         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
2039                                      FOLDER_SPACING,
2040                                      folderxpm, folderxpmmask,
2041                                      folderopenxpm, folderopenxpmmask,
2042                                      FALSE, FALSE);
2043         gtk_ctree_expand(ctree, parent_node);
2044         gtk_ctree_node_set_row_data(ctree, node, item);
2045         if (normal_style)
2046                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
2047         folderview_sort_folders(folderview, parent_node, item->folder);
2048
2049         hookdata.item = item;
2050         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2051         hookdata.msg = NULL;
2052         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2053
2054         gtk_clist_thaw(GTK_CLIST(ctree));
2055 }
2056
2057 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
2058                                       GtkWidget *widget)
2059 {
2060         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2061         FolderItem *item;
2062         GSList *mlist = NULL;
2063         GSList *cur = NULL;
2064         FolderItem *special_trash = NULL;
2065         PrefsAccount *ac;
2066
2067         if (!folderview->selected) return;
2068         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2069         g_return_if_fail(item != NULL);
2070         g_return_if_fail(item->folder != NULL);
2071
2072         if (NULL != (ac = account_find_from_item(item)))
2073                 special_trash = account_get_special_folder(ac, F_TRASH);
2074
2075         if (item != item->folder->trash && item != special_trash
2076         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2077         
2078         if (prefs_common.ask_on_clean) {
2079                 if (alertpanel(_("Empty trash"),
2080                                _("Delete all messages in trash?"),
2081                                GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
2082                         return;
2083         }
2084         
2085         mlist = folder_item_get_msg_list(item);
2086         
2087         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2088                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2089                 if (MSG_IS_LOCKED(msginfo->flags))
2090                         continue;
2091                 /* is it partially received? (partial_recv isn't cached) */
2092                 if (msginfo->total_size != 0 && 
2093                     msginfo->size != (off_t)msginfo->total_size)
2094                         partial_mark_for_delete(msginfo);
2095         }
2096         procmsg_msg_list_free(mlist);
2097
2098         folder_item_remove_all_msg(item);
2099 }
2100
2101 static void folderview_search_cb(FolderView *folderview, guint action,
2102                                  GtkWidget *widget)
2103 {
2104         summary_search(folderview->summaryview);
2105 }
2106
2107 static void folderview_property_cb(FolderView *folderview, guint action,
2108                                    GtkWidget *widget)
2109 {
2110         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2111         FolderItem *item;
2112
2113         if (!folderview->selected) return;
2114
2115         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2116         g_return_if_fail(item != NULL);
2117         g_return_if_fail(item->folder != NULL);
2118
2119         if (folder_item_parent(item) == NULL)
2120                 return;
2121
2122         prefs_folder_item_open(item);
2123 }
2124
2125 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2126 {
2127         GSList *list = NULL;
2128         GSList *done = NULL;
2129         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2130         
2131         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2132                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2133                 &&  list->data != node) {
2134                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2135                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2136                 }
2137         }
2138         for (list = done; list != NULL; list = g_slist_next(list)) {
2139                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2140                                                                  list->data);
2141         }
2142         g_slist_free(done);
2143 }
2144
2145 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2146                             FolderItem *to_folder)
2147 {
2148         FolderItem *from_parent = NULL;
2149         FolderItem *new_folder = NULL;
2150         GtkCTreeNode *src_node = NULL;
2151         gchar *buf;
2152         gint status;
2153
2154         g_return_if_fail(folderview != NULL);
2155         g_return_if_fail(from_folder != NULL);
2156         g_return_if_fail(to_folder != NULL);
2157
2158         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2159         from_parent = folder_item_parent(from_folder);
2160         
2161         if (prefs_common.warn_dnd) {
2162                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2163                                         "sub-folder of '%s' ?"), from_folder->name,
2164                                         to_folder->name);
2165                 status = alertpanel_full(_("Move folder"), buf,
2166                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, TRUE,
2167                                          NULL, ALERT_QUESTION, G_ALERTALTERNATE);
2168                 g_free(buf);
2169
2170                 if (status != G_ALERTDEFAULT
2171                  && status != (G_ALERTDEFAULT | G_ALERTDISABLE))
2172                         return;
2173                 if (status & G_ALERTDISABLE)
2174                         prefs_common.warn_dnd = FALSE;
2175         }
2176
2177         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2178         STATUSBAR_PUSH(folderview->mainwin, buf);
2179         g_free(buf);
2180         summary_clear_all(folderview->summaryview);
2181         folderview->opened = NULL;
2182         folderview->selected = NULL;
2183         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2184         inc_lock();
2185         main_window_cursor_wait(folderview->mainwin);
2186         statusbar_verbosity_set(TRUE);
2187         folder_item_update_freeze();
2188         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2189                 statusbar_verbosity_set(FALSE);
2190                 main_window_cursor_normal(folderview->mainwin);
2191                 STATUSBAR_POP(folderview->mainwin);
2192                 folder_item_update_thaw();
2193                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2194
2195                 folderview_sort_folders(folderview, 
2196                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2197                                 NULL, to_folder), new_folder->folder);
2198                 folderview_select(folderview, new_folder);
2199         } else {
2200                 statusbar_verbosity_set(FALSE);         
2201                 main_window_cursor_normal(folderview->mainwin);
2202                 STATUSBAR_POP(folderview->mainwin);
2203                 folder_item_update_thaw();
2204                 switch (status) {
2205                 case F_MOVE_FAILED_DEST_IS_PARENT:
2206                         alertpanel_error(_("Source and destination are the same."));
2207                         break;
2208                 case F_MOVE_FAILED_DEST_IS_CHILD:
2209                         alertpanel_error(_("Can't move a folder to one of its children."));
2210                         break;
2211                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2212                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2213                         break;
2214                 default:
2215                         alertpanel_error(_("Move failed!"));
2216                         break;
2217                 }
2218         }       
2219         inc_unlock();           
2220         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2221 }
2222
2223 static gint folderview_clist_compare(GtkCList *clist,
2224                                      gconstpointer ptr1, gconstpointer ptr2)
2225 {
2226         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2227         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2228
2229         if (!item1->name)
2230                 return (item2->name != NULL);
2231         if (!item2->name)
2232                 return -1;
2233
2234         return g_utf8_collate(item1->name, item2->name);
2235 }
2236
2237 static void folderview_processing_cb(FolderView *folderview, guint action,
2238                                      GtkWidget *widget)
2239 {
2240         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2241         FolderItem *item;
2242         gchar *id, *title;
2243
2244         if (!folderview->selected) return;
2245
2246         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2247         g_return_if_fail(item != NULL);
2248         g_return_if_fail(item->folder != NULL);
2249
2250         id = folder_item_get_identifier(item);
2251         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2252         g_free (id);
2253
2254         prefs_filtering_open(&item->prefs->processing, title, NULL, NULL);
2255         g_free (title);
2256 }
2257
2258 void folderview_set_target_folder_color(gint color_op) 
2259 {
2260         gint firstone = 1;
2261         GList *list;
2262         FolderView *folderview;
2263
2264         for (list = folderview_list; list != NULL; list = list->next) {
2265                 folderview = (FolderView *)list->data;
2266                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2267                 if (firstone) {
2268                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2269                                 folderview->color_op;
2270                         firstone = 0;
2271                 }
2272         }
2273 }
2274
2275 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2276 {
2277 }
2278
2279 void folderview_reflect_prefs(void)
2280 {
2281         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2282         FolderItem *item = folderview_get_selected_item(folderview);    
2283         normal_style = normal_color_style = bold_style = 
2284                 bold_color_style = bold_tgtfold_style = NULL;
2285
2286         folderview_init(folderview);
2287         folderview_column_set_titles(folderview);
2288         folderview_set_all();
2289         if (item)
2290                 folderview_select(folderview, item);
2291 }
2292
2293 static void drag_state_stop(FolderView *folderview)
2294 {
2295         if (folderview->drag_timer)
2296                 gtk_timeout_remove(folderview->drag_timer);
2297         folderview->drag_timer = 0;
2298         folderview->drag_node = NULL;
2299 }
2300
2301 static gint folderview_defer_expand(FolderView *folderview)
2302 {
2303         if (folderview->drag_node) {
2304                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2305                 if (folderview->drag_item->collapsed) {
2306                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2307                         folderview->nodes_to_recollapse = g_slist_append
2308                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2309                 }
2310         }
2311         folderview->drag_item  = NULL;
2312         folderview->drag_timer = 0;
2313         return FALSE;
2314 }
2315
2316 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2317 {
2318         /* the idea is that we call drag_state_start() whenever we want expansion to
2319          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2320          * we need to call drag_state_stop() */
2321         drag_state_stop(folderview);
2322         /* request expansion */
2323         if (0 != (folderview->drag_timer = gtk_timeout_add
2324                         (prefs_common.hover_timeout, 
2325                          (GtkFunction)folderview_defer_expand,
2326                          folderview))) {
2327                 folderview->drag_node = node;
2328                 folderview->drag_item = item;
2329         }                        
2330 }
2331
2332 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2333                                   FolderView       *folderview)
2334 {
2335         GdkDragContext *context;
2336
2337         g_return_if_fail(folderview != NULL);
2338         if (folderview->selected == NULL) return;
2339         if (folderview->nodes_to_recollapse) 
2340                 g_slist_free(folderview->nodes_to_recollapse);
2341         folderview->nodes_to_recollapse = NULL;
2342         context = gtk_drag_begin(widget, folderview->target_list,
2343                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2344         gtk_drag_set_icon_default(context);
2345 }
2346
2347 static void folderview_drag_data_get(GtkWidget        *widget,
2348                                      GdkDragContext   *drag_context,
2349                                      GtkSelectionData *selection_data,
2350                                      guint             info,
2351                                      guint             time,
2352                                      FolderView       *folderview)
2353 {
2354         FolderItem *item;
2355         GList *cur;
2356         gchar *source = NULL;
2357
2358         for (cur = GTK_CLIST(folderview->ctree)->selection;
2359              cur != NULL; cur = cur->next) {
2360                 item = gtk_ctree_node_get_row_data
2361                         (GTK_CTREE(folderview->ctree), 
2362                          GTK_CTREE_NODE(cur->data));
2363                 if (item) {
2364                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2365                         gtk_selection_data_set(selection_data,
2366                                                selection_data->target, 8,
2367                                                source, strlen(source));
2368                         break;
2369                 } else
2370                         return;
2371         }
2372 }
2373
2374 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2375 {
2376         FolderUpdateData *hookdata;
2377         FolderView *folderview;
2378         GtkWidget *ctree;
2379
2380         hookdata = source;
2381         folderview = (FolderView *) userdata;   
2382         g_return_val_if_fail(hookdata != NULL, FALSE);
2383         g_return_val_if_fail(folderview != NULL, FALSE);
2384
2385         ctree = folderview->ctree;
2386         g_return_val_if_fail(ctree != NULL, FALSE);
2387
2388         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2389                 folderview_create_folder_node(folderview, hookdata->item);
2390         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2391                 GtkCTreeNode *node;
2392
2393                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2394                 if (node != NULL)
2395                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2396         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2397                 folderview_set(folderview);
2398
2399         return FALSE;
2400 }
2401
2402 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2403                                           GdkDragContext *context,
2404                                           gint            x,
2405                                           gint            y,
2406                                           guint           time,
2407                                           FolderView     *folderview)
2408 {
2409         gint row, column;
2410         FolderItem *item = NULL, *src_item = NULL;
2411         GtkCTreeNode *node = NULL;
2412         gboolean acceptable = FALSE;
2413         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2414                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2415         int height = (int)pos->page_size;
2416         int total_height = (int)pos->upper;
2417         int vpos = (int) pos->value;
2418
2419         if (gtk_clist_get_selection_info
2420                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2421                 GtkWidget *srcwidget;
2422
2423                 if (y > height - 24 && height + vpos < total_height)
2424                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2425
2426                 if (y < 48 && y > 0)
2427                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2428
2429                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2430                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2431                 src_item = folderview->summaryview->folder_item;
2432
2433                 srcwidget = gtk_drag_get_source_widget(context);
2434                 if (srcwidget == folderview->summaryview->ctree) {
2435                         /* comes from summaryview */
2436                         /* we are copying messages, so only accept folder items that are not
2437                            the source item, are no root items and can copy messages */
2438                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2439                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2440                                 acceptable = TRUE;
2441                 } else if (srcwidget == folderview->ctree) {
2442                         /* comes from folderview */
2443                         /* we are moving folder items, only accept folders that are not
2444                            the source items and can copy messages and create folder items */
2445                         if (item && item->folder && src_item && src_item != item &&
2446                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2447                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2448                                 acceptable = TRUE;
2449                 }
2450         }
2451
2452         if (acceptable || (src_item && src_item == item))
2453                 drag_state_start(folderview, node, item);
2454         
2455         if (acceptable) {
2456                 g_signal_handlers_block_by_func
2457                         (G_OBJECT(widget),
2458                          G_CALLBACK(folderview_selected), folderview);
2459                 gtk_ctree_select(GTK_CTREE(widget), node);
2460                 g_signal_handlers_unblock_by_func
2461                         (G_OBJECT(widget),
2462                          G_CALLBACK(folderview_selected), folderview);
2463                 gdk_drag_status(context, 
2464                                         (context->actions == GDK_ACTION_COPY ?
2465                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2466         } else {
2467                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2468                 gdk_drag_status(context, 0, time);
2469         }
2470
2471         return acceptable;
2472 }
2473
2474 static void folderview_drag_leave_cb(GtkWidget      *widget,
2475                                      GdkDragContext *context,
2476                                      guint           time,
2477                                      FolderView     *folderview)
2478 {
2479         drag_state_stop(folderview);
2480         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2481 }
2482
2483 static void folderview_drag_received_cb(GtkWidget        *widget,
2484                                         GdkDragContext   *drag_context,
2485                                         gint              x,
2486                                         gint              y,
2487                                         GtkSelectionData *data,
2488                                         guint             info,
2489                                         guint             time,
2490                                         FolderView       *folderview)
2491 {
2492         gint row, column;
2493         FolderItem *item, *src_item;
2494         GtkCTreeNode *node;
2495
2496         drag_state_stop(folderview);
2497         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2498                 /* comes from summaryview */
2499                 if (gtk_clist_get_selection_info
2500                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2501                         return;
2502
2503                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2504                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2505                 src_item = folderview->summaryview->folder_item;
2506                 
2507                 /* re-check (due to acceptable possibly set for folder moves */
2508                 if (!(item && item->folder && item->path && !item->no_select && 
2509                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2510                         return;
2511                 }
2512                 if (item && src_item) {
2513                         switch (drag_context->action) {
2514                         case GDK_ACTION_COPY:
2515                                 summary_copy_selected_to(folderview->summaryview, item);
2516                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2517                                 break;
2518                         case GDK_ACTION_MOVE:
2519                         case GDK_ACTION_DEFAULT:
2520                         default:
2521                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2522                                         summary_copy_selected_to(folderview->summaryview, item);
2523                                 else
2524                                         summary_move_selected_to(folderview->summaryview, item);
2525                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2526                         }
2527                 } else
2528                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2529         } else {
2530                 /* comes from folderview */
2531                 char *source;
2532                 gboolean folder_is_normal = TRUE;
2533                 
2534                 source = data->data + 17;
2535                 if (gtk_clist_get_selection_info
2536                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2537                     || *source == 0) {
2538                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2539                         return;
2540                 }
2541                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2542                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2543                 src_item = folder_find_item_from_identifier(source);
2544
2545                 folder_is_normal = 
2546                         src_item != NULL &&
2547                         src_item->stype == F_NORMAL &&
2548                         !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2549                         !folder_has_parent_of_type(src_item, F_DRAFT) &&
2550                         !folder_has_parent_of_type(src_item, F_QUEUE) &&
2551                         !folder_has_parent_of_type(src_item, F_TRASH);
2552                 if (!item || item->no_select || !src_item 
2553                 ||  !folder_is_normal) {
2554                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2555                         return;
2556                 }
2557
2558                 folderview_move_folder(folderview, src_item, item);
2559                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2560         }
2561         folderview->nodes_to_recollapse = NULL;
2562 }
2563
2564 static void folderview_drag_end_cb(GtkWidget        *widget, 
2565                                    GdkDragContext   *drag_context,
2566                                    FolderView       *folderview)
2567 {
2568         drag_state_stop(folderview);
2569         g_slist_free(folderview->nodes_to_recollapse);
2570         folderview->nodes_to_recollapse = NULL;
2571 }
2572
2573 void folderview_register_popup(FolderViewPopup *fpopup)
2574 {
2575         GList *folderviews;
2576
2577         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2578                 FolderView *folderview = folderviews->data;
2579                 GtkItemFactory *factory;
2580
2581                 factory = create_ifactory(folderview, fpopup);
2582                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2583         }       
2584         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2585 }
2586
2587 void folderview_unregister_popup(FolderViewPopup *fpopup)
2588 {
2589         GList *folderviews;
2590
2591         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2592                 FolderView *folderview = folderviews->data;
2593
2594                 g_hash_table_remove(folderview->popups, fpopup->klass);
2595         }       
2596         g_hash_table_remove(folderview_popups, fpopup->klass);
2597 }