0.8.6claws51
[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 void 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         item->need_update = FALSE;
1259
1260         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1261                 folderview_update_node(folderview, node);
1262 }
1263
1264 void folderview_update_item(gpointer source, gpointer data)
1265 {
1266         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1267         FolderView *folderview = (FolderView *)data;
1268         GtkCTree *ctree;
1269         GtkCTreeNode *node;
1270
1271         g_return_if_fail(update_info != NULL);
1272         g_return_if_fail(update_info->item != NULL);
1273         g_return_if_fail(folderview != NULL);
1274
1275         ctree = GTK_CTREE(folderview->ctree);
1276
1277         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1278         if (node) {
1279                 folderview_update_node(folderview, node);
1280                 if (update_info->content_change && folderview->opened == node)
1281                         summary_show(folderview->summaryview, update_info->item);
1282         }
1283 }
1284
1285 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1286                                       GNode *gnode, GtkCTreeNode *cnode,
1287                                       gpointer data)
1288 {
1289         FolderView *folderview = (FolderView *)data;
1290         FolderItem *item = FOLDER_ITEM(gnode->data);
1291
1292         g_return_val_if_fail(item != NULL, FALSE);
1293
1294         gtk_ctree_node_set_row_data(ctree, cnode, item);
1295         folderview_update_node(folderview, cnode);
1296
1297         return TRUE;
1298 }
1299
1300 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1301                                    gpointer data)
1302 {
1303         FolderView *folderview = (FolderView *)data;
1304         FolderItem *item;
1305
1306         if (GTK_CTREE_ROW(node)->children) {
1307                 item = gtk_ctree_node_get_row_data(ctree, node);
1308                 g_return_if_fail(item != NULL);
1309
1310                 if (!item->collapsed)
1311                         gtk_ctree_expand(ctree, node);
1312                 else
1313                         folderview_update_node(folderview, node);
1314         }
1315 }
1316
1317 #define SET_SPECIAL_FOLDER(ctree, item) \
1318 { \
1319         if (item) { \
1320                 GtkCTreeNode *node, *parent, *sibling; \
1321  \
1322                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
1323                 if (!node) \
1324                         g_warning("%s not found.\n", item->path); \
1325                 else { \
1326                         parent = GTK_CTREE_ROW(node)->parent; \
1327                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
1328                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
1329                         else \
1330                                 sibling = GTK_CTREE_ROW(parent)->children; \
1331                         if (node != sibling) \
1332                                 gtk_ctree_move(ctree, node, parent, sibling); \
1333                 } \
1334  \
1335                 prev = node; \
1336         } \
1337 }
1338
1339 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1340                                     Folder *folder)
1341 {
1342         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1343         GtkCTreeNode *prev = NULL;
1344
1345         gtk_sctree_sort_recursive(ctree, root);
1346
1347         if (GTK_CTREE_ROW(root)->parent) return;
1348
1349         SET_SPECIAL_FOLDER(ctree, folder->inbox);
1350         SET_SPECIAL_FOLDER(ctree, folder->outbox);
1351         SET_SPECIAL_FOLDER(ctree, folder->draft);
1352         SET_SPECIAL_FOLDER(ctree, folder->queue);
1353         SET_SPECIAL_FOLDER(ctree, folder->trash);
1354 }
1355
1356 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1357 {
1358         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1359         GtkCTreeNode *root;
1360
1361         g_return_if_fail(folder != NULL);
1362
1363         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1364                                       folderview_gnode_func, folderview);
1365         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1366                                 folderview);
1367         folderview_sort_folders(folderview, root, folder);
1368 }
1369
1370 void folderview_new_folder(FolderView *folderview)
1371 {
1372         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1373         FolderItem *item;
1374
1375         if (!folderview->selected) return;
1376
1377         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1378         g_return_if_fail(item != NULL);
1379         g_return_if_fail(item->folder != NULL);
1380
1381         switch (item->folder->type) {
1382         case F_MBOX:
1383                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1384                 break;
1385         case F_MH:
1386         case F_MAILDIR:
1387                 folderview_new_folder_cb(folderview, 0, NULL);
1388                 break;
1389         case F_IMAP:
1390                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1391                 break;
1392         case F_NEWS:
1393         default:
1394                 break;
1395         }
1396 }
1397
1398 void folderview_rename_folder(FolderView *folderview)
1399 {
1400         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1401         FolderItem *item;
1402
1403         if (!folderview->selected) return;
1404
1405         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1406         g_return_if_fail(item != NULL);
1407         g_return_if_fail(item->folder != NULL);
1408         if (!item->path) return;
1409         if (item->stype != F_NORMAL) return;
1410
1411         switch (item->folder->type) {
1412         case F_MBOX:
1413                 folderview_rename_mbox_folder_cb(folderview, 0, NULL);
1414         case F_MH:
1415         case F_MAILDIR:
1416         case F_IMAP:
1417                 folderview_rename_folder_cb(folderview, 0, NULL);
1418                 break;
1419         case F_NEWS:
1420         default:
1421                 break;
1422         }
1423 }
1424
1425 void folderview_delete_folder(FolderView *folderview)
1426 {
1427         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1428         FolderItem *item;
1429
1430         if (!folderview->selected) return;
1431
1432         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1433         g_return_if_fail(item != NULL);
1434         g_return_if_fail(item->folder != NULL);
1435         if (!item->path) return;
1436         if (item->stype != F_NORMAL) return;
1437
1438         switch (item->folder->type) {
1439         case F_MH:
1440         case F_MBOX:
1441         case F_MAILDIR:
1442         case F_IMAP:
1443                 folderview_delete_folder_cb(folderview, 0, NULL);
1444                 break;
1445         case F_NEWS:
1446         default:
1447                 break;
1448         }
1449 }
1450
1451
1452 /* callback functions */
1453
1454 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1455                                       FolderView *folderview)
1456 {
1457         GtkCList *clist = GTK_CLIST(ctree);
1458         gint prev_row = -1, row = -1, column = -1;
1459         FolderItem *item;
1460         Folder *folder;
1461         GtkWidget *popup;
1462         gboolean mark_all_read   = FALSE;
1463         gboolean new_folder      = FALSE;
1464         gboolean rename_folder   = FALSE;
1465         gboolean move_folder     = FALSE;
1466         gboolean delete_folder   = FALSE;
1467         gboolean update_tree     = FALSE;
1468         gboolean rescan_tree     = FALSE;
1469         gboolean remove_tree     = FALSE;
1470         gboolean search_folder   = FALSE;
1471         gboolean folder_property = FALSE;
1472         gboolean folder_processing  = FALSE;
1473         gboolean folder_scoring  = FALSE;
1474
1475         if (!event) return;
1476
1477         if (event->button == 1) {
1478                 folderview->open_folder = TRUE;
1479                 return;
1480         }
1481
1482         if (event->button == 2 || event->button == 3) {
1483                 /* right clicked */
1484                 if (clist->selection) {
1485                         GtkCTreeNode *node;
1486
1487                         node = GTK_CTREE_NODE(clist->selection->data);
1488                         if (node)
1489                                 prev_row = gtkut_ctree_get_nth_from_node
1490                                         (GTK_CTREE(ctree), node);
1491                 }
1492
1493                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1494                                                   &row, &column))
1495                         return;
1496                 if (prev_row != row) {
1497                         gtk_clist_unselect_all(clist);
1498                         if (event->button == 2)
1499                                 folderview_select_node
1500                                         (folderview,
1501                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1502                                                             row));
1503                         else
1504                                 gtk_clist_select_row(clist, row, column);
1505                 }
1506         }
1507
1508         if (event->button != 3) return;
1509
1510         item = gtk_clist_get_row_data(clist, row);
1511         g_return_if_fail(item != NULL);
1512         g_return_if_fail(item->folder != NULL);
1513         folder = item->folder;
1514
1515         if (folderview->mainwin->lock_count == 0) {
1516                 new_folder = TRUE;
1517                 if (item->parent == NULL) {
1518                         update_tree = remove_tree = TRUE;
1519                         if (folder->account)
1520                                 folder_property = TRUE;
1521                 } else
1522                         mark_all_read = search_folder = folder_property = TRUE;
1523                         
1524                 if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
1525                         if (item->parent == NULL)
1526                                 update_tree = rescan_tree = TRUE;
1527                         else if (item->stype == F_NORMAL)
1528                                 move_folder = rename_folder = delete_folder = folder_scoring = folder_processing = TRUE;
1529                         else if (item->stype == F_INBOX)
1530                                 folder_scoring = folder_processing = TRUE;
1531                         else if (item->stype == F_TRASH)
1532                                 folder_processing = TRUE;
1533                         else if (item->stype == F_OUTBOX)
1534                                 folder_processing = TRUE;
1535                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
1536                         if (item->parent != NULL)
1537                                 delete_folder = folder_scoring = folder_processing = TRUE;
1538                 }
1539                 if (item->unread < 1) 
1540                         mark_all_read = FALSE;
1541         }
1542
1543 #define SET_SENS(factory, name, sens) \
1544         menu_set_sensitive(folderview->factory, name, sens)
1545         
1546         mark_all_read = mark_all_read && 
1547                         (item == folderview->summaryview->folder_item);
1548
1549         if (FOLDER_IS_LOCAL(folder)) {
1550                 popup = folderview->mail_popup;
1551                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1552                 SET_SENS(mail_factory, "/Mark all read", mark_all_read);
1553                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1554                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1555                 SET_SENS(mail_factory, "/Move folder...", move_folder);
1556                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1557                 SET_SENS(mail_factory, "/Check for new messages", update_tree);
1558                 SET_SENS(mail_factory, "/Rebuild folder tree", rescan_tree);
1559                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1560                 SET_SENS(mail_factory, "/Search folder...", search_folder);
1561                 SET_SENS(mail_factory, "/Properties...", folder_property);
1562                 SET_SENS(mail_factory, "/Processing...", folder_processing);
1563                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1564         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1565                 popup = folderview->imap_popup;
1566                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1567                 SET_SENS(imap_factory, "/Mark all read", mark_all_read);
1568                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1569                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1570                 SET_SENS(imap_factory, "/Move folder...", move_folder);
1571                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1572                 SET_SENS(imap_factory, "/Check for new messages", update_tree);
1573                 SET_SENS(imap_factory, "/Rebuild folder tree", rescan_tree);
1574                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1575                 SET_SENS(imap_factory, "/Search folder...", search_folder);
1576                 SET_SENS(imap_factory, "/Properties...", folder_property);
1577                 SET_SENS(imap_factory, "/Processing...", folder_processing);
1578                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1579         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1580                 popup = folderview->news_popup;
1581                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1582                 SET_SENS(news_factory, "/Mark all read", mark_all_read);
1583                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1584                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1585                 SET_SENS(news_factory, "/Check for new messages", update_tree);
1586                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1587                 SET_SENS(news_factory, "/Search folder...", search_folder);
1588                 SET_SENS(news_factory, "/Properties...", folder_property);
1589                 SET_SENS(news_factory, "/Processing...", folder_processing);
1590                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1591         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1592                 popup = folderview->mbox_popup;
1593                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1594                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1595                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1596                 SET_SENS(mbox_factory, "/Move folder...", move_folder);
1597                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1598                 SET_SENS(news_factory, "/Properties...", folder_property);
1599                 SET_SENS(mbox_factory, "/Processing...", folder_processing);
1600                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1601         } else
1602                 return;
1603
1604 #undef SET_SENS
1605
1606         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1607                        event->button, event->time);
1608 }
1609
1610 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1611                                        FolderView *folderview)
1612 {
1613         if (!event) return;
1614
1615         if (event->button == 1 && folderview->open_folder == FALSE &&
1616             folderview->opened != NULL) {
1617                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1618                                           folderview->opened);
1619                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1620         }
1621 }
1622
1623 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1624                                    FolderView *folderview)
1625 {
1626         if (!event) return;
1627
1628         switch (event->keyval) {
1629         case GDK_Return:
1630                 if (folderview->selected) {
1631                         folderview_select_node(folderview,
1632                                                folderview->selected);
1633                 }
1634                 break;
1635         case GDK_space:
1636                 if (folderview->selected) {
1637                         if (folderview->opened == folderview->selected &&
1638                             folderview->summaryview->messages == 0)
1639                                 folderview_select_next_unread(folderview);
1640                         else
1641                                 folderview_select_node(folderview,
1642                                                        folderview->selected);
1643                 }
1644                 break;
1645         default:
1646                 break;
1647         }
1648 }
1649
1650 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1651                                 gint column, FolderView *folderview)
1652 {
1653         static gboolean can_select = TRUE;      /* exclusive lock */
1654         gboolean opened;
1655         FolderItem *item;
1656         gchar *buf;
1657
1658         folderview->selected = row;
1659
1660         if (folderview->opened == row) {
1661                 folderview->open_folder = FALSE;
1662                 return;
1663         }
1664
1665         if (!can_select || summary_is_locked(folderview->summaryview)) {
1666                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1667                 gtk_ctree_select(ctree, folderview->opened);
1668                 return;
1669         }
1670
1671         if (!folderview->open_folder) return;
1672
1673         item = gtk_ctree_node_get_row_data(ctree, row);
1674         if (!item) return;
1675
1676         can_select = FALSE;
1677
1678         /* Save cache for old folder */
1679         /* We don't want to lose all caches if sylpheed crashed */
1680         if (folderview->opened) {
1681                 FolderItem *olditem;
1682                 
1683                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1684                 if (olditem) {
1685                         /* will be null if we just moved the previously opened folder */
1686                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1687                         folder_item_close(olditem);
1688                 }
1689         }
1690
1691         /* CLAWS: set compose button type: news folder items 
1692          * always have a news folder as parent */
1693         if (item->folder) 
1694                 toolbar_set_compose_button
1695                         (folderview->mainwin->toolbar,
1696                          item->folder->type == F_NEWS ? 
1697                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1698
1699         if (item->path)
1700                 debug_print("Folder %s is selected\n", item->path);
1701
1702         if (!GTK_CTREE_ROW(row)->children)
1703                 gtk_ctree_expand(ctree, row);
1704         if (folderview->opened &&
1705             !GTK_CTREE_ROW(folderview->opened)->children)
1706                 gtk_ctree_collapse(ctree, folderview->opened);
1707
1708         /* ungrab the mouse event */
1709         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1710                 gtk_grab_remove(GTK_WIDGET(ctree));
1711                 if (gdk_pointer_is_grabbed())
1712                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1713         }
1714
1715         /* Open Folder */
1716         buf = g_strdup_printf(_("Opening Folder %s..."), item->path);
1717         debug_print("%s\n", buf);
1718         STATUSBAR_PUSH(folderview->mainwin, buf);
1719         g_free(buf);
1720
1721         main_window_cursor_wait(folderview->mainwin);
1722
1723         if (folder_item_open(item) != 0) {
1724                 main_window_cursor_normal(folderview->mainwin);
1725                 STATUSBAR_POP(folderview->mainwin);
1726
1727                 alertpanel_error(_("Folder could not be opened."));
1728
1729                 return;
1730         }
1731
1732         main_window_cursor_normal(folderview->mainwin);
1733
1734         /* Show messages */
1735         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1736         opened = summary_show(folderview->summaryview, item);
1737
1738         folder_clean_cache_memory();
1739
1740         if (!opened) {
1741                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1742                 gtk_ctree_select(ctree, folderview->opened);
1743         } else {
1744                 folderview->opened = row;
1745                 if (gtk_ctree_node_is_visible(ctree, row)
1746                     != GTK_VISIBILITY_FULL)
1747                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1748         }
1749
1750         STATUSBAR_POP(folderview->mainwin);
1751
1752         folderview->open_folder = FALSE;
1753         can_select = TRUE;
1754 }
1755
1756 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1757                                      FolderView *folderview)
1758 {
1759         FolderItem *item;
1760
1761         item = gtk_ctree_node_get_row_data(ctree, node);
1762         g_return_if_fail(item != NULL);
1763         item->collapsed = FALSE;
1764         folderview_update_node(folderview, node);
1765 }
1766
1767 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1768                                       FolderView *folderview)
1769 {
1770         FolderItem *item;
1771
1772         item = gtk_ctree_node_get_row_data(ctree, node);
1773         g_return_if_fail(item != NULL);
1774         item->collapsed= TRUE;
1775         folderview_update_node(folderview, node);
1776 }
1777
1778 static void folderview_popup_close(GtkMenuShell *menu_shell,
1779                                    FolderView *folderview)
1780 {
1781         if (!folderview->opened) return;
1782
1783         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1784                                   folderview->opened);
1785         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1786 }
1787
1788 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1789                                    FolderView *folderview)
1790 {
1791         switch (column) {
1792         case COL_FOLDER:
1793                 prefs_common.folder_col_folder = width;
1794                 break;
1795         case COL_NEW:
1796                 prefs_common.folder_col_new = width;
1797                 break;
1798         case COL_UNREAD:
1799                 prefs_common.folder_col_unread = width;
1800                 break;
1801         case COL_TOTAL:
1802                 prefs_common.folder_col_total = width;
1803                 break;
1804         default:
1805                 break;
1806         }
1807 }
1808
1809 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1810                                              GtkCTreeNode *node,
1811                                              const gchar *name)
1812 {
1813         FolderItem *item;
1814
1815         if (!node)
1816                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1817         if (!node)
1818                 return NULL;
1819
1820         node = GTK_CTREE_ROW(node)->children;
1821
1822         while (node) {
1823                 item = gtk_ctree_node_get_row_data(ctree, node);
1824                 if (!folderview_compare_name(item, name))
1825                         return node;
1826                 node = GTK_CTREE_ROW(node)->sibling;
1827         }
1828
1829         return NULL;
1830 }
1831
1832 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1833                                       GtkWidget *widget)
1834 {
1835         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1836         FolderItem *item;
1837
1838         if (!folderview->selected) return;
1839
1840         summary_show(folderview->summaryview, NULL);
1841
1842         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1843         g_return_if_fail(item != NULL);
1844         g_return_if_fail(item->folder != NULL);
1845
1846         if (action == 0)
1847                 folderview_check_new(item->folder);
1848         else
1849                 folderview_rescan_tree(item->folder);
1850         
1851         if (folderview->opened) {
1852                 item = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1853                 if (item)
1854                         folder_update_item(item, TRUE);
1855         }
1856 }
1857
1858 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1859 {
1860         GNode *srcnode;
1861
1862         folderview_create_folder_node(folderview, item);
1863         
1864         srcnode = item->folder->node;   
1865         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1866         srcnode = srcnode->children;
1867         while (srcnode != NULL) {
1868                 if (srcnode && srcnode->data) {
1869                         FolderItem *next_item = (FolderItem*) srcnode->data;
1870                         folderview_create_folder_node_recursive(folderview, next_item);
1871                 }
1872                 srcnode = srcnode->next;
1873         }
1874 }
1875
1876 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1877 {
1878         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1879         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1880         GtkCTreeNode *node, *parent_node;
1881         
1882         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, item->parent);
1883
1884         gtk_clist_freeze(GTK_CLIST(ctree));
1885
1886         text[COL_FOLDER] = item->name;
1887         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1888                                      FOLDER_SPACING,
1889                                      folderxpm, folderxpmmask,
1890                                      folderopenxpm, folderopenxpmmask,
1891                                      FALSE, FALSE);
1892         gtk_ctree_expand(ctree, parent_node);
1893         gtk_ctree_node_set_row_data(ctree, node, item);
1894         if (normal_style)
1895                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1896         folderview_sort_folders(folderview, folderview->selected, item->folder);
1897
1898         gtk_clist_thaw(GTK_CLIST(ctree));
1899 }
1900
1901 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1902                                      GtkWidget *widget)
1903 {
1904         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1905         FolderItem *item;
1906         FolderItem *new_item;
1907         gchar *new_folder;
1908         gchar *name, *name_;
1909
1910         if (!folderview->selected) return;
1911
1912         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1913         g_return_if_fail(item != NULL);
1914         g_return_if_fail(item->folder != NULL);
1915
1916         new_folder = input_dialog(_("New folder"),
1917                                   _("Input the name of new folder:"),
1918                                   _("NewFolder"));
1919         if (!new_folder) return;
1920
1921         if (item->folder->type != F_MBOX) {
1922                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1923                         alertpanel_error(_("`%c' can't be included in folder name."),
1924                                          G_DIR_SEPARATOR);
1925                         g_free(new_folder);
1926                         return;
1927                 }
1928         }
1929
1930         name_ = trim_string(new_folder, 32);
1931         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1932         g_free(name_);
1933
1934         /* find whether the directory already exists */
1935         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1936                 alertpanel_error(_("The folder `%s' already exists."), name);
1937                 g_free(new_folder);
1938                 return;
1939         }
1940
1941         new_item = folder_create_folder(item, new_folder);
1942         if (!new_item) {
1943                 alertpanel_error(_("Can't create the folder `%s'."), name);
1944                 g_free(new_folder);
1945                 return;
1946         } 
1947         g_free(new_folder);
1948
1949         folderview_create_folder_node(folderview, new_item);
1950
1951         folder_write_list();
1952 }
1953
1954 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1955                                           GtkWidget *widget)
1956 {
1957         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1958         FolderItem *item;
1959         FolderItem *new_item;
1960         gchar *new_folder;
1961
1962         if (!folderview->selected) return;
1963
1964         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1965         g_return_if_fail(item != NULL);
1966         g_return_if_fail(item->folder != NULL);
1967
1968         new_folder = input_dialog(_("New folder"),
1969                                   _("Input the name of new folder:"),
1970                                   _("NewFolder"));
1971         if (!new_folder) return;
1972
1973         /* find whether the directory already exists */
1974         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1975                 alertpanel_error(_("The folder `%s' already exists."),
1976                                  new_folder);
1977                 g_free(new_folder);
1978                 return;
1979         }
1980
1981         new_item = folder_create_folder(item, new_folder);
1982         g_free(new_folder);
1983         if (!new_item) return;
1984
1985         folderview_create_folder_node(folderview, new_item);
1986
1987         folder_write_list();
1988 }
1989
1990 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1991                                         GtkWidget *widget)
1992 {
1993         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1994         FolderItem *item;
1995         gchar *new_folder;
1996         gchar *name, *name_;
1997         gchar *message;
1998         gchar *old_path;
1999         gchar *old_id;
2000         gchar *new_id;
2001
2002         if (!folderview->selected) return;
2003
2004         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2005         g_return_if_fail(item != NULL);
2006         g_return_if_fail(item->path != NULL);
2007         g_return_if_fail(item->folder != NULL);
2008
2009         name_ = trim_string(item->name, 32);
2010         Xstrdup_a(name, name_, return);
2011         g_free(name_);
2012         message = g_strdup_printf(_("Input new name for `%s':"), name);
2013         new_folder = input_dialog(_("Rename folder"), message,
2014                                   g_basename(item->path));
2015         g_free(message);
2016         if (!new_folder) return;
2017
2018         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
2019                 alertpanel_error(_("`%c' can't be included in folder name."),
2020                                  G_DIR_SEPARATOR);
2021                 g_free(new_folder);
2022                 return;
2023         }
2024
2025         if (folderview_find_by_name
2026                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2027                  new_folder)) {
2028                 name = trim_string(new_folder, 32);
2029                 alertpanel_error(_("The folder `%s' already exists."), name);
2030                 g_free(name);
2031                 g_free(new_folder);
2032                 return;
2033         }
2034
2035         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
2036         old_id = folder_item_get_identifier(item);
2037
2038         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2039                 g_free(old_id);
2040                 g_free(new_folder);
2041                 return;
2042         }
2043         g_free(new_folder);
2044
2045         /* if (FOLDER_TYPE(item->folder) == F_MH)
2046                 prefs_filtering_rename_path(old_path, item->path); */
2047         new_id = folder_item_get_identifier(item);
2048         prefs_filtering_rename_path(old_id, new_id);
2049
2050         g_free(old_id);
2051         g_free(new_id);
2052
2053         gtk_clist_freeze(GTK_CLIST(ctree));
2054
2055         folderview_update_node(folderview, folderview->selected);
2056         folderview_sort_folders(folderview,
2057                                 GTK_CTREE_ROW(folderview->selected)->parent,
2058                                 item->folder);
2059         if (folderview->opened == folderview->selected ||
2060             gtk_ctree_is_ancestor(ctree,
2061                                   folderview->selected,
2062                                   folderview->opened)) {
2063                 GtkCTreeNode *node = folderview->opened;
2064                 folderview_unselect(folderview);
2065                 folderview_select_node(folderview, node);
2066         }
2067
2068         gtk_clist_thaw(GTK_CLIST(ctree));
2069
2070         folder_write_list();
2071 }
2072
2073 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
2074                                              guint action,
2075                                              GtkWidget *widget)
2076 {
2077         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2078         FolderItem *item;
2079         gchar *new_folder;
2080         gchar *message;
2081
2082         if (!folderview->selected) return;
2083
2084         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2085         g_return_if_fail(item != NULL);
2086         g_return_if_fail(item->path != NULL);
2087         g_return_if_fail(item->folder != NULL);
2088
2089         message = g_strdup_printf(_("Input new name for `%s':"),
2090                                   g_basename(item->path));
2091         new_folder = input_dialog(_("Rename folder"), message,
2092                                   g_basename(item->path));
2093         g_free(message);
2094         if (!new_folder) return;
2095
2096         if (folderview_find_by_name
2097                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2098                  new_folder)) {
2099                 alertpanel_error(_("The folder `%s' already exists."),
2100                                  new_folder);
2101                 g_free(new_folder);
2102                 return;
2103         }
2104
2105         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2106                 g_free(new_folder);
2107                 return;
2108         }
2109         g_free(new_folder);
2110
2111         gtk_clist_freeze(GTK_CLIST(ctree));
2112
2113         folderview_update_node(folderview, folderview->selected);
2114         folderview_sort_folders(folderview,
2115                                 GTK_CTREE_ROW(folderview->selected)->parent,
2116                                 item->folder);
2117         if (folderview->opened == folderview->selected) {
2118                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2119                         gtk_ctree_expand(ctree, folderview->opened);
2120                 summary_show(folderview->summaryview, item);
2121         }
2122
2123         gtk_clist_thaw(GTK_CLIST(ctree));
2124
2125         folder_write_list();
2126 }
2127
2128 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2129                                         GtkWidget *widget)
2130 {
2131         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2132         FolderItem *item;
2133         gchar *message, *name, *name_;
2134         AlertValue avalue;
2135         gchar *old_path;
2136         gchar *old_id;
2137
2138         if (!folderview->selected) return;
2139
2140         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2141         g_return_if_fail(item != NULL);
2142         g_return_if_fail(item->path != NULL);
2143         g_return_if_fail(item->folder != NULL);
2144
2145         name_ = trim_string(item->name, 32);
2146         Xstrdup_a(name, name_, return);
2147         g_free(name_);
2148         message = g_strdup_printf
2149                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2150                    "Do you really want to delete?"), name);
2151         avalue = alertpanel(_("Delete folder"), message,
2152                             _("Yes"), _("+No"), NULL);
2153         g_free(message);
2154         if (avalue != G_ALERTDEFAULT) return;
2155
2156         Xstrdup_a(old_path, item->path, return);
2157         old_id = folder_item_get_identifier(item);
2158
2159         if (item->folder->remove_folder(item->folder, item) < 0) {
2160                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2161                 if (folderview->opened == folderview->selected)
2162                         summary_show(folderview->summaryview,
2163                                      folderview->summaryview->folder_item);
2164                 g_free(old_id);
2165                 return;
2166         }
2167
2168         /* if (FOLDER_TYPE(item->folder) == F_MH)
2169                 prefs_filtering_delete_path(old_path); */
2170
2171         if (folderview->opened == folderview->selected ||
2172             gtk_ctree_is_ancestor(ctree,
2173                                   folderview->selected,
2174                                   folderview->opened)) {
2175                 summary_clear_all(folderview->summaryview);
2176                 folderview->opened = NULL;
2177         }
2178
2179         gtk_ctree_remove_node(ctree, folderview->selected);
2180         folder_write_list();
2181
2182         prefs_filtering_delete_path(old_id);
2183         g_free(old_id);
2184
2185 }
2186
2187 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2188                                          GtkWidget *widget)
2189 {
2190         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2191         GtkCTreeNode *node;
2192         FolderItem *item;
2193         gchar *name, *name_;
2194         gchar *message;
2195         AlertValue avalue;
2196
2197         if (!folderview->selected) return;
2198         node = folderview->selected;
2199         item = gtk_ctree_node_get_row_data(ctree, node);
2200         g_return_if_fail(item != NULL);
2201         g_return_if_fail(item->folder != NULL);
2202         if (item->parent) return;
2203
2204         name_ = trim_string(item->folder->name, 32);
2205         Xstrdup_a(name, name_, return);
2206         g_free(name_);
2207         message = g_strdup_printf
2208                 (_("Really remove the mailbox `%s' ?\n"
2209                    "(The messages are NOT deleted from the disk)"), name);
2210         avalue = alertpanel(_("Remove mailbox"), message,
2211                             _("Yes"), _("+No"), NULL);
2212         g_free(message);
2213         if (avalue != G_ALERTDEFAULT) return;
2214
2215         folder_destroy(item->folder);
2216         summary_clear_all(folderview->summaryview);
2217         folderview_unselect(folderview);
2218         gtk_ctree_remove_node(ctree, node);
2219         folder_write_list();
2220 }
2221
2222 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2223                                           GtkWidget *widget)
2224 {
2225         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2226         FolderItem *item;
2227         FolderItem *new_item;
2228         gchar *new_folder;
2229         gchar *name, *name_;
2230         gchar *p;
2231
2232         if (!folderview->selected) return;
2233
2234         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2235         g_return_if_fail(item != NULL);
2236         g_return_if_fail(item->folder != NULL);
2237         g_return_if_fail(item->folder->type == F_IMAP);
2238         g_return_if_fail(item->folder->account != NULL);
2239
2240         new_folder = input_dialog
2241                 (_("New folder"),
2242                  _("Input the name of new folder:\n"
2243                    "(if you want to create a folder to store subfolders,\n"
2244                    " append `/' at the end of the name)"),
2245                  _("NewFolder"));
2246         if (!new_folder) return;
2247
2248         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2249             *(p + 1) != '\0') {
2250                 alertpanel_error(_("`%c' can't be included in folder name."),
2251                                  G_DIR_SEPARATOR);
2252                 g_free(new_folder);
2253                 return;
2254         }
2255
2256         name_ = trim_string(new_folder, 32);
2257         Xstrdup_a(name, name_, return);
2258         g_free(name_);
2259
2260         /* find whether the directory already exists */
2261         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2262                 alertpanel_error(_("The folder `%s' already exists."), name);
2263                 g_free(new_folder);
2264                 return;
2265         }
2266
2267         new_item = folder_create_folder(item, new_folder);
2268         if (!new_item) {
2269                 alertpanel_error(_("Can't create the folder `%s'."), name);
2270                 g_free(new_folder);
2271                 return;
2272         }
2273         g_free(new_folder);
2274
2275         folderview_create_folder_node(folderview, new_item);
2276
2277         folder_write_list();
2278 }
2279
2280 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2281                                          GtkWidget *widget)
2282 {
2283         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2284         FolderItem *item;
2285         PrefsAccount *account;
2286         gchar *name, *name_;
2287         gchar *message;
2288         AlertValue avalue;
2289
2290         if (!folderview->selected) return;
2291
2292         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2293         g_return_if_fail(item != NULL);
2294         g_return_if_fail(item->folder != NULL);
2295         g_return_if_fail(item->folder->type == F_IMAP);
2296         g_return_if_fail(item->folder->account != NULL);
2297
2298         name_ = trim_string(item->folder->name, 32);
2299         Xstrdup_a(name, name_, return);
2300         g_free(name_);
2301         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2302         avalue = alertpanel(_("Delete IMAP4 account"), message,
2303                             _("Yes"), _("+No"), NULL);
2304         g_free(message);
2305
2306         if (avalue != G_ALERTDEFAULT) return;
2307
2308         if (folderview->opened == folderview->selected ||
2309             gtk_ctree_is_ancestor(ctree,
2310                                   folderview->selected,
2311                                   folderview->opened)) {
2312                 summary_clear_all(folderview->summaryview);
2313                 folderview->opened = NULL;
2314         }
2315
2316         account = item->folder->account;
2317         folder_destroy(item->folder);
2318         account_destroy(account);
2319         gtk_ctree_remove_node(ctree, folderview->selected);
2320         account_set_menu();
2321         main_window_reflect_prefs_all();
2322         folder_write_list();
2323 }
2324
2325 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2326                                          GtkWidget *widget)
2327 {
2328         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2329         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2330         GtkCTreeNode *servernode, *node;
2331         Folder *folder;
2332         FolderItem *item;
2333         FolderItem *rootitem;
2334         FolderItem *newitem;
2335         GSList *new_subscr;
2336         GSList *cur;
2337         GNode *gnode;
2338
2339         if (!folderview->selected) return;
2340
2341         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2342         g_return_if_fail(item != NULL);
2343         folder = item->folder;
2344         g_return_if_fail(folder != NULL);
2345         g_return_if_fail(folder->type == F_NEWS);
2346         g_return_if_fail(folder->account != NULL);
2347
2348         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2349                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2350         else
2351                 servernode = folderview->selected;
2352
2353         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2354
2355         new_subscr = grouplist_dialog(folder);
2356
2357         /* remove unsubscribed newsgroups */
2358         for (gnode = folder->node->children; gnode != NULL; ) {
2359                 GNode *next = gnode->next;
2360
2361                 item = FOLDER_ITEM(gnode->data);
2362                 if (g_slist_find_custom(new_subscr, item->path,
2363                                         (GCompareFunc)g_strcasecmp) != NULL) {
2364                         gnode = next;
2365                         continue;
2366                 }
2367
2368                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2369                 if (!node) {
2370                         gnode = next;
2371                         continue;
2372                 }
2373
2374                 if (folderview->opened == node) {
2375                         summary_clear_all(folderview->summaryview);
2376                         folderview->opened = NULL;
2377                 }
2378
2379                 folder_item_remove(item);
2380                 gtk_ctree_remove_node(ctree, node);
2381
2382                 gnode = next;
2383         }
2384
2385         gtk_clist_freeze(GTK_CLIST(ctree));
2386
2387         /* add subscribed newsgroups */
2388         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2389                 gchar *name = (gchar *)cur->data;
2390
2391                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2392                         continue;
2393
2394                 text[COL_FOLDER] = name;
2395                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2396                                              FOLDER_SPACING,
2397                                              folderxpm, folderxpmmask,
2398                                              folderopenxpm, folderopenxpmmask,
2399                                              FALSE, FALSE);
2400                 gtk_ctree_expand(ctree, servernode);
2401
2402                 newitem = folder_item_new(folder, name, name);
2403                 folder_item_append(rootitem, newitem);
2404                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2405         }
2406
2407         folderview_sort_folders(folderview, servernode, folder);
2408         gtk_clist_thaw(GTK_CLIST(ctree));
2409
2410         slist_free_strings(new_subscr);
2411         g_slist_free(new_subscr);
2412
2413         folder_write_list();
2414 }
2415
2416 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2417                                         GtkWidget *widget)
2418 {
2419         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2420         FolderItem *item;
2421         gchar *name, *name_;
2422         gchar *message;
2423         AlertValue avalue;
2424
2425         if (!folderview->selected) return;
2426
2427         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2428         g_return_if_fail(item != NULL);
2429         g_return_if_fail(item->folder != NULL);
2430         g_return_if_fail(item->folder->type == F_NEWS);
2431         g_return_if_fail(item->folder->account != NULL);
2432
2433         name_ = trim_string(item->path, 32);
2434         Xstrdup_a(name, name_, return);
2435         g_free(name_);
2436         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2437         avalue = alertpanel(_("Delete newsgroup"), message,
2438                             _("Yes"), _("+No"), NULL);
2439         g_free(message);
2440         if (avalue != G_ALERTDEFAULT) return;
2441
2442         if (folderview->opened == folderview->selected) {
2443                 summary_clear_all(folderview->summaryview);
2444                 folderview->opened = NULL;
2445         }
2446
2447         folder_item_remove(item);
2448         gtk_ctree_remove_node(ctree, folderview->selected);
2449         folder_write_list();
2450 }
2451
2452 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2453                                          GtkWidget *widget)
2454 {
2455         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2456         FolderItem *item;
2457         PrefsAccount *account;
2458         gchar *name, *name_;
2459         gchar *message;
2460         AlertValue avalue;
2461
2462         if (!folderview->selected) return;
2463
2464         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2465         g_return_if_fail(item != NULL);
2466         g_return_if_fail(item->folder != NULL);
2467         g_return_if_fail(item->folder->type == F_NEWS);
2468         g_return_if_fail(item->folder->account != NULL);
2469
2470         name_ = trim_string(item->folder->name, 32);
2471         Xstrdup_a(name, name_, return);
2472         g_free(name_);
2473         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2474         avalue = alertpanel(_("Delete news account"), message,
2475                             _("Yes"), _("+No"), NULL);
2476         g_free(message);
2477
2478         if (avalue != G_ALERTDEFAULT) return;
2479
2480         if (folderview->opened == folderview->selected ||
2481             gtk_ctree_is_ancestor(ctree,
2482                                   folderview->selected,
2483                                   folderview->opened)) {
2484                 summary_clear_all(folderview->summaryview);
2485                 folderview->opened = NULL;
2486         }
2487
2488         account = item->folder->account;
2489         folder_destroy(item->folder);
2490         account_destroy(account);
2491         gtk_ctree_remove_node(ctree, folderview->selected);
2492         account_set_menu();
2493         main_window_reflect_prefs_all();
2494         folder_write_list();
2495 }
2496
2497 static void folderview_search_cb(FolderView *folderview, guint action,
2498                                  GtkWidget *widget)
2499 {
2500         summary_search(folderview->summaryview);
2501 }
2502
2503 static void folderview_property_cb(FolderView *folderview, guint action,
2504                                    GtkWidget *widget)
2505 {
2506         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2507         FolderItem *item;
2508
2509         if (!folderview->selected) return;
2510
2511         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2512         g_return_if_fail(item != NULL);
2513         g_return_if_fail(item->folder != NULL);
2514
2515 #if CLAWS
2516         prefs_folder_item_create(folderview, item);
2517 #else
2518         /*
2519          * CLAWS: wait till Hiro has completed his stuff
2520          */
2521         if (item->parent == NULL && item->folder->account)
2522                 account_open(item->folder->account);
2523         else
2524                 prefs_folder_item_open(item);
2525 #endif  
2526 }
2527
2528 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2529 {
2530         GSList *list = NULL;
2531         GSList *done = NULL;
2532         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2533         
2534         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2535                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2536                 &&  list->data != node) {
2537                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2538                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2539                 }
2540         }
2541         for (list = done; list != NULL; list = g_slist_next(list)) {
2542                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2543                                                                  list->data);
2544         }
2545         g_slist_free(done);
2546 }
2547
2548 static void folderview_move_to_cb(FolderView *folderview) 
2549 {
2550         FolderItem *from_folder = NULL, *to_folder = NULL;
2551
2552         if (folderview->selected)
2553                 from_folder = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree), folderview->selected);
2554         if (!from_folder || from_folder->folder->type == F_NEWS)
2555                 return;
2556
2557         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
2558         
2559         if (!to_folder || to_folder->folder->type == F_NEWS)
2560                 return;
2561
2562         folderview_move_to(folderview, from_folder, to_folder);
2563 }
2564
2565 static void folderview_move_to(FolderView *folderview, FolderItem *from_folder,
2566                                FolderItem *to_folder)
2567 {
2568         FolderItem *from_parent = NULL;
2569         FolderItem *new_folder = NULL;
2570         GtkCTreeNode *src_node = NULL;
2571         gchar *buf;
2572         gint status;
2573
2574         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2575         from_parent = from_folder->parent;
2576         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2577         STATUSBAR_PUSH(folderview->mainwin, buf);
2578         g_free(buf);
2579         summary_clear_all(folderview->summaryview);
2580         folderview->opened = NULL;
2581         folderview->selected = NULL;
2582         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2583         inc_lock();
2584         main_window_cursor_wait(folderview->mainwin);
2585         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2586                 main_window_cursor_normal(folderview->mainwin);
2587                 STATUSBAR_POP(folderview->mainwin);
2588                 if (src_node)
2589                         gtk_ctree_remove_node(GTK_CTREE(folderview->ctree), src_node);
2590                 else 
2591                         debug_print("can't remove src node: is null\n");
2592
2593                 folderview_create_folder_node_recursive(folderview, new_folder);
2594                 folder_update_item(from_parent, TRUE);
2595                 folder_update_item_recursive(new_folder, TRUE); 
2596                 folderview_sort_folders(folderview, 
2597                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2598                                 NULL, new_folder->parent), new_folder->folder);
2599                 folderview_select(folderview, new_folder);
2600         } else {
2601                 main_window_cursor_normal(folderview->mainwin);
2602                 STATUSBAR_POP(folderview->mainwin);
2603                 switch (status) {
2604                 case F_MOVE_FAILED_DEST_IS_PARENT:
2605                         alertpanel_error(_("Source and destination are the same."));
2606                         break;
2607                 case F_MOVE_FAILED_DEST_IS_CHILD:
2608                         alertpanel_error(_("Can't move a folder to one of its children."));
2609                         break;
2610                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2611                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2612                         break;
2613                 default:
2614                         alertpanel_error(_("Move failed!"));
2615                         break;
2616                 }
2617         }       
2618         inc_unlock();           
2619         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2620 }
2621
2622 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2623                                           GdkDragContext *context,
2624                                           gint            x,
2625                                           gint            y,
2626                                           guint           time,
2627                                           FolderView     *folderview)
2628 {
2629         gint row, column;
2630         FolderItem *item, *src_item;
2631         GtkCTreeNode *node = NULL;
2632         gboolean acceptable = FALSE;
2633         gint height = folderview->ctree->allocation.height;
2634         gint total_height = folderview->ctree->requisition.height;
2635         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2636                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2637         gfloat vpos = pos->value;
2638
2639         if (gtk_clist_get_selection_info
2640                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2641                 if (y > height - 24 && height + vpos < total_height)
2642                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2643
2644                 if (y < 24 && y > 0)
2645                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2646
2647                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2648                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2649                 src_item = folderview->summaryview->folder_item;
2650
2651                 if (item && item->folder && item->path &&
2652                     src_item && src_item != item) {
2653                         switch (item->folder->type) {
2654                         case F_MH:
2655                         case F_MBOX:
2656                         case F_IMAP:
2657                                 acceptable = TRUE;
2658                                 break;
2659                         default:
2660                                 break;
2661                         }
2662                 } else if (item && item->folder && folder_item_get_path(item) &&
2663                            src_item && src_item != item) {
2664                         /* a root folder - acceptable only from folderview */
2665                         if (item->folder->type == F_MH || item->folder->type == F_IMAP)
2666                                 acceptable = TRUE;
2667                 }
2668                         
2669         }
2670
2671         if (acceptable || (src_item && src_item == item)) {
2672                 folderview_recollapse_nodes(folderview, node);
2673                 if (item->collapsed) {
2674                         gtk_ctree_expand(GTK_CTREE(widget), node);
2675                         folderview->nodes_to_recollapse = g_slist_append(
2676                                                                 folderview->nodes_to_recollapse,
2677                                                                 node);
2678                 }
2679         }
2680                 
2681         if (acceptable) {
2682                 gtk_signal_handler_block_by_func
2683                         (GTK_OBJECT(widget),
2684                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2685                 gtk_ctree_select(GTK_CTREE(widget), node);
2686                 gtk_signal_handler_unblock_by_func
2687                         (GTK_OBJECT(widget),
2688                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2689                 gdk_drag_status(context, 
2690                                         (context->actions == GDK_ACTION_COPY ?
2691                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2692         } else {
2693                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2694                 gdk_drag_status(context, 0, time);
2695         }
2696
2697         return acceptable;
2698 }
2699
2700 static void folderview_drag_leave_cb(GtkWidget      *widget,
2701                                      GdkDragContext *context,
2702                                      guint           time,
2703                                      FolderView     *folderview)
2704 {
2705         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2706 }
2707
2708 static void folderview_drag_received_cb(GtkWidget        *widget,
2709                                         GdkDragContext   *drag_context,
2710                                         gint              x,
2711                                         gint              y,
2712                                         GtkSelectionData *data,
2713                                         guint             info,
2714                                         guint             time,
2715                                         FolderView       *folderview)
2716 {
2717         gint row, column;
2718         FolderItem *item, *src_item;
2719         GtkCTreeNode *node;
2720
2721         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2722                 /* comes from summaryview */
2723                 if (gtk_clist_get_selection_info
2724                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2725                         return;
2726
2727                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2728                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2729                 src_item = folderview->summaryview->folder_item;
2730                 
2731                 /* re-check (due to acceptable possibly set for folder moves */
2732                 if (!(item && item->folder && item->path &&
2733                       src_item && src_item != item && 
2734                       (item->folder->type == F_MH || item->folder->type == F_IMAP))) {
2735                         return;
2736                 }
2737                 if (item && src_item) {
2738                         switch (drag_context->action) {
2739                                 case GDK_ACTION_COPY:
2740                                         summary_copy_selected_to(folderview->summaryview, item);
2741                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2742                                         break;
2743                                 case GDK_ACTION_MOVE:
2744                                 case GDK_ACTION_DEFAULT:
2745                                 default:
2746                         if (src_item->folder->type != item->folder->type ||
2747                             (item->folder->type == F_IMAP &&
2748                              src_item->folder != item->folder))
2749                                 summary_copy_selected_to(folderview->summaryview, item);
2750                         else
2751                                 summary_move_selected_to(folderview->summaryview, item);
2752                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2753                         }
2754                 } else
2755                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2756         } else {
2757                 /* comes from folderview */
2758                 char *source;
2759                 
2760                 source = data->data + 17;
2761                 if (gtk_clist_get_selection_info
2762                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2763                     || *source == 0) {
2764                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2765                         return;
2766                 }
2767                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2768                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2769                 src_item = folder_find_item_from_identifier(source);
2770
2771                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2772                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2773                         return;
2774                 }
2775
2776                 folderview_move_to(folderview, src_item, item);
2777                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2778         }
2779         folderview->nodes_to_recollapse = NULL;
2780 }
2781
2782 static gint folderview_clist_compare(GtkCList *clist,
2783                                      gconstpointer ptr1, gconstpointer ptr2)
2784 {
2785         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2786         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2787
2788         if (!item1->name)
2789                 return (item2->name != NULL);
2790         if (!item2->name)
2791                 return -1;
2792
2793         return g_strcasecmp(item1->name, item2->name);
2794 }
2795
2796 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2797 {
2798         const FolderItem *item = a;
2799         const gchar *name = b;
2800
2801         if (!item->path) return -1;
2802         return strcmp2(g_basename(item->path), name);
2803 }
2804
2805 static void folderview_scoring_cb(FolderView *folderview, guint action,
2806                                    GtkWidget *widget)
2807 {
2808         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2809         FolderItem *item;
2810
2811         if (!folderview->selected) return;
2812
2813         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2814         g_return_if_fail(item != NULL);
2815         g_return_if_fail(item->folder != NULL);
2816
2817         prefs_scoring_open(item);
2818 }
2819
2820 static void folderview_processing_cb(FolderView *folderview, guint action,
2821                                      GtkWidget *widget)
2822 {
2823         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2824         FolderItem *item;
2825
2826         if (!folderview->selected) return;
2827
2828         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2829         g_return_if_fail(item != NULL);
2830         g_return_if_fail(item->folder != NULL);
2831
2832         prefs_filtering_open(item, NULL, NULL);
2833 }
2834
2835 void folderview_set_target_folder_color(gint color_op) 
2836 {
2837         gint firstone = 1;
2838         GList *list;
2839         FolderView *folderview;
2840
2841         for (list = folderview_list; list != NULL; list = list->next) {
2842                 folderview = (FolderView *)list->data;
2843                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2844                 if (firstone) {
2845                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2846                                 folderview->color_op;
2847                         firstone = 0;
2848                 }
2849         }
2850 }
2851
2852 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2853 {
2854         folderview_init(folderview);
2855         folderview_set_all();
2856 }
2857
2858 GtkTargetEntry folderview_drag_types[1] =
2859 {
2860         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
2861 };
2862
2863
2864 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2865                                   FolderView       *folderview)
2866 {
2867         GtkTargetList *list;
2868         GdkDragContext *context;
2869
2870         g_return_if_fail(folderview != NULL);
2871         if (folderview->selected == NULL) return;
2872
2873         list = gtk_target_list_new(folderview_drag_types, 1);
2874
2875         folderview->nodes_to_recollapse = NULL; /* in case the last drag has been cancelled */
2876         
2877         context = gtk_drag_begin(widget, list,
2878                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2879         gtk_drag_set_icon_default(context);
2880 }
2881
2882 static void folderview_drag_data_get(GtkWidget        *widget,
2883                                      GdkDragContext   *drag_context,
2884                                      GtkSelectionData *selection_data,
2885                                      guint             info,
2886                                      guint             time,
2887                                      FolderView       *folderview)
2888 {
2889         FolderItem *item;
2890         GList *cur;
2891         gchar *source=NULL;
2892         
2893         for (cur = GTK_CLIST(folderview->ctree)->selection;
2894              cur != NULL; cur = cur->next) {
2895                 item = gtk_ctree_node_get_row_data
2896                         (GTK_CTREE(folderview->ctree), 
2897                          GTK_CTREE_NODE(cur->data));
2898                 if (item) {
2899                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2900                         gtk_selection_data_set(selection_data,
2901                                                selection_data->target, 8,
2902                                                source, strlen(source));
2903                         break;
2904                 } else
2905                         return;
2906         }
2907 }
2908