2005-08-08 [colin] 1.9.13cvs25
[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                 PangoFontDescription *font_desc;
643                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
644                 font_desc = pango_font_description_from_string(BOLD_FONT);
645                 if (font_desc) {
646                         if (bold_style->font_desc)
647                                 pango_font_description_free
648                                         (bold_style->font_desc);
649                         bold_style->font_desc = font_desc;
650                 }
651                 pango_font_description_set_weight
652                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
653                 bold_color_style = gtk_style_copy(bold_style);
654                 bold_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
655
656                 bold_tgtfold_style = gtk_style_copy(bold_style);
657                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
658         }
659 }
660
661 void folderview_set(FolderView *folderview)
662 {
663         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
664         MainWindow *mainwin = folderview->mainwin;
665
666         debug_print("Setting folder info...\n");
667         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
668
669         main_window_cursor_wait(mainwin);
670
671         folderview->selected = NULL;
672         folderview->opened = NULL;
673
674         gtk_clist_freeze(GTK_CLIST(ctree));
675         gtk_clist_clear(GTK_CLIST(ctree));
676         gtk_clist_thaw(GTK_CLIST(ctree));
677         gtk_clist_freeze(GTK_CLIST(ctree));
678
679         folderview_set_folders(folderview);
680
681         gtk_clist_thaw(GTK_CLIST(ctree));
682         main_window_cursor_normal(mainwin);
683         STATUSBAR_POP(mainwin);
684 }
685
686 void folderview_set_all(void)
687 {
688         GList *list;
689
690         for (list = folderview_list; list != NULL; list = list->next)
691                 folderview_set((FolderView *)list->data);
692 }
693
694 void folderview_select(FolderView *folderview, FolderItem *item)
695 {
696         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
697         GtkCTreeNode *node;
698         GtkCTreeNode *old_selected = folderview->selected;
699
700         if (!item) return;
701
702         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
703         if (node) folderview_select_node(folderview, node);
704
705         if (old_selected != node)
706                 folder_update_op_count();
707 }
708
709 static void mark_all_read_cb(FolderView *folderview, guint action,
710                              GtkWidget *widget)
711 {
712         FolderItem *item;
713
714         item = folderview_get_selected_item(folderview);
715         if (item == NULL)
716                 return;
717
718         summary_lock(folderview->summaryview);
719         folder_item_update_freeze();
720         if (folderview->summaryview->folder_item == item)
721                 gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
722         folderutils_mark_all_read(item);
723         if (folderview->summaryview->folder_item == item)
724                 gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
725         folder_item_update_thaw();
726         summary_unlock(folderview->summaryview);
727 }
728
729 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
730 {
731         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
732
733         g_return_if_fail(node != NULL);
734
735         folderview->open_folder = TRUE;
736         gtkut_ctree_set_focus_row(ctree, node);
737         gtk_ctree_select(ctree, node);
738         if (folderview->summaryview->folder_item &&
739             folderview->summaryview->folder_item->total_msgs > 0)
740                 gtk_widget_grab_focus(folderview->summaryview->ctree);
741         else
742                 gtk_widget_grab_focus(folderview->ctree);
743
744         gtkut_ctree_expand_parent_all(ctree, node);
745 }
746
747 void folderview_unselect(FolderView *folderview)
748 {
749         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
750                 gtk_ctree_collapse
751                         (GTK_CTREE(folderview->ctree), folderview->opened);
752
753         folderview->selected = folderview->opened = NULL;
754 }
755
756 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
757                                                  GtkCTreeNode *node)
758 {
759         FolderItem *item;
760
761         if (node)
762                 node = gtkut_ctree_node_next(ctree, node);
763         else
764                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
765
766         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
767                 item = gtk_ctree_node_get_row_data(ctree, node);
768                 if (item && item->unread_msgs > 0 && item->stype != F_TRASH)
769                         return node;
770         }
771
772         return NULL;
773 }
774
775 void folderview_select_next_unread(FolderView *folderview)
776 {
777         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
778         GtkCTreeNode *node = NULL;
779
780         if ((node = folderview_find_next_unread(ctree, folderview->opened))
781             != NULL) {
782                 folderview_select_node(folderview, node);
783                 return;
784         }
785
786         if (!folderview->opened ||
787             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
788                 return;
789         /* search again from the first node */
790         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
791                 folderview_select_node(folderview, node);
792 }
793
794 FolderItem *folderview_get_selected_item(FolderView *folderview)
795 {
796         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
797
798         if (!folderview->selected) return NULL;
799         return gtk_ctree_node_get_row_data(ctree, folderview->selected);
800 }
801
802 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
803 {
804         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
805         static GtkCTreeNode *prev_row = NULL;
806         FolderItem *item;
807         gint new, unread, total;
808         gchar *new_str, *unread_str, *total_str;
809         gint *col_pos = folderview->col_pos;
810
811         if (!row) return;
812
813         item = gtk_ctree_node_get_row_data(ctree, row);
814         if (!item) return;
815
816         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_NEW], &new_str);
817         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_UNREAD], &unread_str);
818         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_TOTAL], &total_str);
819         new = atoi(new_str);
820         unread = atoi(unread_str);
821         total = atoi(total_str);
822
823         prev_row = row;
824
825         folderview_update_node(folderview, row);
826 }
827
828 void folderview_append_item(FolderItem *item)
829 {
830         GList *list;
831         
832         g_return_if_fail(item != NULL);
833         g_return_if_fail(item->folder != NULL);
834         if (folder_item_parent(item)) return;
835
836         for (list = folderview_list; list != NULL; list = list->next) {
837                 FolderView *folderview = (FolderView *)list->data;
838                 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
839                 GtkCTreeNode *node, *child;
840                 gint *col_pos = folderview->col_pos;
841
842                 node = gtk_ctree_find_by_row_data(ctree, NULL, 
843                                                   folder_item_parent(item));
844                 if (node) {
845                         child = gtk_ctree_find_by_row_data(ctree, node, item);
846                         if (!child) {
847                                 gchar *text[N_FOLDER_COLS] =
848                                         {NULL, "0", "0", "0"};
849
850                                 gtk_clist_freeze(GTK_CLIST(ctree));
851
852                                 text[col_pos[F_COL_FOLDER]] = item->name;
853                                 child = gtk_ctree_insert_node
854                                         (ctree, node, NULL, text,
855                                          FOLDER_SPACING,
856                                          folderxpm, folderxpmmask,
857                                          folderopenxpm, folderopenxpmmask,
858                                          FALSE, FALSE);
859                                 gtk_ctree_node_set_row_data(ctree, child, item);
860                                 gtk_ctree_expand(ctree, node);
861                                 folderview_update_node(folderview, child);
862                                 folderview_sort_folders(folderview, node,
863                                                         item->folder);
864
865                                 gtk_clist_thaw(GTK_CLIST(ctree));
866                         }
867                 }
868         }
869 }
870
871 static void folderview_set_folders(FolderView *folderview)
872 {
873         GList *list;
874
875         list = folder_get_list();
876
877         for (; list != NULL; list = list->next)
878                 folderview_append_folder(folderview, FOLDER(list->data));
879 }
880
881 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
882                                       gpointer data)
883 {
884         GList *list;
885
886         for (list = folderview_list; list != NULL; list = list->next) {
887                 FolderView *folderview = (FolderView *)list->data;
888                 MainWindow *mainwin = folderview->mainwin;
889                 gchar *str;
890
891                 if (item->path)
892                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
893                                               item->folder->name, G_DIR_SEPARATOR,
894                                               item->path);
895                 else
896                         str = g_strdup_printf(_("Scanning folder %s ..."),
897                                               item->folder->name);
898
899                 STATUSBAR_PUSH(mainwin, str);
900                 STATUSBAR_POP(mainwin);
901                 g_free(str);
902         }
903 }
904
905 static GtkWidget *label_window_create(const gchar *str)
906 {
907         GtkWidget *window;
908         GtkWidget *label;
909
910         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
911         gtk_widget_set_size_request(window, 380, 60);
912         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
913         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
914         gtk_window_set_title(GTK_WINDOW(window), str);
915         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
916         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
917         manage_window_set_transient(GTK_WINDOW(window));
918
919         label = gtk_label_new(str);
920         gtk_container_add(GTK_CONTAINER(window), label);
921         gtk_widget_show(label);
922
923         gtk_widget_show_now(window);
924
925         return window;
926 }
927
928 void folderview_rescan_tree(Folder *folder)
929 {
930         GtkWidget *window;
931
932         g_return_if_fail(folder != NULL);
933
934         if (!folder->klass->scan_tree) return;
935
936         inc_lock();
937         window = label_window_create(_("Rebuilding folder tree..."));
938
939         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
940         folder_scan_tree(folder);
941         folder_set_ui_func(folder, NULL, NULL);
942
943         folderview_set_all();
944
945         gtk_widget_destroy(window);
946         inc_unlock();
947 }
948
949 /** folderview_check_new()
950  *  Scan and update the folder and return the 
951  *  count the number of new messages since last check. 
952  *  \param folder the folder to check for new messages
953  *  \return the number of new messages since last check
954  */
955 gint folderview_check_new(Folder *folder)
956 {
957         GList *list;
958         FolderItem *item;
959         FolderView *folderview;
960         GtkCTree *ctree;
961         GtkCTreeNode *node;
962         gint new_msgs = 0;
963         gint former_new_msgs = 0;
964         gint former_new = 0;
965
966         for (list = folderview_list; list != NULL; list = list->next) {
967                 folderview = (FolderView *)list->data;
968                 ctree = GTK_CTREE(folderview->ctree);
969
970                 inc_lock();
971                 main_window_lock(folderview->mainwin);
972                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
973
974                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
975                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
976                         item = gtk_ctree_node_get_row_data(ctree, node);
977                         if (!item || !item->path || !item->folder) continue;
978                         if (item->no_select) continue;
979                         if (folder && folder != item->folder) continue;
980                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
981                         if (!item->prefs->newmailcheck) continue;
982
983                         folderview_scan_tree_func(item->folder, item, NULL);
984                         former_new = item->new_msgs;
985
986                         if (folder_item_scan(item) < 0) {
987                                 summaryview_unlock(folderview->summaryview, item);
988                                 if (folder && !FOLDER_IS_LOCAL(folder))
989                                         break;
990                         }
991
992                         folderview_update_node(folderview, node);
993                         new_msgs += item->new_msgs;
994                         former_new_msgs += former_new;
995                 }
996
997                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
998                 main_window_unlock(folderview->mainwin);
999                 inc_unlock();
1000         }
1001
1002         folder_write_list();
1003         /* Number of new messages since last check is the just the difference 
1004          * between former_new_msgs and new_msgs. If new_msgs is less than
1005          * former_new_msgs, that would mean another session accessed the folder
1006          * and the result is not well defined.
1007          */
1008         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1009         return new_msgs;
1010 }
1011
1012 void folderview_check_new_all(void)
1013 {
1014         GList *list;
1015         GtkWidget *window;
1016         FolderView *folderview;
1017
1018         folderview = (FolderView *)folderview_list->data;
1019
1020         inc_lock();
1021         main_window_lock(folderview->mainwin);
1022         window = label_window_create
1023                 (_("Checking for new messages in all folders..."));
1024
1025         list = folder_get_list();
1026         for (; list != NULL; list = list->next) {
1027                 Folder *folder = list->data;
1028
1029                 folderview_check_new(folder);
1030         }
1031
1032         folder_write_list();
1033         folderview_set_all();
1034
1035         gtk_widget_destroy(window);
1036         main_window_unlock(folderview->mainwin);
1037         inc_unlock();
1038 }
1039
1040 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1041                                                  FolderItem *item,
1042                                                  gboolean in_sub)
1043 {
1044         GNode *node = NULL;
1045         
1046         if (!item || !item->folder || !item->folder->node)
1047                 return FALSE;
1048                 
1049         node = item->folder->node;
1050         
1051         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1052         node = node->children;
1053
1054         if (in_sub &&
1055             (item->new_msgs > 0 ||
1056             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1057                 return TRUE;
1058         }
1059
1060         while (node != NULL) {
1061                 if (node && node->data) {
1062                         FolderItem *next_item = (FolderItem*) node->data;
1063                         node = node->next;
1064                         if (folderview_have_new_children_sub(folderview, 
1065                                                              next_item, TRUE))
1066                                 return TRUE;
1067                 }
1068         }
1069
1070         return FALSE;
1071 }
1072
1073 static gboolean folderview_have_new_children(FolderView *folderview,
1074                                              FolderItem *item)
1075 {
1076         return folderview_have_new_children_sub(folderview, item, FALSE);
1077 }
1078
1079 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1080                                                     FolderItem *item, 
1081                                                     gboolean in_sub)
1082 {
1083         GNode *node = NULL;
1084         
1085         if (!item || !item->folder || !item->folder->node)
1086                 return FALSE;
1087         
1088         node = item->folder->node;
1089         
1090         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1091         node = node->children;
1092
1093         if (in_sub &&
1094             (item->unread_msgs > 0 ||
1095             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1096                 return TRUE;
1097         }
1098
1099         while (node != NULL) {
1100                 if (node && node->data) {
1101                         FolderItem *next_item = (FolderItem*) node->data;
1102                         node = node->next;
1103                         if (folderview_have_unread_children_sub(folderview, 
1104                                                                 next_item, 
1105                                                                 TRUE))
1106                                 return TRUE;
1107                 }
1108         }
1109
1110         return FALSE;
1111 }
1112
1113 static gboolean folderview_have_unread_children(FolderView *folderview,
1114                                                 FolderItem *item)
1115 {
1116         return folderview_have_unread_children_sub(folderview, item, FALSE);
1117 }
1118
1119 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1120                                                       FolderItem *item,
1121                                                       gboolean in_sub)
1122 {
1123         GNode *node = NULL;
1124
1125         if (!item || !item->folder || !item->folder->node)
1126                 return FALSE;
1127
1128         node = item->folder->node;
1129         
1130         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1131         node = node->children;
1132
1133         if (in_sub && item->search_match){
1134                 return TRUE;
1135         }
1136
1137         while (node != NULL) {
1138                 if (node && node->data) {
1139                         FolderItem *next_item = (FolderItem*) node->data;
1140                         node = node->next;
1141                         if (folderview_have_matching_children_sub(folderview, 
1142                                                                   next_item, 
1143                                                                   TRUE))
1144                                 return TRUE;
1145                 }
1146         }
1147
1148         return FALSE;
1149 }
1150
1151 static gboolean folderview_have_matching_children(FolderView *folderview,
1152                                                   FolderItem *item)
1153 {
1154         return folderview_have_matching_children_sub(folderview, item, FALSE);
1155 }
1156
1157 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1158 {
1159         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1160         GtkStyle *style = NULL;
1161         GtkStyle *color_style = NULL;
1162         FolderItem *item;
1163         GdkPixmap *xpm, *openxpm;
1164         GdkBitmap *mask, *openmask;
1165         static GdkPixmap *searchicon;
1166         static GdkBitmap *searchmask;
1167         gboolean mark = FALSE;
1168         gchar *name;
1169         gchar *str;
1170         gboolean add_unread_mark;
1171         gboolean add_sub_match_mark;
1172         gboolean use_bold, use_color;
1173         gint *col_pos = folderview->col_pos;
1174
1175         item = gtk_ctree_node_get_row_data(ctree, node);
1176         g_return_if_fail(item != NULL);
1177
1178         mark = (item->marked_msgs != 0);
1179
1180         switch (item->stype) {
1181         case F_INBOX:
1182                 if (item->hide_read_msgs) {
1183                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1184                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1185                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1186                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1187                 } else {
1188                         xpm = mark?m_inboxxpm:inboxxpm;
1189                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1190                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1191                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1192                 }
1193                 break;
1194         case F_OUTBOX:
1195                 if (item->hide_read_msgs) {
1196                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1197                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1198                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1199                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1200                 } else {
1201                         xpm = mark?m_outboxxpm:outboxxpm;
1202                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1203                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1204                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1205                 }
1206                 break;
1207         case F_QUEUE:
1208                 if (item->hide_read_msgs) {
1209                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1210                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1211                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1212                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1213                 } else {
1214                         xpm = mark?m_queuexpm:queuexpm;
1215                         mask = mark?m_queuexpmmask:queuexpmmask;
1216                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1217                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1218                 }
1219                 break;
1220         case F_TRASH:
1221                 if (item->hide_read_msgs) {
1222                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1223                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1224                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1225                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1226                 } else {
1227                         xpm = mark?m_trashxpm:trashxpm;
1228                         mask = mark?m_trashxpmmask:trashxpmmask;
1229                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1230                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1231                 }
1232                 break;
1233         case F_DRAFT:
1234                 xpm = mark?m_draftsxpm:draftsxpm;
1235                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1236                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1237                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1238                 break;
1239         default:
1240                 if (item->hide_read_msgs) {
1241                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1242                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1243                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1244                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1245                 } else {
1246                         xpm = mark?m_folderxpm:folderxpm;
1247                         mask = mark?m_folderxpmmask:folderxpmmask;
1248                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1249                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1250                 }
1251         }
1252         
1253         if (item->no_select) {
1254                 xpm = openxpm = noselectxpm;
1255                 mask = openmask = noselectxpmmask;
1256         }
1257
1258         name = folder_item_get_name(item);
1259
1260         if (!GTK_CTREE_ROW(node)->expanded) {
1261                 add_unread_mark = folderview_have_unread_children(
1262                                         folderview, item);
1263                 add_sub_match_mark = folderview_have_matching_children(
1264                                         folderview, item);
1265         } else {
1266                 add_unread_mark = FALSE;
1267                 add_sub_match_mark = FALSE;
1268         }
1269
1270         if (item->search_match) {
1271                 if (!searchicon) {
1272                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1273                          &searchicon, &searchmask);
1274                 }
1275                 xpm = openxpm = searchicon;
1276                 mask = openmask = searchmask;
1277         }
1278
1279         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1280             prefs_common.display_folder_unread) {
1281                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1282                                       add_unread_mark ? "+" : "");
1283                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1284                                         xpm, mask, openxpm, openmask,
1285                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1286                 g_free(str);
1287         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1288                     prefs_common.display_folder_unread) 
1289                    || add_sub_match_mark) {
1290
1291                 if (item->unread_msgs > 0)
1292                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1293                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1294                                               item->unreadmarked_msgs > 0 ? "!":"");
1295                 else
1296                         str = g_strdup_printf("%s (+)", name);
1297                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1298                                         xpm, mask, openxpm, openmask,
1299                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1300                 g_free(str);
1301         } else {
1302                 str = g_strdup_printf("%s%s", name, 
1303                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1304         
1305                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1306                                         xpm, mask, openxpm, openmask,
1307                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1308                 g_free(str);
1309         }
1310         g_free(name);
1311
1312         if (!folder_item_parent(item)) {
1313                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1314                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1315                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1316         } else {
1317                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1318                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1319                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1320         }
1321
1322         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1323             folder_has_parent_of_type(item, F_DRAFT) ||
1324             folder_has_parent_of_type(item, F_TRASH)) {
1325                 use_bold = use_color = FALSE;
1326         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1327                 /* highlight queue folder if there are any messages */
1328                 use_bold = use_color = (item->total_msgs > 0);
1329         } else {
1330                 /* if unread messages exist, print with bold font */
1331                 use_bold = (item->unread_msgs > 0) || add_unread_mark;
1332                 /* if new messages exist, print with colored letter */
1333                 use_color =
1334                         (item->new_msgs > 0) ||
1335                         (add_unread_mark &&
1336                          folderview_have_new_children(folderview, item));       
1337         }
1338
1339         gtk_ctree_node_set_foreground(ctree, node, NULL);
1340
1341         if (use_bold) {
1342                 if (item->prefs->color > 0 && !use_color) {
1343                         GdkColor gdk_color;
1344
1345                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1346                         color_style = gtk_style_copy(bold_style);
1347                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1348                         style = color_style;
1349                 } else if (use_color)
1350                         style = bold_color_style;
1351                 else
1352                         style = bold_style;
1353                 if (item->op_count > 0) {
1354                         style = bold_tgtfold_style;
1355                 }
1356         } else if (use_color) {
1357                 style = normal_color_style;
1358                 gtk_ctree_node_set_foreground(ctree, node,
1359                                               &folderview->color_new);
1360         } else if (item->op_count > 0) {
1361                 style = bold_tgtfold_style;
1362         } else if (item->prefs->color > 0) {
1363                 GdkColor gdk_color;
1364                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1365                 color_style = gtk_style_copy(normal_style);
1366                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1367                 style = color_style;
1368         } else {
1369                 style = normal_style;
1370         }
1371
1372         gtk_ctree_node_set_row_style(ctree, node, style);
1373
1374         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1375                 folderview_update_node(folderview, node);
1376 }
1377
1378 #if !CLAWS /* keep it here for syncs */
1379 void folderview_update_item(FolderItem *item, gboolean update_summary)
1380 {
1381         GList *list;
1382         FolderView *folderview;
1383         GtkCTree *ctree;
1384         GtkCTreeNode *node;
1385
1386         g_return_if_fail(item != NULL);
1387
1388         for (list = folderview_list; list != NULL; list = list->next) {
1389                 folderview = (FolderView *)list->data;
1390                 ctree = GTK_CTREE(folderview->ctree);
1391
1392                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1393                 if (node) {
1394                         folderview_update_node(folderview, node);
1395                         if (update_summary && folderview->opened == node)
1396                                 summary_show(folderview->summaryview,
1397                                              item, FALSE);
1398                 }
1399         }
1400 }
1401 #endif
1402
1403 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1404 {
1405         GList *list;
1406         FolderView *folderview;
1407         GtkCTree *ctree;
1408         GtkCTreeNode *node;
1409
1410         g_return_if_fail(item != NULL);
1411
1412         for (list = folderview_list; list != NULL; list = list->next) {
1413                 folderview = (FolderView *)list->data;
1414                 ctree = GTK_CTREE(folderview->ctree);
1415
1416                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1417                 if (node) {
1418                         item->search_match = matches;
1419                         folderview_update_node(folderview, node);
1420                 }
1421         }
1422 }
1423
1424 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1425 {
1426         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1427         FolderView *folderview = (FolderView *)data;
1428         GtkCTree *ctree;
1429         GtkCTreeNode *node;
1430         g_return_val_if_fail(update_info != NULL, TRUE);
1431         g_return_val_if_fail(update_info->item != NULL, TRUE);
1432         g_return_val_if_fail(folderview != NULL, FALSE);
1433
1434         ctree = GTK_CTREE(folderview->ctree);
1435
1436         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1437         if (node) {
1438                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1439                         folderview_update_node(folderview, node);
1440                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1441                         summary_show(folderview->summaryview, update_info->item);
1442         }
1443         
1444         return FALSE;
1445 }
1446
1447 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1448                                                 gpointer data)
1449 {
1450         /* CLAWS: share this joy with other hook functions ... */
1451         folder_item_update((FolderItem *)key, 
1452                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1453 }
1454
1455 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1456 {
1457         GList *list;
1458         FolderItemUpdateFlags flags;
1459         
1460         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1461                 : 0;
1462         for (list = folderview_list; list != NULL; list = list->next)
1463                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1464                                      GINT_TO_POINTER(flags));
1465 }
1466
1467 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1468                                       GNode *gnode, GtkCTreeNode *cnode,
1469                                       gpointer data)
1470 {
1471         FolderView *folderview = (FolderView *)data;
1472         FolderItem *item = FOLDER_ITEM(gnode->data);
1473
1474         g_return_val_if_fail(item != NULL, FALSE);
1475
1476         gtk_ctree_node_set_row_data(ctree, cnode, item);
1477         folderview_update_node(folderview, cnode);
1478
1479         return TRUE;
1480 }
1481
1482 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1483                                    gpointer data)
1484 {
1485         FolderView *folderview = (FolderView *)data;
1486         FolderItem *item;
1487
1488         if (GTK_CTREE_ROW(node)->children) {
1489                 item = gtk_ctree_node_get_row_data(ctree, node);
1490                 g_return_if_fail(item != NULL);
1491
1492                 if (!item->collapsed)
1493                         gtk_ctree_expand(ctree, node);
1494                 else
1495                         folderview_update_node(folderview, node);
1496         }
1497 }
1498
1499 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1500                                GtkCTreeNode *root, GtkCTreeNode **prev)
1501 {
1502         if (item) {
1503                 GtkCTreeNode *node, *parent, *sibling;
1504
1505                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1506                 if (!node)
1507                         g_warning("%s not found.\n", item->path);
1508                 else {
1509                         parent = GTK_CTREE_ROW(node)->parent;
1510                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1511                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1512                         else
1513                                 sibling = GTK_CTREE_ROW(parent)->children;
1514                         while (sibling) {
1515                                 FolderItem *tmp;
1516
1517                                 tmp = gtk_ctree_node_get_row_data
1518                                         (ctree, sibling);
1519                                 if (tmp->stype != F_NORMAL)
1520                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1521                                 else
1522                                         break;
1523                         }
1524                         if (node != sibling)
1525                                 gtk_ctree_move(ctree, node, parent, sibling);
1526                 }
1527
1528                 *prev = node;
1529         }
1530 }
1531
1532 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1533                                     Folder *folder)
1534 {
1535         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1536         GtkCTreeNode *prev = NULL;
1537
1538         gtk_sctree_sort_recursive(ctree, root);
1539
1540         if (root && GTK_CTREE_ROW(root)->parent) return;
1541
1542         set_special_folder(ctree, folder->inbox, root, &prev);
1543         set_special_folder(ctree, folder->outbox, root, &prev);
1544         set_special_folder(ctree, folder->draft, root, &prev);
1545         set_special_folder(ctree, folder->queue, root, &prev);
1546         set_special_folder(ctree, folder->trash, root, &prev);
1547 }
1548
1549 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1550 {
1551         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1552         GtkCTreeNode *root;
1553
1554         g_return_if_fail(folder != NULL);
1555
1556         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1557                                       folderview_gnode_func, folderview);
1558         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1559                                 folderview);
1560         folderview_sort_folders(folderview, root, folder);
1561 }
1562
1563 /* callback functions */
1564
1565 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1566                                           FolderView *folderview)
1567 {
1568         GtkCList *clist = GTK_CLIST(ctree);
1569         gint prev_row = -1, row = -1, column = -1;
1570         FolderItem *item;
1571         Folder *folder;
1572         FolderViewPopup *fpopup;
1573         GtkItemFactory *fpopup_factory;
1574         GtkWidget *popup;
1575
1576         if (!event) return FALSE;
1577
1578         if (event->button == 1) {
1579                 folderview->open_folder = TRUE;
1580
1581                 if (event->type == GDK_2BUTTON_PRESS) {
1582                         if (clist->selection) {
1583                                 GtkCTreeNode *node;
1584
1585                                 node = GTK_CTREE_NODE(clist->selection->data);
1586                                 if (node)
1587                                         gtk_ctree_toggle_expansion(
1588                                                 GTK_CTREE(ctree),
1589                                                 node);
1590                         }
1591                 }
1592                 return FALSE;
1593         }
1594
1595         if (event->button == 2 || event->button == 3) {
1596                 /* right clicked */
1597                 if (clist->selection) {
1598                         GtkCTreeNode *node;
1599
1600                         node = GTK_CTREE_NODE(clist->selection->data);
1601                         if (node)
1602                                 prev_row = gtkut_ctree_get_nth_from_node
1603                                         (GTK_CTREE(ctree), node);
1604                 }
1605
1606                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1607                                                   &row, &column))
1608                         return FALSE;
1609                 if (prev_row != row) {
1610                         gtk_clist_unselect_all(clist);
1611                         if (event->button == 2)
1612                                 folderview_select_node
1613                                         (folderview,
1614                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1615                                                             row));
1616                         else
1617                                 gtk_clist_select_row(clist, row, column);
1618                 }
1619         }
1620
1621         if (event->button != 3) return FALSE;
1622
1623         item = gtk_clist_get_row_data(clist, row);
1624         g_return_val_if_fail(item != NULL, FALSE);
1625         g_return_val_if_fail(item->folder != NULL, FALSE);
1626         folder = item->folder;
1627
1628         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1629         if (fpopup != NULL)
1630                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1631         else {
1632                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1633                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1634         }
1635
1636         if (fpopup->set_sensitivity != NULL)
1637                 fpopup->set_sensitivity(fpopup_factory, item);
1638
1639         if (item == folder->trash &&
1640             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1641                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1642                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1643         } else if (item != folder->trash) {
1644                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1645                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1646         }
1647         
1648 #define SET_SENS(name, sens) \
1649         menu_set_sensitive(fpopup_factory, name, sens)
1650
1651         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1652         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1653                  folderview->selected == folderview->opened);
1654         SET_SENS("/Properties...", item->node->parent != NULL);
1655         SET_SENS("/Processing...", item->node->parent != NULL);
1656         if (item == folder->trash) {
1657                 GSList *msglist = folder_item_get_msg_list(item);
1658                 SET_SENS("/Empty trash...", msglist != NULL);
1659                 procmsg_msg_list_free(msglist);
1660         }
1661 #undef SET_SENS
1662
1663         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1664         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1665                        event->button, event->time);
1666
1667         return FALSE;
1668 }
1669
1670 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1671                                            FolderView *folderview)
1672 {
1673         if (!event) return FALSE;
1674
1675         if (event->button == 1 && folderview->open_folder == FALSE &&
1676             folderview->opened != NULL) {
1677                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1678                                           folderview->opened);
1679                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1680         }
1681
1682         return FALSE;
1683 }
1684
1685 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1686                                        FolderView *folderview)
1687 {
1688         if (!event) return FALSE;
1689
1690         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1691                 return FALSE;
1692
1693         switch (event->keyval) {
1694         case GDK_Return:
1695                 if (folderview->selected) {
1696                         folderview_select_node(folderview,
1697                                                folderview->selected);
1698                 }
1699                 break;
1700         case GDK_space:
1701                 if (folderview->selected) {
1702                         if (folderview->opened == folderview->selected &&
1703                             (!folderview->summaryview->folder_item ||
1704                              folderview->summaryview->folder_item->total_msgs == 0))
1705                                 folderview_select_next_unread(folderview);
1706                         else
1707                                 folderview_select_node(folderview,
1708                                                        folderview->selected);
1709                 }
1710                 break;
1711         default:
1712                 break;
1713         }
1714
1715         return FALSE;
1716 }
1717
1718 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1719                                 gint column, FolderView *folderview)
1720 {
1721         static gboolean can_select = TRUE;      /* exclusive lock */
1722         gboolean opened;
1723         FolderItem *item;
1724         gchar *buf;
1725
1726         folderview->selected = row;
1727
1728         if (folderview->opened == row) {
1729                 folderview->open_folder = FALSE;
1730                 return;
1731         }
1732         
1733         if (!can_select || summary_is_locked(folderview->summaryview)) {
1734                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1735                 gtk_ctree_select(ctree, folderview->opened);
1736                 return;
1737         }
1738
1739         if (!folderview->open_folder) return;
1740
1741         item = gtk_ctree_node_get_row_data(ctree, row);
1742         if (!item || item->no_select) return;
1743
1744         can_select = FALSE;
1745
1746         /* Save cache for old folder */
1747         /* We don't want to lose all caches if sylpheed crashed */
1748         if (folderview->opened) {
1749                 FolderItem *olditem;
1750                 
1751                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1752                 if (olditem) {
1753                         /* will be null if we just moved the previously opened folder */
1754                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1755                         folder_item_close(olditem);
1756                 }
1757         }
1758
1759         /* CLAWS: set compose button type: news folder items 
1760          * always have a news folder as parent */
1761         if (item->folder) 
1762                 toolbar_set_compose_button
1763                         (folderview->mainwin->toolbar,
1764                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1765                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1766
1767         if (item->path)
1768                 debug_print("Folder %s is selected\n", item->path);
1769
1770         if (!GTK_CTREE_ROW(row)->children)
1771                 gtk_ctree_expand(ctree, row);
1772         if (folderview->opened &&
1773             !GTK_CTREE_ROW(folderview->opened)->children)
1774                 gtk_ctree_collapse(ctree, folderview->opened);
1775
1776         /* ungrab the mouse event */
1777         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1778                 gtk_grab_remove(GTK_WIDGET(ctree));
1779                 if (gdk_pointer_is_grabbed())
1780                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1781         }
1782
1783         /* Open Folder */
1784         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1785                                         item->path : "(null)");
1786         debug_print("%s\n", buf);
1787         STATUSBAR_PUSH(folderview->mainwin, buf);
1788         g_free(buf);
1789
1790         main_window_cursor_wait(folderview->mainwin);
1791
1792         if (folder_item_open(item) != 0) {
1793                 main_window_cursor_normal(folderview->mainwin);
1794                 STATUSBAR_POP(folderview->mainwin);
1795
1796                 alertpanel_error(_("Folder could not be opened."));
1797
1798                 folderview->open_folder = FALSE;
1799                 can_select = TRUE;
1800
1801                 return;
1802         }
1803
1804         main_window_cursor_normal(folderview->mainwin);
1805
1806         /* Show messages */
1807         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1808         opened = summary_show(folderview->summaryview, item);
1809         
1810         folder_clean_cache_memory();
1811
1812         if (!opened) {
1813                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1814                 gtk_ctree_select(ctree, folderview->opened);
1815         } else {
1816                 folderview->opened = row;
1817                 if (gtk_ctree_node_is_visible(ctree, row)
1818                     != GTK_VISIBILITY_FULL)
1819                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1820         }
1821
1822         STATUSBAR_POP(folderview->mainwin);
1823
1824         folderview->open_folder = FALSE;
1825         can_select = TRUE;
1826 }
1827
1828 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1829                                      FolderView *folderview)
1830 {
1831         FolderItem *item;
1832
1833         item = gtk_ctree_node_get_row_data(ctree, node);
1834         g_return_if_fail(item != NULL);
1835         item->collapsed = FALSE;
1836         folderview_update_node(folderview, node);
1837 }
1838
1839 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1840                                       FolderView *folderview)
1841 {
1842         FolderItem *item;
1843
1844         item = gtk_ctree_node_get_row_data(ctree, node);
1845         g_return_if_fail(item != NULL);
1846         item->collapsed = TRUE;
1847         folderview_update_node(folderview, node);
1848 }
1849
1850 static void folderview_popup_close(GtkMenuShell *menu_shell,
1851                                    FolderView *folderview)
1852 {
1853         if (!folderview->opened) return;
1854
1855         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1856                                   folderview->opened);
1857         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1858 }
1859
1860 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1861                                    FolderView *folderview)
1862 {
1863         FolderColumnType type = folderview->col_state[column].type;
1864
1865         prefs_common.folder_col_size[type] = width;
1866 }
1867
1868 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1869 {
1870         GNode *srcnode;
1871
1872         folderview_create_folder_node(folderview, item);
1873
1874         if (!item || !item->folder || !item->folder->node)
1875                 return;
1876
1877         srcnode = item->folder->node;   
1878         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1879         srcnode = srcnode->children;
1880         while (srcnode != NULL) {
1881                 if (srcnode && srcnode->data) {
1882                         FolderItem *next_item = (FolderItem*) srcnode->data;
1883                         folderview_create_folder_node_recursive(folderview, next_item);
1884                 }
1885                 srcnode = srcnode->next;
1886         }
1887 }
1888
1889 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1890 {
1891         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1892         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1893         GtkCTreeNode *node, *parent_node;
1894         gint *col_pos = folderview->col_pos;
1895
1896         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
1897         if (parent_node == NULL)
1898                 return;
1899
1900         gtk_clist_freeze(GTK_CLIST(ctree));
1901
1902         text[col_pos[F_COL_FOLDER]] = item->name;
1903         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1904                                      FOLDER_SPACING,
1905                                      folderxpm, folderxpmmask,
1906                                      folderopenxpm, folderopenxpmmask,
1907                                      FALSE, FALSE);
1908         gtk_ctree_expand(ctree, parent_node);
1909         gtk_ctree_node_set_row_data(ctree, node, item);
1910         if (normal_style)
1911                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1912         folderview_sort_folders(folderview, folderview->selected, item->folder);
1913
1914         gtk_clist_thaw(GTK_CLIST(ctree));
1915 }
1916
1917 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
1918                                       GtkWidget *widget)
1919 {
1920         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1921         FolderItem *item;
1922         GSList *mlist = NULL;
1923         GSList *cur = NULL;
1924         if (!folderview->selected) return;
1925         
1926         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1927         g_return_if_fail(item != NULL);
1928         g_return_if_fail(item->folder != NULL);
1929         if (item != item->folder->trash) return;
1930         
1931         if (prefs_common.ask_on_clean) {
1932                 if (alertpanel(_("Empty trash"),
1933                                _("Empty all messages in trash?"),
1934                                GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
1935                         return;
1936         }
1937         
1938         mlist = folder_item_get_msg_list(item);
1939         
1940         for (cur = mlist ; cur != NULL ; cur = cur->next) {
1941                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1942                 if (MSG_IS_LOCKED(msginfo->flags))
1943                         continue;
1944                 /* is it partially received? (partial_recv isn't cached) */
1945                 if (msginfo->total_size != 0 && 
1946                     msginfo->size != (off_t)msginfo->total_size)
1947                         partial_mark_for_delete(msginfo);
1948         }
1949         procmsg_msg_list_free(mlist);
1950
1951         folder_item_remove_all_msg(item);
1952 }
1953
1954 static void folderview_search_cb(FolderView *folderview, guint action,
1955                                  GtkWidget *widget)
1956 {
1957         summary_search(folderview->summaryview);
1958 }
1959
1960 static void folderview_property_cb(FolderView *folderview, guint action,
1961                                    GtkWidget *widget)
1962 {
1963         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1964         FolderItem *item;
1965
1966         if (!folderview->selected) return;
1967
1968         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1969         g_return_if_fail(item != NULL);
1970         g_return_if_fail(item->folder != NULL);
1971
1972         if (folder_item_parent(item) == NULL)
1973                 return;
1974
1975         prefs_folder_item_open(item);
1976 }
1977
1978 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
1979 {
1980         GSList *list = NULL;
1981         GSList *done = NULL;
1982         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1983         
1984         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
1985                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
1986                 &&  list->data != node) {
1987                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
1988                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
1989                 }
1990         }
1991         for (list = done; list != NULL; list = g_slist_next(list)) {
1992                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
1993                                                                  list->data);
1994         }
1995         g_slist_free(done);
1996 }
1997
1998 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
1999                             FolderItem *to_folder)
2000 {
2001         FolderItem *from_parent = NULL;
2002         FolderItem *new_folder = NULL;
2003         GtkCTreeNode *src_node = NULL;
2004         gchar *buf;
2005         gint status;
2006
2007         g_return_if_fail(folderview != NULL);
2008         g_return_if_fail(from_folder != NULL);
2009         g_return_if_fail(to_folder != NULL);
2010
2011         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2012         from_parent = folder_item_parent(from_folder);
2013         
2014         if (prefs_common.warn_dnd) {
2015                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2016                                         "sub-folder of '%s' ?"), from_folder->name,
2017                                         to_folder->name);
2018                 status = alertpanel_full(_("Move folder"), buf,
2019                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL, TRUE,
2020                                          NULL, ALERT_QUESTION, G_ALERTALTERNATE);
2021                 g_free(buf);
2022
2023                 if (status != G_ALERTDEFAULT
2024                  && status != (G_ALERTDEFAULT | G_ALERTDISABLE))
2025                         return;
2026                 if (status & G_ALERTDISABLE)
2027                         prefs_common.warn_dnd = FALSE;
2028         }
2029
2030         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2031         STATUSBAR_PUSH(folderview->mainwin, buf);
2032         g_free(buf);
2033         summary_clear_all(folderview->summaryview);
2034         folderview->opened = NULL;
2035         folderview->selected = NULL;
2036         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2037         inc_lock();
2038         main_window_cursor_wait(folderview->mainwin);
2039         statusbar_verbosity_set(TRUE);
2040         folder_item_update_freeze();
2041         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2042                 statusbar_verbosity_set(FALSE);
2043                 main_window_cursor_normal(folderview->mainwin);
2044                 STATUSBAR_POP(folderview->mainwin);
2045                 folder_item_update_thaw();
2046                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2047
2048                 folderview_sort_folders(folderview, 
2049                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2050                                 NULL, folder_item_parent(new_folder)), new_folder->folder);
2051                 folderview_select(folderview, new_folder);
2052         } else {
2053                 statusbar_verbosity_set(FALSE);         
2054                 main_window_cursor_normal(folderview->mainwin);
2055                 STATUSBAR_POP(folderview->mainwin);
2056                 folder_item_update_thaw();
2057                 switch (status) {
2058                 case F_MOVE_FAILED_DEST_IS_PARENT:
2059                         alertpanel_error(_("Source and destination are the same."));
2060                         break;
2061                 case F_MOVE_FAILED_DEST_IS_CHILD:
2062                         alertpanel_error(_("Can't move a folder to one of its children."));
2063                         break;
2064                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2065                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2066                         break;
2067                 default:
2068                         alertpanel_error(_("Move failed!"));
2069                         break;
2070                 }
2071         }       
2072         inc_unlock();           
2073         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2074 }
2075
2076 static gint folderview_clist_compare(GtkCList *clist,
2077                                      gconstpointer ptr1, gconstpointer ptr2)
2078 {
2079         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2080         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2081
2082         if (!item1->name)
2083                 return (item2->name != NULL);
2084         if (!item2->name)
2085                 return -1;
2086
2087         return g_utf8_collate(item1->name, item2->name);
2088 }
2089
2090 static void folderview_processing_cb(FolderView *folderview, guint action,
2091                                      GtkWidget *widget)
2092 {
2093         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2094         FolderItem *item;
2095
2096         if (!folderview->selected) return;
2097
2098         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2099         g_return_if_fail(item != NULL);
2100         g_return_if_fail(item->folder != NULL);
2101
2102         prefs_filtering_open(&item->prefs->processing,
2103                              _("Processing configuration"), NULL, NULL);
2104 }
2105
2106 void folderview_set_target_folder_color(gint color_op) 
2107 {
2108         gint firstone = 1;
2109         GList *list;
2110         FolderView *folderview;
2111
2112         for (list = folderview_list; list != NULL; list = list->next) {
2113                 folderview = (FolderView *)list->data;
2114                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2115                 if (firstone) {
2116                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2117                                 folderview->color_op;
2118                         firstone = 0;
2119                 }
2120         }
2121 }
2122
2123 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2124 {
2125 }
2126
2127 void folderview_reflect_prefs(void)
2128 {
2129         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2130         normal_style = normal_color_style = bold_style = 
2131                 bold_color_style = bold_tgtfold_style = NULL;
2132         FolderItem *item = folderview_get_selected_item(folderview);
2133         folderview_init(folderview);
2134         folderview_column_set_titles(folderview);
2135         folderview_set_all();
2136         if (item)
2137                 folderview_select(folderview, item);
2138 }
2139
2140 static void drag_state_stop(FolderView *folderview)
2141 {
2142         if (folderview->drag_timer)
2143                 gtk_timeout_remove(folderview->drag_timer);
2144         folderview->drag_timer = 0;
2145         folderview->drag_node = NULL;
2146 }
2147
2148 static gint folderview_defer_expand(FolderView *folderview)
2149 {
2150         if (folderview->drag_node) {
2151                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2152                 if (folderview->drag_item->collapsed) {
2153                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2154                         folderview->nodes_to_recollapse = g_slist_append
2155                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2156                 }
2157         }
2158         folderview->drag_item  = NULL;
2159         folderview->drag_timer = 0;
2160         return FALSE;
2161 }
2162
2163 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2164 {
2165         /* the idea is that we call drag_state_start() whenever we want expansion to
2166          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2167          * we need to call drag_state_stop() */
2168         drag_state_stop(folderview);
2169         /* request expansion */
2170         if (0 != (folderview->drag_timer = gtk_timeout_add
2171                         (prefs_common.hover_timeout, 
2172                          (GtkFunction)folderview_defer_expand,
2173                          folderview))) {
2174                 folderview->drag_node = node;
2175                 folderview->drag_item = item;
2176         }                        
2177 }
2178
2179 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2180                                   FolderView       *folderview)
2181 {
2182         GdkDragContext *context;
2183
2184         g_return_if_fail(folderview != NULL);
2185         if (folderview->selected == NULL) return;
2186         if (folderview->nodes_to_recollapse) 
2187                 g_slist_free(folderview->nodes_to_recollapse);
2188         folderview->nodes_to_recollapse = NULL;
2189         context = gtk_drag_begin(widget, folderview->target_list,
2190                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2191         gtk_drag_set_icon_default(context);
2192 }
2193
2194 static void folderview_drag_data_get(GtkWidget        *widget,
2195                                      GdkDragContext   *drag_context,
2196                                      GtkSelectionData *selection_data,
2197                                      guint             info,
2198                                      guint             time,
2199                                      FolderView       *folderview)
2200 {
2201         FolderItem *item;
2202         GList *cur;
2203         gchar *source = NULL;
2204
2205         for (cur = GTK_CLIST(folderview->ctree)->selection;
2206              cur != NULL; cur = cur->next) {
2207                 item = gtk_ctree_node_get_row_data
2208                         (GTK_CTREE(folderview->ctree), 
2209                          GTK_CTREE_NODE(cur->data));
2210                 if (item) {
2211                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2212                         gtk_selection_data_set(selection_data,
2213                                                selection_data->target, 8,
2214                                                source, strlen(source));
2215                         break;
2216                 } else
2217                         return;
2218         }
2219 }
2220
2221 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2222 {
2223         FolderUpdateData *hookdata;
2224         FolderView *folderview;
2225         GtkWidget *ctree;
2226
2227         hookdata = source;
2228         folderview = (FolderView *) userdata;   
2229         g_return_val_if_fail(hookdata != NULL, FALSE);
2230         g_return_val_if_fail(folderview != NULL, FALSE);
2231
2232         ctree = folderview->ctree;
2233         g_return_val_if_fail(ctree != NULL, FALSE);
2234
2235         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2236                 folderview_create_folder_node(folderview, hookdata->item);
2237         else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2238                 GtkCTreeNode *node;
2239
2240                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2241                 if (node != NULL)
2242                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2243         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2244                 folderview_set(folderview);
2245
2246         return FALSE;
2247 }
2248
2249 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2250                                           GdkDragContext *context,
2251                                           gint            x,
2252                                           gint            y,
2253                                           guint           time,
2254                                           FolderView     *folderview)
2255 {
2256         gint row, column;
2257         FolderItem *item = NULL, *src_item = NULL;
2258         GtkCTreeNode *node = NULL;
2259         gboolean acceptable = FALSE;
2260         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2261                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2262         int height = (int)pos->page_size;
2263         int total_height = (int)pos->upper;
2264         int vpos = (int) pos->value;
2265
2266         if (gtk_clist_get_selection_info
2267                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2268                 GtkWidget *srcwidget;
2269
2270                 if (y > height - 24 && height + vpos < total_height)
2271                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2272
2273                 if (y < 48 && y > 0)
2274                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2275
2276                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2277                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2278                 src_item = folderview->summaryview->folder_item;
2279
2280                 srcwidget = gtk_drag_get_source_widget(context);
2281                 if (srcwidget == folderview->summaryview->ctree) {
2282                         /* comes from summaryview */
2283                         /* we are copying messages, so only accept folder items that are not
2284                            the source item, are no root items and can copy messages */
2285                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2286                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2287                                 acceptable = TRUE;
2288                 } else if (srcwidget == folderview->ctree) {
2289                         /* comes from folderview */
2290                         /* we are moving folder items, only accept folders that are not
2291                            the source items and can copy messages and create folder items */
2292                         if (item && item->folder && src_item && src_item != item &&
2293                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2294                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2295                                 acceptable = TRUE;
2296                 }
2297         }
2298
2299         if (acceptable || (src_item && src_item == item))
2300                 drag_state_start(folderview, node, item);
2301         
2302         if (acceptable) {
2303                 g_signal_handlers_block_by_func
2304                         (G_OBJECT(widget),
2305                          G_CALLBACK(folderview_selected), folderview);
2306                 gtk_ctree_select(GTK_CTREE(widget), node);
2307                 g_signal_handlers_unblock_by_func
2308                         (G_OBJECT(widget),
2309                          G_CALLBACK(folderview_selected), folderview);
2310                 gdk_drag_status(context, 
2311                                         (context->actions == GDK_ACTION_COPY ?
2312                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2313         } else {
2314                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2315                 gdk_drag_status(context, 0, time);
2316         }
2317
2318         return acceptable;
2319 }
2320
2321 static void folderview_drag_leave_cb(GtkWidget      *widget,
2322                                      GdkDragContext *context,
2323                                      guint           time,
2324                                      FolderView     *folderview)
2325 {
2326         drag_state_stop(folderview);
2327         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2328 }
2329
2330 static void folderview_drag_received_cb(GtkWidget        *widget,
2331                                         GdkDragContext   *drag_context,
2332                                         gint              x,
2333                                         gint              y,
2334                                         GtkSelectionData *data,
2335                                         guint             info,
2336                                         guint             time,
2337                                         FolderView       *folderview)
2338 {
2339         gint row, column;
2340         FolderItem *item, *src_item;
2341         GtkCTreeNode *node;
2342
2343         drag_state_stop(folderview);
2344         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2345                 /* comes from summaryview */
2346                 if (gtk_clist_get_selection_info
2347                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2348                         return;
2349
2350                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2351                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2352                 src_item = folderview->summaryview->folder_item;
2353                 
2354                 /* re-check (due to acceptable possibly set for folder moves */
2355                 if (!(item && item->folder && item->path && !item->no_select && 
2356                       src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2357                         return;
2358                 }
2359                 if (item && src_item) {
2360                         switch (drag_context->action) {
2361                         case GDK_ACTION_COPY:
2362                                 summary_copy_selected_to(folderview->summaryview, item);
2363                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2364                                 break;
2365                         case GDK_ACTION_MOVE:
2366                         case GDK_ACTION_DEFAULT:
2367                         default:
2368                                 if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2369                                         summary_copy_selected_to(folderview->summaryview, item);
2370                                 else
2371                                         summary_move_selected_to(folderview->summaryview, item);
2372                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2373                         }
2374                 } else
2375                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2376         } else {
2377                 /* comes from folderview */
2378                 char *source;
2379                 gboolean folder_is_normal = TRUE;
2380                 
2381                 source = data->data + 17;
2382                 if (gtk_clist_get_selection_info
2383                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2384                     || *source == 0) {
2385                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2386                         return;
2387                 }
2388                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2389                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2390                 src_item = folder_find_item_from_identifier(source);
2391
2392                 folder_is_normal = 
2393                         src_item != NULL &&
2394                         src_item->stype == F_NORMAL &&
2395                         !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2396                         !folder_has_parent_of_type(src_item, F_DRAFT) &&
2397                         !folder_has_parent_of_type(src_item, F_QUEUE) &&
2398                         !folder_has_parent_of_type(src_item, F_TRASH);
2399                 if (!item || item->no_select || !src_item 
2400                 ||  !folder_is_normal) {
2401                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2402                         return;
2403                 }
2404
2405                 folderview_move_folder(folderview, src_item, item);
2406                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2407         }
2408         folderview->nodes_to_recollapse = NULL;
2409 }
2410
2411 static void folderview_drag_end_cb(GtkWidget        *widget, 
2412                                    GdkDragContext   *drag_context,
2413                                    FolderView       *folderview)
2414 {
2415         drag_state_stop(folderview);
2416         g_slist_free(folderview->nodes_to_recollapse);
2417         folderview->nodes_to_recollapse = NULL;
2418 }
2419
2420 void folderview_register_popup(FolderViewPopup *fpopup)
2421 {
2422         GList *folderviews;
2423
2424         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2425                 FolderView *folderview = folderviews->data;
2426                 GtkItemFactory *factory;
2427
2428                 factory = create_ifactory(folderview, fpopup);
2429                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2430         }       
2431         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2432 }
2433
2434 void folderview_unregister_popup(FolderViewPopup *fpopup)
2435 {
2436         GList *folderviews;
2437
2438         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2439                 FolderView *folderview = folderviews->data;
2440
2441                 g_hash_table_remove(folderview->popups, fpopup->klass);
2442         }       
2443         g_hash_table_remove(folderview_popups, fpopup->klass);
2444 }