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