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