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