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