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