added newsgroup list dialog
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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
57 #include "pixmaps/inbox.xpm"
58 #include "pixmaps/outbox.xpm"
59 #include "pixmaps/dir-close.xpm"
60 #include "pixmaps/dir-open.xpm"
61 #include "pixmaps/trash.xpm"
62
63 typedef enum
64 {
65         COL_FOLDER      = 0,
66         COL_NEW         = 1,
67         COL_UNREAD      = 2,
68         COL_TOTAL       = 3
69 } FolderColumnPos;
70
71 #define N_FOLDER_COLS           4
72 #define COL_FOLDER_WIDTH        150
73 #define COL_NUM_WIDTH           32
74
75 #define STATUSBAR_PUSH(mainwin, str) \
76 { \
77         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
78                            mainwin->folderview_cid, str); \
79         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
80 }
81
82 #define STATUSBAR_POP(mainwin) \
83 { \
84         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
85                           mainwin->folderview_cid); \
86 }
87
88 static GList *folderview_list = NULL;
89
90 static GdkFont *normalfont;
91 static GdkFont *boldfont;
92
93 static GdkPixmap *inboxxpm;
94 static GdkBitmap *inboxxpmmask;
95 static GdkPixmap *outboxxpm;
96 static GdkBitmap *outboxxpmmask;
97 static GdkPixmap *folderxpm;
98 static GdkBitmap *folderxpmmask;
99 static GdkPixmap *folderopenxpm;
100 static GdkBitmap *folderopenxpmmask;
101 static GdkPixmap *trashxpm;
102 static GdkBitmap *trashxpmmask;
103
104 static void folderview_select_node       (FolderView    *folderview,
105                                           GtkCTreeNode  *node);
106 static void folderview_set_folders       (FolderView    *folderview);
107 static void folderview_sort_folders      (FolderView    *folderview,
108                                           GtkCTreeNode  *root,
109                                           Folder        *folder);
110 static void folderview_append_folder     (FolderView    *folderview,
111                                           Folder        *folder);
112 static void folderview_update_node       (FolderView    *folderview,
113                                           GtkCTreeNode  *node);
114
115 static GtkCTreeNode *folderview_find_by_name    (GtkCTree       *ctree,
116                                                  GtkCTreeNode   *node,
117                                                  const gchar    *name);
118
119 static gint folderview_compare_name     (gconstpointer   a,
120                                          gconstpointer   b);
121
122 /* callback functions */
123 static void folderview_button_pressed   (GtkWidget      *ctree,
124                                          GdkEventButton *event,
125                                          FolderView     *folderview);
126 static void folderview_button_released  (GtkWidget      *ctree,
127                                          GdkEventButton *event,
128                                          FolderView     *folderview);
129 static void folderview_key_pressed      (GtkWidget      *widget,
130                                          GdkEventKey    *event,
131                                          FolderView     *folderview);
132 static void folderview_selected         (GtkCTree       *ctree,
133                                          GtkCTreeNode   *row,
134                                          gint            column,
135                                          FolderView     *folderview);
136 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
137                                          FolderView     *folderview);
138 static void folderview_col_resized      (GtkCList       *clist,
139                                          gint            column,
140                                          gint            width,
141                                          FolderView     *folderview);
142
143 static void folderview_new_folder_cb    (FolderView     *folderview,
144                                          guint           action,
145                                          GtkWidget      *widget);
146 static void folderview_rename_folder_cb (FolderView     *folderview,
147                                          guint           action,
148                                          GtkWidget      *widget);
149 static void folderview_delete_folder_cb (FolderView     *folderview,
150                                          guint           action,
151                                          GtkWidget      *widget);
152 static void folderview_remove_mailbox_cb(FolderView     *folderview,
153                                          guint           action,
154                                          GtkWidget      *widget);
155
156 static void folderview_new_imap_folder_cb(FolderView    *folderview,
157                                           guint          action,
158                                           GtkWidget     *widget);
159 static void folderview_rm_imap_folder_cb (FolderView    *folderview,
160                                           guint          action,
161                                           GtkWidget     *widget);
162 static void folderview_rm_imap_server_cb (FolderView    *folderview,
163                                           guint          action,
164                                           GtkWidget     *widget);
165
166 static void folderview_new_news_group_cb(FolderView     *folderview,
167                                          guint           action,
168                                          GtkWidget      *widget);
169 static void folderview_rm_news_group_cb (FolderView     *folderview,
170                                          guint           action,
171                                          GtkWidget      *widget);
172 static void folderview_rm_news_server_cb(FolderView     *folderview,
173                                          guint           action,
174                                          GtkWidget      *widget);
175
176 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
177                                           GdkDragContext *context,
178                                           gint            x,
179                                           gint            y,
180                                           guint           time,
181                                           FolderView     *folderview);
182 static void folderview_drag_leave_cb     (GtkWidget        *widget,
183                                           GdkDragContext   *context,
184                                           guint             time,
185                                           FolderView       *folderview);
186 static void folderview_drag_received_cb  (GtkWidget        *widget,
187                                           GdkDragContext   *drag_context,
188                                           gint              x,
189                                           gint              y,
190                                           GtkSelectionData *data,
191                                           guint             info,
192                                           guint             time,
193                                           FolderView       *folderview);
194
195 static GtkItemFactoryEntry folderview_mail_popup_entries[] =
196 {
197         {N_("/Create _new folder..."),  NULL, folderview_new_folder_cb,    0, NULL},
198         {N_("/_Rename folder..."),      NULL, folderview_rename_folder_cb, 0, NULL},
199         {N_("/_Delete folder"),         NULL, folderview_delete_folder_cb, 0, NULL},
200         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
201         {N_("/Remove _mailbox"),        NULL, folderview_remove_mailbox_cb, 0, NULL},
202         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
203         {N_("/_Property..."),           NULL, NULL, 0, NULL}
204 };
205
206 static GtkItemFactoryEntry folderview_imap_popup_entries[] =
207 {
208         {N_("/Create _new folder..."),  NULL, folderview_new_imap_folder_cb, 0, NULL},
209         {N_("/_Rename folder..."),      NULL, NULL, 0, NULL},
210         {N_("/_Delete folder"),         NULL, folderview_rm_imap_folder_cb, 0, NULL},
211         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
212         {N_("/Remove _IMAP4 server"),   NULL, folderview_rm_imap_server_cb, 0, NULL},
213         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
214         {N_("/_Property..."),           NULL, NULL, 0, NULL}
215 };
216
217 static GtkItemFactoryEntry folderview_news_popup_entries[] =
218 {
219         {N_("/_Subscribe to newsgroup..."),
220                                          NULL, folderview_new_news_group_cb, 0, NULL},
221         {N_("/_Remove newsgroup"),       NULL, folderview_rm_news_group_cb, 0, NULL},
222         {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
223         {N_("/Remove _news server"),     NULL, folderview_rm_news_server_cb, 0, NULL},
224         {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
225         {N_("/_Property..."),            NULL, NULL, 0, NULL}
226 };
227
228
229 FolderView *folderview_create(void)
230 {
231         FolderView *folderview;
232         GtkWidget *scrolledwin;
233         GtkWidget *ctree;
234         gchar *titles[N_FOLDER_COLS] = {_("Folder"), _("New"),
235                                         _("Unread"), _("#")};
236         GtkWidget *mail_popup;
237         GtkWidget *news_popup;
238         GtkWidget *imap_popup;
239         GtkItemFactory *mail_factory;
240         GtkItemFactory *news_factory;
241         GtkItemFactory *imap_factory;
242         gint n_entries;
243         gint i;
244
245         debug_print(_("Creating folder view...\n"));
246         folderview = g_new0(FolderView, 1);
247
248         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
249         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
250                                        GTK_POLICY_AUTOMATIC,
251                                        GTK_POLICY_ALWAYS);
252         gtk_widget_set_usize(scrolledwin,
253                              prefs_common.folderview_width,
254                              prefs_common.folderview_height);
255         //                   COL_FOLDER_WIDTH + COL_NUM_WIDTH - 3, -1);
256
257         ctree = gtk_ctree_new_with_titles(N_FOLDER_COLS, COL_FOLDER, titles);
258         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
259         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
260         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_NEW,
261                                            GTK_JUSTIFY_RIGHT);
262         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_UNREAD,
263                                            GTK_JUSTIFY_RIGHT);
264         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_TOTAL,
265                                            GTK_JUSTIFY_RIGHT);
266         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_FOLDER,
267                                    prefs_common.folder_col_folder);
268         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_NEW,
269                                    prefs_common.folder_col_new);
270         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_UNREAD,        
271                                    prefs_common.folder_col_unread);
272         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_TOTAL,
273                                    prefs_common.folder_col_total);
274         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
275         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
276                                      GTK_CTREE_EXPANDER_SQUARE);
277         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
278         //gtk_clist_set_reorderable(GTK_CLIST(ctree), TRUE);
279         /* don't let title buttons take key focus */
280         for (i = 0; i < N_FOLDER_COLS; i++)
281                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
282                                        GTK_CAN_FOCUS);
283
284         /* popup menu */
285         n_entries = sizeof(folderview_mail_popup_entries) /
286                 sizeof(folderview_mail_popup_entries[0]);
287         mail_popup = menu_create_items(folderview_mail_popup_entries,
288                                        n_entries,
289                                        "<MailFolder>", &mail_factory,
290                                        folderview);
291         n_entries = sizeof(folderview_imap_popup_entries) /
292                 sizeof(folderview_imap_popup_entries[0]);
293         imap_popup = menu_create_items(folderview_imap_popup_entries,
294                                        n_entries,
295                                        "<IMAPFolder>", &imap_factory,
296                                        folderview);
297         n_entries = sizeof(folderview_news_popup_entries) /
298                 sizeof(folderview_news_popup_entries[0]);
299         news_popup = menu_create_items(folderview_news_popup_entries,
300                                        n_entries,
301                                        "<NewsFolder>", &news_factory,
302                                        folderview);
303
304         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
305                            GTK_SIGNAL_FUNC(folderview_key_pressed),
306                            folderview);
307         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
308                            GTK_SIGNAL_FUNC(folderview_button_pressed),
309                            folderview);
310         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
311                            GTK_SIGNAL_FUNC(folderview_button_released),
312                            folderview);
313         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
314                            GTK_SIGNAL_FUNC(folderview_selected), folderview);
315         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
316                            GTK_SIGNAL_FUNC(folderview_col_resized),
317                            folderview);
318
319         gtk_signal_connect(GTK_OBJECT(mail_popup), "selection_done",
320                            GTK_SIGNAL_FUNC(folderview_popup_close),
321                            folderview);
322         gtk_signal_connect(GTK_OBJECT(imap_popup), "selection_done",
323                            GTK_SIGNAL_FUNC(folderview_popup_close),
324                            folderview);
325         gtk_signal_connect(GTK_OBJECT(news_popup), "selection_done",
326                            GTK_SIGNAL_FUNC(folderview_popup_close),
327                            folderview);
328
329         /* drop callback */
330         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL &
331                           ~GTK_DEST_DEFAULT_HIGHLIGHT,
332                           summary_drag_types, 1,
333                           GDK_ACTION_MOVE);
334         gtk_signal_connect(GTK_OBJECT(ctree), "drag_motion",
335                            GTK_SIGNAL_FUNC(folderview_drag_motion_cb),
336                            folderview);
337         gtk_signal_connect(GTK_OBJECT(ctree), "drag_leave",
338                            GTK_SIGNAL_FUNC(folderview_drag_leave_cb),
339                            folderview);
340         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_received",
341                            GTK_SIGNAL_FUNC(folderview_drag_received_cb),
342                            folderview);
343
344         folderview->scrolledwin  = scrolledwin;
345         folderview->ctree        = ctree;
346         folderview->mail_popup   = mail_popup;
347         folderview->mail_factory = mail_factory;
348         folderview->imap_popup   = imap_popup;
349         folderview->imap_factory = imap_factory;
350         folderview->news_popup   = news_popup;
351         folderview->news_factory = news_factory;
352
353         gtk_widget_show_all(scrolledwin);
354
355         folderview_list = g_list_append(folderview_list, folderview);
356
357         return folderview;
358 }
359
360 void folderview_init(FolderView *folderview)
361 {
362         GtkWidget *ctree = folderview->ctree;
363
364         PIXMAP_CREATE(ctree, inboxxpm, inboxxpmmask, inbox_xpm);
365         PIXMAP_CREATE(ctree, outboxxpm, outboxxpmmask, outbox_xpm);
366         PIXMAP_CREATE(ctree, folderxpm, folderxpmmask, DIRECTORY_CLOSE_XPM);
367         PIXMAP_CREATE(ctree, folderopenxpm, folderopenxpmmask,
368                       DIRECTORY_OPEN_XPM);
369         PIXMAP_CREATE(ctree, trashxpm, trashxpmmask, trash_xpm);
370
371         if (!normalfont)
372                 normalfont = gdk_fontset_load(NORMAL_FONT);
373         if (!boldfont)
374                 boldfont = gdk_fontset_load(BOLD_FONT);
375 }
376
377 void folderview_set(FolderView *folderview)
378 {
379         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
380         MainWindow *mainwin = folderview->mainwin;
381
382         debug_print(_("Setting folder info...\n"));
383         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
384
385         main_window_cursor_wait(mainwin);
386
387         folderview->selected = NULL;
388         folderview->opened = NULL;
389
390         gtk_clist_freeze(GTK_CLIST(ctree));
391         gtk_clist_clear(GTK_CLIST(ctree));
392         gtk_clist_thaw(GTK_CLIST(ctree));
393         gtk_clist_freeze(GTK_CLIST(ctree));
394
395         folderview_set_folders(folderview);
396
397         gtk_clist_thaw(GTK_CLIST(ctree));
398         main_window_cursor_normal(mainwin);
399         STATUSBAR_POP(mainwin);
400 }
401
402 void folderview_select(FolderView *folderview, FolderItem *item)
403 {
404         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
405         GtkCTreeNode *node;
406
407         if (!item) return;
408
409         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
410         if (node) folderview_select_node(folderview, node);
411 }
412
413 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
414 {
415         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
416
417         g_return_if_fail(node != NULL);
418
419         folderview->open_folder = TRUE;
420         gtk_ctree_select(ctree, node);
421         gtkut_ctree_set_focus_row(ctree, node);
422         if (folderview->summaryview->messages > 0)
423                 gtk_widget_grab_focus(folderview->summaryview->ctree);
424         else
425                 gtk_widget_grab_focus(folderview->ctree);
426 }
427
428 void folderview_unselect(FolderView *folderview)
429 {
430         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
431                 gtk_ctree_collapse
432                         (GTK_CTREE(folderview->ctree), folderview->opened);
433
434         folderview->selected = folderview->opened = NULL;
435 }
436
437 void folderview_select_next_unread(FolderView *folderview)
438 {
439         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
440         GtkCTreeNode *node;
441         FolderItem *item;
442
443         if (folderview->opened)
444                 node = GTK_CTREE_NODE_NEXT(folderview->opened);
445         else
446                 node = gtk_ctree_node_nth(ctree, 1);
447
448         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
449                 item = gtk_ctree_node_get_row_data(ctree, node);
450                 if (item && (item->unread > 0 || item->new > 0) &&
451                     item->stype != F_TRASH) {
452                         folderview_select_node(folderview, node);
453                         return;
454                 }
455         }
456         if (!folderview->opened) return;
457         /* search again from the first node */
458         for (node = gtk_ctree_node_nth(ctree, 1); node != NULL;
459              node = GTK_CTREE_NODE_NEXT(node)) {
460                 item = gtk_ctree_node_get_row_data(ctree, node);
461                 if (item && (item->unread > 0 || item->new > 0) &&
462                     item->stype != F_TRASH) {
463                         folderview_select_node(folderview, node);
464                         return;
465                 }
466         }
467 }
468
469 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row,
470                                gint new, gint unread, gint total)
471 {
472         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
473         static GtkCTreeNode *prev_row = NULL;
474         FolderItem *item;
475
476         if (!row) return;
477
478         item = gtk_ctree_node_get_row_data(ctree, row);
479         if (!item) return;
480         if (prev_row     == row    &&
481             item->new    == new    &&
482             item->unread == unread &&
483             item->total  == total)
484                 return;
485
486         prev_row = row;
487
488         item->new    = new;
489         item->unread = unread;
490         item->total  = total;
491
492         folderview_update_node(folderview, row);
493 }
494
495 static void folderview_set_folders(FolderView *folderview)
496 {
497         GList *list;
498
499         list = folder_get_list();
500
501         for (; list != NULL; list = list->next)
502                 folderview_append_folder(folderview, FOLDER(list->data));
503 }
504
505 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
506                                       gpointer data)
507 {
508         GList *list;
509
510         for (list = folderview_list; list != NULL; list = list->next) {
511                 FolderView *folderview = (FolderView *)list->data;
512                 MainWindow *mainwin = folderview->mainwin;
513                 gchar *str;
514
515                 if (item->path)
516                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
517                                               LOCAL_FOLDER(folder)->rootpath,
518                                               G_DIR_SEPARATOR,
519                                               item->path);
520                 else
521                         str = g_strdup_printf(_("Scanning folder %s ..."),
522                                               LOCAL_FOLDER(folder)->rootpath);
523
524                 STATUSBAR_PUSH(mainwin, str);
525                 STATUSBAR_POP(mainwin);
526                 g_free(str);
527         }
528 }
529
530 static GtkWidget *label_window_create(const gchar *str)
531 {
532         GtkWidget *window;
533         GtkWidget *label;
534
535         window = gtk_window_new(GTK_WINDOW_DIALOG);
536         gtk_widget_set_usize(window, 380, 60);
537         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
538         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
539         gtk_window_set_title(GTK_WINDOW(window), str);
540         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
541         gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
542         manage_window_set_transient(GTK_WINDOW(window));
543
544         label = gtk_label_new(str);
545         gtk_container_add(GTK_CONTAINER(window), label);
546         gtk_widget_show(label);
547
548         gtk_widget_show_now(window);
549
550         return window;
551 }
552
553 void folderview_update_all(void)
554 {
555         GList *list;
556         GtkWidget *window;
557
558         window = label_window_create(_("Updating all folders..."));
559
560         list = folder_get_list();
561         for (; list != NULL; list = list->next) {
562                 Folder *folder = list->data;
563
564                 if (!folder->scan_tree) continue;
565                 folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
566                 folder->scan_tree(folder);
567                 folder_set_ui_func(folder, NULL, NULL);
568         }
569
570         folder_write_list();
571
572         for (list = folderview_list; list != NULL; list = list->next) {
573                 FolderView *folderview = (FolderView *)list->data;
574
575                 folderview_set(folderview);
576         }
577
578         gtk_widget_destroy(window);
579 }
580
581 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
582 {
583         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
584         GtkStyle *style, *prev_style, *ctree_style;
585         FolderItem *item;
586         GdkPixmap *xpm, *openxpm;
587         GdkBitmap *mask, *openmask;
588         gchar *name;
589
590         item = gtk_ctree_node_get_row_data(ctree, node);
591         g_return_if_fail(item != NULL);
592
593         switch (item->stype) {
594         case F_INBOX:
595                 xpm = openxpm = inboxxpm;
596                 mask = openmask = inboxxpmmask;
597                 name = g_strdup(_("Inbox"));
598                 break;
599         case F_OUTBOX:
600                 xpm = openxpm = outboxxpm;
601                 mask = openmask = outboxxpmmask;
602                 name = g_strdup(_("Outbox"));
603                 break;
604         case F_QUEUE:
605                 xpm = openxpm = outboxxpm;
606                 mask = openmask = outboxxpmmask;
607                 name = g_strdup(_("Queue"));
608                 break;
609         case F_TRASH:
610                 xpm = openxpm = trashxpm;
611                 mask = openmask = trashxpmmask;
612                 name = g_strdup(_("Trash"));
613                 break;
614         case F_DRAFT:
615                 xpm = folderxpm;
616                 mask = folderxpmmask;
617                 openxpm = folderopenxpm;
618                 openmask = folderopenxpmmask;
619                 name = g_strdup(_("Draft"));
620                 break;
621         default:
622                 xpm = folderxpm;
623                 mask = folderxpmmask;
624                 openxpm = folderopenxpm;
625                 openmask = folderopenxpmmask;
626                 if (!item->parent) {
627                         switch (item->folder->type) {
628                         case F_MH:
629                                 name = " (MH)"; break;
630                         case F_IMAP:
631                                 name = " (IMAP4)"; break;
632                         case F_NEWS:
633                                 name = " (News)"; break;
634                         default:
635                                 name = "";
636                         }
637                         name = g_strconcat(item->name, name, NULL);
638                 } else
639                         name = g_strdup(item->name);
640         }
641         if (item->unread > 0 && prefs_common.display_folder_unread) {
642                 gchar *str;
643
644                 str = g_strdup_printf("%s (%d)", name, item->unread);
645                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
646                                         xpm, mask, openxpm, openmask,
647                                         FALSE, GTK_CTREE_ROW(node)->expanded);
648                 g_free(str);
649         } else
650                 gtk_ctree_set_node_info(ctree, node, name, FOLDER_SPACING,
651                                         xpm, mask, openxpm, openmask,
652                                         FALSE, GTK_CTREE_ROW(node)->expanded);
653         g_free(name);
654
655         if (!item->parent) {
656                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
657                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
658                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
659                 return;
660         } else {
661                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
662                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
663                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
664         }
665
666         if (item->stype == F_TRASH) return;
667
668         ctree_style = gtk_widget_get_style(GTK_WIDGET(ctree));
669         prev_style = gtk_ctree_node_get_row_style(ctree, node);
670         if (!prev_style)
671                 prev_style = ctree_style;
672         style = gtk_style_copy(prev_style);
673
674         if (style) {
675                 gboolean use_bold, use_color;
676
677                 if (item->stype == F_QUEUE) {
678                         /* highlight queue folder if there are any messages */
679                         use_bold = use_color = (item->total > 0);
680                 } else {
681                         /* if unread messages exist, print with bold font */
682                         use_bold = (item->unread > 0);
683                         /* if new messages exist, print with colored letter */
684                         use_color = (item->new > 0);
685                 }
686
687                 if (use_bold && boldfont)
688                         style->font = boldfont;
689                 else
690                         style->font = ctree_style->font;
691
692                 if (use_color) {
693                         style->fg[GTK_STATE_NORMAL]   = folderview->color_new;
694                         style->fg[GTK_STATE_SELECTED] = folderview->color_new;
695                 } else {
696                         style->fg[GTK_STATE_NORMAL] =
697                                 ctree_style->fg[GTK_STATE_NORMAL];
698                         style->fg[GTK_STATE_SELECTED] =
699                                 ctree_style->fg[GTK_STATE_SELECTED];
700                 }
701
702                 gtk_ctree_node_set_row_style(ctree, node, style);
703         }
704 }
705
706 void folderview_update_item(FolderItem *item, gboolean update_summary)
707 {
708         GList *list;
709         FolderView *folderview;
710         GtkCTree *ctree;
711         GtkCTreeNode *node;
712
713         g_return_if_fail(item != NULL);
714
715         for (list = folderview_list; list != NULL; list = list->next) {
716                 folderview = (FolderView *)list->data;
717                 ctree = GTK_CTREE(folderview->ctree);
718
719                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
720                 if (node) {
721                         folderview_update_node(folderview, node);
722                         if (update_summary && folderview->opened == node)
723                                 summary_show(folderview->summaryview,
724                                              item, FALSE);
725                 }
726         }
727 }
728
729 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
730                                                 gpointer data)
731 {
732         folderview_update_item((FolderItem *)key, FALSE);
733 }
734
735 void folderview_update_item_foreach(GHashTable *table)
736 {
737         g_hash_table_foreach(table, folderview_update_item_foreach_func, NULL);
738 }
739
740 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
741                                       GNode *gnode, GtkCTreeNode *cnode,
742                                       gpointer data)
743 {
744         FolderView *folderview = (FolderView *)data;
745         FolderItem *item = FOLDER_ITEM(gnode->data);
746
747         g_return_val_if_fail(item != NULL, FALSE);
748
749         gtk_ctree_node_set_row_data(ctree, cnode, item);
750         folderview_update_node(folderview, cnode);
751
752         return TRUE;
753 }
754
755 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
756                                    gpointer data)
757 {
758         if (GTK_CTREE_ROW(node)->children)
759                 gtk_ctree_expand(ctree, node);
760 }
761
762 #define SET_SPECIAL_FOLDER(ctree, item) \
763 { \
764         if (item) { \
765                 GtkCTreeNode *node, *sibling; \
766  \
767                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
768                 if (!node) \
769                         g_warning("%s not found.\n", item->path); \
770                 else { \
771                         if (!prev) \
772                                 sibling = GTK_CTREE_ROW(root)->children; \
773                         else \
774                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
775                         if (node != sibling) \
776                                 gtk_ctree_move(ctree, node, root, sibling); \
777                 } \
778  \
779                 prev = node; \
780         } \
781 }
782
783 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
784                                     Folder *folder)
785 {
786         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
787         GtkCTreeNode *prev = NULL;
788
789         gtk_ctree_sort_recursive(ctree, root);
790
791         if (GTK_CTREE_ROW(root)->parent) return;
792
793         SET_SPECIAL_FOLDER(ctree, folder->inbox);
794         SET_SPECIAL_FOLDER(ctree, folder->outbox);
795         SET_SPECIAL_FOLDER(ctree, folder->draft);
796         SET_SPECIAL_FOLDER(ctree, folder->queue);
797         SET_SPECIAL_FOLDER(ctree, folder->trash);
798 }
799
800 static void folderview_append_folder(FolderView *folderview, Folder *folder)
801 {
802         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
803         GtkCTreeNode *root;
804
805         g_return_if_fail(folder != NULL);
806
807         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
808                                       folderview_gnode_func, folderview);
809         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
810                                 folderview);
811         folderview_sort_folders(folderview, root, folder);
812 }
813
814 void folderview_new_folder(FolderView *folderview)
815 {
816         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
817         FolderItem *item;
818
819         if (!folderview->selected) return;
820
821         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
822         g_return_if_fail(item != NULL);
823         g_return_if_fail(item->folder != NULL);
824
825         switch (item->folder->type) {
826         case F_MH:
827         case F_MBOX:
828         case F_MAILDIR:
829                 folderview_new_folder_cb(folderview, 0, NULL);
830                 break;
831         case F_IMAP:
832                 folderview_new_imap_folder_cb(folderview, 0, NULL);
833                 break;
834         case F_NEWS:
835         default:
836                 break;
837         }
838 }
839
840 void folderview_rename_folder(FolderView *folderview)
841 {
842         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
843         FolderItem *item;
844
845         if (!folderview->selected) return;
846
847         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
848         g_return_if_fail(item != NULL);
849         g_return_if_fail(item->folder != NULL);
850         if (!item->path) return;
851         if (item->stype != F_NORMAL) return;
852
853         switch (item->folder->type) {
854         case F_MH:
855         case F_MBOX:
856         case F_MAILDIR:
857                 folderview_rename_folder_cb(folderview, 0, NULL);
858                 break;
859         case F_IMAP:
860         case F_NEWS:
861         default:
862                 break;
863         }
864 }
865
866 void folderview_delete_folder(FolderView *folderview)
867 {
868         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
869         FolderItem *item;
870
871         if (!folderview->selected) return;
872
873         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
874         g_return_if_fail(item != NULL);
875         g_return_if_fail(item->folder != NULL);
876         if (!item->path) return;
877         if (item->stype != F_NORMAL) return;
878
879         switch (item->folder->type) {
880         case F_MH:
881         case F_MBOX:
882         case F_MAILDIR:
883                 folderview_delete_folder_cb(folderview, 0, NULL);
884                 break;
885         case F_IMAP:
886                 folderview_rm_imap_folder_cb(folderview, 0, NULL);
887         case F_NEWS:
888         default:
889                 break;
890         }
891 }
892
893
894 /* callback functions */
895
896 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
897                                       FolderView *folderview)
898 {
899         GtkCList *clist = GTK_CLIST(ctree);
900         gint prev_row = -1, row = -1, column = -1;
901         FolderItem *item;
902         Folder *folder;
903
904         if (!event) return;
905
906         if (event->button == 1) {
907                 folderview->open_folder = TRUE;
908                 return;
909         }
910
911         if (event->button == 2 || event->button == 3) {
912                 /* right clicked */
913                 if (clist->selection) {
914                         GtkCTreeNode *node;
915
916                         node = GTK_CTREE_NODE(clist->selection->data);
917                         if (node)
918                                 prev_row = gtkut_ctree_get_nth_from_node
919                                         (GTK_CTREE(ctree), node);
920                 }
921
922                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
923                                                   &row, &column))
924                         return;
925                 if (prev_row != row) {
926                         gtk_clist_unselect_all(clist);
927                         if (event->button == 2)
928                                 folderview_select_node
929                                         (folderview,
930                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
931                                                             row));
932                         else
933                                 gtk_clist_select_row(clist, row, column);
934                 }
935         }
936
937         if (event->button != 3) return;
938
939         item = gtk_clist_get_row_data(clist, row);
940         g_return_if_fail(item != NULL);
941         g_return_if_fail(item->folder != NULL);
942         folder = item->folder;
943
944         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->mail_popup));
945         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->imap_popup));
946         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->news_popup));
947
948         if (folder->type == F_MH && item->parent == NULL) {
949                 menu_set_sensitive(folderview->mail_factory,
950                                    "/Create new folder...", TRUE);
951                 menu_set_sensitive(folderview->mail_factory,
952                                    "/Remove mailbox", TRUE);
953         } else if (folder->type == F_MH && item->stype != F_NORMAL) {
954                 menu_set_sensitive(folderview->mail_factory,
955                                    "/Create new folder...", TRUE);
956         } else if (folder->type == F_MH) {
957                 menu_set_sensitive(folderview->mail_factory,
958                                    "/Create new folder...", TRUE);
959                 menu_set_sensitive(folderview->mail_factory,
960                                    "/Rename folder...", TRUE);
961                 menu_set_sensitive(folderview->mail_factory,
962                                    "/Delete folder", TRUE);
963         } else if (folder->type == F_IMAP && item->parent == NULL) {
964                 menu_set_sensitive(folderview->imap_factory,
965                                    "/Create new folder...", TRUE);
966                 menu_set_sensitive(folderview->imap_factory,
967                                    "/Remove IMAP4 server", TRUE);
968         } else if (folder->type == F_IMAP && item->parent != NULL) {
969                 menu_set_sensitive(folderview->imap_factory,
970                                    "/Create new folder...", TRUE);
971                 menu_set_sensitive(folderview->imap_factory,
972                                    "/Delete folder", TRUE);
973         } else if (folder->type == F_NEWS && item->parent == NULL) {
974                 menu_set_sensitive(folderview->news_factory,
975                                    "/Subscribe to newsgroup...", TRUE);
976                 menu_set_sensitive(folderview->news_factory,
977                                    "/Remove news server", TRUE);
978         } else if (folder->type == F_NEWS && item->parent != NULL) {
979                 menu_set_sensitive(folderview->news_factory,
980                                    "/Subscribe to newsgroup...", TRUE);
981                 menu_set_sensitive(folderview->news_factory,
982                                    "/Remove newsgroup", TRUE);
983         }
984
985         if (folder->type == F_MH)
986                 gtk_menu_popup(GTK_MENU(folderview->mail_popup), NULL, NULL,
987                                NULL, NULL, event->button, event->time);
988         else if (folder->type == F_IMAP)
989                 gtk_menu_popup(GTK_MENU(folderview->imap_popup), NULL, NULL,
990                                NULL, NULL, event->button, event->time);
991         else if (folder->type == F_NEWS)
992                 gtk_menu_popup(GTK_MENU(folderview->news_popup), NULL, NULL,
993                                NULL, NULL, event->button, event->time);
994 }
995
996 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
997                                        FolderView *folderview)
998 {
999         if (!event) return;
1000
1001         if (event->button == 1 && folderview->opened != NULL) {
1002                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1003                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1004                                           folderview->opened);
1005         }
1006 }
1007
1008 #define BREAK_ON_MODIFIER_KEY() \
1009         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1010
1011 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1012                                    FolderView *folderview)
1013 {
1014         if (!event) return;
1015
1016         switch (event->keyval) {
1017         case GDK_Return:
1018         case GDK_space:
1019                 if (folderview->selected) {
1020                         folderview_select_node(folderview,
1021                                                folderview->selected);
1022                 }
1023                 break;
1024         case GDK_v:
1025         case GDK_V:
1026         case GDK_g:
1027         case GDK_G:
1028         case GDK_x:
1029         case GDK_X:
1030         case GDK_w:
1031         case GDK_D:
1032         case GDK_Q:
1033                 BREAK_ON_MODIFIER_KEY();
1034                 summary_pass_key_press_event(folderview->summaryview, event);
1035         default:
1036         }
1037 }
1038
1039 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1040                                 gint column, FolderView *folderview)
1041 {
1042         static gboolean can_select = TRUE;      /* exclusive lock */
1043         FolderItem *item;
1044
1045         folderview->selected = row;
1046
1047         if (folderview->opened == row) {
1048                 folderview->open_folder = FALSE;
1049                 return;
1050         }
1051
1052         if (!can_select) {
1053                 gtk_ctree_select(ctree, folderview->opened);
1054                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1055                 return;
1056         }
1057
1058         if (!folderview->open_folder) return;
1059         folderview->open_folder = FALSE;
1060
1061         item = gtk_ctree_node_get_row_data(ctree, row);
1062         if (!item) return;
1063
1064         can_select = FALSE;
1065
1066         if (item->path)
1067                 debug_print(_("Folder %s is selected\n"), item->path);
1068
1069         if (!GTK_CTREE_ROW(row)->children)
1070                 gtk_ctree_expand(ctree, row);
1071         if (folderview->opened &&
1072             !GTK_CTREE_ROW(folderview->opened)->children)
1073                 gtk_ctree_collapse(ctree, folderview->opened);
1074
1075         folderview->opened = row;
1076
1077         summary_show(folderview->summaryview, item, FALSE);
1078
1079         can_select = TRUE;
1080 }
1081
1082 static void folderview_popup_close(GtkMenuShell *menu_shell,
1083                                    FolderView *folderview)
1084 {
1085         if (!folderview->opened) return;
1086
1087         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1088         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1089                                   folderview->opened);
1090 }
1091
1092 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1093                                    FolderView *folderview)
1094 {
1095         switch (column) {
1096         case COL_FOLDER:
1097                 prefs_common.folder_col_folder = width;
1098                 break;
1099         case COL_NEW:
1100                 prefs_common.folder_col_new = width;
1101                 break;
1102         case COL_UNREAD:
1103                 prefs_common.folder_col_unread = width;
1104                 break;
1105         case COL_TOTAL:
1106                 prefs_common.folder_col_total = width;
1107                 break;
1108         default:
1109         }
1110 }
1111
1112 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1113                                              GtkCTreeNode *node,
1114                                              const gchar *name)
1115 {
1116         FolderItem *item;
1117
1118         if (!node)
1119                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1120         if (!node)
1121                 return NULL;
1122
1123         node = GTK_CTREE_ROW(node)->children;
1124
1125         while (node) {
1126                 item = gtk_ctree_node_get_row_data(ctree, node);
1127                 if (!folderview_compare_name(item, name))
1128                         return node;
1129                 node = GTK_CTREE_ROW(node)->sibling;
1130         }
1131
1132         return NULL;
1133 }
1134
1135 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1136                                      GtkWidget *widget)
1137 {
1138         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1139         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1140         FolderItem *item;
1141         FolderItem *new_item;
1142         gchar *new_folder;
1143         GtkCTreeNode *node;
1144
1145         if (!folderview->selected) return;
1146
1147         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1148         g_return_if_fail(item != NULL);
1149         g_return_if_fail(item->folder != NULL);
1150
1151         new_folder = input_dialog(_("New folder"),
1152                                   _("Input the name of new folder:"),
1153                                   _("NewFolder"));
1154         if (!new_folder) return;
1155
1156         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1157                 alertpanel_error(_("`%c' can't be included in folder name."),
1158                                  G_DIR_SEPARATOR);
1159                 g_free(new_folder);
1160                 return;
1161         }
1162
1163         /* find whether the directory already exists */
1164         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1165                 alertpanel_error(_("The folder `%s' already exists."),
1166                                  new_folder);
1167                 g_free(new_folder);
1168                 return;
1169         }
1170
1171         new_item = item->folder->create_folder(item->folder, item, new_folder);
1172         g_free(new_folder);
1173         if (!new_item) return;
1174
1175         gtk_clist_freeze(GTK_CLIST(ctree));
1176
1177         text[COL_FOLDER] = new_item->name;
1178         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1179                                      FOLDER_SPACING,
1180                                      folderxpm, folderxpmmask,
1181                                      folderopenxpm, folderopenxpmmask,
1182                                      FALSE, FALSE);
1183         gtk_ctree_expand(ctree, folderview->selected);
1184         gtk_ctree_node_set_row_data(ctree, node, new_item);
1185         folderview_sort_folders(folderview, folderview->selected, item->folder);
1186
1187         gtk_clist_thaw(GTK_CLIST(ctree));
1188
1189         folder_write_list();
1190 }
1191
1192 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1193                                         GtkWidget *widget)
1194 {
1195         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1196         FolderItem *item;
1197         gchar *new_folder;
1198         gchar *message;
1199
1200         if (!folderview->selected) return;
1201
1202         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1203         g_return_if_fail(item != NULL);
1204         g_return_if_fail(item->path != NULL);
1205         g_return_if_fail(item->folder != NULL);
1206
1207         message = g_strdup_printf(_("Input new name for `%s':"),
1208                                   g_basename(item->path));
1209         new_folder = input_dialog(_("Rename folder"), message,
1210                                   g_basename(item->path));
1211         g_free(message);
1212         if (!new_folder) return;
1213
1214         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1215                 alertpanel_error(_("`%c' can't be included in folder name."),
1216                                  G_DIR_SEPARATOR);
1217                 g_free(new_folder);
1218                 return;
1219         }
1220
1221         if (folderview_find_by_name
1222                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1223                  new_folder)) {
1224                 alertpanel_error(_("The folder `%s' already exists."),
1225                                  new_folder);
1226                 g_free(new_folder);
1227                 return;
1228         }
1229
1230         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1231                 g_free(new_folder);
1232                 return;
1233         }
1234         g_free(new_folder);
1235
1236         gtk_clist_freeze(GTK_CLIST(ctree));
1237
1238         folderview_update_node(folderview, folderview->selected);
1239         folderview_sort_folders(folderview,
1240                                 GTK_CTREE_ROW(folderview->selected)->parent,
1241                                 item->folder);
1242         if (folderview->opened == folderview->selected) {
1243                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1244                         gtk_ctree_expand(ctree, folderview->opened);
1245                 summary_show(folderview->summaryview, item, FALSE);
1246         }
1247
1248         gtk_clist_thaw(GTK_CLIST(ctree));
1249
1250         folder_write_list();
1251 }
1252
1253 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1254                                         GtkWidget *widget)
1255 {
1256         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1257         FolderItem *item;
1258         gchar *message;
1259         AlertValue avalue;
1260
1261         if (!folderview->selected) return;
1262
1263         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1264         g_return_if_fail(item != NULL);
1265         g_return_if_fail(item->path != NULL);
1266         g_return_if_fail(item->folder != NULL);
1267
1268         message = g_strdup_printf
1269                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1270                    "Do you really want to delete?"),
1271                  g_basename(item->path));
1272         avalue = alertpanel(_("Delete folder"), message,
1273                             _("Yes"), _("+No"), NULL);
1274         g_free(message);
1275         if (avalue != G_ALERTDEFAULT) return;
1276
1277         if (item->folder->remove_folder(item->folder, item) < 0) {
1278                 g_warning(_("can't remove folder `%s'\n"), item->path);
1279                 return;
1280         }
1281
1282         if (folderview->opened == folderview->selected ||
1283             gtk_ctree_is_ancestor(ctree,
1284                                   folderview->selected,
1285                                   folderview->opened)) {
1286                 summary_clear_all(folderview->summaryview);
1287                 folderview->opened = NULL;
1288         }
1289
1290         gtk_ctree_remove_node(ctree, folderview->selected);
1291         folder_write_list();
1292 }
1293
1294 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
1295                                          GtkWidget *widget)
1296 {
1297         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1298         GtkCTreeNode *node;
1299         FolderItem *item;
1300         gchar *message;
1301         AlertValue avalue;
1302
1303         if (!folderview->selected) return;
1304         node = folderview->selected;
1305         item = gtk_ctree_node_get_row_data(ctree, node);
1306         g_return_if_fail(item != NULL);
1307         g_return_if_fail(item->folder != NULL);
1308         if (item->parent) return;
1309
1310         message = g_strdup_printf
1311                 (_("Really remove the mailbox `%s' ?\n"
1312                    "(The messages are NOT deleted from disk)"),
1313                  item->folder->name);
1314         avalue = alertpanel(_("Remove folder"), message,
1315                             _("Yes"), _("+No"), NULL);
1316         g_free(message);
1317         if (avalue != G_ALERTDEFAULT) return;
1318
1319         folder_destroy(item->folder);
1320         summary_clear_all(folderview->summaryview);
1321         folderview_unselect(folderview);
1322         gtk_ctree_remove_node(ctree, node);
1323         folder_write_list();
1324 }
1325
1326 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
1327                                           GtkWidget *widget)
1328 {
1329         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1330         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1331         GtkCTreeNode *node;
1332         FolderItem *item;
1333         FolderItem *new_item;
1334         gchar *new_folder;
1335
1336         if (!folderview->selected) return;
1337
1338         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1339         g_return_if_fail(item != NULL);
1340         g_return_if_fail(item->folder != NULL);
1341         g_return_if_fail(item->folder->type == F_IMAP);
1342         g_return_if_fail(item->folder->account != NULL);
1343
1344         new_folder = input_dialog(_("New folder"),
1345                                   _("Input the name of new folder:"),
1346                                   _("NewFolder"));
1347         if (!new_folder) return;
1348
1349         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1350                 alertpanel_error(_("`%c' can't be included in folder name."),
1351                                  G_DIR_SEPARATOR);
1352                 g_free(new_folder);
1353                 return;
1354         }
1355
1356         /* find whether the directory already exists */
1357         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1358                 alertpanel_error(_("The folder `%s' already exists."),
1359                                  new_folder);
1360                 g_free(new_folder);
1361                 return;
1362         }
1363
1364         new_item = item->folder->create_folder(item->folder, item, new_folder);
1365         g_free(new_folder);
1366         if (!new_item) return;
1367
1368         gtk_clist_freeze(GTK_CLIST(ctree));
1369
1370         text[COL_FOLDER] = new_item->name;
1371         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1372                                      FOLDER_SPACING,
1373                                      folderxpm, folderxpmmask,
1374                                      folderopenxpm, folderopenxpmmask,
1375                                      FALSE, FALSE);
1376         gtk_ctree_expand(ctree, folderview->selected);
1377         gtk_ctree_node_set_row_data(ctree, node, new_item);
1378         folderview_sort_folders(folderview, folderview->selected, item->folder);
1379
1380         gtk_clist_thaw(GTK_CLIST(ctree));
1381
1382         folder_write_list();
1383 }
1384
1385 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
1386                                          GtkWidget *widget)
1387 {
1388         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1389         FolderItem *item;
1390         gchar *message;
1391         AlertValue avalue;
1392
1393         if (!folderview->selected) return;
1394
1395         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1396         g_return_if_fail(item != NULL);
1397         g_return_if_fail(item->folder != NULL);
1398         g_return_if_fail(item->folder->type == F_IMAP);
1399         g_return_if_fail(item->folder->account != NULL);
1400
1401         message = g_strdup_printf(_("Really delete folder `%s'?"),
1402                                   g_basename(item->path));
1403         avalue = alertpanel(_("Delete folder"), message,
1404                             _("Yes"), _("+No"), NULL);
1405         g_free(message);
1406         if (avalue != G_ALERTDEFAULT) return;
1407
1408         if (folderview->opened == folderview->selected) {
1409                 summary_clear_all(folderview->summaryview);
1410                 folderview->opened = NULL;
1411         }
1412
1413         folder_item_remove(item);
1414         gtk_ctree_remove_node(ctree, folderview->selected);
1415         folder_write_list();
1416 }
1417
1418 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
1419                                          GtkWidget *widget)
1420 {
1421         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1422         FolderItem *item;
1423         gchar *message;
1424         AlertValue avalue;
1425
1426         if (!folderview->selected) return;
1427
1428         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1429         g_return_if_fail(item != NULL);
1430         g_return_if_fail(item->folder != NULL);
1431         g_return_if_fail(item->folder->type == F_IMAP);
1432         g_return_if_fail(item->folder->account != NULL);
1433
1434         message = g_strdup_printf(_("Really delete IMAP4 server `%s'?"),
1435                                   item->folder->name);
1436         avalue = alertpanel(_("Delete IMAP4 server"), message,
1437                             _("Yes"), _("+No"), NULL);
1438         g_free(message);
1439
1440         if (avalue != G_ALERTDEFAULT) return;
1441
1442         if (folderview->opened == folderview->selected ||
1443             gtk_ctree_is_ancestor(ctree,
1444                                   folderview->selected,
1445                                   folderview->opened)) {
1446                 summary_clear_all(folderview->summaryview);
1447                 folderview->opened = NULL;
1448         }
1449
1450         account_destroy(item->folder->account);
1451         folder_destroy(item->folder);
1452         gtk_ctree_remove_node(ctree, folderview->selected);
1453         account_set_menu();
1454         main_window_reflect_prefs_all();
1455         folder_write_list();
1456 }
1457
1458 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
1459                                          GtkWidget *widget)
1460 {
1461         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1462         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1463         GtkCTreeNode *servernode, *node;
1464         FolderItem *item;
1465         FolderItem *newitem;
1466         gchar *new_group;
1467         const gchar *server;
1468
1469         if (!folderview->selected) return;
1470
1471         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1472         g_return_if_fail(item != NULL);
1473         g_return_if_fail(item->folder != NULL);
1474         g_return_if_fail(item->folder->type == F_NEWS);
1475         g_return_if_fail(item->folder->account != NULL);
1476
1477         new_group = grouplist_dialog(item);
1478         if (!new_group) return;
1479
1480         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
1481                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
1482         else
1483                 servernode = folderview->selected;
1484
1485         if (folderview_find_by_name(ctree, servernode, new_group)) {
1486                 alertpanel_error(_("The newsgroup `%s' already exists."),
1487                                  new_group);
1488                 g_free(new_group);
1489                 return;
1490         }
1491
1492         gtk_clist_freeze(GTK_CLIST(ctree));
1493
1494         text[COL_FOLDER] = new_group;
1495         node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
1496                                      FOLDER_SPACING,
1497                                      folderxpm, folderxpmmask,
1498                                      folderopenxpm, folderopenxpmmask,
1499                                      FALSE, FALSE);
1500         gtk_ctree_expand(ctree, servernode);
1501
1502         item = gtk_ctree_node_get_row_data(ctree, servernode);
1503         server = item->folder->account->nntp_server;
1504
1505         newitem = folder_item_new(new_group, new_group);
1506         g_free(new_group);
1507         folder_item_append(item, newitem);
1508         gtk_ctree_node_set_row_data(ctree, node, newitem);
1509         gtk_ctree_sort_node(ctree, servernode);
1510
1511         gtk_clist_thaw(GTK_CLIST(ctree));
1512
1513         folder_write_list();
1514 }
1515
1516 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
1517                                         GtkWidget *widget)
1518 {
1519         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1520         FolderItem *item;
1521         gchar *message;
1522         AlertValue avalue;
1523
1524         if (!folderview->selected) return;
1525
1526         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1527         g_return_if_fail(item != NULL);
1528         g_return_if_fail(item->folder != NULL);
1529         g_return_if_fail(item->folder->type == F_NEWS);
1530         g_return_if_fail(item->folder->account != NULL);
1531
1532         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
1533                                   g_basename(item->path));
1534         avalue = alertpanel(_("Delete newsgroup"), message,
1535                             _("Yes"), _("+No"), NULL);
1536         g_free(message);
1537         if (avalue != G_ALERTDEFAULT) return;
1538
1539         if (folderview->opened == folderview->selected) {
1540                 summary_clear_all(folderview->summaryview);
1541                 folderview->opened = NULL;
1542         }
1543
1544         folder_item_remove(item);
1545         gtk_ctree_remove_node(ctree, folderview->selected);
1546         folder_write_list();
1547 }
1548
1549 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
1550                                          GtkWidget *widget)
1551 {
1552         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1553         FolderItem *item;
1554         gchar *message;
1555         AlertValue avalue;
1556
1557         if (!folderview->selected) return;
1558
1559         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1560         g_return_if_fail(item != NULL);
1561         g_return_if_fail(item->folder != NULL);
1562         g_return_if_fail(item->folder->type == F_NEWS);
1563         g_return_if_fail(item->folder->account != NULL);
1564
1565         message = g_strdup_printf(_("Really delete news server `%s'?"),
1566                                   item->folder->name);
1567         avalue = alertpanel(_("Delete news server"), message,
1568                             _("Yes"), _("+No"), NULL);
1569         g_free(message);
1570
1571         if (avalue != G_ALERTDEFAULT) return;
1572
1573         if (folderview->opened == folderview->selected ||
1574             gtk_ctree_is_ancestor(ctree,
1575                                   folderview->selected,
1576                                   folderview->opened)) {
1577                 summary_clear_all(folderview->summaryview);
1578                 folderview->opened = NULL;
1579         }
1580
1581         account_destroy(item->folder->account);
1582         folder_destroy(item->folder);
1583         gtk_ctree_remove_node(ctree, folderview->selected);
1584         account_set_menu();
1585         main_window_reflect_prefs_all();
1586         folder_write_list();
1587 }
1588
1589 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
1590                                           GdkDragContext *context,
1591                                           gint            x,
1592                                           gint            y,
1593                                           guint           time,
1594                                           FolderView     *folderview)
1595 {
1596         gint row, column;
1597         FolderItem *item;
1598         GtkCTreeNode *node;
1599
1600         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
1601                                          x - 24, y - 24, &row, &column)) {
1602                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
1603                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
1604                 if (item != NULL &&
1605                     item->path != NULL &&
1606                     folderview->summaryview->folder_item != item) {
1607                         if (item->folder->type != F_NEWS) {
1608                                 gtk_ctree_select(GTK_CTREE(widget), node);
1609                                 gdk_drag_status(context,
1610                                                 context->suggested_action,
1611                                                 time);
1612                                 return TRUE;
1613                         }
1614                 }
1615         }
1616
1617         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
1618         gdk_drag_status(context, 0, time);
1619
1620         return FALSE;
1621 }
1622
1623 static void folderview_drag_leave_cb(GtkWidget      *widget,
1624                                      GdkDragContext *context,
1625                                      guint           time,
1626                                      FolderView     *folderview)
1627 {
1628         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
1629 }
1630
1631 static void folderview_drag_received_cb(GtkWidget        *widget,
1632                                         GdkDragContext   *drag_context,
1633                                         gint              x,
1634                                         gint              y,
1635                                         GtkSelectionData *data,
1636                                         guint             info,
1637                                         guint             time,
1638                                         FolderView       *folderview)
1639 {
1640         gint row, column;
1641         FolderItem *item;
1642         GtkCTreeNode *node;
1643
1644         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
1645                                          x - 24, y - 24, &row, &column) == 0)
1646                 return;
1647
1648         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
1649         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
1650         if (item != NULL) {
1651                 summary_move_selected_to(folderview->summaryview, item);
1652                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
1653         } else
1654                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
1655 }
1656
1657 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
1658 {
1659         const FolderItem *item = a;
1660         const gchar *name = b;
1661
1662         if (!item->path) return -1;
1663         return strcmp2(g_basename(item->path), name);
1664 }