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