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