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