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