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