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