8b181701d94821d6097b21be70b790ad6e6a9ecb
[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         /* TODO: wwp: avoid displaying (null) in the status bar */
2197         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
2198                                         item->path : "(null)");
2199         debug_print("%s\n", buf);
2200         STATUSBAR_PUSH(folderview->mainwin, buf);
2201         g_free(buf);
2202
2203         main_window_cursor_wait(folderview->mainwin);
2204
2205         res = folder_item_open(item);
2206         if (res == -1) {
2207                 main_window_cursor_normal(folderview->mainwin);
2208                 STATUSBAR_POP(folderview->mainwin);
2209
2210                 alertpanel_error(_("Folder could not be opened."));
2211
2212                 folderview->open_folder = FALSE;
2213                 can_select = TRUE;
2214                 END_TIMING();
2215                 return;
2216         } else if (res == -2) {
2217                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2218                 data->ctree = ctree;
2219                 data->row = row;
2220                 data->column = column;
2221                 data->folderview = folderview;
2222                 debug_print("postponing open of %s till end of scan\n",
2223                         item->path ? item->path:item->name);
2224                 folderview->open_folder = FALSE;
2225                 can_select = TRUE;
2226                 g_timeout_add(500, postpone_select, data);
2227                 END_TIMING();
2228                 return;
2229         }
2230         
2231         main_window_cursor_normal(folderview->mainwin);
2232
2233         /* Show messages */
2234         summary_set_prefs_from_folderitem(folderview->summaryview, item);
2235         opened = summary_show(folderview->summaryview, item);
2236         
2237         folder_clean_cache_memory(item);
2238
2239         if (!opened) {
2240                 gtkut_ctree_set_focus_row(ctree, old_opened);
2241                 gtk_ctree_select(ctree, old_opened);
2242                 folderview->opened = old_opened;
2243         } else {
2244                 folderview->opened = row;
2245                 if (gtk_ctree_node_is_visible(ctree, row)
2246                     != GTK_VISIBILITY_FULL)
2247                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
2248         }
2249
2250         STATUSBAR_POP(folderview->mainwin);
2251
2252         folderview->open_folder = FALSE;
2253         can_select = TRUE;
2254         END_TIMING();
2255 }
2256
2257 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
2258                                      FolderView *folderview)
2259 {
2260         FolderItem *item;
2261
2262         item = gtk_ctree_node_get_row_data(ctree, node);
2263         g_return_if_fail(item != NULL);
2264         item->collapsed = FALSE;
2265         folderview_update_node(folderview, node);
2266 }
2267
2268 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
2269                                       FolderView *folderview)
2270 {
2271         FolderItem *item;
2272
2273         item = gtk_ctree_node_get_row_data(ctree, node);
2274         g_return_if_fail(item != NULL);
2275         item->collapsed = TRUE;
2276         folderview_update_node(folderview, node);
2277 }
2278
2279 static void folderview_popup_close(GtkMenuShell *menu_shell,
2280                                    FolderView *folderview)
2281 {
2282         if (!folderview->opened) return;
2283
2284         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
2285 }
2286
2287 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
2288                                    FolderView *folderview)
2289 {
2290         FolderColumnType type = folderview->col_state[column].type;
2291
2292         prefs_common.folder_col_size[type] = width;
2293 }
2294
2295 static void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
2296 {
2297         GNode *srcnode;
2298
2299         folderview_create_folder_node(folderview, item);
2300
2301         if (!item || !item->folder || !item->folder->node)
2302                 return;
2303
2304         srcnode = item->folder->node;   
2305         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2306         srcnode = srcnode->children;
2307         while (srcnode != NULL) {
2308                 if (srcnode && srcnode->data) {
2309                         FolderItem *next_item = (FolderItem*) srcnode->data;
2310                         folderview_create_folder_node_recursive(folderview, next_item);
2311                 }
2312                 srcnode = srcnode->next;
2313         }
2314 }
2315
2316 static void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2317 {
2318         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2319         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2320         GtkCTreeNode *node, *parent_node;
2321         gint *col_pos = folderview->col_pos;
2322         FolderItemUpdateData hookdata;
2323
2324         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2325         if (parent_node == NULL)
2326                 return;
2327
2328         gtk_clist_freeze(GTK_CLIST(ctree));
2329
2330         text[col_pos[F_COL_FOLDER]] = item->name;
2331         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2332                                      FOLDER_SPACING,
2333                                      folderxpm, folderxpmmask,
2334                                      folderopenxpm, folderopenxpmmask,
2335                                      FALSE, FALSE);
2336         gtk_ctree_expand(ctree, parent_node);
2337         gtk_ctree_node_set_row_data(ctree, node, item);
2338         if (normal_style)
2339                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
2340         folderview_sort_folders(folderview, parent_node, item->folder);
2341
2342         hookdata.item = item;
2343         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2344         hookdata.msg = NULL;
2345         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2346
2347         gtk_clist_thaw(GTK_CLIST(ctree));
2348 }
2349
2350 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
2351                                       GtkWidget *widget)
2352 {
2353         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2354         FolderItem *item;
2355         GSList *mlist = NULL;
2356         GSList *cur = NULL;
2357         FolderItem *special_trash = NULL;
2358         PrefsAccount *ac;
2359
2360         if (!folderview->selected) return;
2361         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2362         g_return_if_fail(item != NULL);
2363         g_return_if_fail(item->folder != NULL);
2364
2365         if (NULL != (ac = account_find_from_item(item)))
2366                 special_trash = account_get_special_folder(ac, F_TRASH);
2367
2368         if (item != item->folder->trash && item != special_trash
2369         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2370         
2371         if (prefs_common.ask_on_clean) {
2372                 if (alertpanel(_("Empty trash"),
2373                                _("Delete all messages in trash?"),
2374                                GTK_STOCK_CANCEL, _("+_Empty trash"), NULL) != G_ALERTALTERNATE)
2375                         return;
2376         }
2377         
2378         mlist = folder_item_get_msg_list(item);
2379         
2380         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2381                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2382                 if (MSG_IS_LOCKED(msginfo->flags))
2383                         continue;
2384                 /* is it partially received? (partial_recv isn't cached) */
2385                 if (msginfo->total_size != 0 && 
2386                     msginfo->size != (off_t)msginfo->total_size)
2387                         partial_mark_for_delete(msginfo);
2388         }
2389         procmsg_msg_list_free(mlist);
2390
2391         folder_item_remove_all_msg(item);
2392 }
2393
2394 static void folderview_send_queue_cb(FolderView *folderview, guint action,
2395                                       GtkWidget *widget)
2396 {
2397         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2398         FolderItem *item;
2399         FolderItem *special_queue = NULL;
2400         PrefsAccount *ac;
2401         gchar *errstr = NULL;
2402
2403         if (!folderview->selected) return;
2404         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2405         g_return_if_fail(item != NULL);
2406         g_return_if_fail(item->folder != NULL);
2407
2408         if (NULL != (ac = account_find_from_item(item)))
2409                 special_queue = account_get_special_folder(ac, F_QUEUE);
2410
2411         if (item != item->folder->queue && item != special_queue
2412         &&  !folder_has_parent_of_type(item, F_QUEUE)) return;
2413         
2414         if (procmsg_queue_is_empty(item))
2415                 return;
2416
2417         if (prefs_common.work_offline)
2418                 if (alertpanel(_("Offline warning"), 
2419                                _("You're working offline. Override?"),
2420                                GTK_STOCK_NO, GTK_STOCK_YES,
2421                                NULL) != G_ALERTALTERNATE)
2422                 return;
2423
2424         /* ask for confirmation before sending queued messages only
2425            in online mode and if there is at least one message queued
2426            in any of the folder queue
2427         */
2428         if (prefs_common.confirm_send_queued_messages) {
2429                 if (!prefs_common.work_offline) {
2430                         if (alertpanel(_("Send queued messages"), 
2431                                    _("Send all queued messages?"),
2432                                    GTK_STOCK_CANCEL, _("_Send"),
2433                                    NULL) != G_ALERTALTERNATE)
2434                                 return;
2435                 }
2436         }
2437
2438         if (procmsg_send_queue(item, prefs_common.savemsg, &errstr) < 0) {
2439                 if (!errstr)
2440                         alertpanel_error_log(_("Some errors occurred while "
2441                                            "sending queued messages."));
2442                 else {
2443                         gchar *tmp = g_strdup_printf(_("Some errors occurred "
2444                                         "while sending queued messages:\n%s"), errstr);
2445                         g_free(errstr);
2446                         alertpanel_error_log(tmp);
2447                         g_free(tmp);
2448                 }
2449         }
2450 }
2451
2452 static void folderview_search_cb(FolderView *folderview, guint action,
2453                                  GtkWidget *widget)
2454 {
2455         summary_search(folderview->summaryview);
2456 }
2457
2458 static void folderview_run_processing_cb(FolderView *folderview, guint action,
2459                                  GtkWidget *widget)
2460 {
2461         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2462         FolderItem *item;
2463
2464         if (!folderview->selected) return;
2465
2466         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2467         g_return_if_fail(item != NULL);
2468         g_return_if_fail(item->folder != NULL);
2469
2470         folder_item_apply_processing(item);
2471 }
2472
2473 static void folderview_property_cb(FolderView *folderview, guint action,
2474                                    GtkWidget *widget)
2475 {
2476         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2477         FolderItem *item;
2478
2479         if (!folderview->selected) return;
2480
2481         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2482         g_return_if_fail(item != NULL);
2483         g_return_if_fail(item->folder != NULL);
2484
2485         prefs_folder_item_open(item);
2486 }
2487
2488 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2489 {
2490         GSList *list = NULL;
2491         GSList *done = NULL;
2492         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2493         
2494         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2495                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2496                 &&  list->data != node) {
2497                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2498                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2499                 }
2500         }
2501         for (list = done; list != NULL; list = g_slist_next(list)) {
2502                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2503                                                                  list->data);
2504         }
2505         g_slist_free(done);
2506 }
2507
2508 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2509                             FolderItem *to_folder, gboolean copy)
2510 {
2511         FolderItem *from_parent = NULL;
2512         FolderItem *new_folder = NULL;
2513         GtkCTreeNode *src_node = NULL;
2514         gchar *buf;
2515         gint status;
2516
2517         g_return_if_fail(folderview != NULL);
2518         g_return_if_fail(from_folder != NULL);
2519         g_return_if_fail(to_folder != NULL);
2520
2521         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2522         from_parent = folder_item_parent(from_folder);
2523         
2524         if (prefs_common.warn_dnd) {
2525                 buf = g_strdup_printf(copy ? _("Do you really want to copy folder '%s' in '%s' ?"):
2526                                              _("Do you really want to make folder '%s' a subfolder of '%s' ?"), 
2527                                         from_folder->name, to_folder->name);
2528                 status = alertpanel_full(copy ? _("Copy folder"):_("Move folder"), buf,
2529                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, TRUE,
2530                                          NULL, ALERT_QUESTION, G_ALERTDEFAULT);
2531                 g_free(buf);
2532
2533                 if ((status & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
2534                         return;
2535                 else if (status & G_ALERTDISABLE)
2536                         prefs_common.warn_dnd = FALSE;
2537         }
2538
2539         buf = g_strdup_printf(copy ? _("Copying %s to %s..."):_("Moving %s to %s..."), 
2540                                 from_folder->name, to_folder->name);
2541         STATUSBAR_PUSH(folderview->mainwin, buf);
2542         g_free(buf);
2543         summary_clear_all(folderview->summaryview);
2544         folderview->opened = NULL;
2545         folderview->selected = NULL;
2546         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2547         inc_lock();
2548         main_window_cursor_wait(folderview->mainwin);
2549
2550         statusbar_verbosity_set(FALSE);
2551         folder_item_update_freeze();
2552         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder, copy)) == F_MOVE_OK) {
2553                 statusbar_verbosity_set(FALSE);
2554                 main_window_cursor_normal(folderview->mainwin);
2555                 STATUSBAR_POP(folderview->mainwin);
2556                 folder_item_update_thaw();
2557                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2558
2559                 folderview_sort_folders(folderview, 
2560                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2561                                 NULL, to_folder), new_folder->folder);
2562                 folderview_select(folderview, new_folder);
2563         } else {
2564                 statusbar_verbosity_set(FALSE);         
2565                 main_window_cursor_normal(folderview->mainwin);
2566                 STATUSBAR_POP(folderview->mainwin);
2567                 folder_item_update_thaw();
2568                 switch (status) {
2569                 case F_MOVE_FAILED_DEST_IS_PARENT:
2570                         alertpanel_error(_("Source and destination are the same."));
2571                         break;
2572                 case F_MOVE_FAILED_DEST_IS_CHILD:
2573                         alertpanel_error(copy ? _("Can't copy a folder to one of its children."):
2574                                                 _("Can't move a folder to one of its children."));
2575                         break;
2576                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2577                         alertpanel_error(_("A folder cannot be moved between different mailboxes."));
2578                         break;
2579                 default:
2580                         alertpanel_error(copy ? _("Copy failed!"):_("Move failed!"));
2581                         break;
2582                 }
2583         }       
2584         inc_unlock();           
2585         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2586 }
2587
2588 static gint folderview_clist_compare(GtkCList *clist,
2589                                      gconstpointer ptr1, gconstpointer ptr2)
2590 {
2591         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2592         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2593
2594         if (item1->order > 0 && item2->order > 0)  // if we have an order item, use it
2595         {
2596                 return item1->order - item2->order;
2597         }
2598
2599         // if only one folder has an order it comes first
2600         if (item1->order > 0)
2601         {
2602                 return -1;
2603         }
2604         if (item2->order > 0)
2605         {
2606                 return 1;
2607         }
2608
2609         if (!item1->name)
2610                 return (item2->name != NULL);
2611         if (!item2->name)
2612                 return -1;
2613
2614         return g_utf8_collate(item1->name, item2->name);
2615 }
2616
2617 static void folderview_processing_cb(FolderView *folderview, guint action,
2618                                      GtkWidget *widget)
2619 {
2620         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2621         FolderItem *item;
2622         gchar *id, *title;
2623
2624         if (!folderview->selected) return;
2625
2626         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2627         g_return_if_fail(item != NULL);
2628         g_return_if_fail(item->folder != NULL);
2629
2630         id = folder_item_get_identifier(item);
2631         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2632         g_free (id);
2633
2634         prefs_filtering_open(&item->prefs->processing, title,
2635                         MANUAL_ANCHOR_PROCESSING, NULL, NULL, FALSE);
2636         g_free (title);
2637 }
2638
2639 void folderview_set_target_folder_color(gint color_op) 
2640 {
2641         gint firstone = 1;
2642         GList *list;
2643         FolderView *folderview;
2644
2645         for (list = folderview_list; list != NULL; list = list->next) {
2646                 folderview = (FolderView *)list->data;
2647                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2648                 if (firstone) {
2649                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2650                                 folderview->color_op;
2651                         firstone = 0;
2652                 }
2653         }
2654 }
2655
2656 static gchar *last_font = NULL;
2657 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2658 {
2659         /* force reinit */
2660         g_free(last_font);
2661         last_font = NULL;
2662         
2663 }
2664
2665 void folderview_reflect_prefs(void)
2666 {
2667         gboolean update_font = TRUE;
2668         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2669         FolderItem *item = folderview_get_selected_item(folderview);
2670         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2671                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2672         gint height = pos->value;
2673
2674         if (last_font && !strcmp(last_font, NORMAL_FONT))
2675                 update_font = FALSE;
2676
2677         g_free(last_font);
2678         last_font = g_strdup(NORMAL_FONT);
2679
2680         if (update_font) {              
2681                 normal_style = normal_color_style = bold_style = 
2682                         bold_color_style = bold_tgtfold_style = NULL;
2683
2684                 folderview_init(folderview);
2685         }
2686         gtk_clist_freeze(GTK_CLIST(folderview->ctree));
2687         folderview_column_set_titles(folderview);
2688         folderview_set_all();
2689
2690         g_signal_handlers_block_by_func
2691                 (G_OBJECT(folderview->ctree),
2692                  G_CALLBACK(folderview_selected), folderview);
2693
2694         if (item) {
2695                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(
2696                         GTK_CTREE(folderview->ctree), NULL, item);
2697
2698                 folderview_select(folderview, item);
2699                 folderview->open_folder = FALSE;
2700                 folderview->selected = node;
2701         }
2702
2703         g_signal_handlers_unblock_by_func
2704                 (G_OBJECT(folderview->ctree),
2705                  G_CALLBACK(folderview_selected), folderview);
2706
2707         pos = gtk_scrolled_window_get_vadjustment(
2708                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2709         gtk_adjustment_set_value(pos, height);
2710         gtk_clist_thaw(GTK_CLIST(folderview->ctree));
2711 }
2712
2713 static void drag_state_stop(FolderView *folderview)
2714 {
2715         if (folderview->drag_timer)
2716                 g_source_remove(folderview->drag_timer);
2717         folderview->drag_timer = 0;
2718         folderview->drag_node = NULL;
2719 }
2720
2721 static gint folderview_defer_expand(FolderView *folderview)
2722 {
2723         if (folderview->drag_node) {
2724                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2725                 if (folderview->drag_item->collapsed) {
2726                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2727                         folderview->nodes_to_recollapse = g_slist_append
2728                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2729                 }
2730         }
2731         folderview->drag_item  = NULL;
2732         folderview->drag_timer = 0;
2733         return FALSE;
2734 }
2735
2736 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2737 {
2738         /* the idea is that we call drag_state_start() whenever we want expansion to
2739          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2740          * we need to call drag_state_stop() */
2741         drag_state_stop(folderview);
2742         /* request expansion */
2743         if (0 != (folderview->drag_timer = g_timeout_add
2744                         (prefs_common.hover_timeout, 
2745                          (GtkFunction)folderview_defer_expand,
2746                          folderview))) {
2747                 folderview->drag_node = node;
2748                 folderview->drag_item = item;
2749         }                        
2750 }
2751
2752 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2753                                   FolderView       *folderview)
2754 {
2755         GdkDragContext *context;
2756
2757         g_return_if_fail(folderview != NULL);
2758         if (folderview->selected == NULL) return;
2759         if (folderview->nodes_to_recollapse) 
2760                 g_slist_free(folderview->nodes_to_recollapse);
2761         folderview->nodes_to_recollapse = NULL;
2762         context = gtk_drag_begin(widget, folderview->target_list,
2763                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2764         gtk_drag_set_icon_default(context);
2765 }
2766
2767 static void folderview_drag_data_get(GtkWidget        *widget,
2768                                      GdkDragContext   *drag_context,
2769                                      GtkSelectionData *selection_data,
2770                                      guint             info,
2771                                      guint             time,
2772                                      FolderView       *folderview)
2773 {
2774         FolderItem *item;
2775         GList *cur;
2776         gchar *source = NULL;
2777         if (info == TARGET_DUMMY) {
2778                 for (cur = GTK_CLIST(folderview->ctree)->selection;
2779                      cur != NULL; cur = cur->next) {
2780                         item = gtk_ctree_node_get_row_data
2781                                 (GTK_CTREE(folderview->ctree), 
2782                                  GTK_CTREE_NODE(cur->data));
2783                         if (item) {
2784                                 source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2785                                 gtk_selection_data_set(selection_data,
2786                                                        selection_data->target, 8,
2787                                                        source, strlen(source));
2788                                 break;
2789                         } else
2790                                 return;
2791                 }
2792         } else {
2793                 g_warning("unknown info %d\n", info);
2794         }
2795 }
2796
2797 static gboolean folderview_update_folder(gpointer source, gpointer userdata)
2798 {
2799         FolderUpdateData *hookdata;
2800         FolderView *folderview;
2801         GtkWidget *ctree;
2802
2803         hookdata = source;
2804         folderview = (FolderView *) userdata;   
2805         g_return_val_if_fail(hookdata != NULL, FALSE);
2806         g_return_val_if_fail(folderview != NULL, FALSE);
2807
2808         ctree = folderview->ctree;
2809         g_return_val_if_fail(ctree != NULL, FALSE);
2810
2811         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2812                 folderview_create_folder_node(folderview, hookdata->item);
2813         else if (hookdata->update_flags & FOLDER_RENAME_FOLDERITEM) {
2814                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree),
2815                                 NULL, folder_item_parent(hookdata->item));
2816                 folderview_sort_folders(folderview, node, hookdata->folder);
2817         } else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2818                 GtkCTreeNode *node;
2819
2820                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2821                 if (node != NULL) {
2822                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2823                         if (folderview->selected == node)
2824                                 folderview->selected = NULL;
2825                         if (folderview->opened == node)
2826                                 folderview->opened = NULL;
2827                 }
2828         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2829                 folderview_set(folderview);
2830
2831         return FALSE;
2832 }
2833
2834 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2835                                           GdkDragContext *context,
2836                                           gint            x,
2837                                           gint            y,
2838                                           guint           time,
2839                                           FolderView     *folderview)
2840 {
2841         gint row, column;
2842         FolderItem *item = NULL, *src_item = NULL;
2843         GtkCTreeNode *node = NULL;
2844         gboolean acceptable = FALSE;
2845         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2846                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2847         int height = (int)pos->page_size;
2848         int total_height = (int)pos->upper;
2849         int vpos = (int) pos->value;
2850
2851         if (gtk_clist_get_selection_info
2852                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2853                 GtkWidget *srcwidget;
2854
2855                 if (y > height - 24 && height + vpos < total_height)
2856                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2857
2858                 if (y < 48 && y > 0)
2859                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2860
2861                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2862                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2863                 src_item = folderview->summaryview->folder_item;
2864
2865                 srcwidget = gtk_drag_get_source_widget(context);
2866                 if (srcwidget == summary_get_main_widget(folderview->summaryview)) {
2867                         /* comes from summaryview */
2868                         /* we are copying messages, so only accept folder items that are not
2869                            the source item, are no root items and can copy messages */
2870                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2871                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2872                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
2873                                 acceptable = TRUE;
2874                 } else if (srcwidget == folderview->ctree) {
2875                         /* comes from folderview */
2876                         /* we are moving folder items, only accept folders that are not
2877                            the source items and can copy messages and create folder items */
2878                         if (item && item->folder && src_item && src_item != item &&
2879                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2880                             FOLDER_CLASS(item->folder)->create_folder != NULL &&
2881                             ((FOLDER_TYPE(item->folder) != F_UNKNOWN &&  FOLDER_TYPE(src_item->folder) != F_UNKNOWN)
2882                              || item->folder == src_item->folder))
2883                                 acceptable = TRUE;
2884                 } else {
2885                         /* comes from another app */
2886                         /* we are adding messages, so only accept folder items that are 
2887                            no root items and can copy messages */
2888                         if (item && item->folder && folder_item_parent(item) != NULL
2889                             && FOLDER_CLASS(item->folder)->add_msg != NULL &&
2890                             FOLDER_TYPE(item->folder) != F_UNKNOWN)
2891                                 acceptable = TRUE;
2892                 }
2893         }
2894
2895         if (acceptable || (src_item && src_item == item))
2896                 drag_state_start(folderview, node, item);
2897         
2898         if (acceptable) {
2899                 g_signal_handlers_block_by_func
2900                         (G_OBJECT(widget),
2901                          G_CALLBACK(folderview_selected), folderview);
2902                 gtk_ctree_select(GTK_CTREE(widget), node);
2903                 g_signal_handlers_unblock_by_func
2904                         (G_OBJECT(widget),
2905                          G_CALLBACK(folderview_selected), folderview);
2906                 gdk_drag_status(context, 
2907                                         (context->actions == GDK_ACTION_COPY ?
2908                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2909         } else {
2910                 if (folderview->opened)
2911                         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2912                 gdk_drag_status(context, 0, time);
2913         }
2914
2915         return acceptable;
2916 }
2917
2918 static void folderview_drag_leave_cb(GtkWidget      *widget,
2919                                      GdkDragContext *context,
2920                                      guint           time,
2921                                      FolderView     *folderview)
2922 {
2923         drag_state_stop(folderview);
2924         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2925 }
2926
2927 static void free_info (gpointer stuff, gpointer data)
2928 {
2929         g_free(stuff);
2930 }
2931
2932 void folderview_finish_dnd(const gchar *data, GdkDragContext *drag_context,
2933                            guint time, FolderItem *item)
2934 {
2935         GList *list, *tmp;
2936         GSList *msglist = NULL;
2937         list = uri_list_extract_filenames(data);
2938         if (!(item && item->folder && folder_item_parent(item) != NULL
2939                     && FOLDER_CLASS(item->folder)->add_msg != NULL))
2940         {
2941                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2942                 debug_print("item doesn't fit\n");                      
2943                 return;
2944         }       
2945         if (!list) {
2946                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2947                 debug_print("list is empty\n");                 
2948                 return;
2949         }
2950         for (tmp = list; tmp != NULL; tmp = tmp->next) {
2951                 MsgFileInfo *info = NULL;
2952
2953                 if (file_is_email((gchar *)tmp->data)) {
2954                         info = g_new0(MsgFileInfo, 1);
2955                         info->msginfo = NULL;
2956                         info->file = (gchar *)tmp->data;
2957                         msglist = g_slist_prepend(msglist, info);
2958                         debug_print("file is a mail\n");
2959                 } else {
2960                         debug_print("file isn't a mail\n");
2961                 }
2962         }
2963         if (msglist) {
2964                 msglist = g_slist_reverse(msglist);
2965                 folder_item_add_msgs(item, msglist, FALSE);
2966                 g_slist_foreach(msglist, free_info, NULL);
2967                 g_slist_free(msglist);
2968                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2969         } else {
2970                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2971         }
2972         list_free_strings(list);
2973         g_list_free(list);
2974 }
2975
2976 static void folderview_drag_received_cb(GtkWidget        *widget,
2977                                         GdkDragContext   *drag_context,
2978                                         gint              x,
2979                                         gint              y,
2980                                         GtkSelectionData *data,
2981                                         guint             info,
2982                                         guint             time,
2983                                         FolderView       *folderview)
2984 {
2985         gint row, column;
2986         FolderItem *item = NULL, *src_item;
2987         GtkCTreeNode *node;
2988
2989         if (info == TARGET_DUMMY) {
2990                 drag_state_stop(folderview);
2991                 if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2992                         /* comes from summaryview */
2993                         if (gtk_clist_get_selection_info
2994                                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2995                                 return;
2996
2997                         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2998                         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2999                         src_item = folderview->summaryview->folder_item;
3000
3001                         /* re-check (due to acceptable possibly set for folder moves */
3002                         if (!(item && item->folder && item->path && !item->no_select && 
3003                               src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
3004                                 return;
3005                         }
3006                         if (item && src_item) {
3007                                 switch (drag_context->action) {
3008                                 case GDK_ACTION_COPY:
3009                                         summary_copy_selected_to(folderview->summaryview, item);
3010                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
3011                                         break;
3012                                 case GDK_ACTION_MOVE:
3013                                 case GDK_ACTION_DEFAULT:
3014                                 default:
3015                                         if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
3016                                                 summary_copy_selected_to(folderview->summaryview, item);
3017                                         else
3018                                                 summary_move_selected_to(folderview->summaryview, item);
3019                                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3020                                 }
3021                         } else
3022                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
3023                 } else {
3024                         /* comes from folderview */
3025                         char *source;
3026                         gboolean folder_is_normal = TRUE;
3027                         gboolean copy = (drag_context->action == GDK_ACTION_COPY);
3028
3029                         source = data->data + 17;
3030                         if (gtk_clist_get_selection_info
3031                             (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
3032                             || *source == 0) {
3033                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3034                                 return;
3035                         }
3036                         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
3037                         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
3038                         src_item = folder_find_item_from_identifier(source);
3039
3040                         folder_is_normal = 
3041                                 src_item != NULL &&
3042                                 src_item->stype == F_NORMAL &&
3043                                 !folder_has_parent_of_type(src_item, F_OUTBOX) &&
3044                                 !folder_has_parent_of_type(src_item, F_DRAFT) &&
3045                                 !folder_has_parent_of_type(src_item, F_QUEUE) &&
3046                                 !folder_has_parent_of_type(src_item, F_TRASH);
3047                         if (!item || item->no_select || !src_item 
3048                         ||  !folder_is_normal) {
3049                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3050                                 return;
3051                         }
3052
3053                         folderview_move_folder(folderview, src_item, item, copy);
3054                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
3055                 }
3056                 folderview->nodes_to_recollapse = NULL;
3057         } else if (info == TARGET_MAIL_URI_LIST) {
3058                 if (gtk_clist_get_selection_info
3059                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
3060                         return;
3061
3062                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
3063                 if (!node) {
3064                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
3065                         debug_print("no node\n");               
3066                         return;
3067                 }
3068                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
3069                 if (!item) {
3070                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
3071                         debug_print("no item\n");
3072                         return;
3073                 }
3074                 folderview_finish_dnd(data->data, drag_context, time, item);
3075         }
3076 }
3077
3078 static void folderview_drag_end_cb(GtkWidget        *widget, 
3079                                    GdkDragContext   *drag_context,
3080                                    FolderView       *folderview)
3081 {
3082         drag_state_stop(folderview);
3083         g_slist_free(folderview->nodes_to_recollapse);
3084         folderview->nodes_to_recollapse = NULL;
3085 }
3086
3087 void folderview_register_popup(FolderViewPopup *fpopup)
3088 {
3089         GList *folderviews;
3090
3091         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
3092                 FolderView *folderview = folderviews->data;
3093                 GtkItemFactory *factory;
3094
3095                 factory = create_ifactory(folderview, fpopup);
3096                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
3097         }       
3098         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
3099 }
3100
3101 void folderview_unregister_popup(FolderViewPopup *fpopup)
3102 {
3103         GList *folderviews;
3104
3105         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
3106                 FolderView *folderview = folderviews->data;
3107
3108                 g_hash_table_remove(folderview->popups, fpopup->klass);
3109         }       
3110         g_hash_table_remove(folderview_popups, fpopup->klass);
3111 }