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