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