toolbar reply refactoring: part 2
[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         /* messageview could have deleted messages in this folder */
1764         if (prefs_common.immediate_exec)
1765                 summary_execute(folderview->summaryview);
1766
1767         folder_clean_cache_memory();
1768
1769         if (!opened) {
1770                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1771                 gtk_ctree_select(ctree, folderview->opened);
1772         } else {
1773                 folderview->opened = row;
1774                 if (gtk_ctree_node_is_visible(ctree, row)
1775                     != GTK_VISIBILITY_FULL)
1776                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1777         }
1778
1779         STATUSBAR_POP(folderview->mainwin);
1780
1781         folderview->open_folder = FALSE;
1782         can_select = TRUE;
1783 }
1784
1785 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1786                                      FolderView *folderview)
1787 {
1788         FolderItem *item;
1789
1790         item = gtk_ctree_node_get_row_data(ctree, node);
1791         g_return_if_fail(item != NULL);
1792         item->collapsed = FALSE;
1793         folderview_update_node(folderview, node);
1794 }
1795
1796 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1797                                       FolderView *folderview)
1798 {
1799         FolderItem *item;
1800
1801         item = gtk_ctree_node_get_row_data(ctree, node);
1802         g_return_if_fail(item != NULL);
1803         item->collapsed= TRUE;
1804         folderview_update_node(folderview, node);
1805 }
1806
1807 static void folderview_popup_close(GtkMenuShell *menu_shell,
1808                                    FolderView *folderview)
1809 {
1810         if (!folderview->opened) return;
1811
1812         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1813                                   folderview->opened);
1814         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1815 }
1816
1817 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1818                                    FolderView *folderview)
1819 {
1820         switch (column) {
1821         case COL_FOLDER:
1822                 prefs_common.folder_col_folder = width;
1823                 break;
1824         case COL_NEW:
1825                 prefs_common.folder_col_new = width;
1826                 break;
1827         case COL_UNREAD:
1828                 prefs_common.folder_col_unread = width;
1829                 break;
1830         case COL_TOTAL:
1831                 prefs_common.folder_col_total = width;
1832                 break;
1833         default:
1834                 break;
1835         }
1836 }
1837
1838 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1839                                              GtkCTreeNode *node,
1840                                              const gchar *name)
1841 {
1842         FolderItem *item;
1843
1844         if (!node)
1845                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1846         if (!node)
1847                 return NULL;
1848
1849         node = GTK_CTREE_ROW(node)->children;
1850
1851         while (node) {
1852                 item = gtk_ctree_node_get_row_data(ctree, node);
1853                 if (!folderview_compare_name(item, name))
1854                         return node;
1855                 node = GTK_CTREE_ROW(node)->sibling;
1856         }
1857
1858         return NULL;
1859 }
1860
1861 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1862                                       GtkWidget *widget)
1863 {
1864         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1865         FolderItem *item;
1866
1867         if (!folderview->selected) return;
1868
1869         summary_show(folderview->summaryview, NULL);
1870
1871         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1872         g_return_if_fail(item != NULL);
1873         g_return_if_fail(item->folder != NULL);
1874
1875         if (action == 0)
1876                 folderview_check_new(item->folder);
1877         else
1878                 folderview_rescan_tree(item->folder);
1879 }
1880
1881 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1882 {
1883         GNode *srcnode;
1884
1885         folderview_create_folder_node(folderview, item);
1886         
1887         srcnode = item->folder->node;   
1888         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1889         srcnode = srcnode->children;
1890         while (srcnode != NULL) {
1891                 if (srcnode && srcnode->data) {
1892                         FolderItem *next_item = (FolderItem*) srcnode->data;
1893                         folderview_create_folder_node_recursive(folderview, next_item);
1894                 }
1895                 srcnode = srcnode->next;
1896         }
1897 }
1898
1899 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1900 {
1901         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1902         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1903         GtkCTreeNode *node, *parent_node;
1904         
1905         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, item->parent);
1906
1907         gtk_clist_freeze(GTK_CLIST(ctree));
1908
1909         text[COL_FOLDER] = item->name;
1910         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1911                                      FOLDER_SPACING,
1912                                      folderxpm, folderxpmmask,
1913                                      folderopenxpm, folderopenxpmmask,
1914                                      FALSE, FALSE);
1915         gtk_ctree_expand(ctree, parent_node);
1916         gtk_ctree_node_set_row_data(ctree, node, item);
1917         if (normal_style)
1918                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1919         folderview_sort_folders(folderview, folderview->selected, item->folder);
1920
1921         gtk_clist_thaw(GTK_CLIST(ctree));
1922 }
1923
1924 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1925                                      GtkWidget *widget)
1926 {
1927         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1928         FolderItem *item;
1929         FolderItem *new_item;
1930         gchar *new_folder;
1931         gchar *name, *name_;
1932
1933         if (!folderview->selected) return;
1934
1935         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1936         g_return_if_fail(item != NULL);
1937         g_return_if_fail(item->folder != NULL);
1938
1939         new_folder = input_dialog(_("New folder"),
1940                                   _("Input the name of new folder:"),
1941                                   _("NewFolder"));
1942         if (!new_folder) return;
1943
1944         if (item->folder->type != F_MBOX) {
1945                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1946                         alertpanel_error(_("`%c' can't be included in folder name."),
1947                                          G_DIR_SEPARATOR);
1948                         g_free(new_folder);
1949                         return;
1950                 }
1951         }
1952
1953         name_ = trim_string(new_folder, 32);
1954         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1955         g_free(name_);
1956
1957         /* find whether the directory already exists */
1958         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1959                 alertpanel_error(_("The folder `%s' already exists."), name);
1960                 g_free(new_folder);
1961                 return;
1962         }
1963
1964         new_item = folder_create_folder(item, new_folder);
1965         if (!new_item) {
1966                 alertpanel_error(_("Can't create the folder `%s'."), name);
1967                 g_free(new_folder);
1968                 return;
1969         } 
1970         g_free(new_folder);
1971
1972         folderview_create_folder_node(folderview, new_item);
1973
1974         folder_write_list();
1975 }
1976
1977 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1978                                           GtkWidget *widget)
1979 {
1980         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1981         FolderItem *item;
1982         FolderItem *new_item;
1983         gchar *new_folder;
1984
1985         if (!folderview->selected) return;
1986
1987         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1988         g_return_if_fail(item != NULL);
1989         g_return_if_fail(item->folder != NULL);
1990
1991         new_folder = input_dialog(_("New folder"),
1992                                   _("Input the name of new folder:"),
1993                                   _("NewFolder"));
1994         if (!new_folder) return;
1995
1996         /* find whether the directory already exists */
1997         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1998                 alertpanel_error(_("The folder `%s' already exists."),
1999                                  new_folder);
2000                 g_free(new_folder);
2001                 return;
2002         }
2003
2004         new_item = folder_create_folder(item, new_folder);
2005         g_free(new_folder);
2006         if (!new_item) return;
2007
2008         folderview_create_folder_node(folderview, new_item);
2009
2010         folder_write_list();
2011 }
2012
2013 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
2014                                         GtkWidget *widget)
2015 {
2016         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2017         FolderItem *item;
2018         gchar *new_folder;
2019         gchar *name, *name_;
2020         gchar *message;
2021         gchar *old_path;
2022         gchar *old_id;
2023         gchar *new_id;
2024
2025         if (!folderview->selected) return;
2026
2027         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2028         g_return_if_fail(item != NULL);
2029         g_return_if_fail(item->path != NULL);
2030         g_return_if_fail(item->folder != NULL);
2031
2032         name_ = trim_string(item->name, 32);
2033         Xstrdup_a(name, name_, return);
2034         g_free(name_);
2035         message = g_strdup_printf(_("Input new name for `%s':"), name);
2036         new_folder = input_dialog(_("Rename folder"), message,
2037                                   g_basename(item->path));
2038         g_free(message);
2039         if (!new_folder) return;
2040
2041         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
2042                 alertpanel_error(_("`%c' can't be included in folder name."),
2043                                  G_DIR_SEPARATOR);
2044                 g_free(new_folder);
2045                 return;
2046         }
2047
2048         if (folderview_find_by_name
2049                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2050                  new_folder)) {
2051                 name = trim_string(new_folder, 32);
2052                 alertpanel_error(_("The folder `%s' already exists."), name);
2053                 g_free(name);
2054                 g_free(new_folder);
2055                 return;
2056         }
2057
2058         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
2059         old_id = folder_item_get_identifier(item);
2060
2061         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2062                 g_free(old_id);
2063                 g_free(new_folder);
2064                 return;
2065         }
2066         g_free(new_folder);
2067
2068         /* if (FOLDER_TYPE(item->folder) == F_MH)
2069                 prefs_filtering_rename_path(old_path, item->path); */
2070         new_id = folder_item_get_identifier(item);
2071         prefs_filtering_rename_path(old_id, new_id);
2072
2073         g_free(old_id);
2074         g_free(new_id);
2075
2076         gtk_clist_freeze(GTK_CLIST(ctree));
2077
2078         folderview_update_node(folderview, folderview->selected);
2079         folderview_sort_folders(folderview,
2080                                 GTK_CTREE_ROW(folderview->selected)->parent,
2081                                 item->folder);
2082         if (folderview->opened == folderview->selected ||
2083             gtk_ctree_is_ancestor(ctree,
2084                                   folderview->selected,
2085                                   folderview->opened)) {
2086                 GtkCTreeNode *node = folderview->opened;
2087                 folderview_unselect(folderview);
2088                 folderview_select_node(folderview, node);
2089         }
2090
2091         gtk_clist_thaw(GTK_CLIST(ctree));
2092
2093         folder_write_list();
2094 }
2095
2096 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
2097                                              guint action,
2098                                              GtkWidget *widget)
2099 {
2100         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2101         FolderItem *item;
2102         gchar *new_folder;
2103         gchar *message;
2104
2105         if (!folderview->selected) return;
2106
2107         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2108         g_return_if_fail(item != NULL);
2109         g_return_if_fail(item->path != NULL);
2110         g_return_if_fail(item->folder != NULL);
2111
2112         message = g_strdup_printf(_("Input new name for `%s':"),
2113                                   g_basename(item->path));
2114         new_folder = input_dialog(_("Rename folder"), message,
2115                                   g_basename(item->path));
2116         g_free(message);
2117         if (!new_folder) return;
2118
2119         if (folderview_find_by_name
2120                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2121                  new_folder)) {
2122                 alertpanel_error(_("The folder `%s' already exists."),
2123                                  new_folder);
2124                 g_free(new_folder);
2125                 return;
2126         }
2127
2128         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2129                 g_free(new_folder);
2130                 return;
2131         }
2132         g_free(new_folder);
2133
2134         gtk_clist_freeze(GTK_CLIST(ctree));
2135
2136         folderview_update_node(folderview, folderview->selected);
2137         folderview_sort_folders(folderview,
2138                                 GTK_CTREE_ROW(folderview->selected)->parent,
2139                                 item->folder);
2140         if (folderview->opened == folderview->selected) {
2141                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2142                         gtk_ctree_expand(ctree, folderview->opened);
2143                 summary_show(folderview->summaryview, item);
2144         }
2145
2146         gtk_clist_thaw(GTK_CLIST(ctree));
2147
2148         folder_write_list();
2149 }
2150
2151 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2152                                         GtkWidget *widget)
2153 {
2154         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2155         FolderItem *item;
2156         gchar *message, *name, *name_;
2157         AlertValue avalue;
2158         gchar *old_path;
2159         gchar *old_id;
2160
2161         if (!folderview->selected) return;
2162
2163         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2164         g_return_if_fail(item != NULL);
2165         g_return_if_fail(item->path != NULL);
2166         g_return_if_fail(item->folder != NULL);
2167
2168         name_ = trim_string(item->name, 32);
2169         Xstrdup_a(name, name_, return);
2170         g_free(name_);
2171         message = g_strdup_printf
2172                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2173                    "Do you really want to delete?"), name);
2174         avalue = alertpanel(_("Delete folder"), message,
2175                             _("Yes"), _("+No"), NULL);
2176         g_free(message);
2177         if (avalue != G_ALERTDEFAULT) return;
2178
2179         Xstrdup_a(old_path, item->path, return);
2180         old_id = folder_item_get_identifier(item);
2181
2182         if (item->folder->remove_folder(item->folder, item) < 0) {
2183                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2184                 if (folderview->opened == folderview->selected)
2185                         summary_show(folderview->summaryview,
2186                                      folderview->summaryview->folder_item);
2187                 g_free(old_id);
2188                 return;
2189         }
2190
2191         /* if (FOLDER_TYPE(item->folder) == F_MH)
2192                 prefs_filtering_delete_path(old_path); */
2193
2194         if (folderview->opened == folderview->selected ||
2195             gtk_ctree_is_ancestor(ctree,
2196                                   folderview->selected,
2197                                   folderview->opened)) {
2198                 summary_clear_all(folderview->summaryview);
2199                 folderview->opened = NULL;
2200         }
2201
2202         gtk_ctree_remove_node(ctree, folderview->selected);
2203         folder_write_list();
2204
2205         prefs_filtering_delete_path(old_id);
2206         g_free(old_id);
2207
2208 }
2209
2210 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2211                                          GtkWidget *widget)
2212 {
2213         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2214         GtkCTreeNode *node;
2215         FolderItem *item;
2216         gchar *name, *name_;
2217         gchar *message;
2218         AlertValue avalue;
2219
2220         if (!folderview->selected) return;
2221         node = folderview->selected;
2222         item = gtk_ctree_node_get_row_data(ctree, node);
2223         g_return_if_fail(item != NULL);
2224         g_return_if_fail(item->folder != NULL);
2225         if (item->parent) return;
2226
2227         name_ = trim_string(item->folder->name, 32);
2228         Xstrdup_a(name, name_, return);
2229         g_free(name_);
2230         message = g_strdup_printf
2231                 (_("Really remove the mailbox `%s' ?\n"
2232                    "(The messages are NOT deleted from the disk)"), name);
2233         avalue = alertpanel(_("Remove mailbox"), message,
2234                             _("Yes"), _("+No"), NULL);
2235         g_free(message);
2236         if (avalue != G_ALERTDEFAULT) return;
2237
2238         folder_destroy(item->folder);
2239         summary_clear_all(folderview->summaryview);
2240         folderview_unselect(folderview);
2241         gtk_ctree_remove_node(ctree, node);
2242         folder_write_list();
2243 }
2244
2245 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2246                                           GtkWidget *widget)
2247 {
2248         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2249         FolderItem *item;
2250         FolderItem *new_item;
2251         gchar *new_folder;
2252         gchar *name, *name_;
2253         gchar *p;
2254
2255         if (!folderview->selected) return;
2256
2257         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2258         g_return_if_fail(item != NULL);
2259         g_return_if_fail(item->folder != NULL);
2260         g_return_if_fail(item->folder->type == F_IMAP);
2261         g_return_if_fail(item->folder->account != NULL);
2262
2263         new_folder = input_dialog
2264                 (_("New folder"),
2265                  _("Input the name of new folder:\n"
2266                    "(if you want to create a folder to store subfolders,\n"
2267                    " append `/' at the end of the name)"),
2268                  _("NewFolder"));
2269         if (!new_folder) return;
2270
2271         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2272             *(p + 1) != '\0') {
2273                 alertpanel_error(_("`%c' can't be included in folder name."),
2274                                  G_DIR_SEPARATOR);
2275                 g_free(new_folder);
2276                 return;
2277         }
2278
2279         name_ = trim_string(new_folder, 32);
2280         Xstrdup_a(name, name_, return);
2281         g_free(name_);
2282
2283         /* find whether the directory already exists */
2284         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2285                 alertpanel_error(_("The folder `%s' already exists."), name);
2286                 g_free(new_folder);
2287                 return;
2288         }
2289
2290         new_item = folder_create_folder(item, new_folder);
2291         if (!new_item) {
2292                 alertpanel_error(_("Can't create the folder `%s'."), name);
2293                 g_free(new_folder);
2294                 return;
2295         }
2296         g_free(new_folder);
2297
2298         folderview_create_folder_node(folderview, new_item);
2299
2300         folder_write_list();
2301 }
2302
2303 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2304                                          GtkWidget *widget)
2305 {
2306         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2307         FolderItem *item;
2308         PrefsAccount *account;
2309         gchar *name, *name_;
2310         gchar *message;
2311         AlertValue avalue;
2312
2313         if (!folderview->selected) return;
2314
2315         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2316         g_return_if_fail(item != NULL);
2317         g_return_if_fail(item->folder != NULL);
2318         g_return_if_fail(item->folder->type == F_IMAP);
2319         g_return_if_fail(item->folder->account != NULL);
2320
2321         name_ = trim_string(item->folder->name, 32);
2322         Xstrdup_a(name, name_, return);
2323         g_free(name_);
2324         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2325         avalue = alertpanel(_("Delete IMAP4 account"), message,
2326                             _("Yes"), _("+No"), NULL);
2327         g_free(message);
2328
2329         if (avalue != G_ALERTDEFAULT) return;
2330
2331         if (folderview->opened == folderview->selected ||
2332             gtk_ctree_is_ancestor(ctree,
2333                                   folderview->selected,
2334                                   folderview->opened)) {
2335                 summary_clear_all(folderview->summaryview);
2336                 folderview->opened = NULL;
2337         }
2338
2339         account = item->folder->account;
2340         folder_destroy(item->folder);
2341         account_destroy(account);
2342         gtk_ctree_remove_node(ctree, folderview->selected);
2343         account_set_menu();
2344         main_window_reflect_prefs_all();
2345         folder_write_list();
2346 }
2347
2348 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2349                                          GtkWidget *widget)
2350 {
2351         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2352         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2353         GtkCTreeNode *servernode, *node;
2354         Folder *folder;
2355         FolderItem *item;
2356         FolderItem *rootitem;
2357         FolderItem *newitem;
2358         GSList *new_subscr;
2359         GSList *cur;
2360         GNode *gnode;
2361
2362         if (!folderview->selected) return;
2363
2364         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2365         g_return_if_fail(item != NULL);
2366         folder = item->folder;
2367         g_return_if_fail(folder != NULL);
2368         g_return_if_fail(folder->type == F_NEWS);
2369         g_return_if_fail(folder->account != NULL);
2370
2371         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2372                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2373         else
2374                 servernode = folderview->selected;
2375
2376         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2377
2378         new_subscr = grouplist_dialog(folder);
2379
2380         /* remove unsubscribed newsgroups */
2381         for (gnode = folder->node->children; gnode != NULL; ) {
2382                 GNode *next = gnode->next;
2383
2384                 item = FOLDER_ITEM(gnode->data);
2385                 if (g_slist_find_custom(new_subscr, item->path,
2386                                         (GCompareFunc)g_strcasecmp) != NULL) {
2387                         gnode = next;
2388                         continue;
2389                 }
2390
2391                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2392                 if (!node) {
2393                         gnode = next;
2394                         continue;
2395                 }
2396
2397                 if (folderview->opened == node) {
2398                         summary_clear_all(folderview->summaryview);
2399                         folderview->opened = NULL;
2400                 }
2401
2402                 folder_item_remove(item);
2403                 gtk_ctree_remove_node(ctree, node);
2404
2405                 gnode = next;
2406         }
2407
2408         gtk_clist_freeze(GTK_CLIST(ctree));
2409
2410         /* add subscribed newsgroups */
2411         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2412                 gchar *name = (gchar *)cur->data;
2413
2414                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2415                         continue;
2416
2417                 text[COL_FOLDER] = name;
2418                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2419                                              FOLDER_SPACING,
2420                                              folderxpm, folderxpmmask,
2421                                              folderopenxpm, folderopenxpmmask,
2422                                              FALSE, FALSE);
2423                 gtk_ctree_expand(ctree, servernode);
2424
2425                 newitem = folder_item_new(folder, name, name);
2426                 folder_item_append(rootitem, newitem);
2427                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2428         }
2429
2430         folderview_sort_folders(folderview, servernode, folder);
2431         gtk_clist_thaw(GTK_CLIST(ctree));
2432
2433         slist_free_strings(new_subscr);
2434         g_slist_free(new_subscr);
2435
2436         folder_write_list();
2437 }
2438
2439 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2440                                         GtkWidget *widget)
2441 {
2442         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2443         FolderItem *item;
2444         gchar *name, *name_;
2445         gchar *message;
2446         AlertValue avalue;
2447
2448         if (!folderview->selected) return;
2449
2450         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2451         g_return_if_fail(item != NULL);
2452         g_return_if_fail(item->folder != NULL);
2453         g_return_if_fail(item->folder->type == F_NEWS);
2454         g_return_if_fail(item->folder->account != NULL);
2455
2456         name_ = trim_string(item->path, 32);
2457         Xstrdup_a(name, name_, return);
2458         g_free(name_);
2459         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2460         avalue = alertpanel(_("Delete newsgroup"), message,
2461                             _("Yes"), _("+No"), NULL);
2462         g_free(message);
2463         if (avalue != G_ALERTDEFAULT) return;
2464
2465         if (folderview->opened == folderview->selected) {
2466                 summary_clear_all(folderview->summaryview);
2467                 folderview->opened = NULL;
2468         }
2469
2470         folder_item_remove(item);
2471         gtk_ctree_remove_node(ctree, folderview->selected);
2472         folder_write_list();
2473         
2474         prefs_filtering_delete_path(name);
2475 }
2476
2477 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2478                                          GtkWidget *widget)
2479 {
2480         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2481         FolderItem *item;
2482         PrefsAccount *account;
2483         gchar *name, *name_;
2484         gchar *message;
2485         AlertValue avalue;
2486
2487         if (!folderview->selected) return;
2488
2489         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2490         g_return_if_fail(item != NULL);
2491         g_return_if_fail(item->folder != NULL);
2492         g_return_if_fail(item->folder->type == F_NEWS);
2493         g_return_if_fail(item->folder->account != NULL);
2494
2495         name_ = trim_string(item->folder->name, 32);
2496         Xstrdup_a(name, name_, return);
2497         g_free(name_);
2498         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2499         avalue = alertpanel(_("Delete news account"), message,
2500                             _("Yes"), _("+No"), NULL);
2501         g_free(message);
2502
2503         if (avalue != G_ALERTDEFAULT) return;
2504
2505         if (folderview->opened == folderview->selected ||
2506             gtk_ctree_is_ancestor(ctree,
2507                                   folderview->selected,
2508                                   folderview->opened)) {
2509                 summary_clear_all(folderview->summaryview);
2510                 folderview->opened = NULL;
2511         }
2512
2513         account = item->folder->account;
2514         folder_destroy(item->folder);
2515         account_destroy(account);
2516         gtk_ctree_remove_node(ctree, folderview->selected);
2517         account_set_menu();
2518         main_window_reflect_prefs_all();
2519         folder_write_list();
2520 }
2521
2522 static void folderview_search_cb(FolderView *folderview, guint action,
2523                                  GtkWidget *widget)
2524 {
2525         summary_search(folderview->summaryview);
2526 }
2527
2528 static void folderview_property_cb(FolderView *folderview, guint action,
2529                                    GtkWidget *widget)
2530 {
2531         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2532         FolderItem *item;
2533
2534         if (!folderview->selected) return;
2535
2536         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2537         g_return_if_fail(item != NULL);
2538         g_return_if_fail(item->folder != NULL);
2539
2540         if (item->parent == NULL && item->folder->account)
2541                 account_open(item->folder->account);
2542         else {
2543                 summary_save_prefs_to_folderitem(folderview->summaryview, item);
2544                 prefs_folder_item_create(folderview, item);
2545         }
2546 }
2547
2548 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2549 {
2550         GSList *list = NULL;
2551         GSList *done = NULL;
2552         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2553         
2554         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2555                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2556                 &&  list->data != node) {
2557                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2558                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2559                 }
2560         }
2561         for (list = done; list != NULL; list = g_slist_next(list)) {
2562                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2563                                                                  list->data);
2564         }
2565         g_slist_free(done);
2566 }
2567
2568 static void folderview_move_to_cb(FolderView *folderview) 
2569 {
2570         FolderItem *from_folder = NULL, *to_folder = NULL;
2571
2572         if (folderview->selected)
2573                 from_folder = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree), folderview->selected);
2574         if (!from_folder || from_folder->folder->type == F_NEWS)
2575                 return;
2576
2577         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
2578         
2579         if (!to_folder || to_folder->folder->type == F_NEWS)
2580                 return;
2581
2582         folderview_move_to(folderview, from_folder, to_folder);
2583 }
2584
2585 static void folderview_move_to(FolderView *folderview, FolderItem *from_folder,
2586                                FolderItem *to_folder)
2587 {
2588         FolderItem *from_parent = NULL;
2589         FolderItem *new_folder = NULL;
2590         GtkCTreeNode *src_node = NULL;
2591         gchar *buf;
2592         gint status;
2593
2594         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2595         from_parent = from_folder->parent;
2596         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2597         STATUSBAR_PUSH(folderview->mainwin, buf);
2598         g_free(buf);
2599         summary_clear_all(folderview->summaryview);
2600         folderview->opened = NULL;
2601         folderview->selected = NULL;
2602         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2603         inc_lock();
2604         main_window_cursor_wait(folderview->mainwin);
2605         statusbar_verbosity_set(TRUE);
2606         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2607                 statusbar_verbosity_set(FALSE);
2608                 main_window_cursor_normal(folderview->mainwin);
2609                 STATUSBAR_POP(folderview->mainwin);
2610                 if (src_node)
2611                         gtk_ctree_remove_node(GTK_CTREE(folderview->ctree), src_node);
2612                 else 
2613                         debug_print("can't remove src node: is null\n");
2614
2615                 folderview_create_folder_node_recursive(folderview, new_folder);
2616                 folderview_sort_folders(folderview, 
2617                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2618                                 NULL, new_folder->parent), new_folder->folder);
2619                 folderview_select(folderview, new_folder);
2620         } else {
2621                 statusbar_verbosity_set(FALSE);         
2622                 main_window_cursor_normal(folderview->mainwin);
2623                 STATUSBAR_POP(folderview->mainwin);
2624                 switch (status) {
2625                 case F_MOVE_FAILED_DEST_IS_PARENT:
2626                         alertpanel_error(_("Source and destination are the same."));
2627                         break;
2628                 case F_MOVE_FAILED_DEST_IS_CHILD:
2629                         alertpanel_error(_("Can't move a folder to one of its children."));
2630                         break;
2631                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2632                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2633                         break;
2634                 default:
2635                         alertpanel_error(_("Move failed!"));
2636                         break;
2637                 }
2638         }       
2639         inc_unlock();           
2640         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2641 }
2642
2643 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2644                                           GdkDragContext *context,
2645                                           gint            x,
2646                                           gint            y,
2647                                           guint           time,
2648                                           FolderView     *folderview)
2649 {
2650         gint row, column;
2651         FolderItem *item, *src_item;
2652         GtkCTreeNode *node = NULL;
2653         gboolean acceptable = FALSE;
2654         gint height = folderview->ctree->allocation.height;
2655         gint total_height = folderview->ctree->requisition.height;
2656         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2657                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2658         gfloat vpos = pos->value;
2659
2660         if (gtk_clist_get_selection_info
2661                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2662                 if (y > height - 24 && height + vpos < total_height)
2663                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2664
2665                 if (y < 24 && y > 0)
2666                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2667
2668                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2669                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2670                 src_item = folderview->summaryview->folder_item;
2671
2672                 if (item && item->folder && item->path &&
2673                     src_item && src_item != item) {
2674                         switch (item->folder->type) {
2675                         case F_MH:
2676                         case F_MBOX:
2677                         case F_IMAP:
2678                                 acceptable = TRUE;
2679                                 break;
2680                         default:
2681                                 break;
2682                         }
2683                 } else if (item && item->folder && folder_item_get_path(item) &&
2684                            src_item && src_item != item) {
2685                         /* a root folder - acceptable only from folderview */
2686                         if (item->folder->type == F_MH || item->folder->type == F_IMAP)
2687                                 acceptable = TRUE;
2688                 }
2689                         
2690         }
2691
2692         if (acceptable || (src_item && src_item == item)) {
2693                 folderview_recollapse_nodes(folderview, node);
2694                 if (item->collapsed) {
2695                         gtk_ctree_expand(GTK_CTREE(widget), node);
2696                         folderview->nodes_to_recollapse = g_slist_append(
2697                                                                 folderview->nodes_to_recollapse,
2698                                                                 node);
2699                 }
2700         }
2701                 
2702         if (acceptable) {
2703                 gtk_signal_handler_block_by_func
2704                         (GTK_OBJECT(widget),
2705                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2706                 gtk_ctree_select(GTK_CTREE(widget), node);
2707                 gtk_signal_handler_unblock_by_func
2708                         (GTK_OBJECT(widget),
2709                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2710                 gdk_drag_status(context, 
2711                                         (context->actions == GDK_ACTION_COPY ?
2712                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2713         } else {
2714                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2715                 gdk_drag_status(context, 0, time);
2716         }
2717
2718         return acceptable;
2719 }
2720
2721 static void folderview_drag_leave_cb(GtkWidget      *widget,
2722                                      GdkDragContext *context,
2723                                      guint           time,
2724                                      FolderView     *folderview)
2725 {
2726         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2727 }
2728
2729 static void folderview_drag_received_cb(GtkWidget        *widget,
2730                                         GdkDragContext   *drag_context,
2731                                         gint              x,
2732                                         gint              y,
2733                                         GtkSelectionData *data,
2734                                         guint             info,
2735                                         guint             time,
2736                                         FolderView       *folderview)
2737 {
2738         gint row, column;
2739         FolderItem *item, *src_item;
2740         GtkCTreeNode *node;
2741
2742         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2743                 /* comes from summaryview */
2744                 if (gtk_clist_get_selection_info
2745                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2746                         return;
2747
2748                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2749                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2750                 src_item = folderview->summaryview->folder_item;
2751                 
2752                 /* re-check (due to acceptable possibly set for folder moves */
2753                 if (!(item && item->folder && item->path &&
2754                       src_item && src_item != item && 
2755                       (item->folder->type == F_MH || item->folder->type == F_IMAP))) {
2756                         return;
2757                 }
2758                 if (item && src_item) {
2759                         switch (drag_context->action) {
2760                                 case GDK_ACTION_COPY:
2761                                         summary_copy_selected_to(folderview->summaryview, item);
2762                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2763                                         break;
2764                                 case GDK_ACTION_MOVE:
2765                                 case GDK_ACTION_DEFAULT:
2766                                 default:
2767                         if (src_item->folder->type != item->folder->type ||
2768                             (item->folder->type == F_IMAP &&
2769                              src_item->folder != item->folder))
2770                                 summary_copy_selected_to(folderview->summaryview, item);
2771                         else
2772                                 summary_move_selected_to(folderview->summaryview, item);
2773                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2774                         }
2775                 } else
2776                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2777         } else {
2778                 /* comes from folderview */
2779                 char *source;
2780                 
2781                 source = data->data + 17;
2782                 if (gtk_clist_get_selection_info
2783                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2784                     || *source == 0) {
2785                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2786                         return;
2787                 }
2788                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2789                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2790                 src_item = folder_find_item_from_identifier(source);
2791
2792                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2793                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2794                         return;
2795                 }
2796
2797                 folderview_move_to(folderview, src_item, item);
2798                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2799         }
2800         folderview->nodes_to_recollapse = NULL;
2801 }
2802
2803 static gint folderview_clist_compare(GtkCList *clist,
2804                                      gconstpointer ptr1, gconstpointer ptr2)
2805 {
2806         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2807         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2808
2809         if (!item1->name)
2810                 return (item2->name != NULL);
2811         if (!item2->name)
2812                 return -1;
2813
2814         return g_strcasecmp(item1->name, item2->name);
2815 }
2816
2817 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2818 {
2819         const FolderItem *item = a;
2820         const gchar *name = b;
2821
2822         if (!item->path) return -1;
2823         return strcmp2(g_basename(item->path), name);
2824 }
2825
2826 static void folderview_scoring_cb(FolderView *folderview, guint action,
2827                                    GtkWidget *widget)
2828 {
2829         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2830         FolderItem *item;
2831
2832         if (!folderview->selected) return;
2833
2834         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2835         g_return_if_fail(item != NULL);
2836         g_return_if_fail(item->folder != NULL);
2837
2838         prefs_scoring_open(item);
2839 }
2840
2841 static void folderview_processing_cb(FolderView *folderview, guint action,
2842                                      GtkWidget *widget)
2843 {
2844         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2845         FolderItem *item;
2846
2847         if (!folderview->selected) return;
2848
2849         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2850         g_return_if_fail(item != NULL);
2851         g_return_if_fail(item->folder != NULL);
2852
2853         prefs_filtering_open(item, NULL, NULL);
2854 }
2855
2856 void folderview_set_target_folder_color(gint color_op) 
2857 {
2858         gint firstone = 1;
2859         GList *list;
2860         FolderView *folderview;
2861
2862         for (list = folderview_list; list != NULL; list = list->next) {
2863                 folderview = (FolderView *)list->data;
2864                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2865                 if (firstone) {
2866                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2867                                 folderview->color_op;
2868                         firstone = 0;
2869                 }
2870         }
2871 }
2872
2873 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2874 {
2875         folderview_init(folderview);
2876         folderview_set_all();
2877 }
2878
2879 GtkTargetEntry folderview_drag_types[1] =
2880 {
2881         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
2882 };
2883
2884
2885 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2886                                   FolderView       *folderview)
2887 {
2888         GtkTargetList *list;
2889         GdkDragContext *context;
2890
2891         g_return_if_fail(folderview != NULL);
2892         if (folderview->selected == NULL) return;
2893
2894         list = gtk_target_list_new(folderview_drag_types, 1);
2895
2896         folderview->nodes_to_recollapse = NULL; /* in case the last drag has been cancelled */
2897         
2898         context = gtk_drag_begin(widget, list,
2899                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2900         gtk_drag_set_icon_default(context);
2901 }
2902
2903 static void folderview_drag_data_get(GtkWidget        *widget,
2904                                      GdkDragContext   *drag_context,
2905                                      GtkSelectionData *selection_data,
2906                                      guint             info,
2907                                      guint             time,
2908                                      FolderView       *folderview)
2909 {
2910         FolderItem *item;
2911         GList *cur;
2912         gchar *source=NULL;
2913         
2914         for (cur = GTK_CLIST(folderview->ctree)->selection;
2915              cur != NULL; cur = cur->next) {
2916                 item = gtk_ctree_node_get_row_data
2917                         (GTK_CTREE(folderview->ctree), 
2918                          GTK_CTREE_NODE(cur->data));
2919                 if (item) {
2920                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2921                         gtk_selection_data_set(selection_data,
2922                                                selection_data->target, 8,
2923                                                source, strlen(source));
2924                         break;
2925                 } else
2926                         return;
2927         }
2928 }
2929