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