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