* src/folder.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         gchar *buf;
1636
1637         folderview->selected = row;
1638
1639         if (folderview->opened == row) {
1640                 folderview->open_folder = FALSE;
1641                 return;
1642         }
1643
1644         if (!can_select || summary_is_locked(folderview->summaryview)) {
1645                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1646                 gtk_ctree_select(ctree, folderview->opened);
1647                 return;
1648         }
1649
1650         if (!folderview->open_folder) return;
1651
1652         item = gtk_ctree_node_get_row_data(ctree, row);
1653         if (!item) return;
1654
1655         can_select = FALSE;
1656
1657         /* Save cache for old folder */
1658         /* We don't want to lose all caches if sylpheed crashed */
1659         if (folderview->opened) {
1660                 FolderItem *olditem;
1661                 
1662                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1663                 if (olditem) {
1664                         /* will be null if we just moved the previously opened folder */
1665                         folder_item_write_cache(olditem);
1666                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1667                         folder_item_close(olditem);
1668                 }
1669         }
1670
1671         /* CLAWS: set compose button type: news folder items 
1672          * always have a news folder as parent */
1673         if (item->folder) 
1674                 toolbar_set_compose_button
1675                         (folderview->mainwin->toolbar,
1676                          item->folder->type == F_NEWS ? 
1677                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1678
1679         if (item->path)
1680                 debug_print("Folder %s is selected\n", item->path);
1681
1682         if (!GTK_CTREE_ROW(row)->children)
1683                 gtk_ctree_expand(ctree, row);
1684         if (folderview->opened &&
1685             !GTK_CTREE_ROW(folderview->opened)->children)
1686                 gtk_ctree_collapse(ctree, folderview->opened);
1687
1688         /* ungrab the mouse event */
1689         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1690                 gtk_grab_remove(GTK_WIDGET(ctree));
1691                 if (gdk_pointer_is_grabbed())
1692                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1693         }
1694
1695         /* Open Folder */
1696         buf = g_strdup_printf(_("Opening Folder %s..."), item->path);
1697         debug_print("%s\n", buf);
1698         STATUSBAR_PUSH(folderview->mainwin, buf);
1699         g_free(buf);
1700
1701         main_window_cursor_wait(folderview->mainwin);
1702
1703         if (folder_item_open(item) != 0) {
1704                 main_window_cursor_normal(folderview->mainwin);
1705                 STATUSBAR_POP(folderview->mainwin);
1706
1707                 alertpanel_error(_("Folder could not be opened."));
1708
1709                 return;
1710         }
1711
1712         main_window_cursor_normal(folderview->mainwin);
1713
1714         /* Show messages */
1715         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1716         opened = summary_show(folderview->summaryview, item);
1717
1718         folder_clean_cache_memory();
1719
1720         if (!opened) {
1721                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1722                 gtk_ctree_select(ctree, folderview->opened);
1723         } else {
1724                 folderview->opened = row;
1725                 if (gtk_ctree_node_is_visible(ctree, row)
1726                     != GTK_VISIBILITY_FULL)
1727                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1728         }
1729
1730         STATUSBAR_POP(folderview->mainwin);
1731
1732         folderview->open_folder = FALSE;
1733         can_select = TRUE;
1734 }
1735
1736 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1737                                      FolderView *folderview)
1738 {
1739         FolderItem *item;
1740
1741         item = gtk_ctree_node_get_row_data(ctree, node);
1742         g_return_if_fail(item != NULL);
1743         item->collapsed = FALSE;
1744         folderview_update_node(folderview, node);
1745 }
1746
1747 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1748                                       FolderView *folderview)
1749 {
1750         FolderItem *item;
1751
1752         item = gtk_ctree_node_get_row_data(ctree, node);
1753         g_return_if_fail(item != NULL);
1754         item->collapsed= TRUE;
1755         folderview_update_node(folderview, node);
1756 }
1757
1758 static void folderview_popup_close(GtkMenuShell *menu_shell,
1759                                    FolderView *folderview)
1760 {
1761         if (!folderview->opened) return;
1762
1763         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1764                                   folderview->opened);
1765         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1766 }
1767
1768 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1769                                    FolderView *folderview)
1770 {
1771         switch (column) {
1772         case COL_FOLDER:
1773                 prefs_common.folder_col_folder = width;
1774                 break;
1775         case COL_NEW:
1776                 prefs_common.folder_col_new = width;
1777                 break;
1778         case COL_UNREAD:
1779                 prefs_common.folder_col_unread = width;
1780                 break;
1781         case COL_TOTAL:
1782                 prefs_common.folder_col_total = width;
1783                 break;
1784         default:
1785                 break;
1786         }
1787 }
1788
1789 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1790                                              GtkCTreeNode *node,
1791                                              const gchar *name)
1792 {
1793         FolderItem *item;
1794
1795         if (!node)
1796                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1797         if (!node)
1798                 return NULL;
1799
1800         node = GTK_CTREE_ROW(node)->children;
1801
1802         while (node) {
1803                 item = gtk_ctree_node_get_row_data(ctree, node);
1804                 if (!folderview_compare_name(item, name))
1805                         return node;
1806                 node = GTK_CTREE_ROW(node)->sibling;
1807         }
1808
1809         return NULL;
1810 }
1811
1812 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1813                                       GtkWidget *widget)
1814 {
1815         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1816         FolderItem *item;
1817
1818         if (!folderview->selected) return;
1819
1820         summary_show(folderview->summaryview, NULL);
1821
1822         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1823         g_return_if_fail(item != NULL);
1824         g_return_if_fail(item->folder != NULL);
1825
1826         if (action == 0)
1827                 folderview_check_new(item->folder);
1828         else
1829                 folderview_rescan_tree(item->folder);
1830         
1831         if (folderview->opened) {
1832                 item = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1833                 if (item)
1834                         folder_update_item(item, TRUE);
1835         }
1836 }
1837
1838 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1839 {
1840         GNode *srcnode;
1841
1842         folderview_create_folder_node(folderview, item);
1843         
1844         srcnode = item->folder->node;   
1845         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1846         srcnode = srcnode->children;
1847         while (srcnode != NULL) {
1848                 if (srcnode && srcnode->data) {
1849                         FolderItem *next_item = (FolderItem*) srcnode->data;
1850                         folderview_create_folder_node_recursive(folderview, next_item);
1851                 }
1852                 srcnode = srcnode->next;
1853         }
1854 }
1855
1856 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1857 {
1858         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1859         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1860         GtkCTreeNode *node, *parent_node;
1861         
1862         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, item->parent);
1863
1864         gtk_clist_freeze(GTK_CLIST(ctree));
1865
1866         text[COL_FOLDER] = item->name;
1867         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1868                                      FOLDER_SPACING,
1869                                      folderxpm, folderxpmmask,
1870                                      folderopenxpm, folderopenxpmmask,
1871                                      FALSE, FALSE);
1872         gtk_ctree_expand(ctree, parent_node);
1873         gtk_ctree_node_set_row_data(ctree, node, item);
1874         if (normal_style)
1875                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1876         folderview_sort_folders(folderview, folderview->selected, item->folder);
1877
1878         gtk_clist_thaw(GTK_CLIST(ctree));
1879 }
1880
1881 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1882                                      GtkWidget *widget)
1883 {
1884         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1885         FolderItem *item;
1886         FolderItem *new_item;
1887         gchar *new_folder;
1888         gchar *name, *name_;
1889
1890         if (!folderview->selected) return;
1891
1892         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1893         g_return_if_fail(item != NULL);
1894         g_return_if_fail(item->folder != NULL);
1895
1896         new_folder = input_dialog(_("New folder"),
1897                                   _("Input the name of new folder:"),
1898                                   _("NewFolder"));
1899         if (!new_folder) return;
1900
1901         if (item->folder->type != F_MBOX) {
1902                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1903                         alertpanel_error(_("`%c' can't be included in folder name."),
1904                                          G_DIR_SEPARATOR);
1905                         g_free(new_folder);
1906                         return;
1907                 }
1908         }
1909
1910         name_ = trim_string(new_folder, 32);
1911         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1912         g_free(name_);
1913
1914         /* find whether the directory already exists */
1915         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1916                 alertpanel_error(_("The folder `%s' already exists."), name);
1917                 g_free(new_folder);
1918                 return;
1919         }
1920
1921         new_item = folder_create_folder(item, new_folder);
1922         if (!new_item) {
1923                 alertpanel_error(_("Can't create the folder `%s'."), name);
1924                 g_free(new_folder);
1925                 return;
1926         } 
1927         g_free(new_folder);
1928
1929         folderview_create_folder_node(folderview, new_item);
1930
1931         folder_write_list();
1932 }
1933
1934 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1935                                           GtkWidget *widget)
1936 {
1937         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1938         FolderItem *item;
1939         FolderItem *new_item;
1940         gchar *new_folder;
1941
1942         if (!folderview->selected) return;
1943
1944         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1945         g_return_if_fail(item != NULL);
1946         g_return_if_fail(item->folder != NULL);
1947
1948         new_folder = input_dialog(_("New folder"),
1949                                   _("Input the name of new folder:"),
1950                                   _("NewFolder"));
1951         if (!new_folder) return;
1952
1953         /* find whether the directory already exists */
1954         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1955                 alertpanel_error(_("The folder `%s' already exists."),
1956                                  new_folder);
1957                 g_free(new_folder);
1958                 return;
1959         }
1960
1961         new_item = folder_create_folder(item, new_folder);
1962         g_free(new_folder);
1963         if (!new_item) return;
1964
1965         folderview_create_folder_node(folderview, new_item);
1966
1967         folder_write_list();
1968 }
1969
1970 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1971                                         GtkWidget *widget)
1972 {
1973         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1974         FolderItem *item;
1975         gchar *new_folder;
1976         gchar *name, *name_;
1977         gchar *message;
1978         gchar *old_path;
1979         gchar *old_id;
1980         gchar *new_id;
1981
1982         if (!folderview->selected) return;
1983
1984         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1985         g_return_if_fail(item != NULL);
1986         g_return_if_fail(item->path != NULL);
1987         g_return_if_fail(item->folder != NULL);
1988
1989         name_ = trim_string(item->name, 32);
1990         Xstrdup_a(name, name_, return);
1991         g_free(name_);
1992         message = g_strdup_printf(_("Input new name for `%s':"), name);
1993         new_folder = input_dialog(_("Rename folder"), message,
1994                                   g_basename(item->path));
1995         g_free(message);
1996         if (!new_folder) return;
1997
1998         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1999                 alertpanel_error(_("`%c' can't be included in folder name."),
2000                                  G_DIR_SEPARATOR);
2001                 g_free(new_folder);
2002                 return;
2003         }
2004
2005         if (folderview_find_by_name
2006                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2007                  new_folder)) {
2008                 name = trim_string(new_folder, 32);
2009                 alertpanel_error(_("The folder `%s' already exists."), name);
2010                 g_free(name);
2011                 g_free(new_folder);
2012                 return;
2013         }
2014
2015         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
2016         old_id = folder_item_get_identifier(item);
2017
2018         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2019                 g_free(old_id);
2020                 g_free(new_folder);
2021                 return;
2022         }
2023         g_free(new_folder);
2024
2025         /* if (FOLDER_TYPE(item->folder) == F_MH)
2026                 prefs_filtering_rename_path(old_path, item->path); */
2027         new_id = folder_item_get_identifier(item);
2028         prefs_filtering_rename_path(old_id, new_id);
2029
2030         g_free(old_id);
2031         g_free(new_id);
2032
2033         gtk_clist_freeze(GTK_CLIST(ctree));
2034
2035         folderview_update_node(folderview, folderview->selected);
2036         folderview_sort_folders(folderview,
2037                                 GTK_CTREE_ROW(folderview->selected)->parent,
2038                                 item->folder);
2039         if (folderview->opened == folderview->selected ||
2040             gtk_ctree_is_ancestor(ctree,
2041                                   folderview->selected,
2042                                   folderview->opened)) {
2043                 GtkCTreeNode *node = folderview->opened;
2044                 folderview_unselect(folderview);
2045                 folderview_select_node(folderview, node);
2046         }
2047
2048         gtk_clist_thaw(GTK_CLIST(ctree));
2049
2050         folder_write_list();
2051 }
2052
2053 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
2054                                              guint action,
2055                                              GtkWidget *widget)
2056 {
2057         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2058         FolderItem *item;
2059         gchar *new_folder;
2060         gchar *message;
2061
2062         if (!folderview->selected) return;
2063
2064         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2065         g_return_if_fail(item != NULL);
2066         g_return_if_fail(item->path != NULL);
2067         g_return_if_fail(item->folder != NULL);
2068
2069         message = g_strdup_printf(_("Input new name for `%s':"),
2070                                   g_basename(item->path));
2071         new_folder = input_dialog(_("Rename folder"), message,
2072                                   g_basename(item->path));
2073         g_free(message);
2074         if (!new_folder) return;
2075
2076         if (folderview_find_by_name
2077                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2078                  new_folder)) {
2079                 alertpanel_error(_("The folder `%s' already exists."),
2080                                  new_folder);
2081                 g_free(new_folder);
2082                 return;
2083         }
2084
2085         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2086                 g_free(new_folder);
2087                 return;
2088         }
2089         g_free(new_folder);
2090
2091         gtk_clist_freeze(GTK_CLIST(ctree));
2092
2093         folderview_update_node(folderview, folderview->selected);
2094         folderview_sort_folders(folderview,
2095                                 GTK_CTREE_ROW(folderview->selected)->parent,
2096                                 item->folder);
2097         if (folderview->opened == folderview->selected) {
2098                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2099                         gtk_ctree_expand(ctree, folderview->opened);
2100                 summary_show(folderview->summaryview, item);
2101         }
2102
2103         gtk_clist_thaw(GTK_CLIST(ctree));
2104
2105         folder_write_list();
2106 }
2107
2108 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2109                                         GtkWidget *widget)
2110 {
2111         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2112         FolderItem *item;
2113         gchar *message, *name, *name_;
2114         AlertValue avalue;
2115         gchar *old_path;
2116         gchar *old_id;
2117
2118         if (!folderview->selected) return;
2119
2120         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2121         g_return_if_fail(item != NULL);
2122         g_return_if_fail(item->path != NULL);
2123         g_return_if_fail(item->folder != NULL);
2124
2125         name_ = trim_string(item->name, 32);
2126         Xstrdup_a(name, name_, return);
2127         g_free(name_);
2128         message = g_strdup_printf
2129                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2130                    "Do you really want to delete?"), name);
2131         avalue = alertpanel(_("Delete folder"), message,
2132                             _("Yes"), _("+No"), NULL);
2133         g_free(message);
2134         if (avalue != G_ALERTDEFAULT) return;
2135
2136         Xstrdup_a(old_path, item->path, return);
2137         old_id = folder_item_get_identifier(item);
2138
2139         if (item->folder->remove_folder(item->folder, item) < 0) {
2140                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2141                 if (folderview->opened == folderview->selected)
2142                         summary_show(folderview->summaryview,
2143                                      folderview->summaryview->folder_item);
2144                 g_free(old_id);
2145                 return;
2146         }
2147
2148         /* if (FOLDER_TYPE(item->folder) == F_MH)
2149                 prefs_filtering_delete_path(old_path); */
2150
2151         if (folderview->opened == folderview->selected ||
2152             gtk_ctree_is_ancestor(ctree,
2153                                   folderview->selected,
2154                                   folderview->opened)) {
2155                 summary_clear_all(folderview->summaryview);
2156                 folderview->opened = NULL;
2157         }
2158
2159         gtk_ctree_remove_node(ctree, folderview->selected);
2160         folder_write_list();
2161
2162         prefs_filtering_delete_path(old_id);
2163         g_free(old_id);
2164
2165 }
2166
2167 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2168                                          GtkWidget *widget)
2169 {
2170         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2171         GtkCTreeNode *node;
2172         FolderItem *item;
2173         gchar *name, *name_;
2174         gchar *message;
2175         AlertValue avalue;
2176
2177         if (!folderview->selected) return;
2178         node = folderview->selected;
2179         item = gtk_ctree_node_get_row_data(ctree, node);
2180         g_return_if_fail(item != NULL);
2181         g_return_if_fail(item->folder != NULL);
2182         if (item->parent) return;
2183
2184         name_ = trim_string(item->folder->name, 32);
2185         Xstrdup_a(name, name_, return);
2186         g_free(name_);
2187         message = g_strdup_printf
2188                 (_("Really remove the mailbox `%s' ?\n"
2189                    "(The messages are NOT deleted from the disk)"), name);
2190         avalue = alertpanel(_("Remove mailbox"), message,
2191                             _("Yes"), _("+No"), NULL);
2192         g_free(message);
2193         if (avalue != G_ALERTDEFAULT) return;
2194
2195         folder_destroy(item->folder);
2196         summary_clear_all(folderview->summaryview);
2197         folderview_unselect(folderview);
2198         gtk_ctree_remove_node(ctree, node);
2199         folder_write_list();
2200 }
2201
2202 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2203                                           GtkWidget *widget)
2204 {
2205         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2206         FolderItem *item;
2207         FolderItem *new_item;
2208         gchar *new_folder;
2209         gchar *name, *name_;
2210         gchar *p;
2211
2212         if (!folderview->selected) return;
2213
2214         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2215         g_return_if_fail(item != NULL);
2216         g_return_if_fail(item->folder != NULL);
2217         g_return_if_fail(item->folder->type == F_IMAP);
2218         g_return_if_fail(item->folder->account != NULL);
2219
2220         new_folder = input_dialog
2221                 (_("New folder"),
2222                  _("Input the name of new folder:\n"
2223                    "(if you want to create a folder to store subfolders,\n"
2224                    " append `/' at the end of the name)"),
2225                  _("NewFolder"));
2226         if (!new_folder) return;
2227
2228         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2229             *(p + 1) != '\0') {
2230                 alertpanel_error(_("`%c' can't be included in folder name."),
2231                                  G_DIR_SEPARATOR);
2232                 g_free(new_folder);
2233                 return;
2234         }
2235
2236         name_ = trim_string(new_folder, 32);
2237         Xstrdup_a(name, name_, return);
2238         g_free(name_);
2239
2240         /* find whether the directory already exists */
2241         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2242                 alertpanel_error(_("The folder `%s' already exists."), name);
2243                 g_free(new_folder);
2244                 return;
2245         }
2246
2247         new_item = folder_create_folder(item, new_folder);
2248         if (!new_item) {
2249                 alertpanel_error(_("Can't create the folder `%s'."), name);
2250                 g_free(new_folder);
2251                 return;
2252         }
2253         g_free(new_folder);
2254
2255         folderview_create_folder_node(folderview, new_item);
2256
2257         folder_write_list();
2258 }
2259
2260 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2261                                          GtkWidget *widget)
2262 {
2263         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2264         FolderItem *item;
2265         PrefsAccount *account;
2266         gchar *name, *name_;
2267         gchar *message;
2268         AlertValue avalue;
2269
2270         if (!folderview->selected) return;
2271
2272         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2273         g_return_if_fail(item != NULL);
2274         g_return_if_fail(item->folder != NULL);
2275         g_return_if_fail(item->folder->type == F_IMAP);
2276         g_return_if_fail(item->folder->account != NULL);
2277
2278         name_ = trim_string(item->folder->name, 32);
2279         Xstrdup_a(name, name_, return);
2280         g_free(name_);
2281         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2282         avalue = alertpanel(_("Delete IMAP4 account"), message,
2283                             _("Yes"), _("+No"), NULL);
2284         g_free(message);
2285
2286         if (avalue != G_ALERTDEFAULT) return;
2287
2288         if (folderview->opened == folderview->selected ||
2289             gtk_ctree_is_ancestor(ctree,
2290                                   folderview->selected,
2291                                   folderview->opened)) {
2292                 summary_clear_all(folderview->summaryview);
2293                 folderview->opened = NULL;
2294         }
2295
2296         account = item->folder->account;
2297         folder_destroy(item->folder);
2298         account_destroy(account);
2299         gtk_ctree_remove_node(ctree, folderview->selected);
2300         account_set_menu();
2301         main_window_reflect_prefs_all();
2302         folder_write_list();
2303 }
2304
2305 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2306                                          GtkWidget *widget)
2307 {
2308         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2309         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2310         GtkCTreeNode *servernode, *node;
2311         Folder *folder;
2312         FolderItem *item;
2313         FolderItem *rootitem;
2314         FolderItem *newitem;
2315         GSList *new_subscr;
2316         GSList *cur;
2317         GNode *gnode;
2318
2319         if (!folderview->selected) return;
2320
2321         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2322         g_return_if_fail(item != NULL);
2323         folder = item->folder;
2324         g_return_if_fail(folder != NULL);
2325         g_return_if_fail(folder->type == F_NEWS);
2326         g_return_if_fail(folder->account != NULL);
2327
2328         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2329                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2330         else
2331                 servernode = folderview->selected;
2332
2333         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2334
2335         new_subscr = grouplist_dialog(folder);
2336
2337         /* remove unsubscribed newsgroups */
2338         for (gnode = folder->node->children; gnode != NULL; ) {
2339                 GNode *next = gnode->next;
2340
2341                 item = FOLDER_ITEM(gnode->data);
2342                 if (g_slist_find_custom(new_subscr, item->path,
2343                                         (GCompareFunc)g_strcasecmp) != NULL) {
2344                         gnode = next;
2345                         continue;
2346                 }
2347
2348                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2349                 if (!node) {
2350                         gnode = next;
2351                         continue;
2352                 }
2353
2354                 if (folderview->opened == node) {
2355                         summary_clear_all(folderview->summaryview);
2356                         folderview->opened = NULL;
2357                 }
2358
2359                 folder_item_remove(item);
2360                 gtk_ctree_remove_node(ctree, node);
2361
2362                 gnode = next;
2363         }
2364
2365         gtk_clist_freeze(GTK_CLIST(ctree));
2366
2367         /* add subscribed newsgroups */
2368         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2369                 gchar *name = (gchar *)cur->data;
2370
2371                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2372                         continue;
2373
2374                 text[COL_FOLDER] = name;
2375                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2376                                              FOLDER_SPACING,
2377                                              folderxpm, folderxpmmask,
2378                                              folderopenxpm, folderopenxpmmask,
2379                                              FALSE, FALSE);
2380                 gtk_ctree_expand(ctree, servernode);
2381
2382                 newitem = folder_item_new(folder, name, name);
2383                 folder_item_append(rootitem, newitem);
2384                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2385         }
2386
2387         folderview_sort_folders(folderview, servernode, folder);
2388         gtk_clist_thaw(GTK_CLIST(ctree));
2389
2390         slist_free_strings(new_subscr);
2391         g_slist_free(new_subscr);
2392
2393         folder_write_list();
2394 }
2395
2396 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2397                                         GtkWidget *widget)
2398 {
2399         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2400         FolderItem *item;
2401         gchar *name, *name_;
2402         gchar *message;
2403         AlertValue avalue;
2404
2405         if (!folderview->selected) return;
2406
2407         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2408         g_return_if_fail(item != NULL);
2409         g_return_if_fail(item->folder != NULL);
2410         g_return_if_fail(item->folder->type == F_NEWS);
2411         g_return_if_fail(item->folder->account != NULL);
2412
2413         name_ = trim_string(item->path, 32);
2414         Xstrdup_a(name, name_, return);
2415         g_free(name_);
2416         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2417         avalue = alertpanel(_("Delete newsgroup"), message,
2418                             _("Yes"), _("+No"), NULL);
2419         g_free(message);
2420         if (avalue != G_ALERTDEFAULT) return;
2421
2422         if (folderview->opened == folderview->selected) {
2423                 summary_clear_all(folderview->summaryview);
2424                 folderview->opened = NULL;
2425         }
2426
2427         folder_item_remove(item);
2428         gtk_ctree_remove_node(ctree, folderview->selected);
2429         folder_write_list();
2430 }
2431
2432 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2433                                          GtkWidget *widget)
2434 {
2435         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2436         FolderItem *item;
2437         PrefsAccount *account;
2438         gchar *name, *name_;
2439         gchar *message;
2440         AlertValue avalue;
2441
2442         if (!folderview->selected) return;
2443
2444         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2445         g_return_if_fail(item != NULL);
2446         g_return_if_fail(item->folder != NULL);
2447         g_return_if_fail(item->folder->type == F_NEWS);
2448         g_return_if_fail(item->folder->account != NULL);
2449
2450         name_ = trim_string(item->folder->name, 32);
2451         Xstrdup_a(name, name_, return);
2452         g_free(name_);
2453         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2454         avalue = alertpanel(_("Delete news account"), message,
2455                             _("Yes"), _("+No"), NULL);
2456         g_free(message);
2457
2458         if (avalue != G_ALERTDEFAULT) return;
2459
2460         if (folderview->opened == folderview->selected ||
2461             gtk_ctree_is_ancestor(ctree,
2462                                   folderview->selected,
2463                                   folderview->opened)) {
2464                 summary_clear_all(folderview->summaryview);
2465                 folderview->opened = NULL;
2466         }
2467
2468         account = item->folder->account;
2469         folder_destroy(item->folder);
2470         account_destroy(account);
2471         gtk_ctree_remove_node(ctree, folderview->selected);
2472         account_set_menu();
2473         main_window_reflect_prefs_all();
2474         folder_write_list();
2475 }
2476
2477 static void folderview_search_cb(FolderView *folderview, guint action,
2478                                  GtkWidget *widget)
2479 {
2480         summary_search(folderview->summaryview);
2481 }
2482
2483 static void folderview_property_cb(FolderView *folderview, guint action,
2484                                    GtkWidget *widget)
2485 {
2486         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2487         FolderItem *item;
2488
2489         if (!folderview->selected) return;
2490
2491         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2492         g_return_if_fail(item != NULL);
2493         g_return_if_fail(item->folder != NULL);
2494
2495 #if CLAWS
2496         prefs_folder_item_create(folderview, item);
2497 #else
2498         /*
2499          * CLAWS: wait till Hiro has completed his stuff
2500          */
2501         if (item->parent == NULL && item->folder->account)
2502                 account_open(item->folder->account);
2503         else
2504                 prefs_folder_item_open(item);
2505 #endif  
2506 }
2507
2508 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2509                                           GdkDragContext *context,
2510                                           gint            x,
2511                                           gint            y,
2512                                           guint           time,
2513                                           FolderView     *folderview)
2514 {
2515         gint row, column;
2516         FolderItem *item, *src_item;
2517         GtkCTreeNode *node = NULL;
2518         gboolean acceptable = FALSE;
2519
2520         if (gtk_clist_get_selection_info
2521                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2522                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2523                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2524                 src_item = folderview->summaryview->folder_item;
2525
2526                 if (item && item->folder && item->path &&
2527                     src_item && src_item != item) {
2528                         switch (item->folder->type) {
2529                         case F_MH:
2530                         case F_IMAP:
2531                                 acceptable = TRUE;
2532                                 break;
2533                         default:
2534                                 break;
2535                         }
2536                 } else if (item && item->folder && folder_item_get_path(item) &&
2537                            src_item && src_item != item) {
2538                         /* a root folder - acceptable only from folderview */
2539                         if (item->folder->type == F_MH || item->folder->type == F_IMAP)
2540                                 acceptable = TRUE;
2541                 }
2542                         
2543         }
2544
2545         if (acceptable) {
2546                 gtk_signal_handler_block_by_func
2547                         (GTK_OBJECT(widget),
2548                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2549                 gtk_ctree_select(GTK_CTREE(widget), node);
2550                 gtk_signal_handler_unblock_by_func
2551                         (GTK_OBJECT(widget),
2552                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2553                 gdk_drag_status(context, 
2554                                         (context->actions == GDK_ACTION_COPY ?
2555                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2556         } else {
2557                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2558                 gdk_drag_status(context, 0, time);
2559         }
2560
2561         return acceptable;
2562 }
2563
2564 static void folderview_drag_leave_cb(GtkWidget      *widget,
2565                                      GdkDragContext *context,
2566                                      guint           time,
2567                                      FolderView     *folderview)
2568 {
2569         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2570 }
2571
2572 static void folderview_drag_received_cb(GtkWidget        *widget,
2573                                         GdkDragContext   *drag_context,
2574                                         gint              x,
2575                                         gint              y,
2576                                         GtkSelectionData *data,
2577                                         guint             info,
2578                                         guint             time,
2579                                         FolderView       *folderview)
2580 {
2581         gint row, column;
2582         FolderItem *item, *src_item;
2583         GtkCTreeNode *node;
2584
2585         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2586                 /* comes from summaryview */
2587                 if (gtk_clist_get_selection_info
2588                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2589                         return;
2590
2591                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2592                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2593                 src_item = folderview->summaryview->folder_item;
2594                 
2595                 /* re-check (due to acceptable possibly set for folder moves */
2596                 if (!(item && item->folder && item->path &&
2597                       src_item && src_item != item && 
2598                       (item->folder->type == F_MH || item->folder->type == F_IMAP))) {
2599                         return;
2600                 }
2601                 if (item && src_item) {
2602                         switch (drag_context->action) {
2603                                 case GDK_ACTION_COPY:
2604                                         summary_copy_selected_to(folderview->summaryview, item);
2605                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2606                                         break;
2607                                 case GDK_ACTION_MOVE:
2608                                 case GDK_ACTION_DEFAULT:
2609                                 default:
2610                         if (src_item->folder->type != item->folder->type ||
2611                             (item->folder->type == F_IMAP &&
2612                              src_item->folder != item->folder))
2613                                 summary_copy_selected_to(folderview->summaryview, item);
2614                         else
2615                                 summary_move_selected_to(folderview->summaryview, item);
2616                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2617                         }
2618                 } else
2619                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2620         } else {
2621                 /* comes from folderview */
2622                 char *source;
2623                 char *buf;
2624                 GtkCTreeNode *src_node;
2625                 FolderItem *new_item, *src_parent;
2626                 
2627                 source = data->data + 17;
2628                 if (gtk_clist_get_selection_info
2629                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2630                     || *source == 0) {
2631                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2632                         return;
2633                 }
2634                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2635                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2636                 src_item = folder_find_item_from_path(source);
2637
2638                 if (!src_item || src_item->stype != F_NORMAL) {
2639                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2640                         return;
2641                 }
2642                 src_parent = src_item->parent;
2643                 src_node = gtk_ctree_find_by_row_data(GTK_CTREE(widget), NULL, src_item);
2644
2645                 buf = g_strdup_printf(_("Moving %s to %s..."), src_item->name, item->name);
2646                 STATUSBAR_PUSH(folderview->mainwin, buf);
2647                 g_free(buf);
2648                 summary_clear_all(folderview->summaryview);
2649                 folderview->opened = NULL;
2650                 folderview->selected = NULL;
2651                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
2652                 inc_lock();
2653                 main_window_cursor_wait(folderview->mainwin);
2654                 if ((new_item = folder_item_move_to(src_item, item)) != NULL) {
2655                         main_window_cursor_normal(folderview->mainwin);
2656                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2657                 
2658                         if (src_node)
2659                                 gtk_ctree_remove_node(GTK_CTREE(widget), src_node);
2660                         else 
2661                                 debug_print("can't remove src node: is null\n");
2662
2663                         STATUSBAR_POP(folderview->mainwin);
2664                         folderview_create_folder_node_recursive(folderview, new_item);
2665                         folder_update_item(src_parent, TRUE);
2666                         folder_update_item_recursive(new_item, TRUE); 
2667                         folderview_sort_folders(folderview, 
2668                                 gtk_ctree_find_by_row_data(GTK_CTREE(widget), 
2669                                         NULL, new_item->parent), new_item->folder);
2670                         folderview_select(folderview, new_item);
2671                 } else {
2672                         main_window_cursor_normal(folderview->mainwin);
2673                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2674                         STATUSBAR_POP(folderview->mainwin);
2675                 }       
2676                 inc_unlock();           
2677                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
2678         }
2679 }
2680
2681 static gint folderview_clist_compare(GtkCList *clist,
2682                                      gconstpointer ptr1, gconstpointer ptr2)
2683 {
2684         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2685         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2686
2687         if (!item1->name)
2688                 return (item2->name != NULL);
2689         if (!item2->name)
2690                 return -1;
2691
2692         return g_strcasecmp(item1->name, item2->name);
2693 }
2694
2695 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2696 {
2697         const FolderItem *item = a;
2698         const gchar *name = b;
2699
2700         if (!item->path) return -1;
2701         return strcmp2(g_basename(item->path), name);
2702 }
2703
2704 static void folderview_scoring_cb(FolderView *folderview, guint action,
2705                                    GtkWidget *widget)
2706 {
2707         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2708         FolderItem *item;
2709
2710         if (!folderview->selected) return;
2711
2712         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2713         g_return_if_fail(item != NULL);
2714         g_return_if_fail(item->folder != NULL);
2715
2716         prefs_scoring_open(item);
2717 }
2718
2719 static void folderview_processing_cb(FolderView *folderview, guint action,
2720                                      GtkWidget *widget)
2721 {
2722         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2723         FolderItem *item;
2724
2725         if (!folderview->selected) return;
2726
2727         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2728         g_return_if_fail(item != NULL);
2729         g_return_if_fail(item->folder != NULL);
2730
2731         prefs_filtering_open(item, NULL, NULL);
2732 }
2733
2734 void folderview_set_target_folder_color(gint color_op) 
2735 {
2736         gint firstone = 1;
2737         GList *list;
2738         FolderView *folderview;
2739
2740         for (list = folderview_list; list != NULL; list = list->next) {
2741                 folderview = (FolderView *)list->data;
2742                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2743                 if (firstone) {
2744                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2745                                 folderview->color_op;
2746                         firstone = 0;
2747                 }
2748         }
2749 }
2750
2751 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2752 {
2753         folderview_init(folderview);
2754         folderview_set_all();
2755 }
2756
2757 GtkTargetEntry folderview_drag_types[1] =
2758 {
2759         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
2760 };
2761
2762
2763 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2764                                   FolderView       *folderview)
2765 {
2766         GtkTargetList *list;
2767         GdkDragContext *context;
2768
2769         g_return_if_fail(folderview != NULL);
2770         if (folderview->selected == NULL) return;
2771
2772         list = gtk_target_list_new(folderview_drag_types, 1);
2773
2774         context = gtk_drag_begin(widget, list,
2775                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2776         gtk_drag_set_icon_default(context);
2777 }
2778
2779 static void folderview_drag_data_get(GtkWidget        *widget,
2780                                      GdkDragContext   *drag_context,
2781                                      GtkSelectionData *selection_data,
2782                                      guint             info,
2783                                      guint             time,
2784                                      FolderView       *folderview)
2785 {
2786         FolderItem *item;
2787         GList *cur;
2788         gchar *source=NULL;
2789         
2790         for (cur = GTK_CLIST(folderview->ctree)->selection;
2791              cur != NULL; cur = cur->next) {
2792                 item = gtk_ctree_node_get_row_data
2793                         (GTK_CTREE(folderview->ctree), 
2794                          GTK_CTREE_NODE(cur->data));
2795                 if (item) {
2796                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", item->path);
2797                         gtk_selection_data_set(selection_data,
2798                                                selection_data->target, 8,
2799                                                source, strlen(source));
2800                         break;
2801                 } else
2802                         return;
2803         }
2804 }
2805