more sync with 0.7.8cvs21
[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         folderview_check_new(folder);
816         
817         gtk_widget_destroy(window);
818         inc_unlock();
819 }
820
821 void folderview_rescan_all(void)
822 {
823         GList *list;
824         GtkWidget *window;
825
826         inc_lock();
827         window = label_window_create(_("Rescanning all folder trees..."));
828
829         list = folder_get_list();
830         for (; list != NULL; list = list->next) {
831                 Folder *folder = list->data;
832
833                 if (!folder->scan_tree) continue;
834                 folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
835                 folder_scan_tree(folder);
836                 folder_set_ui_func(folder, NULL, NULL);
837         }
838
839         folder_write_list();
840         folderview_set_all();
841
842         list = folder_get_list();
843         for (; list != NULL; list = list->next) {
844                 Folder *folder = list->data;
845
846                 folderview_check_new(folder);
847         }
848
849         gtk_widget_destroy(window);
850         inc_unlock();
851 }
852
853 void folderview_check_new(Folder *folder)
854 {
855         GList *list;
856         FolderItem *item;
857         FolderView *folderview;
858         GtkCTree *ctree;
859         GtkCTreeNode *node;
860
861         for (list = folderview_list; list != NULL; list = list->next) {
862                 folderview = (FolderView *)list->data;
863                 ctree = GTK_CTREE(folderview->ctree);
864
865                 inc_lock();
866                 main_window_lock(folderview->mainwin);
867                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
868
869                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
870                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
871                         item = gtk_ctree_node_get_row_data(ctree, node);
872                         if (!item || !item->path || !item->folder) continue;
873                         if (item->no_select) continue;
874                         if (folder && folder != item->folder) continue;
875                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
876
877                         folderview_scan_tree_func(item->folder, item, NULL);
878                         if (folder_item_scan(item) < 0) {
879                                 if (folder && !FOLDER_IS_LOCAL(folder))
880                                         break;
881                         }
882                         folderview_update_node(folderview, node);
883                 }
884
885                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
886                 main_window_unlock(folderview->mainwin);
887                 inc_unlock();
888         }
889
890         folder_write_list();
891 }
892
893 static gboolean folderview_search_new_recursive(GtkCTree *ctree,
894                                                 GtkCTreeNode *node)
895 {
896         FolderItem *item;
897
898         if (node) {
899                 item = gtk_ctree_node_get_row_data(ctree, node);
900                 if (item) {
901                         if (item->new > 0 ||
902                             (item->stype == F_QUEUE && item->total > 0))
903                                 return TRUE;
904                 }
905                 node = GTK_CTREE_ROW(node)->children;
906         } else
907                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
908
909         while (node) {
910                 if (folderview_search_new_recursive(ctree, node) == TRUE)
911                         return TRUE;
912                 node = GTK_CTREE_ROW(node)->sibling;
913         }
914
915         return FALSE;
916 }
917
918 static gboolean folderview_have_new_children(FolderView *folderview,
919                                              GtkCTreeNode *node)
920 {
921         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
922
923         if (!node)
924                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
925         if (!node)
926                 return FALSE;
927
928         node = GTK_CTREE_ROW(node)->children;
929
930         while (node) {
931                 if (folderview_search_new_recursive(ctree, node) == TRUE)
932                         return TRUE;
933                 node = GTK_CTREE_ROW(node)->sibling;
934         }
935
936         return FALSE;
937 }
938
939 static gboolean folderview_search_unread_recursive(GtkCTree *ctree,
940                                                    GtkCTreeNode *node)
941 {
942         FolderItem *item;
943
944         if (node) {
945                 item = gtk_ctree_node_get_row_data(ctree, node);
946                 if (item) {
947                         if (item->unread > 0 ||
948                             (item->stype == F_QUEUE && item->total > 0))
949                                 return TRUE;
950                 }
951                 node = GTK_CTREE_ROW(node)->children;
952         } else
953                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
954
955         while (node) {
956                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
957                         return TRUE;
958                 node = GTK_CTREE_ROW(node)->sibling;
959         }
960
961         return FALSE;
962 }
963
964 static gboolean folderview_have_unread_children(FolderView *folderview,
965                                                 GtkCTreeNode *node)
966 {
967         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
968
969         if (!node)
970                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
971         if (!node)
972                 return FALSE;
973
974         node = GTK_CTREE_ROW(node)->children;
975
976         while (node) {
977                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
978                         return TRUE;
979                 node = GTK_CTREE_ROW(node)->sibling;
980         }
981
982         return FALSE;
983 }
984
985 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
986 {
987         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
988         GtkStyle *style = NULL;
989         FolderItem *item;
990         GdkPixmap *xpm, *openxpm;
991         GdkBitmap *mask, *openmask;
992         gchar *name;
993         gchar *str;
994         gboolean add_unread_mark;
995         gboolean use_bold, use_color;
996
997         item = gtk_ctree_node_get_row_data(ctree, node);
998         g_return_if_fail(item != NULL);
999
1000         switch (item->stype) {
1001         case F_INBOX:
1002                 if (item->hide_read_msgs) {
1003                         xpm = inboxhrmxpm;
1004                         mask = inboxhrmxpmmask;
1005                         openxpm = inboxopenhrmxpm;
1006                         openmask = inboxopenhrmxpmmask;
1007                 } else {
1008                         xpm = inboxxpm;
1009                         mask = inboxxpmmask;
1010                         openxpm = inboxopenxpm;
1011                         openmask = inboxopenxpmmask;
1012                 }
1013                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1014                                 !strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1015                                 item->name);
1016                 break;
1017         case F_OUTBOX:
1018                 if (item->hide_read_msgs) {
1019                         xpm = outboxhrmxpm;
1020                         mask = outboxhrmxpmmask;
1021                         openxpm = outboxopenhrmxpm;
1022                         openmask = outboxopenhrmxpmmask;
1023                 } else {
1024                         xpm = outboxxpm;
1025                         mask = outboxxpmmask;
1026                         openxpm = outboxopenxpm;
1027                         openmask = outboxopenxpmmask;
1028                 }
1029                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1030                                 !strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1031                                 item->name);
1032                 break;
1033         case F_QUEUE:
1034                 if (item->hide_read_msgs) {
1035                         xpm = queuehrmxpm;
1036                         mask = queuehrmxpmmask;
1037                         openxpm = queueopenhrmxpm;
1038                         openmask = queueopenhrmxpmmask;
1039                 } else {
1040                         xpm = queuexpm;
1041                         mask = queuexpmmask;
1042                         openxpm = queueopenxpm;
1043                         openmask = queueopenxpmmask;
1044                 }
1045                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1046                                 !strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1047                                 item->name);
1048                 break;
1049         case F_TRASH:
1050                 if (item->hide_read_msgs) {
1051                         xpm = trashhrmxpm;
1052                         mask = trashhrmxpmmask;
1053                         openxpm = trashopenhrmxpm;
1054                         openmask = trashopenhrmxpmmask;
1055                 } else {
1056                         xpm = trashxpm;
1057                         mask = trashxpmmask;
1058                         openxpm = trashopenxpm;
1059                         openmask = trashopenxpmmask;
1060                 }
1061                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1062                                 !strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1063                                 item->name);
1064                 break;
1065         case F_DRAFT:
1066                 xpm = folderxpm;
1067                 mask = folderxpmmask;
1068                 if (item->hide_read_msgs) {
1069                         openxpm = folderopenhrmxpm;
1070                         openmask = folderopenhrmxpmmask;
1071                 } else {
1072                         openxpm = folderopenxpm;
1073                         openmask = folderopenxpmmask;
1074                 }
1075                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1076                                 !strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1077                                 item->name);
1078                 break;
1079         default:
1080                 if (item->hide_read_msgs) {
1081                         xpm = folderhrmxpm;
1082                         mask = folderhrmxpmmask;
1083                         openxpm = folderopenhrmxpm;
1084                         openmask = folderopenhrmxpmmask;
1085                 } else {
1086                         xpm = folderxpm;
1087                         mask = folderxpmmask;
1088                         openxpm = folderopenxpm;
1089                         openmask = folderopenxpmmask;
1090                 }
1091                 if (!item->parent) {
1092                         switch (item->folder->type) {
1093                         case F_MH:
1094                                 name = " (MH)"; break;
1095                         case F_MBOX:
1096                                 name = " (mbox)"; break;
1097                         case F_IMAP:
1098                                 name = " (IMAP4)"; break;
1099                         case F_NEWS:
1100                                 name = " (News)"; break;
1101                         default:
1102                                 name = "";
1103                         }
1104                         name = g_strconcat(item->name, name, NULL);
1105                 } else {
1106                         if (item->folder->type == F_NEWS &&
1107                             item->path &&
1108                             !strcmp2(item->name, item->path) &&
1109                             prefs_common.ng_abbrev_len < strlen(item->path))
1110                                 name = get_abbrev_newsgroup_name(item->path);
1111                         else
1112                                 name = g_strdup(item->name);
1113                 }
1114         }
1115
1116         if (!GTK_CTREE_ROW(node)->expanded &&
1117             folderview_have_unread_children(folderview, node))
1118                 add_unread_mark = TRUE;
1119         else
1120                 add_unread_mark = FALSE;
1121
1122         if (item->stype == F_QUEUE && item->total > 0 &&
1123             prefs_common.display_folder_unread) {
1124                 str = g_strdup_printf("%s (%d%s)", name, item->total,
1125                                       add_unread_mark ? "+" : "");
1126                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1127                                         xpm, mask, openxpm, openmask,
1128                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1129                 g_free(str);
1130         } else if ((item->unread > 0 || add_unread_mark) &&
1131                  prefs_common.display_folder_unread) {
1132
1133                 if (item->unread > 0)
1134                         str = g_strdup_printf("%s (%d%s)", name, item->unread,
1135                                               add_unread_mark ? "+" : "");
1136                 else
1137                         str = g_strdup_printf("%s (+)", name);
1138                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1139                                         xpm, mask, openxpm, openmask,
1140                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1141                 g_free(str);
1142         } else
1143                 gtk_ctree_set_node_info(ctree, node, name, FOLDER_SPACING,
1144                                         xpm, mask, openxpm, openmask,
1145                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1146         g_free(name);
1147
1148         if (!item->parent) {
1149                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1150                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1151                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1152         } else {
1153                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
1154                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
1155                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
1156         }
1157
1158         if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1159             item->stype == F_TRASH) {
1160                 use_bold = use_color = FALSE;
1161         } else if (item->stype == F_QUEUE) {
1162                 /* highlight queue folder if there are any messages */
1163                 use_bold = use_color = (item->total > 0);
1164         } else {
1165                 /* if unread messages exist, print with bold font */
1166                 use_bold = (item->unread > 0) || add_unread_mark;
1167                 /* if new messages exist, print with colored letter */
1168                 use_color =
1169                         (item->new > 0) ||
1170                         (add_unread_mark &&
1171                          folderview_have_new_children(folderview, node));
1172         }
1173
1174         gtk_ctree_node_set_foreground(ctree, node, NULL);
1175
1176         if (use_bold && use_color)
1177                 style = bold_color_style;
1178         else if (use_bold) {
1179                 style = bold_style;
1180                 if (item->op_count > 0) {
1181                         style = bold_tgtfold_style;
1182                 }
1183         }
1184         else if (use_color) {
1185                 style = normal_color_style;
1186                 gtk_ctree_node_set_foreground(ctree, node,
1187                                               &folderview->color_new);
1188         }
1189         else if (item->op_count > 0) {
1190                 style = bold_tgtfold_style;
1191         } else {
1192                 style = normal_style;
1193         }
1194
1195         gtk_ctree_node_set_row_style(ctree, node, style);
1196
1197         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1198                 folderview_update_node(folderview, node);
1199 }
1200
1201 void folderview_update_item(FolderItem *item, gboolean update_summary)
1202 {
1203         GList *list;
1204         FolderView *folderview;
1205         GtkCTree *ctree;
1206         GtkCTreeNode *node;
1207
1208         g_return_if_fail(item != NULL);
1209
1210         for (list = folderview_list; list != NULL; list = list->next) {
1211                 folderview = (FolderView *)list->data;
1212                 ctree = GTK_CTREE(folderview->ctree);
1213
1214                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1215                 if (node) {
1216                         folderview_update_node(folderview, node);
1217                         if (update_summary && folderview->opened == node)
1218                                 summary_show(folderview->summaryview,
1219                                              item, FALSE);
1220                 }
1221         }
1222 }
1223
1224 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1225                                                 gpointer data)
1226 {
1227         folderview_update_item((FolderItem *)key, FALSE);
1228 }
1229
1230 void folderview_update_item_foreach(GHashTable *table)
1231 {
1232         g_hash_table_foreach(table, folderview_update_item_foreach_func, NULL);
1233 }
1234
1235 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1236                                       GNode *gnode, GtkCTreeNode *cnode,
1237                                       gpointer data)
1238 {
1239         FolderView *folderview = (FolderView *)data;
1240         FolderItem *item = FOLDER_ITEM(gnode->data);
1241
1242         g_return_val_if_fail(item != NULL, FALSE);
1243
1244         gtk_ctree_node_set_row_data(ctree, cnode, item);
1245         folderview_update_node(folderview, cnode);
1246
1247         return TRUE;
1248 }
1249
1250 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1251                                    gpointer data)
1252 {
1253         FolderView *folderview = (FolderView *)data;
1254         FolderItem *item;
1255
1256         if (GTK_CTREE_ROW(node)->children) {
1257                 item = gtk_ctree_node_get_row_data(ctree, node);
1258                 g_return_if_fail(item != NULL);
1259
1260                 if (!item->collapsed)
1261                         gtk_ctree_expand(ctree, node);
1262                 else
1263                         folderview_update_node(folderview, node);
1264         }
1265 }
1266
1267 #define SET_SPECIAL_FOLDER(ctree, item) \
1268 { \
1269         if (item) { \
1270                 GtkCTreeNode *node, *parent, *sibling; \
1271  \
1272                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
1273                 if (!node) \
1274                         g_warning("%s not found.\n", item->path); \
1275                 else { \
1276                         parent = GTK_CTREE_ROW(node)->parent; \
1277                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
1278                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
1279                         else \
1280                                 sibling = GTK_CTREE_ROW(parent)->children; \
1281                         if (node != sibling) \
1282                                 gtk_ctree_move(ctree, node, parent, sibling); \
1283                 } \
1284  \
1285                 prev = node; \
1286         } \
1287 }
1288
1289 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1290                                     Folder *folder)
1291 {
1292         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1293         GtkCTreeNode *prev = NULL;
1294
1295         gtk_ctree_sort_recursive(ctree, root);
1296
1297         if (GTK_CTREE_ROW(root)->parent) return;
1298
1299         SET_SPECIAL_FOLDER(ctree, folder->inbox);
1300         SET_SPECIAL_FOLDER(ctree, folder->outbox);
1301         SET_SPECIAL_FOLDER(ctree, folder->draft);
1302         SET_SPECIAL_FOLDER(ctree, folder->queue);
1303         SET_SPECIAL_FOLDER(ctree, folder->trash);
1304 }
1305
1306 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1307 {
1308         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1309         GtkCTreeNode *root;
1310
1311         g_return_if_fail(folder != NULL);
1312
1313         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1314                                       folderview_gnode_func, folderview);
1315         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1316                                 folderview);
1317         folderview_sort_folders(folderview, root, folder);
1318 }
1319
1320 void folderview_new_folder(FolderView *folderview)
1321 {
1322         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1323         FolderItem *item;
1324
1325         if (!folderview->selected) return;
1326
1327         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1328         g_return_if_fail(item != NULL);
1329         g_return_if_fail(item->folder != NULL);
1330
1331         switch (item->folder->type) {
1332         case F_MBOX:
1333                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1334                 break;
1335         case F_MH:
1336         case F_MAILDIR:
1337                 folderview_new_folder_cb(folderview, 0, NULL);
1338                 break;
1339         case F_IMAP:
1340                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1341                 break;
1342         case F_NEWS:
1343         default:
1344                 break;
1345         }
1346 }
1347
1348 void folderview_rename_folder(FolderView *folderview)
1349 {
1350         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1351         FolderItem *item;
1352
1353         if (!folderview->selected) return;
1354
1355         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1356         g_return_if_fail(item != NULL);
1357         g_return_if_fail(item->folder != NULL);
1358         if (!item->path) return;
1359         if (item->stype != F_NORMAL) return;
1360
1361         switch (item->folder->type) {
1362         case F_MBOX:
1363                 folderview_rename_mbox_folder_cb(folderview, 0, NULL);
1364         case F_MH:
1365         case F_MAILDIR:
1366         case F_IMAP:
1367                 folderview_rename_folder_cb(folderview, 0, NULL);
1368                 break;
1369         case F_NEWS:
1370         default:
1371                 break;
1372         }
1373 }
1374
1375 void folderview_delete_folder(FolderView *folderview)
1376 {
1377         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1378         FolderItem *item;
1379
1380         if (!folderview->selected) return;
1381
1382         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1383         g_return_if_fail(item != NULL);
1384         g_return_if_fail(item->folder != NULL);
1385         if (!item->path) return;
1386         if (item->stype != F_NORMAL) return;
1387
1388         switch (item->folder->type) {
1389         case F_MH:
1390         case F_MBOX:
1391         case F_MAILDIR:
1392         case F_IMAP:
1393                 folderview_delete_folder_cb(folderview, 0, NULL);
1394                 break;
1395         case F_NEWS:
1396         default:
1397                 break;
1398         }
1399 }
1400
1401
1402 /* callback functions */
1403
1404 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1405                                       FolderView *folderview)
1406 {
1407         GtkCList *clist = GTK_CLIST(ctree);
1408         gint prev_row = -1, row = -1, column = -1;
1409         FolderItem *item;
1410         Folder *folder;
1411         GtkWidget *popup;
1412         gboolean mark_all_read   = FALSE;
1413         gboolean new_folder      = FALSE;
1414         gboolean rename_folder   = FALSE;
1415         gboolean delete_folder   = FALSE;
1416         gboolean update_tree     = FALSE;
1417         gboolean rescan_tree     = FALSE;
1418         gboolean remove_tree     = FALSE;
1419         gboolean search_folder   = FALSE;
1420         gboolean folder_property = FALSE;
1421         gboolean folder_processing  = FALSE;
1422         gboolean folder_scoring  = FALSE;
1423
1424         if (!event) return;
1425
1426         if (event->button == 1) {
1427                 folderview->open_folder = TRUE;
1428                 return;
1429         }
1430
1431         if (event->button == 2 || event->button == 3) {
1432                 /* right clicked */
1433                 if (clist->selection) {
1434                         GtkCTreeNode *node;
1435
1436                         node = GTK_CTREE_NODE(clist->selection->data);
1437                         if (node)
1438                                 prev_row = gtkut_ctree_get_nth_from_node
1439                                         (GTK_CTREE(ctree), node);
1440                 }
1441
1442                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1443                                                   &row, &column))
1444                         return;
1445                 if (prev_row != row) {
1446                         gtk_clist_unselect_all(clist);
1447                         if (event->button == 2)
1448                                 folderview_select_node
1449                                         (folderview,
1450                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1451                                                             row));
1452                         else
1453                                 gtk_clist_select_row(clist, row, column);
1454                 }
1455         }
1456
1457         if (event->button != 3) return;
1458
1459         item = gtk_clist_get_row_data(clist, row);
1460         g_return_if_fail(item != NULL);
1461         g_return_if_fail(item->folder != NULL);
1462         folder = item->folder;
1463
1464         if (folderview->mainwin->lock_count == 0) {
1465                 new_folder = TRUE;
1466                 if (item->parent == NULL)
1467                         update_tree = remove_tree = TRUE;
1468                 else
1469                         mark_all_read = search_folder = folder_property = TRUE;
1470                 if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
1471                         if (item->parent == NULL)
1472                                 update_tree = rescan_tree = TRUE;
1473                         else if (item->stype == F_NORMAL)
1474                                 rename_folder = delete_folder = folder_scoring = folder_processing = TRUE;
1475                         else if (item->stype == F_INBOX)
1476                                 folder_scoring = folder_processing = TRUE;
1477                         else if (item->stype == F_TRASH)
1478                                 folder_processing = TRUE;
1479                         else if (item->stype == F_OUTBOX)
1480                                 folder_processing = TRUE;
1481                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
1482                         if (item->parent != NULL)
1483                                 delete_folder = folder_scoring = folder_processing = TRUE;
1484                 }
1485                 if (item->unread < 1) 
1486                         mark_all_read = FALSE;
1487         }
1488
1489 #define SET_SENS(factory, name, sens) \
1490         menu_set_sensitive(folderview->factory, name, sens)
1491         
1492         mark_all_read = mark_all_read && 
1493                         (item == folderview->summaryview->folder_item);
1494
1495         if (FOLDER_IS_LOCAL(folder)) {
1496                 popup = folderview->mail_popup;
1497                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1498                 SET_SENS(mail_factory, "/Mark all read", mark_all_read);
1499                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1500                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1501                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1502                 SET_SENS(mail_factory, "/Check for new messages", update_tree);
1503                 SET_SENS(mail_factory, "/Rescan folder tree", rescan_tree);
1504                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1505                 SET_SENS(mail_factory, "/Search folder...", search_folder);
1506                 SET_SENS(mail_factory, "/Property...", folder_property);
1507                 SET_SENS(mail_factory, "/Processing...", folder_processing);
1508                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1509         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1510                 popup = folderview->imap_popup;
1511                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1512                 SET_SENS(imap_factory, "/Mark all read", mark_all_read);
1513                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1514                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1515                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1516                 SET_SENS(imap_factory, "/Check for new messages", update_tree);
1517                 SET_SENS(imap_factory, "/Rescan folder tree", rescan_tree);
1518                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1519                 SET_SENS(imap_factory, "/Search folder...", search_folder);
1520                 SET_SENS(imap_factory, "/Property...", folder_property);
1521                 SET_SENS(imap_factory, "/Processing...", folder_processing);
1522                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1523         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1524                 popup = folderview->news_popup;
1525                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1526                 SET_SENS(news_factory, "/Mark all read", mark_all_read);
1527                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1528                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1529 #if 0
1530                 SET_SENS(news_factory, "/Check for new messages", update_tree);
1531 #endif
1532                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1533                 SET_SENS(news_factory, "/Search folder...", search_folder);
1534                 SET_SENS(news_factory, "/Property...", folder_property);
1535                 SET_SENS(news_factory, "/Processing...", folder_processing);
1536                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1537         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1538                 popup = folderview->mbox_popup;
1539                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1540                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1541                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1542                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1543                 SET_SENS(news_factory, "/Property...", folder_property);
1544                 SET_SENS(mbox_factory, "/Processing...", folder_processing);
1545                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1546         } else
1547                 return;
1548
1549 #undef SET_SENS
1550
1551         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1552                        event->button, event->time);
1553 }
1554
1555 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1556                                        FolderView *folderview)
1557 {
1558         if (!event) return;
1559
1560         if (event->button == 1 && folderview->open_folder == FALSE &&
1561             folderview->opened != NULL) {
1562                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1563                                           folderview->opened);
1564                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1565         }
1566 }
1567
1568 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1569                                    FolderView *folderview)
1570 {
1571         if (!event) return;
1572
1573         switch (event->keyval) {
1574         case GDK_Return:
1575                 if (folderview->selected) {
1576                         folderview_select_node(folderview,
1577                                                folderview->selected);
1578                 }
1579                 break;
1580         case GDK_space:
1581                 if (folderview->selected) {
1582                         if (folderview->opened == folderview->selected &&
1583                             folderview->summaryview->messages == 0)
1584                                 folderview_select_next_unread(folderview);
1585                         else
1586                                 folderview_select_node(folderview,
1587                                                        folderview->selected);
1588                 }
1589                 break;
1590         default:
1591                 break;
1592         }
1593 }
1594
1595 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1596                                 gint column, FolderView *folderview)
1597 {
1598         static gboolean can_select = TRUE;      /* exclusive lock */
1599         gboolean opened;
1600         FolderItem *item;
1601
1602         folderview->selected = row;
1603
1604         if (folderview->opened == row) {
1605                 folderview->open_folder = FALSE;
1606                 return;
1607         }
1608
1609         if (!can_select || summary_is_locked(folderview->summaryview)) {
1610                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1611                 gtk_ctree_select(ctree, folderview->opened);
1612                 return;
1613         }
1614
1615         if (!folderview->open_folder) return;
1616
1617         item = gtk_ctree_node_get_row_data(ctree, row);
1618         if (!item) return;
1619
1620         can_select = FALSE;
1621
1622         /* CLAWS: set compose button type: news folder items 
1623          * always have a news folder as parent */
1624         if (item->folder) 
1625                 main_window_toolbar_set_compose_button
1626                         (folderview->mainwin,
1627                          item->folder->type == F_NEWS ? 
1628                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1629
1630         if (item->path)
1631                 debug_print(_("Folder %s is selected\n"), item->path);
1632
1633         if (!GTK_CTREE_ROW(row)->children)
1634                 gtk_ctree_expand(ctree, row);
1635         if (folderview->opened &&
1636             !GTK_CTREE_ROW(folderview->opened)->children)
1637                 gtk_ctree_collapse(ctree, folderview->opened);
1638
1639         /* ungrab the mouse event */
1640         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1641                 gtk_grab_remove(GTK_WIDGET(ctree));
1642                 if (gdk_pointer_is_grabbed())
1643                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1644         }
1645
1646         if((item->folder->type == F_IMAP) || (item->folder->type == F_NEWS)) {
1647                 folder_item_scan(item);
1648         }
1649
1650         opened = summary_show(folderview->summaryview, item, FALSE);
1651
1652         folder_clean_cache_memory();
1653         
1654         if (!opened) {
1655                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1656                 gtk_ctree_select(ctree, folderview->opened);
1657         } else {
1658                 folderview->opened = row;
1659                 if (gtk_ctree_node_is_visible(ctree, row)
1660                     != GTK_VISIBILITY_FULL)
1661                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1662         }
1663
1664         folderview->open_folder = FALSE;
1665         can_select = TRUE;
1666 }
1667
1668 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1669                                      FolderView *folderview)
1670 {
1671         FolderItem *item;
1672
1673         item = gtk_ctree_node_get_row_data(ctree, node);
1674         g_return_if_fail(item != NULL);
1675         item->collapsed = FALSE;
1676         folderview_update_node(folderview, node);
1677 }
1678
1679 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1680                                       FolderView *folderview)
1681 {
1682         FolderItem *item;
1683
1684         item = gtk_ctree_node_get_row_data(ctree, node);
1685         g_return_if_fail(item != NULL);
1686         item->collapsed= TRUE;
1687         folderview_update_node(folderview, node);
1688 }
1689
1690 static void folderview_popup_close(GtkMenuShell *menu_shell,
1691                                    FolderView *folderview)
1692 {
1693         if (!folderview->opened) return;
1694
1695         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1696                                   folderview->opened);
1697         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1698 }
1699
1700 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1701                                    FolderView *folderview)
1702 {
1703         switch (column) {
1704         case COL_FOLDER:
1705                 prefs_common.folder_col_folder = width;
1706                 break;
1707         case COL_NEW:
1708                 prefs_common.folder_col_new = width;
1709                 break;
1710         case COL_UNREAD:
1711                 prefs_common.folder_col_unread = width;
1712                 break;
1713         case COL_TOTAL:
1714                 prefs_common.folder_col_total = width;
1715                 break;
1716         default:
1717                 break;
1718         }
1719 }
1720
1721 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1722                                              GtkCTreeNode *node,
1723                                              const gchar *name)
1724 {
1725         FolderItem *item;
1726
1727         if (!node)
1728                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1729         if (!node)
1730                 return NULL;
1731
1732         node = GTK_CTREE_ROW(node)->children;
1733
1734         while (node) {
1735                 item = gtk_ctree_node_get_row_data(ctree, node);
1736                 if (!folderview_compare_name(item, name))
1737                         return node;
1738                 node = GTK_CTREE_ROW(node)->sibling;
1739         }
1740
1741         return NULL;
1742 }
1743
1744 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1745                                       GtkWidget *widget)
1746 {
1747         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1748         FolderItem *item;
1749
1750         if (!folderview->selected) return;
1751
1752         summary_show(folderview->summaryview, NULL, FALSE);
1753
1754         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1755         g_return_if_fail(item != NULL);
1756         g_return_if_fail(item->folder != NULL);
1757
1758         if (action == 0)
1759                 folderview_check_new(item->folder);
1760         else
1761                 folderview_rescan_tree(item->folder);
1762 }
1763
1764 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1765                                      GtkWidget *widget)
1766 {
1767         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1768         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1769         FolderItem *item;
1770         FolderItem *new_item;
1771         gchar *new_folder;
1772         gchar *name, *name_;
1773         GtkCTreeNode *node;
1774
1775         if (!folderview->selected) return;
1776
1777         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1778         g_return_if_fail(item != NULL);
1779         g_return_if_fail(item->folder != NULL);
1780
1781         new_folder = input_dialog(_("New folder"),
1782                                   _("Input the name of new folder:"),
1783                                   _("NewFolder"));
1784         if (!new_folder) return;
1785
1786         if (item->folder->type != F_MBOX) {
1787                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1788                         alertpanel_error(_("`%c' can't be included in folder name."),
1789                                          G_DIR_SEPARATOR);
1790                         g_free(new_folder);
1791                         return;
1792                 }
1793         }
1794
1795         name_ = trim_string(new_folder, 32);
1796         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1797         g_free(name_);
1798
1799         /* find whether the directory already exists */
1800         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1801                 alertpanel_error(_("The folder `%s' already exists."), name);
1802                 g_free(new_folder);
1803                 return;
1804         }
1805
1806         new_item = item->folder->create_folder(item->folder, item, new_folder);
1807         if (!new_item) {
1808                 alertpanel_error(_("Can't create the folder `%s'."), name);
1809                 g_free(new_folder);
1810                 return;
1811         } 
1812         g_free(new_folder);
1813
1814         gtk_clist_freeze(GTK_CLIST(ctree));
1815
1816         text[COL_FOLDER] = new_item->name;
1817         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1818                                      FOLDER_SPACING,
1819                                      folderxpm, folderxpmmask,
1820                                      folderopenxpm, folderopenxpmmask,
1821                                      FALSE, FALSE);
1822         gtk_ctree_expand(ctree, folderview->selected);
1823         gtk_ctree_node_set_row_data(ctree, node, new_item);
1824         folderview_sort_folders(folderview, folderview->selected, item->folder);
1825
1826         gtk_clist_thaw(GTK_CLIST(ctree));
1827
1828         folder_write_list();
1829 }
1830
1831 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1832                                           GtkWidget *widget)
1833 {
1834         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1835         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1836         FolderItem *item;
1837         FolderItem *new_item;
1838         gchar *new_folder;
1839         GtkCTreeNode *node;
1840
1841         if (!folderview->selected) return;
1842
1843         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1844         g_return_if_fail(item != NULL);
1845         g_return_if_fail(item->folder != NULL);
1846
1847         new_folder = input_dialog(_("New folder"),
1848                                   _("Input the name of new folder:"),
1849                                   _("NewFolder"));
1850         if (!new_folder) return;
1851
1852         /* find whether the directory already exists */
1853         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1854                 alertpanel_error(_("The folder `%s' already exists."),
1855                                  new_folder);
1856                 g_free(new_folder);
1857                 return;
1858         }
1859
1860         new_item = item->folder->create_folder(item->folder, item, new_folder);
1861         g_free(new_folder);
1862         if (!new_item) return;
1863
1864         gtk_clist_freeze(GTK_CLIST(ctree));
1865
1866         text[COL_FOLDER] = new_item->name;
1867         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1868                                      FOLDER_SPACING,
1869                                      folderxpm, folderxpmmask,
1870                                      folderopenxpm, folderopenxpmmask,
1871                                      FALSE, FALSE);
1872         gtk_ctree_expand(ctree, folderview->selected);
1873         gtk_ctree_node_set_row_data(ctree, node, new_item);
1874         folderview_sort_folders(folderview, folderview->selected, item->folder);
1875
1876         gtk_clist_thaw(GTK_CLIST(ctree));
1877
1878         folder_write_list();
1879 }
1880
1881 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1882                                         GtkWidget *widget)
1883 {
1884         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1885         FolderItem *item;
1886         gchar *new_folder;
1887         gchar *name, *name_;
1888         gchar *message;
1889         gchar *old_path;
1890         gchar *old_id;
1891         gchar *new_id;
1892
1893         if (!folderview->selected) return;
1894
1895         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1896         g_return_if_fail(item != NULL);
1897         g_return_if_fail(item->path != NULL);
1898         g_return_if_fail(item->folder != NULL);
1899
1900         name_ = trim_string(item->name, 32);
1901         Xstrdup_a(name, name_, return);
1902         g_free(name_);
1903         message = g_strdup_printf(_("Input new name for `%s':"), name);
1904         new_folder = input_dialog(_("Rename folder"), message,
1905                                   g_basename(item->path));
1906         g_free(message);
1907         if (!new_folder) return;
1908
1909         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1910                 alertpanel_error(_("`%c' can't be included in folder name."),
1911                                  G_DIR_SEPARATOR);
1912                 g_free(new_folder);
1913                 return;
1914         }
1915
1916         if (folderview_find_by_name
1917                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1918                  new_folder)) {
1919                 name = trim_string(new_folder, 32);
1920                 alertpanel_error(_("The folder `%s' already exists."), name);
1921                 g_free(name);
1922                 g_free(new_folder);
1923                 return;
1924         }
1925
1926         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
1927         old_id = folder_item_get_identifier(item);
1928
1929         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1930                 g_free(old_id);
1931                 g_free(new_folder);
1932                 return;
1933         }
1934         g_free(new_folder);
1935
1936         if (prefs_common.fltlist) {
1937                 if (folder_get_default_folder() == item->folder)
1938                         prefs_filter_rename_path(old_path, item->path);
1939                 new_id = folder_item_get_identifier(item);
1940                 prefs_filter_rename_path(old_id, new_id);
1941         } else {
1942                 if (FOLDER_TYPE(item->folder) == F_MH)
1943                         prefs_filtering_rename_path(old_path, item->path);
1944                 new_id = folder_item_get_identifier(item);
1945                 prefs_filtering_rename_path(old_id, new_id);
1946         }
1947         g_free(old_id);
1948         g_free(new_id);
1949
1950         gtk_clist_freeze(GTK_CLIST(ctree));
1951
1952         folderview_update_node(folderview, folderview->selected);
1953         folderview_sort_folders(folderview,
1954                                 GTK_CTREE_ROW(folderview->selected)->parent,
1955                                 item->folder);
1956         if (folderview->opened == folderview->selected ||
1957             gtk_ctree_is_ancestor(ctree,
1958                                   folderview->selected,
1959                                   folderview->opened)) {
1960                 GtkCTreeNode *node = folderview->opened;
1961                 folderview_unselect(folderview);
1962                 folderview_select_node(folderview, node);
1963         }
1964
1965         gtk_clist_thaw(GTK_CLIST(ctree));
1966
1967         folder_write_list();
1968 }
1969
1970 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
1971                                              guint action,
1972                                              GtkWidget *widget)
1973 {
1974         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1975         FolderItem *item;
1976         gchar *new_folder;
1977         gchar *message;
1978
1979         if (!folderview->selected) return;
1980
1981         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1982         g_return_if_fail(item != NULL);
1983         g_return_if_fail(item->path != NULL);
1984         g_return_if_fail(item->folder != NULL);
1985
1986         message = g_strdup_printf(_("Input new name for `%s':"),
1987                                   g_basename(item->path));
1988         new_folder = input_dialog(_("Rename folder"), message,
1989                                   g_basename(item->path));
1990         g_free(message);
1991         if (!new_folder) return;
1992
1993         if (folderview_find_by_name
1994                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1995                  new_folder)) {
1996                 alertpanel_error(_("The folder `%s' already exists."),
1997                                  new_folder);
1998                 g_free(new_folder);
1999                 return;
2000         }
2001
2002         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2003                 g_free(new_folder);
2004                 return;
2005         }
2006         g_free(new_folder);
2007
2008         gtk_clist_freeze(GTK_CLIST(ctree));
2009
2010         folderview_update_node(folderview, folderview->selected);
2011         folderview_sort_folders(folderview,
2012                                 GTK_CTREE_ROW(folderview->selected)->parent,
2013                                 item->folder);
2014         if (folderview->opened == folderview->selected) {
2015                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2016                         gtk_ctree_expand(ctree, folderview->opened);
2017                 summary_show(folderview->summaryview, item, FALSE);
2018         }
2019
2020         gtk_clist_thaw(GTK_CLIST(ctree));
2021
2022         folder_write_list();
2023 }
2024
2025 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2026                                         GtkWidget *widget)
2027 {
2028         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2029         FolderItem *item;
2030         gchar *message, *name, *name_;
2031         AlertValue avalue;
2032         gchar *old_path;
2033         gchar *old_id;
2034
2035         if (!folderview->selected) return;
2036
2037         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2038         g_return_if_fail(item != NULL);
2039         g_return_if_fail(item->path != NULL);
2040         g_return_if_fail(item->folder != NULL);
2041
2042         name_ = trim_string(item->name, 32);
2043         Xstrdup_a(name, name_, return);
2044         g_free(name_);
2045         message = g_strdup_printf
2046                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2047                    "Do you really want to delete?"), name);
2048         avalue = alertpanel(_("Delete folder"), message,
2049                             _("Yes"), _("+No"), NULL);
2050         g_free(message);
2051         if (avalue != G_ALERTDEFAULT) return;
2052
2053         Xstrdup_a(old_path, item->path, return);
2054         old_id = folder_item_get_identifier(item);
2055
2056         if (item->folder->remove_folder(item->folder, item) < 0) {
2057                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2058                 if (folderview->opened == folderview->selected)
2059                         summary_show(folderview->summaryview,
2060                                      folderview->summaryview->folder_item,
2061                                      FALSE);
2062                 g_free(old_id);
2063                 return;
2064         }
2065
2066         if (prefs_common.fltlist) {
2067                 if (folder_get_default_folder() == item->folder)
2068                         prefs_filter_delete_path(old_path);
2069                 prefs_filter_delete_path(old_id);
2070                 g_free(old_id);
2071         } else {
2072                 if (FOLDER_TYPE(item->folder) == F_MH)
2073                         prefs_filtering_delete_path(old_path);
2074                 prefs_filtering_delete_path(old_id);
2075                 g_free(old_id);
2076         }
2077         if (folderview->opened == folderview->selected ||
2078             gtk_ctree_is_ancestor(ctree,
2079                                   folderview->selected,
2080                                   folderview->opened)) {
2081                 summary_clear_all(folderview->summaryview);
2082                 folderview->opened = NULL;
2083         }
2084
2085         gtk_ctree_remove_node(ctree, folderview->selected);
2086         folder_write_list();
2087 }
2088
2089 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2090                                          GtkWidget *widget)
2091 {
2092         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2093         GtkCTreeNode *node;
2094         FolderItem *item;
2095         gchar *name, *name_;
2096         gchar *message;
2097         AlertValue avalue;
2098
2099         if (!folderview->selected) return;
2100         node = folderview->selected;
2101         item = gtk_ctree_node_get_row_data(ctree, node);
2102         g_return_if_fail(item != NULL);
2103         g_return_if_fail(item->folder != NULL);
2104         if (item->parent) return;
2105
2106         name_ = trim_string(item->folder->name, 32);
2107         Xstrdup_a(name, name_, return);
2108         g_free(name_);
2109         message = g_strdup_printf
2110                 (_("Really remove the mailbox `%s' ?\n"
2111                    "(The messages are NOT deleted from the disk)"), name);
2112         avalue = alertpanel(_("Remove mailbox"), message,
2113                             _("Yes"), _("+No"), NULL);
2114         g_free(message);
2115         if (avalue != G_ALERTDEFAULT) return;
2116
2117         folder_destroy(item->folder);
2118         summary_clear_all(folderview->summaryview);
2119         folderview_unselect(folderview);
2120         gtk_ctree_remove_node(ctree, node);
2121         folder_write_list();
2122 }
2123
2124 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2125                                           GtkWidget *widget)
2126 {
2127         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2128         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2129         GtkCTreeNode *node;
2130         FolderItem *item;
2131         FolderItem *new_item;
2132         gchar *new_folder;
2133         gchar *name, *name_;
2134         gchar *p;
2135
2136         if (!folderview->selected) return;
2137
2138         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2139         g_return_if_fail(item != NULL);
2140         g_return_if_fail(item->folder != NULL);
2141         g_return_if_fail(item->folder->type == F_IMAP);
2142         g_return_if_fail(item->folder->account != NULL);
2143
2144         new_folder = input_dialog
2145                 (_("New folder"),
2146                  _("Input the name of new folder:\n"
2147                    "(if you want to create a folder to store subfolders,\n"
2148                    " append `/' at the end of the name)"),
2149                  _("NewFolder"));
2150         if (!new_folder) return;
2151
2152         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2153             *(p + 1) != '\0') {
2154                 alertpanel_error(_("`%c' can't be included in folder name."),
2155                                  G_DIR_SEPARATOR);
2156                 g_free(new_folder);
2157                 return;
2158         }
2159
2160         name_ = trim_string(new_folder, 32);
2161         Xstrdup_a(name, name_, return);
2162         g_free(name_);
2163
2164         /* find whether the directory already exists */
2165         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2166                 alertpanel_error(_("The folder `%s' already exists."), name);
2167                 g_free(new_folder);
2168                 return;
2169         }
2170
2171         new_item = item->folder->create_folder(item->folder, item, new_folder);
2172         if (!new_item) {
2173                 alertpanel_error(_("Can't create the folder `%s'."), name);
2174                 g_free(new_folder);
2175                 return;
2176         }
2177         g_free(new_folder);
2178
2179         gtk_clist_freeze(GTK_CLIST(ctree));
2180
2181         text[COL_FOLDER] = new_item->name;
2182         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
2183                                      FOLDER_SPACING,
2184                                      folderxpm, folderxpmmask,
2185                                      folderopenxpm, folderopenxpmmask,
2186                                      FALSE, FALSE);
2187         gtk_ctree_expand(ctree, folderview->selected);
2188         gtk_ctree_node_set_row_data(ctree, node, new_item);
2189         folderview_sort_folders(folderview, folderview->selected, item->folder);
2190
2191         gtk_clist_thaw(GTK_CLIST(ctree));
2192
2193         folder_write_list();
2194 }
2195
2196 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2197                                          GtkWidget *widget)
2198 {
2199         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2200         FolderItem *item;
2201         gchar *name, *name_;
2202         gchar *message;
2203         AlertValue avalue;
2204
2205         if (!folderview->selected) return;
2206
2207         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2208         g_return_if_fail(item != NULL);
2209         g_return_if_fail(item->folder != NULL);
2210         g_return_if_fail(item->folder->type == F_IMAP);
2211         g_return_if_fail(item->folder->account != NULL);
2212
2213         name_ = trim_string(item->folder->name, 32);
2214         Xstrdup_a(name, name_, return);
2215         g_free(name_);
2216         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2217         avalue = alertpanel(_("Delete IMAP4 account"), message,
2218                             _("Yes"), _("+No"), NULL);
2219         g_free(message);
2220
2221         if (avalue != G_ALERTDEFAULT) return;
2222
2223         if (folderview->opened == folderview->selected ||
2224             gtk_ctree_is_ancestor(ctree,
2225                                   folderview->selected,
2226                                   folderview->opened)) {
2227                 summary_clear_all(folderview->summaryview);
2228                 folderview->opened = NULL;
2229         }
2230
2231         account_destroy(item->folder->account);
2232         folder_destroy(item->folder);
2233         gtk_ctree_remove_node(ctree, folderview->selected);
2234         account_set_menu();
2235         main_window_reflect_prefs_all();
2236         folder_write_list();
2237 }
2238
2239 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2240                                          GtkWidget *widget)
2241 {
2242         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2243         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2244         GtkCTreeNode *servernode, *node;
2245         Folder *folder;
2246         FolderItem *item;
2247         FolderItem *rootitem;
2248         FolderItem *newitem;
2249         GSList *new_subscr;
2250         GSList *cur;
2251         GNode *gnode;
2252
2253         if (!folderview->selected) return;
2254
2255         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2256         g_return_if_fail(item != NULL);
2257         folder = item->folder;
2258         g_return_if_fail(folder != NULL);
2259         g_return_if_fail(folder->type == F_NEWS);
2260         g_return_if_fail(folder->account != NULL);
2261
2262         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2263                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2264         else
2265                 servernode = folderview->selected;
2266
2267         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2268
2269         new_subscr = grouplist_dialog(folder);
2270
2271         /* remove unsubscribed newsgroups */
2272         for (gnode = folder->node->children; gnode != NULL; ) {
2273                 GNode *next = gnode->next;
2274
2275                 item = FOLDER_ITEM(gnode->data);
2276                 if (g_slist_find_custom(new_subscr, item->path,
2277                                         (GCompareFunc)g_strcasecmp) != NULL) {
2278                         gnode = next;
2279                         continue;
2280                 }
2281
2282                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2283                 if (!node) {
2284                         gnode = next;
2285                         continue;
2286                 }
2287
2288                 if (folderview->opened == node) {
2289                         summary_clear_all(folderview->summaryview);
2290                         folderview->opened = NULL;
2291                 }
2292
2293                 folder_item_remove(item);
2294                 gtk_ctree_remove_node(ctree, node);
2295
2296                 gnode = next;
2297         }
2298
2299         gtk_clist_freeze(GTK_CLIST(ctree));
2300
2301         /* add subscribed newsgroups */
2302         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2303                 gchar *name = (gchar *)cur->data;
2304
2305                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2306                         continue;
2307
2308                 text[COL_FOLDER] = name;
2309                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2310                                              FOLDER_SPACING,
2311                                              folderxpm, folderxpmmask,
2312                                              folderopenxpm, folderopenxpmmask,
2313                                              FALSE, FALSE);
2314                 gtk_ctree_expand(ctree, servernode);
2315
2316                 newitem = folder_item_new(name, name);
2317                 folder_item_append(rootitem, newitem);
2318                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2319         }
2320
2321         folderview_sort_folders(folderview, servernode, folder);
2322         gtk_clist_thaw(GTK_CLIST(ctree));
2323
2324         slist_free_strings(new_subscr);
2325         g_slist_free(new_subscr);
2326
2327         folder_write_list();
2328 }
2329
2330 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2331                                         GtkWidget *widget)
2332 {
2333         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2334         FolderItem *item;
2335         gchar *name, *name_;
2336         gchar *message;
2337         AlertValue avalue;
2338
2339         if (!folderview->selected) return;
2340
2341         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2342         g_return_if_fail(item != NULL);
2343         g_return_if_fail(item->folder != NULL);
2344         g_return_if_fail(item->folder->type == F_NEWS);
2345         g_return_if_fail(item->folder->account != NULL);
2346
2347         name_ = trim_string(item->path, 32);
2348         Xstrdup_a(name, name_, return);
2349         g_free(name_);
2350         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2351         avalue = alertpanel(_("Delete newsgroup"), message,
2352                             _("Yes"), _("+No"), NULL);
2353         g_free(message);
2354         if (avalue != G_ALERTDEFAULT) return;
2355
2356         if (folderview->opened == folderview->selected) {
2357                 summary_clear_all(folderview->summaryview);
2358                 folderview->opened = NULL;
2359         }
2360
2361         folder_item_remove(item);
2362         gtk_ctree_remove_node(ctree, folderview->selected);
2363         folder_write_list();
2364 }
2365
2366 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2367                                          GtkWidget *widget)
2368 {
2369         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2370         FolderItem *item;
2371         gchar *name, *name_;
2372         gchar *message;
2373         AlertValue avalue;
2374
2375         if (!folderview->selected) return;
2376
2377         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2378         g_return_if_fail(item != NULL);
2379         g_return_if_fail(item->folder != NULL);
2380         g_return_if_fail(item->folder->type == F_NEWS);
2381         g_return_if_fail(item->folder->account != NULL);
2382
2383         name_ = trim_string(item->folder->name, 32);
2384         Xstrdup_a(name, name_, return);
2385         g_free(name_);
2386         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2387         avalue = alertpanel(_("Delete news account"), message,
2388                             _("Yes"), _("+No"), NULL);
2389         g_free(message);
2390
2391         if (avalue != G_ALERTDEFAULT) return;
2392
2393         if (folderview->opened == folderview->selected ||
2394             gtk_ctree_is_ancestor(ctree,
2395                                   folderview->selected,
2396                                   folderview->opened)) {
2397                 summary_clear_all(folderview->summaryview);
2398                 folderview->opened = NULL;
2399         }
2400
2401         account_destroy(item->folder->account);
2402         folder_destroy(item->folder);
2403         gtk_ctree_remove_node(ctree, folderview->selected);
2404         account_set_menu();
2405         main_window_reflect_prefs_all();
2406         folder_write_list();
2407 }
2408
2409 static void folderview_search_cb(FolderView *folderview, guint action,
2410                                  GtkWidget *widget)
2411 {
2412         summary_search(folderview->summaryview);
2413 }
2414
2415 static void folderview_property_cb(FolderView *folderview, guint action,
2416                                    GtkWidget *widget)
2417 {
2418         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2419         FolderItem *item;
2420
2421         if (!folderview->selected) return;
2422
2423         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2424         g_return_if_fail(item != NULL);
2425         g_return_if_fail(item->folder != NULL);
2426
2427 #if CLAWS
2428         prefs_folder_item_create(folderview, item);
2429 #else
2430         /*
2431          * CLAWS: wait till Hiro has completed his stuff
2432          */
2433         prefs_folder_item_open(item);
2434 #endif  
2435 }
2436
2437 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2438                                           GdkDragContext *context,
2439                                           gint            x,
2440                                           gint            y,
2441                                           guint           time,
2442                                           FolderView     *folderview)
2443 {
2444         gint row, column;
2445         FolderItem *item, *src_item;
2446         GtkCTreeNode *node = NULL;
2447         gboolean acceptable = FALSE;
2448
2449         if (gtk_clist_get_selection_info
2450                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2451                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2452                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2453                 src_item = folderview->summaryview->folder_item;
2454                 if (item && item->folder && item->path &&
2455                     src_item && src_item != item) {
2456                         switch (item->folder->type) {
2457                         case F_MH:
2458                         case F_IMAP:
2459                                 acceptable = TRUE;
2460                                 break;
2461                         default:
2462                                 break;
2463                         }
2464                 }
2465         }
2466
2467         if (acceptable) {
2468                 gtk_signal_handler_block_by_func
2469                         (GTK_OBJECT(widget),
2470                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2471                 gtk_ctree_select(GTK_CTREE(widget), node);
2472                 gtk_signal_handler_unblock_by_func
2473                         (GTK_OBJECT(widget),
2474                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2475                 gdk_drag_status(context, 
2476                                         (context->actions == GDK_ACTION_COPY ?
2477                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2478         } else {
2479                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2480                 gdk_drag_status(context, 0, time);
2481         }
2482
2483         return acceptable;
2484 }
2485
2486 static void folderview_drag_leave_cb(GtkWidget      *widget,
2487                                      GdkDragContext *context,
2488                                      guint           time,
2489                                      FolderView     *folderview)
2490 {
2491         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2492 }
2493
2494 static void folderview_drag_received_cb(GtkWidget        *widget,
2495                                         GdkDragContext   *drag_context,
2496                                         gint              x,
2497                                         gint              y,
2498                                         GtkSelectionData *data,
2499                                         guint             info,
2500                                         guint             time,
2501                                         FolderView       *folderview)
2502 {
2503         gint row, column;
2504         FolderItem *item, *src_item;
2505         GtkCTreeNode *node;
2506
2507         if (gtk_clist_get_selection_info
2508                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2509                 return;
2510
2511         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2512         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2513         src_item = folderview->summaryview->folder_item;
2514         if (item && src_item) {
2515                 switch (drag_context->action) {
2516                         case GDK_ACTION_COPY:
2517                                 summary_copy_selected_to(folderview->summaryview, item);
2518                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2519                                 break;
2520                         case GDK_ACTION_MOVE:
2521                         case GDK_ACTION_DEFAULT:
2522                         default:
2523                 if (src_item->folder->type != item->folder->type ||
2524                     (item->folder->type == F_IMAP &&
2525                      src_item->folder != item->folder))
2526                         summary_copy_selected_to(folderview->summaryview, item);
2527                 else
2528                         summary_move_selected_to(folderview->summaryview, item);
2529                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2530                 }
2531         } else
2532                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2533 }
2534
2535 static gint folderview_clist_compare(GtkCList *clist,
2536                                      gconstpointer ptr1, gconstpointer ptr2)
2537 {
2538         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2539         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2540
2541         if (!item1->name)
2542                 return (item2->name != NULL);
2543         if (!item2->name)
2544                 return -1;
2545
2546         return g_strcasecmp(item1->name, item2->name);
2547 }
2548
2549 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2550 {
2551         const FolderItem *item = a;
2552         const gchar *name = b;
2553
2554         if (!item->path) return -1;
2555         return strcmp2(g_basename(item->path), name);
2556 }
2557
2558 static void folderview_scoring_cb(FolderView *folderview, guint action,
2559                                    GtkWidget *widget)
2560 {
2561         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2562         FolderItem *item;
2563
2564         if (!folderview->selected) return;
2565
2566         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2567         g_return_if_fail(item != NULL);
2568         g_return_if_fail(item->folder != NULL);
2569
2570         prefs_scoring_open(item);
2571 }
2572
2573 static void folderview_processing_cb(FolderView *folderview, guint action,
2574                                      GtkWidget *widget)
2575 {
2576         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2577         FolderItem *item;
2578
2579         if (!folderview->selected) return;
2580
2581         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2582         g_return_if_fail(item != NULL);
2583         g_return_if_fail(item->folder != NULL);
2584
2585         prefs_filtering_open(item, NULL, NULL);
2586 }
2587
2588 void folderview_set_target_folder_color(gint color_op) 
2589 {
2590         gint firstone = 1;
2591         GList *list;
2592         FolderView *folderview;
2593
2594         for (list = folderview_list; list != NULL; list = list->next) {
2595                 folderview = (FolderView *)list->data;
2596                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2597                 if (firstone) {
2598                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2599                                 folderview->color_op;
2600                         firstone = 0;
2601                 }
2602         }
2603 }
2604
2605 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2606 {
2607         folderview_init(folderview);
2608         folderview_set_all();
2609 }