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