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