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