Fix my previous patch which did not compile after latest merge from main.
[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 (folder && folder != item->folder) continue;
857                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
858
859                         folderview_scan_tree_func(item->folder, item, NULL);
860                         if (folder_item_scan(item) < 0) {
861                                 if (folder && !FOLDER_IS_LOCAL(folder))
862                                         break;
863                         }
864                         folderview_update_node(folderview, node);
865                 }
866
867                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
868                 main_window_unlock(folderview->mainwin);
869                 inc_unlock();
870         }
871
872         folder_write_list();
873 }
874
875 static gboolean folderview_search_new_recursive(GtkCTree *ctree,
876                                                 GtkCTreeNode *node)
877 {
878         FolderItem *item;
879
880         if (node) {
881                 item = gtk_ctree_node_get_row_data(ctree, node);
882                 if (item) {
883                         if (item->new > 0 ||
884                             (item->stype == F_QUEUE && item->total > 0))
885                                 return TRUE;
886                 }
887                 node = GTK_CTREE_ROW(node)->children;
888         } else
889                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
890
891         while (node) {
892                 if (folderview_search_new_recursive(ctree, node) == TRUE)
893                         return TRUE;
894                 node = GTK_CTREE_ROW(node)->sibling;
895         }
896
897         return FALSE;
898 }
899
900 static gboolean folderview_have_new_children(FolderView *folderview,
901                                              GtkCTreeNode *node)
902 {
903         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
904
905         if (!node)
906                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
907         if (!node)
908                 return FALSE;
909
910         node = GTK_CTREE_ROW(node)->children;
911
912         while (node) {
913                 if (folderview_search_new_recursive(ctree, node) == TRUE)
914                         return TRUE;
915                 node = GTK_CTREE_ROW(node)->sibling;
916         }
917
918         return FALSE;
919 }
920
921 static gboolean folderview_search_unread_recursive(GtkCTree *ctree,
922                                                    GtkCTreeNode *node)
923 {
924         FolderItem *item;
925
926         if (node) {
927                 item = gtk_ctree_node_get_row_data(ctree, node);
928                 if (item) {
929                         if (item->unread > 0 ||
930                             (item->stype == F_QUEUE && item->total > 0))
931                                 return TRUE;
932                 }
933                 node = GTK_CTREE_ROW(node)->children;
934         } else
935                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
936
937         while (node) {
938                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
939                         return TRUE;
940                 node = GTK_CTREE_ROW(node)->sibling;
941         }
942
943         return FALSE;
944 }
945
946 static gboolean folderview_have_unread_children(FolderView *folderview,
947                                                 GtkCTreeNode *node)
948 {
949         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
950
951         if (!node)
952                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
953         if (!node)
954                 return FALSE;
955
956         node = GTK_CTREE_ROW(node)->children;
957
958         while (node) {
959                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
960                         return TRUE;
961                 node = GTK_CTREE_ROW(node)->sibling;
962         }
963
964         return FALSE;
965 }
966
967 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
968 {
969         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
970         GtkStyle *style = NULL;
971         FolderItem *item;
972         GdkPixmap *xpm, *openxpm;
973         GdkBitmap *mask, *openmask;
974         gchar *name;
975         gchar *str;
976         gboolean add_unread_mark;
977         gboolean use_bold, use_color;
978
979         item = gtk_ctree_node_get_row_data(ctree, node);
980         g_return_if_fail(item != NULL);
981
982         switch (item->stype) {
983         case F_INBOX:
984                 if (item->hide_read_msgs) {
985                         xpm = inboxhrmxpm;
986                         mask = inboxhrmxpmmask;
987                         openxpm = inboxopenhrmxpm;
988                         openmask = inboxopenhrmxpmmask;
989                 } else {
990                         xpm = inboxxpm;
991                         mask = inboxxpmmask;
992                         openxpm = inboxopenxpm;
993                         openmask = inboxopenxpmmask;
994                 }
995                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
996                                 !strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
997                                 item->name);
998                 break;
999         case F_OUTBOX:
1000                 if (item->hide_read_msgs) {
1001                         xpm = outboxhrmxpm;
1002                         mask = outboxhrmxpmmask;
1003                         openxpm = outboxopenhrmxpm;
1004                         openmask = outboxopenhrmxpmmask;
1005                 } else {
1006                         xpm = outboxxpm;
1007                         mask = outboxxpmmask;
1008                         openxpm = outboxopenxpm;
1009                         openmask = outboxopenxpmmask;
1010                 }
1011                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1012                                 !strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1013                                 item->name);
1014                 break;
1015         case F_QUEUE:
1016                 if (item->hide_read_msgs) {
1017                         xpm = queuehrmxpm;
1018                         mask = queuehrmxpmmask;
1019                         openxpm = queueopenhrmxpm;
1020                         openmask = queueopenhrmxpmmask;
1021                 } else {
1022                         xpm = queuexpm;
1023                         mask = queuexpmmask;
1024                         openxpm = queueopenxpm;
1025                         openmask = queueopenxpmmask;
1026                 }
1027                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1028                                 !strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1029                                 item->name);
1030                 break;
1031         case F_TRASH:
1032                 if (item->hide_read_msgs) {
1033                         xpm = trashhrmxpm;
1034                         mask = trashhrmxpmmask;
1035                         openxpm = trashopenhrmxpm;
1036                         openmask = trashopenhrmxpmmask;
1037                 } else {
1038                         xpm = trashxpm;
1039                         mask = trashxpmmask;
1040                         openxpm = trashopenxpm;
1041                         openmask = trashopenxpmmask;
1042                 }
1043                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1044                                 !strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1045                                 item->name);
1046                 break;
1047         case F_DRAFT:
1048                 xpm = folderxpm;
1049                 mask = folderxpmmask;
1050                 if (item->hide_read_msgs) {
1051                         openxpm = folderopenhrmxpm;
1052                         openmask = folderopenhrmxpmmask;
1053                 } else {
1054                         openxpm = folderopenxpm;
1055                         openmask = folderopenxpmmask;
1056                 }
1057                 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
1058                                 !strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1059                                 item->name);
1060                 break;
1061         default:
1062                 if (item->hide_read_msgs) {
1063                         xpm = folderhrmxpm;
1064                         mask = folderhrmxpmmask;
1065                         openxpm = folderopenhrmxpm;
1066                         openmask = folderopenhrmxpmmask;
1067                 } else {
1068                         xpm = folderxpm;
1069                         mask = folderxpmmask;
1070                         openxpm = folderopenxpm;
1071                         openmask = folderopenxpmmask;
1072                 }
1073                 if (!item->parent) {
1074                         switch (item->folder->type) {
1075                         case F_MH:
1076                                 name = " (MH)"; break;
1077                         case F_MBOX:
1078                                 name = " (mbox)"; break;
1079                         case F_IMAP:
1080                                 name = " (IMAP4)"; break;
1081                         case F_NEWS:
1082                                 name = " (News)"; break;
1083                         default:
1084                                 name = "";
1085                         }
1086                         name = g_strconcat(item->name, name, NULL);
1087                 } else {
1088                         if (item->folder->type == F_NEWS &&
1089                             !strcmp2(item->name, item->path))
1090                                 name = get_abbrev_newsgroup_name(item->path);
1091                         else
1092                                 name = g_strdup(item->name);
1093                 }
1094         }
1095
1096         if (!GTK_CTREE_ROW(node)->expanded &&
1097             folderview_have_unread_children(folderview, node))
1098                 add_unread_mark = TRUE;
1099         else
1100                 add_unread_mark = FALSE;
1101
1102         if (item->stype == F_QUEUE && item->total > 0 &&
1103             prefs_common.display_folder_unread) {
1104                 str = g_strdup_printf("%s (%d%s)", name, item->total,
1105                                       add_unread_mark ? "+" : "");
1106                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1107                                         xpm, mask, openxpm, openmask,
1108                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1109                 g_free(str);
1110         } else if ((item->unread > 0 || add_unread_mark) &&
1111                  prefs_common.display_folder_unread) {
1112
1113                 if (item->unread > 0)
1114                         str = g_strdup_printf("%s (%d%s)", name, item->unread,
1115                                               add_unread_mark ? "+" : "");
1116                 else
1117                         str = g_strdup_printf("%s (+)", name);
1118                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1119                                         xpm, mask, openxpm, openmask,
1120                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1121                 g_free(str);
1122         } else
1123                 gtk_ctree_set_node_info(ctree, node, name, FOLDER_SPACING,
1124                                         xpm, mask, openxpm, openmask,
1125                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1126         g_free(name);
1127
1128         if (!item->parent) {
1129                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1130                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1131                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1132         } else {
1133                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
1134                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
1135                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
1136         }
1137
1138         if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1139             item->stype == F_TRASH) {
1140                 use_bold = use_color = FALSE;
1141         } else if (item->stype == F_QUEUE) {
1142                 /* highlight queue folder if there are any messages */
1143                 use_bold = use_color = (item->total > 0);
1144         } else {
1145                 /* if unread messages exist, print with bold font */
1146                 use_bold = (item->unread > 0) || add_unread_mark;
1147                 /* if new messages exist, print with colored letter */
1148                 use_color =
1149                         (item->new > 0) ||
1150                         (add_unread_mark &&
1151                          folderview_have_new_children(folderview, node));
1152         }
1153
1154         gtk_ctree_node_set_foreground(ctree, node, NULL);
1155
1156         if (use_bold && use_color)
1157                 style = bold_color_style;
1158         else if (use_bold) {
1159                 style = bold_style;
1160                 if (item->op_count > 0) {
1161                         style = bold_tgtfold_style;
1162                 }
1163         }
1164         else if (use_color) {
1165                 style = normal_color_style;
1166                 gtk_ctree_node_set_foreground(ctree, node,
1167                                               &folderview->color_new);
1168         }
1169         else if (item->op_count > 0) {
1170                 style = bold_tgtfold_style;
1171         } else {
1172                 style = normal_style;
1173         }
1174
1175         gtk_ctree_node_set_row_style(ctree, node, style);
1176
1177         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1178                 folderview_update_node(folderview, node);
1179 }
1180
1181 void folderview_update_item(FolderItem *item, gboolean update_summary)
1182 {
1183         GList *list;
1184         FolderView *folderview;
1185         GtkCTree *ctree;
1186         GtkCTreeNode *node;
1187
1188         g_return_if_fail(item != NULL);
1189
1190         for (list = folderview_list; list != NULL; list = list->next) {
1191                 folderview = (FolderView *)list->data;
1192                 ctree = GTK_CTREE(folderview->ctree);
1193
1194                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1195                 if (node) {
1196                         folderview_update_node(folderview, node);
1197                         if (update_summary && folderview->opened == node)
1198                                 summary_show(folderview->summaryview,
1199                                              item, FALSE);
1200                 }
1201         }
1202 }
1203
1204 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1205                                                 gpointer data)
1206 {
1207         folderview_update_item((FolderItem *)key, FALSE);
1208 }
1209
1210 void folderview_update_item_foreach(GHashTable *table)
1211 {
1212         g_hash_table_foreach(table, folderview_update_item_foreach_func, NULL);
1213 }
1214
1215 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1216                                       GNode *gnode, GtkCTreeNode *cnode,
1217                                       gpointer data)
1218 {
1219         FolderView *folderview = (FolderView *)data;
1220         FolderItem *item = FOLDER_ITEM(gnode->data);
1221
1222         g_return_val_if_fail(item != NULL, FALSE);
1223
1224         gtk_ctree_node_set_row_data(ctree, cnode, item);
1225         folderview_update_node(folderview, cnode);
1226
1227         return TRUE;
1228 }
1229
1230 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1231                                    gpointer data)
1232 {
1233         FolderView *folderview = (FolderView *)data;
1234         FolderItem *item;
1235
1236         if (GTK_CTREE_ROW(node)->children) {
1237                 item = gtk_ctree_node_get_row_data(ctree, node);
1238                 g_return_if_fail(item != NULL);
1239
1240                 if (!item->collapsed)
1241                         gtk_ctree_expand(ctree, node);
1242                 else
1243                         folderview_update_node(folderview, node);
1244         }
1245 }
1246
1247 #define SET_SPECIAL_FOLDER(ctree, item) \
1248 { \
1249         if (item) { \
1250                 GtkCTreeNode *node, *parent, *sibling; \
1251  \
1252                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
1253                 if (!node) \
1254                         g_warning("%s not found.\n", item->path); \
1255                 else { \
1256                         parent = GTK_CTREE_ROW(node)->parent; \
1257                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
1258                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
1259                         else \
1260                                 sibling = GTK_CTREE_ROW(parent)->children; \
1261                         if (node != sibling) \
1262                                 gtk_ctree_move(ctree, node, parent, sibling); \
1263                 } \
1264  \
1265                 prev = node; \
1266         } \
1267 }
1268
1269 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1270                                     Folder *folder)
1271 {
1272         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1273         GtkCTreeNode *prev = NULL;
1274
1275         gtk_ctree_sort_recursive(ctree, root);
1276
1277         if (GTK_CTREE_ROW(root)->parent) return;
1278
1279         SET_SPECIAL_FOLDER(ctree, folder->inbox);
1280         SET_SPECIAL_FOLDER(ctree, folder->outbox);
1281         SET_SPECIAL_FOLDER(ctree, folder->draft);
1282         SET_SPECIAL_FOLDER(ctree, folder->queue);
1283         SET_SPECIAL_FOLDER(ctree, folder->trash);
1284 }
1285
1286 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1287 {
1288         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1289         GtkCTreeNode *root;
1290
1291         g_return_if_fail(folder != NULL);
1292
1293         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1294                                       folderview_gnode_func, folderview);
1295         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1296                                 folderview);
1297         folderview_sort_folders(folderview, root, folder);
1298 }
1299
1300 void folderview_new_folder(FolderView *folderview)
1301 {
1302         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1303         FolderItem *item;
1304
1305         if (!folderview->selected) return;
1306
1307         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1308         g_return_if_fail(item != NULL);
1309         g_return_if_fail(item->folder != NULL);
1310
1311         switch (item->folder->type) {
1312         case F_MBOX:
1313                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1314                 break;
1315         case F_MH:
1316         case F_MAILDIR:
1317                 folderview_new_folder_cb(folderview, 0, NULL);
1318                 break;
1319         case F_IMAP:
1320                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1321                 break;
1322         case F_NEWS:
1323         default:
1324                 break;
1325         }
1326 }
1327
1328 void folderview_rename_folder(FolderView *folderview)
1329 {
1330         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1331         FolderItem *item;
1332
1333         if (!folderview->selected) return;
1334
1335         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1336         g_return_if_fail(item != NULL);
1337         g_return_if_fail(item->folder != NULL);
1338         if (!item->path) return;
1339         if (item->stype != F_NORMAL) return;
1340
1341         switch (item->folder->type) {
1342         case F_MBOX:
1343                 folderview_rename_mbox_folder_cb(folderview, 0, NULL);
1344         case F_MH:
1345         case F_MAILDIR:
1346         case F_IMAP:
1347                 folderview_rename_folder_cb(folderview, 0, NULL);
1348                 break;
1349         case F_NEWS:
1350         default:
1351                 break;
1352         }
1353 }
1354
1355 void folderview_delete_folder(FolderView *folderview)
1356 {
1357         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1358         FolderItem *item;
1359
1360         if (!folderview->selected) return;
1361
1362         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1363         g_return_if_fail(item != NULL);
1364         g_return_if_fail(item->folder != NULL);
1365         if (!item->path) return;
1366         if (item->stype != F_NORMAL) return;
1367
1368         switch (item->folder->type) {
1369         case F_MH:
1370         case F_MBOX:
1371         case F_MAILDIR:
1372         case F_IMAP:
1373                 folderview_delete_folder_cb(folderview, 0, NULL);
1374                 break;
1375         case F_NEWS:
1376         default:
1377                 break;
1378         }
1379 }
1380
1381
1382 /* callback functions */
1383
1384 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1385                                       FolderView *folderview)
1386 {
1387         GtkCList *clist = GTK_CLIST(ctree);
1388         gint prev_row = -1, row = -1, column = -1;
1389         FolderItem *item;
1390         Folder *folder;
1391         GtkWidget *popup;
1392         gboolean mark_all_read   = FALSE;
1393         gboolean new_folder      = FALSE;
1394         gboolean rename_folder   = FALSE;
1395         gboolean delete_folder   = FALSE;
1396         gboolean update_tree     = FALSE;
1397         gboolean rescan_tree     = FALSE;
1398         gboolean remove_tree     = FALSE;
1399         gboolean search_folder   = FALSE;
1400         gboolean folder_property = FALSE;
1401         gboolean folder_processing  = FALSE;
1402         gboolean folder_scoring  = FALSE;
1403
1404         if (!event) return;
1405
1406         if (event->button == 1) {
1407                 folderview->open_folder = TRUE;
1408                 return;
1409         }
1410
1411         if (event->button == 2 || event->button == 3) {
1412                 /* right clicked */
1413                 if (clist->selection) {
1414                         GtkCTreeNode *node;
1415
1416                         node = GTK_CTREE_NODE(clist->selection->data);
1417                         if (node)
1418                                 prev_row = gtkut_ctree_get_nth_from_node
1419                                         (GTK_CTREE(ctree), node);
1420                 }
1421
1422                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1423                                                   &row, &column))
1424                         return;
1425                 if (prev_row != row) {
1426                         gtk_clist_unselect_all(clist);
1427                         if (event->button == 2)
1428                                 folderview_select_node
1429                                         (folderview,
1430                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1431                                                             row));
1432                         else
1433                                 gtk_clist_select_row(clist, row, column);
1434                 }
1435         }
1436
1437         if (event->button != 3) return;
1438
1439         item = gtk_clist_get_row_data(clist, row);
1440         g_return_if_fail(item != NULL);
1441         g_return_if_fail(item->folder != NULL);
1442         folder = item->folder;
1443
1444         if (folderview->mainwin->lock_count == 0) {
1445                 new_folder = TRUE;
1446                 if (item->parent == NULL)
1447                         update_tree = remove_tree = TRUE;
1448                 else
1449                         mark_all_read = search_folder = folder_property = TRUE;
1450                 if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
1451                         if (item->parent == NULL)
1452                                 update_tree = rescan_tree = TRUE;
1453                         else if (item->stype == F_NORMAL)
1454                                 rename_folder = delete_folder = folder_scoring = folder_processing = TRUE;
1455                         else if (item->stype == F_INBOX)
1456                                 folder_scoring = folder_processing = TRUE;
1457                         else if (item->stype == F_TRASH)
1458                                 folder_processing = TRUE;
1459                         else if (item->stype == F_OUTBOX)
1460                                 folder_processing = TRUE;
1461                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
1462                         if (item->parent != NULL)
1463                                 delete_folder = folder_scoring = folder_processing = TRUE;
1464                 }
1465                 if (item->unread < 1) 
1466                         mark_all_read = FALSE;
1467         }
1468
1469 #define SET_SENS(factory, name, sens) \
1470         menu_set_sensitive(folderview->factory, name, sens)
1471         
1472         mark_all_read = mark_all_read && 
1473                         (item == folderview->summaryview->folder_item);
1474
1475         if (FOLDER_IS_LOCAL(folder)) {
1476                 popup = folderview->mail_popup;
1477                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1478                 SET_SENS(mail_factory, "/Mark all read", mark_all_read);
1479                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1480                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1481                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1482                 SET_SENS(mail_factory, "/Check for new messages", update_tree);
1483                 SET_SENS(mail_factory, "/Rescan folder tree", rescan_tree);
1484                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1485                 SET_SENS(mail_factory, "/Search folder...", search_folder);
1486                 SET_SENS(mail_factory, "/Property...", folder_property);
1487                 SET_SENS(mail_factory, "/Processing...", folder_processing);
1488                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1489         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1490                 popup = folderview->imap_popup;
1491                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1492                 SET_SENS(imap_factory, "/Mark all read", mark_all_read);
1493                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1494                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1495                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1496                 SET_SENS(imap_factory, "/Check for new messages", update_tree);
1497                 SET_SENS(imap_factory, "/Rescan folder tree", rescan_tree);
1498                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1499                 SET_SENS(imap_factory, "/Search folder...", search_folder);
1500                 SET_SENS(imap_factory, "/Property...", folder_property);
1501                 SET_SENS(imap_factory, "/Processing...", folder_processing);
1502                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1503         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1504                 popup = folderview->news_popup;
1505                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1506                 SET_SENS(news_factory, "/Mark all read", mark_all_read);
1507                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1508                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1509 #if 0
1510                 SET_SENS(news_factory, "/Check for new messages", update_tree);
1511 #endif
1512                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1513                 SET_SENS(news_factory, "/Search folder...", search_folder);
1514                 SET_SENS(news_factory, "/Property...", folder_property);
1515                 SET_SENS(news_factory, "/Processing...", folder_processing);
1516                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1517         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1518                 popup = folderview->mbox_popup;
1519                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1520                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1521                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1522                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1523                 SET_SENS(news_factory, "/Property...", folder_property);
1524                 SET_SENS(mbox_factory, "/Processing...", folder_processing);
1525                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1526         } else
1527                 return;
1528
1529 #undef SET_SENS
1530
1531         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1532                        event->button, event->time);
1533 }
1534
1535 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1536                                        FolderView *folderview)
1537 {
1538         if (!event) return;
1539
1540         if (event->button == 1 && folderview->open_folder == FALSE &&
1541             folderview->opened != NULL) {
1542                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1543                                           folderview->opened);
1544                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1545         }
1546 }
1547
1548 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1549                                    FolderView *folderview)
1550 {
1551         if (!event) return;
1552
1553         switch (event->keyval) {
1554         case GDK_Return:
1555                 if (folderview->selected) {
1556                         folderview_select_node(folderview,
1557                                                folderview->selected);
1558                 }
1559                 break;
1560         case GDK_space:
1561                 if (folderview->selected) {
1562                         if (folderview->opened == folderview->selected &&
1563                             folderview->summaryview->messages == 0)
1564                                 folderview_select_next_unread(folderview);
1565                         else
1566                                 folderview_select_node(folderview,
1567                                                        folderview->selected);
1568                 }
1569                 break;
1570         default:
1571                 break;
1572         }
1573 }
1574
1575 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1576                                 gint column, FolderView *folderview)
1577 {
1578         static gboolean can_select = TRUE;      /* exclusive lock */
1579         gboolean opened;
1580         FolderItem *item;
1581
1582         folderview->selected = row;
1583
1584         if (folderview->opened == row) {
1585                 folderview->open_folder = FALSE;
1586                 return;
1587         }
1588
1589         if (!can_select) {
1590                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1591                 gtk_ctree_select(ctree, folderview->opened);
1592                 return;
1593         }
1594
1595         if (!folderview->open_folder) return;
1596
1597         item = gtk_ctree_node_get_row_data(ctree, row);
1598         if (!item) return;
1599
1600         can_select = FALSE;
1601
1602         /* CLAWS: set compose button type: news folder items 
1603          * always have a news folder as parent */
1604         if (item->folder) 
1605                 main_window_toolbar_set_compose_button
1606                         (folderview->mainwin,
1607                          item->folder->type == F_NEWS ? 
1608                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1609          
1610         if (item->path)
1611                 debug_print(_("Folder %s is selected\n"), item->path);
1612
1613         if (!GTK_CTREE_ROW(row)->children)
1614                 gtk_ctree_expand(ctree, row);
1615         if (folderview->opened &&
1616             !GTK_CTREE_ROW(folderview->opened)->children)
1617                 gtk_ctree_collapse(ctree, folderview->opened);
1618
1619         /* ungrab the mouse event */
1620         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1621                 gtk_grab_remove(GTK_WIDGET(ctree));
1622                 if (gdk_pointer_is_grabbed())
1623                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1624         }
1625
1626         opened = summary_show(folderview->summaryview, item, FALSE);
1627
1628         if (!opened) {
1629                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1630                 gtk_ctree_select(ctree, folderview->opened);
1631         } else {
1632                 folderview->opened = row;
1633                 if (gtk_ctree_node_is_visible(ctree, row)
1634                     != GTK_VISIBILITY_FULL)
1635                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1636         }
1637
1638         folderview->open_folder = FALSE;
1639         can_select = TRUE;
1640 }
1641
1642 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1643                                      FolderView *folderview)
1644 {
1645         FolderItem *item;
1646
1647         item = gtk_ctree_node_get_row_data(ctree, node);
1648         g_return_if_fail(item != NULL);
1649         item->collapsed = FALSE;
1650         folderview_update_node(folderview, node);
1651 }
1652
1653 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1654                                       FolderView *folderview)
1655 {
1656         FolderItem *item;
1657
1658         item = gtk_ctree_node_get_row_data(ctree, node);
1659         g_return_if_fail(item != NULL);
1660         item->collapsed= TRUE;
1661         folderview_update_node(folderview, node);
1662 }
1663
1664 static void folderview_popup_close(GtkMenuShell *menu_shell,
1665                                    FolderView *folderview)
1666 {
1667         if (!folderview->opened) return;
1668
1669         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1670                                   folderview->opened);
1671         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1672 }
1673
1674 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1675                                    FolderView *folderview)
1676 {
1677         switch (column) {
1678         case COL_FOLDER:
1679                 prefs_common.folder_col_folder = width;
1680                 break;
1681         case COL_NEW:
1682                 prefs_common.folder_col_new = width;
1683                 break;
1684         case COL_UNREAD:
1685                 prefs_common.folder_col_unread = width;
1686                 break;
1687         case COL_TOTAL:
1688                 prefs_common.folder_col_total = width;
1689                 break;
1690         default:
1691                 break;
1692         }
1693 }
1694
1695 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1696                                              GtkCTreeNode *node,
1697                                              const gchar *name)
1698 {
1699         FolderItem *item;
1700
1701         if (!node)
1702                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1703         if (!node)
1704                 return NULL;
1705
1706         node = GTK_CTREE_ROW(node)->children;
1707
1708         while (node) {
1709                 item = gtk_ctree_node_get_row_data(ctree, node);
1710                 if (!folderview_compare_name(item, name))
1711                         return node;
1712                 node = GTK_CTREE_ROW(node)->sibling;
1713         }
1714
1715         return NULL;
1716 }
1717
1718 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1719                                       GtkWidget *widget)
1720 {
1721         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1722         FolderItem *item;
1723
1724         if (!folderview->selected) return;
1725
1726         summary_show(folderview->summaryview, NULL, FALSE);
1727
1728         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1729         g_return_if_fail(item != NULL);
1730         g_return_if_fail(item->folder != NULL);
1731
1732         if (action == 0)
1733                 folderview_check_new(item->folder);
1734         else
1735                 folderview_rescan_tree(item->folder);
1736 }
1737
1738 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1739                                      GtkWidget *widget)
1740 {
1741         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1742         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1743         FolderItem *item;
1744         FolderItem *new_item;
1745         gchar *new_folder;
1746         GtkCTreeNode *node;
1747
1748         if (!folderview->selected) return;
1749
1750         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1751         g_return_if_fail(item != NULL);
1752         g_return_if_fail(item->folder != NULL);
1753
1754         new_folder = input_dialog(_("New folder"),
1755                                   _("Input the name of new folder:"),
1756                                   _("NewFolder"));
1757         if (!new_folder) return;
1758
1759         if (item->folder->type != F_MBOX) {
1760                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1761                         alertpanel_error(_("`%c' can't be included in folder name."),
1762                                          G_DIR_SEPARATOR);
1763                         g_free(new_folder);
1764                         return;
1765                 }
1766         }
1767
1768         /* find whether the directory already exists */
1769         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1770                 alertpanel_error(_("The folder `%s' already exists."),
1771                                  new_folder);
1772                 g_free(new_folder);
1773                 return;
1774         }
1775
1776         new_item = item->folder->create_folder(item->folder, item, new_folder);
1777         if (!new_item) {
1778                 alertpanel_error(_("The folder `%s' could not be created."), 
1779                                  new_folder);
1780                 g_free(new_folder);
1781                 return;
1782         } 
1783         g_free(new_folder);
1784
1785         gtk_clist_freeze(GTK_CLIST(ctree));
1786
1787         text[COL_FOLDER] = new_item->name;
1788         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1789                                      FOLDER_SPACING,
1790                                      folderxpm, folderxpmmask,
1791                                      folderopenxpm, folderopenxpmmask,
1792                                      FALSE, FALSE);
1793         gtk_ctree_expand(ctree, folderview->selected);
1794         gtk_ctree_node_set_row_data(ctree, node, new_item);
1795         folderview_sort_folders(folderview, folderview->selected, item->folder);
1796
1797         gtk_clist_thaw(GTK_CLIST(ctree));
1798
1799         folder_write_list();
1800 }
1801
1802 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1803                                           GtkWidget *widget)
1804 {
1805         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1806         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1807         FolderItem *item;
1808         FolderItem *new_item;
1809         gchar *new_folder;
1810         GtkCTreeNode *node;
1811
1812         if (!folderview->selected) return;
1813
1814         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1815         g_return_if_fail(item != NULL);
1816         g_return_if_fail(item->folder != NULL);
1817
1818         new_folder = input_dialog(_("New folder"),
1819                                   _("Input the name of new folder:"),
1820                                   _("NewFolder"));
1821         if (!new_folder) return;
1822
1823         /* find whether the directory already exists */
1824         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1825                 alertpanel_error(_("The folder `%s' already exists."),
1826                                  new_folder);
1827                 g_free(new_folder);
1828                 return;
1829         }
1830
1831         new_item = item->folder->create_folder(item->folder, item, new_folder);
1832         g_free(new_folder);
1833         if (!new_item) return;
1834
1835         gtk_clist_freeze(GTK_CLIST(ctree));
1836
1837         text[COL_FOLDER] = new_item->name;
1838         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1839                                      FOLDER_SPACING,
1840                                      folderxpm, folderxpmmask,
1841                                      folderopenxpm, folderopenxpmmask,
1842                                      FALSE, FALSE);
1843         gtk_ctree_expand(ctree, folderview->selected);
1844         gtk_ctree_node_set_row_data(ctree, node, new_item);
1845         folderview_sort_folders(folderview, folderview->selected, item->folder);
1846
1847         gtk_clist_thaw(GTK_CLIST(ctree));
1848
1849         folder_write_list();
1850 }
1851
1852 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1853                                         GtkWidget *widget)
1854 {
1855         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1856         FolderItem *item;
1857         gchar *new_folder;
1858         gchar *message;
1859         gchar *old_path;
1860         gchar *old_id;
1861         gchar *new_id;
1862
1863         if (!folderview->selected) return;
1864
1865         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1866         g_return_if_fail(item != NULL);
1867         g_return_if_fail(item->path != NULL);
1868         g_return_if_fail(item->folder != NULL);
1869
1870         message = g_strdup_printf(_("Input new name for `%s':"),
1871                                   g_basename(item->path));
1872         new_folder = input_dialog(_("Rename folder"), message,
1873                                   g_basename(item->path));
1874         g_free(message);
1875         if (!new_folder) return;
1876
1877         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1878                 alertpanel_error(_("`%c' can't be included in folder name."),
1879                                  G_DIR_SEPARATOR);
1880                 g_free(new_folder);
1881                 return;
1882         }
1883
1884         if (folderview_find_by_name
1885                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1886                  new_folder)) {
1887                 alertpanel_error(_("The folder `%s' already exists."),
1888                                  new_folder);
1889                 g_free(new_folder);
1890                 return;
1891         }
1892
1893         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
1894         old_id = folder_item_get_identifier(item);
1895
1896         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1897                 g_free(old_id);
1898                 g_free(new_folder);
1899                 return;
1900         }
1901         g_free(new_folder);
1902
1903         if (prefs_common.fltlist) {
1904                 if (folder_get_default_folder() == item->folder)
1905                         prefs_filter_rename_path(old_path, item->path);
1906                 new_id = folder_item_get_identifier(item);
1907                 prefs_filter_rename_path(old_id, new_id);
1908         } else {
1909                 if (FOLDER_TYPE(item->folder) == F_MH)
1910                         prefs_filtering_rename_path(old_path, item->path);
1911                 new_id = folder_item_get_identifier(item);
1912                 prefs_filtering_rename_path(old_id, new_id);
1913         }
1914         g_free(old_id);
1915         g_free(new_id);
1916
1917         gtk_clist_freeze(GTK_CLIST(ctree));
1918
1919         folderview_update_node(folderview, folderview->selected);
1920         folderview_sort_folders(folderview,
1921                                 GTK_CTREE_ROW(folderview->selected)->parent,
1922                                 item->folder);
1923         if (folderview->opened == folderview->selected ||
1924             gtk_ctree_is_ancestor(ctree,
1925                                   folderview->selected,
1926                                   folderview->opened)) {
1927                 GtkCTreeNode *node = folderview->opened;
1928                 folderview_unselect(folderview);
1929                 folderview_select_node(folderview, node);
1930         }
1931
1932         gtk_clist_thaw(GTK_CLIST(ctree));
1933
1934         folder_write_list();
1935 }
1936
1937 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
1938                                              guint action,
1939                                              GtkWidget *widget)
1940 {
1941         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1942         FolderItem *item;
1943         gchar *new_folder;
1944         gchar *message;
1945
1946         if (!folderview->selected) return;
1947
1948         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1949         g_return_if_fail(item != NULL);
1950         g_return_if_fail(item->path != NULL);
1951         g_return_if_fail(item->folder != NULL);
1952
1953         message = g_strdup_printf(_("Input new name for `%s':"),
1954                                   g_basename(item->path));
1955         new_folder = input_dialog(_("Rename folder"), message,
1956                                   g_basename(item->path));
1957         g_free(message);
1958         if (!new_folder) return;
1959
1960         if (folderview_find_by_name
1961                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1962                  new_folder)) {
1963                 alertpanel_error(_("The folder `%s' already exists."),
1964                                  new_folder);
1965                 g_free(new_folder);
1966                 return;
1967         }
1968
1969         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1970                 g_free(new_folder);
1971                 return;
1972         }
1973         g_free(new_folder);
1974
1975         gtk_clist_freeze(GTK_CLIST(ctree));
1976
1977         folderview_update_node(folderview, folderview->selected);
1978         folderview_sort_folders(folderview,
1979                                 GTK_CTREE_ROW(folderview->selected)->parent,
1980                                 item->folder);
1981         if (folderview->opened == folderview->selected) {
1982                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1983                         gtk_ctree_expand(ctree, folderview->opened);
1984                 summary_show(folderview->summaryview, item, FALSE);
1985         }
1986
1987         gtk_clist_thaw(GTK_CLIST(ctree));
1988
1989         folder_write_list();
1990 }
1991
1992 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1993                                         GtkWidget *widget)
1994 {
1995         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1996         FolderItem *item;
1997         gchar *message;
1998         AlertValue avalue;
1999         gchar *old_path;
2000         gchar *old_id;
2001
2002         if (!folderview->selected) return;
2003
2004         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2005         g_return_if_fail(item != NULL);
2006         g_return_if_fail(item->path != NULL);
2007         g_return_if_fail(item->folder != NULL);
2008
2009         message = g_strdup_printf
2010                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2011                    "Do you really want to delete?"),
2012                  g_basename(item->path));
2013         avalue = alertpanel(_("Delete folder"), message,
2014                             _("Yes"), _("+No"), NULL);
2015         g_free(message);
2016         if (avalue != G_ALERTDEFAULT) return;
2017
2018         Xstrdup_a(old_path, item->path, return);
2019         old_id = folder_item_get_identifier(item);
2020
2021         if (item->folder->remove_folder(item->folder, item) < 0) {
2022                 alertpanel_error(_("Can't remove the folder `%s'."),
2023                                  item->path);
2024                 if (folderview->opened == folderview->selected)
2025                         summary_show(folderview->summaryview,
2026                                      folderview->summaryview->folder_item,
2027                                      FALSE);
2028                 g_free(old_id);
2029                 return;
2030         }
2031
2032         if (prefs_common.fltlist) {
2033                 if (folder_get_default_folder() == item->folder)
2034                         prefs_filter_delete_path(old_path);
2035                 prefs_filter_delete_path(old_id);
2036                 g_free(old_id);
2037         } else {
2038                 if (FOLDER_TYPE(item->folder) == F_MH)
2039                         prefs_filtering_delete_path(old_path);
2040                 prefs_filtering_delete_path(old_id);
2041                 g_free(old_id);
2042         }
2043         if (folderview->opened == folderview->selected ||
2044             gtk_ctree_is_ancestor(ctree,
2045                                   folderview->selected,
2046                                   folderview->opened)) {
2047                 summary_clear_all(folderview->summaryview);
2048                 folderview->opened = NULL;
2049         }
2050
2051         gtk_ctree_remove_node(ctree, folderview->selected);
2052         folder_write_list();
2053 }
2054
2055 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2056                                          GtkWidget *widget)
2057 {
2058         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2059         GtkCTreeNode *node;
2060         FolderItem *item;
2061         gchar *message;
2062         AlertValue avalue;
2063
2064         if (!folderview->selected) return;
2065         node = folderview->selected;
2066         item = gtk_ctree_node_get_row_data(ctree, node);
2067         g_return_if_fail(item != NULL);
2068         g_return_if_fail(item->folder != NULL);
2069         if (item->parent) return;
2070
2071         message = g_strdup_printf
2072                 (_("Really remove the mailbox `%s' ?\n"
2073                    "(The messages are NOT deleted from disk)"),
2074                  item->folder->name);
2075         avalue = alertpanel(_("Remove folder"), message,
2076                             _("Yes"), _("+No"), NULL);
2077         g_free(message);
2078         if (avalue != G_ALERTDEFAULT) return;
2079
2080         folder_destroy(item->folder);
2081         summary_clear_all(folderview->summaryview);
2082         folderview_unselect(folderview);
2083         gtk_ctree_remove_node(ctree, node);
2084         folder_write_list();
2085 }
2086
2087 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2088                                           GtkWidget *widget)
2089 {
2090         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2091         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2092         GtkCTreeNode *node;
2093         FolderItem *item;
2094         FolderItem *new_item;
2095         gchar *new_folder;
2096         gchar *p;
2097
2098         if (!folderview->selected) return;
2099
2100         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2101         g_return_if_fail(item != NULL);
2102         g_return_if_fail(item->folder != NULL);
2103         g_return_if_fail(item->folder->type == F_IMAP);
2104         g_return_if_fail(item->folder->account != NULL);
2105
2106         new_folder = input_dialog
2107                 (_("New folder"),
2108                  _("Input the name of new folder:\n"
2109                    "(if you want to create a folder to store subfolders,\n"
2110                    " append `/' at the end of the name)"),
2111                  _("NewFolder"));
2112         if (!new_folder) return;
2113
2114         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2115             *(p + 1) != '\0') {
2116                 alertpanel_error(_("`%c' can't be included in folder name."),
2117                                  G_DIR_SEPARATOR);
2118                 g_free(new_folder);
2119                 return;
2120         }
2121
2122         /* find whether the directory already exists */
2123         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2124                 alertpanel_error(_("The folder `%s' already exists."),
2125                                  new_folder);
2126                 g_free(new_folder);
2127                 return;
2128         }
2129
2130         new_item = item->folder->create_folder(item->folder, item, new_folder);
2131         if (!new_item) {
2132                 alertpanel_error(_("Can't create the folder `%s'."),
2133                                  new_folder);
2134                 g_free(new_folder);
2135                 return;
2136         }
2137         g_free(new_folder);
2138
2139         gtk_clist_freeze(GTK_CLIST(ctree));
2140
2141         text[COL_FOLDER] = new_item->name;
2142         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
2143                                      FOLDER_SPACING,
2144                                      folderxpm, folderxpmmask,
2145                                      folderopenxpm, folderopenxpmmask,
2146                                      FALSE, FALSE);
2147         gtk_ctree_expand(ctree, folderview->selected);
2148         gtk_ctree_node_set_row_data(ctree, node, new_item);
2149         folderview_sort_folders(folderview, folderview->selected, item->folder);
2150
2151         gtk_clist_thaw(GTK_CLIST(ctree));
2152
2153         folder_write_list();
2154 }
2155
2156 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2157                                          GtkWidget *widget)
2158 {
2159         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2160         FolderItem *item;
2161         gchar *message;
2162         AlertValue avalue;
2163
2164         if (!folderview->selected) return;
2165
2166         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2167         g_return_if_fail(item != NULL);
2168         g_return_if_fail(item->folder != NULL);
2169         g_return_if_fail(item->folder->type == F_IMAP);
2170         g_return_if_fail(item->folder->account != NULL);
2171
2172         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"),
2173                                   item->folder->name);
2174         avalue = alertpanel(_("Delete IMAP4 account"), message,
2175                             _("Yes"), _("+No"), NULL);
2176         g_free(message);
2177
2178         if (avalue != G_ALERTDEFAULT) return;
2179
2180         if (folderview->opened == folderview->selected ||
2181             gtk_ctree_is_ancestor(ctree,
2182                                   folderview->selected,
2183                                   folderview->opened)) {
2184                 summary_clear_all(folderview->summaryview);
2185                 folderview->opened = NULL;
2186         }
2187
2188         account_destroy(item->folder->account);
2189         folder_destroy(item->folder);
2190         gtk_ctree_remove_node(ctree, folderview->selected);
2191         account_set_menu();
2192         main_window_reflect_prefs_all();
2193         folder_write_list();
2194 }
2195
2196 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2197                                          GtkWidget *widget)
2198 {
2199         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2200         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2201         GtkCTreeNode *servernode, *node;
2202         Folder *folder;
2203         FolderItem *item;
2204         FolderItem *rootitem;
2205         FolderItem *newitem;
2206         GSList *new_subscr;
2207         GSList *cur;
2208         GNode *gnode;
2209
2210         if (!folderview->selected) return;
2211
2212         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2213         g_return_if_fail(item != NULL);
2214         folder = item->folder;
2215         g_return_if_fail(folder != NULL);
2216         g_return_if_fail(folder->type == F_NEWS);
2217         g_return_if_fail(folder->account != NULL);
2218
2219         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2220                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2221         else
2222                 servernode = folderview->selected;
2223
2224         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2225
2226         new_subscr = grouplist_dialog(folder);
2227
2228         /* remove unsubscribed newsgroups */
2229         for (gnode = folder->node->children; gnode != NULL; ) {
2230                 GNode *next = gnode->next;
2231
2232                 item = FOLDER_ITEM(gnode->data);
2233                 if (g_slist_find_custom(new_subscr, item->path,
2234                                         (GCompareFunc)g_strcasecmp) != NULL) {
2235                         gnode = next;
2236                         continue;
2237                 }
2238
2239                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2240                 if (!node) {
2241                         gnode = next;
2242                         continue;
2243                 }
2244
2245                 if (folderview->opened == node) {
2246                         summary_clear_all(folderview->summaryview);
2247                         folderview->opened = NULL;
2248                 }
2249
2250                 folder_item_remove(item);
2251                 gtk_ctree_remove_node(ctree, node);
2252
2253                 gnode = next;
2254         }
2255
2256         gtk_clist_freeze(GTK_CLIST(ctree));
2257
2258         /* add subscribed newsgroups */
2259         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2260                 gchar *name = (gchar *)cur->data;
2261
2262                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2263                         continue;
2264
2265                 text[COL_FOLDER] = name;
2266                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2267                                              FOLDER_SPACING,
2268                                              folderxpm, folderxpmmask,
2269                                              folderopenxpm, folderopenxpmmask,
2270                                              FALSE, FALSE);
2271                 gtk_ctree_expand(ctree, servernode);
2272
2273                 newitem = folder_item_new(name, name);
2274                 folder_item_append(rootitem, newitem);
2275                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2276         }
2277
2278         gtk_ctree_sort_node(ctree, servernode);
2279         gtk_clist_thaw(GTK_CLIST(ctree));
2280
2281         slist_free_strings(new_subscr);
2282         g_slist_free(new_subscr);
2283
2284         folder_write_list();
2285 }
2286
2287 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2288                                         GtkWidget *widget)
2289 {
2290         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2291         FolderItem *item;
2292         gchar *message;
2293         AlertValue avalue;
2294
2295         if (!folderview->selected) return;
2296
2297         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2298         g_return_if_fail(item != NULL);
2299         g_return_if_fail(item->folder != NULL);
2300         g_return_if_fail(item->folder->type == F_NEWS);
2301         g_return_if_fail(item->folder->account != NULL);
2302
2303         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
2304                                   g_basename(item->path));
2305         avalue = alertpanel(_("Delete newsgroup"), message,
2306                             _("Yes"), _("+No"), NULL);
2307         g_free(message);
2308         if (avalue != G_ALERTDEFAULT) return;
2309
2310         if (folderview->opened == folderview->selected) {
2311                 summary_clear_all(folderview->summaryview);
2312                 folderview->opened = NULL;
2313         }
2314
2315         folder_item_remove(item);
2316         gtk_ctree_remove_node(ctree, folderview->selected);
2317         folder_write_list();
2318 }
2319
2320 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2321                                          GtkWidget *widget)
2322 {
2323         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2324         FolderItem *item;
2325         gchar *message;
2326         AlertValue avalue;
2327
2328         if (!folderview->selected) return;
2329
2330         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2331         g_return_if_fail(item != NULL);
2332         g_return_if_fail(item->folder != NULL);
2333         g_return_if_fail(item->folder->type == F_NEWS);
2334         g_return_if_fail(item->folder->account != NULL);
2335
2336         message = g_strdup_printf(_("Really delete news account `%s'?"),
2337                                   item->folder->name);
2338         avalue = alertpanel(_("Delete news account"), message,
2339                             _("Yes"), _("+No"), NULL);
2340         g_free(message);
2341
2342         if (avalue != G_ALERTDEFAULT) return;
2343
2344         if (folderview->opened == folderview->selected ||
2345             gtk_ctree_is_ancestor(ctree,
2346                                   folderview->selected,
2347                                   folderview->opened)) {
2348                 summary_clear_all(folderview->summaryview);
2349                 folderview->opened = NULL;
2350         }
2351
2352         account_destroy(item->folder->account);
2353         folder_destroy(item->folder);
2354         gtk_ctree_remove_node(ctree, folderview->selected);
2355         account_set_menu();
2356         main_window_reflect_prefs_all();
2357         folder_write_list();
2358 }
2359
2360 static void folderview_search_cb(FolderView *folderview, guint action,
2361                                  GtkWidget *widget)
2362 {
2363         summary_search(folderview->summaryview);
2364 }
2365
2366 static void folderview_property_cb(FolderView *folderview, guint action,
2367                                    GtkWidget *widget)
2368 {
2369         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2370         FolderItem *item;
2371
2372         if (!folderview->selected) return;
2373
2374         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2375         g_return_if_fail(item != NULL);
2376         g_return_if_fail(item->folder != NULL);
2377
2378 #if CLAWS
2379         prefs_folder_item_create(folderview, item);
2380 #else
2381         /*
2382          * CLAWS: wait till Hiro has completed his stuff
2383          */
2384         prefs_folder_item_open(item);
2385 #endif  
2386 }
2387
2388 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2389                                           GdkDragContext *context,
2390                                           gint            x,
2391                                           gint            y,
2392                                           guint           time,
2393                                           FolderView     *folderview)
2394 {
2395         gint row, column;
2396         FolderItem *item, *src_item;
2397         GtkCTreeNode *node = NULL;
2398         gboolean acceptable = FALSE;
2399
2400         if (gtk_clist_get_selection_info
2401                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2402                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2403                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2404                 src_item = folderview->summaryview->folder_item;
2405                 if (item && item->folder && item->path &&
2406                     src_item && src_item != item) {
2407                         switch (item->folder->type) {
2408                         case F_MH:
2409                         case F_IMAP:
2410                                 acceptable = TRUE;
2411                                 break;
2412                         default:
2413                                 break;
2414                         }
2415                 }
2416         }
2417
2418         if (acceptable) {
2419                 gtk_signal_handler_block_by_func
2420                         (GTK_OBJECT(widget),
2421                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2422                 gtk_ctree_select(GTK_CTREE(widget), node);
2423                 gtk_signal_handler_unblock_by_func
2424                         (GTK_OBJECT(widget),
2425                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2426                 gdk_drag_status(context, 
2427                                         (context->actions == GDK_ACTION_COPY ?
2428                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2429         } else {
2430                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2431                 gdk_drag_status(context, 0, time);
2432         }
2433
2434         return acceptable;
2435 }
2436
2437 static void folderview_drag_leave_cb(GtkWidget      *widget,
2438                                      GdkDragContext *context,
2439                                      guint           time,
2440                                      FolderView     *folderview)
2441 {
2442         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2443 }
2444
2445 static void folderview_drag_received_cb(GtkWidget        *widget,
2446                                         GdkDragContext   *drag_context,
2447                                         gint              x,
2448                                         gint              y,
2449                                         GtkSelectionData *data,
2450                                         guint             info,
2451                                         guint             time,
2452                                         FolderView       *folderview)
2453 {
2454         gint row, column;
2455         FolderItem *item, *src_item;
2456         GtkCTreeNode *node;
2457
2458         if (gtk_clist_get_selection_info
2459                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2460                 return;
2461
2462         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2463         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2464         src_item = folderview->summaryview->folder_item;
2465         if (item && src_item) {
2466                 switch (drag_context->action) {
2467                         case GDK_ACTION_COPY:
2468                                 summary_copy_selected_to(folderview->summaryview, item);
2469                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2470                                 break;
2471                         case GDK_ACTION_MOVE:
2472                         case GDK_ACTION_DEFAULT:
2473                         default:
2474                 if (src_item->folder->type != item->folder->type ||
2475                     (item->folder->type == F_IMAP &&
2476                      src_item->folder != item->folder))
2477                         summary_copy_selected_to(folderview->summaryview, item);
2478                 else
2479                         summary_move_selected_to(folderview->summaryview, item);
2480                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2481                 }
2482         } else
2483                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2484 }
2485
2486 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2487 {
2488         const FolderItem *item = a;
2489         const gchar *name = b;
2490
2491         if (!item->path) return -1;
2492         return strcmp2(g_basename(item->path), name);
2493 }
2494
2495 static void folderview_scoring_cb(FolderView *folderview, guint action,
2496                                    GtkWidget *widget)
2497 {
2498         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2499         FolderItem *item;
2500
2501         if (!folderview->selected) return;
2502
2503         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2504         g_return_if_fail(item != NULL);
2505         g_return_if_fail(item->folder != NULL);
2506
2507         prefs_scoring_open(item);
2508 }
2509
2510 static void folderview_processing_cb(FolderView *folderview, guint action,
2511                                      GtkWidget *widget)
2512 {
2513         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2514         FolderItem *item;
2515
2516         if (!folderview->selected) return;
2517
2518         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2519         g_return_if_fail(item != NULL);
2520         g_return_if_fail(item->folder != NULL);
2521
2522         prefs_filtering_open(item);
2523 }
2524
2525 void folderview_set_target_folder_color(gint color_op) 
2526 {
2527         gint firstone = 1;
2528         GList *list;
2529         FolderView *folderview;
2530
2531         for (list = folderview_list; list != NULL; list = list->next) {
2532                 folderview = (FolderView *)list->data;
2533                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2534                 if (firstone) {
2535                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2536                                 folderview->color_op;
2537                         firstone = 0;
2538                 }
2539         }
2540 }
2541
2542 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2543 {
2544         folderview_init(folderview);
2545         folderview_set_all();
2546 }