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