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