d04a4be921a686334c3efb72fb56ac141ff808fa
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <gdk/gdkkeysyms.h>
24 #include <gtk/gtkwidget.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkctree.h>
27 #include <gtk/gtkcontainer.h>
28 #include <gtk/gtkclist.h>
29 #include <gtk/gtkstyle.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkstatusbar.h>
33 #include <gtk/gtkmenu.h>
34 #include <gtk/gtkmenuitem.h>
35 #include <gtk/gtkitemfactory.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "intl.h"
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 "grouplistdialog.h"
48 #include "manage_window.h"
49 #include "alertpanel.h"
50 #include "menu.h"
51 #include "stock_pixmap.h"
52 #include "procmsg.h"
53 #include "utils.h"
54 #include "gtkutils.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "prefs_filter.h"
58 #include "account.h"
59 #include "folder.h"
60 #include "inc.h"
61
62 typedef enum
63 {
64         COL_FOLDER      = 0,
65         COL_NEW         = 1,
66         COL_UNREAD      = 2,
67         COL_TOTAL       = 3
68 } FolderColumnPos;
69
70 #define N_FOLDER_COLS           4
71 #define COL_FOLDER_WIDTH        150
72 #define COL_NUM_WIDTH           32
73
74 #define STATUSBAR_PUSH(mainwin, str) \
75 { \
76         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
77                            mainwin->folderview_cid, str); \
78         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
79 }
80
81 #define STATUSBAR_POP(mainwin) \
82 { \
83         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
84                           mainwin->folderview_cid); \
85 }
86
87 static GList *folderview_list = NULL;
88
89 static GdkFont *normalfont;
90 static GdkFont *boldfont;
91
92 static GtkStyle *normal_style;
93 static GtkStyle *normal_color_style;
94 static GtkStyle *bold_style;
95 static GtkStyle *bold_color_style;
96 static GtkStyle *bold_tgtfold_style;
97
98 static GdkBitmap *inboxxpm;
99 static GdkBitmap *inboxxpmmask;
100 static GdkPixmap *inboxhrmxpm;
101 static GdkBitmap *inboxhrmxpmmask;
102 static GdkPixmap *inboxopenxpm;
103 static GdkBitmap *inboxopenxpmmask;
104 static GdkPixmap *inboxopenhrmxpm;
105 static GdkBitmap *inboxopenhrmxpmmask;
106 static GdkPixmap *outboxxpm;
107 static GdkBitmap *outboxxpmmask;
108 static GdkPixmap *outboxhrmxpm;
109 static GdkBitmap *outboxhrmxpmmask;
110 static GdkPixmap *outboxopenxpm;
111 static GdkBitmap *outboxopenxpmmask;
112 static GdkPixmap *outboxopenhrmxpm;
113 static GdkBitmap *outboxopenhrmxpmmask;
114 static GdkPixmap *folderxpm;
115 static GdkBitmap *folderxpmmask;
116 static GdkPixmap *folderhrmxpm;
117 static GdkBitmap *folderhrmxpmmask;
118 static GdkPixmap *folderopenxpm;
119 static GdkBitmap *folderopenxpmmask;
120 static GdkPixmap *folderopenhrmxpm;
121 static GdkBitmap *folderopenhrmxpmmask;
122 static GdkPixmap *trashopenxpm;
123 static GdkBitmap *trashopenxpmmask;
124 static GdkPixmap *trashopenhrmxpm;
125 static GdkBitmap *trashopenhrmxpmmask;
126 static GdkPixmap *trashxpm;
127 static GdkBitmap *trashxpmmask;
128 static GdkPixmap *trashhrmxpm;
129 static GdkBitmap *trashhrmxpmmask;
130 static GdkPixmap *queuexpm;
131 static GdkBitmap *queuexpmmask;
132 static GdkPixmap *queuehrmxpm;
133 static GdkBitmap *queuehrmxpmmask;
134 static GdkPixmap *queueopenxpm;
135 static GdkBitmap *queueopenxpmmask;
136 static GdkPixmap *queueopenhrmxpm;
137 static GdkBitmap *queueopenhrmxpmmask;
138 static GdkPixmap *newxpm;
139 static GdkBitmap *newxpmmask;
140 static GdkPixmap *unreadxpm;
141 static GdkBitmap *unreadxpmmask;
142
143 static void folderview_select_node       (FolderView    *folderview,
144                                           GtkCTreeNode  *node);
145 static void folderview_set_folders       (FolderView    *folderview);
146 static void folderview_sort_folders      (FolderView    *folderview,
147                                           GtkCTreeNode  *root,
148                                           Folder        *folder);
149 static void folderview_append_folder     (FolderView    *folderview,
150                                           Folder        *folder);
151 static void folderview_update_node       (FolderView    *folderview,
152                                           GtkCTreeNode  *node);
153
154 static GtkCTreeNode *folderview_find_by_name    (GtkCTree       *ctree,
155                                                  GtkCTreeNode   *node,
156                                                  const gchar    *name);
157
158 static gint folderview_compare_name     (gconstpointer   a,
159                                          gconstpointer   b);
160
161 /* callback functions */
162 static void folderview_button_pressed   (GtkWidget      *ctree,
163                                          GdkEventButton *event,
164                                          FolderView     *folderview);
165 static void folderview_button_released  (GtkWidget      *ctree,
166                                          GdkEventButton *event,
167                                          FolderView     *folderview);
168 static void folderview_key_pressed      (GtkWidget      *widget,
169                                          GdkEventKey    *event,
170                                          FolderView     *folderview);
171 static void folderview_selected         (GtkCTree       *ctree,
172                                          GtkCTreeNode   *row,
173                                          gint            column,
174                                          FolderView     *folderview);
175 static void folderview_tree_expanded    (GtkCTree       *ctree,
176                                          GtkCTreeNode   *node,
177                                          FolderView     *folderview);
178 static void folderview_tree_collapsed   (GtkCTree       *ctree,
179                                          GtkCTreeNode   *node,
180                                          FolderView     *folderview);
181 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
182                                          FolderView     *folderview);
183 static void folderview_col_resized      (GtkCList       *clist,
184                                          gint            column,
185                                          gint            width,
186                                          FolderView     *folderview);
187
188 static void folderview_update_tree_cb   (FolderView     *folderview,
189                                          guint           action,
190                                          GtkWidget      *widget);
191
192 static void mark_all_read_cb            (FolderView    *folderview,
193                                          guint           action,
194                                          GtkWidget      *widget);
195 static void folderview_new_folder_cb    (FolderView     *folderview,
196                                          guint           action,
197                                          GtkWidget      *widget);
198 static void folderview_new_mbox_folder_cb(FolderView *folderview,
199                                           guint action,
200                                           GtkWidget *widget);
201 static void folderview_rename_folder_cb (FolderView     *folderview,
202                                          guint           action,
203                                          GtkWidget      *widget);
204 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
205                                              guint action,
206                                              GtkWidget *widget);
207 static void folderview_delete_folder_cb (FolderView     *folderview,
208                                          guint           action,
209                                          GtkWidget      *widget);
210 static void folderview_remove_mailbox_cb(FolderView     *folderview,
211                                          guint           action,
212                                          GtkWidget      *widget);
213
214 static void folderview_new_imap_folder_cb(FolderView    *folderview,
215                                           guint          action,
216                                           GtkWidget     *widget);
217 static void folderview_rm_imap_folder_cb (FolderView    *folderview,
218                                           guint          action,
219                                           GtkWidget     *widget);
220 static void folderview_rm_imap_server_cb (FolderView    *folderview,
221                                           guint          action,
222                                           GtkWidget     *widget);
223
224 static void folderview_new_news_group_cb(FolderView     *folderview,
225                                          guint           action,
226                                          GtkWidget      *widget);
227 static void folderview_rm_news_group_cb (FolderView     *folderview,
228                                          guint           action,
229                                          GtkWidget      *widget);
230 static void folderview_rm_news_server_cb(FolderView     *folderview,
231                                          guint           action,
232                                          GtkWidget      *widget);
233
234 static void folderview_search_cb        (FolderView     *folderview,
235                                          guint           action,
236                                          GtkWidget      *widget);
237
238 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
239                                           GdkDragContext *context,
240                                           gint            x,
241                                           gint            y,
242                                           guint           time,
243                                           FolderView     *folderview);
244 static void folderview_drag_leave_cb     (GtkWidget        *widget,
245                                           GdkDragContext   *context,
246                                           guint             time,
247                                           FolderView       *folderview);
248 static void folderview_drag_received_cb  (GtkWidget        *widget,
249                                           GdkDragContext   *drag_context,
250                                           gint              x,
251                                           gint              y,
252                                           GtkSelectionData *data,
253                                           guint             info,
254                                           guint             time,
255                                           FolderView       *folderview);
256 static void folderview_scoring_cb(FolderView *folderview, guint action,
257                                   GtkWidget *widget);
258 static void folderview_processing_cb(FolderView *folderview, guint action,
259                                      GtkWidget *widget);
260 static void folderview_property_cb(FolderView *folderview, guint action, GtkWidget *widget);
261
262 static GtkItemFactoryEntry folderview_mbox_popup_entries[] =
263 {
264         {N_("/Create _new folder..."),  NULL, folderview_new_mbox_folder_cb,    0, NULL},
265         {N_("/_Rename folder..."),      NULL, folderview_rename_mbox_folder_cb, 0, NULL},
266         {N_("/_Delete folder"),         NULL, folderview_delete_folder_cb, 0, NULL},
267         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
268         {N_("/Remove _mailbox"),        NULL, folderview_remove_mailbox_cb, 0, NULL},
269         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
270         {N_("/_Property..."),           NULL, NULL, 0, NULL},
271         {N_("/_Processing..."),         NULL, folderview_processing_cb, 0, NULL},
272         {N_("/_Scoring..."),            NULL, folderview_scoring_cb, 0, NULL}
273 };
274
275 static GtkItemFactoryEntry folderview_mail_popup_entries[] =
276 {
277         {N_("/Mark all _read"),         NULL, mark_all_read_cb, 0, NULL},
278         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
279         {N_("/Create _new folder..."),  NULL, folderview_new_folder_cb,    0, NULL},
280         {N_("/_Rename folder..."),      NULL, folderview_rename_folder_cb, 0, NULL},
281         {N_("/_Delete folder"),         NULL, folderview_delete_folder_cb, 0, NULL},
282         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
283         {N_("/_Check for new messages"),
284                                         NULL, folderview_update_tree_cb, 0, NULL},
285         {N_("/R_escan folder tree"),    NULL, folderview_update_tree_cb, 1, NULL},
286         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
287         {N_("/Remove _mailbox"),        NULL, folderview_remove_mailbox_cb, 0, NULL},
288         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
289         {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
290         {N_("/_Property..."),           NULL, folderview_property_cb, 0, NULL},
291         {N_("/_Processing..."),         NULL, folderview_processing_cb, 0, NULL},
292         {N_("/S_coring..."),            NULL, folderview_scoring_cb, 0, NULL}
293 };
294
295 static GtkItemFactoryEntry folderview_imap_popup_entries[] =
296 {
297         {N_("/Mark all _read"),         NULL, mark_all_read_cb, 0, NULL},
298         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
299         {N_("/Create _new folder..."),  NULL, folderview_new_imap_folder_cb, 0, NULL},
300         {N_("/_Rename folder..."),      NULL, folderview_rename_folder_cb,   0, NULL},
301         {N_("/_Delete folder"),         NULL, folderview_rm_imap_folder_cb,  0, NULL},
302         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
303         {N_("/_Check for new messages"),
304                                         NULL, folderview_update_tree_cb, 0, NULL},
305         {N_("/R_escan folder tree"),    NULL, folderview_update_tree_cb, 1, NULL},
306         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
307         {N_("/Remove _IMAP4 account"),  NULL, folderview_rm_imap_server_cb, 0, NULL},
308         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
309         {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
310         {N_("/_Property..."),           NULL, NULL, 0, NULL},
311         {N_("/_Processing..."),         NULL, folderview_processing_cb, 0, NULL},
312         {N_("/S_coring..."),            NULL, folderview_scoring_cb, 0, NULL}
313 };
314
315 static GtkItemFactoryEntry folderview_news_popup_entries[] =
316 {
317         {N_("/Mark all _read"),         NULL, mark_all_read_cb, 0, NULL},
318         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
319         {N_("/_Subscribe to newsgroup..."),
320                                          NULL, folderview_new_news_group_cb, 0, NULL},
321         {N_("/_Remove newsgroup"),       NULL, folderview_rm_news_group_cb, 0, NULL},
322         {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
323         {N_("/Remove _news account"),    NULL, folderview_rm_news_server_cb, 0, NULL},
324         {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
325         {N_("/_Search folder..."),       NULL, folderview_search_cb, 0, NULL},
326         {N_("/_Property..."),            NULL, NULL, 0, NULL},
327         {N_("/_Processing..."),          NULL, folderview_processing_cb, 0, NULL},
328         {N_("/S_coring..."),            NULL, folderview_scoring_cb, 0, NULL}
329 };
330
331
332 FolderView *folderview_create(void)
333 {
334         FolderView *folderview;
335         GtkWidget *scrolledwin;
336         GtkWidget *ctree;
337         gchar *titles[N_FOLDER_COLS];
338         GtkWidget *mail_popup;
339         GtkWidget *news_popup;
340         GtkWidget *imap_popup;
341         GtkWidget *mbox_popup;
342         GtkItemFactory *mail_factory;
343         GtkItemFactory *news_factory;
344         GtkItemFactory *imap_factory;
345         GtkItemFactory *mbox_factory;
346         gint n_entries;
347         gint i;
348
349         debug_print(_("Creating folder view...\n"));
350         folderview = g_new0(FolderView, 1);
351
352         titles[COL_FOLDER] = _("Folder");
353         titles[COL_NEW]    = _("New");
354         titles[COL_UNREAD] = _("Unread");
355         titles[COL_TOTAL]  = _("#");
356
357         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
358         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
359                                        GTK_POLICY_AUTOMATIC,
360                                        GTK_POLICY_ALWAYS);
361         gtk_widget_set_usize(scrolledwin,
362                              prefs_common.folderview_width,
363                              prefs_common.folderview_height);
364
365         ctree = gtk_ctree_new_with_titles(N_FOLDER_COLS, COL_FOLDER, titles);
366         
367         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
368         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
369 #ifndef CLAWS /* text instead of pixmaps */
370         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_NEW,
371                                            GTK_JUSTIFY_RIGHT);
372         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_UNREAD,
373                                            GTK_JUSTIFY_RIGHT);
374 #endif                                     
375         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_TOTAL,
376                                            GTK_JUSTIFY_RIGHT);
377         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_FOLDER,
378                                    prefs_common.folder_col_folder);
379         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_NEW,
380                                    prefs_common.folder_col_new);
381         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_UNREAD,        
382                                    prefs_common.folder_col_unread);
383         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_TOTAL,
384                                    prefs_common.folder_col_total);
385         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
386         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
387                                      GTK_CTREE_EXPANDER_SQUARE);
388         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
389
390         /* don't let title buttons take key focus */
391         for (i = 0; i < N_FOLDER_COLS; i++)
392                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
393                                        GTK_CAN_FOCUS);
394
395         /* popup menu */
396         n_entries = sizeof(folderview_mail_popup_entries) /
397                 sizeof(folderview_mail_popup_entries[0]);
398         mail_popup = menu_create_items(folderview_mail_popup_entries,
399                                        n_entries,
400                                        "<MailFolder>", &mail_factory,
401                                        folderview);
402         n_entries = sizeof(folderview_imap_popup_entries) /
403                 sizeof(folderview_imap_popup_entries[0]);
404         imap_popup = menu_create_items(folderview_imap_popup_entries,
405                                        n_entries,
406                                        "<IMAPFolder>", &imap_factory,
407                                        folderview);
408         n_entries = sizeof(folderview_news_popup_entries) /
409                 sizeof(folderview_news_popup_entries[0]);
410         news_popup = menu_create_items(folderview_news_popup_entries,
411                                        n_entries,
412                                        "<NewsFolder>", &news_factory,
413                                        folderview);
414         n_entries = sizeof(folderview_mbox_popup_entries) /
415                 sizeof(folderview_mbox_popup_entries[0]);
416         mbox_popup = menu_create_items(folderview_mbox_popup_entries,
417                                        n_entries,
418                                        "<MailFolder>", &mbox_factory,
419                                        folderview);
420
421         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
422                            GTK_SIGNAL_FUNC(folderview_key_pressed),
423                            folderview);
424         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
425                            GTK_SIGNAL_FUNC(folderview_button_pressed),
426                            folderview);
427         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
428                            GTK_SIGNAL_FUNC(folderview_button_released),
429                            folderview);
430         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
431                            GTK_SIGNAL_FUNC(folderview_selected), folderview);
432
433         gtk_signal_connect_after(GTK_OBJECT(ctree), "tree_expand",
434                                  GTK_SIGNAL_FUNC(folderview_tree_expanded),
435                                  folderview);
436         gtk_signal_connect_after(GTK_OBJECT(ctree), "tree_collapse",
437                                  GTK_SIGNAL_FUNC(folderview_tree_collapsed),
438                                  folderview);
439
440         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
441                            GTK_SIGNAL_FUNC(folderview_col_resized),
442                            folderview);
443
444         gtk_signal_connect(GTK_OBJECT(mail_popup), "selection_done",
445                            GTK_SIGNAL_FUNC(folderview_popup_close),
446                            folderview);
447         gtk_signal_connect(GTK_OBJECT(imap_popup), "selection_done",
448                            GTK_SIGNAL_FUNC(folderview_popup_close),
449                            folderview);
450         gtk_signal_connect(GTK_OBJECT(news_popup), "selection_done",
451                            GTK_SIGNAL_FUNC(folderview_popup_close),
452                            folderview);
453         gtk_signal_connect(GTK_OBJECT(mbox_popup), "selection_done",
454                            GTK_SIGNAL_FUNC(folderview_popup_close),
455                            folderview);
456
457         /* drop callback */
458         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL &
459                           ~GTK_DEST_DEFAULT_HIGHLIGHT,
460                           summary_drag_types, 1,
461                           GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT);
462         gtk_signal_connect(GTK_OBJECT(ctree), "drag_motion",
463                            GTK_SIGNAL_FUNC(folderview_drag_motion_cb),
464                            folderview);
465         gtk_signal_connect(GTK_OBJECT(ctree), "drag_leave",
466                            GTK_SIGNAL_FUNC(folderview_drag_leave_cb),
467                            folderview);
468         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_received",
469                            GTK_SIGNAL_FUNC(folderview_drag_received_cb),
470                            folderview);
471
472         folderview->scrolledwin  = scrolledwin;
473         folderview->ctree        = ctree;
474         folderview->mail_popup   = mail_popup;
475         folderview->mail_factory = mail_factory;
476         folderview->imap_popup   = imap_popup;
477         folderview->imap_factory = imap_factory;
478         folderview->news_popup   = news_popup;
479         folderview->news_factory = news_factory;
480         folderview->mbox_popup   = mbox_popup;
481         folderview->mbox_factory = mbox_factory;
482
483         gtk_widget_show_all(scrolledwin);
484
485         folderview_list = g_list_append(folderview_list, folderview);
486
487         return folderview;
488 }
489
490 void folderview_init(FolderView *folderview)
491 {
492         GtkWidget *ctree = folderview->ctree;
493         GtkWidget *label_new;
494         GtkWidget *label_unread;
495         GtkWidget *hbox_new;
496         GtkWidget *hbox_unread;
497
498         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
499         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
500         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
501         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
502         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
503         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
504         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
505         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
506         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
507         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
508         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
509         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
510         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
511         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
512         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
513         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
514         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
515         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
516         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
517         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
518
519         /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
520          * instead text (text overflows making them unreadable and ugly) */
521         stock_pixmap_gdk(ctree, STOCK_PIXMAP_NEW,
522                          &newxpm, &newxpmmask);
523         stock_pixmap_gdk(ctree, STOCK_PIXMAP_UNREAD,
524                          &unreadxpm, &unreadxpmmask);
525                 
526         label_new = gtk_pixmap_new(newxpm, newxpmmask);
527         label_unread = gtk_pixmap_new(unreadxpm, unreadxpmmask);
528
529         hbox_new = gtk_hbox_new(FALSE, 4);
530         hbox_unread = gtk_hbox_new(FALSE, 4);
531
532         /* left justified */
533         gtk_box_pack_start(GTK_BOX(hbox_new),label_new,FALSE,FALSE,0);
534         gtk_box_pack_start(GTK_BOX(hbox_unread),label_unread,FALSE,FALSE,0);
535
536         gtk_widget_show_all(hbox_new);
537         gtk_widget_show_all(hbox_unread);
538
539         gtk_clist_set_column_widget(GTK_CLIST(ctree),COL_NEW,hbox_new);
540         gtk_clist_set_column_widget(GTK_CLIST(ctree),COL_UNREAD,hbox_unread);
541                         
542
543
544         if (!normalfont)
545                 normalfont = gdk_fontset_load(NORMAL_FONT);
546         if (!boldfont)
547                 boldfont = gdk_fontset_load(BOLD_FONT);
548
549         if (!bold_style) {
550                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
551                 bold_style->font = boldfont;
552                 bold_color_style = gtk_style_copy(bold_style);
553                 bold_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
554
555                 bold_tgtfold_style = gtk_style_copy(bold_style);
556                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
557         }
558         if (!normal_style) {
559                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
560                 normal_style->font = normalfont;
561                 normal_color_style = gtk_style_copy(normal_style);
562                 normal_color_style->fg[GTK_STATE_NORMAL] = folderview->color_new;
563         }
564 }
565
566 void folderview_set(FolderView *folderview)
567 {
568         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
569         MainWindow *mainwin = folderview->mainwin;
570
571         debug_print(_("Setting folder info...\n"));
572         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
573
574         main_window_cursor_wait(mainwin);
575
576         folderview->selected = NULL;
577         folderview->opened = NULL;
578
579         gtk_clist_freeze(GTK_CLIST(ctree));
580         gtk_clist_clear(GTK_CLIST(ctree));
581         gtk_clist_thaw(GTK_CLIST(ctree));
582         gtk_clist_freeze(GTK_CLIST(ctree));
583
584         folderview_set_folders(folderview);
585
586         gtk_clist_thaw(GTK_CLIST(ctree));
587         main_window_cursor_normal(mainwin);
588         STATUSBAR_POP(mainwin);
589 }
590
591 void folderview_set_all(void)
592 {
593         GList *list;
594
595         for (list = folderview_list; list != NULL; list = list->next)
596                 folderview_set((FolderView *)list->data);
597 }
598
599 void folderview_select(FolderView *folderview, FolderItem *item)
600 {
601         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
602         GtkCTreeNode *node;
603         GtkCTreeNode *old_selected = folderview->selected;
604
605         if (!item) return;
606
607         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
608         if (node) folderview_select_node(folderview, node);
609
610         if (old_selected != node)
611                 folder_update_op_count();
612 }
613
614 static void mark_all_read_cb(FolderView *folderview, guint action,
615                              GtkWidget *widget)
616 {
617         if (folderview->selected)
618                 summary_mark_all_read(folderview->summaryview);
619 }
620
621 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
622 {
623         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
624
625         g_return_if_fail(node != NULL);
626
627         folderview->open_folder = TRUE;
628         gtkut_ctree_set_focus_row(ctree, node);
629         gtk_ctree_select(ctree, node);
630         if (folderview->summaryview->messages > 0)
631                 gtk_widget_grab_focus(folderview->summaryview->ctree);
632         else
633                 gtk_widget_grab_focus(folderview->ctree);
634
635         gtkut_ctree_expand_parent_all(ctree, node);
636 }
637
638 void folderview_unselect(FolderView *folderview)
639 {
640         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
641                 gtk_ctree_collapse
642                         (GTK_CTREE(folderview->ctree), folderview->opened);
643
644         folderview->selected = folderview->opened = NULL;
645 }
646
647 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
648                                                  GtkCTreeNode *node)
649 {
650         FolderItem *item;
651
652         if (node)
653                 node = gtkut_ctree_node_next(ctree, node);
654         else
655                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
656
657         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
658                 item = gtk_ctree_node_get_row_data(ctree, node);
659                 if (item && item->unread > 0 && item->stype != F_TRASH)
660                         return node;
661         }
662
663         return NULL;
664 }
665
666 void folderview_select_next_unread(FolderView *folderview)
667 {
668         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
669         GtkCTreeNode *node = NULL;
670
671         if ((node = folderview_find_next_unread(ctree, folderview->opened))
672             != NULL) {
673                 folderview_select_node(folderview, node);
674                 return;
675         }
676
677         if (!folderview->opened ||
678             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
679                 return;
680         /* search again from the first node */
681         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
682                 folderview_select_node(folderview, node);
683 }
684
685 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
686 {
687         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
688         static GtkCTreeNode *prev_row = NULL;
689         FolderItem *item;
690         gint new, unread, total;
691         gchar *new_str, *unread_str, *total_str;
692
693         if (!row) return;
694
695         item = gtk_ctree_node_get_row_data(ctree, row);
696         if (!item) return;
697
698         gtk_ctree_node_get_text(ctree, row, COL_NEW, &new_str);
699         gtk_ctree_node_get_text(ctree, row, COL_UNREAD, &unread_str);
700         gtk_ctree_node_get_text(ctree, row, COL_TOTAL, &total_str);
701         new = atoi(new_str);
702         unread = atoi(unread_str);
703         total = atoi(total_str);
704
705         /* CLAWS: don't know why but this always seems to be true
706          * when deleting messages. Somewhere claws does a folder
707          * scan which sets all new, unread & total to the correct
708          * values. It then enters this function, but leaves it
709          * because new, unread and total are the same... */
710 #ifndef CLAWS    
711         if (prev_row     == row    &&
712             item->new    == new    &&
713             item->unread == unread &&
714             item->total  == total)
715                 return;
716 #endif          
717
718         prev_row = row;
719
720         folderview_update_node(folderview, row);
721 }
722
723 static void folderview_set_folders(FolderView *folderview)
724 {
725         GList *list;
726
727         list = folder_get_list();
728
729         for (; list != NULL; list = list->next)
730                 folderview_append_folder(folderview, FOLDER(list->data));
731 }
732
733 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
734                                       gpointer data)
735 {
736         GList *list;
737         gchar *rootpath;
738
739         if (FOLDER_IS_LOCAL(folder))
740                 rootpath = LOCAL_FOLDER(folder)->rootpath;
741         else if (folder->type == F_IMAP && folder->account &&
742                  folder->account->recv_server)
743                 rootpath = folder->account->recv_server;
744         else
745                 return;
746
747         for (list = folderview_list; list != NULL; list = list->next) {
748                 FolderView *folderview = (FolderView *)list->data;
749                 MainWindow *mainwin = folderview->mainwin;
750                 gchar *str;
751
752                 if (item->path)
753                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
754                                               rootpath, G_DIR_SEPARATOR,
755                                               item->path);
756                 else
757                         str = g_strdup_printf(_("Scanning folder %s ..."),
758                                               rootpath);
759
760                 STATUSBAR_PUSH(mainwin, str);
761                 STATUSBAR_POP(mainwin);
762                 g_free(str);
763         }
764 }
765
766 static GtkWidget *label_window_create(const gchar *str)
767 {
768         GtkWidget *window;
769         GtkWidget *label;
770
771         window = gtk_window_new(GTK_WINDOW_DIALOG);
772         gtk_widget_set_usize(window, 380, 60);
773         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
774         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
775         gtk_window_set_title(GTK_WINDOW(window), str);
776         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
777         gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
778         manage_window_set_transient(GTK_WINDOW(window));
779
780         label = gtk_label_new(str);
781         gtk_container_add(GTK_CONTAINER(window), label);
782         gtk_widget_show(label);
783
784         gtk_widget_show_now(window);
785
786         return window;
787 }
788
789 void folderview_rescan_tree(Folder *folder)
790 {
791         GtkWidget *window;
792
793         g_return_if_fail(folder != NULL);
794
795         if (!folder->scan_tree) return;
796
797         inc_lock();
798         window = label_window_create(_("Rescanning folder tree..."));
799
800         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
801         folder->scan_tree(folder);
802         folder_set_ui_func(folder, NULL, NULL);
803
804         folder_write_list();
805         folderview_set_all();
806
807         gtk_widget_destroy(window);
808         inc_unlock();
809 }
810
811 void folderview_rescan_all(void)
812 {
813         GList *list;
814         GtkWidget *window;
815
816         inc_lock();
817         window = label_window_create(_("Rescanning all folder trees..."));
818
819         list = folder_get_list();
820         for (; list != NULL; list = list->next) {
821                 Folder *folder = list->data;
822
823                 if (!folder->scan_tree) continue;
824                 folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
825                 folder->scan_tree(folder);
826                 folder_set_ui_func(folder, NULL, NULL);
827         }
828
829         folder_write_list();
830         folderview_set_all();
831         gtk_widget_destroy(window);
832         inc_unlock();
833 }
834
835 void folderview_check_new(Folder *folder)
836 {
837         GList *list;
838         FolderItem *item;
839         FolderView *folderview;
840         GtkCTree *ctree;
841         GtkCTreeNode *node;
842
843         for (list = folderview_list; list != NULL; list = list->next) {
844                 folderview = (FolderView *)list->data;
845                 ctree = GTK_CTREE(folderview->ctree);
846
847                 inc_lock();
848                 main_window_lock(folderview->mainwin);
849                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
850
851                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
852                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
853                         item = gtk_ctree_node_get_row_data(ctree, node);
854                         if (!item || !item->path || !item->folder) continue;
855                         if (folder && folder != item->folder) continue;
856                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
857
858                         folderview_scan_tree_func(item->folder, item, NULL);
859                         folder_item_scan(item);
860                         folderview_update_node(folderview, node);
861                 }
862
863                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
864                 main_window_unlock(folderview->mainwin);
865                 inc_unlock();
866         }
867
868         folder_write_list();
869 }
870
871 static gboolean folderview_search_new_recursive(GtkCTree *ctree,
872                                                 GtkCTreeNode *node)
873 {
874         FolderItem *item;
875
876         if (node) {
877                 item = gtk_ctree_node_get_row_data(ctree, node);
878                 if (item) {
879                         if (item->new > 0 ||
880                             (item->stype == F_QUEUE && item->total > 0))
881                                 return TRUE;
882                 }
883                 node = GTK_CTREE_ROW(node)->children;
884         } else
885                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
886
887         while (node) {
888                 if (folderview_search_new_recursive(ctree, node) == TRUE)
889                         return TRUE;
890                 node = GTK_CTREE_ROW(node)->sibling;
891         }
892
893         return FALSE;
894 }
895
896 static gboolean folderview_have_new_children(FolderView *folderview,
897                                              GtkCTreeNode *node)
898 {
899         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
900
901         if (!node)
902                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
903         if (!node)
904                 return FALSE;
905
906         node = GTK_CTREE_ROW(node)->children;
907
908         while (node) {
909                 if (folderview_search_new_recursive(ctree, node) == TRUE)
910                         return TRUE;
911                 node = GTK_CTREE_ROW(node)->sibling;
912         }
913
914         return FALSE;
915 }
916
917 static gboolean folderview_search_unread_recursive(GtkCTree *ctree,
918                                                    GtkCTreeNode *node)
919 {
920         FolderItem *item;
921
922         if (node) {
923                 item = gtk_ctree_node_get_row_data(ctree, node);
924                 if (item) {
925                         if (item->unread > 0 ||
926                             (item->stype == F_QUEUE && item->total > 0))
927                                 return TRUE;
928                 }
929                 node = GTK_CTREE_ROW(node)->children;
930         } else
931                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
932
933         while (node) {
934                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
935                         return TRUE;
936                 node = GTK_CTREE_ROW(node)->sibling;
937         }
938
939         return FALSE;
940 }
941
942 static gboolean folderview_have_unread_children(FolderView *folderview,
943                                                 GtkCTreeNode *node)
944 {
945         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
946
947         if (!node)
948                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
949         if (!node)
950                 return FALSE;
951
952         node = GTK_CTREE_ROW(node)->children;
953
954         while (node) {
955                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
956                         return TRUE;
957                 node = GTK_CTREE_ROW(node)->sibling;
958         }
959
960         return FALSE;
961 }
962
963 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
964 {
965         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
966         GtkStyle *style = NULL;
967         FolderItem *item;
968         GdkPixmap *xpm, *openxpm;
969         GdkBitmap *mask, *openmask;
970         gchar *name;
971         gchar *str;
972         gboolean add_unread_mark;
973         gboolean use_bold, use_color;
974
975         item = gtk_ctree_node_get_row_data(ctree, node);
976         g_return_if_fail(item != NULL);
977
978         switch (item->stype) {
979         case F_INBOX:
980                 if (item->hide_read_msgs) {
981                         xpm = inboxhrmxpm;
982                         mask = inboxhrmxpmmask;
983                         openxpm = inboxopenhrmxpm;
984                         openmask = inboxopenhrmxpmmask;
985                 } else {
986                         xpm = inboxxpm;
987                         mask = inboxxpmmask;
988                         openxpm = inboxopenxpm;
989                         openmask = inboxopenxpmmask;
990                 }
991                 name = g_strdup(_("Inbox"));
992                 break;
993         case F_OUTBOX:
994                 if (item->hide_read_msgs) {
995                         xpm = outboxhrmxpm;
996                         mask = outboxhrmxpmmask;
997                         openxpm = outboxopenhrmxpm;
998                         openmask = outboxopenhrmxpmmask;
999                 } else {
1000                         xpm = outboxxpm;
1001                         mask = outboxxpmmask;
1002                         openxpm = outboxopenxpm;
1003                         openmask = outboxopenxpmmask;
1004                 }
1005                 name = g_strdup(_("Sent"));
1006                 break;
1007         case F_QUEUE:
1008                 if (item->hide_read_msgs) {
1009                         xpm = queuehrmxpm;
1010                         mask = queuehrmxpmmask;
1011                         openxpm = queueopenhrmxpm;
1012                         openmask = queueopenhrmxpmmask;
1013                 } else {
1014                         xpm = queuexpm;
1015                         mask = queuexpmmask;
1016                         openxpm = queueopenxpm;
1017                         openmask = queueopenxpmmask;
1018                 }
1019                 name = g_strdup(_("Queue"));
1020                 break;
1021         case F_TRASH:
1022                 if (item->hide_read_msgs) {
1023                         xpm = trashhrmxpm;
1024                         mask = trashhrmxpmmask;
1025                         openxpm = trashopenhrmxpm;
1026                         openmask = trashopenhrmxpmmask;
1027                 } else {
1028                         xpm = trashxpm;
1029                         mask = trashxpmmask;
1030                         openxpm = trashopenxpm;
1031                         openmask = trashopenxpmmask;
1032                 }
1033                 name = g_strdup(_("Trash"));
1034                 break;
1035         case F_DRAFT:
1036                 xpm = folderxpm;
1037                 mask = folderxpmmask;
1038                 if (item->hide_read_msgs) {
1039                         openxpm = folderopenhrmxpm;
1040                         openmask = folderopenhrmxpmmask;
1041                 } else {
1042                         openxpm = folderopenxpm;
1043                         openmask = folderopenxpmmask;
1044                 }
1045                 name = g_strdup(_("Drafts"));
1046                 break;
1047         default:
1048                 if (item->hide_read_msgs) {
1049                         xpm = folderhrmxpm;
1050                         mask = folderhrmxpmmask;
1051                         openxpm = folderopenhrmxpm;
1052                         openmask = folderopenhrmxpmmask;
1053                 } else {
1054                         xpm = folderxpm;
1055                         mask = folderxpmmask;
1056                         openxpm = folderopenxpm;
1057                         openmask = folderopenxpmmask;
1058                 }
1059                 if (!item->parent) {
1060                         switch (item->folder->type) {
1061                         case F_MH:
1062                                 name = " (MH)"; break;
1063                         case F_MBOX:
1064                                 name = " (mbox)"; break;
1065                         case F_IMAP:
1066                                 name = " (IMAP4)"; break;
1067                         case F_NEWS:
1068                                 name = " (News)"; break;
1069                         default:
1070                                 name = "";
1071                         }
1072                         name = g_strconcat(item->name, name, NULL);
1073                 } else {
1074                         if (item->folder->type == F_NEWS &&
1075                             !strcmp2(item->name, item->path))
1076                                 name = get_abbrev_newsgroup_name(item->path);
1077                         else
1078                                 name = g_strdup(item->name);
1079                 }
1080         }
1081
1082         if (!GTK_CTREE_ROW(node)->expanded &&
1083             folderview_have_unread_children(folderview, node))
1084                 add_unread_mark = TRUE;
1085         else
1086                 add_unread_mark = FALSE;
1087
1088         if (item->stype == F_QUEUE && item->total > 0 &&
1089             prefs_common.display_folder_unread) {
1090                 str = g_strdup_printf("%s (%d%s)", name, item->total,
1091                                       add_unread_mark ? "+" : "");
1092                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1093                                         xpm, mask, openxpm, openmask,
1094                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1095                 g_free(str);
1096         } else if ((item->unread > 0 || add_unread_mark) &&
1097                  prefs_common.display_folder_unread) {
1098
1099                 if (item->unread > 0)
1100                         str = g_strdup_printf("%s (%d%s)", name, item->unread,
1101                                               add_unread_mark ? "+" : "");
1102                 else
1103                         str = g_strdup_printf("%s (+)", name);
1104                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1105                                         xpm, mask, openxpm, openmask,
1106                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1107                 g_free(str);
1108         } else
1109                 gtk_ctree_set_node_info(ctree, node, name, FOLDER_SPACING,
1110                                         xpm, mask, openxpm, openmask,
1111                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1112         g_free(name);
1113
1114         if (!item->parent) {
1115                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1116                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1117                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1118         } else {
1119                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
1120                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
1121                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
1122         }
1123
1124         if (item->stype == F_TRASH)
1125                 use_bold = use_color = FALSE;
1126         if (item->stype == F_QUEUE) {
1127                 /* highlight queue folder if there are any messages */
1128                 use_bold = use_color = (item->total > 0);
1129         } else {
1130                 /* if unread messages exist, print with bold font */
1131                 use_bold = (item->unread > 0) || add_unread_mark;
1132                 /* if new messages exist, print with colored letter */
1133                 use_color =
1134                         (item->new > 0) ||
1135                         (add_unread_mark &&
1136                          folderview_have_new_children(folderview, node));
1137         }
1138
1139         gtk_ctree_node_set_foreground(ctree, node, NULL);
1140
1141         if (use_bold && use_color)
1142                 style = bold_color_style;
1143         else if (use_bold) {
1144                 style = bold_style;
1145                 if (item->op_count > 0) {
1146                         style = bold_tgtfold_style;
1147                 }
1148         }
1149         else if (use_color) {
1150                 style = normal_color_style;
1151                 gtk_ctree_node_set_foreground(ctree, node,
1152                                               &folderview->color_new);
1153         }
1154         else if (item->op_count > 0) {
1155                 style = bold_tgtfold_style;
1156         } else {
1157                 style = normal_style;
1158         }
1159
1160         gtk_ctree_node_set_row_style(ctree, node, style);
1161
1162         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1163                 folderview_update_node(folderview, node);
1164 }
1165
1166 void folderview_update_item(FolderItem *item, gboolean update_summary)
1167 {
1168         GList *list;
1169         FolderView *folderview;
1170         GtkCTree *ctree;
1171         GtkCTreeNode *node;
1172
1173         g_return_if_fail(item != NULL);
1174
1175         for (list = folderview_list; list != NULL; list = list->next) {
1176                 folderview = (FolderView *)list->data;
1177                 ctree = GTK_CTREE(folderview->ctree);
1178
1179                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1180                 if (node) {
1181                         folderview_update_node(folderview, node);
1182                         if (update_summary && folderview->opened == node)
1183                                 summary_show(folderview->summaryview,
1184                                              item, FALSE);
1185                 }
1186         }
1187 }
1188
1189 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1190                                                 gpointer data)
1191 {
1192         folderview_update_item((FolderItem *)key, FALSE);
1193 }
1194
1195 void folderview_update_item_foreach(GHashTable *table)
1196 {
1197         g_hash_table_foreach(table, folderview_update_item_foreach_func, NULL);
1198 }
1199
1200 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1201                                       GNode *gnode, GtkCTreeNode *cnode,
1202                                       gpointer data)
1203 {
1204         FolderView *folderview = (FolderView *)data;
1205         FolderItem *item = FOLDER_ITEM(gnode->data);
1206
1207         g_return_val_if_fail(item != NULL, FALSE);
1208
1209         gtk_ctree_node_set_row_data(ctree, cnode, item);
1210         folderview_update_node(folderview, cnode);
1211
1212         return TRUE;
1213 }
1214
1215 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1216                                    gpointer data)
1217 {
1218         FolderView *folderview = (FolderView *)data;
1219         FolderItem *item;
1220
1221         if (GTK_CTREE_ROW(node)->children) {
1222                 item = gtk_ctree_node_get_row_data(ctree, node);
1223                 g_return_if_fail(item != NULL);
1224
1225                 if (!item->collapsed)
1226                         gtk_ctree_expand(ctree, node);
1227                 else
1228                         folderview_update_node(folderview, node);
1229         }
1230 }
1231
1232 #define SET_SPECIAL_FOLDER(ctree, item) \
1233 { \
1234         if (item) { \
1235                 GtkCTreeNode *node, *parent, *sibling; \
1236  \
1237                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
1238                 if (!node) \
1239                         g_warning("%s not found.\n", item->path); \
1240                 else { \
1241                         parent = GTK_CTREE_ROW(node)->parent; \
1242                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
1243                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
1244                         else \
1245                                 sibling = GTK_CTREE_ROW(parent)->children; \
1246                         if (node != sibling) \
1247                                 gtk_ctree_move(ctree, node, parent, sibling); \
1248                 } \
1249  \
1250                 prev = node; \
1251         } \
1252 }
1253
1254 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1255                                     Folder *folder)
1256 {
1257         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1258         GtkCTreeNode *prev = NULL;
1259
1260         gtk_ctree_sort_recursive(ctree, root);
1261
1262         if (GTK_CTREE_ROW(root)->parent) return;
1263
1264         SET_SPECIAL_FOLDER(ctree, folder->inbox);
1265         SET_SPECIAL_FOLDER(ctree, folder->outbox);
1266         SET_SPECIAL_FOLDER(ctree, folder->draft);
1267         SET_SPECIAL_FOLDER(ctree, folder->queue);
1268         SET_SPECIAL_FOLDER(ctree, folder->trash);
1269 }
1270
1271 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1272 {
1273         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1274         GtkCTreeNode *root;
1275
1276         g_return_if_fail(folder != NULL);
1277
1278         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1279                                       folderview_gnode_func, folderview);
1280         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1281                                 folderview);
1282         folderview_sort_folders(folderview, root, folder);
1283 }
1284
1285 void folderview_new_folder(FolderView *folderview)
1286 {
1287         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1288         FolderItem *item;
1289
1290         if (!folderview->selected) return;
1291
1292         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1293         g_return_if_fail(item != NULL);
1294         g_return_if_fail(item->folder != NULL);
1295
1296         switch (item->folder->type) {
1297         case F_MBOX:
1298                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1299                 break;
1300         case F_MH:
1301         case F_MAILDIR:
1302                 folderview_new_folder_cb(folderview, 0, NULL);
1303                 break;
1304         case F_IMAP:
1305                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1306                 break;
1307         case F_NEWS:
1308         default:
1309                 break;
1310         }
1311 }
1312
1313 void folderview_rename_folder(FolderView *folderview)
1314 {
1315         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1316         FolderItem *item;
1317
1318         if (!folderview->selected) return;
1319
1320         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1321         g_return_if_fail(item != NULL);
1322         g_return_if_fail(item->folder != NULL);
1323         if (!item->path) return;
1324         if (item->stype != F_NORMAL) return;
1325
1326         switch (item->folder->type) {
1327         case F_MBOX:
1328                 folderview_rename_mbox_folder_cb(folderview, 0, NULL);
1329         case F_MH:
1330         case F_MAILDIR:
1331         case F_IMAP:
1332                 folderview_rename_folder_cb(folderview, 0, NULL);
1333                 break;
1334         case F_NEWS:
1335         default:
1336                 break;
1337         }
1338 }
1339
1340 void folderview_delete_folder(FolderView *folderview)
1341 {
1342         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1343         FolderItem *item;
1344
1345         if (!folderview->selected) return;
1346
1347         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1348         g_return_if_fail(item != NULL);
1349         g_return_if_fail(item->folder != NULL);
1350         if (!item->path) return;
1351         if (item->stype != F_NORMAL) return;
1352
1353         switch (item->folder->type) {
1354         case F_MH:
1355         case F_MBOX:
1356         case F_MAILDIR:
1357                 folderview_delete_folder_cb(folderview, 0, NULL);
1358                 break;
1359         case F_IMAP:
1360                 folderview_rm_imap_folder_cb(folderview, 0, NULL);
1361         case F_NEWS:
1362         default:
1363                 break;
1364         }
1365 }
1366
1367
1368 /* callback functions */
1369
1370 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1371                                       FolderView *folderview)
1372 {
1373         GtkCList *clist = GTK_CLIST(ctree);
1374         gint prev_row = -1, row = -1, column = -1;
1375         FolderItem *item;
1376         Folder *folder;
1377         GtkWidget *popup;
1378         gboolean mark_all_read   = FALSE;
1379         gboolean new_folder      = FALSE;
1380         gboolean rename_folder   = FALSE;
1381         gboolean delete_folder   = FALSE;
1382         gboolean update_tree     = FALSE;
1383         gboolean rescan_tree     = FALSE;
1384         gboolean remove_tree     = FALSE;
1385         gboolean folder_property = FALSE;
1386         gboolean folder_processing  = FALSE;
1387         gboolean folder_scoring  = FALSE;
1388         gboolean search_folder = FALSE;
1389
1390         if (!event) return;
1391
1392         if (event->button == 1) {
1393                 folderview->open_folder = TRUE;
1394                 return;
1395         }
1396
1397         if (event->button == 2 || event->button == 3) {
1398                 /* right clicked */
1399                 if (clist->selection) {
1400                         GtkCTreeNode *node;
1401
1402                         node = GTK_CTREE_NODE(clist->selection->data);
1403                         if (node)
1404                                 prev_row = gtkut_ctree_get_nth_from_node
1405                                         (GTK_CTREE(ctree), node);
1406                 }
1407
1408                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1409                                                   &row, &column))
1410                         return;
1411                 if (prev_row != row) {
1412                         gtk_clist_unselect_all(clist);
1413                         if (event->button == 2)
1414                                 folderview_select_node
1415                                         (folderview,
1416                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1417                                                             row));
1418                         else
1419                                 gtk_clist_select_row(clist, row, column);
1420                 }
1421         }
1422
1423         if (event->button != 3) return;
1424
1425         item = gtk_clist_get_row_data(clist, row);
1426         g_return_if_fail(item != NULL);
1427         g_return_if_fail(item->folder != NULL);
1428         folder = item->folder;
1429
1430         if (folderview->mainwin->lock_count == 0) {
1431                 new_folder = TRUE;
1432                 if (item->parent == NULL)
1433                         update_tree = remove_tree = TRUE;
1434                 else
1435                         mark_all_read = search_folder = TRUE;
1436                 if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
1437                         if (item->parent == NULL)
1438                                 update_tree = rescan_tree = TRUE;
1439                         else if (item->stype == F_NORMAL)
1440                                 rename_folder = delete_folder = folder_property = folder_scoring = folder_processing = TRUE;
1441                         else if (item->stype == F_INBOX)
1442                                 folder_property = folder_scoring = folder_processing = TRUE;
1443                         else if (item->stype == F_TRASH)
1444                                 folder_processing = TRUE;
1445                         else if (item->stype == F_OUTBOX)
1446                                 folder_processing = TRUE;
1447                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
1448                         if (item->parent != NULL)
1449                                 delete_folder = folder_scoring = folder_processing = TRUE;
1450                 }
1451                 if (item->unread < 1) 
1452                         mark_all_read = FALSE;
1453         }
1454
1455 #define SET_SENS(factory, name, sens) \
1456         menu_set_sensitive(folderview->factory, name, sens)
1457         
1458         mark_all_read = mark_all_read && 
1459                         (item == folderview->summaryview->folder_item);
1460
1461         if (FOLDER_IS_LOCAL(folder)) {
1462                 popup = folderview->mail_popup;
1463                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1464                 SET_SENS(mail_factory, "/Mark all read", mark_all_read);
1465                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1466                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1467                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1468                 SET_SENS(mail_factory, "/Check for new messages", update_tree);
1469                 SET_SENS(mail_factory, "/Rescan folder tree", rescan_tree);
1470                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1471                 SET_SENS(mail_factory, "/Property...", folder_property);
1472                 SET_SENS(mail_factory, "/Processing...", folder_processing);
1473                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1474                 SET_SENS(mail_factory, "/Search folder...", search_folder);
1475         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1476                 popup = folderview->imap_popup;
1477                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1478                 SET_SENS(imap_factory, "/Mark all read", mark_all_read);
1479                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1480                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1481                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1482                 SET_SENS(imap_factory, "/Check for new messages", update_tree);
1483                 SET_SENS(imap_factory, "/Rescan folder tree", rescan_tree);
1484                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1485                 SET_SENS(imap_factory, "/Processing...", folder_processing);
1486                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1487                 SET_SENS(imap_factory, "/Search folder...", search_folder);
1488         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1489                 popup = folderview->news_popup;
1490                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1491                 SET_SENS(news_factory, "/Mark all read", mark_all_read);
1492                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1493                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1494 #if 0
1495                 SET_SENS(news_factory, "/Check for new messages", update_tree);
1496 #endif
1497                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1498                 SET_SENS(news_factory, "/Search folder...", search_folder);
1499                 SET_SENS(news_factory, "/Processing...", folder_processing);
1500                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1501         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1502                 popup = folderview->mbox_popup;
1503                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1504                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1505                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1506                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1507                 SET_SENS(mbox_factory, "/Processing...", folder_processing);
1508                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1509         } else
1510                 return;
1511
1512 #undef SET_SENS
1513
1514         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1515                        event->button, event->time);
1516 }
1517
1518 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1519                                        FolderView *folderview)
1520 {
1521         if (!event) return;
1522
1523         if (event->button == 1 && folderview->open_folder == FALSE &&
1524             folderview->opened != NULL) {
1525                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1526                                           folderview->opened);
1527                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1528         }
1529 }
1530
1531 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1532                                    FolderView *folderview)
1533 {
1534         if (!event) return;
1535
1536         switch (event->keyval) {
1537         case GDK_Return:
1538                 if (folderview->selected) {
1539                         folderview_select_node(folderview,
1540                                                folderview->selected);
1541                 }
1542                 break;
1543         case GDK_space:
1544                 if (folderview->selected) {
1545                         if (folderview->opened == folderview->selected &&
1546                             folderview->summaryview->messages == 0)
1547                                 folderview_select_next_unread(folderview);
1548                         else
1549                                 folderview_select_node(folderview,
1550                                                        folderview->selected);
1551                 }
1552                 break;
1553         default:
1554                 break;
1555         }
1556 }
1557
1558 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1559                                 gint column, FolderView *folderview)
1560 {
1561         static gboolean can_select = TRUE;      /* exclusive lock */
1562         gboolean opened;
1563         FolderItem *item;
1564
1565         folderview->selected = row;
1566
1567         if (folderview->opened == row) {
1568                 folderview->open_folder = FALSE;
1569                 return;
1570         }
1571
1572         if (!can_select) {
1573                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1574                 gtk_ctree_select(ctree, folderview->opened);
1575                 return;
1576         }
1577
1578         if (!folderview->open_folder) return;
1579
1580         item = gtk_ctree_node_get_row_data(ctree, row);
1581         if (!item) return;
1582
1583         can_select = FALSE;
1584
1585         /* CLAWS: set compose button type: news folder items 
1586          * always have a news folder as parent */
1587         if (item->folder) 
1588                 main_window_toolbar_set_compose_button
1589                         (folderview->mainwin,
1590                          item->folder->type == F_NEWS ? 
1591                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1592          
1593         if (item->path)
1594                 debug_print(_("Folder %s is selected\n"), item->path);
1595
1596         if (!GTK_CTREE_ROW(row)->children)
1597                 gtk_ctree_expand(ctree, row);
1598         if (folderview->opened &&
1599             !GTK_CTREE_ROW(folderview->opened)->children)
1600                 gtk_ctree_collapse(ctree, folderview->opened);
1601
1602         /* ungrab the mouse event */
1603         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1604                 gtk_grab_remove(GTK_WIDGET(ctree));
1605                 if (gdk_pointer_is_grabbed())
1606                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1607         }
1608
1609         opened = summary_show(folderview->summaryview, item, FALSE);
1610
1611         if (!opened) {
1612                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1613                 gtk_ctree_select(ctree, folderview->opened);
1614         } else {
1615                 folderview->opened = row;
1616                 if (gtk_ctree_node_is_visible(ctree, row)
1617                     != GTK_VISIBILITY_FULL)
1618                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1619         }
1620
1621         folderview->open_folder = FALSE;
1622         can_select = TRUE;
1623 }
1624
1625 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1626                                      FolderView *folderview)
1627 {
1628         FolderItem *item;
1629
1630         item = gtk_ctree_node_get_row_data(ctree, node);
1631         g_return_if_fail(item != NULL);
1632         item->collapsed = FALSE;
1633         folderview_update_node(folderview, node);
1634 }
1635
1636 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1637                                       FolderView *folderview)
1638 {
1639         FolderItem *item;
1640
1641         item = gtk_ctree_node_get_row_data(ctree, node);
1642         g_return_if_fail(item != NULL);
1643         item->collapsed= TRUE;
1644         folderview_update_node(folderview, node);
1645 }
1646
1647 static void folderview_popup_close(GtkMenuShell *menu_shell,
1648                                    FolderView *folderview)
1649 {
1650         if (!folderview->opened) return;
1651
1652         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1653                                   folderview->opened);
1654         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1655 }
1656
1657 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1658                                    FolderView *folderview)
1659 {
1660         switch (column) {
1661         case COL_FOLDER:
1662                 prefs_common.folder_col_folder = width;
1663                 break;
1664         case COL_NEW:
1665                 prefs_common.folder_col_new = width;
1666                 break;
1667         case COL_UNREAD:
1668                 prefs_common.folder_col_unread = width;
1669                 break;
1670         case COL_TOTAL:
1671                 prefs_common.folder_col_total = width;
1672                 break;
1673         default:
1674                 break;
1675         }
1676 }
1677
1678 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1679                                              GtkCTreeNode *node,
1680                                              const gchar *name)
1681 {
1682         FolderItem *item;
1683
1684         if (!node)
1685                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1686         if (!node)
1687                 return NULL;
1688
1689         node = GTK_CTREE_ROW(node)->children;
1690
1691         while (node) {
1692                 item = gtk_ctree_node_get_row_data(ctree, node);
1693                 if (!folderview_compare_name(item, name))
1694                         return node;
1695                 node = GTK_CTREE_ROW(node)->sibling;
1696         }
1697
1698         return NULL;
1699 }
1700
1701 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1702                                       GtkWidget *widget)
1703 {
1704         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1705         FolderItem *item;
1706
1707         if (!folderview->selected) return;
1708
1709         summary_show(folderview->summaryview, NULL, FALSE);
1710
1711         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1712         g_return_if_fail(item != NULL);
1713         g_return_if_fail(item->folder != NULL);
1714
1715         if (action == 0)
1716                 folderview_check_new(item->folder);
1717         else
1718                 folderview_rescan_tree(item->folder);
1719 }
1720
1721 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1722                                      GtkWidget *widget)
1723 {
1724         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1725         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1726         FolderItem *item;
1727         FolderItem *new_item;
1728         gchar *new_folder;
1729         GtkCTreeNode *node;
1730
1731         if (!folderview->selected) return;
1732
1733         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1734         g_return_if_fail(item != NULL);
1735         g_return_if_fail(item->folder != NULL);
1736
1737         new_folder = input_dialog(_("New folder"),
1738                                   _("Input the name of new folder:"),
1739                                   _("NewFolder"));
1740         if (!new_folder) return;
1741
1742         if (item->folder->type != F_MBOX) {
1743                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1744                         alertpanel_error(_("`%c' can't be included in folder name."),
1745                                          G_DIR_SEPARATOR);
1746                         g_free(new_folder);
1747                         return;
1748                 }
1749         }
1750
1751         /* find whether the directory already exists */
1752         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1753                 alertpanel_error(_("The folder `%s' already exists."),
1754                                  new_folder);
1755                 g_free(new_folder);
1756                 return;
1757         }
1758
1759         new_item = item->folder->create_folder(item->folder, item, new_folder);
1760         if (!new_item) {
1761                 alertpanel_error(_("The folder `%s' could not be created."), 
1762                                  new_folder);
1763                 g_free(new_folder);
1764                 return;
1765         } 
1766         g_free(new_folder);
1767
1768         gtk_clist_freeze(GTK_CLIST(ctree));
1769
1770         text[COL_FOLDER] = new_item->name;
1771         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1772                                      FOLDER_SPACING,
1773                                      folderxpm, folderxpmmask,
1774                                      folderopenxpm, folderopenxpmmask,
1775                                      FALSE, FALSE);
1776         gtk_ctree_expand(ctree, folderview->selected);
1777         gtk_ctree_node_set_row_data(ctree, node, new_item);
1778         folderview_sort_folders(folderview, folderview->selected, item->folder);
1779
1780         gtk_clist_thaw(GTK_CLIST(ctree));
1781
1782         folder_write_list();
1783 }
1784
1785 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1786                                           GtkWidget *widget)
1787 {
1788         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1789         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1790         FolderItem *item;
1791         FolderItem *new_item;
1792         gchar *new_folder;
1793         GtkCTreeNode *node;
1794
1795         if (!folderview->selected) return;
1796
1797         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1798         g_return_if_fail(item != NULL);
1799         g_return_if_fail(item->folder != NULL);
1800
1801         new_folder = input_dialog(_("New folder"),
1802                                   _("Input the name of new folder:"),
1803                                   _("NewFolder"));
1804         if (!new_folder) return;
1805
1806         /* find whether the directory already exists */
1807         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1808                 alertpanel_error(_("The folder `%s' already exists."),
1809                                  new_folder);
1810                 g_free(new_folder);
1811                 return;
1812         }
1813
1814         new_item = item->folder->create_folder(item->folder, item, new_folder);
1815         g_free(new_folder);
1816         if (!new_item) return;
1817
1818         gtk_clist_freeze(GTK_CLIST(ctree));
1819
1820         text[COL_FOLDER] = new_item->name;
1821         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1822                                      FOLDER_SPACING,
1823                                      folderxpm, folderxpmmask,
1824                                      folderopenxpm, folderopenxpmmask,
1825                                      FALSE, FALSE);
1826         gtk_ctree_expand(ctree, folderview->selected);
1827         gtk_ctree_node_set_row_data(ctree, node, new_item);
1828         folderview_sort_folders(folderview, folderview->selected, item->folder);
1829
1830         gtk_clist_thaw(GTK_CLIST(ctree));
1831
1832         folder_write_list();
1833 }
1834
1835 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1836                                         GtkWidget *widget)
1837 {
1838         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1839         FolderItem *item;
1840         gchar *new_folder;
1841         gchar *message;
1842         gchar *old_path;
1843         gchar *new_path;
1844
1845         if (!folderview->selected) return;
1846
1847         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1848         g_return_if_fail(item != NULL);
1849         g_return_if_fail(item->path != NULL);
1850         g_return_if_fail(item->folder != NULL);
1851
1852         message = g_strdup_printf(_("Input new name for `%s':"),
1853                                   g_basename(item->path));
1854         new_folder = input_dialog(_("Rename folder"), message,
1855                                   g_basename(item->path));
1856         g_free(message);
1857         if (!new_folder) return;
1858
1859         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1860                 alertpanel_error(_("`%c' can't be included in folder name."),
1861                                  G_DIR_SEPARATOR);
1862                 g_free(new_folder);
1863                 return;
1864         }
1865
1866         if (folderview_find_by_name
1867                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1868                  new_folder)) {
1869                 alertpanel_error(_("The folder `%s' already exists."),
1870                                  new_folder);
1871                 g_free(new_folder);
1872                 return;
1873         }
1874
1875         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
1876
1877         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1878                 g_free(new_folder);
1879                 return;
1880         }
1881
1882         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
1883                 gchar *dirname;
1884                 dirname = g_dirname(item->path);
1885                 new_path = g_strconcat(dirname, G_DIR_SEPARATOR_S, new_folder, NULL);
1886                 g_free(dirname);
1887         } else
1888                 new_path = g_strdup(new_folder);
1889
1890         g_free(new_folder);
1891
1892         prefs_filter_rename_path(old_path, new_path);
1893
1894         if (FOLDER_TYPE(item->folder) == F_MH)
1895                 prefs_filtering_rename_path(old_path, new_path);
1896
1897         g_free(new_path);
1898
1899         gtk_clist_freeze(GTK_CLIST(ctree));
1900
1901         folderview_update_node(folderview, folderview->selected);
1902         folderview_sort_folders(folderview,
1903                                 GTK_CTREE_ROW(folderview->selected)->parent,
1904                                 item->folder);
1905         if (folderview->opened == folderview->selected ||
1906             gtk_ctree_is_ancestor(ctree,
1907                                   folderview->selected,
1908                                   folderview->opened)) {
1909                 GtkCTreeNode *node = folderview->opened;
1910                 folderview_unselect(folderview);
1911                 folderview_select_node(folderview, node);
1912         }
1913
1914         gtk_clist_thaw(GTK_CLIST(ctree));
1915
1916         folder_write_list();
1917 }
1918
1919 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
1920                                              guint action,
1921                                              GtkWidget *widget)
1922 {
1923         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1924         FolderItem *item;
1925         gchar *new_folder;
1926         gchar *message;
1927
1928         if (!folderview->selected) return;
1929
1930         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1931         g_return_if_fail(item != NULL);
1932         g_return_if_fail(item->path != NULL);
1933         g_return_if_fail(item->folder != NULL);
1934
1935         message = g_strdup_printf(_("Input new name for `%s':"),
1936                                   g_basename(item->path));
1937         new_folder = input_dialog(_("Rename folder"), message,
1938                                   g_basename(item->path));
1939         g_free(message);
1940         if (!new_folder) return;
1941
1942         if (folderview_find_by_name
1943                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1944                  new_folder)) {
1945                 alertpanel_error(_("The folder `%s' already exists."),
1946                                  new_folder);
1947                 g_free(new_folder);
1948                 return;
1949         }
1950
1951         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1952                 g_free(new_folder);
1953                 return;
1954         }
1955         g_free(new_folder);
1956
1957         gtk_clist_freeze(GTK_CLIST(ctree));
1958
1959         folderview_update_node(folderview, folderview->selected);
1960         folderview_sort_folders(folderview,
1961                                 GTK_CTREE_ROW(folderview->selected)->parent,
1962                                 item->folder);
1963         if (folderview->opened == folderview->selected) {
1964                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1965                         gtk_ctree_expand(ctree, folderview->opened);
1966                 summary_show(folderview->summaryview, item, FALSE);
1967         }
1968
1969         gtk_clist_thaw(GTK_CLIST(ctree));
1970
1971         folder_write_list();
1972 }
1973
1974 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1975                                         GtkWidget *widget)
1976 {
1977         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1978         FolderItem *item;
1979         gchar *message;
1980         AlertValue avalue;
1981         gchar *old_path;
1982
1983         if (!folderview->selected) return;
1984
1985         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1986         g_return_if_fail(item != NULL);
1987         g_return_if_fail(item->path != NULL);
1988         g_return_if_fail(item->folder != NULL);
1989
1990         message = g_strdup_printf
1991                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1992                    "Do you really want to delete?"),
1993                  g_basename(item->path));
1994         avalue = alertpanel(_("Delete folder"), message,
1995                             _("Yes"), _("+No"), NULL);
1996         g_free(message);
1997         if (avalue != G_ALERTDEFAULT) return;
1998
1999         Xstrdup_a(old_path, item->path, return);
2000
2001         if (item->folder->remove_folder(item->folder, item) < 0) {
2002                 alertpanel_error(_("Can't remove the folder `%s'."),
2003                                  item->path);
2004                 return;
2005         }
2006
2007         prefs_filter_delete_path(old_path);
2008
2009         if (FOLDER_TYPE(item->folder) == F_MH)
2010                 prefs_filtering_delete_path(old_path);
2011
2012         if (folderview->opened == folderview->selected ||
2013             gtk_ctree_is_ancestor(ctree,
2014                                   folderview->selected,
2015                                   folderview->opened)) {
2016                 summary_clear_all(folderview->summaryview);
2017                 folderview->opened = NULL;
2018         }
2019
2020         gtk_ctree_remove_node(ctree, folderview->selected);
2021         folder_write_list();
2022 }
2023
2024 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2025                                          GtkWidget *widget)
2026 {
2027         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2028         GtkCTreeNode *node;
2029         FolderItem *item;
2030         gchar *message;
2031         AlertValue avalue;
2032
2033         if (!folderview->selected) return;
2034         node = folderview->selected;
2035         item = gtk_ctree_node_get_row_data(ctree, node);
2036         g_return_if_fail(item != NULL);
2037         g_return_if_fail(item->folder != NULL);
2038         if (item->parent) return;
2039
2040         message = g_strdup_printf
2041                 (_("Really remove the mailbox `%s' ?\n"
2042                    "(The messages are NOT deleted from disk)"),
2043                  item->folder->name);
2044         avalue = alertpanel(_("Remove folder"), message,
2045                             _("Yes"), _("+No"), NULL);
2046         g_free(message);
2047         if (avalue != G_ALERTDEFAULT) return;
2048
2049         folder_destroy(item->folder);
2050         summary_clear_all(folderview->summaryview);
2051         folderview_unselect(folderview);
2052         gtk_ctree_remove_node(ctree, node);
2053         folder_write_list();
2054 }
2055
2056 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2057                                           GtkWidget *widget)
2058 {
2059         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2060         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2061         GtkCTreeNode *node;
2062         FolderItem *item;
2063         FolderItem *new_item;
2064         gchar *new_folder;
2065         gchar *p;
2066
2067         if (!folderview->selected) return;
2068
2069         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2070         g_return_if_fail(item != NULL);
2071         g_return_if_fail(item->folder != NULL);
2072         g_return_if_fail(item->folder->type == F_IMAP);
2073         g_return_if_fail(item->folder->account != NULL);
2074
2075         new_folder = input_dialog
2076                 (_("New folder"),
2077                  _("Input the name of new folder:\n"
2078                    "(if you want to create a folder to store subfolders,\n"
2079                    " append `/' at the end of the name)"),
2080                  _("NewFolder"));
2081         if (!new_folder) return;
2082
2083         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2084             *(p + 1) != '\0') {
2085                 alertpanel_error(_("`%c' can't be included in folder name."),
2086                                  G_DIR_SEPARATOR);
2087                 g_free(new_folder);
2088                 return;
2089         }
2090
2091         /* find whether the directory already exists */
2092         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2093                 alertpanel_error(_("The folder `%s' already exists."),
2094                                  new_folder);
2095                 g_free(new_folder);
2096                 return;
2097         }
2098
2099         new_item = item->folder->create_folder(item->folder, item, new_folder);
2100         if (!new_item) {
2101                 alertpanel_error(_("Can't create the folder `%s'."),
2102                                  new_folder);
2103                 g_free(new_folder);
2104                 return;
2105         }
2106         g_free(new_folder);
2107
2108         gtk_clist_freeze(GTK_CLIST(ctree));
2109
2110         text[COL_FOLDER] = new_item->name;
2111         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
2112                                      FOLDER_SPACING,
2113                                      folderxpm, folderxpmmask,
2114                                      folderopenxpm, folderopenxpmmask,
2115                                      FALSE, FALSE);
2116         gtk_ctree_expand(ctree, folderview->selected);
2117         gtk_ctree_node_set_row_data(ctree, node, new_item);
2118         folderview_sort_folders(folderview, folderview->selected, item->folder);
2119
2120         gtk_clist_thaw(GTK_CLIST(ctree));
2121
2122         folder_write_list();
2123 }
2124
2125 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
2126                                          GtkWidget *widget)
2127 {
2128         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2129         FolderItem *item;
2130         gchar *message;
2131         AlertValue avalue;
2132
2133         if (!folderview->selected) return;
2134
2135         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2136         g_return_if_fail(item != NULL);
2137         g_return_if_fail(item->folder != NULL);
2138         g_return_if_fail(item->folder->type == F_IMAP);
2139         g_return_if_fail(item->folder->account != NULL);
2140
2141         message = g_strdup_printf(_("Really delete folder `%s'?"),
2142                                   g_basename(item->path));
2143         avalue = alertpanel(_("Delete folder"), message,
2144                             _("Yes"), _("+No"), NULL);
2145         g_free(message);
2146         if (avalue != G_ALERTDEFAULT) return;
2147
2148         if (item->folder->remove_folder(item->folder, item) < 0) {
2149                 alertpanel_error(_("Can't remove the folder `%s'."),
2150                                  item->path);
2151                 if (folderview->opened == folderview->selected)
2152                         summary_show(folderview->summaryview,
2153                                      folderview->summaryview->folder_item,
2154                                      FALSE);
2155                 return;
2156         }
2157
2158         if (folderview->opened == folderview->selected ||
2159             gtk_ctree_is_ancestor(ctree,
2160                                   folderview->selected,
2161                                   folderview->opened)) {
2162                 summary_clear_all(folderview->summaryview);
2163                 folderview->opened = NULL;
2164         }
2165
2166         gtk_ctree_remove_node(ctree, folderview->selected);
2167         folder_write_list();
2168 }
2169
2170 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2171                                          GtkWidget *widget)
2172 {
2173         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2174         FolderItem *item;
2175         gchar *message;
2176         AlertValue avalue;
2177
2178         if (!folderview->selected) return;
2179
2180         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2181         g_return_if_fail(item != NULL);
2182         g_return_if_fail(item->folder != NULL);
2183         g_return_if_fail(item->folder->type == F_IMAP);
2184         g_return_if_fail(item->folder->account != NULL);
2185
2186         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"),
2187                                   item->folder->name);
2188         avalue = alertpanel(_("Delete IMAP4 account"), message,
2189                             _("Yes"), _("+No"), NULL);
2190         g_free(message);
2191
2192         if (avalue != G_ALERTDEFAULT) return;
2193
2194         if (folderview->opened == folderview->selected ||
2195             gtk_ctree_is_ancestor(ctree,
2196                                   folderview->selected,
2197                                   folderview->opened)) {
2198                 summary_clear_all(folderview->summaryview);
2199                 folderview->opened = NULL;
2200         }
2201
2202         account_destroy(item->folder->account);
2203         folder_destroy(item->folder);
2204         gtk_ctree_remove_node(ctree, folderview->selected);
2205         account_set_menu();
2206         main_window_reflect_prefs_all();
2207         folder_write_list();
2208 }
2209
2210 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2211                                          GtkWidget *widget)
2212 {
2213         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2214         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2215         GtkCTreeNode *servernode, *node;
2216         Folder *folder;
2217         FolderItem *item;
2218         FolderItem *rootitem;
2219         FolderItem *newitem;
2220         GSList *new_subscr;
2221         GSList *cur;
2222         GNode *gnode;
2223
2224         if (!folderview->selected) return;
2225
2226         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2227         g_return_if_fail(item != NULL);
2228         folder = item->folder;
2229         g_return_if_fail(folder != NULL);
2230         g_return_if_fail(folder->type == F_NEWS);
2231         g_return_if_fail(folder->account != NULL);
2232
2233         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2234                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2235         else
2236                 servernode = folderview->selected;
2237
2238         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2239
2240         new_subscr = grouplist_dialog(folder);
2241
2242         /* remove unsubscribed newsgroups */
2243         for (gnode = folder->node->children; gnode != NULL; ) {
2244                 GNode *next = gnode->next;
2245
2246                 item = FOLDER_ITEM(gnode->data);
2247                 if (g_slist_find_custom(new_subscr, item->path,
2248                                         (GCompareFunc)g_strcasecmp) != NULL) {
2249                         gnode = next;
2250                         continue;
2251                 }
2252
2253                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2254                 if (!node) {
2255                         gnode = next;
2256                         continue;
2257                 }
2258
2259                 if (folderview->opened == node) {
2260                         summary_clear_all(folderview->summaryview);
2261                         folderview->opened = NULL;
2262                 }
2263
2264                 folder_item_remove(item);
2265                 gtk_ctree_remove_node(ctree, node);
2266
2267                 gnode = next;
2268         }
2269
2270         gtk_clist_freeze(GTK_CLIST(ctree));
2271
2272         /* add subscribed newsgroups */
2273         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2274                 gchar *name = (gchar *)cur->data;
2275
2276                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2277                         continue;
2278
2279                 text[COL_FOLDER] = name;
2280                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2281                                              FOLDER_SPACING,
2282                                              folderxpm, folderxpmmask,
2283                                              folderopenxpm, folderopenxpmmask,
2284                                              FALSE, FALSE);
2285                 gtk_ctree_expand(ctree, servernode);
2286
2287                 newitem = folder_item_new(name, name);
2288                 folder_item_append(rootitem, newitem);
2289                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2290         }
2291
2292         gtk_ctree_sort_node(ctree, servernode);
2293         gtk_clist_thaw(GTK_CLIST(ctree));
2294
2295         slist_free_strings(new_subscr);
2296         g_slist_free(new_subscr);
2297
2298         folder_write_list();
2299 }
2300
2301 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2302                                         GtkWidget *widget)
2303 {
2304         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2305         FolderItem *item;
2306         gchar *message;
2307         AlertValue avalue;
2308
2309         if (!folderview->selected) return;
2310
2311         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2312         g_return_if_fail(item != NULL);
2313         g_return_if_fail(item->folder != NULL);
2314         g_return_if_fail(item->folder->type == F_NEWS);
2315         g_return_if_fail(item->folder->account != NULL);
2316
2317         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
2318                                   g_basename(item->path));
2319         avalue = alertpanel(_("Delete newsgroup"), message,
2320                             _("Yes"), _("+No"), NULL);
2321         g_free(message);
2322         if (avalue != G_ALERTDEFAULT) return;
2323
2324         if (folderview->opened == folderview->selected) {
2325                 summary_clear_all(folderview->summaryview);
2326                 folderview->opened = NULL;
2327         }
2328
2329         folder_item_remove(item);
2330         gtk_ctree_remove_node(ctree, folderview->selected);
2331         folder_write_list();
2332 }
2333
2334 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2335                                          GtkWidget *widget)
2336 {
2337         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2338         FolderItem *item;
2339         gchar *message;
2340         AlertValue avalue;
2341
2342         if (!folderview->selected) return;
2343
2344         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2345         g_return_if_fail(item != NULL);
2346         g_return_if_fail(item->folder != NULL);
2347         g_return_if_fail(item->folder->type == F_NEWS);
2348         g_return_if_fail(item->folder->account != NULL);
2349
2350         message = g_strdup_printf(_("Really delete news account `%s'?"),
2351                                   item->folder->name);
2352         avalue = alertpanel(_("Delete news account"), message,
2353                             _("Yes"), _("+No"), NULL);
2354         g_free(message);
2355
2356         if (avalue != G_ALERTDEFAULT) return;
2357
2358         if (folderview->opened == folderview->selected ||
2359             gtk_ctree_is_ancestor(ctree,
2360                                   folderview->selected,
2361                                   folderview->opened)) {
2362                 summary_clear_all(folderview->summaryview);
2363                 folderview->opened = NULL;
2364         }
2365
2366         account_destroy(item->folder->account);
2367         folder_destroy(item->folder);
2368         gtk_ctree_remove_node(ctree, folderview->selected);
2369         account_set_menu();
2370         main_window_reflect_prefs_all();
2371         folder_write_list();
2372 }
2373
2374 static void folderview_search_cb(FolderView *folderview, guint action,
2375                                  GtkWidget *widget)
2376 {
2377         summary_search(folderview->summaryview);
2378 }
2379
2380 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2381                                           GdkDragContext *context,
2382                                           gint            x,
2383                                           gint            y,
2384                                           guint           time,
2385                                           FolderView     *folderview)
2386 {
2387         gint row, column;
2388         FolderItem *item, *src_item;
2389         GtkCTreeNode *node = NULL;
2390         gboolean acceptable = FALSE;
2391
2392         if (gtk_clist_get_selection_info
2393                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2394                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2395                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2396                 src_item = folderview->summaryview->folder_item;
2397                 if (item && item->folder && item->path &&
2398                     src_item && src_item != item) {
2399                         switch (item->folder->type) {
2400                         case F_MH:
2401                         case F_IMAP:
2402                                 acceptable = TRUE;
2403                                 break;
2404                         default:
2405                                 break;
2406                         }
2407                 }
2408         }
2409
2410         if (acceptable) {
2411                 gtk_signal_handler_block_by_func
2412                         (GTK_OBJECT(widget),
2413                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2414                 gtk_ctree_select(GTK_CTREE(widget), node);
2415                 gtk_signal_handler_unblock_by_func
2416                         (GTK_OBJECT(widget),
2417                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2418                 gdk_drag_status(context, 
2419                                         (context->actions == GDK_ACTION_COPY ?
2420                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2421         } else {
2422                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2423                 gdk_drag_status(context, 0, time);
2424         }
2425
2426         return acceptable;
2427 }
2428
2429 static void folderview_drag_leave_cb(GtkWidget      *widget,
2430                                      GdkDragContext *context,
2431                                      guint           time,
2432                                      FolderView     *folderview)
2433 {
2434         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2435 }
2436
2437 static void folderview_drag_received_cb(GtkWidget        *widget,
2438                                         GdkDragContext   *drag_context,
2439                                         gint              x,
2440                                         gint              y,
2441                                         GtkSelectionData *data,
2442                                         guint             info,
2443                                         guint             time,
2444                                         FolderView       *folderview)
2445 {
2446         gint row, column;
2447         FolderItem *item, *src_item;
2448         GtkCTreeNode *node;
2449
2450         if (gtk_clist_get_selection_info
2451                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2452                 return;
2453
2454         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2455         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2456         src_item = folderview->summaryview->folder_item;
2457         if (item && src_item) {
2458                 switch (drag_context->action) {
2459                         case GDK_ACTION_COPY:
2460                                 summary_copy_selected_to(folderview->summaryview, item);
2461                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2462                                 break;
2463                         case GDK_ACTION_MOVE:
2464                         case GDK_ACTION_DEFAULT:
2465                         default:
2466                 if (src_item->folder->type != item->folder->type ||
2467                     (item->folder->type == F_IMAP &&
2468                      src_item->folder != item->folder))
2469                         summary_copy_selected_to(folderview->summaryview, item);
2470                 else
2471                         summary_move_selected_to(folderview->summaryview, item);
2472                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2473                 }
2474         } else
2475                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2476 }
2477
2478 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2479 {
2480         const FolderItem *item = a;
2481         const gchar *name = b;
2482
2483         if (!item->path) return -1;
2484         return strcmp2(g_basename(item->path), name);
2485 }
2486
2487 static void folderview_scoring_cb(FolderView *folderview, guint action,
2488                                    GtkWidget *widget)
2489 {
2490         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2491         FolderItem *item;
2492
2493         if (!folderview->selected) return;
2494
2495         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2496         g_return_if_fail(item != NULL);
2497         g_return_if_fail(item->folder != NULL);
2498
2499         prefs_scoring_open(item);
2500 }
2501
2502 static void folderview_processing_cb(FolderView *folderview, guint action,
2503                                      GtkWidget *widget)
2504 {
2505         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2506         FolderItem *item;
2507
2508         if (!folderview->selected) return;
2509
2510         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2511         g_return_if_fail(item != NULL);
2512         g_return_if_fail(item->folder != NULL);
2513
2514         prefs_filtering_open(item);
2515 }
2516
2517 static void folderview_property_cb(FolderView *folderview, guint action, 
2518                                    GtkWidget *widget) 
2519 {
2520         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2521         FolderItem *item;
2522
2523         if (!folderview->selected) return;
2524
2525         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2526         g_return_if_fail(item != NULL);
2527         g_return_if_fail(item->folder != NULL);
2528
2529         prefs_folder_item_create(folderview, item);
2530 }
2531
2532 void folderview_set_target_folder_color(gint color_op) 
2533 {
2534         gint firstone = 1;
2535         GList *list;
2536         FolderView *folderview;
2537
2538         for (list = folderview_list; list != NULL; list = list->next) {
2539                 folderview = (FolderView *)list->data;
2540                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2541                 if (firstone) {
2542                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2543                                 folderview->color_op;
2544                         firstone = 0;
2545                 }
2546         }
2547 }
2548
2549 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2550 {
2551         folderview_init(folderview);
2552         folderview_set_all();
2553 }