sync with sylpheed 0.6.5cvs7
[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 (item->folder->type != F_MBOX) {
1535                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1536                         alertpanel_error(_("`%c' can't be included in folder name."),
1537                                          G_DIR_SEPARATOR);
1538                         g_free(new_folder);
1539                         return;
1540                 }
1541         }
1542
1543         /* find whether the directory already exists */
1544         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1545                 alertpanel_error(_("The folder `%s' already exists."),
1546                                  new_folder);
1547                 g_free(new_folder);
1548                 return;
1549         }
1550
1551         new_item = item->folder->create_folder(item->folder, item, new_folder);
1552         if (!new_item) {
1553                 alertpanel_error(_("The folder `%s' could not be created."), 
1554                                  new_folder);
1555                 g_free(new_folder);
1556                 return;
1557         } 
1558         g_free(new_folder);
1559
1560         gtk_clist_freeze(GTK_CLIST(ctree));
1561
1562         text[COL_FOLDER] = new_item->name;
1563         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1564                                      FOLDER_SPACING,
1565                                      folderxpm, folderxpmmask,
1566                                      folderopenxpm, folderopenxpmmask,
1567                                      FALSE, FALSE);
1568         gtk_ctree_expand(ctree, folderview->selected);
1569         gtk_ctree_node_set_row_data(ctree, node, new_item);
1570         folderview_sort_folders(folderview, folderview->selected, item->folder);
1571
1572         gtk_clist_thaw(GTK_CLIST(ctree));
1573
1574         folder_write_list();
1575 }
1576
1577 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1578                                           GtkWidget *widget)
1579 {
1580         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1581         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1582         FolderItem *item;
1583         FolderItem *new_item;
1584         gchar *new_folder;
1585         GtkCTreeNode *node;
1586
1587         if (!folderview->selected) return;
1588
1589         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1590         g_return_if_fail(item != NULL);
1591         g_return_if_fail(item->folder != NULL);
1592
1593         new_folder = input_dialog(_("New folder"),
1594                                   _("Input the name of new folder:"),
1595                                   _("NewFolder"));
1596         if (!new_folder) return;
1597
1598         /* find whether the directory already exists */
1599         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1600                 alertpanel_error(_("The folder `%s' already exists."),
1601                                  new_folder);
1602                 g_free(new_folder);
1603                 return;
1604         }
1605
1606         new_item = item->folder->create_folder(item->folder, item, new_folder);
1607         g_free(new_folder);
1608         if (!new_item) return;
1609
1610         gtk_clist_freeze(GTK_CLIST(ctree));
1611
1612         text[COL_FOLDER] = new_item->name;
1613         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1614                                      FOLDER_SPACING,
1615                                      folderxpm, folderxpmmask,
1616                                      folderopenxpm, folderopenxpmmask,
1617                                      FALSE, FALSE);
1618         gtk_ctree_expand(ctree, folderview->selected);
1619         gtk_ctree_node_set_row_data(ctree, node, new_item);
1620         folderview_sort_folders(folderview, folderview->selected, item->folder);
1621
1622         gtk_clist_thaw(GTK_CLIST(ctree));
1623
1624         folder_write_list();
1625 }
1626
1627 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1628                                         GtkWidget *widget)
1629 {
1630         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1631         FolderItem *item;
1632         gchar *new_folder;
1633         gchar *message;
1634
1635         if (!folderview->selected) return;
1636
1637         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1638         g_return_if_fail(item != NULL);
1639         g_return_if_fail(item->path != NULL);
1640         g_return_if_fail(item->folder != NULL);
1641
1642         message = g_strdup_printf(_("Input new name for `%s':"),
1643                                   g_basename(item->path));
1644         new_folder = input_dialog(_("Rename folder"), message,
1645                                   g_basename(item->path));
1646         g_free(message);
1647         if (!new_folder) return;
1648
1649         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1650                 alertpanel_error(_("`%c' can't be included in folder name."),
1651                                  G_DIR_SEPARATOR);
1652                 g_free(new_folder);
1653                 return;
1654         }
1655
1656         if (folderview_find_by_name
1657                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1658                  new_folder)) {
1659                 alertpanel_error(_("The folder `%s' already exists."),
1660                                  new_folder);
1661                 g_free(new_folder);
1662                 return;
1663         }
1664
1665         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1666                 g_free(new_folder);
1667                 return;
1668         }
1669         g_free(new_folder);
1670
1671         gtk_clist_freeze(GTK_CLIST(ctree));
1672
1673         folderview_update_node(folderview, folderview->selected);
1674         folderview_sort_folders(folderview,
1675                                 GTK_CTREE_ROW(folderview->selected)->parent,
1676                                 item->folder);
1677         if (folderview->opened == folderview->selected) {
1678                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1679                         gtk_ctree_expand(ctree, folderview->opened);
1680                 summary_show(folderview->summaryview, item, FALSE);
1681         }
1682
1683         gtk_clist_thaw(GTK_CLIST(ctree));
1684
1685         folder_write_list();
1686 }
1687
1688 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
1689                                              guint action,
1690                                              GtkWidget *widget)
1691 {
1692         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1693         FolderItem *item;
1694         gchar *new_folder;
1695         gchar *message;
1696
1697         if (!folderview->selected) return;
1698
1699         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1700         g_return_if_fail(item != NULL);
1701         g_return_if_fail(item->path != NULL);
1702         g_return_if_fail(item->folder != NULL);
1703
1704         message = g_strdup_printf(_("Input new name for `%s':"),
1705                                   g_basename(item->path));
1706         new_folder = input_dialog(_("Rename folder"), message,
1707                                   g_basename(item->path));
1708         g_free(message);
1709         if (!new_folder) return;
1710
1711         if (folderview_find_by_name
1712                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1713                  new_folder)) {
1714                 alertpanel_error(_("The folder `%s' already exists."),
1715                                  new_folder);
1716                 g_free(new_folder);
1717                 return;
1718         }
1719
1720         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1721                 g_free(new_folder);
1722                 return;
1723         }
1724         g_free(new_folder);
1725
1726         gtk_clist_freeze(GTK_CLIST(ctree));
1727
1728         folderview_update_node(folderview, folderview->selected);
1729         folderview_sort_folders(folderview,
1730                                 GTK_CTREE_ROW(folderview->selected)->parent,
1731                                 item->folder);
1732         if (folderview->opened == folderview->selected) {
1733                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1734                         gtk_ctree_expand(ctree, folderview->opened);
1735                 summary_show(folderview->summaryview, item, FALSE);
1736         }
1737
1738         gtk_clist_thaw(GTK_CLIST(ctree));
1739
1740         folder_write_list();
1741 }
1742
1743 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1744                                         GtkWidget *widget)
1745 {
1746         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1747         FolderItem *item;
1748         gchar *message;
1749         AlertValue avalue;
1750
1751         if (!folderview->selected) return;
1752
1753         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1754         g_return_if_fail(item != NULL);
1755         g_return_if_fail(item->path != NULL);
1756         g_return_if_fail(item->folder != NULL);
1757
1758         message = g_strdup_printf
1759                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1760                    "Do you really want to delete?"),
1761                  g_basename(item->path));
1762         avalue = alertpanel(_("Delete folder"), message,
1763                             _("Yes"), _("+No"), NULL);
1764         g_free(message);
1765         if (avalue != G_ALERTDEFAULT) return;
1766
1767         if (item->folder->remove_folder(item->folder, item) < 0) {
1768                 alertpanel_error(_("Can't remove the folder `%s'."),
1769                                  item->path);
1770                 return;
1771         }
1772
1773         if (folderview->opened == folderview->selected ||
1774             gtk_ctree_is_ancestor(ctree,
1775                                   folderview->selected,
1776                                   folderview->opened)) {
1777                 summary_clear_all(folderview->summaryview);
1778                 folderview->opened = NULL;
1779         }
1780
1781         gtk_ctree_remove_node(ctree, folderview->selected);
1782         folder_write_list();
1783 }
1784
1785 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
1786                                          GtkWidget *widget)
1787 {
1788         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1789         GtkCTreeNode *node;
1790         FolderItem *item;
1791         gchar *message;
1792         AlertValue avalue;
1793
1794         if (!folderview->selected) return;
1795         node = folderview->selected;
1796         item = gtk_ctree_node_get_row_data(ctree, node);
1797         g_return_if_fail(item != NULL);
1798         g_return_if_fail(item->folder != NULL);
1799         if (item->parent) return;
1800
1801         message = g_strdup_printf
1802                 (_("Really remove the mailbox `%s' ?\n"
1803                    "(The messages are NOT deleted from disk)"),
1804                  item->folder->name);
1805         avalue = alertpanel(_("Remove folder"), message,
1806                             _("Yes"), _("+No"), NULL);
1807         g_free(message);
1808         if (avalue != G_ALERTDEFAULT) return;
1809
1810         folder_destroy(item->folder);
1811         summary_clear_all(folderview->summaryview);
1812         folderview_unselect(folderview);
1813         gtk_ctree_remove_node(ctree, node);
1814         folder_write_list();
1815 }
1816
1817 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
1818                                           GtkWidget *widget)
1819 {
1820         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1821         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1822         GtkCTreeNode *node;
1823         FolderItem *item;
1824         FolderItem *new_item;
1825         gchar *new_folder;
1826         gchar *p;
1827
1828         if (!folderview->selected) return;
1829
1830         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1831         g_return_if_fail(item != NULL);
1832         g_return_if_fail(item->folder != NULL);
1833         g_return_if_fail(item->folder->type == F_IMAP);
1834         g_return_if_fail(item->folder->account != NULL);
1835
1836         new_folder = input_dialog
1837                 (_("New folder"),
1838                  _("Input the name of new folder:\n"
1839                    "(if you want to create a folder to store subfolders,\n"
1840                    " append `/' at the end of the name)"),
1841                  _("NewFolder"));
1842         if (!new_folder) return;
1843
1844         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
1845             *(p + 1) != '\0') {
1846                 alertpanel_error(_("`%c' can't be included in folder name."),
1847                                  G_DIR_SEPARATOR);
1848                 g_free(new_folder);
1849                 return;
1850         }
1851
1852         /* find whether the directory already exists */
1853         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1854                 alertpanel_error(_("The folder `%s' already exists."),
1855                                  new_folder);
1856                 g_free(new_folder);
1857                 return;
1858         }
1859
1860         new_item = item->folder->create_folder(item->folder, item, new_folder);
1861         if (!new_item) {
1862                 alertpanel_error(_("Can't create the folder `%s'."),
1863                                  new_folder);
1864                 g_free(new_folder);
1865                 return;
1866         }
1867         g_free(new_folder);
1868
1869         gtk_clist_freeze(GTK_CLIST(ctree));
1870
1871         text[COL_FOLDER] = new_item->name;
1872         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1873                                      FOLDER_SPACING,
1874                                      folderxpm, folderxpmmask,
1875                                      folderopenxpm, folderopenxpmmask,
1876                                      FALSE, FALSE);
1877         gtk_ctree_expand(ctree, folderview->selected);
1878         gtk_ctree_node_set_row_data(ctree, node, new_item);
1879         folderview_sort_folders(folderview, folderview->selected, item->folder);
1880
1881         gtk_clist_thaw(GTK_CLIST(ctree));
1882
1883         folder_write_list();
1884 }
1885
1886 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
1887                                          GtkWidget *widget)
1888 {
1889         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1890         FolderItem *item;
1891         gchar *message;
1892         AlertValue avalue;
1893
1894         if (!folderview->selected) return;
1895
1896         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1897         g_return_if_fail(item != NULL);
1898         g_return_if_fail(item->folder != NULL);
1899         g_return_if_fail(item->folder->type == F_IMAP);
1900         g_return_if_fail(item->folder->account != NULL);
1901
1902         message = g_strdup_printf(_("Really delete folder `%s'?"),
1903                                   g_basename(item->path));
1904         avalue = alertpanel(_("Delete folder"), message,
1905                             _("Yes"), _("+No"), NULL);
1906         g_free(message);
1907         if (avalue != G_ALERTDEFAULT) return;
1908
1909         if (item->folder->remove_folder(item->folder, item) < 0) {
1910                 alertpanel_error(_("Can't remove the folder `%s'."),
1911                                  item->path);
1912                 if (folderview->opened == folderview->selected)
1913                         summary_show(folderview->summaryview,
1914                                      folderview->summaryview->folder_item,
1915                                      FALSE);
1916                 return;
1917         }
1918
1919         if (folderview->opened == folderview->selected ||
1920             gtk_ctree_is_ancestor(ctree,
1921                                   folderview->selected,
1922                                   folderview->opened)) {
1923                 summary_clear_all(folderview->summaryview);
1924                 folderview->opened = NULL;
1925         }
1926
1927         gtk_ctree_remove_node(ctree, folderview->selected);
1928         folder_write_list();
1929 }
1930
1931 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
1932                                          GtkWidget *widget)
1933 {
1934         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1935         FolderItem *item;
1936         gchar *message;
1937         AlertValue avalue;
1938
1939         if (!folderview->selected) return;
1940
1941         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1942         g_return_if_fail(item != NULL);
1943         g_return_if_fail(item->folder != NULL);
1944         g_return_if_fail(item->folder->type == F_IMAP);
1945         g_return_if_fail(item->folder->account != NULL);
1946
1947         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"),
1948                                   item->folder->name);
1949         avalue = alertpanel(_("Delete IMAP4 account"), message,
1950                             _("Yes"), _("+No"), NULL);
1951         g_free(message);
1952
1953         if (avalue != G_ALERTDEFAULT) return;
1954
1955         if (folderview->opened == folderview->selected ||
1956             gtk_ctree_is_ancestor(ctree,
1957                                   folderview->selected,
1958                                   folderview->opened)) {
1959                 summary_clear_all(folderview->summaryview);
1960                 folderview->opened = NULL;
1961         }
1962
1963         account_destroy(item->folder->account);
1964         folder_destroy(item->folder);
1965         gtk_ctree_remove_node(ctree, folderview->selected);
1966         account_set_menu();
1967         main_window_reflect_prefs_all();
1968         folder_write_list();
1969 }
1970
1971 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
1972                                          GtkWidget *widget)
1973 {
1974         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1975         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1976         GtkCTreeNode *servernode, *node;
1977         Folder *folder;
1978         FolderItem *item;
1979         FolderItem *rootitem;
1980         FolderItem *newitem;
1981         GSList *new_subscr;
1982         GSList *cur;
1983         GNode *gnode;
1984
1985         if (!folderview->selected) return;
1986
1987         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1988         g_return_if_fail(item != NULL);
1989         folder = item->folder;
1990         g_return_if_fail(folder != NULL);
1991         g_return_if_fail(folder->type == F_NEWS);
1992         g_return_if_fail(folder->account != NULL);
1993
1994         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
1995                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
1996         else
1997                 servernode = folderview->selected;
1998
1999         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2000
2001         new_subscr = grouplist_dialog(folder);
2002
2003         /* remove unsubscribed newsgroups */
2004         for (gnode = folder->node->children; gnode != NULL; ) {
2005                 GNode *next = gnode->next;
2006
2007                 item = FOLDER_ITEM(gnode->data);
2008                 if (g_slist_find_custom(new_subscr, item->name,
2009                                         (GCompareFunc)g_strcasecmp) != NULL) {
2010                         gnode = next;
2011                         continue;
2012                 }
2013
2014                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2015                 if (!node) {
2016                         gnode = next;
2017                         continue;
2018                 }
2019
2020                 if (folderview->opened == node) {
2021                         summary_clear_all(folderview->summaryview);
2022                         folderview->opened = NULL;
2023                 }
2024
2025                 folder_item_remove(item);
2026                 gtk_ctree_remove_node(ctree, node);
2027
2028                 gnode = next;
2029         }
2030
2031         gtk_clist_freeze(GTK_CLIST(ctree));
2032
2033         /* add subscribed newsgroups */
2034         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2035                 gchar *name = (gchar *)cur->data;
2036
2037                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2038                         continue;
2039
2040                 text[COL_FOLDER] = name;
2041                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2042                                              FOLDER_SPACING,
2043                                              folderxpm, folderxpmmask,
2044                                              folderopenxpm, folderopenxpmmask,
2045                                              FALSE, FALSE);
2046                 gtk_ctree_expand(ctree, servernode);
2047
2048                 newitem = folder_item_new(name, name);
2049                 folder_item_append(rootitem, newitem);
2050                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2051         }
2052
2053         gtk_ctree_sort_node(ctree, servernode);
2054         gtk_clist_thaw(GTK_CLIST(ctree));
2055
2056         slist_free_strings(new_subscr);
2057         g_slist_free(new_subscr);
2058
2059         folder_write_list();
2060 }
2061
2062 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2063                                         GtkWidget *widget)
2064 {
2065         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2066         FolderItem *item;
2067         gchar *message;
2068         AlertValue avalue;
2069
2070         if (!folderview->selected) return;
2071
2072         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2073         g_return_if_fail(item != NULL);
2074         g_return_if_fail(item->folder != NULL);
2075         g_return_if_fail(item->folder->type == F_NEWS);
2076         g_return_if_fail(item->folder->account != NULL);
2077
2078         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
2079                                   g_basename(item->path));
2080         avalue = alertpanel(_("Delete newsgroup"), message,
2081                             _("Yes"), _("+No"), NULL);
2082         g_free(message);
2083         if (avalue != G_ALERTDEFAULT) return;
2084
2085         if (folderview->opened == folderview->selected) {
2086                 summary_clear_all(folderview->summaryview);
2087                 folderview->opened = NULL;
2088         }
2089
2090         folder_item_remove(item);
2091         gtk_ctree_remove_node(ctree, folderview->selected);
2092         folder_write_list();
2093 }
2094
2095 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2096                                          GtkWidget *widget)
2097 {
2098         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2099         FolderItem *item;
2100         gchar *message;
2101         AlertValue avalue;
2102
2103         if (!folderview->selected) return;
2104
2105         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2106         g_return_if_fail(item != NULL);
2107         g_return_if_fail(item->folder != NULL);
2108         g_return_if_fail(item->folder->type == F_NEWS);
2109         g_return_if_fail(item->folder->account != NULL);
2110
2111         message = g_strdup_printf(_("Really delete news account `%s'?"),
2112                                   item->folder->name);
2113         avalue = alertpanel(_("Delete news account"), message,
2114                             _("Yes"), _("+No"), NULL);
2115         g_free(message);
2116
2117         if (avalue != G_ALERTDEFAULT) return;
2118
2119         if (folderview->opened == folderview->selected ||
2120             gtk_ctree_is_ancestor(ctree,
2121                                   folderview->selected,
2122                                   folderview->opened)) {
2123                 summary_clear_all(folderview->summaryview);
2124                 folderview->opened = NULL;
2125         }
2126
2127         account_destroy(item->folder->account);
2128         folder_destroy(item->folder);
2129         gtk_ctree_remove_node(ctree, folderview->selected);
2130         account_set_menu();
2131         main_window_reflect_prefs_all();
2132         folder_write_list();
2133 }
2134
2135 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2136                                           GdkDragContext *context,
2137                                           gint            x,
2138                                           gint            y,
2139                                           guint           time,
2140                                           FolderView     *folderview)
2141 {
2142         gint row, column;
2143         FolderItem *item, *current_item;
2144         GtkCTreeNode *node = NULL;
2145         gboolean acceptable = FALSE;
2146
2147         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
2148                                          x - 24, y - 24, &row, &column)) {
2149                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2150                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2151                 current_item = folderview->summaryview->folder_item;
2152                 if (item != NULL &&
2153                     item->path != NULL &&
2154                     current_item != NULL &&
2155                     current_item != item) {
2156                         switch (item->folder->type){
2157                         case F_MH:
2158                                 if (current_item->folder->type == F_MH)
2159                                     acceptable = TRUE;
2160                                 break;
2161                         case F_IMAP:
2162                                 if (current_item->folder->account == item->folder->account)
2163                                     acceptable = TRUE;
2164                                 break;
2165                         default:
2166                         }
2167                 }
2168         }
2169
2170         if (acceptable) {
2171                 gtk_ctree_select(GTK_CTREE(widget), node);
2172                 gdk_drag_status(context, context->suggested_action, time);
2173         } else {
2174                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2175                 gdk_drag_status(context, 0, time);
2176         }
2177
2178         return acceptable;
2179 }
2180
2181 static void folderview_drag_leave_cb(GtkWidget      *widget,
2182                                      GdkDragContext *context,
2183                                      guint           time,
2184                                      FolderView     *folderview)
2185 {
2186         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2187 }
2188
2189 static void folderview_drag_received_cb(GtkWidget        *widget,
2190                                         GdkDragContext   *drag_context,
2191                                         gint              x,
2192                                         gint              y,
2193                                         GtkSelectionData *data,
2194                                         guint             info,
2195                                         guint             time,
2196                                         FolderView       *folderview)
2197 {
2198         gint row, column;
2199         FolderItem *item;
2200         GtkCTreeNode *node;
2201
2202         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
2203                                          x - 24, y - 24, &row, &column) == 0)
2204                 return;
2205
2206         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2207         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2208         if (item != NULL) {
2209                 summary_move_selected_to(folderview->summaryview, item);
2210                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2211         } else
2212                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2213 }
2214
2215 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2216 {
2217         const FolderItem *item = a;
2218         const gchar *name = b;
2219
2220         if (!item->path) return -1;
2221         return strcmp2(g_basename(item->path), name);
2222 }
2223
2224 static void folderview_scoring_cb(FolderView *folderview, guint action,
2225                                    GtkWidget *widget)
2226 {
2227         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2228         FolderItem *item;
2229
2230         if (!folderview->selected) return;
2231
2232         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2233         g_return_if_fail(item != NULL);
2234         g_return_if_fail(item->folder != NULL);
2235
2236         prefs_scoring_open(item);
2237 }
2238
2239 static void folderview_property_cb(FolderView *folderview, guint action, GtkWidget *widget) {
2240         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2241         FolderItem *item;
2242
2243         if (!folderview->selected) return;
2244
2245         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2246         g_return_if_fail(item != NULL);
2247         g_return_if_fail(item->folder != NULL);
2248
2249         prefs_folder_item_create(item);
2250 }
2251
2252 void folderview_set_target_folder_color(gint color_op) {
2253         gint firstone = 1;
2254         GList *list;
2255         FolderView *folderview;
2256
2257         for (list = folderview_list; list != NULL; list = list->next) {
2258                 folderview = (FolderView *)list->data;
2259                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2260                 if (firstone) {
2261                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2262                                 folderview->color_op;
2263                         firstone = 0;
2264                 }
2265         }
2266 }