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