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