folderview_check_new(): More precise function description
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkwidget.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkctree.h>
27 #include <gtk/gtkcontainer.h>
28 #include <gtk/gtkclist.h>
29 #include <gtk/gtkstyle.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkstatusbar.h>
33 #include <gtk/gtkmenu.h>
34 #include <gtk/gtkmenuitem.h>
35 #include <gtk/gtkitemfactory.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "summary_search.h"
46 #include "inputdialog.h"
47 #include "grouplistdialog.h"
48 #include "manage_window.h"
49 #include "alertpanel.h"
50 #include "menu.h"
51 #include "stock_pixmap.h"
52 #include "procmsg.h"
53 #include "utils.h"
54 #include "gtkutils.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "prefs_filtering.h"
58 #include "prefs_scoring.h"
59 #include "prefs_folder_item.h"
60 #include "account.h"
61 #include "folder.h"
62 #include "foldersel.h"
63 #include "inc.h"
64 #include "statusbar.h"
65 #include "hooks.h"
66
67 typedef enum
68 {
69         COL_FOLDER      = 0,
70         COL_NEW         = 1,
71         COL_UNREAD      = 2,
72         COL_TOTAL       = 3
73 } FolderColumnPos;
74
75 #define N_FOLDER_COLS           4
76 #define COL_FOLDER_WIDTH        150
77 #define COL_NUM_WIDTH           32
78
79 static GList *folderview_list = NULL;
80
81 static GdkFont *normalfont;
82 static GdkFont *boldfont;
83
84 static GtkStyle *normal_style;
85 static GtkStyle *normal_color_style;
86 static GtkStyle *bold_style;
87 static GtkStyle *bold_color_style;
88 static GtkStyle *bold_tgtfold_style;
89
90 static GdkBitmap *inboxxpm;
91 static GdkBitmap *inboxxpmmask;
92 static GdkPixmap *inboxhrmxpm;
93 static GdkBitmap *inboxhrmxpmmask;
94 static GdkPixmap *inboxopenxpm;
95 static GdkBitmap *inboxopenxpmmask;
96 static GdkPixmap *inboxopenhrmxpm;
97 static GdkBitmap *inboxopenhrmxpmmask;
98 static GdkPixmap *outboxxpm;
99 static GdkBitmap *outboxxpmmask;
100 static GdkPixmap *outboxhrmxpm;
101 static GdkBitmap *outboxhrmxpmmask;
102 static GdkPixmap *outboxopenxpm;
103 static GdkBitmap *outboxopenxpmmask;
104 static GdkPixmap *outboxopenhrmxpm;
105 static GdkBitmap *outboxopenhrmxpmmask;
106 static GdkPixmap *folderxpm;
107 static GdkBitmap *folderxpmmask;
108 static GdkPixmap *folderhrmxpm;
109 static GdkBitmap *folderhrmxpmmask;
110 static GdkPixmap *folderopenxpm;
111 static GdkBitmap *folderopenxpmmask;
112 static GdkPixmap *folderopenhrmxpm;
113 static GdkBitmap *folderopenhrmxpmmask;
114 static GdkPixmap *trashopenxpm;
115 static GdkBitmap *trashopenxpmmask;
116 static GdkPixmap *trashopenhrmxpm;
117 static GdkBitmap *trashopenhrmxpmmask;
118 static GdkPixmap *trashxpm;
119 static GdkBitmap *trashxpmmask;
120 static GdkPixmap *trashhrmxpm;
121 static GdkBitmap *trashhrmxpmmask;
122 static GdkPixmap *queuexpm;
123 static GdkBitmap *queuexpmmask;
124 static GdkPixmap *queuehrmxpm;
125 static GdkBitmap *queuehrmxpmmask;
126 static GdkPixmap *queueopenxpm;
127 static GdkBitmap *queueopenxpmmask;
128 static GdkPixmap *queueopenhrmxpm;
129 static GdkBitmap *queueopenhrmxpmmask;
130 static GdkPixmap *newxpm;
131 static GdkBitmap *newxpmmask;
132 static GdkPixmap *unreadxpm;
133 static GdkBitmap *unreadxpmmask;
134
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 if (folder->type == F_NEWS && folder->account &&
773                  folder->account->nntp_server)
774                 rootpath = folder->account->nntp_server;
775         else
776                 return;
777
778         for (list = folderview_list; list != NULL; list = list->next) {
779                 FolderView *folderview = (FolderView *)list->data;
780                 MainWindow *mainwin = folderview->mainwin;
781                 gchar *str;
782
783                 if (item->path)
784                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
785                                               rootpath, G_DIR_SEPARATOR,
786                                               item->path);
787                 else
788                         str = g_strdup_printf(_("Scanning folder %s ..."),
789                                               rootpath);
790
791                 STATUSBAR_PUSH(mainwin, str);
792                 STATUSBAR_POP(mainwin);
793                 g_free(str);
794         }
795 }
796
797 static GtkWidget *label_window_create(const gchar *str)
798 {
799         GtkWidget *window;
800         GtkWidget *label;
801
802         window = gtk_window_new(GTK_WINDOW_DIALOG);
803         gtk_widget_set_usize(window, 380, 60);
804         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
805         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
806         gtk_window_set_title(GTK_WINDOW(window), str);
807         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
808         gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
809         manage_window_set_transient(GTK_WINDOW(window));
810
811         label = gtk_label_new(str);
812         gtk_container_add(GTK_CONTAINER(window), label);
813         gtk_widget_show(label);
814
815         gtk_widget_show_now(window);
816
817         return window;
818 }
819
820 void folderview_rescan_tree(Folder *folder)
821 {
822         GtkWidget *window;
823
824         g_return_if_fail(folder != NULL);
825
826         if (!folder->scan_tree) return;
827
828         inc_lock();
829         window = label_window_create(_("Rebuilding folder tree..."));
830
831         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
832         folder_scan_tree(folder);
833         folder_set_ui_func(folder, NULL, NULL);
834
835         folderview_set_all();
836
837         folderview_check_new(folder);
838         
839         gtk_widget_destroy(window);
840         inc_unlock();
841 }
842
843 #if 0
844 void folderview_rescan_all(void)
845 {
846         GList *list;
847         GtkWidget *window;
848
849         inc_lock();
850         window = label_window_create(_("Rescanning all folder trees..."));
851
852         list = folder_get_list();
853         for (; list != NULL; list = list->next) {
854                 Folder *folder = list->data;
855
856                 if (!folder->scan_tree) continue;
857                 folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
858                 folder_scan_tree(folder);
859                 folder_set_ui_func(folder, NULL, NULL);
860         }
861
862         folder_write_list();
863         folderview_set_all();
864
865         list = folder_get_list();
866         for (; list != NULL; list = list->next) {
867                 Folder *folder = list->data;
868
869                 folderview_check_new(folder);
870         }
871
872         gtk_widget_destroy(window);
873         inc_unlock();
874 }
875 #endif
876
877 /** folderview_check_new()
878  *  Scan and update the folder and return the 
879  *  count the number of new messages since last check. 
880  *  \param folder the folder to check for new messages
881  *  \return the number of new messages since last check
882  */
883 gint folderview_check_new(Folder *folder)
884 {
885         GList *list;
886         FolderItem *item;
887         FolderView *folderview;
888         GtkCTree *ctree;
889         GtkCTreeNode *node;
890         gint new_msgs = 0;
891         gint former_new_msgs = 0;
892         gint former_new = 0;
893
894         for (list = folderview_list; list != NULL; list = list->next) {
895                 folderview = (FolderView *)list->data;
896                 ctree = GTK_CTREE(folderview->ctree);
897
898                 inc_lock();
899                 main_window_lock(folderview->mainwin);
900                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
901
902                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
903                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
904                         item = gtk_ctree_node_get_row_data(ctree, node);
905                         if (!item || !item->path || !item->folder) continue;
906                         if (item->no_select) continue;
907                         if (folder && folder != item->folder) continue;
908                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
909
910                         folderview_scan_tree_func(item->folder, item, NULL);
911                         former_new = item->new;
912                         if (folder_item_scan(item) < 0) {
913                                 if (folder && !FOLDER_IS_LOCAL(folder))
914                                         break;
915                         }
916                         folderview_update_node(folderview, node);
917                         new_msgs += item->new;
918                         former_new_msgs += former_new;
919                 }
920
921                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
922                 main_window_unlock(folderview->mainwin);
923                 inc_unlock();
924         }
925
926         folder_write_list();
927         /* Number of new messages since last check is the just the difference 
928          * between former_new_msgs and new_msgs. If new_msgs is less than
929          * former_new_msgs, that would mean another session accessed the folder
930          * and the result is not well defined.
931          */
932         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
933         return new_msgs;
934 }
935
936 void folderview_check_new_all(void)
937 {
938         GList *list;
939         GtkWidget *window;
940         FolderView *folderview;
941
942         folderview = (FolderView *)folderview_list->data;
943
944         inc_lock();
945         main_window_lock(folderview->mainwin);
946         window = label_window_create
947                 (_("Checking for new messages in all folders..."));
948
949         list = folder_get_list();
950         for (; list != NULL; list = list->next) {
951                 Folder *folder = list->data;
952
953                 folderview_check_new(folder);
954         }
955
956         folder_write_list();
957         folderview_set_all();
958
959         gtk_widget_destroy(window);
960         main_window_unlock(folderview->mainwin);
961         inc_unlock();
962 }
963
964 static gboolean folderview_search_new_recursive(GtkCTree *ctree,
965                                                 GtkCTreeNode *node)
966 {
967         FolderItem *item;
968
969         if (node) {
970                 item = gtk_ctree_node_get_row_data(ctree, node);
971                 if (item) {
972                         if (item->new > 0 ||
973                             (item->stype == F_QUEUE && item->total > 0))
974                                 return TRUE;
975                 }
976                 node = GTK_CTREE_ROW(node)->children;
977         } else
978                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
979
980         while (node) {
981                 if (folderview_search_new_recursive(ctree, node) == TRUE)
982                         return TRUE;
983                 node = GTK_CTREE_ROW(node)->sibling;
984         }
985
986         return FALSE;
987 }
988
989 static gboolean folderview_have_new_children(FolderView *folderview,
990                                              GtkCTreeNode *node)
991 {
992         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
993
994         if (!node)
995                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
996         if (!node)
997                 return FALSE;
998
999         node = GTK_CTREE_ROW(node)->children;
1000
1001         while (node) {
1002                 if (folderview_search_new_recursive(ctree, node) == TRUE)
1003                         return TRUE;
1004                 node = GTK_CTREE_ROW(node)->sibling;
1005         }
1006
1007         return FALSE;
1008 }
1009
1010 static gboolean folderview_search_unread_recursive(GtkCTree *ctree,
1011                                                    GtkCTreeNode *node)
1012 {
1013         FolderItem *item;
1014
1015         if (node) {
1016                 item = gtk_ctree_node_get_row_data(ctree, node);
1017                 if (item) {
1018                         if (item->unread > 0 ||
1019                             (item->stype == F_QUEUE && item->total > 0))
1020                                 return TRUE;
1021                 }
1022                 node = GTK_CTREE_ROW(node)->children;
1023         } else
1024                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1025
1026         while (node) {
1027                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
1028                         return TRUE;
1029                 node = GTK_CTREE_ROW(node)->sibling;
1030         }
1031
1032         return FALSE;
1033 }
1034
1035 static gboolean folderview_have_unread_children(FolderView *folderview,
1036                                                 GtkCTreeNode *node)
1037 {
1038         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1039
1040         if (!node)
1041                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1042         if (!node)
1043                 return FALSE;
1044
1045         node = GTK_CTREE_ROW(node)->children;
1046
1047         while (node) {
1048                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
1049                         return TRUE;
1050                 node = GTK_CTREE_ROW(node)->sibling;
1051         }
1052
1053         return FALSE;
1054 }
1055
1056 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1057 {
1058         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1059         GtkStyle *style = NULL;
1060         GtkStyle *color_style = NULL;
1061         FolderItem *item;
1062         GdkPixmap *xpm, *openxpm;
1063         GdkBitmap *mask, *openmask;
1064         gchar *name;
1065         gchar *str;
1066         gboolean add_unread_mark;
1067         gboolean use_bold, use_color;
1068
1069         item = gtk_ctree_node_get_row_data(ctree, node);
1070         g_return_if_fail(item != NULL);
1071
1072         switch (item->stype) {
1073         case F_INBOX:
1074                 if (item->hide_read_msgs) {
1075                         xpm = inboxhrmxpm;
1076                         mask = inboxhrmxpmmask;
1077                         openxpm = inboxopenhrmxpm;
1078                         openmask = inboxopenhrmxpmmask;
1079                 } else {
1080                         xpm = inboxxpm;
1081                         mask = inboxxpmmask;
1082                         openxpm = inboxopenxpm;
1083                         openmask = inboxopenxpmmask;
1084                 }
1085                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1086                                 !strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1087                                 item->name);
1088                 break;
1089         case F_OUTBOX:
1090                 if (item->hide_read_msgs) {
1091                         xpm = outboxhrmxpm;
1092                         mask = outboxhrmxpmmask;
1093                         openxpm = outboxopenhrmxpm;
1094                         openmask = outboxopenhrmxpmmask;
1095                 } else {
1096                         xpm = outboxxpm;
1097                         mask = outboxxpmmask;
1098                         openxpm = outboxopenxpm;
1099                         openmask = outboxopenxpmmask;
1100                 }
1101                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1102                                 !strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1103                                 item->name);
1104                 break;
1105         case F_QUEUE:
1106                 if (item->hide_read_msgs) {
1107                         xpm = queuehrmxpm;
1108                         mask = queuehrmxpmmask;
1109                         openxpm = queueopenhrmxpm;
1110                         openmask = queueopenhrmxpmmask;
1111                 } else {
1112                         xpm = queuexpm;
1113                         mask = queuexpmmask;
1114                         openxpm = queueopenxpm;
1115                         openmask = queueopenxpmmask;
1116                 }
1117                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1118                                 !strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1119                                 item->name);
1120                 break;
1121         case F_TRASH:
1122                 if (item->hide_read_msgs) {
1123                         xpm = trashhrmxpm;
1124                         mask = trashhrmxpmmask;
1125                         openxpm = trashopenhrmxpm;
1126                         openmask = trashopenhrmxpmmask;
1127                 } else {
1128                         xpm = trashxpm;
1129                         mask = trashxpmmask;
1130                         openxpm = trashopenxpm;
1131                         openmask = trashopenxpmmask;
1132                 }
1133                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1134                                 !strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1135                                 item->name);
1136                 break;
1137         case F_DRAFT:
1138                 xpm = folderxpm;
1139                 mask = folderxpmmask;
1140                 if (item->hide_read_msgs) {
1141                         openxpm = folderopenhrmxpm;
1142                         openmask = folderopenhrmxpmmask;
1143                 } else {
1144                         openxpm = folderopenxpm;
1145                         openmask = folderopenxpmmask;
1146                 }
1147                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1148                                 !strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1149                                 item->name);
1150                 break;
1151         default:
1152                 if (item->hide_read_msgs) {
1153                         xpm = folderhrmxpm;
1154                         mask = folderhrmxpmmask;
1155                         openxpm = folderopenhrmxpm;
1156                         openmask = folderopenhrmxpmmask;
1157                 } else {
1158                         xpm = folderxpm;
1159                         mask = folderxpmmask;
1160                         openxpm = folderopenxpm;
1161                         openmask = folderopenxpmmask;
1162                 }
1163                 if (!item->parent) {
1164                         switch (item->folder->type) {
1165                         case F_MH:
1166                                 name = " (MH)"; break;
1167                         case F_MBOX:
1168                                 name = " (mbox)"; break;
1169                         case F_IMAP:
1170                                 name = " (IMAP4)"; break;
1171                         case F_NEWS:
1172                                 name = " (News)"; break;
1173                         default:
1174                                 name = "";
1175                         }
1176                         name = g_strconcat(item->name, name, NULL);
1177                 } else {
1178                         if (item->folder->type == F_NEWS &&
1179                             item->path &&
1180                             !strcmp2(item->name, item->path))
1181                                 name = get_abbrev_newsgroup_name
1182                                         (item->path,
1183                                          prefs_common.ng_abbrev_len);
1184                         else
1185                                 name = g_strdup(item->name);
1186                 }
1187         }
1188
1189         if (!GTK_CTREE_ROW(node)->expanded &&
1190             folderview_have_unread_children(folderview, node))
1191                 add_unread_mark = TRUE;
1192         else
1193                 add_unread_mark = FALSE;
1194
1195         if (item->stype == F_QUEUE && item->total > 0 &&
1196             prefs_common.display_folder_unread) {
1197                 str = g_strdup_printf("%s (%d%s)", name, item->total,
1198                                       add_unread_mark ? "+" : "");
1199                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1200                                         xpm, mask, openxpm, openmask,
1201                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1202                 g_free(str);
1203         } else if ((item->unread > 0 || add_unread_mark) &&
1204                  prefs_common.display_folder_unread) {
1205
1206                 if (item->unread > 0)
1207                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread,
1208                                               add_unread_mark ? "+" : "", 
1209                                               item->unreadmarked > 0 ? "!":"");
1210                 else
1211                         str = g_strdup_printf("%s (+)", name);
1212                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1213                                         xpm, mask, openxpm, openmask,
1214                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1215                 g_free(str);
1216         } else {
1217                 str = g_strdup_printf("%s%s", name, 
1218                                       item->unreadmarked > 0 ? " (!)":"");
1219         
1220                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1221                                         xpm, mask, openxpm, openmask,
1222                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1223                 g_free(str);
1224         }
1225         g_free(name);
1226
1227         if (!item->parent) {
1228                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1229                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1230                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1231         } else {
1232                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
1233                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
1234                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
1235         }
1236
1237         if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1238             item->stype == F_TRASH) {
1239                 use_bold = use_color = FALSE;
1240         } else if (item->stype == F_QUEUE) {
1241                 /* highlight queue folder if there are any messages */
1242                 use_bold = use_color = (item->total > 0);
1243         } else {
1244                 /* if unread messages exist, print with bold font */
1245                 use_bold = (item->unread > 0) || add_unread_mark;
1246                 /* if new messages exist, print with colored letter */
1247                 use_color =
1248                         (item->new > 0) ||
1249                         (add_unread_mark &&
1250                          folderview_have_new_children(folderview, node));       
1251         }
1252
1253         gtk_ctree_node_set_foreground(ctree, node, NULL);
1254
1255         if (use_bold && use_color) {
1256                 style = bold_color_style;
1257         } else if (use_bold) {
1258                 style = bold_style;
1259                 if (item->op_count > 0) {
1260                         style = bold_tgtfold_style;
1261                 }
1262         } else if (use_color) {
1263                 style = normal_color_style;
1264                 gtk_ctree_node_set_foreground(ctree, node,
1265                                               &folderview->color_new);
1266         } else if (item->op_count > 0) {
1267                 style = bold_tgtfold_style;
1268         } else if (item->prefs->color > 0) {
1269                 GdkColor gdk_color;
1270
1271                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1272                 color_style = gtk_style_copy(normal_style);
1273                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1274                 style = color_style;
1275         } else {
1276                 style = normal_style;
1277         }
1278
1279         gtk_ctree_node_set_row_style(ctree, node, style);
1280
1281         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1282                 folderview_update_node(folderview, node);
1283 }
1284
1285 gboolean folderview_update_item(gpointer source, gpointer data)
1286 {
1287         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1288         FolderView *folderview = (FolderView *)data;
1289         GtkCTree *ctree;
1290         GtkCTreeNode *node;
1291
1292         g_return_val_if_fail(update_info != NULL, TRUE);
1293         g_return_val_if_fail(update_info->item != NULL, TRUE);
1294         g_return_val_if_fail(folderview != NULL, FALSE);
1295
1296         ctree = GTK_CTREE(folderview->ctree);
1297
1298         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1299         if (node) {
1300                 if (update_info->update_flags & F_ITEM_UPDATE_MSGCNT)
1301                         folderview_update_node(folderview, node);
1302                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1303                         summary_show(folderview->summaryview, update_info->item);
1304         }
1305         
1306         return FALSE;
1307 }
1308
1309 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1310                                       GNode *gnode, GtkCTreeNode *cnode,
1311                                       gpointer data)
1312 {
1313         FolderView *folderview = (FolderView *)data;
1314         FolderItem *item = FOLDER_ITEM(gnode->data);
1315
1316         g_return_val_if_fail(item != NULL, FALSE);
1317
1318         gtk_ctree_node_set_row_data(ctree, cnode, item);
1319         folderview_update_node(folderview, cnode);
1320
1321         return TRUE;
1322 }
1323
1324 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1325                                    gpointer data)
1326 {
1327         FolderView *folderview = (FolderView *)data;
1328         FolderItem *item;
1329
1330         if (GTK_CTREE_ROW(node)->children) {
1331                 item = gtk_ctree_node_get_row_data(ctree, node);
1332                 g_return_if_fail(item != NULL);
1333
1334                 if (!item->collapsed)
1335                         gtk_ctree_expand(ctree, node);
1336                 else
1337                         folderview_update_node(folderview, node);
1338         }
1339 }
1340
1341 #define SET_SPECIAL_FOLDER(ctree, item) \
1342 { \
1343         if (item) { \
1344                 GtkCTreeNode *node, *parent, *sibling; \
1345  \
1346                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
1347                 if (!node) \
1348                         g_warning("%s not found.\n", item->path); \
1349                 else { \
1350                         parent = GTK_CTREE_ROW(node)->parent; \
1351                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
1352                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
1353                         else \
1354                                 sibling = GTK_CTREE_ROW(parent)->children; \
1355                         if (node != sibling) \
1356                                 gtk_ctree_move(ctree, node, parent, sibling); \
1357                 } \
1358  \
1359                 prev = node; \
1360         } \
1361 }
1362
1363 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1364                                     Folder *folder)
1365 {
1366         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1367         GtkCTreeNode *prev = NULL;
1368
1369         gtk_sctree_sort_recursive(ctree, root);
1370
1371         if (GTK_CTREE_ROW(root)->parent) return;
1372
1373         SET_SPECIAL_FOLDER(ctree, folder->inbox);
1374         SET_SPECIAL_FOLDER(ctree, folder->outbox);
1375         SET_SPECIAL_FOLDER(ctree, folder->draft);
1376         SET_SPECIAL_FOLDER(ctree, folder->queue);
1377         SET_SPECIAL_FOLDER(ctree, folder->trash);
1378 }
1379
1380 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1381 {
1382         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1383         GtkCTreeNode *root;
1384
1385         g_return_if_fail(folder != NULL);
1386
1387         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1388                                       folderview_gnode_func, folderview);
1389         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1390                                 folderview);
1391         folderview_sort_folders(folderview, root, folder);
1392 }
1393
1394 void folderview_new_folder(FolderView *folderview)
1395 {
1396         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1397         FolderItem *item;
1398
1399         if (!folderview->selected) return;
1400
1401         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1402         g_return_if_fail(item != NULL);
1403         g_return_if_fail(item->folder != NULL);
1404
1405         switch (item->folder->type) {
1406         case F_MBOX:
1407                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1408                 break;
1409         case F_MH:
1410         case F_MAILDIR:
1411                 folderview_new_folder_cb(folderview, 0, NULL);
1412                 break;
1413         case F_IMAP:
1414                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1415                 break;
1416         case F_NEWS:
1417         default:
1418                 break;
1419         }
1420 }
1421
1422 void folderview_rename_folder(FolderView *folderview)
1423 {
1424         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1425         FolderItem *item;
1426
1427         if (!folderview->selected) return;
1428
1429         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1430         g_return_if_fail(item != NULL);
1431         g_return_if_fail(item->folder != NULL);
1432         if (!item->path) return;
1433         if (item->stype != F_NORMAL) return;
1434
1435         switch (item->folder->type) {
1436         case F_MBOX:
1437                 folderview_rename_mbox_folder_cb(folderview, 0, NULL);
1438         case F_MH:
1439         case F_MAILDIR:
1440         case F_IMAP:
1441                 folderview_rename_folder_cb(folderview, 0, NULL);
1442                 break;
1443         case F_NEWS:
1444         default:
1445                 break;
1446         }
1447 }
1448
1449 void folderview_delete_folder(FolderView *folderview)
1450 {
1451         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1452         FolderItem *item;
1453
1454         if (!folderview->selected) return;
1455
1456         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1457         g_return_if_fail(item != NULL);
1458         g_return_if_fail(item->folder != NULL);
1459         if (!item->path) return;
1460         if (item->stype != F_NORMAL) return;
1461
1462         switch (item->folder->type) {
1463         case F_MH:
1464         case F_MBOX:
1465         case F_MAILDIR:
1466         case F_IMAP:
1467                 folderview_delete_folder_cb(folderview, 0, NULL);
1468                 break;
1469         case F_NEWS:
1470         default:
1471                 break;
1472         }
1473 }
1474
1475
1476 /* callback functions */
1477
1478 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1479                                       FolderView *folderview)
1480 {
1481         GtkCList *clist = GTK_CLIST(ctree);
1482         gint prev_row = -1, row = -1, column = -1;
1483         FolderItem *item;
1484         Folder *folder;
1485         GtkWidget *popup;
1486         gboolean mark_all_read   = FALSE;
1487         gboolean new_folder      = FALSE;
1488         gboolean rename_folder   = FALSE;
1489         gboolean move_folder     = FALSE;
1490         gboolean delete_folder   = FALSE;
1491         gboolean update_tree     = FALSE;
1492         gboolean rescan_tree     = FALSE;
1493         gboolean remove_tree     = FALSE;
1494         gboolean search_folder   = FALSE;
1495         gboolean folder_property = FALSE;
1496         gboolean folder_processing  = FALSE;
1497         gboolean folder_scoring  = FALSE;
1498
1499         if (!event) return;
1500
1501         if (event->button == 1) {
1502                 folderview->open_folder = TRUE;
1503                 return;
1504         }
1505
1506         if (event->button == 2 || event->button == 3) {
1507                 /* right clicked */
1508                 if (clist->selection) {
1509                         GtkCTreeNode *node;
1510
1511                         node = GTK_CTREE_NODE(clist->selection->data);
1512                         if (node)
1513                                 prev_row = gtkut_ctree_get_nth_from_node
1514                                         (GTK_CTREE(ctree), node);
1515                 }
1516
1517                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1518                                                   &row, &column))
1519                         return;
1520                 if (prev_row != row) {
1521                         gtk_clist_unselect_all(clist);
1522                         if (event->button == 2)
1523                                 folderview_select_node
1524                                         (folderview,
1525                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1526                                                             row));
1527                         else
1528                                 gtk_clist_select_row(clist, row, column);
1529                 }
1530         }
1531
1532         if (event->button != 3) return;
1533
1534         item = gtk_clist_get_row_data(clist, row);
1535         g_return_if_fail(item != NULL);
1536         g_return_if_fail(item->folder != NULL);
1537         folder = item->folder;
1538
1539         if (folderview->mainwin->lock_count == 0) {
1540                 new_folder = TRUE;
1541                 if (item->parent == NULL) {
1542                         update_tree = remove_tree = TRUE;
1543                         if (folder->account)
1544                                 folder_property = TRUE;
1545                 } else
1546                         mark_all_read = search_folder = folder_property = TRUE;
1547                         
1548                 if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
1549                         if (item->parent == NULL)
1550                                 update_tree = rescan_tree = TRUE;
1551                         else if (item->stype == F_NORMAL)
1552                                 move_folder = rename_folder = delete_folder = folder_scoring = folder_processing = TRUE;
1553                         else if (item->stype == F_INBOX)
1554                                 folder_scoring = folder_processing = TRUE;
1555                         else if (item->stype == F_TRASH)
1556                                 folder_processing = TRUE;
1557                         else if (item->stype == F_OUTBOX)
1558                                 folder_processing = TRUE;
1559                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
1560                         if (item->parent != NULL)
1561                                 delete_folder = folder_scoring = folder_processing = TRUE;
1562                 }
1563                 if (item->unread < 1) 
1564                         mark_all_read = FALSE;
1565         }
1566
1567 #define SET_SENS(factory, name, sens) \
1568         menu_set_sensitive(folderview->factory, name, sens)
1569         
1570         mark_all_read = mark_all_read && 
1571                         (item == folderview->summaryview->folder_item);
1572
1573         if (FOLDER_IS_LOCAL(folder)) {
1574                 popup = folderview->mail_popup;
1575                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1576                 SET_SENS(mail_factory, "/Mark all read", mark_all_read);
1577                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1578                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1579                 SET_SENS(mail_factory, "/Move folder...", move_folder);
1580                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1581                 SET_SENS(mail_factory, "/Check for new messages", update_tree);
1582                 SET_SENS(mail_factory, "/Rebuild folder tree", rescan_tree);
1583                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1584                 SET_SENS(mail_factory, "/Search folder...", search_folder);
1585                 SET_SENS(mail_factory, "/Properties...", folder_property);
1586                 SET_SENS(mail_factory, "/Processing...", folder_processing);
1587                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1588         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1589                 popup = folderview->imap_popup;
1590                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1591                 SET_SENS(imap_factory, "/Mark all read", mark_all_read);
1592                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1593                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1594                 SET_SENS(imap_factory, "/Move folder...", move_folder);
1595                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1596                 SET_SENS(imap_factory, "/Check for new messages", update_tree);
1597                 SET_SENS(imap_factory, "/Rebuild folder tree", rescan_tree);
1598                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1599                 SET_SENS(imap_factory, "/Search folder...", search_folder);
1600                 SET_SENS(imap_factory, "/Properties...", folder_property);
1601                 SET_SENS(imap_factory, "/Processing...", folder_processing);
1602                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1603         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1604                 popup = folderview->news_popup;
1605                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1606                 SET_SENS(news_factory, "/Mark all read", mark_all_read);
1607                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1608                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1609                 SET_SENS(news_factory, "/Check for new messages", update_tree);
1610                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1611                 SET_SENS(news_factory, "/Search folder...", search_folder);
1612                 SET_SENS(news_factory, "/Properties...", folder_property);
1613                 SET_SENS(news_factory, "/Processing...", folder_processing);
1614                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1615         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1616                 popup = folderview->mbox_popup;
1617                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1618                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1619                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1620                 SET_SENS(mbox_factory, "/Move folder...", move_folder);
1621                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1622                 SET_SENS(news_factory, "/Properties...", folder_property);
1623                 SET_SENS(mbox_factory, "/Processing...", folder_processing);
1624                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1625         } else
1626                 return;
1627
1628 #undef SET_SENS
1629
1630         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1631                        event->button, event->time);
1632 }
1633
1634 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1635                                        FolderView *folderview)
1636 {
1637         if (!event) return;
1638
1639         if (event->button == 1 && folderview->open_folder == FALSE &&
1640             folderview->opened != NULL) {
1641                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1642                                           folderview->opened);
1643                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1644         }
1645 }
1646
1647 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1648                                    FolderView *folderview)
1649 {
1650         if (!event) return;
1651
1652         switch (event->keyval) {
1653         case GDK_Return:
1654                 if (folderview->selected) {
1655                         folderview_select_node(folderview,
1656                                                folderview->selected);
1657                 }
1658                 break;
1659         case GDK_space:
1660                 if (folderview->selected) {
1661                         if (folderview->opened == folderview->selected &&
1662                             folderview->summaryview->messages == 0)
1663                                 folderview_select_next_unread(folderview);
1664                         else
1665                                 folderview_select_node(folderview,
1666                                                        folderview->selected);
1667                 }
1668                 break;
1669         default:
1670                 break;
1671         }
1672 }
1673
1674 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1675                                 gint column, FolderView *folderview)
1676 {
1677         static gboolean can_select = TRUE;      /* exclusive lock */
1678         gboolean opened;
1679         FolderItem *item;
1680         gchar *buf;
1681
1682         folderview->selected = row;
1683
1684         if (folderview->opened == row) {
1685                 folderview->open_folder = FALSE;
1686                 return;
1687         }
1688
1689         if (!can_select || summary_is_locked(folderview->summaryview)) {
1690                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1691                 gtk_ctree_select(ctree, folderview->opened);
1692                 return;
1693         }
1694
1695         if (!folderview->open_folder) return;
1696
1697         item = gtk_ctree_node_get_row_data(ctree, row);
1698         if (!item) return;
1699
1700         can_select = FALSE;
1701
1702         /* Save cache for old folder */
1703         /* We don't want to lose all caches if sylpheed crashed */
1704         if (folderview->opened) {
1705                 FolderItem *olditem;
1706                 
1707                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1708                 if (olditem) {
1709                         /* will be null if we just moved the previously opened folder */
1710                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1711                         folder_item_close(olditem);
1712                 }
1713         }
1714
1715         /* CLAWS: set compose button type: news folder items 
1716          * always have a news folder as parent */
1717         if (item->folder) 
1718                 toolbar_set_compose_button
1719                         (folderview->mainwin->toolbar,
1720                          item->folder->type == F_NEWS ? 
1721                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1722
1723         if (item->path)
1724                 debug_print("Folder %s is selected\n", item->path);
1725
1726         if (!GTK_CTREE_ROW(row)->children)
1727                 gtk_ctree_expand(ctree, row);
1728         if (folderview->opened &&
1729             !GTK_CTREE_ROW(folderview->opened)->children)
1730                 gtk_ctree_collapse(ctree, folderview->opened);
1731
1732         /* ungrab the mouse event */
1733         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1734                 gtk_grab_remove(GTK_WIDGET(ctree));
1735                 if (gdk_pointer_is_grabbed())
1736                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1737         }
1738
1739         /* Open Folder */
1740         buf = g_strdup_printf(_("Opening Folder %s..."), item->path);
1741         debug_print("%s\n", buf);
1742         STATUSBAR_PUSH(folderview->mainwin, buf);
1743         g_free(buf);
1744
1745         main_window_cursor_wait(folderview->mainwin);
1746
1747         if (folder_item_open(item) != 0) {
1748                 main_window_cursor_normal(folderview->mainwin);
1749                 STATUSBAR_POP(folderview->mainwin);
1750
1751                 alertpanel_error(_("Folder could not be opened."));
1752
1753                 return;
1754         }
1755
1756         main_window_cursor_normal(folderview->mainwin);
1757
1758         /* Show messages */
1759         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1760         opened = summary_show(folderview->summaryview, item);
1761
1762         folder_clean_cache_memory();
1763
1764         if (!opened) {
1765                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1766                 gtk_ctree_select(ctree, folderview->opened);
1767         } else {
1768                 folderview->opened = row;
1769                 if (gtk_ctree_node_is_visible(ctree, row)
1770                     != GTK_VISIBILITY_FULL)
1771                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1772         }
1773
1774         STATUSBAR_POP(folderview->mainwin);
1775
1776         folderview->open_folder = FALSE;
1777         can_select = TRUE;
1778 }
1779
1780 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1781                                      FolderView *folderview)
1782 {
1783         FolderItem *item;
1784
1785         item = gtk_ctree_node_get_row_data(ctree, node);
1786         g_return_if_fail(item != NULL);
1787         item->collapsed = FALSE;
1788         folderview_update_node(folderview, node);
1789 }
1790
1791 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1792                                       FolderView *folderview)
1793 {
1794         FolderItem *item;
1795
1796         item = gtk_ctree_node_get_row_data(ctree, node);
1797         g_return_if_fail(item != NULL);
1798         item->collapsed= TRUE;
1799         folderview_update_node(folderview, node);
1800 }
1801
1802 static void folderview_popup_close(GtkMenuShell *menu_shell,
1803                                    FolderView *folderview)
1804 {
1805         if (!folderview->opened) return;
1806
1807         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1808                                   folderview->opened);
1809         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1810 }
1811
1812 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1813                                    FolderView *folderview)
1814 {
1815         switch (column) {
1816         case COL_FOLDER:
1817                 prefs_common.folder_col_folder = width;
1818                 break;
1819         case COL_NEW:
1820                 prefs_common.folder_col_new = width;
1821                 break;
1822         case COL_UNREAD:
1823                 prefs_common.folder_col_unread = width;
1824                 break;
1825         case COL_TOTAL:
1826                 prefs_common.folder_col_total = width;
1827                 break;
1828         default:
1829                 break;
1830         }
1831 }
1832
1833 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1834                                              GtkCTreeNode *node,
1835                                              const gchar *name)
1836 {
1837         FolderItem *item;
1838
1839         if (!node)
1840                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1841         if (!node)
1842                 return NULL;
1843
1844         node = GTK_CTREE_ROW(node)->children;
1845
1846         while (node) {
1847                 item = gtk_ctree_node_get_row_data(ctree, node);
1848                 if (!folderview_compare_name(item, name))
1849                         return node;
1850                 node = GTK_CTREE_ROW(node)->sibling;
1851         }
1852
1853         return NULL;
1854 }
1855
1856 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1857                                       GtkWidget *widget)
1858 {
1859         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1860         FolderItem *item;
1861
1862         if (!folderview->selected) return;
1863
1864         summary_show(folderview->summaryview, NULL);
1865
1866         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1867         g_return_if_fail(item != NULL);
1868         g_return_if_fail(item->folder != NULL);
1869
1870         if (action == 0)
1871                 folderview_check_new(item->folder);
1872         else
1873                 folderview_rescan_tree(item->folder);
1874 }
1875
1876 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1877 {
1878         GNode *srcnode;
1879
1880         folderview_create_folder_node(folderview, item);
1881         
1882         srcnode = item->folder->node;   
1883         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1884         srcnode = srcnode->children;
1885         while (srcnode != NULL) {
1886                 if (srcnode && srcnode->data) {
1887                         FolderItem *next_item = (FolderItem*) srcnode->data;
1888                         folderview_create_folder_node_recursive(folderview, next_item);
1889                 }
1890                 srcnode = srcnode->next;
1891         }
1892 }
1893
1894 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1895 {
1896         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1897         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1898         GtkCTreeNode *node, *parent_node;
1899         
1900         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, item->parent);
1901
1902         gtk_clist_freeze(GTK_CLIST(ctree));
1903
1904         text[COL_FOLDER] = item->name;
1905         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1906                                      FOLDER_SPACING,
1907                                      folderxpm, folderxpmmask,
1908                                      folderopenxpm, folderopenxpmmask,
1909                                      FALSE, FALSE);
1910         gtk_ctree_expand(ctree, parent_node);
1911         gtk_ctree_node_set_row_data(ctree, node, item);
1912         if (normal_style)
1913                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1914         folderview_sort_folders(folderview, folderview->selected, item->folder);
1915
1916         gtk_clist_thaw(GTK_CLIST(ctree));
1917 }
1918
1919 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1920                                      GtkWidget *widget)
1921 {
1922         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1923         FolderItem *item;
1924         FolderItem *new_item;
1925         gchar *new_folder;
1926         gchar *name, *name_;
1927
1928         if (!folderview->selected) return;
1929
1930         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1931         g_return_if_fail(item != NULL);
1932         g_return_if_fail(item->folder != NULL);
1933
1934         new_folder = input_dialog(_("New folder"),
1935                                   _("Input the name of new folder:"),
1936                                   _("NewFolder"));
1937         if (!new_folder) return;
1938
1939         if (item->folder->type != F_MBOX) {
1940                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1941                         alertpanel_error(_("`%c' can't be included in folder name."),
1942                                          G_DIR_SEPARATOR);
1943                         g_free(new_folder);
1944                         return;
1945                 }
1946         }
1947
1948         name_ = trim_string(new_folder, 32);
1949         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1950         g_free(name_);
1951
1952         /* find whether the directory already exists */
1953         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1954                 alertpanel_error(_("The folder `%s' already exists."), name);
1955                 g_free(new_folder);
1956                 return;
1957         }
1958
1959         new_item = folder_create_folder(item, new_folder);
1960         if (!new_item) {
1961                 alertpanel_error(_("Can't create the folder `%s'."), name);
1962                 g_free(new_folder);
1963                 return;
1964         } 
1965         g_free(new_folder);
1966
1967         folderview_create_folder_node(folderview, new_item);
1968
1969         folder_write_list();
1970 }
1971
1972 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1973                                           GtkWidget *widget)
1974 {
1975         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1976         FolderItem *item;
1977         FolderItem *new_item;
1978         gchar *new_folder;
1979
1980         if (!folderview->selected) return;
1981
1982         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1983         g_return_if_fail(item != NULL);
1984         g_return_if_fail(item->folder != NULL);
1985
1986         new_folder = input_dialog(_("New folder"),
1987                                   _("Input the name of new folder:"),
1988                                   _("NewFolder"));
1989         if (!new_folder) return;
1990
1991         /* find whether the directory already exists */
1992         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1993                 alertpanel_error(_("The folder `%s' already exists."),
1994                                  new_folder);
1995                 g_free(new_folder);
1996                 return;
1997         }
1998
1999         new_item = folder_create_folder(item, new_folder);
2000         g_free(new_folder);
2001         if (!new_item) return;
2002
2003         folderview_create_folder_node(folderview, new_item);
2004
2005         folder_write_list();
2006 }
2007
2008 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
2009                                         GtkWidget *widget)
2010 {
2011         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2012         FolderItem *item;
2013         gchar *new_folder;
2014         gchar *name, *name_;
2015         gchar *message;
2016         gchar *old_path;
2017         gchar *old_id;
2018         gchar *new_id;
2019
2020         if (!folderview->selected) return;
2021
2022         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2023         g_return_if_fail(item != NULL);
2024         g_return_if_fail(item->path != NULL);
2025         g_return_if_fail(item->folder != NULL);
2026
2027         name_ = trim_string(item->name, 32);
2028         Xstrdup_a(name, name_, return);
2029         g_free(name_);
2030         message = g_strdup_printf(_("Input new name for `%s':"), name);
2031         new_folder = input_dialog(_("Rename folder"), message,
2032                                   g_basename(item->path));
2033         g_free(message);
2034         if (!new_folder) return;
2035
2036         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
2037                 alertpanel_error(_("`%c' can't be included in folder name."),
2038                                  G_DIR_SEPARATOR);
2039                 g_free(new_folder);
2040                 return;
2041         }
2042
2043         if (folderview_find_by_name
2044                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2045                  new_folder)) {
2046                 name = trim_string(new_folder, 32);
2047                 alertpanel_error(_("The folder `%s' already exists."), name);
2048                 g_free(name);
2049                 g_free(new_folder);
2050                 return;
2051         }
2052
2053         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
2054         old_id = folder_item_get_identifier(item);
2055
2056         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2057                 g_free(old_id);
2058                 g_free(new_folder);
2059                 return;
2060         }
2061         g_free(new_folder);
2062
2063         /* if (FOLDER_TYPE(item->folder) == F_MH)
2064                 prefs_filtering_rename_path(old_path, item->path); */
2065         new_id = folder_item_get_identifier(item);
2066         prefs_filtering_rename_path(old_id, new_id);
2067
2068         g_free(old_id);
2069         g_free(new_id);
2070
2071         gtk_clist_freeze(GTK_CLIST(ctree));
2072
2073         folderview_update_node(folderview, folderview->selected);
2074         folderview_sort_folders(folderview,
2075                                 GTK_CTREE_ROW(folderview->selected)->parent,
2076                                 item->folder);
2077         if (folderview->opened == folderview->selected ||
2078             gtk_ctree_is_ancestor(ctree,
2079                                   folderview->selected,
2080                                   folderview->opened)) {
2081                 GtkCTreeNode *node = folderview->opened;
2082                 folderview_unselect(folderview);
2083                 folderview_select_node(folderview, node);
2084         }
2085
2086         gtk_clist_thaw(GTK_CLIST(ctree));
2087
2088         folder_write_list();
2089 }
2090
2091 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
2092                                              guint action,
2093                                              GtkWidget *widget)
2094 {
2095         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2096         FolderItem *item;
2097         gchar *new_folder;
2098         gchar *message;
2099
2100         if (!folderview->selected) return;
2101
2102         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2103         g_return_if_fail(item != NULL);
2104         g_return_if_fail(item->path != NULL);
2105         g_return_if_fail(item->folder != NULL);
2106
2107         message = g_strdup_printf(_("Input new name for `%s':"),
2108                                   g_basename(item->path));
2109         new_folder = input_dialog(_("Rename folder"), message,
2110                                   g_basename(item->path));
2111         g_free(message);
2112         if (!new_folder) return;
2113
2114         if (folderview_find_by_name
2115                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2116                  new_folder)) {
2117                 alertpanel_error(_("The folder `%s' already exists."),
2118                                  new_folder);
2119                 g_free(new_folder);
2120                 return;
2121         }
2122
2123         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2124                 g_free(new_folder);
2125                 return;
2126         }
2127         g_free(new_folder);
2128
2129         gtk_clist_freeze(GTK_CLIST(ctree));
2130
2131         folderview_update_node(folderview, folderview->selected);
2132         folderview_sort_folders(folderview,
2133                                 GTK_CTREE_ROW(folderview->selected)->parent,
2134                                 item->folder);
2135         if (folderview->opened == folderview->selected) {
2136                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2137                         gtk_ctree_expand(ctree, folderview->opened);
2138                 summary_show(folderview->summaryview, item);
2139         }
2140
2141         gtk_clist_thaw(GTK_CLIST(ctree));
2142
2143         folder_write_list();
2144 }
2145
2146 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2147                                         GtkWidget *widget)
2148 {
2149         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2150         FolderItem *item;
2151         gchar *message, *name, *name_;
2152         AlertValue avalue;
2153         gchar *old_path;
2154         gchar *old_id;
2155
2156         if (!folderview->selected) return;
2157
2158         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2159         g_return_if_fail(item != NULL);
2160         g_return_if_fail(item->path != NULL);
2161         g_return_if_fail(item->folder != NULL);
2162
2163         name_ = trim_string(item->name, 32);
2164         Xstrdup_a(name, name_, return);
2165         g_free(name_);
2166         message = g_strdup_printf
2167                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2168                    "Do you really want to delete?"), name);
2169         avalue = alertpanel(_("Delete folder"), message,
2170                             _("Yes"), _("+No"), NULL);
2171         g_free(message);
2172         if (avalue != G_ALERTDEFAULT) return;
2173
2174         Xstrdup_a(old_path, item->path, return);
2175         old_id = folder_item_get_identifier(item);
2176
2177         if (item->folder->remove_folder(item->folder, item) < 0) {
2178                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2179                 if (folderview->opened == folderview->selected)
2180                         summary_show(folderview->summaryview,
2181                                      folderview->summaryview->folder_item);
2182                 g_free(old_id);
2183                 return;
2184         }
2185
2186         /* if (FOLDER_TYPE(item->folder) == F_MH)
2187                 prefs_filtering_delete_path(old_path); */
2188
2189         if (folderview->opened == folderview->selected ||
2190             gtk_ctree_is_ancestor(ctree,
2191                                   folderview->selected,
2192                                   folderview->opened)) {
2193                 summary_clear_all(folderview->summaryview);
2194                 folderview->opened = NULL;
2195         }
2196
2197         gtk_ctree_remove_node(ctree, folderview->selected);
2198         folder_write_list();
2199
2200         prefs_filtering_delete_path(old_id);
2201         g_free(old_id);
2202
2203 }
2204
2205 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2206                                          GtkWidget *widget)
2207 {
2208         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2209         GtkCTreeNode *node;
2210         FolderItem *item;
2211         gchar *name, *name_;
2212         gchar *message;
2213         AlertValue avalue;
2214
2215         if (!folderview->selected) return;
2216         node = folderview->selected;
2217         item = gtk_ctree_node_get_row_data(ctree, node);
2218         g_return_if_fail(item != NULL);
2219         g_return_if_fail(item->folder != NULL);
2220         if (item->parent) return;
2221
2222         name_ = trim_string(item->folder->name, 32);
2223         Xstrdup_a(name, name_, return);
2224         g_free(name_);
2225         message = g_strdup_printf
2226                 (_("Really remove the mailbox `%s' ?\n"
2227                    "(The messages are NOT deleted from the disk)"), name);
2228         avalue = alertpanel(_("Remove mailbox"), message,
2229                             _("Yes"), _("+No"), NULL);
2230         g_free(message);
2231         if (avalue != G_ALERTDEFAULT) return;
2232
2233         folder_destroy(item->folder);
2234         summary_clear_all(folderview->summaryview);
2235         folderview_unselect(folderview);
2236         gtk_ctree_remove_node(ctree, node);
2237         folder_write_list();
2238 }
2239
2240 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2241                                           GtkWidget *widget)
2242 {
2243         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2244         FolderItem *item;
2245         FolderItem *new_item;
2246         gchar *new_folder;
2247         gchar *name, *name_;
2248         gchar *p;
2249
2250         if (!folderview->selected) return;
2251
2252         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2253         g_return_if_fail(item != NULL);
2254         g_return_if_fail(item->folder != NULL);
2255         g_return_if_fail(item->folder->type == F_IMAP);
2256         g_return_if_fail(item->folder->account != NULL);
2257
2258         new_folder = input_dialog
2259                 (_("New folder"),
2260                  _("Input the name of new folder:\n"
2261                    "(if you want to create a folder to store subfolders,\n"
2262                    " append `/' at the end of the name)"),
2263                  _("NewFolder"));
2264         if (!new_folder) return;
2265
2266         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2267             *(p + 1) != '\0') {
2268                 alertpanel_error(_("`%c' can't be included in folder name."),
2269                                  G_DIR_SEPARATOR);
2270                 g_free(new_folder);
2271                 return;
2272         }
2273
2274         name_ = trim_string(new_folder, 32);
2275         Xstrdup_a(name, name_, return);
2276         g_free(name_);
2277
2278         /* find whether the directory already exists */
2279         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2280                 alertpanel_error(_("The folder `%s' already exists."), name);
2281                 g_free(new_folder);
2282                 return;
2283         }
2284
2285         new_item = folder_create_folder(item, new_folder);
2286         if (!new_item) {
2287                 alertpanel_error(_("Can't create the folder `%s'."), name);
2288                 g_free(new_folder);
2289                 return;
2290         }
2291         g_free(new_folder);
2292
2293         folderview_create_folder_node(folderview, new_item);
2294
2295         folder_write_list();
2296 }
2297
2298 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2299                                          GtkWidget *widget)
2300 {
2301         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2302         FolderItem *item;
2303         PrefsAccount *account;
2304         gchar *name, *name_;
2305         gchar *message;
2306         AlertValue avalue;
2307
2308         if (!folderview->selected) return;
2309
2310         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2311         g_return_if_fail(item != NULL);
2312         g_return_if_fail(item->folder != NULL);
2313         g_return_if_fail(item->folder->type == F_IMAP);
2314         g_return_if_fail(item->folder->account != NULL);
2315
2316         name_ = trim_string(item->folder->name, 32);
2317         Xstrdup_a(name, name_, return);
2318         g_free(name_);
2319         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2320         avalue = alertpanel(_("Delete IMAP4 account"), message,
2321                             _("Yes"), _("+No"), NULL);
2322         g_free(message);
2323
2324         if (avalue != G_ALERTDEFAULT) return;
2325
2326         if (folderview->opened == folderview->selected ||
2327             gtk_ctree_is_ancestor(ctree,
2328                                   folderview->selected,
2329                                   folderview->opened)) {
2330                 summary_clear_all(folderview->summaryview);
2331                 folderview->opened = NULL;
2332         }
2333
2334         account = item->folder->account;
2335         folder_destroy(item->folder);
2336         account_destroy(account);
2337         gtk_ctree_remove_node(ctree, folderview->selected);
2338         account_set_menu();
2339         main_window_reflect_prefs_all();
2340         folder_write_list();
2341 }
2342
2343 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2344                                          GtkWidget *widget)
2345 {
2346         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2347         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2348         GtkCTreeNode *servernode, *node;
2349         Folder *folder;
2350         FolderItem *item;
2351         FolderItem *rootitem;
2352         FolderItem *newitem;
2353         GSList *new_subscr;
2354         GSList *cur;
2355         GNode *gnode;
2356
2357         if (!folderview->selected) return;
2358
2359         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2360         g_return_if_fail(item != NULL);
2361         folder = item->folder;
2362         g_return_if_fail(folder != NULL);
2363         g_return_if_fail(folder->type == F_NEWS);
2364         g_return_if_fail(folder->account != NULL);
2365
2366         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2367                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2368         else
2369                 servernode = folderview->selected;
2370
2371         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2372
2373         new_subscr = grouplist_dialog(folder);
2374
2375         /* remove unsubscribed newsgroups */
2376         for (gnode = folder->node->children; gnode != NULL; ) {
2377                 GNode *next = gnode->next;
2378
2379                 item = FOLDER_ITEM(gnode->data);
2380                 if (g_slist_find_custom(new_subscr, item->path,
2381                                         (GCompareFunc)g_strcasecmp) != NULL) {
2382                         gnode = next;
2383                         continue;
2384                 }
2385
2386                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2387                 if (!node) {
2388                         gnode = next;
2389                         continue;
2390                 }
2391
2392                 if (folderview->opened == node) {
2393                         summary_clear_all(folderview->summaryview);
2394                         folderview->opened = NULL;
2395                 }
2396
2397                 folder_item_remove(item);
2398                 gtk_ctree_remove_node(ctree, node);
2399
2400                 gnode = next;
2401         }
2402
2403         gtk_clist_freeze(GTK_CLIST(ctree));
2404
2405         /* add subscribed newsgroups */
2406         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2407                 gchar *name = (gchar *)cur->data;
2408
2409                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2410                         continue;
2411
2412                 text[COL_FOLDER] = name;
2413                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2414                                              FOLDER_SPACING,
2415                                              folderxpm, folderxpmmask,
2416                                              folderopenxpm, folderopenxpmmask,
2417                                              FALSE, FALSE);
2418                 gtk_ctree_expand(ctree, servernode);
2419
2420                 newitem = folder_item_new(folder, name, name);
2421                 folder_item_append(rootitem, newitem);
2422                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2423         }
2424
2425         folderview_sort_folders(folderview, servernode, folder);
2426         gtk_clist_thaw(GTK_CLIST(ctree));
2427
2428         slist_free_strings(new_subscr);
2429         g_slist_free(new_subscr);
2430
2431         folder_write_list();
2432 }
2433
2434 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2435                                         GtkWidget *widget)
2436 {
2437         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2438         FolderItem *item;
2439         gchar *name, *name_;
2440         gchar *message;
2441         AlertValue avalue;
2442
2443         if (!folderview->selected) return;
2444
2445         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2446         g_return_if_fail(item != NULL);
2447         g_return_if_fail(item->folder != NULL);
2448         g_return_if_fail(item->folder->type == F_NEWS);
2449         g_return_if_fail(item->folder->account != NULL);
2450
2451         name_ = trim_string(item->path, 32);
2452         Xstrdup_a(name, name_, return);
2453         g_free(name_);
2454         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2455         avalue = alertpanel(_("Delete newsgroup"), message,
2456                             _("Yes"), _("+No"), NULL);
2457         g_free(message);
2458         if (avalue != G_ALERTDEFAULT) return;
2459
2460         if (folderview->opened == folderview->selected) {
2461                 summary_clear_all(folderview->summaryview);
2462                 folderview->opened = NULL;
2463         }
2464
2465         folder_item_remove(item);
2466         gtk_ctree_remove_node(ctree, folderview->selected);
2467         folder_write_list();
2468 }
2469
2470 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2471                                          GtkWidget *widget)
2472 {
2473         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2474         FolderItem *item;
2475         PrefsAccount *account;
2476         gchar *name, *name_;
2477         gchar *message;
2478         AlertValue avalue;
2479
2480         if (!folderview->selected) return;
2481
2482         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2483         g_return_if_fail(item != NULL);
2484         g_return_if_fail(item->folder != NULL);
2485         g_return_if_fail(item->folder->type == F_NEWS);
2486         g_return_if_fail(item->folder->account != NULL);
2487
2488         name_ = trim_string(item->folder->name, 32);
2489         Xstrdup_a(name, name_, return);
2490         g_free(name_);
2491         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2492         avalue = alertpanel(_("Delete news account"), message,
2493                             _("Yes"), _("+No"), NULL);
2494         g_free(message);
2495
2496         if (avalue != G_ALERTDEFAULT) return;
2497
2498         if (folderview->opened == folderview->selected ||
2499             gtk_ctree_is_ancestor(ctree,
2500                                   folderview->selected,
2501                                   folderview->opened)) {
2502                 summary_clear_all(folderview->summaryview);
2503                 folderview->opened = NULL;
2504         }
2505
2506         account = item->folder->account;
2507         folder_destroy(item->folder);
2508         account_destroy(account);
2509         gtk_ctree_remove_node(ctree, folderview->selected);
2510         account_set_menu();
2511         main_window_reflect_prefs_all();
2512         folder_write_list();
2513 }
2514
2515 static void folderview_search_cb(FolderView *folderview, guint action,
2516                                  GtkWidget *widget)
2517 {
2518         summary_search(folderview->summaryview);
2519 }
2520
2521 static void folderview_property_cb(FolderView *folderview, guint action,
2522                                    GtkWidget *widget)
2523 {
2524         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2525         FolderItem *item;
2526
2527         if (!folderview->selected) return;
2528
2529         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2530         g_return_if_fail(item != NULL);
2531         g_return_if_fail(item->folder != NULL);
2532
2533         if (item->parent == NULL && item->folder->account)
2534                 account_open(item->folder->account);
2535         else
2536                 prefs_folder_item_create(folderview, item);
2537 }
2538
2539 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2540 {
2541         GSList *list = NULL;
2542         GSList *done = NULL;
2543         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2544         
2545         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2546                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2547                 &&  list->data != node) {
2548                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2549                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2550                 }
2551         }
2552         for (list = done; list != NULL; list = g_slist_next(list)) {
2553                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2554                                                                  list->data);
2555         }
2556         g_slist_free(done);
2557 }
2558
2559 static void folderview_move_to_cb(FolderView *folderview) 
2560 {
2561         FolderItem *from_folder = NULL, *to_folder = NULL;
2562
2563         if (folderview->selected)
2564                 from_folder = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree), folderview->selected);
2565         if (!from_folder || from_folder->folder->type == F_NEWS)
2566                 return;
2567
2568         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
2569         
2570         if (!to_folder || to_folder->folder->type == F_NEWS)
2571                 return;
2572
2573         folderview_move_to(folderview, from_folder, to_folder);
2574 }
2575
2576 static void folderview_move_to(FolderView *folderview, FolderItem *from_folder,
2577                                FolderItem *to_folder)
2578 {
2579         FolderItem *from_parent = NULL;
2580         FolderItem *new_folder = NULL;
2581         GtkCTreeNode *src_node = NULL;
2582         gchar *buf;
2583         gint status;
2584
2585         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2586         from_parent = from_folder->parent;
2587         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2588         STATUSBAR_PUSH(folderview->mainwin, buf);
2589         g_free(buf);
2590         summary_clear_all(folderview->summaryview);
2591         folderview->opened = NULL;
2592         folderview->selected = NULL;
2593         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2594         inc_lock();
2595         main_window_cursor_wait(folderview->mainwin);
2596         statusbar_verbosity_set(TRUE);
2597         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2598                 statusbar_verbosity_set(FALSE);
2599                 main_window_cursor_normal(folderview->mainwin);
2600                 STATUSBAR_POP(folderview->mainwin);
2601                 if (src_node)
2602                         gtk_ctree_remove_node(GTK_CTREE(folderview->ctree), src_node);
2603                 else 
2604                         debug_print("can't remove src node: is null\n");
2605
2606                 folderview_create_folder_node_recursive(folderview, new_folder);
2607                 folderview_sort_folders(folderview, 
2608                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2609                                 NULL, new_folder->parent), new_folder->folder);
2610                 folderview_select(folderview, new_folder);
2611         } else {
2612                 statusbar_verbosity_set(FALSE);         
2613                 main_window_cursor_normal(folderview->mainwin);
2614                 STATUSBAR_POP(folderview->mainwin);
2615                 switch (status) {
2616                 case F_MOVE_FAILED_DEST_IS_PARENT:
2617                         alertpanel_error(_("Source and destination are the same."));
2618                         break;
2619                 case F_MOVE_FAILED_DEST_IS_CHILD:
2620                         alertpanel_error(_("Can't move a folder to one of its children."));
2621                         break;
2622                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2623                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2624                         break;
2625                 default:
2626                         alertpanel_error(_("Move failed!"));
2627                         break;
2628                 }
2629         }       
2630         inc_unlock();           
2631         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2632 }
2633
2634 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2635                                           GdkDragContext *context,
2636                                           gint            x,
2637                                           gint            y,
2638                                           guint           time,
2639                                           FolderView     *folderview)
2640 {
2641         gint row, column;
2642         FolderItem *item, *src_item;
2643         GtkCTreeNode *node = NULL;
2644         gboolean acceptable = FALSE;
2645         gint height = folderview->ctree->allocation.height;
2646         gint total_height = folderview->ctree->requisition.height;
2647         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2648                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2649         gfloat vpos = pos->value;
2650
2651         if (gtk_clist_get_selection_info
2652                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2653                 if (y > height - 24 && height + vpos < total_height)
2654                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2655
2656                 if (y < 24 && y > 0)
2657                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2658
2659                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2660                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2661                 src_item = folderview->summaryview->folder_item;
2662
2663                 if (item && item->folder && item->path &&
2664                     src_item && src_item != item) {
2665                         switch (item->folder->type) {
2666                         case F_MH:
2667                         case F_MBOX:
2668                         case F_IMAP:
2669                                 acceptable = TRUE;
2670                                 break;
2671                         default:
2672                                 break;
2673                         }
2674                 } else if (item && item->folder && folder_item_get_path(item) &&
2675                            src_item && src_item != item) {
2676                         /* a root folder - acceptable only from folderview */
2677                         if (item->folder->type == F_MH || item->folder->type == F_IMAP)
2678                                 acceptable = TRUE;
2679                 }
2680                         
2681         }
2682
2683         if (acceptable || (src_item && src_item == item)) {
2684                 folderview_recollapse_nodes(folderview, node);
2685                 if (item->collapsed) {
2686                         gtk_ctree_expand(GTK_CTREE(widget), node);
2687                         folderview->nodes_to_recollapse = g_slist_append(
2688                                                                 folderview->nodes_to_recollapse,
2689                                                                 node);
2690                 }
2691         }
2692                 
2693         if (acceptable) {
2694                 gtk_signal_handler_block_by_func
2695                         (GTK_OBJECT(widget),
2696                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2697                 gtk_ctree_select(GTK_CTREE(widget), node);
2698                 gtk_signal_handler_unblock_by_func
2699                         (GTK_OBJECT(widget),
2700                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2701                 gdk_drag_status(context, 
2702                                         (context->actions == GDK_ACTION_COPY ?
2703                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2704         } else {
2705                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2706                 gdk_drag_status(context, 0, time);
2707         }
2708
2709         return acceptable;
2710 }
2711
2712 static void folderview_drag_leave_cb(GtkWidget      *widget,
2713                                      GdkDragContext *context,
2714                                      guint           time,
2715                                      FolderView     *folderview)
2716 {
2717         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2718 }
2719
2720 static void folderview_drag_received_cb(GtkWidget        *widget,
2721                                         GdkDragContext   *drag_context,
2722                                         gint              x,
2723                                         gint              y,
2724                                         GtkSelectionData *data,
2725                                         guint             info,
2726                                         guint             time,
2727                                         FolderView       *folderview)
2728 {
2729         gint row, column;
2730         FolderItem *item, *src_item;
2731         GtkCTreeNode *node;
2732
2733         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2734                 /* comes from summaryview */
2735                 if (gtk_clist_get_selection_info
2736                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2737                         return;
2738
2739                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2740                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2741                 src_item = folderview->summaryview->folder_item;
2742                 
2743                 /* re-check (due to acceptable possibly set for folder moves */
2744                 if (!(item && item->folder && item->path &&
2745                       src_item && src_item != item && 
2746                       (item->folder->type == F_MH || item->folder->type == F_IMAP))) {
2747                         return;
2748                 }
2749                 if (item && src_item) {
2750                         switch (drag_context->action) {
2751                                 case GDK_ACTION_COPY:
2752                                         summary_copy_selected_to(folderview->summaryview, item);
2753                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2754                                         break;
2755                                 case GDK_ACTION_MOVE:
2756                                 case GDK_ACTION_DEFAULT:
2757                                 default:
2758                         if (src_item->folder->type != item->folder->type ||
2759                             (item->folder->type == F_IMAP &&
2760                              src_item->folder != item->folder))
2761                                 summary_copy_selected_to(folderview->summaryview, item);
2762                         else
2763                                 summary_move_selected_to(folderview->summaryview, item);
2764                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2765                         }
2766                 } else
2767                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2768         } else {
2769                 /* comes from folderview */
2770                 char *source;
2771                 
2772                 source = data->data + 17;
2773                 if (gtk_clist_get_selection_info
2774                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2775                     || *source == 0) {
2776                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2777                         return;
2778                 }
2779                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2780                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2781                 src_item = folder_find_item_from_identifier(source);
2782
2783                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2784                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2785                         return;
2786                 }
2787
2788                 folderview_move_to(folderview, src_item, item);
2789                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2790         }
2791         folderview->nodes_to_recollapse = NULL;
2792 }
2793
2794 static gint folderview_clist_compare(GtkCList *clist,
2795                                      gconstpointer ptr1, gconstpointer ptr2)
2796 {
2797         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2798         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2799
2800         if (!item1->name)
2801                 return (item2->name != NULL);
2802         if (!item2->name)
2803                 return -1;
2804
2805         return g_strcasecmp(item1->name, item2->name);
2806 }
2807
2808 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2809 {
2810         const FolderItem *item = a;
2811         const gchar *name = b;
2812
2813         if (!item->path) return -1;
2814         return strcmp2(g_basename(item->path), name);
2815 }
2816
2817 static void folderview_scoring_cb(FolderView *folderview, guint action,
2818                                    GtkWidget *widget)
2819 {
2820         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2821         FolderItem *item;
2822
2823         if (!folderview->selected) return;
2824
2825         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2826         g_return_if_fail(item != NULL);
2827         g_return_if_fail(item->folder != NULL);
2828
2829         prefs_scoring_open(item);
2830 }
2831
2832 static void folderview_processing_cb(FolderView *folderview, guint action,
2833                                      GtkWidget *widget)
2834 {
2835         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2836         FolderItem *item;
2837
2838         if (!folderview->selected) return;
2839
2840         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2841         g_return_if_fail(item != NULL);
2842         g_return_if_fail(item->folder != NULL);
2843
2844         prefs_filtering_open(item, NULL, NULL);
2845 }
2846
2847 void folderview_set_target_folder_color(gint color_op) 
2848 {
2849         gint firstone = 1;
2850         GList *list;
2851         FolderView *folderview;
2852
2853         for (list = folderview_list; list != NULL; list = list->next) {
2854                 folderview = (FolderView *)list->data;
2855                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2856                 if (firstone) {
2857                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2858                                 folderview->color_op;
2859                         firstone = 0;
2860                 }
2861         }
2862 }
2863
2864 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2865 {
2866         folderview_init(folderview);
2867         folderview_set_all();
2868 }
2869
2870 GtkTargetEntry folderview_drag_types[1] =
2871 {
2872         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
2873 };
2874
2875
2876 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2877                                   FolderView       *folderview)
2878 {
2879         GtkTargetList *list;
2880         GdkDragContext *context;
2881
2882         g_return_if_fail(folderview != NULL);
2883         if (folderview->selected == NULL) return;
2884
2885         list = gtk_target_list_new(folderview_drag_types, 1);
2886
2887         folderview->nodes_to_recollapse = NULL; /* in case the last drag has been cancelled */
2888         
2889         context = gtk_drag_begin(widget, list,
2890                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2891         gtk_drag_set_icon_default(context);
2892 }
2893
2894 static void folderview_drag_data_get(GtkWidget        *widget,
2895                                      GdkDragContext   *drag_context,
2896                                      GtkSelectionData *selection_data,
2897                                      guint             info,
2898                                      guint             time,
2899                                      FolderView       *folderview)
2900 {
2901         FolderItem *item;
2902         GList *cur;
2903         gchar *source=NULL;
2904         
2905         for (cur = GTK_CLIST(folderview->ctree)->selection;
2906              cur != NULL; cur = cur->next) {
2907                 item = gtk_ctree_node_get_row_data
2908                         (GTK_CTREE(folderview->ctree), 
2909                          GTK_CTREE_NODE(cur->data));
2910                 if (item) {
2911                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2912                         gtk_selection_data_set(selection_data,
2913                                                selection_data->target, 8,
2914                                                source, strlen(source));
2915                         break;
2916                 } else
2917                         return;
2918         }
2919 }
2920