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