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