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