more sync with 0.4.99cvs2.
[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 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
699 {
700         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
701         GtkStyle *style, *prev_style, *ctree_style;
702         GtkCTreeNode *parent;
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         parent = node;
845         while ((parent = gtkut_ctree_find_collapsed_parent(ctree, parent))
846                != NULL)
847                 folderview_update_node(folderview, parent);
848 }
849
850 void folderview_update_item(FolderItem *item, gboolean update_summary)
851 {
852         GList *list;
853         FolderView *folderview;
854         GtkCTree *ctree;
855         GtkCTreeNode *node;
856
857         g_return_if_fail(item != NULL);
858
859         for (list = folderview_list; list != NULL; list = list->next) {
860                 folderview = (FolderView *)list->data;
861                 ctree = GTK_CTREE(folderview->ctree);
862
863                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
864                 if (node) {
865                         folderview_update_node(folderview, node);
866                         if (update_summary && folderview->opened == node)
867                                 summary_show(folderview->summaryview,
868                                              item, FALSE);
869                 }
870         }
871 }
872
873 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
874                                                 gpointer data)
875 {
876         folderview_update_item((FolderItem *)key, FALSE);
877 }
878
879 void folderview_update_item_foreach(GHashTable *table)
880 {
881         g_hash_table_foreach(table, folderview_update_item_foreach_func, NULL);
882 }
883
884 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
885                                       GNode *gnode, GtkCTreeNode *cnode,
886                                       gpointer data)
887 {
888         FolderView *folderview = (FolderView *)data;
889         FolderItem *item = FOLDER_ITEM(gnode->data);
890
891         g_return_val_if_fail(item != NULL, FALSE);
892
893         gtk_ctree_node_set_row_data(ctree, cnode, item);
894         folderview_update_node(folderview, cnode);
895
896         return TRUE;
897 }
898
899 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
900                                    gpointer data)
901 {
902         if (GTK_CTREE_ROW(node)->children)
903                 gtk_ctree_expand(ctree, node);
904 }
905
906 #define SET_SPECIAL_FOLDER(ctree, item) \
907 { \
908         if (item) { \
909                 GtkCTreeNode *node, *sibling; \
910  \
911                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
912                 if (!node) \
913                         g_warning("%s not found.\n", item->path); \
914                 else { \
915                         if (!prev) \
916                                 sibling = GTK_CTREE_ROW(root)->children; \
917                         else \
918                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
919                         if (node != sibling) \
920                                 gtk_ctree_move(ctree, node, root, sibling); \
921                 } \
922  \
923                 prev = node; \
924         } \
925 }
926
927 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
928                                     Folder *folder)
929 {
930         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
931         GtkCTreeNode *prev = NULL;
932
933         gtk_ctree_sort_recursive(ctree, root);
934
935         if (GTK_CTREE_ROW(root)->parent) return;
936
937         SET_SPECIAL_FOLDER(ctree, folder->inbox);
938         SET_SPECIAL_FOLDER(ctree, folder->outbox);
939         SET_SPECIAL_FOLDER(ctree, folder->draft);
940         SET_SPECIAL_FOLDER(ctree, folder->queue);
941         SET_SPECIAL_FOLDER(ctree, folder->trash);
942 }
943
944 static void folderview_append_folder(FolderView *folderview, Folder *folder)
945 {
946         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
947         GtkCTreeNode *root;
948
949         g_return_if_fail(folder != NULL);
950
951         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
952                                       folderview_gnode_func, folderview);
953         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
954                                 folderview);
955         folderview_sort_folders(folderview, root, folder);
956 }
957
958 void folderview_new_folder(FolderView *folderview)
959 {
960         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
961         FolderItem *item;
962
963         if (!folderview->selected) return;
964
965         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
966         g_return_if_fail(item != NULL);
967         g_return_if_fail(item->folder != NULL);
968
969         switch (item->folder->type) {
970         case F_MH:
971         case F_MBOX:
972         case F_MAILDIR:
973                 folderview_new_folder_cb(folderview, 0, NULL);
974                 break;
975         case F_IMAP:
976                 folderview_new_imap_folder_cb(folderview, 0, NULL);
977                 break;
978         case F_NEWS:
979         default:
980                 break;
981         }
982 }
983
984 void folderview_rename_folder(FolderView *folderview)
985 {
986         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
987         FolderItem *item;
988
989         if (!folderview->selected) return;
990
991         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
992         g_return_if_fail(item != NULL);
993         g_return_if_fail(item->folder != NULL);
994         if (!item->path) return;
995         if (item->stype != F_NORMAL) return;
996
997         switch (item->folder->type) {
998         case F_MH:
999         case F_MBOX:
1000         case F_MAILDIR:
1001                 folderview_rename_folder_cb(folderview, 0, NULL);
1002                 break;
1003         case F_IMAP:
1004         case F_NEWS:
1005         default:
1006                 break;
1007         }
1008 }
1009
1010 void folderview_delete_folder(FolderView *folderview)
1011 {
1012         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1013         FolderItem *item;
1014
1015         if (!folderview->selected) return;
1016
1017         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1018         g_return_if_fail(item != NULL);
1019         g_return_if_fail(item->folder != NULL);
1020         if (!item->path) return;
1021         if (item->stype != F_NORMAL) return;
1022
1023         switch (item->folder->type) {
1024         case F_MH:
1025         case F_MBOX:
1026         case F_MAILDIR:
1027                 folderview_delete_folder_cb(folderview, 0, NULL);
1028                 break;
1029         case F_IMAP:
1030                 folderview_rm_imap_folder_cb(folderview, 0, NULL);
1031         case F_NEWS:
1032         default:
1033                 break;
1034         }
1035 }
1036
1037
1038 /* callback functions */
1039
1040 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1041                                       FolderView *folderview)
1042 {
1043         GtkCList *clist = GTK_CLIST(ctree);
1044         gint prev_row = -1, row = -1, column = -1;
1045         FolderItem *item;
1046         Folder *folder;
1047
1048         if (!event) return;
1049
1050         if (event->button == 1) {
1051                 folderview->open_folder = TRUE;
1052                 return;
1053         }
1054
1055         if (event->button == 2 || event->button == 3) {
1056                 /* right clicked */
1057                 if (clist->selection) {
1058                         GtkCTreeNode *node;
1059
1060                         node = GTK_CTREE_NODE(clist->selection->data);
1061                         if (node)
1062                                 prev_row = gtkut_ctree_get_nth_from_node
1063                                         (GTK_CTREE(ctree), node);
1064                 }
1065
1066                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1067                                                   &row, &column))
1068                         return;
1069                 if (prev_row != row) {
1070                         gtk_clist_unselect_all(clist);
1071                         if (event->button == 2)
1072                                 folderview_select_node
1073                                         (folderview,
1074                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1075                                                             row));
1076                         else
1077                                 gtk_clist_select_row(clist, row, column);
1078                 }
1079         }
1080
1081         if (event->button != 3) return;
1082
1083         item = gtk_clist_get_row_data(clist, row);
1084         g_return_if_fail(item != NULL);
1085         g_return_if_fail(item->folder != NULL);
1086         folder = item->folder;
1087
1088         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->mail_popup));
1089         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->imap_popup));
1090         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->news_popup));
1091
1092         if (folder->type == F_MH && item->parent == NULL) {
1093                 menu_set_sensitive(folderview->mail_factory,
1094                                    "/Create new folder...", TRUE);
1095                 menu_set_sensitive(folderview->mail_factory,
1096                                    "/Remove mailbox", TRUE);
1097         } else if (folder->type == F_MH && item->stype != F_NORMAL) {
1098                 menu_set_sensitive(folderview->mail_factory,
1099                                    "/Create new folder...", TRUE);
1100         } else if (folder->type == F_MH) {
1101                 menu_set_sensitive(folderview->mail_factory,
1102                                    "/Create new folder...", TRUE);
1103                 menu_set_sensitive(folderview->mail_factory,
1104                                    "/Rename folder...", TRUE);
1105                 menu_set_sensitive(folderview->mail_factory,
1106                                    "/Delete folder", TRUE);
1107         } else if (folder->type == F_IMAP && item->parent == NULL) {
1108                 menu_set_sensitive(folderview->imap_factory,
1109                                    "/Create new folder...", TRUE);
1110                 menu_set_sensitive(folderview->imap_factory,
1111                                    "/Remove IMAP4 server", TRUE);
1112         } else if (folder->type == F_IMAP && item->parent != NULL) {
1113                 menu_set_sensitive(folderview->imap_factory,
1114                                    "/Create new folder...", TRUE);
1115                 menu_set_sensitive(folderview->imap_factory,
1116                                    "/Delete folder", TRUE);
1117         } else if (folder->type == F_NEWS && item->parent == NULL) {
1118                 menu_set_sensitive(folderview->news_factory,
1119                                    "/Subscribe to newsgroup...", TRUE);
1120                 menu_set_sensitive(folderview->news_factory,
1121                                    "/Remove news server", TRUE);
1122         } else if (folder->type == F_NEWS && item->parent != NULL) {
1123                 menu_set_sensitive(folderview->news_factory,
1124                                    "/Subscribe to newsgroup...", TRUE);
1125                 menu_set_sensitive(folderview->news_factory,
1126                                    "/Remove newsgroup", TRUE);
1127         }
1128
1129         if (folder->type == F_MH)
1130                 gtk_menu_popup(GTK_MENU(folderview->mail_popup), NULL, NULL,
1131                                NULL, NULL, event->button, event->time);
1132         else if (folder->type == F_IMAP)
1133                 gtk_menu_popup(GTK_MENU(folderview->imap_popup), NULL, NULL,
1134                                NULL, NULL, event->button, event->time);
1135         else if (folder->type == F_NEWS)
1136                 gtk_menu_popup(GTK_MENU(folderview->news_popup), NULL, NULL,
1137                                NULL, NULL, event->button, event->time);
1138 }
1139
1140 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1141                                        FolderView *folderview)
1142 {
1143         if (!event) return;
1144
1145         if (event->button == 1 && folderview->opened != NULL) {
1146                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1147                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1148                                           folderview->opened);
1149         }
1150 }
1151
1152 #define BREAK_ON_MODIFIER_KEY() \
1153         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1154
1155 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1156                                    FolderView *folderview)
1157 {
1158         if (!event) return;
1159
1160         switch (event->keyval) {
1161         case GDK_Return:
1162         case GDK_space:
1163                 if (folderview->selected) {
1164                         folderview_select_node(folderview,
1165                                                folderview->selected);
1166                 }
1167                 break;
1168         case GDK_v:
1169         case GDK_V:
1170         case GDK_g:
1171         case GDK_G:
1172         case GDK_x:
1173         case GDK_X:
1174         case GDK_w:
1175         case GDK_D:
1176         case GDK_Q:
1177                 BREAK_ON_MODIFIER_KEY();
1178                 summary_pass_key_press_event(folderview->summaryview, event);
1179         default:
1180         }
1181 }
1182
1183 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1184                                 gint column, FolderView *folderview)
1185 {
1186         static gboolean can_select = TRUE;      /* exclusive lock */
1187         FolderItem *item;
1188
1189         folderview->selected = row;
1190
1191         if (folderview->opened == row) {
1192                 folderview->open_folder = FALSE;
1193                 return;
1194         }
1195
1196         if (!can_select) {
1197                 gtk_ctree_select(ctree, folderview->opened);
1198                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1199                 return;
1200         }
1201
1202         if (!folderview->open_folder) return;
1203         folderview->open_folder = FALSE;
1204
1205         item = gtk_ctree_node_get_row_data(ctree, row);
1206         if (!item) return;
1207
1208         can_select = FALSE;
1209
1210         if (item->path)
1211                 debug_print(_("Folder %s is selected\n"), item->path);
1212
1213         if (!GTK_CTREE_ROW(row)->children)
1214                 gtk_ctree_expand(ctree, row);
1215         if (folderview->opened &&
1216             !GTK_CTREE_ROW(folderview->opened)->children)
1217                 gtk_ctree_collapse(ctree, folderview->opened);
1218
1219         folderview->opened = row;
1220
1221         summary_show(folderview->summaryview, item, FALSE);
1222
1223         can_select = TRUE;
1224 }
1225
1226 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1227                                      FolderView *folderview)
1228 {
1229         folderview_update_node(folderview, node);
1230 }
1231
1232 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1233                                       FolderView *folderview)
1234 {
1235         folderview_update_node(folderview, node);
1236 }
1237
1238 static void folderview_popup_close(GtkMenuShell *menu_shell,
1239                                    FolderView *folderview)
1240 {
1241         if (!folderview->opened) return;
1242
1243         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1244         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1245                                   folderview->opened);
1246 }
1247
1248 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1249                                    FolderView *folderview)
1250 {
1251         switch (column) {
1252         case COL_FOLDER:
1253                 prefs_common.folder_col_folder = width;
1254                 break;
1255         case COL_NEW:
1256                 prefs_common.folder_col_new = width;
1257                 break;
1258         case COL_UNREAD:
1259                 prefs_common.folder_col_unread = width;
1260                 break;
1261         case COL_TOTAL:
1262                 prefs_common.folder_col_total = width;
1263                 break;
1264         default:
1265         }
1266 }
1267
1268 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1269                                              GtkCTreeNode *node,
1270                                              const gchar *name)
1271 {
1272         FolderItem *item;
1273
1274         if (!node)
1275                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1276         if (!node)
1277                 return NULL;
1278
1279         node = GTK_CTREE_ROW(node)->children;
1280
1281         while (node) {
1282                 item = gtk_ctree_node_get_row_data(ctree, node);
1283                 if (!folderview_compare_name(item, name))
1284                         return node;
1285                 node = GTK_CTREE_ROW(node)->sibling;
1286         }
1287
1288         return NULL;
1289 }
1290
1291 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1292                                      GtkWidget *widget)
1293 {
1294         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1295         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1296         FolderItem *item;
1297         FolderItem *new_item;
1298         gchar *new_folder;
1299         GtkCTreeNode *node;
1300
1301         if (!folderview->selected) return;
1302
1303         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1304         g_return_if_fail(item != NULL);
1305         g_return_if_fail(item->folder != NULL);
1306
1307         new_folder = input_dialog(_("New folder"),
1308                                   _("Input the name of new folder:"),
1309                                   _("NewFolder"));
1310         if (!new_folder) return;
1311
1312         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1313                 alertpanel_error(_("`%c' can't be included in folder name."),
1314                                  G_DIR_SEPARATOR);
1315                 g_free(new_folder);
1316                 return;
1317         }
1318
1319         /* find whether the directory already exists */
1320         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1321                 alertpanel_error(_("The folder `%s' already exists."),
1322                                  new_folder);
1323                 g_free(new_folder);
1324                 return;
1325         }
1326
1327         new_item = item->folder->create_folder(item->folder, item, new_folder);
1328         g_free(new_folder);
1329         if (!new_item) return;
1330
1331         gtk_clist_freeze(GTK_CLIST(ctree));
1332
1333         text[COL_FOLDER] = new_item->name;
1334         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1335                                      FOLDER_SPACING,
1336                                      folderxpm, folderxpmmask,
1337                                      folderopenxpm, folderopenxpmmask,
1338                                      FALSE, FALSE);
1339         gtk_ctree_expand(ctree, folderview->selected);
1340         gtk_ctree_node_set_row_data(ctree, node, new_item);
1341         folderview_sort_folders(folderview, folderview->selected, item->folder);
1342
1343         gtk_clist_thaw(GTK_CLIST(ctree));
1344
1345         folder_write_list();
1346 }
1347
1348 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1349                                         GtkWidget *widget)
1350 {
1351         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1352         FolderItem *item;
1353         gchar *new_folder;
1354         gchar *message;
1355
1356         if (!folderview->selected) return;
1357
1358         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1359         g_return_if_fail(item != NULL);
1360         g_return_if_fail(item->path != NULL);
1361         g_return_if_fail(item->folder != NULL);
1362
1363         message = g_strdup_printf(_("Input new name for `%s':"),
1364                                   g_basename(item->path));
1365         new_folder = input_dialog(_("Rename folder"), message,
1366                                   g_basename(item->path));
1367         g_free(message);
1368         if (!new_folder) return;
1369
1370         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1371                 alertpanel_error(_("`%c' can't be included in folder name."),
1372                                  G_DIR_SEPARATOR);
1373                 g_free(new_folder);
1374                 return;
1375         }
1376
1377         if (folderview_find_by_name
1378                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1379                  new_folder)) {
1380                 alertpanel_error(_("The folder `%s' already exists."),
1381                                  new_folder);
1382                 g_free(new_folder);
1383                 return;
1384         }
1385
1386         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1387                 g_free(new_folder);
1388                 return;
1389         }
1390         g_free(new_folder);
1391
1392         gtk_clist_freeze(GTK_CLIST(ctree));
1393
1394         folderview_update_node(folderview, folderview->selected);
1395         folderview_sort_folders(folderview,
1396                                 GTK_CTREE_ROW(folderview->selected)->parent,
1397                                 item->folder);
1398         if (folderview->opened == folderview->selected) {
1399                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1400                         gtk_ctree_expand(ctree, folderview->opened);
1401                 summary_show(folderview->summaryview, item, FALSE);
1402         }
1403
1404         gtk_clist_thaw(GTK_CLIST(ctree));
1405
1406         folder_write_list();
1407 }
1408
1409 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1410                                         GtkWidget *widget)
1411 {
1412         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1413         FolderItem *item;
1414         gchar *message;
1415         AlertValue avalue;
1416
1417         if (!folderview->selected) return;
1418
1419         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1420         g_return_if_fail(item != NULL);
1421         g_return_if_fail(item->path != NULL);
1422         g_return_if_fail(item->folder != NULL);
1423
1424         message = g_strdup_printf
1425                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1426                    "Do you really want to delete?"),
1427                  g_basename(item->path));
1428         avalue = alertpanel(_("Delete folder"), message,
1429                             _("Yes"), _("+No"), NULL);
1430         g_free(message);
1431         if (avalue != G_ALERTDEFAULT) return;
1432
1433         if (item->folder->remove_folder(item->folder, item) < 0) {
1434                 g_warning(_("can't remove folder `%s'\n"), item->path);
1435                 return;
1436         }
1437
1438         if (folderview->opened == folderview->selected ||
1439             gtk_ctree_is_ancestor(ctree,
1440                                   folderview->selected,
1441                                   folderview->opened)) {
1442                 summary_clear_all(folderview->summaryview);
1443                 folderview->opened = NULL;
1444         }
1445
1446         gtk_ctree_remove_node(ctree, folderview->selected);
1447         folder_write_list();
1448 }
1449
1450 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
1451                                          GtkWidget *widget)
1452 {
1453         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1454         GtkCTreeNode *node;
1455         FolderItem *item;
1456         gchar *message;
1457         AlertValue avalue;
1458
1459         if (!folderview->selected) return;
1460         node = folderview->selected;
1461         item = gtk_ctree_node_get_row_data(ctree, node);
1462         g_return_if_fail(item != NULL);
1463         g_return_if_fail(item->folder != NULL);
1464         if (item->parent) return;
1465
1466         message = g_strdup_printf
1467                 (_("Really remove the mailbox `%s' ?\n"
1468                    "(The messages are NOT deleted from disk)"),
1469                  item->folder->name);
1470         avalue = alertpanel(_("Remove folder"), message,
1471                             _("Yes"), _("+No"), NULL);
1472         g_free(message);
1473         if (avalue != G_ALERTDEFAULT) return;
1474
1475         folder_destroy(item->folder);
1476         summary_clear_all(folderview->summaryview);
1477         folderview_unselect(folderview);
1478         gtk_ctree_remove_node(ctree, node);
1479         folder_write_list();
1480 }
1481
1482 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
1483                                           GtkWidget *widget)
1484 {
1485         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1486         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1487         GtkCTreeNode *node;
1488         FolderItem *item;
1489         FolderItem *new_item;
1490         gchar *new_folder;
1491
1492         if (!folderview->selected) return;
1493
1494         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1495         g_return_if_fail(item != NULL);
1496         g_return_if_fail(item->folder != NULL);
1497         g_return_if_fail(item->folder->type == F_IMAP);
1498         g_return_if_fail(item->folder->account != NULL);
1499
1500         new_folder = input_dialog(_("New folder"),
1501                                   _("Input the name of new folder:"),
1502                                   _("NewFolder"));
1503         if (!new_folder) return;
1504
1505         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1506                 alertpanel_error(_("`%c' can't be included in folder name."),
1507                                  G_DIR_SEPARATOR);
1508                 g_free(new_folder);
1509                 return;
1510         }
1511
1512         /* find whether the directory already exists */
1513         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1514                 alertpanel_error(_("The folder `%s' already exists."),
1515                                  new_folder);
1516                 g_free(new_folder);
1517                 return;
1518         }
1519
1520         new_item = item->folder->create_folder(item->folder, item, new_folder);
1521         g_free(new_folder);
1522         if (!new_item) return;
1523
1524         gtk_clist_freeze(GTK_CLIST(ctree));
1525
1526         text[COL_FOLDER] = new_item->name;
1527         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1528                                      FOLDER_SPACING,
1529                                      folderxpm, folderxpmmask,
1530                                      folderopenxpm, folderopenxpmmask,
1531                                      FALSE, FALSE);
1532         gtk_ctree_expand(ctree, folderview->selected);
1533         gtk_ctree_node_set_row_data(ctree, node, new_item);
1534         folderview_sort_folders(folderview, folderview->selected, item->folder);
1535
1536         gtk_clist_thaw(GTK_CLIST(ctree));
1537
1538         folder_write_list();
1539 }
1540
1541 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
1542                                          GtkWidget *widget)
1543 {
1544         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1545         FolderItem *item;
1546         gchar *message;
1547         AlertValue avalue;
1548
1549         if (!folderview->selected) return;
1550
1551         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1552         g_return_if_fail(item != NULL);
1553         g_return_if_fail(item->folder != NULL);
1554         g_return_if_fail(item->folder->type == F_IMAP);
1555         g_return_if_fail(item->folder->account != NULL);
1556
1557         message = g_strdup_printf(_("Really delete folder `%s'?"),
1558                                   g_basename(item->path));
1559         avalue = alertpanel(_("Delete folder"), message,
1560                             _("Yes"), _("+No"), NULL);
1561         g_free(message);
1562         if (avalue != G_ALERTDEFAULT) return;
1563
1564         if (item->folder->remove_folder(item->folder, item) < 0) {
1565                 g_warning(_("can't remove folder `%s'\n"), item->path);
1566                 return;
1567         }
1568
1569         if (folderview->opened == folderview->selected ||
1570             gtk_ctree_is_ancestor(ctree,
1571                                   folderview->selected,
1572                                   folderview->opened)) {
1573                 summary_clear_all(folderview->summaryview);
1574                 folderview->opened = NULL;
1575         }
1576
1577         gtk_ctree_remove_node(ctree, folderview->selected);
1578         folder_write_list();
1579 }
1580
1581 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
1582                                          GtkWidget *widget)
1583 {
1584         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1585         FolderItem *item;
1586         gchar *message;
1587         AlertValue avalue;
1588
1589         if (!folderview->selected) return;
1590
1591         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1592         g_return_if_fail(item != NULL);
1593         g_return_if_fail(item->folder != NULL);
1594         g_return_if_fail(item->folder->type == F_IMAP);
1595         g_return_if_fail(item->folder->account != NULL);
1596
1597         message = g_strdup_printf(_("Really delete IMAP4 server `%s'?"),
1598                                   item->folder->name);
1599         avalue = alertpanel(_("Delete IMAP4 server"), message,
1600                             _("Yes"), _("+No"), NULL);
1601         g_free(message);
1602
1603         if (avalue != G_ALERTDEFAULT) return;
1604
1605         if (folderview->opened == folderview->selected ||
1606             gtk_ctree_is_ancestor(ctree,
1607                                   folderview->selected,
1608                                   folderview->opened)) {
1609                 summary_clear_all(folderview->summaryview);
1610                 folderview->opened = NULL;
1611         }
1612
1613         account_destroy(item->folder->account);
1614         folder_destroy(item->folder);
1615         gtk_ctree_remove_node(ctree, folderview->selected);
1616         account_set_menu();
1617         main_window_reflect_prefs_all();
1618         folder_write_list();
1619 }
1620
1621 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
1622                                          GtkWidget *widget)
1623 {
1624         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1625         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1626         GtkCTreeNode *servernode, *node;
1627         FolderItem *item;
1628         FolderItem *newitem;
1629         gchar *new_group;
1630         const gchar *server;
1631
1632         if (!folderview->selected) return;
1633
1634         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1635         g_return_if_fail(item != NULL);
1636         g_return_if_fail(item->folder != NULL);
1637         g_return_if_fail(item->folder->type == F_NEWS);
1638         g_return_if_fail(item->folder->account != NULL);
1639
1640         new_group = grouplist_dialog(item);
1641         if (!new_group) return;
1642
1643         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
1644                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
1645         else
1646                 servernode = folderview->selected;
1647
1648         if (folderview_find_by_name(ctree, servernode, new_group)) {
1649                 alertpanel_error(_("The newsgroup `%s' already exists."),
1650                                  new_group);
1651                 g_free(new_group);
1652                 return;
1653         }
1654
1655         gtk_clist_freeze(GTK_CLIST(ctree));
1656
1657         text[COL_FOLDER] = new_group;
1658         node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
1659                                      FOLDER_SPACING,
1660                                      folderxpm, folderxpmmask,
1661                                      folderopenxpm, folderopenxpmmask,
1662                                      FALSE, FALSE);
1663         gtk_ctree_expand(ctree, servernode);
1664
1665         item = gtk_ctree_node_get_row_data(ctree, servernode);
1666         server = item->folder->account->nntp_server;
1667
1668         newitem = folder_item_new(new_group, new_group);
1669         g_free(new_group);
1670         folder_item_append(item, newitem);
1671         gtk_ctree_node_set_row_data(ctree, node, newitem);
1672         gtk_ctree_sort_node(ctree, servernode);
1673
1674         gtk_clist_thaw(GTK_CLIST(ctree));
1675
1676         folder_write_list();
1677 }
1678
1679 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
1680                                         GtkWidget *widget)
1681 {
1682         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1683         FolderItem *item;
1684         gchar *message;
1685         AlertValue avalue;
1686
1687         if (!folderview->selected) return;
1688
1689         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1690         g_return_if_fail(item != NULL);
1691         g_return_if_fail(item->folder != NULL);
1692         g_return_if_fail(item->folder->type == F_NEWS);
1693         g_return_if_fail(item->folder->account != NULL);
1694
1695         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
1696                                   g_basename(item->path));
1697         avalue = alertpanel(_("Delete newsgroup"), message,
1698                             _("Yes"), _("+No"), NULL);
1699         g_free(message);
1700         if (avalue != G_ALERTDEFAULT) return;
1701
1702         if (folderview->opened == folderview->selected) {
1703                 summary_clear_all(folderview->summaryview);
1704                 folderview->opened = NULL;
1705         }
1706
1707         folder_item_remove(item);
1708         gtk_ctree_remove_node(ctree, folderview->selected);
1709         folder_write_list();
1710 }
1711
1712 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
1713                                          GtkWidget *widget)
1714 {
1715         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1716         FolderItem *item;
1717         gchar *message;
1718         AlertValue avalue;
1719
1720         if (!folderview->selected) return;
1721
1722         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1723         g_return_if_fail(item != NULL);
1724         g_return_if_fail(item->folder != NULL);
1725         g_return_if_fail(item->folder->type == F_NEWS);
1726         g_return_if_fail(item->folder->account != NULL);
1727
1728         message = g_strdup_printf(_("Really delete news server `%s'?"),
1729                                   item->folder->name);
1730         avalue = alertpanel(_("Delete news server"), message,
1731                             _("Yes"), _("+No"), NULL);
1732         g_free(message);
1733
1734         if (avalue != G_ALERTDEFAULT) return;
1735
1736         if (folderview->opened == folderview->selected ||
1737             gtk_ctree_is_ancestor(ctree,
1738                                   folderview->selected,
1739                                   folderview->opened)) {
1740                 summary_clear_all(folderview->summaryview);
1741                 folderview->opened = NULL;
1742         }
1743
1744         account_destroy(item->folder->account);
1745         folder_destroy(item->folder);
1746         gtk_ctree_remove_node(ctree, folderview->selected);
1747         account_set_menu();
1748         main_window_reflect_prefs_all();
1749         folder_write_list();
1750 }
1751
1752 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
1753                                           GdkDragContext *context,
1754                                           gint            x,
1755                                           gint            y,
1756                                           guint           time,
1757                                           FolderView     *folderview)
1758 {
1759         gint row, column;
1760         FolderItem *item;
1761         GtkCTreeNode *node;
1762
1763         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
1764                                          x - 24, y - 24, &row, &column)) {
1765                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
1766                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
1767                 if (item != NULL &&
1768                     item->path != NULL &&
1769                     folderview->summaryview->folder_item != item) {
1770                         if (item->folder->type != F_NEWS) {
1771                                 gtk_ctree_select(GTK_CTREE(widget), node);
1772                                 gdk_drag_status(context,
1773                                                 context->suggested_action,
1774                                                 time);
1775                                 return TRUE;
1776                         }
1777                 }
1778         }
1779
1780         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
1781         gdk_drag_status(context, 0, time);
1782
1783         return FALSE;
1784 }
1785
1786 static void folderview_drag_leave_cb(GtkWidget      *widget,
1787                                      GdkDragContext *context,
1788                                      guint           time,
1789                                      FolderView     *folderview)
1790 {
1791         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
1792 }
1793
1794 static void folderview_drag_received_cb(GtkWidget        *widget,
1795                                         GdkDragContext   *drag_context,
1796                                         gint              x,
1797                                         gint              y,
1798                                         GtkSelectionData *data,
1799                                         guint             info,
1800                                         guint             time,
1801                                         FolderView       *folderview)
1802 {
1803         gint row, column;
1804         FolderItem *item;
1805         GtkCTreeNode *node;
1806
1807         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
1808                                          x - 24, y - 24, &row, &column) == 0)
1809                 return;
1810
1811         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
1812         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
1813         if (item != NULL) {
1814                 summary_move_selected_to(folderview->summaryview, item);
1815                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
1816         } else
1817                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
1818 }
1819
1820 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
1821 {
1822         const FolderItem *item = a;
1823         const gchar *name = b;
1824
1825         if (!item->path) return -1;
1826         return strcmp2(g_basename(item->path), name);
1827 }