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