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