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