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