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