error panel when a folder could not be created (closes bug #471080)
[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 (FOLDER_TYPE(folder) == F_NEWS) {
1257                         if (item->parent != NULL)
1258                                 delete_folder = folder_scoring = TRUE;
1259                 }
1260         }
1261
1262 #define SET_SENS(factory, name, sens) \
1263         menu_set_sensitive(folderview->factory, name, sens)
1264
1265         if (FOLDER_IS_LOCAL(folder)) {
1266                 popup = folderview->mail_popup;
1267                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1268                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1269                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1270                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1271                 SET_SENS(mail_factory, "/Update folder tree", update_tree);
1272                 SET_SENS(mail_factory, "/Rescan folder tree", rescan_tree);
1273                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1274                 SET_SENS(mail_factory, "/Property...", folder_property);
1275                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1276         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1277                 popup = folderview->imap_popup;
1278                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1279                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1280                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1281                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1282                 SET_SENS(imap_factory, "/Update folder tree", update_tree);
1283                 SET_SENS(imap_factory, "/Rescan folder tree", rescan_tree);
1284                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1285                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1286         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1287                 popup = folderview->news_popup;
1288                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1289                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1290                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1291                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1292                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1293         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1294                 popup = folderview->mbox_popup;
1295                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1296                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1297                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1298                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1299                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1300         } else
1301                 return;
1302
1303 #undef SET_SENS
1304
1305         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1306                        event->button, event->time);
1307 }
1308
1309 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1310                                        FolderView *folderview)
1311 {
1312         if (!event) return;
1313
1314         if (event->button == 1 && folderview->open_folder == FALSE &&
1315             folderview->opened != NULL) {
1316                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1317                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1318                                           folderview->opened);
1319         }
1320 }
1321
1322 #define BREAK_ON_MODIFIER_KEY() \
1323         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1324
1325 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1326                                    FolderView *folderview)
1327 {
1328         if (!event) return;
1329
1330         switch (event->keyval) {
1331         case GDK_Return:
1332         case GDK_space:
1333                 if (folderview->selected) {
1334                         folderview_select_node(folderview,
1335                                                folderview->selected);
1336                 }
1337                 break;
1338         case GDK_v:
1339         case GDK_V:
1340         case GDK_g:
1341         case GDK_G:
1342         case GDK_x:
1343         case GDK_X:
1344         case GDK_w:
1345         case GDK_D:
1346         case GDK_Q:
1347                 BREAK_ON_MODIFIER_KEY();
1348                 summary_pass_key_press_event(folderview->summaryview, event);
1349         default:
1350         }
1351 }
1352
1353 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1354                                 gint column, FolderView *folderview)
1355 {
1356         static gboolean can_select = TRUE;      /* exclusive lock */
1357         gboolean opened;
1358         FolderItem *item;
1359
1360         folderview->selected = row;
1361
1362         if (folderview->opened == row) {
1363                 folderview->open_folder = FALSE;
1364                 return;
1365         }
1366
1367         if (!can_select) {
1368                 gtk_ctree_select(ctree, folderview->opened);
1369                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1370                 return;
1371         }
1372
1373         if (!folderview->open_folder) return;
1374
1375         item = gtk_ctree_node_get_row_data(ctree, row);
1376         if (!item) return;
1377
1378         can_select = FALSE;
1379
1380         /* CLAWS: set compose button type: news folder items 
1381          * always have a news folder as parent */
1382         if (item->folder) 
1383                 main_window_toolbar_set_compose_button
1384                         (folderview->mainwin,
1385                          item->folder->type == F_NEWS ? 
1386                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1387          
1388         if (item->path)
1389                 debug_print(_("Folder %s is selected\n"), item->path);
1390
1391         if (!GTK_CTREE_ROW(row)->children)
1392                 gtk_ctree_expand(ctree, row);
1393         if (folderview->opened &&
1394             !GTK_CTREE_ROW(folderview->opened)->children)
1395                 gtk_ctree_collapse(ctree, folderview->opened);
1396
1397         /* ungrab the mouse event */
1398         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1399                 gtk_grab_remove(GTK_WIDGET(ctree));
1400                 if (gdk_pointer_is_grabbed())
1401                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1402         }
1403
1404         opened = summary_show(folderview->summaryview, item, FALSE);
1405
1406         if (!opened) {
1407                 gtk_ctree_select(ctree, folderview->opened);
1408                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1409         } else
1410                 folderview->opened = row;
1411
1412         folderview->open_folder = FALSE;
1413         can_select = TRUE;
1414 }
1415
1416 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1417                                      FolderView *folderview)
1418 {
1419         FolderItem *item;
1420
1421         item = gtk_ctree_node_get_row_data(ctree, node);
1422         g_return_if_fail(item != NULL);
1423         item->collapsed = FALSE;
1424         folderview_update_node(folderview, node);
1425 }
1426
1427 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1428                                       FolderView *folderview)
1429 {
1430         FolderItem *item;
1431
1432         item = gtk_ctree_node_get_row_data(ctree, node);
1433         g_return_if_fail(item != NULL);
1434         item->collapsed= TRUE;
1435         folderview_update_node(folderview, node);
1436 }
1437
1438 static void folderview_popup_close(GtkMenuShell *menu_shell,
1439                                    FolderView *folderview)
1440 {
1441         if (!folderview->opened) return;
1442
1443         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1444         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1445                                   folderview->opened);
1446 }
1447
1448 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1449                                    FolderView *folderview)
1450 {
1451         switch (column) {
1452         case COL_FOLDER:
1453                 prefs_common.folder_col_folder = width;
1454                 break;
1455         case COL_NEW:
1456                 prefs_common.folder_col_new = width;
1457                 break;
1458         case COL_UNREAD:
1459                 prefs_common.folder_col_unread = width;
1460                 break;
1461         case COL_TOTAL:
1462                 prefs_common.folder_col_total = width;
1463                 break;
1464         default:
1465         }
1466 }
1467
1468 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1469                                              GtkCTreeNode *node,
1470                                              const gchar *name)
1471 {
1472         FolderItem *item;
1473
1474         if (!node)
1475                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1476         if (!node)
1477                 return NULL;
1478
1479         node = GTK_CTREE_ROW(node)->children;
1480
1481         while (node) {
1482                 item = gtk_ctree_node_get_row_data(ctree, node);
1483                 if (!folderview_compare_name(item, name))
1484                         return node;
1485                 node = GTK_CTREE_ROW(node)->sibling;
1486         }
1487
1488         return NULL;
1489 }
1490
1491 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1492                                       GtkWidget *widget)
1493 {
1494         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1495         FolderItem *item;
1496
1497         if (!folderview->selected) return;
1498
1499         summary_show(folderview->summaryview, NULL, FALSE);
1500
1501         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1502         g_return_if_fail(item != NULL);
1503         g_return_if_fail(item->folder != NULL);
1504
1505         if (action == 0)
1506                 folderview_update_all_node();
1507         else
1508                 folderview_update_tree(item->folder);
1509 }
1510
1511 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1512                                      GtkWidget *widget)
1513 {
1514         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1515         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1516         FolderItem *item;
1517         FolderItem *new_item;
1518         gchar *new_folder;
1519         GtkCTreeNode *node;
1520
1521         if (!folderview->selected) return;
1522
1523         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1524         g_return_if_fail(item != NULL);
1525         g_return_if_fail(item->folder != NULL);
1526
1527         new_folder = input_dialog(_("New folder"),
1528                                   _("Input the name of new folder:"),
1529                                   _("NewFolder"));
1530         if (!new_folder) return;
1531
1532         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1533                 alertpanel_error(_("`%c' can't be included in folder name."),
1534                                  G_DIR_SEPARATOR);
1535                 g_free(new_folder);
1536                 return;
1537         }
1538
1539         /* find whether the directory already exists */
1540         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1541                 alertpanel_error(_("The folder `%s' already exists."),
1542                                  new_folder);
1543                 g_free(new_folder);
1544                 return;
1545         }
1546
1547         new_item = item->folder->create_folder(item->folder, item, new_folder);
1548         if (!new_item) {
1549                 alertpanel_error(_("The folder `%s' could not be created."), 
1550                                  new_folder);
1551                 g_free(new_folder);
1552                 return;
1553         } 
1554         g_free(new_folder);
1555
1556         gtk_clist_freeze(GTK_CLIST(ctree));
1557
1558         text[COL_FOLDER] = new_item->name;
1559         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1560                                      FOLDER_SPACING,
1561                                      folderxpm, folderxpmmask,
1562                                      folderopenxpm, folderopenxpmmask,
1563                                      FALSE, FALSE);
1564         gtk_ctree_expand(ctree, folderview->selected);
1565         gtk_ctree_node_set_row_data(ctree, node, new_item);
1566         folderview_sort_folders(folderview, folderview->selected, item->folder);
1567
1568         gtk_clist_thaw(GTK_CLIST(ctree));
1569
1570         folder_write_list();
1571 }
1572
1573 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1574                                           GtkWidget *widget)
1575 {
1576         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1577         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1578         FolderItem *item;
1579         FolderItem *new_item;
1580         gchar *new_folder;
1581         GtkCTreeNode *node;
1582
1583         if (!folderview->selected) return;
1584
1585         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1586         g_return_if_fail(item != NULL);
1587         g_return_if_fail(item->folder != NULL);
1588
1589         new_folder = input_dialog(_("New folder"),
1590                                   _("Input the name of new folder:"),
1591                                   _("NewFolder"));
1592         if (!new_folder) return;
1593
1594         /* find whether the directory already exists */
1595         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1596                 alertpanel_error(_("The folder `%s' already exists."),
1597                                  new_folder);
1598                 g_free(new_folder);
1599                 return;
1600         }
1601
1602         new_item = item->folder->create_folder(item->folder, item, new_folder);
1603         g_free(new_folder);
1604         if (!new_item) return;
1605
1606         gtk_clist_freeze(GTK_CLIST(ctree));
1607
1608         text[COL_FOLDER] = new_item->name;
1609         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1610                                      FOLDER_SPACING,
1611                                      folderxpm, folderxpmmask,
1612                                      folderopenxpm, folderopenxpmmask,
1613                                      FALSE, FALSE);
1614         gtk_ctree_expand(ctree, folderview->selected);
1615         gtk_ctree_node_set_row_data(ctree, node, new_item);
1616         folderview_sort_folders(folderview, folderview->selected, item->folder);
1617
1618         gtk_clist_thaw(GTK_CLIST(ctree));
1619
1620         folder_write_list();
1621 }
1622
1623 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1624                                         GtkWidget *widget)
1625 {
1626         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1627         FolderItem *item;
1628         gchar *new_folder;
1629         gchar *message;
1630
1631         if (!folderview->selected) return;
1632
1633         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1634         g_return_if_fail(item != NULL);
1635         g_return_if_fail(item->path != NULL);
1636         g_return_if_fail(item->folder != NULL);
1637
1638         message = g_strdup_printf(_("Input new name for `%s':"),
1639                                   g_basename(item->path));
1640         new_folder = input_dialog(_("Rename folder"), message,
1641                                   g_basename(item->path));
1642         g_free(message);
1643         if (!new_folder) return;
1644
1645         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1646                 alertpanel_error(_("`%c' can't be included in folder name."),
1647                                  G_DIR_SEPARATOR);
1648                 g_free(new_folder);
1649                 return;
1650         }
1651
1652         if (folderview_find_by_name
1653                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1654                  new_folder)) {
1655                 alertpanel_error(_("The folder `%s' already exists."),
1656                                  new_folder);
1657                 g_free(new_folder);
1658                 return;
1659         }
1660
1661         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1662                 g_free(new_folder);
1663                 return;
1664         }
1665         g_free(new_folder);
1666
1667         gtk_clist_freeze(GTK_CLIST(ctree));
1668
1669         folderview_update_node(folderview, folderview->selected);
1670         folderview_sort_folders(folderview,
1671                                 GTK_CTREE_ROW(folderview->selected)->parent,
1672                                 item->folder);
1673         if (folderview->opened == folderview->selected) {
1674                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1675                         gtk_ctree_expand(ctree, folderview->opened);
1676                 summary_show(folderview->summaryview, item, FALSE);
1677         }
1678
1679         gtk_clist_thaw(GTK_CLIST(ctree));
1680
1681         folder_write_list();
1682 }
1683
1684 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
1685                                              guint action,
1686                                              GtkWidget *widget)
1687 {
1688         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1689         FolderItem *item;
1690         gchar *new_folder;
1691         gchar *message;
1692
1693         if (!folderview->selected) return;
1694
1695         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1696         g_return_if_fail(item != NULL);
1697         g_return_if_fail(item->path != NULL);
1698         g_return_if_fail(item->folder != NULL);
1699
1700         message = g_strdup_printf(_("Input new name for `%s':"),
1701                                   g_basename(item->path));
1702         new_folder = input_dialog(_("Rename folder"), message,
1703                                   g_basename(item->path));
1704         g_free(message);
1705         if (!new_folder) return;
1706
1707         if (folderview_find_by_name
1708                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1709                  new_folder)) {
1710                 alertpanel_error(_("The folder `%s' already exists."),
1711                                  new_folder);
1712                 g_free(new_folder);
1713                 return;
1714         }
1715
1716         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1717                 g_free(new_folder);
1718                 return;
1719         }
1720         g_free(new_folder);
1721
1722         gtk_clist_freeze(GTK_CLIST(ctree));
1723
1724         folderview_update_node(folderview, folderview->selected);
1725         folderview_sort_folders(folderview,
1726                                 GTK_CTREE_ROW(folderview->selected)->parent,
1727                                 item->folder);
1728         if (folderview->opened == folderview->selected) {
1729                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1730                         gtk_ctree_expand(ctree, folderview->opened);
1731                 summary_show(folderview->summaryview, item, FALSE);
1732         }
1733
1734         gtk_clist_thaw(GTK_CLIST(ctree));
1735
1736         folder_write_list();
1737 }
1738
1739 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1740                                         GtkWidget *widget)
1741 {
1742         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1743         FolderItem *item;
1744         gchar *message;
1745         AlertValue avalue;
1746
1747         if (!folderview->selected) return;
1748
1749         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1750         g_return_if_fail(item != NULL);
1751         g_return_if_fail(item->path != NULL);
1752         g_return_if_fail(item->folder != NULL);
1753
1754         message = g_strdup_printf
1755                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1756                    "Do you really want to delete?"),
1757                  g_basename(item->path));
1758         avalue = alertpanel(_("Delete folder"), message,
1759                             _("Yes"), _("+No"), NULL);
1760         g_free(message);
1761         if (avalue != G_ALERTDEFAULT) return;
1762
1763         if (item->folder->remove_folder(item->folder, item) < 0) {
1764                 alertpanel_error(_("Can't remove the folder `%s'."),
1765                                  item->path);
1766                 return;
1767         }
1768
1769         if (folderview->opened == folderview->selected ||
1770             gtk_ctree_is_ancestor(ctree,
1771                                   folderview->selected,
1772                                   folderview->opened)) {
1773                 summary_clear_all(folderview->summaryview);
1774                 folderview->opened = NULL;
1775         }
1776
1777         gtk_ctree_remove_node(ctree, folderview->selected);
1778         folder_write_list();
1779 }
1780
1781 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
1782                                          GtkWidget *widget)
1783 {
1784         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1785         GtkCTreeNode *node;
1786         FolderItem *item;
1787         gchar *message;
1788         AlertValue avalue;
1789
1790         if (!folderview->selected) return;
1791         node = folderview->selected;
1792         item = gtk_ctree_node_get_row_data(ctree, node);
1793         g_return_if_fail(item != NULL);
1794         g_return_if_fail(item->folder != NULL);
1795         if (item->parent) return;
1796
1797         message = g_strdup_printf
1798                 (_("Really remove the mailbox `%s' ?\n"
1799                    "(The messages are NOT deleted from disk)"),
1800                  item->folder->name);
1801         avalue = alertpanel(_("Remove folder"), message,
1802                             _("Yes"), _("+No"), NULL);
1803         g_free(message);
1804         if (avalue != G_ALERTDEFAULT) return;
1805
1806         folder_destroy(item->folder);
1807         summary_clear_all(folderview->summaryview);
1808         folderview_unselect(folderview);
1809         gtk_ctree_remove_node(ctree, node);
1810         folder_write_list();
1811 }
1812
1813 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
1814                                           GtkWidget *widget)
1815 {
1816         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1817         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1818         GtkCTreeNode *node;
1819         FolderItem *item;
1820         FolderItem *new_item;
1821         gchar *new_folder;
1822         gchar *p;
1823
1824         if (!folderview->selected) return;
1825
1826         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1827         g_return_if_fail(item != NULL);
1828         g_return_if_fail(item->folder != NULL);
1829         g_return_if_fail(item->folder->type == F_IMAP);
1830         g_return_if_fail(item->folder->account != NULL);
1831
1832         new_folder = input_dialog
1833                 (_("New folder"),
1834                  _("Input the name of new folder:\n"
1835                    "(if you want to create a folder to store subfolders,\n"
1836                    " append `/' at the end of the name)"),
1837                  _("NewFolder"));
1838         if (!new_folder) return;
1839
1840         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
1841             *(p + 1) != '\0') {
1842                 alertpanel_error(_("`%c' can't be included in folder name."),
1843                                  G_DIR_SEPARATOR);
1844                 g_free(new_folder);
1845                 return;
1846         }
1847
1848         /* find whether the directory already exists */
1849         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1850                 alertpanel_error(_("The folder `%s' already exists."),
1851                                  new_folder);
1852                 g_free(new_folder);
1853                 return;
1854         }
1855
1856         new_item = item->folder->create_folder(item->folder, item, new_folder);
1857         if (!new_item) {
1858                 alertpanel_error(_("Can't create the folder `%s'."),
1859                                  new_folder);
1860                 g_free(new_folder);
1861                 return;
1862         }
1863         g_free(new_folder);
1864
1865         gtk_clist_freeze(GTK_CLIST(ctree));
1866
1867         text[COL_FOLDER] = new_item->name;
1868         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1869                                      FOLDER_SPACING,
1870                                      folderxpm, folderxpmmask,
1871                                      folderopenxpm, folderopenxpmmask,
1872                                      FALSE, FALSE);
1873         gtk_ctree_expand(ctree, folderview->selected);
1874         gtk_ctree_node_set_row_data(ctree, node, new_item);
1875         folderview_sort_folders(folderview, folderview->selected, item->folder);
1876
1877         gtk_clist_thaw(GTK_CLIST(ctree));
1878
1879         folder_write_list();
1880 }
1881
1882 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
1883                                          GtkWidget *widget)
1884 {
1885         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1886         FolderItem *item;
1887         gchar *message;
1888         AlertValue avalue;
1889
1890         if (!folderview->selected) return;
1891
1892         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1893         g_return_if_fail(item != NULL);
1894         g_return_if_fail(item->folder != NULL);
1895         g_return_if_fail(item->folder->type == F_IMAP);
1896         g_return_if_fail(item->folder->account != NULL);
1897
1898         message = g_strdup_printf(_("Really delete folder `%s'?"),
1899                                   g_basename(item->path));
1900         avalue = alertpanel(_("Delete folder"), message,
1901                             _("Yes"), _("+No"), NULL);
1902         g_free(message);
1903         if (avalue != G_ALERTDEFAULT) return;
1904
1905         if (item->folder->remove_folder(item->folder, item) < 0) {
1906                 alertpanel_error(_("Can't remove the folder `%s'."),
1907                                  item->path);
1908                 if (folderview->opened == folderview->selected)
1909                         summary_show(folderview->summaryview,
1910                                      folderview->summaryview->folder_item,
1911                                      FALSE);
1912                 return;
1913         }
1914
1915         if (folderview->opened == folderview->selected ||
1916             gtk_ctree_is_ancestor(ctree,
1917                                   folderview->selected,
1918                                   folderview->opened)) {
1919                 summary_clear_all(folderview->summaryview);
1920                 folderview->opened = NULL;
1921         }
1922
1923         gtk_ctree_remove_node(ctree, folderview->selected);
1924         folder_write_list();
1925 }
1926
1927 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
1928                                          GtkWidget *widget)
1929 {
1930         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1931         FolderItem *item;
1932         gchar *message;
1933         AlertValue avalue;
1934
1935         if (!folderview->selected) return;
1936
1937         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1938         g_return_if_fail(item != NULL);
1939         g_return_if_fail(item->folder != NULL);
1940         g_return_if_fail(item->folder->type == F_IMAP);
1941         g_return_if_fail(item->folder->account != NULL);
1942
1943         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"),
1944                                   item->folder->name);
1945         avalue = alertpanel(_("Delete IMAP4 account"), message,
1946                             _("Yes"), _("+No"), NULL);
1947         g_free(message);
1948
1949         if (avalue != G_ALERTDEFAULT) return;
1950
1951         if (folderview->opened == folderview->selected ||
1952             gtk_ctree_is_ancestor(ctree,
1953                                   folderview->selected,
1954                                   folderview->opened)) {
1955                 summary_clear_all(folderview->summaryview);
1956                 folderview->opened = NULL;
1957         }
1958
1959         account_destroy(item->folder->account);
1960         folder_destroy(item->folder);
1961         gtk_ctree_remove_node(ctree, folderview->selected);
1962         account_set_menu();
1963         main_window_reflect_prefs_all();
1964         folder_write_list();
1965 }
1966
1967
1968
1969
1970
1971 static GList *folderview_news_get_subscriptions(GtkCTree *ctree,
1972                                                 GtkCTreeNode *node)
1973 {
1974         FolderItem *item;
1975         GList * result;
1976
1977         result = NULL;
1978
1979         if (!node)
1980                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1981         if (!node)
1982                 return NULL;
1983
1984         node = GTK_CTREE_ROW(node)->children;
1985
1986         while (node) {
1987                 item = gtk_ctree_node_get_row_data(ctree, node);
1988
1989                 result = g_list_append(result, item->name);
1990                 node = GTK_CTREE_ROW(node)->sibling;
1991         }
1992
1993         return result;
1994 }
1995
1996
1997 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
1998                                          GtkWidget *widget)
1999 {
2000         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2001         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2002         GtkCTreeNode *servernode, *node;
2003         FolderItem *item;
2004         FolderItem *newitem;
2005         const gchar *server;
2006         GList * subscriptions;
2007         GList * new_subscriptions;
2008         GList * l;
2009
2010         if (!folderview->selected) return;
2011
2012         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2013         g_return_if_fail(item != NULL);
2014         g_return_if_fail(item->folder != NULL);
2015         g_return_if_fail(item->folder->type == F_NEWS);
2016         g_return_if_fail(item->folder->account != NULL);
2017
2018         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2019                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2020         else
2021                 servernode = folderview->selected;
2022
2023         subscriptions = folderview_news_get_subscriptions(ctree, servernode);
2024
2025         new_subscriptions = grouplist_dialog(item->folder, subscriptions);
2026
2027         for(l = subscriptions ; l != NULL ; l = l->next) {
2028           gchar * name;
2029           
2030           name = (gchar *) l->data;
2031
2032           if (g_list_find_custom(new_subscriptions, name,
2033                                  (GCompareFunc) g_strcasecmp) == NULL) {
2034             GtkCTreeNode * sel;
2035
2036             /* remove subscription */
2037
2038             sel = folderview_find_by_name(ctree, servernode, name);
2039             g_return_if_fail(sel != NULL);
2040
2041             item = gtk_ctree_node_get_row_data(ctree, sel);
2042
2043             g_return_if_fail(item != NULL);
2044             g_return_if_fail(item->folder != NULL);
2045             g_return_if_fail(item->folder->type == F_NEWS);
2046             g_return_if_fail(item->folder->account != NULL);
2047
2048             if (folderview->opened == sel) {
2049               summary_clear_all(folderview->summaryview);
2050               folderview->opened = NULL;
2051             }
2052             
2053             folder_item_remove(item);
2054             gtk_ctree_remove_node(ctree, sel);
2055
2056           }
2057         }
2058
2059         g_list_free(subscriptions);
2060
2061         for(l = new_subscriptions ; l != NULL ; l = l->next) {
2062           gchar * name;
2063           
2064           name = (gchar *) l->data;
2065
2066           if (folderview_find_by_name(ctree, servernode, name) == NULL) {
2067
2068             /* add newsgroup */
2069
2070             gtk_clist_freeze(GTK_CLIST(ctree));
2071             
2072             text[COL_FOLDER] = name;
2073             node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2074                                          FOLDER_SPACING,
2075                                          folderxpm, folderxpmmask,
2076                                          folderopenxpm, folderopenxpmmask,
2077                                          FALSE, FALSE);
2078             gtk_ctree_expand(ctree, servernode);
2079             
2080             item = gtk_ctree_node_get_row_data(ctree, servernode);
2081             server = item->folder->account->nntp_server;
2082             
2083             newitem = folder_item_new(name, name);
2084             folder_item_append(item, newitem);
2085             gtk_ctree_node_set_row_data(ctree, node, newitem);
2086             gtk_ctree_sort_node(ctree, servernode);
2087             
2088             gtk_clist_thaw(GTK_CLIST(ctree));
2089             
2090           }
2091         }
2092
2093         list_free_strings(new_subscriptions);
2094         g_list_free(new_subscriptions);
2095
2096         folder_write_list();
2097 }
2098
2099 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2100                                         GtkWidget *widget)
2101 {
2102         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2103         FolderItem *item;
2104         gchar *message;
2105         AlertValue avalue;
2106
2107         if (!folderview->selected) return;
2108
2109         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2110         g_return_if_fail(item != NULL);
2111         g_return_if_fail(item->folder != NULL);
2112         g_return_if_fail(item->folder->type == F_NEWS);
2113         g_return_if_fail(item->folder->account != NULL);
2114
2115         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
2116                                   g_basename(item->path));
2117         avalue = alertpanel(_("Delete newsgroup"), message,
2118                             _("Yes"), _("+No"), NULL);
2119         g_free(message);
2120         if (avalue != G_ALERTDEFAULT) return;
2121
2122         if (folderview->opened == folderview->selected) {
2123                 summary_clear_all(folderview->summaryview);
2124                 folderview->opened = NULL;
2125         }
2126
2127         folder_item_remove(item);
2128         gtk_ctree_remove_node(ctree, folderview->selected);
2129         folder_write_list();
2130 }
2131
2132 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2133                                          GtkWidget *widget)
2134 {
2135         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2136         FolderItem *item;
2137         gchar *message;
2138         AlertValue avalue;
2139
2140         if (!folderview->selected) return;
2141
2142         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2143         g_return_if_fail(item != NULL);
2144         g_return_if_fail(item->folder != NULL);
2145         g_return_if_fail(item->folder->type == F_NEWS);
2146         g_return_if_fail(item->folder->account != NULL);
2147
2148         message = g_strdup_printf(_("Really delete news account `%s'?"),
2149                                   item->folder->name);
2150         avalue = alertpanel(_("Delete news account"), message,
2151                             _("Yes"), _("+No"), NULL);
2152         g_free(message);
2153
2154         if (avalue != G_ALERTDEFAULT) return;
2155
2156         if (folderview->opened == folderview->selected ||
2157             gtk_ctree_is_ancestor(ctree,
2158                                   folderview->selected,
2159                                   folderview->opened)) {
2160                 summary_clear_all(folderview->summaryview);
2161                 folderview->opened = NULL;
2162         }
2163
2164         account_destroy(item->folder->account);
2165         folder_destroy(item->folder);
2166         gtk_ctree_remove_node(ctree, folderview->selected);
2167         account_set_menu();
2168         main_window_reflect_prefs_all();
2169         folder_write_list();
2170 }
2171
2172 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2173                                           GdkDragContext *context,
2174                                           gint            x,
2175                                           gint            y,
2176                                           guint           time,
2177                                           FolderView     *folderview)
2178 {
2179         gint row, column;
2180         FolderItem *item, *current_item;
2181         GtkCTreeNode *node = NULL;
2182         gboolean acceptable = FALSE;
2183
2184         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
2185                                          x - 24, y - 24, &row, &column)) {
2186                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2187                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2188                 current_item = folderview->summaryview->folder_item;
2189                 if (item != NULL &&
2190                     item->path != NULL &&
2191                     current_item != NULL &&
2192                     current_item != item) {
2193                         switch (item->folder->type){
2194                         case F_MH:
2195                                 if (current_item->folder->type == F_MH)
2196                                     acceptable = TRUE;
2197                                 break;
2198                         case F_IMAP:
2199                                 if (current_item->folder->account == item->folder->account)
2200                                     acceptable = TRUE;
2201                                 break;
2202                         default:
2203                         }
2204                 }
2205         }
2206
2207         if (acceptable) {
2208                 gtk_ctree_select(GTK_CTREE(widget), node);
2209                 gdk_drag_status(context, context->suggested_action, time);
2210         } else {
2211                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2212                 gdk_drag_status(context, 0, time);
2213         }
2214
2215         return acceptable;
2216 }
2217
2218 static void folderview_drag_leave_cb(GtkWidget      *widget,
2219                                      GdkDragContext *context,
2220                                      guint           time,
2221                                      FolderView     *folderview)
2222 {
2223         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2224 }
2225
2226 static void folderview_drag_received_cb(GtkWidget        *widget,
2227                                         GdkDragContext   *drag_context,
2228                                         gint              x,
2229                                         gint              y,
2230                                         GtkSelectionData *data,
2231                                         guint             info,
2232                                         guint             time,
2233                                         FolderView       *folderview)
2234 {
2235         gint row, column;
2236         FolderItem *item;
2237         GtkCTreeNode *node;
2238
2239         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
2240                                          x - 24, y - 24, &row, &column) == 0)
2241                 return;
2242
2243         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2244         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2245         if (item != NULL) {
2246                 summary_move_selected_to(folderview->summaryview, item);
2247                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2248         } else
2249                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2250 }
2251
2252 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2253 {
2254         const FolderItem *item = a;
2255         const gchar *name = b;
2256
2257         if (!item->path) return -1;
2258         return strcmp2(g_basename(item->path), name);
2259 }
2260
2261 static void folderview_scoring_cb(FolderView *folderview, guint action,
2262                                    GtkWidget *widget)
2263 {
2264         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2265         FolderItem *item;
2266
2267         if (!folderview->selected) return;
2268
2269         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2270         g_return_if_fail(item != NULL);
2271         g_return_if_fail(item->folder != NULL);
2272
2273         prefs_scoring_open(item);
2274 }
2275
2276 static void folderview_property_cb(FolderView *folderview, guint action, GtkWidget *widget) {
2277         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2278         FolderItem *item;
2279
2280         if (!folderview->selected) return;
2281
2282         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2283         g_return_if_fail(item != NULL);
2284         g_return_if_fail(item->folder != NULL);
2285
2286         prefs_folder_item_create(item);
2287 }
2288
2289 void folderview_set_target_folder_color(gint color_op) {
2290         gint firstone = 1;
2291         GList *list;
2292         FolderView *folderview;
2293
2294         for (list = folderview_list; list != NULL; list = list->next) {
2295                 folderview = (FolderView *)list->data;
2296                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2297                 if (firstone) {
2298                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2299                                 folderview->color_op;
2300                         firstone = 0;
2301                 }
2302         }
2303 }