0.9.4claws40
[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 (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
1856         gtk_clist_freeze(GTK_CLIST(ctree));
1857
1858         text[COL_FOLDER] = item->name;
1859         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1860                                      FOLDER_SPACING,
1861                                      folderxpm, folderxpmmask,
1862                                      folderopenxpm, folderopenxpmmask,
1863                                      FALSE, FALSE);
1864         gtk_ctree_expand(ctree, parent_node);
1865         gtk_ctree_node_set_row_data(ctree, node, item);
1866         if (normal_style)
1867                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1868         folderview_sort_folders(folderview, folderview->selected, item->folder);
1869
1870         gtk_clist_thaw(GTK_CLIST(ctree));
1871 }
1872
1873 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1874                                      GtkWidget *widget)
1875 {
1876         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1877         FolderItem *item;
1878         FolderItem *new_item;
1879         gchar *new_folder;
1880         gchar *name, *name_;
1881
1882         if (!folderview->selected) return;
1883
1884         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1885         g_return_if_fail(item != NULL);
1886         g_return_if_fail(item->folder != NULL);
1887
1888         new_folder = input_dialog(_("New folder"),
1889                                   _("Input the name of new folder:"),
1890                                   _("NewFolder"));
1891         if (!new_folder) return;
1892
1893         if (FOLDER_TYPE(item->folder) != F_MBOX) {
1894                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1895                         alertpanel_error(_("`%c' can't be included in folder name."),
1896                                          G_DIR_SEPARATOR);
1897                         g_free(new_folder);
1898                         return;
1899                 }
1900         }
1901
1902         name_ = trim_string(new_folder, 32);
1903         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1904         g_free(name_);
1905
1906         /* find whether the directory already exists */
1907         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1908                 alertpanel_error(_("The folder `%s' already exists."), name);
1909                 g_free(new_folder);
1910                 return;
1911         }
1912
1913         new_item = folder_create_folder(item, new_folder);
1914         if (!new_item) {
1915                 alertpanel_error(_("Can't create the folder `%s'."), name);
1916                 g_free(new_folder);
1917                 return;
1918         } 
1919         g_free(new_folder);
1920
1921         folderview_create_folder_node(folderview, new_item);
1922
1923         folder_write_list();
1924 }
1925
1926 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1927                                           GtkWidget *widget)
1928 {
1929         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1930         FolderItem *item;
1931         FolderItem *new_item;
1932         gchar *new_folder;
1933
1934         if (!folderview->selected) return;
1935
1936         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1937         g_return_if_fail(item != NULL);
1938         g_return_if_fail(item->folder != NULL);
1939
1940         new_folder = input_dialog(_("New folder"),
1941                                   _("Input the name of new folder:"),
1942                                   _("NewFolder"));
1943         if (!new_folder) return;
1944
1945         /* find whether the directory already exists */
1946         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1947                 alertpanel_error(_("The folder `%s' already exists."),
1948                                  new_folder);
1949                 g_free(new_folder);
1950                 return;
1951         }
1952
1953         new_item = folder_create_folder(item, new_folder);
1954         g_free(new_folder);
1955         if (!new_item) return;
1956
1957         folderview_create_folder_node(folderview, new_item);
1958
1959         folder_write_list();
1960 }
1961
1962 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1963                                         GtkWidget *widget)
1964 {
1965         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1966         FolderItem *item;
1967         gchar *new_folder;
1968         gchar *name, *name_;
1969         gchar *message;
1970         gchar *old_path;
1971         gchar *old_id;
1972         gchar *new_id;
1973
1974         if (!folderview->selected) return;
1975
1976         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1977         g_return_if_fail(item != NULL);
1978         g_return_if_fail(item->path != NULL);
1979         g_return_if_fail(item->folder != NULL);
1980
1981         name_ = trim_string(item->name, 32);
1982         Xstrdup_a(name, name_, return);
1983         g_free(name_);
1984         message = g_strdup_printf(_("Input new name for `%s':"), name);
1985         new_folder = input_dialog(_("Rename folder"), message,
1986                                   g_basename(item->path));
1987         g_free(message);
1988         if (!new_folder) return;
1989
1990         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1991                 alertpanel_error(_("`%c' can't be included in folder name."),
1992                                  G_DIR_SEPARATOR);
1993                 g_free(new_folder);
1994                 return;
1995         }
1996
1997         if (folderview_find_by_name
1998                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1999                  new_folder)) {
2000                 name = trim_string(new_folder, 32);
2001                 alertpanel_error(_("The folder `%s' already exists."), name);
2002                 g_free(name);
2003                 g_free(new_folder);
2004                 return;
2005         }
2006
2007         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
2008         old_id = folder_item_get_identifier(item);
2009
2010         if (item->folder->klass->rename_folder(item->folder, item, new_folder) < 0) {
2011                 g_free(old_id);
2012                 g_free(new_folder);
2013                 return;
2014         }
2015         g_free(new_folder);
2016
2017         /* if (FOLDER_TYPE(item->folder) == F_MH)
2018                 prefs_filtering_rename_path(old_path, item->path); */
2019         new_id = folder_item_get_identifier(item);
2020         prefs_filtering_rename_path(old_id, new_id);
2021
2022         g_free(old_id);
2023         g_free(new_id);
2024
2025         gtk_clist_freeze(GTK_CLIST(ctree));
2026
2027         folderview_update_node(folderview, folderview->selected);
2028         folderview_sort_folders(folderview,
2029                                 GTK_CTREE_ROW(folderview->selected)->parent,
2030                                 item->folder);
2031         if (folderview->opened == folderview->selected ||
2032             gtk_ctree_is_ancestor(ctree,
2033                                   folderview->selected,
2034                                   folderview->opened)) {
2035                 GtkCTreeNode *node = folderview->opened;
2036                 folderview_unselect(folderview);
2037                 folderview_select_node(folderview, node);
2038         }
2039
2040         gtk_clist_thaw(GTK_CLIST(ctree));
2041
2042         folder_write_list();
2043 }
2044
2045 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
2046                                              guint action,
2047                                              GtkWidget *widget)
2048 {
2049         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2050         FolderItem *item;
2051         gchar *new_folder;
2052         gchar *message;
2053
2054         if (!folderview->selected) return;
2055
2056         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2057         g_return_if_fail(item != NULL);
2058         g_return_if_fail(item->path != NULL);
2059         g_return_if_fail(item->folder != NULL);
2060
2061         message = g_strdup_printf(_("Input new name for `%s':"),
2062                                   g_basename(item->path));
2063         new_folder = input_dialog(_("Rename folder"), message,
2064                                   g_basename(item->path));
2065         g_free(message);
2066         if (!new_folder) return;
2067
2068         if (folderview_find_by_name
2069                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2070                  new_folder)) {
2071                 alertpanel_error(_("The folder `%s' already exists."),
2072                                  new_folder);
2073                 g_free(new_folder);
2074                 return;
2075         }
2076
2077         if (item->folder->klass->rename_folder(item->folder, item, new_folder) < 0) {
2078                 g_free(new_folder);
2079                 return;
2080         }
2081         g_free(new_folder);
2082
2083         gtk_clist_freeze(GTK_CLIST(ctree));
2084
2085         folderview_update_node(folderview, folderview->selected);
2086         folderview_sort_folders(folderview,
2087                                 GTK_CTREE_ROW(folderview->selected)->parent,
2088                                 item->folder);
2089         if (folderview->opened == folderview->selected) {
2090                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2091                         gtk_ctree_expand(ctree, folderview->opened);
2092                 summary_show(folderview->summaryview, item);
2093         }
2094
2095         gtk_clist_thaw(GTK_CLIST(ctree));
2096
2097         folder_write_list();
2098 }
2099
2100 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2101                                         GtkWidget *widget)
2102 {
2103         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2104         FolderItem *item;
2105         gchar *message, *name, *name_;
2106         AlertValue avalue;
2107         gchar *old_path;
2108         gchar *old_id;
2109
2110         if (!folderview->selected) return;
2111
2112         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2113         g_return_if_fail(item != NULL);
2114         g_return_if_fail(item->path != NULL);
2115         g_return_if_fail(item->folder != NULL);
2116
2117         name_ = trim_string(item->name, 32);
2118         Xstrdup_a(name, name_, return);
2119         g_free(name_);
2120         message = g_strdup_printf
2121                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2122                    "Do you really want to delete?"), name);
2123         avalue = alertpanel(_("Delete folder"), message,
2124                             _("Yes"), _("+No"), NULL);
2125         g_free(message);
2126         if (avalue != G_ALERTDEFAULT) return;
2127
2128         Xstrdup_a(old_path, item->path, return);
2129         old_id = folder_item_get_identifier(item);
2130
2131         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
2132                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2133                 if (folderview->opened == folderview->selected)
2134                         summary_show(folderview->summaryview,
2135                                      folderview->summaryview->folder_item);
2136                 g_free(old_id);
2137                 return;
2138         }
2139
2140         /* if (FOLDER_TYPE(item->folder) == F_MH)
2141                 prefs_filtering_delete_path(old_path); */
2142
2143         if (folderview->opened == folderview->selected ||
2144             gtk_ctree_is_ancestor(ctree,
2145                                   folderview->selected,
2146                                   folderview->opened)) {
2147                 summary_clear_all(folderview->summaryview);
2148                 folderview->opened = NULL;
2149         }
2150
2151         gtk_ctree_remove_node(ctree, folderview->selected);
2152         folder_write_list();
2153
2154         prefs_filtering_delete_path(old_id);
2155         g_free(old_id);
2156
2157 }
2158
2159 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2160                                          GtkWidget *widget)
2161 {
2162         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2163         GtkCTreeNode *node;
2164         FolderItem *item;
2165         gchar *name, *name_;
2166         gchar *message;
2167         AlertValue avalue;
2168
2169         if (!folderview->selected) return;
2170         node = folderview->selected;
2171         item = gtk_ctree_node_get_row_data(ctree, node);
2172         g_return_if_fail(item != NULL);
2173         g_return_if_fail(item->folder != NULL);
2174         if (item->parent) return;
2175
2176         name_ = trim_string(item->folder->name, 32);
2177         Xstrdup_a(name, name_, return);
2178         g_free(name_);
2179         message = g_strdup_printf
2180                 (_("Really remove the mailbox `%s' ?\n"
2181                    "(The messages are NOT deleted from the disk)"), name);
2182         avalue = alertpanel(_("Remove mailbox"), message,
2183                             _("Yes"), _("+No"), NULL);
2184         g_free(message);
2185         if (avalue != G_ALERTDEFAULT) return;
2186
2187         folder_destroy(item->folder);
2188         summary_clear_all(folderview->summaryview);
2189         folderview_unselect(folderview);
2190         gtk_ctree_remove_node(ctree, node);
2191         folder_write_list();
2192 }
2193
2194 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2195                                           GtkWidget *widget)
2196 {
2197         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2198         FolderItem *item;
2199         FolderItem *new_item;
2200         gchar *new_folder;
2201         gchar *name, *name_;
2202         gchar *p;
2203
2204         if (!folderview->selected) return;
2205
2206         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2207         g_return_if_fail(item != NULL);
2208         g_return_if_fail(item->folder != NULL);
2209         g_return_if_fail(FOLDER_TYPE(item->folder) == F_IMAP);
2210         g_return_if_fail(item->folder->account != NULL);
2211
2212         new_folder = input_dialog
2213                 (_("New folder"),
2214                  _("Input the name of new folder:\n"
2215                    "(if you want to create a folder to store subfolders,\n"
2216                    " append `/' at the end of the name)"),
2217                  _("NewFolder"));
2218         if (!new_folder) return;
2219
2220         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2221             *(p + 1) != '\0') {
2222                 alertpanel_error(_("`%c' can't be included in folder name."),
2223                                  G_DIR_SEPARATOR);
2224                 g_free(new_folder);
2225                 return;
2226         }
2227
2228         name_ = trim_string(new_folder, 32);
2229         Xstrdup_a(name, name_, return);
2230         g_free(name_);
2231
2232         /* find whether the directory already exists */
2233         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2234                 alertpanel_error(_("The folder `%s' already exists."), name);
2235                 g_free(new_folder);
2236                 return;
2237         }
2238
2239         new_item = folder_create_folder(item, new_folder);
2240         if (!new_item) {
2241                 alertpanel_error(_("Can't create the folder `%s'."), name);
2242                 g_free(new_folder);
2243                 return;
2244         }
2245         g_free(new_folder);
2246
2247         folderview_create_folder_node(folderview, new_item);
2248
2249         folder_write_list();
2250 }
2251
2252 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2253                                          GtkWidget *widget)
2254 {
2255         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2256         FolderItem *item;
2257         PrefsAccount *account;
2258         gchar *name, *name_;
2259         gchar *message;
2260         AlertValue avalue;
2261
2262         if (!folderview->selected) return;
2263
2264         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2265         g_return_if_fail(item != NULL);
2266         g_return_if_fail(item->folder != NULL);
2267         g_return_if_fail(FOLDER_TYPE(item->folder) == F_IMAP);
2268         g_return_if_fail(item->folder->account != NULL);
2269
2270         name_ = trim_string(item->folder->name, 32);
2271         Xstrdup_a(name, name_, return);
2272         g_free(name_);
2273         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2274         avalue = alertpanel(_("Delete IMAP4 account"), message,
2275                             _("Yes"), _("+No"), NULL);
2276         g_free(message);
2277
2278         if (avalue != G_ALERTDEFAULT) return;
2279
2280         if (folderview->opened == folderview->selected ||
2281             gtk_ctree_is_ancestor(ctree,
2282                                   folderview->selected,
2283                                   folderview->opened)) {
2284                 summary_clear_all(folderview->summaryview);
2285                 folderview->opened = NULL;
2286         }
2287
2288         account = item->folder->account;
2289         folder_destroy(item->folder);
2290         account_destroy(account);
2291         gtk_ctree_remove_node(ctree, folderview->selected);
2292         account_set_menu();
2293         main_window_reflect_prefs_all();
2294         folder_write_list();
2295 }
2296
2297 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2298                                          GtkWidget *widget)
2299 {
2300         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2301         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2302         GtkCTreeNode *servernode, *node;
2303         Folder *folder;
2304         FolderItem *item;
2305         FolderItem *rootitem;
2306         FolderItem *newitem;
2307         GSList *new_subscr;
2308         GSList *cur;
2309         GNode *gnode;
2310
2311         if (!folderview->selected) return;
2312
2313         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2314         g_return_if_fail(item != NULL);
2315         folder = item->folder;
2316         g_return_if_fail(folder != NULL);
2317         g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
2318         g_return_if_fail(folder->account != NULL);
2319
2320         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2321                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2322         else
2323                 servernode = folderview->selected;
2324
2325         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2326
2327         new_subscr = grouplist_dialog(folder);
2328
2329         /* remove unsubscribed newsgroups */
2330         for (gnode = folder->node->children; gnode != NULL; ) {
2331                 GNode *next = gnode->next;
2332
2333                 item = FOLDER_ITEM(gnode->data);
2334                 if (g_slist_find_custom(new_subscr, item->path,
2335                                         (GCompareFunc)g_strcasecmp) != NULL) {
2336                         gnode = next;
2337                         continue;
2338                 }
2339
2340                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2341                 if (!node) {
2342                         gnode = next;
2343                         continue;
2344                 }
2345
2346                 if (folderview->opened == node) {
2347                         summary_clear_all(folderview->summaryview);
2348                         folderview->opened = NULL;
2349                 }
2350
2351                 folder_item_remove(item);
2352                 gtk_ctree_remove_node(ctree, node);
2353
2354                 gnode = next;
2355         }
2356
2357         gtk_clist_freeze(GTK_CLIST(ctree));
2358
2359         /* add subscribed newsgroups */
2360         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2361                 gchar *name = (gchar *)cur->data;
2362
2363                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2364                         continue;
2365
2366                 text[COL_FOLDER] = name;
2367                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2368                                              FOLDER_SPACING,
2369                                              folderxpm, folderxpmmask,
2370                                              folderopenxpm, folderopenxpmmask,
2371                                              FALSE, FALSE);
2372                 gtk_ctree_expand(ctree, servernode);
2373
2374                 newitem = folder_item_new(folder, name, name);
2375                 folder_item_append(rootitem, newitem);
2376                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2377         }
2378
2379         folderview_sort_folders(folderview, servernode, folder);
2380         gtk_clist_thaw(GTK_CLIST(ctree));
2381
2382         slist_free_strings(new_subscr);
2383         g_slist_free(new_subscr);
2384
2385         folder_write_list();
2386 }
2387
2388 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2389                                         GtkWidget *widget)
2390 {
2391         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2392         FolderItem *item;
2393         gchar *name, *name_;
2394         gchar *message;
2395         AlertValue avalue;
2396
2397         if (!folderview->selected) return;
2398
2399         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2400         g_return_if_fail(item != NULL);
2401         g_return_if_fail(item->folder != NULL);
2402         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
2403         g_return_if_fail(item->folder->account != NULL);
2404
2405         name_ = trim_string(item->path, 32);
2406         Xstrdup_a(name, name_, return);
2407         g_free(name_);
2408         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2409         avalue = alertpanel(_("Delete newsgroup"), message,
2410                             _("Yes"), _("+No"), NULL);
2411         g_free(message);
2412         if (avalue != G_ALERTDEFAULT) return;
2413
2414         if (folderview->opened == folderview->selected) {
2415                 summary_clear_all(folderview->summaryview);
2416                 folderview->opened = NULL;
2417         }
2418
2419         folder_item_remove(item);
2420         gtk_ctree_remove_node(ctree, folderview->selected);
2421         folder_write_list();
2422         
2423         prefs_filtering_delete_path(name);
2424 }
2425
2426 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2427                                          GtkWidget *widget)
2428 {
2429         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2430         FolderItem *item;
2431         PrefsAccount *account;
2432         gchar *name, *name_;
2433         gchar *message;
2434         AlertValue avalue;
2435
2436         if (!folderview->selected) return;
2437
2438         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2439         g_return_if_fail(item != NULL);
2440         g_return_if_fail(item->folder != NULL);
2441         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
2442         g_return_if_fail(item->folder->account != NULL);
2443
2444         name_ = trim_string(item->folder->name, 32);
2445         Xstrdup_a(name, name_, return);
2446         g_free(name_);
2447         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2448         avalue = alertpanel(_("Delete news account"), message,
2449                             _("Yes"), _("+No"), NULL);
2450         g_free(message);
2451
2452         if (avalue != G_ALERTDEFAULT) return;
2453
2454         if (folderview->opened == folderview->selected ||
2455             gtk_ctree_is_ancestor(ctree,
2456                                   folderview->selected,
2457                                   folderview->opened)) {
2458                 summary_clear_all(folderview->summaryview);
2459                 folderview->opened = NULL;
2460         }
2461
2462         account = item->folder->account;
2463         folder_destroy(item->folder);
2464         account_destroy(account);
2465         gtk_ctree_remove_node(ctree, folderview->selected);
2466         account_set_menu();
2467         main_window_reflect_prefs_all();
2468         folder_write_list();
2469 }
2470
2471 static void folderview_search_cb(FolderView *folderview, guint action,
2472                                  GtkWidget *widget)
2473 {
2474         summary_search(folderview->summaryview);
2475 }
2476
2477 static void folderview_property_cb(FolderView *folderview, guint action,
2478                                    GtkWidget *widget)
2479 {
2480         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2481         FolderItem *item;
2482
2483         if (!folderview->selected) return;
2484
2485         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2486         g_return_if_fail(item != NULL);
2487         g_return_if_fail(item->folder != NULL);
2488
2489         if (item->parent == NULL && item->folder->account)
2490                 account_open(item->folder->account);
2491         else {
2492                 prefs_folder_item_open(item);
2493         }
2494 }
2495
2496 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2497 {
2498         GSList *list = NULL;
2499         GSList *done = NULL;
2500         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2501         
2502         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2503                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2504                 &&  list->data != node) {
2505                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2506                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2507                 }
2508         }
2509         for (list = done; list != NULL; list = g_slist_next(list)) {
2510                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2511                                                                  list->data);
2512         }
2513         g_slist_free(done);
2514 }
2515
2516 static void folderview_recollapse_all_nodes(FolderView *folderview)
2517 {
2518         GSList *list = folderview->nodes_to_recollapse;
2519         if (!list) return;
2520         for (; list != NULL; list = g_slist_next(list))
2521                 if (list->data) 
2522                         gtk_ctree_collapse(GTK_CTREE(folderview->ctree), 
2523                                            GTK_CTREE_NODE(list->data));
2524         g_slist_free(folderview->nodes_to_recollapse);  
2525         folderview->nodes_to_recollapse = NULL;
2526 }
2527
2528 static void folderview_move_to_cb(FolderView *folderview) 
2529 {
2530         FolderItem *from_folder = NULL, *to_folder = NULL;
2531
2532         if (folderview->selected)
2533                 from_folder = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree), folderview->selected);
2534         if (!from_folder || FOLDER_TYPE(from_folder->folder) == F_NEWS)
2535                 return;
2536
2537         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
2538         
2539         if (!to_folder || FOLDER_TYPE(to_folder->folder) == F_NEWS)
2540                 return;
2541
2542         folderview_move_to(folderview, from_folder, to_folder);
2543 }
2544
2545 static void folderview_move_to(FolderView *folderview, FolderItem *from_folder,
2546                                FolderItem *to_folder)
2547 {
2548         FolderItem *from_parent = NULL;
2549         FolderItem *new_folder = NULL;
2550         GtkCTreeNode *src_node = NULL;
2551         gchar *buf;
2552         gint status;
2553
2554         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2555         from_parent = from_folder->parent;
2556         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2557         STATUSBAR_PUSH(folderview->mainwin, buf);
2558         g_free(buf);
2559         summary_clear_all(folderview->summaryview);
2560         folderview->opened = NULL;
2561         folderview->selected = NULL;
2562         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2563         inc_lock();
2564         main_window_cursor_wait(folderview->mainwin);
2565         statusbar_verbosity_set(TRUE);
2566         folder_item_update_freeze();
2567         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2568                 statusbar_verbosity_set(FALSE);
2569                 main_window_cursor_normal(folderview->mainwin);
2570                 STATUSBAR_POP(folderview->mainwin);
2571                 folder_item_update_thaw();
2572                 if (src_node)
2573                         gtk_ctree_remove_node(GTK_CTREE(folderview->ctree), src_node);
2574                 else 
2575                         debug_print("can't remove src node: is null\n");
2576
2577                 folderview_create_folder_node_recursive(folderview, new_folder);
2578
2579                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2580
2581                 folderview_sort_folders(folderview, 
2582                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2583                                 NULL, new_folder->parent), new_folder->folder);
2584                 folderview_select(folderview, new_folder);
2585         } else {
2586                 statusbar_verbosity_set(FALSE);         
2587                 main_window_cursor_normal(folderview->mainwin);
2588                 STATUSBAR_POP(folderview->mainwin);
2589                 folder_item_update_thaw();
2590                 switch (status) {
2591                 case F_MOVE_FAILED_DEST_IS_PARENT:
2592                         alertpanel_error(_("Source and destination are the same."));
2593                         break;
2594                 case F_MOVE_FAILED_DEST_IS_CHILD:
2595                         alertpanel_error(_("Can't move a folder to one of its children."));
2596                         break;
2597                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2598                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2599                         break;
2600                 default:
2601                         alertpanel_error(_("Move failed!"));
2602                         break;
2603                 }
2604         }       
2605         inc_unlock();           
2606         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2607 }
2608
2609 static gint folderview_clist_compare(GtkCList *clist,
2610                                      gconstpointer ptr1, gconstpointer ptr2)
2611 {
2612         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2613         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2614
2615         if (!item1->name)
2616                 return (item2->name != NULL);
2617         if (!item2->name)
2618                 return -1;
2619
2620         return g_strcasecmp(item1->name, item2->name);
2621 }
2622
2623 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2624 {
2625         const FolderItem *item = a;
2626         const gchar *name = b;
2627
2628         if (!item->path) return -1;
2629         return strcmp2(g_basename(item->path), name);
2630 }
2631
2632 static void folderview_scoring_cb(FolderView *folderview, guint action,
2633                                    GtkWidget *widget)
2634 {
2635         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2636         FolderItem *item;
2637
2638         if (!folderview->selected) return;
2639
2640         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2641         g_return_if_fail(item != NULL);
2642         g_return_if_fail(item->folder != NULL);
2643
2644         prefs_scoring_open(item);
2645 }
2646
2647 static void folderview_processing_cb(FolderView *folderview, guint action,
2648                                      GtkWidget *widget)
2649 {
2650         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2651         FolderItem *item;
2652
2653         if (!folderview->selected) return;
2654
2655         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2656         g_return_if_fail(item != NULL);
2657         g_return_if_fail(item->folder != NULL);
2658
2659         prefs_filtering_open(item, NULL, NULL);
2660 }
2661
2662 void folderview_set_target_folder_color(gint color_op) 
2663 {
2664         gint firstone = 1;
2665         GList *list;
2666         FolderView *folderview;
2667
2668         for (list = folderview_list; list != NULL; list = list->next) {
2669                 folderview = (FolderView *)list->data;
2670                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2671                 if (firstone) {
2672                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2673                                 folderview->color_op;
2674                         firstone = 0;
2675                 }
2676         }
2677 }
2678
2679 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2680 {
2681         folderview_init(folderview);
2682         folderview_set_all();
2683 }
2684
2685 static void drag_state_stop(FolderView *folderview)
2686 {
2687         if (folderview->drag_timer)
2688                 gtk_timeout_remove(folderview->drag_timer);
2689         folderview->drag_timer = 0;
2690         folderview->drag_node = NULL;
2691 }
2692
2693 static gint folderview_defer_expand(FolderView *folderview)
2694 {
2695         if (folderview->drag_node) {
2696                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2697                 if (folderview->drag_item->collapsed) {
2698                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2699                         folderview->nodes_to_recollapse = g_slist_append
2700                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2701                 }
2702         }
2703         folderview->drag_item  = NULL;
2704         folderview->drag_timer = 0;
2705         return FALSE;
2706 }
2707
2708 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2709 {
2710         /* the idea is that we call drag_state_start() whenever we want expansion to
2711          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2712          * we need to call drag_state_stop() */
2713         drag_state_stop(folderview);
2714         /* request expansion */
2715         if (0 != (folderview->drag_timer = gtk_timeout_add
2716                         (prefs_common.hover_timeout, 
2717                          (GtkFunction)folderview_defer_expand,
2718                          folderview))) {
2719                 folderview->drag_node = node;
2720                 folderview->drag_item = item;
2721         }                        
2722 }
2723
2724 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2725                                   FolderView       *folderview)
2726 {
2727         GdkDragContext *context;
2728
2729         g_return_if_fail(folderview != NULL);
2730         if (folderview->selected == NULL) return;
2731         if (folderview->nodes_to_recollapse) 
2732                 g_slist_free(folderview->nodes_to_recollapse);
2733         folderview->nodes_to_recollapse = NULL;
2734         context = gtk_drag_begin(widget, folderview->target_list,
2735                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2736         gtk_drag_set_icon_default(context);
2737 }
2738
2739 static void folderview_drag_data_get(GtkWidget        *widget,
2740                                      GdkDragContext   *drag_context,
2741                                      GtkSelectionData *selection_data,
2742                                      guint             info,
2743                                      guint             time,
2744                                      FolderView       *folderview)
2745 {
2746         FolderItem *item;
2747         GList *cur;
2748         gchar *source=NULL;
2749         
2750         for (cur = GTK_CLIST(folderview->ctree)->selection;
2751              cur != NULL; cur = cur->next) {
2752                 item = gtk_ctree_node_get_row_data
2753                         (GTK_CTREE(folderview->ctree), 
2754                          GTK_CTREE_NODE(cur->data));
2755                 if (item) {
2756                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2757                         gtk_selection_data_set(selection_data,
2758                                                selection_data->target, 8,
2759                                                source, strlen(source));
2760                         break;
2761                 } else
2762                         return;
2763         }
2764 }
2765
2766 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2767 {
2768         FolderUpdateData *hookdata;
2769         FolderView *folderview;
2770         GtkWidget *ctree;
2771
2772         hookdata = source;
2773         folderview = (FolderView *) userdata;   
2774         g_return_val_if_fail(hookdata != NULL, FALSE);
2775         g_return_val_if_fail(folderview != NULL, FALSE);
2776
2777         ctree = folderview->ctree;
2778         g_return_val_if_fail(ctree != NULL, FALSE);
2779
2780         if (hookdata->update_flags & FOLDER_TREE_CHANGED) {
2781                 folderview_set(folderview);
2782         }
2783
2784         return FALSE;
2785 }
2786
2787 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2788                                           GdkDragContext *context,
2789                                           gint            x,
2790                                           gint            y,
2791                                           guint           time,
2792                                           FolderView     *folderview)
2793 {
2794         gint row, column;
2795         FolderItem *item, *src_item = NULL;
2796         GtkCTreeNode *node = NULL;
2797         gboolean acceptable = FALSE;
2798         gint height = folderview->ctree->allocation.height;
2799         gint total_height = folderview->ctree->requisition.height;
2800         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2801                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2802         gfloat vpos = pos->value;
2803
2804         if (gtk_clist_get_selection_info
2805                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2806                 if (y > height - 24 && height + vpos < total_height)
2807                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2808
2809                 if (y < 24 && y > 0)
2810                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2811
2812                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2813                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2814                 src_item = folderview->summaryview->folder_item;
2815
2816                 if (item && item->folder && item->path &&
2817                     src_item && src_item != item) {
2818                         switch (FOLDER_TYPE(item->folder)) {
2819                         case F_MH:
2820                         case F_MBOX:
2821                         case F_IMAP:
2822                                 acceptable = TRUE;
2823                                 break;
2824                         default:
2825                                 break;
2826                         }
2827                 } else if (item && item->folder && folder_item_get_path(item) &&
2828                            src_item && src_item != item) {
2829                         /* a root folder - acceptable only from folderview */
2830                         if (FOLDER_TYPE(item->folder) == F_MH || FOLDER_TYPE(item->folder) == F_IMAP)
2831                                 acceptable = TRUE;
2832                 }
2833                         
2834         }
2835
2836         if (acceptable || (src_item && src_item == item))
2837                 drag_state_start(folderview, node, item);
2838         
2839         if (acceptable) {
2840                 gtk_signal_handler_block_by_func
2841                         (GTK_OBJECT(widget),
2842                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2843                 gtk_ctree_select(GTK_CTREE(widget), node);
2844                 gtk_signal_handler_unblock_by_func
2845                         (GTK_OBJECT(widget),
2846                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2847                 gdk_drag_status(context, 
2848                                         (context->actions == GDK_ACTION_COPY ?
2849                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2850         } else {
2851                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2852                 gdk_drag_status(context, 0, time);
2853         }
2854
2855         return acceptable;
2856 }
2857
2858 static void folderview_drag_leave_cb(GtkWidget      *widget,
2859                                      GdkDragContext *context,
2860                                      guint           time,
2861                                      FolderView     *folderview)
2862 {
2863         drag_state_stop(folderview);
2864         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2865 }
2866
2867 static void folderview_drag_received_cb(GtkWidget        *widget,
2868                                         GdkDragContext   *drag_context,
2869                                         gint              x,
2870                                         gint              y,
2871                                         GtkSelectionData *data,
2872                                         guint             info,
2873                                         guint             time,
2874                                         FolderView       *folderview)
2875 {
2876         gint row, column;
2877         FolderItem *item, *src_item;
2878         GtkCTreeNode *node;
2879
2880         drag_state_stop(folderview);
2881         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2882                 /* comes from summaryview */
2883                 if (gtk_clist_get_selection_info
2884                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2885                         return;
2886
2887                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2888                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2889                 src_item = folderview->summaryview->folder_item;
2890                 
2891                 /* re-check (due to acceptable possibly set for folder moves */
2892                 if (!(item && item->folder && item->path &&
2893                       src_item && src_item != item && 
2894                       (FOLDER_TYPE(item->folder) == F_MH || FOLDER_TYPE(item->folder) == F_IMAP))) {
2895                         return;
2896                 }
2897                 if (item && src_item) {
2898                         switch (drag_context->action) {
2899                                 case GDK_ACTION_COPY:
2900                                         summary_copy_selected_to(folderview->summaryview, item);
2901                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2902                                         break;
2903                                 case GDK_ACTION_MOVE:
2904                                 case GDK_ACTION_DEFAULT:
2905                                 default:
2906                         if (FOLDER_TYPE(src_item->folder) != FOLDER_TYPE(item->folder) ||
2907                             (FOLDER_TYPE(item->folder) == F_IMAP &&
2908                              src_item->folder != item->folder))
2909                                 summary_copy_selected_to(folderview->summaryview, item);
2910                         else
2911                                 summary_move_selected_to(folderview->summaryview, item);
2912                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2913                         }
2914                 } else
2915                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2916         } else {
2917                 /* comes from folderview */
2918                 char *source;
2919                 
2920                 source = data->data + 17;
2921                 if (gtk_clist_get_selection_info
2922                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2923                     || *source == 0) {
2924                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2925                         return;
2926                 }
2927                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2928                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2929                 src_item = folder_find_item_from_identifier(source);
2930
2931                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2932                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2933                         return;
2934                 }
2935
2936                 folderview_move_to(folderview, src_item, item);
2937                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2938         }
2939         folderview->nodes_to_recollapse = NULL;
2940 }
2941
2942 static void folderview_drag_end_cb(GtkWidget        *widget, 
2943                                    GdkDragContext   *drag_context,
2944                                    FolderView       *folderview)
2945 {
2946         drag_state_stop(folderview);
2947         g_slist_free(folderview->nodes_to_recollapse);
2948         folderview->nodes_to_recollapse = NULL;
2949 }
2950