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