2005-09-14 [paul] 1.9.14cvs29
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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
68 #define COL_FOLDER_WIDTH        150
69 #define COL_NUM_WIDTH           32
70
71 static GList *folderview_list = NULL;
72
73 static GtkStyle *normal_style;
74 static GtkStyle *normal_color_style;
75 static GtkStyle *bold_style;
76 static GtkStyle *bold_color_style;
77 static GtkStyle *bold_tgtfold_style;
78
79 static GdkPixmap *inboxxpm;
80 static GdkBitmap *inboxxpmmask;
81 static GdkPixmap *inboxhrmxpm;
82 static GdkBitmap *inboxhrmxpmmask;
83 static GdkPixmap *inboxopenxpm;
84 static GdkBitmap *inboxopenxpmmask;
85 static GdkPixmap *inboxopenhrmxpm;
86 static GdkBitmap *inboxopenhrmxpmmask;
87 static GdkPixmap *outboxxpm;
88 static GdkBitmap *outboxxpmmask;
89 static GdkPixmap *outboxhrmxpm;
90 static GdkBitmap *outboxhrmxpmmask;
91 static GdkPixmap *outboxopenxpm;
92 static GdkBitmap *outboxopenxpmmask;
93 static GdkPixmap *outboxopenhrmxpm;
94 static GdkBitmap *outboxopenhrmxpmmask;
95 static GdkPixmap *folderxpm;
96 static GdkBitmap *folderxpmmask;
97 static GdkPixmap *folderhrmxpm;
98 static GdkBitmap *folderhrmxpmmask;
99 static GdkPixmap *folderopenxpm;
100 static GdkBitmap *folderopenxpmmask;
101 static GdkPixmap *folderopenhrmxpm;
102 static GdkBitmap *folderopenhrmxpmmask;
103 static GdkPixmap *trashopenxpm;
104 static GdkBitmap *trashopenxpmmask;
105 static GdkPixmap *trashopenhrmxpm;
106 static GdkBitmap *trashopenhrmxpmmask;
107 static GdkPixmap *trashxpm;
108 static GdkBitmap *trashxpmmask;
109 static GdkPixmap *trashhrmxpm;
110 static GdkBitmap *trashhrmxpmmask;
111 static GdkPixmap *queuexpm;
112 static GdkBitmap *queuexpmmask;
113 static GdkPixmap *queuehrmxpm;
114 static GdkBitmap *queuehrmxpmmask;
115 static GdkPixmap *queueopenxpm;
116 static GdkBitmap *queueopenxpmmask;
117 static GdkPixmap *queueopenhrmxpm;
118 static GdkBitmap *queueopenhrmxpmmask;
119 static GdkPixmap *draftsxpm;
120 static GdkBitmap *draftsxpmmask;
121 static GdkPixmap *draftsopenxpm;
122 static GdkBitmap *draftsopenxpmmask;
123 static GdkPixmap *noselectxpm;
124 static GdkBitmap *noselectxpmmask;
125
126 static GdkPixmap *m_inboxxpm;
127 static GdkBitmap *m_inboxxpmmask;
128 static GdkPixmap *m_inboxhrmxpm;
129 static GdkBitmap *m_inboxhrmxpmmask;
130 static GdkPixmap *m_inboxopenxpm;
131 static GdkBitmap *m_inboxopenxpmmask;
132 static GdkPixmap *m_inboxopenhrmxpm;
133 static GdkBitmap *m_inboxopenhrmxpmmask;
134 static GdkPixmap *m_outboxxpm;
135 static GdkBitmap *m_outboxxpmmask;
136 static GdkPixmap *m_outboxhrmxpm;
137 static GdkBitmap *m_outboxhrmxpmmask;
138 static GdkPixmap *m_outboxopenxpm;
139 static GdkBitmap *m_outboxopenxpmmask;
140 static GdkPixmap *m_outboxopenhrmxpm;
141 static GdkBitmap *m_outboxopenhrmxpmmask;
142 static GdkPixmap *m_folderxpm;
143 static GdkBitmap *m_folderxpmmask;
144 static GdkPixmap *m_folderhrmxpm;
145 static GdkBitmap *m_folderhrmxpmmask;
146 static GdkPixmap *m_folderopenxpm;
147 static GdkBitmap *m_folderopenxpmmask;
148 static GdkPixmap *m_folderopenhrmxpm;
149 static GdkBitmap *m_folderopenhrmxpmmask;
150 static GdkPixmap *m_trashopenxpm;
151 static GdkBitmap *m_trashopenxpmmask;
152 static GdkPixmap *m_trashopenhrmxpm;
153 static GdkBitmap *m_trashopenhrmxpmmask;
154 static GdkPixmap *m_trashxpm;
155 static GdkBitmap *m_trashxpmmask;
156 static GdkPixmap *m_trashhrmxpm;
157 static GdkBitmap *m_trashhrmxpmmask;
158 static GdkPixmap *m_queuexpm;
159 static GdkBitmap *m_queuexpmmask;
160 static GdkPixmap *m_queuehrmxpm;
161 static GdkBitmap *m_queuehrmxpmmask;
162 static GdkPixmap *m_queueopenxpm;
163 static GdkBitmap *m_queueopenxpmmask;
164 static GdkPixmap *m_queueopenhrmxpm;
165 static GdkBitmap *m_queueopenhrmxpmmask;
166 static GdkPixmap *m_draftsxpm;
167 static GdkBitmap *m_draftsxpmmask;
168 static GdkPixmap *m_draftsopenxpm;
169 static GdkBitmap *m_draftsopenxpmmask;
170
171 static GdkPixmap *newxpm;
172 static GdkBitmap *newxpmmask;
173 static GdkPixmap *unreadxpm;
174 static GdkBitmap *unreadxpmmask;
175 static GdkPixmap *readxpm;
176 static GdkBitmap *readxpmmask;
177
178
179 static void folderview_select_node       (FolderView    *folderview,
180                                           GtkCTreeNode  *node);
181 static void folderview_set_folders       (FolderView    *folderview);
182 static void folderview_sort_folders      (FolderView    *folderview,
183                                           GtkCTreeNode  *root,
184                                           Folder        *folder);
185 static void folderview_append_folder     (FolderView    *folderview,
186                                           Folder        *folder);
187 static void folderview_update_node       (FolderView    *folderview,
188                                           GtkCTreeNode  *node);
189
190 static gint folderview_clist_compare    (GtkCList       *clist,
191                                          gconstpointer   ptr1,
192                                          gconstpointer   ptr2);
193
194 /* callback functions */
195 static gboolean folderview_button_pressed       (GtkWidget      *ctree,
196                                                  GdkEventButton *event,
197                                                  FolderView     *folderview);
198 static gboolean folderview_button_released      (GtkWidget      *ctree,
199                                                  GdkEventButton *event,
200                                                  FolderView     *folderview);
201 static gboolean folderview_key_pressed  (GtkWidget      *widget,
202                                          GdkEventKey    *event,
203                                          FolderView     *folderview);
204 static void folderview_selected         (GtkCTree       *ctree,
205                                          GtkCTreeNode   *row,
206                                          gint            column,
207                                          FolderView     *folderview);
208 static void folderview_tree_expanded    (GtkCTree       *ctree,
209                                          GtkCTreeNode   *node,
210                                          FolderView     *folderview);
211 static void folderview_tree_collapsed   (GtkCTree       *ctree,
212                                          GtkCTreeNode   *node,
213                                          FolderView     *folderview);
214 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
215                                          FolderView     *folderview);
216 static void folderview_col_resized      (GtkCList       *clist,
217                                          gint            column,
218                                          gint            width,
219                                          FolderView     *folderview);
220
221 static void mark_all_read_cb            (FolderView    *folderview,
222                                          guint           action,
223                                          GtkWidget      *widget);
224
225 static void folderview_empty_trash_cb   (FolderView     *folderview,
226                                          guint           action,
227                                          GtkWidget      *widget);
228
229 static void folderview_search_cb        (FolderView     *folderview,
230                                          guint           action,
231                                          GtkWidget      *widget);
232
233 static void folderview_property_cb      (FolderView     *folderview,
234                                          guint           action,
235                                          GtkWidget      *widget);
236
237 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
238                                           GdkDragContext *context,
239                                           gint            x,
240                                           gint            y,
241                                           guint           time,
242                                           FolderView     *folderview);
243 static void folderview_drag_leave_cb     (GtkWidget        *widget,
244                                           GdkDragContext   *context,
245                                           guint             time,
246                                           FolderView       *folderview);
247 static void folderview_drag_received_cb  (GtkWidget        *widget,
248                                           GdkDragContext   *drag_context,
249                                           gint              x,
250                                           gint              y,
251                                           GtkSelectionData *data,
252                                           guint             info,
253                                           guint             time,
254                                           FolderView       *folderview);
255 static void folderview_start_drag        (GtkWidget *widget, gint button, GdkEvent *event,
256                                           FolderView       *folderview);
257 static void folderview_drag_data_get     (GtkWidget        *widget,
258                                           GdkDragContext   *drag_context,
259                                           GtkSelectionData *selection_data,
260                                           guint             info,
261                                           guint             time,
262                                           FolderView       *folderview);
263 static void folderview_drag_end_cb       (GtkWidget        *widget,
264                                           GdkDragContext   *drag_context,
265                                           FolderView       *folderview);
266
267 void folderview_create_folder_node       (FolderView       *folderview, 
268                                           FolderItem       *item);
269 gboolean folderview_update_folder        (gpointer          source,
270                                           gpointer          userdata);
271 gboolean folderview_update_item_claws    (gpointer          source,
272                                           gpointer          data);
273 static void folderview_processing_cb(FolderView *folderview, guint action,
274                                      GtkWidget *widget);
275
276 GHashTable *folderview_popups;
277
278 static GtkItemFactoryEntry folderview_common_popup_entries[] =
279 {
280         {N_("/Mark all re_ad"),         NULL, mark_all_read_cb, 0, NULL},
281         {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
282         {N_("/_Properties..."),         NULL, folderview_property_cb, 0, NULL},
283         {N_("/Process_ing..."),         NULL, folderview_processing_cb, 0, NULL},
284 };
285
286 static GtkItemFactoryEntry folder_view_trash_popup_entries[] = {
287         {N_("/------"),                 NULL, NULL, 0, "<Separator>"},
288         {N_("/Empty _trash..."),        NULL, folderview_empty_trash_cb, 0, NULL},
289 };
290
291
292 GtkTargetEntry folderview_drag_types[] =
293 {
294         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
295 };
296
297 void folderview_initialize(void)
298 {
299         FolderViewPopup *fpopup;
300         guint i, n_entries;
301         GSList *entries = NULL;
302
303         fpopup = g_new0(FolderViewPopup, 1);
304
305         n_entries = sizeof(folderview_common_popup_entries) /
306                 sizeof(folderview_common_popup_entries[0]);
307         for (i = 0; i < n_entries; i++)
308                 entries = g_slist_append(entries, &folderview_common_popup_entries[i]);
309
310         fpopup->klass = "common";
311         fpopup->path = "<CommonFolder>";
312         fpopup->entries = entries;
313         fpopup->set_sensitivity = NULL;
314
315         folderview_popups = g_hash_table_new(g_str_hash, g_str_equal);
316         g_hash_table_insert(folderview_popups, "common", fpopup);
317 }
318
319 static GtkItemFactory *create_ifactory(FolderView *folderview, FolderViewPopup *fpopup)
320 {
321         GSList *entries;
322         GtkItemFactory *factory;
323         FolderViewPopup *fpopup_common;
324         GtkWidget *popup;
325
326         factory = gtk_item_factory_new(GTK_TYPE_MENU, fpopup->path, NULL);
327         gtk_item_factory_set_translate_func(factory, menu_translate,
328                                             NULL, NULL);
329
330         for (entries = fpopup->entries; entries != NULL; entries = g_slist_next(entries))
331                 gtk_item_factory_create_item(factory, entries->data, folderview, 1);
332
333         fpopup_common = g_hash_table_lookup(folderview_popups, "common");
334         if (fpopup_common != fpopup)
335                 for (entries = fpopup_common->entries; entries != NULL; entries = g_slist_next(entries))
336                         gtk_item_factory_create_item(factory, entries->data, folderview, 1);
337
338         popup = gtk_item_factory_get_widget(factory, fpopup->path);
339         g_signal_connect(G_OBJECT(popup), "selection_done",
340                          G_CALLBACK(folderview_popup_close),
341                          folderview);
342
343         return factory;
344 }
345
346 static void create_ifactories(gpointer key, gpointer value, gpointer data)
347 {
348         FolderView *folderview = data;
349         FolderViewPopup *fpopup = value;
350         GtkItemFactory *factory;
351
352         factory = create_ifactory(folderview, fpopup);
353         g_hash_table_insert(folderview->popups, fpopup->klass, factory);
354 }
355
356 static void folderview_column_set_titles(FolderView *folderview)
357 {
358         GtkWidget *ctree = folderview->ctree;
359         GtkWidget *label_new;
360         GtkWidget *label_unread;
361         GtkWidget *label_total;
362         GtkWidget *hbox_new;
363         GtkWidget *hbox_unread;
364         GtkWidget *hbox_total;
365         gint *col_pos = folderview->col_pos;
366         
367         debug_print("setting titles...\n");
368         gtk_widget_realize(folderview->ctree);
369         gtk_widget_show_all(folderview->scrolledwin);
370         
371         /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
372          * instead text (text overflows making them unreadable and ugly) */
373         stock_pixmap_gdk(ctree, STOCK_PIXMAP_NEW,
374                          &newxpm, &newxpmmask);
375         stock_pixmap_gdk(ctree, STOCK_PIXMAP_UNREAD,
376                          &unreadxpm, &unreadxpmmask);
377         stock_pixmap_gdk(ctree, STOCK_PIXMAP_READ,
378                          &readxpm, &readxpmmask);
379                 
380         label_new = gtk_pixmap_new(newxpm, newxpmmask);
381         label_unread = gtk_pixmap_new(unreadxpm, unreadxpmmask);
382         label_total = gtk_pixmap_new(readxpm, readxpmmask);
383
384         hbox_new = gtk_hbox_new(FALSE, 4);
385         hbox_unread = gtk_hbox_new(FALSE, 4);
386         hbox_total = gtk_hbox_new(FALSE, 4);
387
388         /* left justified */
389         gtk_box_pack_start(GTK_BOX(hbox_new), label_new, TRUE, TRUE, 0);
390         gtk_misc_set_alignment (GTK_MISC (label_new), 1, 0.5);
391         gtk_box_pack_start(GTK_BOX(hbox_unread), label_unread, TRUE, TRUE, 0);
392         gtk_misc_set_alignment (GTK_MISC (label_unread), 1, 0.5);
393         gtk_box_pack_start(GTK_BOX(hbox_total), label_total, TRUE, TRUE, 0);
394         gtk_misc_set_alignment (GTK_MISC (label_total), 1, 0.5);
395
396         gtk_widget_show_all(hbox_new);
397         gtk_widget_show_all(hbox_unread);
398         gtk_widget_show_all(hbox_total);
399
400         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_NEW],hbox_new);
401         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_UNREAD],hbox_unread);
402         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_TOTAL],hbox_total);
403 }
404
405 GtkWidget *folderview_ctree_create(FolderView *folderview)
406 {
407         GtkWidget *ctree;
408         gint *col_pos;
409         FolderColumnState *col_state;
410         FolderColumnType type;
411         gchar *titles[N_FOLDER_COLS];
412         gint i;
413         GtkWidget *scrolledwin = folderview->scrolledwin;
414
415         debug_print("creating tree...\n");
416         memset(titles, 0, sizeof(titles));
417
418         col_state = prefs_folder_column_get_config();
419         memset(titles, 0, sizeof(titles));
420
421         col_pos = folderview->col_pos;
422
423         for (i = 0; i < N_FOLDER_COLS; i++) {
424                 folderview->col_state[i] = col_state[i];
425                 type = col_state[i].type;
426                 col_pos[type] = i;
427         }
428
429         titles[col_pos[F_COL_FOLDER]] = _("Folder");
430         titles[col_pos[F_COL_NEW]]    = _("New");
431         titles[col_pos[F_COL_UNREAD]] = _("Unread");
432         titles[col_pos[F_COL_TOTAL]]  = _("#");
433
434         ctree = gtk_sctree_new_with_titles(N_FOLDER_COLS, col_pos[F_COL_FOLDER],
435                                            titles);
436         
437         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
438         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[F_COL_NEW],
439                                            GTK_JUSTIFY_RIGHT);
440         gtk_clist_set_column_justification(GTK_CLIST(ctree),
441                                            col_pos[F_COL_UNREAD],
442                                            GTK_JUSTIFY_RIGHT);
443         gtk_clist_set_column_justification(GTK_CLIST(ctree), 
444                                            col_pos[F_COL_TOTAL],
445                                            GTK_JUSTIFY_RIGHT);
446         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
447         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
448                                      GTK_CTREE_EXPANDER_SQUARE);
449         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
450         gtk_clist_set_compare_func(GTK_CLIST(ctree), folderview_clist_compare);
451
452         /* don't let title buttons take key focus */
453         for (i = 0; i < N_FOLDER_COLS; i++) {
454                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
455                                        GTK_CAN_FOCUS);
456                 gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[i],
457                                    prefs_common.folder_col_size[i]);
458                 gtk_clist_set_column_visibility
459                         (GTK_CLIST(ctree), i, col_state[i].visible);
460         }
461
462         g_signal_connect(G_OBJECT(ctree), "key_press_event",
463                          G_CALLBACK(folderview_key_pressed),
464                          folderview);
465         g_signal_connect(G_OBJECT(ctree), "button_press_event",
466                          G_CALLBACK(folderview_button_pressed),
467                          folderview);
468         g_signal_connect(G_OBJECT(ctree), "button_release_event",
469                          G_CALLBACK(folderview_button_released),
470                          folderview);
471         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
472                          G_CALLBACK(folderview_selected), folderview);
473         g_signal_connect(G_OBJECT(ctree), "start_drag",
474                          G_CALLBACK(folderview_start_drag), folderview);
475         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
476                          G_CALLBACK(folderview_drag_data_get),
477                          folderview);
478
479         g_signal_connect_after(G_OBJECT(ctree), "tree_expand",
480                                G_CALLBACK(folderview_tree_expanded),
481                                folderview);
482         g_signal_connect_after(G_OBJECT(ctree), "tree_collapse",
483                                G_CALLBACK(folderview_tree_collapsed),
484                                folderview);
485
486         g_signal_connect(G_OBJECT(ctree), "resize_column",
487                          G_CALLBACK(folderview_col_resized),
488                          folderview);
489
490         /* drop callback */
491         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
492                           summary_drag_types, 1,
493                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
494         g_signal_connect(G_OBJECT(ctree), "drag_motion",
495                          G_CALLBACK(folderview_drag_motion_cb),
496                          folderview);
497         g_signal_connect(G_OBJECT(ctree), "drag_leave",
498                          G_CALLBACK(folderview_drag_leave_cb),
499                          folderview);
500         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
501                          G_CALLBACK(folderview_drag_received_cb),
502                          folderview);
503         g_signal_connect(G_OBJECT(ctree), "drag_end",
504                          G_CALLBACK(folderview_drag_end_cb),
505                          folderview);
506
507         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
508
509         return ctree;
510 }
511
512 void folderview_set_column_order(FolderView *folderview)
513 {
514         GtkWidget *ctree;
515         FolderItem *item = folderview_get_selected_item(folderview);
516         GtkWidget *scrolledwin = folderview->scrolledwin;
517
518         debug_print("recreating tree...\n");
519         gtk_widget_destroy(folderview->ctree);
520
521         folderview->ctree = ctree = folderview_ctree_create(folderview);
522         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
523                                             GTK_CLIST(ctree)->hadjustment);
524         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
525                                             GTK_CLIST(ctree)->vadjustment);
526         gtk_widget_show(ctree);
527         
528         folderview_set(folderview);
529         folderview_column_set_titles(folderview);
530
531         folderview_select(folderview,item);
532 }
533
534 FolderView *folderview_create(void)
535 {
536         FolderView *folderview;
537         GtkWidget *scrolledwin;
538         GtkWidget *ctree;
539
540         debug_print("Creating folder view...\n");
541         folderview = g_new0(FolderView, 1);
542
543         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
544         gtk_scrolled_window_set_policy
545                 (GTK_SCROLLED_WINDOW(scrolledwin),
546                  GTK_POLICY_AUTOMATIC,
547                  prefs_common.folderview_vscrollbar_policy);
548         gtk_widget_set_size_request(scrolledwin,
549                              prefs_common.folderview_width,
550                              prefs_common.folderview_height);
551
552         folderview->scrolledwin  = scrolledwin;
553         ctree = folderview_ctree_create(folderview);
554         
555         /* create popup factories */
556         folderview->popups = g_hash_table_new(g_str_hash, g_str_equal);
557         g_hash_table_foreach(folderview_popups, create_ifactories, folderview);
558
559         folderview->ctree        = ctree;
560
561         folderview->folder_update_callback_id =
562                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST, folderview_update_folder, (gpointer) folderview);
563         folderview->folder_item_update_callback_id =
564                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST, folderview_update_item_claws, (gpointer) folderview);
565
566         gtk_widget_show_all(scrolledwin);
567         
568         folderview->target_list = gtk_target_list_new(folderview_drag_types, 1);
569         folderview_list = g_list_append(folderview_list, folderview);
570
571         return folderview;
572 }
573
574 void folderview_init(FolderView *folderview)
575 {
576         GtkWidget *ctree = folderview->ctree;
577
578         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
579         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
580         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
581         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
582         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
583         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
584         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
585         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
586         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
587         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
588         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
589         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
590         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
591         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
592         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
593         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
594         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
595         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
596         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
597         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
598         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE, &draftsxpm, &draftsxpmmask);
599         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN, &draftsopenxpm, &draftsopenxpmmask);
600         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_NOSELECT, &noselectxpm, &noselectxpmmask);
601
602         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_MARK, &m_inboxxpm, &m_inboxxpmmask);
603         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM_MARK, &m_inboxhrmxpm, &m_inboxhrmxpmmask);
604         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_MARK, &m_inboxopenxpm, &m_inboxopenxpmmask);
605         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM_MARK, &m_inboxopenhrmxpm, &m_inboxopenhrmxpmmask);
606         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_MARK, &m_outboxxpm, &m_outboxxpmmask);
607         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM_MARK, &m_outboxhrmxpm, &m_outboxhrmxpmmask);
608         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_MARK, &m_outboxopenxpm, &m_outboxopenxpmmask);
609         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM_MARK, &m_outboxopenhrmxpm, &m_outboxopenhrmxpmmask);
610         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_MARK, &m_folderxpm, &m_folderxpmmask);
611         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM_MARK, &m_folderhrmxpm, &m_folderhrmxpmmask);
612         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_MARK, &m_folderopenxpm, &m_folderopenxpmmask);
613         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM_MARK, &m_folderopenhrmxpm, &m_folderopenhrmxpmmask);
614         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_MARK, &m_trashopenxpm, &m_trashopenxpmmask);
615         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM_MARK, &m_trashopenhrmxpm, &m_trashopenhrmxpmmask);
616         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_MARK, &m_trashxpm, &m_trashxpmmask);
617         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM_MARK, &m_trashhrmxpm, &m_trashhrmxpmmask);
618         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_MARK, &m_queuexpm, &m_queuexpmmask);
619         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM_MARK, &m_queuehrmxpm, &m_queuehrmxpmmask);
620         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_MARK, &m_queueopenxpm, &m_queueopenxpmmask);
621         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM_MARK, &m_queueopenhrmxpm, &m_queueopenhrmxpmmask);
622         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE_MARK, &m_draftsxpm, &m_draftsxpmmask);
623         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN_MARK, &m_draftsopenxpm, &m_draftsopenxpmmask);
624                         
625         if (!normal_style) {
626                 PangoFontDescription *font_desc;
627                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
628                 font_desc = pango_font_description_from_string(NORMAL_FONT);
629                 if (font_desc) {
630                         if (normal_style->font_desc)
631                                 pango_font_description_free
632                                         (normal_style->font_desc);
633                         normal_style->font_desc = font_desc;
634                 }
635                 normal_color_style = gtk_style_copy(normal_style);
636                 normal_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
637
638                 gtk_widget_set_style(ctree, normal_style);
639         }
640
641         if (!bold_style) {
642                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
643                 pango_font_description_set_weight
644                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
645                 bold_color_style = gtk_style_copy(bold_style);
646                 bold_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
647
648                 bold_tgtfold_style = gtk_style_copy(bold_style);
649                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
650         }
651 }
652
653 void folderview_set(FolderView *folderview)
654 {
655         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
656         MainWindow *mainwin = folderview->mainwin;
657
658         debug_print("Setting folder info...\n");
659         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
660
661         main_window_cursor_wait(mainwin);
662
663         folderview->selected = NULL;
664         folderview->opened = NULL;
665
666         gtk_clist_freeze(GTK_CLIST(ctree));
667         gtk_clist_clear(GTK_CLIST(ctree));
668         gtk_clist_thaw(GTK_CLIST(ctree));
669         gtk_clist_freeze(GTK_CLIST(ctree));
670
671         folderview_set_folders(folderview);
672
673         gtk_clist_thaw(GTK_CLIST(ctree));
674         main_window_cursor_normal(mainwin);
675         STATUSBAR_POP(mainwin);
676 }
677
678 void folderview_set_all(void)
679 {
680         GList *list;
681
682         for (list = folderview_list; list != NULL; list = list->next)
683                 folderview_set((FolderView *)list->data);
684 }
685
686 void folderview_select(FolderView *folderview, FolderItem *item)
687 {
688         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
689         GtkCTreeNode *node;
690         GtkCTreeNode *old_selected = folderview->selected;
691
692         if (!item) return;
693
694         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
695         if (node) folderview_select_node(folderview, node);
696
697         if (old_selected != node)
698                 folder_update_op_count();
699 }
700
701 static void mark_all_read_cb(FolderView *folderview, guint action,
702                              GtkWidget *widget)
703 {
704         FolderItem *item;
705
706         item = folderview_get_selected_item(folderview);
707         if (item == NULL)
708                 return;
709
710         summary_lock(folderview->summaryview);
711         folder_item_update_freeze();
712         if (folderview->summaryview->folder_item == item)
713                 gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
714         folderutils_mark_all_read(item);
715         if (folderview->summaryview->folder_item == item)
716                 gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
717         folder_item_update_thaw();
718         summary_unlock(folderview->summaryview);
719 }
720
721 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
722 {
723         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
724
725         g_return_if_fail(node != NULL);
726
727         folderview->open_folder = TRUE;
728         gtkut_ctree_set_focus_row(ctree, node);
729         gtk_ctree_select(ctree, node);
730         if (folderview->summaryview->folder_item &&
731             folderview->summaryview->folder_item->total_msgs > 0)
732                 gtk_widget_grab_focus(folderview->summaryview->ctree);
733         else
734                 gtk_widget_grab_focus(folderview->ctree);
735
736         gtkut_ctree_expand_parent_all(ctree, node);
737 }
738
739 void folderview_unselect(FolderView *folderview)
740 {
741         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
742                 gtk_ctree_collapse
743                         (GTK_CTREE(folderview->ctree), folderview->opened);
744
745         folderview->selected = folderview->opened = NULL;
746 }
747
748 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
749                                                  GtkCTreeNode *node)
750 {
751         FolderItem *item;
752
753         if (node)
754                 node = gtkut_ctree_node_next(ctree, node);
755         else
756                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
757
758         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
759                 item = gtk_ctree_node_get_row_data(ctree, node);
760                 if (item && item->unread_msgs > 0 && item->stype != F_TRASH)
761                         return node;
762         }
763
764         return NULL;
765 }
766
767 void folderview_select_next_unread(FolderView *folderview)
768 {
769         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
770         GtkCTreeNode *node = NULL;
771
772         if ((node = folderview_find_next_unread(ctree, folderview->opened))
773             != NULL) {
774                 folderview_select_node(folderview, node);
775                 return;
776         }
777
778         if (!folderview->opened ||
779             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
780                 return;
781         /* search again from the first node */
782         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
783                 folderview_select_node(folderview, node);
784 }
785
786 static GtkCTreeNode *folderview_find_next_new(GtkCTree *ctree,
787                                                  GtkCTreeNode *node)
788 {
789         FolderItem *item;
790
791         if (node)
792                 node = gtkut_ctree_node_next(ctree, node);
793         else
794                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
795
796         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
797                 item = gtk_ctree_node_get_row_data(ctree, node);
798                 if (item && item->new_msgs > 0 && item->stype != F_TRASH)
799                         return node;
800         }
801
802         return NULL;
803 }
804
805 void folderview_select_next_new(FolderView *folderview)
806 {
807         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
808         GtkCTreeNode *node = NULL;
809
810         if ((node = folderview_find_next_new(ctree, folderview->opened))
811             != NULL) {
812                 folderview_select_node(folderview, node);
813                 return;
814         }
815
816         if (!folderview->opened ||
817             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
818                 return;
819         /* search again from the first node */
820         if ((node = folderview_find_next_new(ctree, NULL)) != NULL)
821                 folderview_select_node(folderview, node);
822 }
823
824 FolderItem *folderview_get_selected_item(FolderView *folderview)
825 {
826         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
827
828         if (!folderview->selected) return NULL;
829         return gtk_ctree_node_get_row_data(ctree, folderview->selected);
830 }
831
832 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
833 {
834         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
835         static GtkCTreeNode *prev_row = NULL;
836         FolderItem *item;
837         gint new, unread, total;
838         gchar *new_str, *unread_str, *total_str;
839         gint *col_pos = folderview->col_pos;
840
841         if (!row) return;
842
843         item = gtk_ctree_node_get_row_data(ctree, row);
844         if (!item) return;
845
846         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_NEW], &new_str);
847         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_UNREAD], &unread_str);
848         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_TOTAL], &total_str);
849         new = atoi(new_str);
850         unread = atoi(unread_str);
851         total = atoi(total_str);
852
853         prev_row = row;
854
855         folderview_update_node(folderview, row);
856 }
857
858 void folderview_append_item(FolderItem *item)
859 {
860         GList *list;
861         
862         g_return_if_fail(item != NULL);
863         g_return_if_fail(item->folder != NULL);
864         if (folder_item_parent(item)) return;
865
866         for (list = folderview_list; list != NULL; list = list->next) {
867                 FolderView *folderview = (FolderView *)list->data;
868                 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
869                 GtkCTreeNode *node, *child;
870                 gint *col_pos = folderview->col_pos;
871
872                 node = gtk_ctree_find_by_row_data(ctree, NULL, 
873                                                   folder_item_parent(item));
874                 if (node) {
875                         child = gtk_ctree_find_by_row_data(ctree, node, item);
876                         if (!child) {
877                                 gchar *text[N_FOLDER_COLS] =
878                                         {NULL, "0", "0", "0"};
879
880                                 gtk_clist_freeze(GTK_CLIST(ctree));
881
882                                 text[col_pos[F_COL_FOLDER]] = item->name;
883                                 child = gtk_ctree_insert_node
884                                         (ctree, node, NULL, text,
885                                          FOLDER_SPACING,
886                                          folderxpm, folderxpmmask,
887                                          folderopenxpm, folderopenxpmmask,
888                                          FALSE, FALSE);
889                                 gtk_ctree_node_set_row_data(ctree, child, item);
890                                 gtk_ctree_expand(ctree, node);
891                                 folderview_update_node(folderview, child);
892                                 folderview_sort_folders(folderview, node,
893                                                         item->folder);
894
895                                 gtk_clist_thaw(GTK_CLIST(ctree));
896                         }
897                 }
898         }
899 }
900
901 static void folderview_set_folders(FolderView *folderview)
902 {
903         GList *list;
904
905         list = folder_get_list();
906
907         for (; list != NULL; list = list->next)
908                 folderview_append_folder(folderview, FOLDER(list->data));
909 }
910
911 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
912                                       gpointer data)
913 {
914         GList *list;
915
916         for (list = folderview_list; list != NULL; list = list->next) {
917                 FolderView *folderview = (FolderView *)list->data;
918                 MainWindow *mainwin = folderview->mainwin;
919                 gchar *str;
920
921                 if (item->path)
922                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
923                                               item->folder->name, G_DIR_SEPARATOR,
924                                               item->path);
925                 else
926                         str = g_strdup_printf(_("Scanning folder %s ..."),
927                                               item->folder->name);
928
929                 STATUSBAR_PUSH(mainwin, str);
930                 STATUSBAR_POP(mainwin);
931                 g_free(str);
932         }
933 }
934
935 static GtkWidget *label_window_create(const gchar *str)
936 {
937         GtkWidget *window;
938         GtkWidget *label;
939
940         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
941         gtk_widget_set_size_request(window, 380, 60);
942         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
943         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
944         gtk_window_set_title(GTK_WINDOW(window), str);
945         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
946         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
947         manage_window_set_transient(GTK_WINDOW(window));
948
949         label = gtk_label_new(str);
950         gtk_container_add(GTK_CONTAINER(window), label);
951         gtk_widget_show(label);
952
953         gtk_widget_show_now(window);
954
955         return window;
956 }
957
958 void folderview_rescan_tree(Folder *folder)
959 {
960         GtkWidget *window;
961
962         g_return_if_fail(folder != NULL);
963
964         if (!folder->klass->scan_tree) return;
965
966         inc_lock();
967         window = label_window_create(_("Rebuilding folder tree..."));
968
969         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
970         folder_scan_tree(folder);
971         folder_set_ui_func(folder, NULL, NULL);
972
973         folderview_set_all();
974
975         gtk_widget_destroy(window);
976         inc_unlock();
977 }
978
979 /** folderview_check_new()
980  *  Scan and update the folder and return the 
981  *  count the number of new messages since last check. 
982  *  \param folder the folder to check for new messages
983  *  \return the number of new messages since last check
984  */
985 gint folderview_check_new(Folder *folder)
986 {
987         GList *list;
988         FolderItem *item;
989         FolderView *folderview;
990         GtkCTree *ctree;
991         GtkCTreeNode *node;
992         gint new_msgs = 0;
993         gint former_new_msgs = 0;
994         gint former_new = 0;
995
996         for (list = folderview_list; list != NULL; list = list->next) {
997                 folderview = (FolderView *)list->data;
998                 ctree = GTK_CTREE(folderview->ctree);
999
1000                 inc_lock();
1001                 main_window_lock(folderview->mainwin);
1002                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
1003
1004                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1005                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1006                         item = gtk_ctree_node_get_row_data(ctree, node);
1007                         if (!item || !item->path || !item->folder) continue;
1008                         if (item->no_select) continue;
1009                         if (folder && folder != item->folder) continue;
1010                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1011                         if (!item->prefs->newmailcheck) continue;
1012
1013                         folderview_scan_tree_func(item->folder, item, NULL);
1014                         former_new = item->new_msgs;
1015
1016                         if (folder_item_scan(item) < 0) {
1017                                 summaryview_unlock(folderview->summaryview, item);
1018                                 if (folder && !FOLDER_IS_LOCAL(folder))
1019                                         break;
1020                         }
1021
1022                         folderview_update_node(folderview, node);
1023                         new_msgs += item->new_msgs;
1024                         former_new_msgs += former_new;
1025                 }
1026
1027                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
1028                 main_window_unlock(folderview->mainwin);
1029                 inc_unlock();
1030         }
1031
1032         folder_write_list();
1033         /* Number of new messages since last check is the just the difference 
1034          * between former_new_msgs and new_msgs. If new_msgs is less than
1035          * former_new_msgs, that would mean another session accessed the folder
1036          * and the result is not well defined.
1037          */
1038         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1039         return new_msgs;
1040 }
1041
1042 void folderview_check_new_all(void)
1043 {
1044         GList *list;
1045         GtkWidget *window;
1046         FolderView *folderview;
1047
1048         folderview = (FolderView *)folderview_list->data;
1049
1050         inc_lock();
1051         main_window_lock(folderview->mainwin);
1052         window = label_window_create
1053                 (_("Checking for new messages in all folders..."));
1054
1055         list = folder_get_list();
1056         for (; list != NULL; list = list->next) {
1057                 Folder *folder = list->data;
1058
1059                 folderview_check_new(folder);
1060         }
1061
1062         folder_write_list();
1063         folderview_set_all();
1064
1065         gtk_widget_destroy(window);
1066         main_window_unlock(folderview->mainwin);
1067         inc_unlock();
1068 }
1069
1070 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1071                                                  FolderItem *item,
1072                                                  gboolean in_sub)
1073 {
1074         GNode *node = NULL;
1075         
1076         if (!item || !item->folder || !item->folder->node)
1077                 return FALSE;
1078                 
1079         node = item->folder->node;
1080         
1081         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1082         node = node->children;
1083
1084         if (in_sub &&
1085             (item->new_msgs > 0 ||
1086             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1087                 return TRUE;
1088         }
1089
1090         while (node != NULL) {
1091                 if (node && node->data) {
1092                         FolderItem *next_item = (FolderItem*) node->data;
1093                         node = node->next;
1094                         if (folderview_have_new_children_sub(folderview, 
1095                                                              next_item, TRUE))
1096                                 return TRUE;
1097                 }
1098         }
1099
1100         return FALSE;
1101 }
1102
1103 static gboolean folderview_have_new_children(FolderView *folderview,
1104                                              FolderItem *item)
1105 {
1106         return folderview_have_new_children_sub(folderview, item, FALSE);
1107 }
1108
1109 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1110                                                     FolderItem *item, 
1111                                                     gboolean in_sub)
1112 {
1113         GNode *node = NULL;
1114         
1115         if (!item || !item->folder || !item->folder->node)
1116                 return FALSE;
1117         
1118         node = item->folder->node;
1119         
1120         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1121         node = node->children;
1122
1123         if (in_sub &&
1124             (item->unread_msgs > 0 ||
1125             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1126                 return TRUE;
1127         }
1128
1129         while (node != NULL) {
1130                 if (node && node->data) {
1131                         FolderItem *next_item = (FolderItem*) node->data;
1132                         node = node->next;
1133                         if (folderview_have_unread_children_sub(folderview, 
1134                                                                 next_item, 
1135                                                                 TRUE))
1136                                 return TRUE;
1137                 }
1138         }
1139
1140         return FALSE;
1141 }
1142
1143 static gboolean folderview_have_unread_children(FolderView *folderview,
1144                                                 FolderItem *item)
1145 {
1146         return folderview_have_unread_children_sub(folderview, item, FALSE);
1147 }
1148
1149 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1150                                                       FolderItem *item,
1151                                                       gboolean in_sub)
1152 {
1153         GNode *node = NULL;
1154
1155         if (!item || !item->folder || !item->folder->node)
1156                 return FALSE;
1157
1158         node = item->folder->node;
1159         
1160         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1161         node = node->children;
1162
1163         if (in_sub && item->search_match){
1164                 return TRUE;
1165         }
1166
1167         while (node != NULL) {
1168                 if (node && node->data) {
1169                         FolderItem *next_item = (FolderItem*) node->data;
1170                         node = node->next;
1171                         if (folderview_have_matching_children_sub(folderview, 
1172                                                                   next_item, 
1173                                                                   TRUE))
1174                                 return TRUE;
1175                 }
1176         }
1177
1178         return FALSE;
1179 }
1180
1181 static gboolean folderview_have_matching_children(FolderView *folderview,
1182                                                   FolderItem *item)
1183 {
1184         return folderview_have_matching_children_sub(folderview, item, FALSE);
1185 }
1186
1187 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1188 {
1189         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1190         GtkStyle *style = NULL;
1191         GtkStyle *color_style = NULL;
1192         FolderItem *item;
1193         GdkPixmap *xpm, *openxpm;
1194         GdkBitmap *mask, *openmask;
1195         static GdkPixmap *searchicon;
1196         static GdkBitmap *searchmask;
1197         gboolean mark = FALSE;
1198         gchar *name;
1199         gchar *str;
1200         gboolean add_unread_mark;
1201         gboolean add_sub_match_mark;
1202         gboolean use_bold, use_color;
1203         gint *col_pos = folderview->col_pos;
1204         SpecialFolderItemType stype;
1205         
1206         item = gtk_ctree_node_get_row_data(ctree, node);
1207         g_return_if_fail(item != NULL);
1208
1209         mark = (item->marked_msgs != 0);
1210
1211         stype = item->stype;
1212         if (stype == F_NORMAL) {
1213                 if (folder_has_parent_of_type(item, F_TRASH))
1214                         stype = F_TRASH;
1215                 else if (folder_has_parent_of_type(item, F_DRAFT))
1216                         stype = F_DRAFT;
1217                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1218                         stype = F_OUTBOX;
1219                 else if (folder_has_parent_of_type(item, F_QUEUE))
1220                         stype = F_QUEUE;
1221         }
1222         switch (stype) {
1223         case F_INBOX:
1224                 if (item->hide_read_msgs) {
1225                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1226                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1227                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1228                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1229                 } else {
1230                         xpm = mark?m_inboxxpm:inboxxpm;
1231                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1232                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1233                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1234                 }
1235                 break;
1236         case F_OUTBOX:
1237                 if (item->hide_read_msgs) {
1238                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1239                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1240                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1241                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1242                 } else {
1243                         xpm = mark?m_outboxxpm:outboxxpm;
1244                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1245                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1246                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1247                 }
1248                 break;
1249         case F_QUEUE:
1250                 if (item->hide_read_msgs) {
1251                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1252                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1253                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1254                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1255                 } else {
1256                         xpm = mark?m_queuexpm:queuexpm;
1257                         mask = mark?m_queuexpmmask:queuexpmmask;
1258                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1259                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1260                 }
1261                 break;
1262         case F_TRASH:
1263                 if (item->hide_read_msgs) {
1264                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1265                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1266                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1267                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1268                 } else {
1269                         xpm = mark?m_trashxpm:trashxpm;
1270                         mask = mark?m_trashxpmmask:trashxpmmask;
1271                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1272                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1273                 }
1274                 break;
1275         case F_DRAFT:
1276                 xpm = mark?m_draftsxpm:draftsxpm;
1277                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1278                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1279                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1280                 break;
1281         default:
1282                 if (item->hide_read_msgs) {
1283                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1284                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1285                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1286                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1287                 } else {
1288                         xpm = mark?m_folderxpm:folderxpm;
1289                         mask = mark?m_folderxpmmask:folderxpmmask;
1290                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1291                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1292                 }
1293         }
1294         
1295         if (item->no_select) {
1296                 xpm = openxpm = noselectxpm;
1297                 mask = openmask = noselectxpmmask;
1298         }
1299
1300         name = folder_item_get_name(item);
1301
1302         if (!GTK_CTREE_ROW(node)->expanded) {
1303                 add_unread_mark = folderview_have_unread_children(
1304                                         folderview, item);
1305                 add_sub_match_mark = folderview_have_matching_children(
1306                                         folderview, item);
1307         } else {
1308                 add_unread_mark = FALSE;
1309                 add_sub_match_mark = FALSE;
1310         }
1311
1312         if (item->search_match) {
1313                 if (!searchicon) {
1314                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1315                          &searchicon, &searchmask);
1316                 }
1317                 xpm = openxpm = searchicon;
1318                 mask = openmask = searchmask;
1319         }
1320
1321         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1322             prefs_common.display_folder_unread) {
1323                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1324                                       add_unread_mark ? "+" : "");
1325                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1326                                         xpm, mask, openxpm, openmask,
1327                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1328                 g_free(str);
1329         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1330                     prefs_common.display_folder_unread) 
1331                    || add_sub_match_mark) {
1332
1333                 if (item->unread_msgs > 0)
1334                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1335                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1336                                               item->unreadmarked_msgs > 0 ? "!":"");
1337                 else
1338                         str = g_strdup_printf("%s (+)", name);
1339                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1340                                         xpm, mask, openxpm, openmask,
1341                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1342                 g_free(str);
1343         } else {
1344                 str = g_strdup_printf("%s%s", name, 
1345                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1346         
1347                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1348                                         xpm, mask, openxpm, openmask,
1349                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1350                 g_free(str);
1351         }
1352         g_free(name);
1353
1354         if (!folder_item_parent(item)) {
1355                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1356                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1357                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1358         } else {
1359                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1360                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1361                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1362         }
1363
1364         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1365             folder_has_parent_of_type(item, F_DRAFT) ||
1366             folder_has_parent_of_type(item, F_TRASH)) {
1367                 use_bold = use_color = FALSE;
1368         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1369                 /* highlight queue folder if there are any messages */
1370                 use_bold = use_color = (item->total_msgs > 0);
1371         } else {
1372                 /* if unread messages exist, print with bold font */
1373                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1374                                 || add_unread_mark;
1375                 /* if new messages exist, print with colored letter */
1376                 use_color =
1377                         (item->new_msgs > 0) ||
1378                         (add_unread_mark &&
1379                          folderview_have_new_children(folderview, item));       
1380         }
1381
1382         gtk_ctree_node_set_foreground(ctree, node, NULL);
1383
1384         if (use_bold) {
1385                 if (item->prefs->color > 0 && !use_color) {
1386                         GdkColor gdk_color;
1387
1388                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1389                         color_style = gtk_style_copy(bold_style);
1390                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1391                         style = color_style;
1392                 } else if (use_color)
1393                         style = bold_color_style;
1394                 else
1395                         style = bold_style;
1396                 if (item->op_count > 0) {
1397                         style = bold_tgtfold_style;
1398                 }
1399         } else if (use_color) {
1400                 style = normal_color_style;
1401                 gtk_ctree_node_set_foreground(ctree, node,
1402                                               &folderview->color_new);
1403         } else if (item->op_count > 0) {
1404                 style = bold_tgtfold_style;
1405         } else if (item->prefs->color > 0) {
1406                 GdkColor gdk_color;
1407                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1408                 color_style = gtk_style_copy(normal_style);
1409                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1410                 style = color_style;
1411         } else {
1412                 style = normal_style;
1413         }
1414
1415         gtk_ctree_node_set_row_style(ctree, node, style);
1416
1417         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1418                 folderview_update_node(folderview, node);
1419 }
1420
1421 #if !CLAWS /* keep it here for syncs */
1422 void folderview_update_item(FolderItem *item, gboolean update_summary)
1423 {
1424         GList *list;
1425         FolderView *folderview;
1426         GtkCTree *ctree;
1427         GtkCTreeNode *node;
1428
1429         g_return_if_fail(item != NULL);
1430
1431         for (list = folderview_list; list != NULL; list = list->next) {
1432                 folderview = (FolderView *)list->data;
1433                 ctree = GTK_CTREE(folderview->ctree);
1434
1435                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1436                 if (node) {
1437                         folderview_update_node(folderview, node);
1438                         if (update_summary && folderview->opened == node)
1439                                 summary_show(folderview->summaryview,
1440                                              item, FALSE);
1441                 }
1442         }
1443 }
1444 #endif
1445
1446 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1447 {
1448         GList *list;
1449         FolderView *folderview;
1450         GtkCTree *ctree;
1451         GtkCTreeNode *node;
1452
1453         g_return_if_fail(item != NULL);
1454
1455         for (list = folderview_list; list != NULL; list = list->next) {
1456                 folderview = (FolderView *)list->data;
1457                 ctree = GTK_CTREE(folderview->ctree);
1458
1459                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1460                 if (node) {
1461                         item->search_match = matches;
1462                         folderview_update_node(folderview, node);
1463                 }
1464         }
1465 }
1466
1467 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1468 {
1469         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1470         FolderView *folderview = (FolderView *)data;
1471         GtkCTree *ctree;
1472         GtkCTreeNode *node;
1473         g_return_val_if_fail(update_info != NULL, TRUE);
1474         g_return_val_if_fail(update_info->item != NULL, TRUE);
1475         g_return_val_if_fail(folderview != NULL, FALSE);
1476
1477         ctree = GTK_CTREE(folderview->ctree);
1478
1479         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1480         if (node) {
1481                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1482                         folderview_update_node(folderview, node);
1483                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1484                         summary_show(folderview->summaryview, update_info->item);
1485         }
1486         
1487         return FALSE;
1488 }
1489
1490 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1491                                                 gpointer data)
1492 {
1493         /* CLAWS: share this joy with other hook functions ... */
1494         folder_item_update((FolderItem *)key, 
1495                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1496 }
1497
1498 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1499 {
1500         GList *list;
1501         FolderItemUpdateFlags flags;
1502         
1503         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1504                 : 0;
1505         for (list = folderview_list; list != NULL; list = list->next)
1506                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1507                                      GINT_TO_POINTER(flags));
1508 }
1509
1510 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1511                                       GNode *gnode, GtkCTreeNode *cnode,
1512                                       gpointer data)
1513 {
1514         FolderView *folderview = (FolderView *)data;
1515         FolderItem *item = FOLDER_ITEM(gnode->data);
1516
1517         g_return_val_if_fail(item != NULL, FALSE);
1518
1519         gtk_ctree_node_set_row_data(ctree, cnode, item);
1520         folderview_update_node(folderview, cnode);
1521
1522         return TRUE;
1523 }
1524
1525 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1526                                    gpointer data)
1527 {
1528         FolderView *folderview = (FolderView *)data;
1529         FolderItem *item;
1530
1531         if (GTK_CTREE_ROW(node)->children) {
1532                 item = gtk_ctree_node_get_row_data(ctree, node);
1533                 g_return_if_fail(item != NULL);
1534
1535                 if (!item->collapsed)
1536                         gtk_ctree_expand(ctree, node);
1537                 else
1538                         folderview_update_node(folderview, node);
1539         }
1540 }
1541
1542 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1543                                GtkCTreeNode *root, GtkCTreeNode **prev)
1544 {
1545         if (item) {
1546                 GtkCTreeNode *node, *parent, *sibling;
1547
1548                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1549                 if (!node)
1550                         g_warning("%s not found.\n", item->path);
1551                 else {
1552                         parent = GTK_CTREE_ROW(node)->parent;
1553                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1554                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1555                         else
1556                                 sibling = GTK_CTREE_ROW(parent)->children;
1557                         while (sibling) {
1558                                 FolderItem *tmp;
1559
1560                                 tmp = gtk_ctree_node_get_row_data
1561                                         (ctree, sibling);
1562                                 if (tmp->stype != F_NORMAL)
1563                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1564                                 else
1565                                         break;
1566                         }
1567                         if (node != sibling)
1568                                 gtk_ctree_move(ctree, node, parent, sibling);
1569                 }
1570
1571                 *prev = node;
1572         }
1573 }
1574
1575 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1576                                     Folder *folder)
1577 {
1578         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1579         GtkCTreeNode *prev = NULL;
1580
1581         gtk_sctree_sort_recursive(ctree, root);
1582
1583         if (root && GTK_CTREE_ROW(root)->parent) return;
1584
1585         set_special_folder(ctree, folder->inbox, root, &prev);
1586         set_special_folder(ctree, folder->outbox, root, &prev);
1587         set_special_folder(ctree, folder->draft, root, &prev);
1588         set_special_folder(ctree, folder->queue, root, &prev);
1589         set_special_folder(ctree, folder->trash, root, &prev);
1590 }
1591
1592 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1593 {
1594         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1595         GtkCTreeNode *root;
1596
1597         g_return_if_fail(folder != NULL);
1598
1599         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1600                                       folderview_gnode_func, folderview);
1601         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1602                                 folderview);
1603         folderview_sort_folders(folderview, root, folder);
1604 }
1605
1606 /* callback functions */
1607
1608 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1609                                           FolderView *folderview)
1610 {
1611         GtkCList *clist = GTK_CLIST(ctree);
1612         gint prev_row = -1, row = -1, column = -1;
1613         FolderItem *item;
1614         Folder *folder;
1615         FolderViewPopup *fpopup;
1616         GtkItemFactory *fpopup_factory;
1617         GtkWidget *popup;
1618         FolderItem *special_trash = NULL;
1619         PrefsAccount *ac;
1620
1621         if (!event) return FALSE;
1622
1623         if (event->button == 1) {
1624                 folderview->open_folder = TRUE;
1625
1626                 if (event->type == GDK_2BUTTON_PRESS) {
1627                         if (clist->selection) {
1628                                 GtkCTreeNode *node;
1629
1630                                 node = GTK_CTREE_NODE(clist->selection->data);
1631                                 if (node)
1632                                         gtk_ctree_toggle_expansion(
1633                                                 GTK_CTREE(ctree),
1634                                                 node);
1635                         }
1636                 }
1637                 return FALSE;
1638         }
1639
1640         if (event->button == 2 || event->button == 3) {
1641                 /* right clicked */
1642                 if (clist->selection) {
1643                         GtkCTreeNode *node;
1644
1645                         node = GTK_CTREE_NODE(clist->selection->data);
1646                         if (node)
1647                                 prev_row = gtkut_ctree_get_nth_from_node
1648                                         (GTK_CTREE(ctree), node);
1649                 }
1650
1651                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1652                                                   &row, &column))
1653                         return FALSE;
1654                 if (prev_row != row) {
1655                         gtk_clist_unselect_all(clist);
1656                         if (event->button == 2)
1657                                 folderview_select_node
1658                                         (folderview,
1659                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1660                                                             row));
1661                         else
1662                                 gtk_clist_select_row(clist, row, column);
1663                 }
1664         }
1665
1666         if (event->button != 3) return FALSE;
1667
1668         item = gtk_clist_get_row_data(clist, row);
1669         g_return_val_if_fail(item != NULL, FALSE);
1670         g_return_val_if_fail(item->folder != NULL, FALSE);
1671         folder = item->folder;
1672
1673         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1674         if (fpopup != NULL)
1675                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1676         else {
1677                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1678                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1679         }
1680
1681         if (fpopup->set_sensitivity != NULL)
1682                 fpopup->set_sensitivity(fpopup_factory, item);
1683
1684         if (NULL != (ac = account_find_from_item(item)))
1685                 special_trash = account_get_special_folder(ac, F_TRASH);
1686
1687         if ((item == folder->trash || item == special_trash
1688              || folder_has_parent_of_type(item, F_TRASH)) &&
1689             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1690                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1691                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1692         } else if (item != folder->trash && (special_trash == NULL || item != special_trash)
1693                 && !folder_has_parent_of_type(item, F_TRASH)) {
1694                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1695                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1696         }
1697         
1698 #define SET_SENS(name, sens) \
1699         menu_set_sensitive(fpopup_factory, name, sens)
1700
1701         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1702         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1703                  folderview->selected == folderview->opened);
1704         SET_SENS("/Properties...", item->node->parent != NULL);
1705         SET_SENS("/Processing...", item->node->parent != NULL);
1706         if (item == folder->trash || item == special_trash
1707             || folder_has_parent_of_type(item, F_TRASH)) {
1708                 GSList *msglist = folder_item_get_msg_list(item);
1709                 SET_SENS("/Empty trash...", msglist != NULL);
1710                 procmsg_msg_list_free(msglist);
1711         }
1712 #undef SET_SENS
1713
1714         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1715         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1716                        event->button, event->time);
1717
1718         return FALSE;
1719 }
1720
1721 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1722                                            FolderView *folderview)
1723 {
1724         if (!event) return FALSE;
1725
1726         if (event->button == 1 && folderview->open_folder == FALSE &&
1727             folderview->opened != NULL) {
1728                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1729                                           folderview->opened);
1730                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1731         }
1732
1733         return FALSE;
1734 }
1735
1736 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1737                                        FolderView *folderview)
1738 {
1739         if (!event) return FALSE;
1740
1741         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1742                 return FALSE;
1743
1744         switch (event->keyval) {
1745         case GDK_Return:
1746                 if (folderview->selected) {
1747                         folderview_select_node(folderview,
1748                                                folderview->selected);
1749                 }
1750                 break;
1751         case GDK_space:
1752                 if (folderview->selected) {
1753                         if (folderview->opened == folderview->selected &&
1754                             (!folderview->summaryview->folder_item ||
1755                              folderview->summaryview->folder_item->total_msgs == 0))
1756                                 folderview_select_next_unread(folderview);
1757                         else
1758                                 folderview_select_node(folderview,
1759                                                        folderview->selected);
1760                 }
1761                 break;
1762         default:
1763                 break;
1764         }
1765
1766         return FALSE;
1767 }
1768
1769 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1770                                 gint column, FolderView *folderview)
1771 {
1772         static gboolean can_select = TRUE;      /* exclusive lock */
1773         gboolean opened;
1774         FolderItem *item;
1775         gchar *buf;
1776
1777         folderview->selected = row;
1778
1779         if (folderview->opened == row) {
1780                 folderview->open_folder = FALSE;
1781                 return;
1782         }
1783         
1784         if (!can_select || summary_is_locked(folderview->summaryview)) {
1785                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1786                 gtk_ctree_select(ctree, folderview->opened);
1787                 return;
1788         }
1789
1790         if (!folderview->open_folder) return;
1791
1792         item = gtk_ctree_node_get_row_data(ctree, row);
1793         if (!item || item->no_select) return;
1794
1795         can_select = FALSE;
1796
1797         /* Save cache for old folder */
1798         /* We don't want to lose all caches if sylpheed crashed */
1799         if (folderview->opened) {
1800                 FolderItem *olditem;
1801                 
1802                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1803                 if (olditem) {
1804                         /* will be null if we just moved the previously opened folder */
1805                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1806                         folder_item_close(olditem);
1807                 }
1808         }
1809
1810         /* CLAWS: set compose button type: news folder items 
1811          * always have a news folder as parent */
1812         if (item->folder) 
1813                 toolbar_set_compose_button
1814                         (folderview->mainwin->toolbar,
1815                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1816                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1817
1818         if (item->path)
1819                 debug_print("Folder %s is selected\n", item->path);
1820
1821         if (!GTK_CTREE_ROW(row)->children)
1822                 gtk_ctree_expand(ctree, row);
1823         if (folderview->opened &&
1824             !GTK_CTREE_ROW(folderview->opened)->children)
1825                 gtk_ctree_collapse(ctree, folderview->opened);
1826
1827         /* ungrab the mouse event */
1828         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1829                 gtk_grab_remove(GTK_WIDGET(ctree));
1830                 if (gdk_pointer_is_grabbed())
1831                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1832         }
1833
1834         /* Open Folder */
1835         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1836                                         item->path : "(null)");
1837         debug_print("%s\n", buf);
1838         STATUSBAR_PUSH(folderview->mainwin, buf);
1839         g_free(buf);
1840
1841         main_window_cursor_wait(folderview->mainwin);
1842
1843         if (folder_item_open(item) != 0) {
1844                 main_window_cursor_normal(folderview->mainwin);
1845                 STATUSBAR_POP(folderview->mainwin);
1846
1847                 alertpanel_error(_("Folder could not be opened."));
1848
1849                 folderview->open_folder = FALSE;
1850                 can_select = TRUE;
1851
1852                 return;
1853         }
1854
1855         main_window_cursor_normal(folderview->mainwin);
1856
1857         /* Show messages */
1858         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1859         opened = summary_show(folderview->summaryview, item);
1860         
1861         folder_clean_cache_memory();
1862
1863         if (!opened) {
1864                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1865                 gtk_ctree_select(ctree, folderview->opened);
1866         } else {
1867                 folderview->opened = row;
1868                 if (gtk_ctree_node_is_visible(ctree, row)
1869                     != GTK_VISIBILITY_FULL)
1870                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1871         }
1872
1873         STATUSBAR_POP(folderview->mainwin);
1874
1875         folderview->open_folder = FALSE;
1876         can_select = TRUE;
1877 }
1878
1879 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1880                                      FolderView *folderview)
1881 {
1882         FolderItem *item;
1883
1884         item = gtk_ctree_node_get_row_data(ctree, node);
1885         g_return_if_fail(item != NULL);
1886         item->collapsed = FALSE;
1887         folderview_update_node(folderview, node);
1888 }
1889
1890 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1891                                       FolderView *folderview)
1892 {
1893         FolderItem *item;
1894
1895         item = gtk_ctree_node_get_row_data(ctree, node);
1896         g_return_if_fail(item != NULL);
1897         item->collapsed = TRUE;
1898         folderview_update_node(folderview, node);
1899 }
1900
1901 static void folderview_popup_close(GtkMenuShell *menu_shell,
1902                                    FolderView *folderview)
1903 {
1904         if (!folderview->opened) return;
1905
1906         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1907 }
1908
1909 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1910                                    FolderView *folderview)
1911 {
1912         FolderColumnType type = folderview->col_state[column].type;
1913
1914         prefs_common.folder_col_size[type] = width;
1915 }
1916
1917 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1918 {
1919         GNode *srcnode;
1920
1921         folderview_create_folder_node(folderview, item);
1922
1923         if (!item || !item->folder || !item->folder->node)
1924                 return;
1925
1926         srcnode = item->folder->node;   
1927         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1928         srcnode = srcnode->children;
1929         while (srcnode != NULL) {
1930                 if (srcnode && srcnode->data) {
1931                         FolderItem *next_item = (FolderItem*) srcnode->data;
1932                         folderview_create_folder_node_recursive(folderview, next_item);
1933                 }
1934                 srcnode = srcnode->next;
1935         }
1936 }
1937
1938 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1939 {
1940         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1941         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1942         GtkCTreeNode *node, *parent_node;
1943         gint *col_pos = folderview->col_pos;
1944         FolderItemUpdateData hookdata;
1945
1946         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
1947         if (parent_node == NULL)
1948                 return;
1949
1950         gtk_clist_freeze(GTK_CLIST(ctree));
1951
1952         text[col_pos[F_COL_FOLDER]] = item->name;
1953         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1954                                      FOLDER_SPACING,
1955                                      folderxpm, folderxpmmask,
1956                                      folderopenxpm, folderopenxpmmask,
1957                                      FALSE, FALSE);
1958         gtk_ctree_expand(ctree, parent_node);
1959         gtk_ctree_node_set_row_data(ctree, node, item);
1960         if (normal_style)
1961                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1962         folderview_sort_folders(folderview, parent_node, item->folder);
1963
1964         hookdata.item = item;
1965         hookdata.update_flags = F_ITEM_UPDATE_NAME;
1966         hookdata.msg = NULL;
1967         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
1968
1969         gtk_clist_thaw(GTK_CLIST(ctree));
1970 }
1971
1972 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
1973                                       GtkWidget *widget)
1974 {
1975         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1976         FolderItem *item;
1977         GSList *mlist = NULL;
1978         GSList *cur = NULL;
1979         if (!folderview->selected) return;
1980         FolderItem *special_trash = NULL;
1981         PrefsAccount *ac;
1982
1983         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1984         g_return_if_fail(item != NULL);
1985         g_return_if_fail(item->folder != NULL);
1986
1987         if (NULL != (ac = account_find_from_item(item)))
1988                 special_trash = account_get_special_folder(ac, F_TRASH);
1989
1990         if (item != item->folder->trash && item != special_trash
1991         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
1992         
1993         if (prefs_common.ask_on_clean) {
1994                 if (alertpanel(_("Empty trash"),
1995                                _("Delete all messages in trash?"),
1996                                GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
1997                         return;
1998         }
1999         
2000         mlist = folder_item_get_msg_list(item);
2001         
2002         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2003                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2004                 if (MSG_IS_LOCKED(msginfo->flags))
2005                         continue;
2006                 /* is it partially received? (partial_recv isn't cached) */
2007                 if (msginfo->total_size != 0 && 
2008                     msginfo->size != (off_t)msginfo->total_size)
2009                         partial_mark_for_delete(msginfo);
2010         }
2011         procmsg_msg_list_free(mlist);
2012
2013         folder_item_remove_all_msg(item);
2014 }
2015
2016 static void folderview_search_cb(FolderView *folderview, guint action,
2017                                  GtkWidget *widget)
2018 {
2019         summary_search(folderview->summaryview);
2020 }
2021
2022 static void folderview_property_cb(FolderView *folderview, guint action,
2023                                    GtkWidget *widget)
2024 {
2025         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2026         FolderItem *item;
2027
2028         if (!folderview->selected) return;
2029
2030         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2031         g_return_if_fail(item != NULL);
2032         g_return_if_fail(item->folder != NULL);
2033
2034         if (folder_item_parent(item) == NULL)
2035                 return;
2036
2037         prefs_folder_item_open(item);
2038 }
2039
2040 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2041 {
2042         GSList *list = NULL;
2043         GSList *done = NULL;
2044         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2045         
2046         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2047                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2048                 &&  list->data != node) {
2049                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2050                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2051                 }
2052         }
2053         for (list = done; list != NULL; list = g_slist_next(list)) {
2054                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2055                                                                  list->data);
2056         }
2057         g_slist_free(done);
2058 }
2059
2060 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2061                             FolderItem *to_folder)
2062 {
2063         FolderItem *from_parent = NULL;
2064         FolderItem *new_folder = NULL;
2065         GtkCTreeNode *src_node = NULL;
2066         gchar *buf;
2067         gint status;
2068
2069         g_return_if_fail(folderview != NULL);
2070         g_return_if_fail(from_folder != NULL);
2071         g_return_if_fail(to_folder != NULL);
2072
2073         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2074         from_parent = folder_item_parent(from_folder);
2075         
2076         if (prefs_common.warn_dnd) {
2077                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2078                                         "sub-folder of '%s' ?"), from_folder->name,
2079                                         to_folder->name);
2080                 status = alertpanel_full(_("Move folder"), buf,
2081                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, TRUE,
2082                                          NULL, ALERT_QUESTION, G_ALERTALTERNATE);
2083                 g_free(buf);
2084
2085                 if (status != G_ALERTDEFAULT
2086                  && status != (G_ALERTDEFAULT | G_ALERTDISABLE))
2087                         return;
2088                 if (status & G_ALERTDISABLE)
2089                         prefs_common.warn_dnd = FALSE;
2090         }
2091
2092         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2093         STATUSBAR_PUSH(folderview->mainwin, buf);
2094         g_free(buf);
2095         summary_clear_all(folderview->summaryview);
2096         folderview->opened = NULL;
2097         folderview->selected = NULL;
2098         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2099         inc_lock();
2100         main_window_cursor_wait(folderview->mainwin);
2101         statusbar_verbosity_set(TRUE);
2102         folder_item_update_freeze();
2103         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2104                 statusbar_verbosity_set(FALSE);
2105                 main_window_cursor_normal(folderview->mainwin);
2106                 STATUSBAR_POP(folderview->mainwin);
2107                 folder_item_update_thaw();
2108                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2109
2110                 folderview_sort_folders(folderview, 
2111                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2112                                 NULL, to_folder), new_folder->folder);
2113                 folderview_select(folderview, new_folder);
2114         } else {
2115                 statusbar_verbosity_set(FALSE);         
2116                 main_window_cursor_normal(folderview->mainwin);
2117                 STATUSBAR_POP(folderview->mainwin);
2118                 folder_item_update_thaw();
2119                 switch (status) {
2120                 case F_MOVE_FAILED_DEST_IS_PARENT:
2121                         alertpanel_error(_("Source and destination are the same."));
2122                         break;
2123                 case F_MOVE_FAILED_DEST_IS_CHILD:
2124                         alertpanel_error(_("Can't move a folder to one of its children."));
2125                         break;
2126                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2127                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2128                         break;
2129                 default:
2130                         alertpanel_error(_("Move failed!"));
2131                         break;
2132                 }
2133         }       
2134         inc_unlock();           
2135         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2136 }
2137
2138 static gint folderview_clist_compare(GtkCList *clist,
2139                                      gconstpointer ptr1, gconstpointer ptr2)
2140 {
2141         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2142         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2143
2144         if (!item1->name)
2145                 return (item2->name != NULL);
2146         if (!item2->name)
2147                 return -1;
2148
2149         return g_utf8_collate(item1->name, item2->name);
2150 }
2151
2152 static void folderview_processing_cb(FolderView *folderview, guint action,
2153                                      GtkWidget *widget)
2154 {
2155         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2156         FolderItem *item;
2157         gchar *id, *title;
2158
2159         if (!folderview->selected) return;
2160
2161         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2162         g_return_if_fail(item != NULL);
2163         g_return_if_fail(item->folder != NULL);
2164
2165         id = folder_item_get_identifier(item);
2166         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2167         g_free (id);
2168
2169         prefs_filtering_open(&item->prefs->processing, title, NULL, NULL);
2170         g_free (title);
2171 }
2172
2173 void folderview_set_target_folder_color(gint color_op) 
2174 {
2175         gint firstone = 1;
2176         GList *list;
2177         FolderView *folderview;
2178
2179         for (list = folderview_list; list != NULL; list = list->next) {
2180                 folderview = (FolderView *)list->data;
2181                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2182                 if (firstone) {
2183                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2184                                 folderview->color_op;
2185                         firstone = 0;
2186                 }
2187         }
2188 }
2189
2190 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2191 {
2192 }
2193
2194 void folderview_reflect_prefs(void)
2195 {
2196         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2197         normal_style = normal_color_style = bold_style = 
2198                 bold_color_style = bold_tgtfold_style = NULL;
2199         FolderItem *item = folderview_get_selected_item(folderview);
2200         folderview_init(folderview);
2201         folderview_column_set_titles(folderview);
2202         folderview_set_all();
2203         if (item)
2204                 folderview_select(folderview, item);
2205 }
2206
2207 static void drag_state_stop(FolderView *folderview)
2208 {
2209         if (folderview->drag_timer)
2210                 gtk_timeout_remove(folderview->drag_timer);
2211         folderview->drag_timer = 0;
2212         folderview->drag_node = NULL;
2213 }
2214
2215 static gint folderview_defer_expand(FolderView *folderview)
2216 {
2217         if (folderview->drag_node) {
2218                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2219                 if (folderview->drag_item->collapsed) {
2220                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2221                         folderview->nodes_to_recollapse = g_slist_append
2222                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2223                 }
2224         }
2225         folderview->drag_item  = NULL;
2226         folderview->drag_timer = 0;
2227         return FALSE;
2228 }
2229
2230 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2231 {
2232         /* the idea is that we call drag_state_start() whenever we want expansion to
2233          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2234          * we need to call drag_state_stop() */
2235         drag_state_stop(folderview);
2236         /* request expansion */
2237         if (0 != (folderview->drag_timer = gtk_timeout_add
2238                         (prefs_common.hover_timeout, 
2239                          (GtkFunction)folderview_defer_expand,
2240                          folderview))) {
2241                 folderview->drag_node = node;
2242                 folderview->drag_item = item;
2243         }                        
2244 }
2245
2246 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2247                                   FolderView       *folderview)
2248 {
2249         GdkDragContext *context;
2250
2251         g_return_if_fail(folderview != NULL);
2252         if (folderview->selected == NULL) return;
2253         if (folderview->nodes_to_recollapse) 
2254                 g_slist_free(folderview->nodes_to_recollapse);
2255         folderview->nodes_to_recollapse = NULL;
2256         context = gtk_drag_begin(widget, folderview->target_list,
2257                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2258         gtk_drag_set_icon_default(context);
2259 }
2260
2261 static void folderview_drag_data_get(GtkWidget        *widget,
2262                                      GdkDragContext   *drag_context,
2263                                      GtkSelectionData *selection_data,
2264                                      guint             info,
2265                                      guint             time,
2266                                      FolderView       *folderview)
2267 {
2268         FolderItem *item;
2269         GList *cur;
2270         gchar *source = NULL;
2271
2272         for (cur = GTK_CLIST(folderview->ctree)->selection;
2273              cur != NULL; cur = cur->next) {
2274                 item = gtk_ctree_node_get_row_data
2275                         (GTK_CTREE(folderview->ctree), 
2276                          GTK_CTREE_NODE(cur->data));
2277                 if (item) {
2278                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2279                         gtk_selection_data_set(selection_data,
2280                                                selection_data->target, 8,
2281                                                source, strlen(source));
2282                         break;
2283                 } else
2284                         return;
2285         }
2286 }
2287
2288 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2289 {
2290         FolderUpdateData *hookdata;
2291         FolderView *folderview;
2292         GtkWidget *ctree;
2293
2294         hookdata = source;
2295         folderview = (FolderView *) userdata;   
2296         g_return_val_if_fail(hookdata != NULL, FALSE);
2297         g_return_val_if_fail(folderview != NULL, FALSE);
2298
2299         ctree = folderview->ctree;
2300         g_return_val_if_fail(ctree != NULL, FALSE);
2301
2302         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2303                 folderview_create_folder_node(folderview, hookdata->item);
2304         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2305                 GtkCTreeNode *node;
2306
2307                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2308                 if (node != NULL)
2309                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2310         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2311                 folderview_set(folderview);
2312
2313         return FALSE;
2314 }
2315
2316 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2317                                           GdkDragContext *context,
2318                                           gint            x,
2319                                           gint            y,
2320                                           guint           time,
2321                                           FolderView     *folderview)
2322 {
2323         gint row, column;
2324         FolderItem *item = NULL, *src_item = NULL;
2325         GtkCTreeNode *node = NULL;
2326         gboolean acceptable = FALSE;
2327         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2328                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2329         int height = (int)pos->page_size;
2330         int total_height = (int)pos->upper;
2331         int vpos = (int) pos->value;
2332
2333         if (gtk_clist_get_selection_info
2334                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2335                 GtkWidget *srcwidget;
2336
2337                 if (y > height - 24 && height + vpos < total_height)
2338                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2339
2340                 if (y < 48 && y > 0)
2341                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2342
2343                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2344                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2345                 src_item = folderview->summaryview->folder_item;
2346
2347                 srcwidget = gtk_drag_get_source_widget(context);
2348                 if (srcwidget == folderview->summaryview->ctree) {
2349                         /* comes from summaryview */
2350                         /* we are copying messages, so only accept folder items that are not
2351                            the source item, are no root items and can copy messages */
2352                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2353                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2354                                 acceptable = TRUE;
2355                 } else if (srcwidget == folderview->ctree) {
2356                         /* comes from folderview */
2357                         /* we are moving folder items, only accept folders that are not
2358                            the source items and can copy messages and create folder items */
2359                         if (item && item->folder && src_item && src_item != item &&
2360                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2361                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2362                                 acceptable = TRUE;
2363                 }
2364         }
2365
2366         if (acceptable || (src_item && src_item == item))
2367                 drag_state_start(folderview, node, item);
2368         
2369         if (acceptable) {
2370                 g_signal_handlers_block_by_func
2371                         (G_OBJECT(widget),
2372                          G_CALLBACK(folderview_selected), folderview);
2373                 gtk_ctree_select(GTK_CTREE(widget), node);
2374                 g_signal_handlers_unblock_by_func
2375                         (G_OBJECT(widget),
2376                          G_CALLBACK(folderview_selected), folderview);
2377                 gdk_drag_status(context, 
2378                                         (context->actions == GDK_ACTION_COPY ?
2379                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2380         } else {
2381                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2382                 gdk_drag_status(context, 0, time);
2383         }
2384
2385         return acceptable;
2386 }
2387
2388 static void folderview_drag_leave_cb(GtkWidget      *widget,
2389                                      GdkDragContext *context,
2390                                      guint           time,
2391                                      FolderView     *folderview)
2392 {
2393         drag_state_stop(folderview);
2394         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2395 }
2396
2397 static void folderview_drag_received_cb(GtkWidget        *widget,
2398                                         GdkDragContext   *drag_context,
2399                                         gint              x,
2400                                         gint              y,
2401                                         GtkSelectionData *data,
2402                                         guint             info,
2403                                         guint             time,
2404                                         FolderView       *folderview)
2405 {
2406         gint row, column;
2407         FolderItem *item, *src_item;
2408         GtkCTreeNode *node;
2409
2410         drag_state_stop(folderview);
2411         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2412                 /* comes from summaryview */
2413                 if (gtk_clist_get_selection_info
2414                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2415                         return;
2416
2417                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2418                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2419                 src_item = folderview->summaryview->folder_item;
2420                 
2421                 /* re-check (due to acceptable possibly set for folder moves */
2422                 if (!(item && item->folder && item->path && !item->no_select && 
2423                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2424                         return;
2425                 }
2426                 if (item && src_item) {
2427                         switch (drag_context->action) {
2428                         case GDK_ACTION_COPY:
2429                                 summary_copy_selected_to(folderview->summaryview, item);
2430                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2431                                 break;
2432                         case GDK_ACTION_MOVE:
2433                         case GDK_ACTION_DEFAULT:
2434                         default:
2435                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2436                                         summary_copy_selected_to(folderview->summaryview, item);
2437                                 else
2438                                         summary_move_selected_to(folderview->summaryview, item);
2439                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2440                         }
2441                 } else
2442                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2443         } else {
2444                 /* comes from folderview */
2445                 char *source;
2446                 gboolean folder_is_normal = TRUE;
2447                 
2448                 source = data->data + 17;
2449                 if (gtk_clist_get_selection_info
2450                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2451                     || *source == 0) {
2452                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2453                         return;
2454                 }
2455                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2456                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2457                 src_item = folder_find_item_from_identifier(source);
2458
2459                 folder_is_normal = 
2460                         src_item != NULL &&
2461                         src_item->stype == F_NORMAL &&
2462                         !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2463                         !folder_has_parent_of_type(src_item, F_DRAFT) &&
2464                         !folder_has_parent_of_type(src_item, F_QUEUE) &&
2465                         !folder_has_parent_of_type(src_item, F_TRASH);
2466                 if (!item || item->no_select || !src_item 
2467                 ||  !folder_is_normal) {
2468                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2469                         return;
2470                 }
2471
2472                 folderview_move_folder(folderview, src_item, item);
2473                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2474         }
2475         folderview->nodes_to_recollapse = NULL;
2476 }
2477
2478 static void folderview_drag_end_cb(GtkWidget        *widget, 
2479                                    GdkDragContext   *drag_context,
2480                                    FolderView       *folderview)
2481 {
2482         drag_state_stop(folderview);
2483         g_slist_free(folderview->nodes_to_recollapse);
2484         folderview->nodes_to_recollapse = NULL;
2485 }
2486
2487 void folderview_register_popup(FolderViewPopup *fpopup)
2488 {
2489         GList *folderviews;
2490
2491         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2492                 FolderView *folderview = folderviews->data;
2493                 GtkItemFactory *factory;
2494
2495                 factory = create_ifactory(folderview, fpopup);
2496                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2497         }       
2498         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2499 }
2500
2501 void folderview_unregister_popup(FolderViewPopup *fpopup)
2502 {
2503         GList *folderviews;
2504
2505         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2506                 FolderView *folderview = folderviews->data;
2507
2508                 g_hash_table_remove(folderview->popups, fpopup->klass);
2509         }       
2510         g_hash_table_remove(folderview_popups, fpopup->klass);
2511 }