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