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