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