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