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