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