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