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