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