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