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