enable IMAP folder dnd
[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%s)", name, item->unread,
1174                                               add_unread_mark ? "+" : "", 
1175                                               item->unreadmarked > 0 ? "!":"");
1176                 else
1177                         str = g_strdup_printf("%s (+)", name);
1178                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1179                                         xpm, mask, openxpm, openmask,
1180                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1181                 g_free(str);
1182         } else {
1183                 str = g_strdup_printf("%s%s", name, 
1184                                       item->unreadmarked > 0 ? " (!)":"");
1185         
1186                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1187                                         xpm, mask, openxpm, openmask,
1188                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1189                 g_free(str);
1190         }
1191         g_free(name);
1192
1193         if (!item->parent) {
1194                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
1195                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
1196                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
1197         } else {
1198                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
1199                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
1200                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
1201         }
1202
1203         if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1204             item->stype == F_TRASH) {
1205                 use_bold = use_color = FALSE;
1206         } else if (item->stype == F_QUEUE) {
1207                 /* highlight queue folder if there are any messages */
1208                 use_bold = use_color = (item->total > 0);
1209         } else {
1210                 /* if unread messages exist, print with bold font */
1211                 use_bold = (item->unread > 0) || add_unread_mark;
1212                 /* if new messages exist, print with colored letter */
1213                 use_color =
1214                         (item->new > 0) ||
1215                         (add_unread_mark &&
1216                          folderview_have_new_children(folderview, node));       
1217         }
1218
1219         gtk_ctree_node_set_foreground(ctree, node, NULL);
1220
1221         if (use_bold && use_color) {
1222                 style = bold_color_style;
1223         } else if (use_bold) {
1224                 style = bold_style;
1225                 if (item->op_count > 0) {
1226                         style = bold_tgtfold_style;
1227                 }
1228         } else if (use_color) {
1229                 style = normal_color_style;
1230                 gtk_ctree_node_set_foreground(ctree, node,
1231                                               &folderview->color_new);
1232         } else if (item->op_count > 0) {
1233                 style = bold_tgtfold_style;
1234         } else if (item->prefs->color > 0) {
1235                 GdkColor gdk_color;
1236
1237                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1238                 color_style = gtk_style_copy(normal_style);
1239                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1240                 style = color_style;
1241         } else {
1242                 style = normal_style;
1243         }
1244
1245         gtk_ctree_node_set_row_style(ctree, node, style);
1246
1247         item->need_update = FALSE;
1248
1249         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1250                 folderview_update_node(folderview, node);
1251 }
1252
1253 void folderview_update_item(FolderItem *item, gboolean update_summary, gpointer data)
1254 {
1255         FolderView *folderview = (FolderView *)data;
1256         GtkCTree *ctree;
1257         GtkCTreeNode *node;
1258
1259         g_return_if_fail(folderview != NULL);
1260         g_return_if_fail(item != NULL);
1261
1262         ctree = GTK_CTREE(folderview->ctree);
1263
1264         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1265         if (node) {
1266                 folderview_update_node(folderview, node);
1267                 if (update_summary && folderview->opened == node)
1268                         summary_show(folderview->summaryview, item);
1269         }
1270 }
1271
1272 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1273                                       GNode *gnode, GtkCTreeNode *cnode,
1274                                       gpointer data)
1275 {
1276         FolderView *folderview = (FolderView *)data;
1277         FolderItem *item = FOLDER_ITEM(gnode->data);
1278
1279         g_return_val_if_fail(item != NULL, FALSE);
1280
1281         gtk_ctree_node_set_row_data(ctree, cnode, item);
1282         folderview_update_node(folderview, cnode);
1283
1284         return TRUE;
1285 }
1286
1287 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1288                                    gpointer data)
1289 {
1290         FolderView *folderview = (FolderView *)data;
1291         FolderItem *item;
1292
1293         if (GTK_CTREE_ROW(node)->children) {
1294                 item = gtk_ctree_node_get_row_data(ctree, node);
1295                 g_return_if_fail(item != NULL);
1296
1297                 if (!item->collapsed)
1298                         gtk_ctree_expand(ctree, node);
1299                 else
1300                         folderview_update_node(folderview, node);
1301         }
1302 }
1303
1304 #define SET_SPECIAL_FOLDER(ctree, item) \
1305 { \
1306         if (item) { \
1307                 GtkCTreeNode *node, *parent, *sibling; \
1308  \
1309                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
1310                 if (!node) \
1311                         g_warning("%s not found.\n", item->path); \
1312                 else { \
1313                         parent = GTK_CTREE_ROW(node)->parent; \
1314                         if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
1315                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
1316                         else \
1317                                 sibling = GTK_CTREE_ROW(parent)->children; \
1318                         if (node != sibling) \
1319                                 gtk_ctree_move(ctree, node, parent, sibling); \
1320                 } \
1321  \
1322                 prev = node; \
1323         } \
1324 }
1325
1326 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1327                                     Folder *folder)
1328 {
1329         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1330         GtkCTreeNode *prev = NULL;
1331
1332         gtk_sctree_sort_recursive(ctree, root);
1333
1334         if (GTK_CTREE_ROW(root)->parent) return;
1335
1336         SET_SPECIAL_FOLDER(ctree, folder->inbox);
1337         SET_SPECIAL_FOLDER(ctree, folder->outbox);
1338         SET_SPECIAL_FOLDER(ctree, folder->draft);
1339         SET_SPECIAL_FOLDER(ctree, folder->queue);
1340         SET_SPECIAL_FOLDER(ctree, folder->trash);
1341 }
1342
1343 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1344 {
1345         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1346         GtkCTreeNode *root;
1347
1348         g_return_if_fail(folder != NULL);
1349
1350         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1351                                       folderview_gnode_func, folderview);
1352         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1353                                 folderview);
1354         folderview_sort_folders(folderview, root, folder);
1355 }
1356
1357 void folderview_new_folder(FolderView *folderview)
1358 {
1359         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1360         FolderItem *item;
1361
1362         if (!folderview->selected) return;
1363
1364         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1365         g_return_if_fail(item != NULL);
1366         g_return_if_fail(item->folder != NULL);
1367
1368         switch (item->folder->type) {
1369         case F_MBOX:
1370                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1371                 break;
1372         case F_MH:
1373         case F_MAILDIR:
1374                 folderview_new_folder_cb(folderview, 0, NULL);
1375                 break;
1376         case F_IMAP:
1377                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1378                 break;
1379         case F_NEWS:
1380         default:
1381                 break;
1382         }
1383 }
1384
1385 void folderview_rename_folder(FolderView *folderview)
1386 {
1387         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1388         FolderItem *item;
1389
1390         if (!folderview->selected) return;
1391
1392         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1393         g_return_if_fail(item != NULL);
1394         g_return_if_fail(item->folder != NULL);
1395         if (!item->path) return;
1396         if (item->stype != F_NORMAL) return;
1397
1398         switch (item->folder->type) {
1399         case F_MBOX:
1400                 folderview_rename_mbox_folder_cb(folderview, 0, NULL);
1401         case F_MH:
1402         case F_MAILDIR:
1403         case F_IMAP:
1404                 folderview_rename_folder_cb(folderview, 0, NULL);
1405                 break;
1406         case F_NEWS:
1407         default:
1408                 break;
1409         }
1410 }
1411
1412 void folderview_delete_folder(FolderView *folderview)
1413 {
1414         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1415         FolderItem *item;
1416
1417         if (!folderview->selected) return;
1418
1419         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1420         g_return_if_fail(item != NULL);
1421         g_return_if_fail(item->folder != NULL);
1422         if (!item->path) return;
1423         if (item->stype != F_NORMAL) return;
1424
1425         switch (item->folder->type) {
1426         case F_MH:
1427         case F_MBOX:
1428         case F_MAILDIR:
1429         case F_IMAP:
1430                 folderview_delete_folder_cb(folderview, 0, NULL);
1431                 break;
1432         case F_NEWS:
1433         default:
1434                 break;
1435         }
1436 }
1437
1438
1439 /* callback functions */
1440
1441 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1442                                       FolderView *folderview)
1443 {
1444         GtkCList *clist = GTK_CLIST(ctree);
1445         gint prev_row = -1, row = -1, column = -1;
1446         FolderItem *item;
1447         Folder *folder;
1448         GtkWidget *popup;
1449         gboolean mark_all_read   = FALSE;
1450         gboolean new_folder      = FALSE;
1451         gboolean rename_folder   = FALSE;
1452         gboolean delete_folder   = FALSE;
1453         gboolean update_tree     = FALSE;
1454         gboolean rescan_tree     = FALSE;
1455         gboolean remove_tree     = FALSE;
1456         gboolean search_folder   = FALSE;
1457         gboolean folder_property = FALSE;
1458         gboolean folder_processing  = FALSE;
1459         gboolean folder_scoring  = FALSE;
1460
1461         if (!event) return;
1462
1463         if (event->button == 1) {
1464                 folderview->open_folder = TRUE;
1465                 return;
1466         }
1467
1468         if (event->button == 2 || event->button == 3) {
1469                 /* right clicked */
1470                 if (clist->selection) {
1471                         GtkCTreeNode *node;
1472
1473                         node = GTK_CTREE_NODE(clist->selection->data);
1474                         if (node)
1475                                 prev_row = gtkut_ctree_get_nth_from_node
1476                                         (GTK_CTREE(ctree), node);
1477                 }
1478
1479                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1480                                                   &row, &column))
1481                         return;
1482                 if (prev_row != row) {
1483                         gtk_clist_unselect_all(clist);
1484                         if (event->button == 2)
1485                                 folderview_select_node
1486                                         (folderview,
1487                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1488                                                             row));
1489                         else
1490                                 gtk_clist_select_row(clist, row, column);
1491                 }
1492         }
1493
1494         if (event->button != 3) return;
1495
1496         item = gtk_clist_get_row_data(clist, row);
1497         g_return_if_fail(item != NULL);
1498         g_return_if_fail(item->folder != NULL);
1499         folder = item->folder;
1500
1501         if (folderview->mainwin->lock_count == 0) {
1502                 new_folder = TRUE;
1503                 if (item->parent == NULL) {
1504                         update_tree = remove_tree = TRUE;
1505                         if (folder->account)
1506                                 folder_property = TRUE;
1507                 } else
1508                         mark_all_read = search_folder = folder_property = TRUE;
1509                         
1510                 if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
1511                         if (item->parent == NULL)
1512                                 update_tree = rescan_tree = TRUE;
1513                         else if (item->stype == F_NORMAL)
1514                                 rename_folder = delete_folder = folder_scoring = folder_processing = TRUE;
1515                         else if (item->stype == F_INBOX)
1516                                 folder_scoring = folder_processing = TRUE;
1517                         else if (item->stype == F_TRASH)
1518                                 folder_processing = TRUE;
1519                         else if (item->stype == F_OUTBOX)
1520                                 folder_processing = TRUE;
1521                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
1522                         if (item->parent != NULL)
1523                                 delete_folder = folder_scoring = folder_processing = TRUE;
1524                 }
1525                 if (item->unread < 1) 
1526                         mark_all_read = FALSE;
1527         }
1528
1529 #define SET_SENS(factory, name, sens) \
1530         menu_set_sensitive(folderview->factory, name, sens)
1531         
1532         mark_all_read = mark_all_read && 
1533                         (item == folderview->summaryview->folder_item);
1534
1535         if (FOLDER_IS_LOCAL(folder)) {
1536                 popup = folderview->mail_popup;
1537                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1538                 SET_SENS(mail_factory, "/Mark all read", mark_all_read);
1539                 SET_SENS(mail_factory, "/Create new folder...", new_folder);
1540                 SET_SENS(mail_factory, "/Rename folder...", rename_folder);
1541                 SET_SENS(mail_factory, "/Delete folder", delete_folder);
1542                 SET_SENS(mail_factory, "/Check for new messages", update_tree);
1543                 SET_SENS(mail_factory, "/Rebuild folder tree", rescan_tree);
1544                 SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
1545                 SET_SENS(mail_factory, "/Search folder...", search_folder);
1546                 SET_SENS(mail_factory, "/Properties...", folder_property);
1547                 SET_SENS(mail_factory, "/Processing...", folder_processing);
1548                 SET_SENS(mail_factory, "/Scoring...", folder_scoring);
1549         } else if (FOLDER_TYPE(folder) == F_IMAP) {
1550                 popup = folderview->imap_popup;
1551                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1552                 SET_SENS(imap_factory, "/Mark all read", mark_all_read);
1553                 SET_SENS(imap_factory, "/Create new folder...", new_folder);
1554                 SET_SENS(imap_factory, "/Rename folder...", rename_folder);
1555                 SET_SENS(imap_factory, "/Delete folder", delete_folder);
1556                 SET_SENS(imap_factory, "/Check for new messages", update_tree);
1557                 SET_SENS(imap_factory, "/Rebuild folder tree", rescan_tree);
1558                 SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
1559                 SET_SENS(imap_factory, "/Search folder...", search_folder);
1560                 SET_SENS(imap_factory, "/Properties...", folder_property);
1561                 SET_SENS(imap_factory, "/Processing...", folder_processing);
1562                 SET_SENS(imap_factory, "/Scoring...", folder_scoring);
1563         } else if (FOLDER_TYPE(folder) == F_NEWS) {
1564                 popup = folderview->news_popup;
1565                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1566                 SET_SENS(news_factory, "/Mark all read", mark_all_read);
1567                 SET_SENS(news_factory, "/Subscribe to newsgroup...", new_folder);
1568                 SET_SENS(news_factory, "/Remove newsgroup", delete_folder);
1569 #if 0
1570                 SET_SENS(news_factory, "/Check for new messages", update_tree);
1571 #endif
1572                 SET_SENS(news_factory, "/Remove news account", remove_tree);
1573                 SET_SENS(news_factory, "/Search folder...", search_folder);
1574                 SET_SENS(news_factory, "/Properties...", folder_property);
1575                 SET_SENS(news_factory, "/Processing...", folder_processing);
1576                 SET_SENS(news_factory, "/Scoring...", folder_scoring);
1577         } else if (FOLDER_TYPE(folder) == F_MBOX) {
1578                 popup = folderview->mbox_popup;
1579                 menu_set_insensitive_all(GTK_MENU_SHELL(popup));
1580                 SET_SENS(mbox_factory, "/Create new folder...", new_folder);
1581                 SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
1582                 SET_SENS(mbox_factory, "/Delete folder", delete_folder);
1583                 SET_SENS(news_factory, "/Properties...", folder_property);
1584                 SET_SENS(mbox_factory, "/Processing...", folder_processing);
1585                 SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
1586         } else
1587                 return;
1588
1589 #undef SET_SENS
1590
1591         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1592                        event->button, event->time);
1593 }
1594
1595 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1596                                        FolderView *folderview)
1597 {
1598         if (!event) return;
1599
1600         if (event->button == 1 && folderview->open_folder == FALSE &&
1601             folderview->opened != NULL) {
1602                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1603                                           folderview->opened);
1604                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1605         }
1606 }
1607
1608 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1609                                    FolderView *folderview)
1610 {
1611         if (!event) return;
1612
1613         switch (event->keyval) {
1614         case GDK_Return:
1615                 if (folderview->selected) {
1616                         folderview_select_node(folderview,
1617                                                folderview->selected);
1618                 }
1619                 break;
1620         case GDK_space:
1621                 if (folderview->selected) {
1622                         if (folderview->opened == folderview->selected &&
1623                             folderview->summaryview->messages == 0)
1624                                 folderview_select_next_unread(folderview);
1625                         else
1626                                 folderview_select_node(folderview,
1627                                                        folderview->selected);
1628                 }
1629                 break;
1630         default:
1631                 break;
1632         }
1633 }
1634
1635 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1636                                 gint column, FolderView *folderview)
1637 {
1638         static gboolean can_select = TRUE;      /* exclusive lock */
1639         gboolean opened;
1640         FolderItem *item;
1641         gchar *buf;
1642
1643         folderview->selected = row;
1644
1645         if (folderview->opened == row) {
1646                 folderview->open_folder = FALSE;
1647                 return;
1648         }
1649
1650         if (!can_select || summary_is_locked(folderview->summaryview)) {
1651                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1652                 gtk_ctree_select(ctree, folderview->opened);
1653                 return;
1654         }
1655
1656         if (!folderview->open_folder) return;
1657
1658         item = gtk_ctree_node_get_row_data(ctree, row);
1659         if (!item) return;
1660
1661         can_select = FALSE;
1662
1663         /* Save cache for old folder */
1664         /* We don't want to lose all caches if sylpheed crashed */
1665         if (folderview->opened) {
1666                 FolderItem *olditem;
1667                 
1668                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1669                 if (olditem) {
1670                         /* will be null if we just moved the previously opened folder */
1671                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1672                         folder_item_close(olditem);
1673                 }
1674         }
1675
1676         /* CLAWS: set compose button type: news folder items 
1677          * always have a news folder as parent */
1678         if (item->folder) 
1679                 toolbar_set_compose_button
1680                         (folderview->mainwin->toolbar,
1681                          item->folder->type == F_NEWS ? 
1682                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1683
1684         if (item->path)
1685                 debug_print("Folder %s is selected\n", item->path);
1686
1687         if (!GTK_CTREE_ROW(row)->children)
1688                 gtk_ctree_expand(ctree, row);
1689         if (folderview->opened &&
1690             !GTK_CTREE_ROW(folderview->opened)->children)
1691                 gtk_ctree_collapse(ctree, folderview->opened);
1692
1693         /* ungrab the mouse event */
1694         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1695                 gtk_grab_remove(GTK_WIDGET(ctree));
1696                 if (gdk_pointer_is_grabbed())
1697                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1698         }
1699
1700         /* Open Folder */
1701         buf = g_strdup_printf(_("Opening Folder %s..."), item->path);
1702         debug_print("%s\n", buf);
1703         STATUSBAR_PUSH(folderview->mainwin, buf);
1704         g_free(buf);
1705
1706         main_window_cursor_wait(folderview->mainwin);
1707
1708         if (folder_item_open(item) != 0) {
1709                 main_window_cursor_normal(folderview->mainwin);
1710                 STATUSBAR_POP(folderview->mainwin);
1711
1712                 alertpanel_error(_("Folder could not be opened."));
1713
1714                 return;
1715         }
1716
1717         main_window_cursor_normal(folderview->mainwin);
1718
1719         /* Show messages */
1720         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1721         opened = summary_show(folderview->summaryview, item);
1722
1723         folder_clean_cache_memory();
1724
1725         if (!opened) {
1726                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1727                 gtk_ctree_select(ctree, folderview->opened);
1728         } else {
1729                 folderview->opened = row;
1730                 if (gtk_ctree_node_is_visible(ctree, row)
1731                     != GTK_VISIBILITY_FULL)
1732                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1733         }
1734
1735         STATUSBAR_POP(folderview->mainwin);
1736
1737         folderview->open_folder = FALSE;
1738         can_select = TRUE;
1739 }
1740
1741 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1742                                      FolderView *folderview)
1743 {
1744         FolderItem *item;
1745
1746         item = gtk_ctree_node_get_row_data(ctree, node);
1747         g_return_if_fail(item != NULL);
1748         item->collapsed = FALSE;
1749         folderview_update_node(folderview, node);
1750 }
1751
1752 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1753                                       FolderView *folderview)
1754 {
1755         FolderItem *item;
1756
1757         item = gtk_ctree_node_get_row_data(ctree, node);
1758         g_return_if_fail(item != NULL);
1759         item->collapsed= TRUE;
1760         folderview_update_node(folderview, node);
1761 }
1762
1763 static void folderview_popup_close(GtkMenuShell *menu_shell,
1764                                    FolderView *folderview)
1765 {
1766         if (!folderview->opened) return;
1767
1768         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1769                                   folderview->opened);
1770         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1771 }
1772
1773 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1774                                    FolderView *folderview)
1775 {
1776         switch (column) {
1777         case COL_FOLDER:
1778                 prefs_common.folder_col_folder = width;
1779                 break;
1780         case COL_NEW:
1781                 prefs_common.folder_col_new = width;
1782                 break;
1783         case COL_UNREAD:
1784                 prefs_common.folder_col_unread = width;
1785                 break;
1786         case COL_TOTAL:
1787                 prefs_common.folder_col_total = width;
1788                 break;
1789         default:
1790                 break;
1791         }
1792 }
1793
1794 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1795                                              GtkCTreeNode *node,
1796                                              const gchar *name)
1797 {
1798         FolderItem *item;
1799
1800         if (!node)
1801                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1802         if (!node)
1803                 return NULL;
1804
1805         node = GTK_CTREE_ROW(node)->children;
1806
1807         while (node) {
1808                 item = gtk_ctree_node_get_row_data(ctree, node);
1809                 if (!folderview_compare_name(item, name))
1810                         return node;
1811                 node = GTK_CTREE_ROW(node)->sibling;
1812         }
1813
1814         return NULL;
1815 }
1816
1817 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1818                                       GtkWidget *widget)
1819 {
1820         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1821         FolderItem *item;
1822
1823         if (!folderview->selected) return;
1824
1825         summary_show(folderview->summaryview, NULL);
1826
1827         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1828         g_return_if_fail(item != NULL);
1829         g_return_if_fail(item->folder != NULL);
1830
1831         if (action == 0)
1832                 folderview_check_new(item->folder);
1833         else
1834                 folderview_rescan_tree(item->folder);
1835         
1836         if (folderview->opened) {
1837                 item = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1838                 if (item)
1839                         folder_update_item(item, TRUE);
1840         }
1841 }
1842
1843 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
1844 {
1845         GNode *srcnode;
1846
1847         folderview_create_folder_node(folderview, item);
1848         
1849         srcnode = item->folder->node;   
1850         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1851         srcnode = srcnode->children;
1852         while (srcnode != NULL) {
1853                 if (srcnode && srcnode->data) {
1854                         FolderItem *next_item = (FolderItem*) srcnode->data;
1855                         folderview_create_folder_node_recursive(folderview, next_item);
1856                 }
1857                 srcnode = srcnode->next;
1858         }
1859 }
1860
1861 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
1862 {
1863         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1864         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1865         GtkCTreeNode *node, *parent_node;
1866         
1867         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, item->parent);
1868
1869         gtk_clist_freeze(GTK_CLIST(ctree));
1870
1871         text[COL_FOLDER] = item->name;
1872         node = gtk_ctree_insert_node(ctree, parent_node, NULL, text,
1873                                      FOLDER_SPACING,
1874                                      folderxpm, folderxpmmask,
1875                                      folderopenxpm, folderopenxpmmask,
1876                                      FALSE, FALSE);
1877         gtk_ctree_expand(ctree, parent_node);
1878         gtk_ctree_node_set_row_data(ctree, node, item);
1879         if (normal_style)
1880                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
1881         folderview_sort_folders(folderview, folderview->selected, item->folder);
1882
1883         gtk_clist_thaw(GTK_CLIST(ctree));
1884 }
1885
1886 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1887                                      GtkWidget *widget)
1888 {
1889         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1890         FolderItem *item;
1891         FolderItem *new_item;
1892         gchar *new_folder;
1893         gchar *name, *name_;
1894
1895         if (!folderview->selected) return;
1896
1897         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1898         g_return_if_fail(item != NULL);
1899         g_return_if_fail(item->folder != NULL);
1900
1901         new_folder = input_dialog(_("New folder"),
1902                                   _("Input the name of new folder:"),
1903                                   _("NewFolder"));
1904         if (!new_folder) return;
1905
1906         if (item->folder->type != F_MBOX) {
1907                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1908                         alertpanel_error(_("`%c' can't be included in folder name."),
1909                                          G_DIR_SEPARATOR);
1910                         g_free(new_folder);
1911                         return;
1912                 }
1913         }
1914
1915         name_ = trim_string(new_folder, 32);
1916         Xstrdup_a(name, name_, {g_free(new_folder); return;});
1917         g_free(name_);
1918
1919         /* find whether the directory already exists */
1920         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1921                 alertpanel_error(_("The folder `%s' already exists."), name);
1922                 g_free(new_folder);
1923                 return;
1924         }
1925
1926         new_item = folder_create_folder(item, new_folder);
1927         if (!new_item) {
1928                 alertpanel_error(_("Can't create the folder `%s'."), name);
1929                 g_free(new_folder);
1930                 return;
1931         } 
1932         g_free(new_folder);
1933
1934         folderview_create_folder_node(folderview, new_item);
1935
1936         folder_write_list();
1937 }
1938
1939 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1940                                           GtkWidget *widget)
1941 {
1942         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1943         FolderItem *item;
1944         FolderItem *new_item;
1945         gchar *new_folder;
1946
1947         if (!folderview->selected) return;
1948
1949         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1950         g_return_if_fail(item != NULL);
1951         g_return_if_fail(item->folder != NULL);
1952
1953         new_folder = input_dialog(_("New folder"),
1954                                   _("Input the name of new folder:"),
1955                                   _("NewFolder"));
1956         if (!new_folder) return;
1957
1958         /* find whether the directory already exists */
1959         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1960                 alertpanel_error(_("The folder `%s' already exists."),
1961                                  new_folder);
1962                 g_free(new_folder);
1963                 return;
1964         }
1965
1966         new_item = folder_create_folder(item, new_folder);
1967         g_free(new_folder);
1968         if (!new_item) return;
1969
1970         folderview_create_folder_node(folderview, new_item);
1971
1972         folder_write_list();
1973 }
1974
1975 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1976                                         GtkWidget *widget)
1977 {
1978         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1979         FolderItem *item;
1980         gchar *new_folder;
1981         gchar *name, *name_;
1982         gchar *message;
1983         gchar *old_path;
1984         gchar *old_id;
1985         gchar *new_id;
1986
1987         if (!folderview->selected) return;
1988
1989         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1990         g_return_if_fail(item != NULL);
1991         g_return_if_fail(item->path != NULL);
1992         g_return_if_fail(item->folder != NULL);
1993
1994         name_ = trim_string(item->name, 32);
1995         Xstrdup_a(name, name_, return);
1996         g_free(name_);
1997         message = g_strdup_printf(_("Input new name for `%s':"), name);
1998         new_folder = input_dialog(_("Rename folder"), message,
1999                                   g_basename(item->path));
2000         g_free(message);
2001         if (!new_folder) return;
2002
2003         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
2004                 alertpanel_error(_("`%c' can't be included in folder name."),
2005                                  G_DIR_SEPARATOR);
2006                 g_free(new_folder);
2007                 return;
2008         }
2009
2010         if (folderview_find_by_name
2011                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2012                  new_folder)) {
2013                 name = trim_string(new_folder, 32);
2014                 alertpanel_error(_("The folder `%s' already exists."), name);
2015                 g_free(name);
2016                 g_free(new_folder);
2017                 return;
2018         }
2019
2020         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
2021         old_id = folder_item_get_identifier(item);
2022
2023         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2024                 g_free(old_id);
2025                 g_free(new_folder);
2026                 return;
2027         }
2028         g_free(new_folder);
2029
2030         /* if (FOLDER_TYPE(item->folder) == F_MH)
2031                 prefs_filtering_rename_path(old_path, item->path); */
2032         new_id = folder_item_get_identifier(item);
2033         prefs_filtering_rename_path(old_id, new_id);
2034
2035         g_free(old_id);
2036         g_free(new_id);
2037
2038         gtk_clist_freeze(GTK_CLIST(ctree));
2039
2040         folderview_update_node(folderview, folderview->selected);
2041         folderview_sort_folders(folderview,
2042                                 GTK_CTREE_ROW(folderview->selected)->parent,
2043                                 item->folder);
2044         if (folderview->opened == folderview->selected ||
2045             gtk_ctree_is_ancestor(ctree,
2046                                   folderview->selected,
2047                                   folderview->opened)) {
2048                 GtkCTreeNode *node = folderview->opened;
2049                 folderview_unselect(folderview);
2050                 folderview_select_node(folderview, node);
2051         }
2052
2053         gtk_clist_thaw(GTK_CLIST(ctree));
2054
2055         folder_write_list();
2056 }
2057
2058 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
2059                                              guint action,
2060                                              GtkWidget *widget)
2061 {
2062         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2063         FolderItem *item;
2064         gchar *new_folder;
2065         gchar *message;
2066
2067         if (!folderview->selected) return;
2068
2069         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2070         g_return_if_fail(item != NULL);
2071         g_return_if_fail(item->path != NULL);
2072         g_return_if_fail(item->folder != NULL);
2073
2074         message = g_strdup_printf(_("Input new name for `%s':"),
2075                                   g_basename(item->path));
2076         new_folder = input_dialog(_("Rename folder"), message,
2077                                   g_basename(item->path));
2078         g_free(message);
2079         if (!new_folder) return;
2080
2081         if (folderview_find_by_name
2082                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
2083                  new_folder)) {
2084                 alertpanel_error(_("The folder `%s' already exists."),
2085                                  new_folder);
2086                 g_free(new_folder);
2087                 return;
2088         }
2089
2090         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
2091                 g_free(new_folder);
2092                 return;
2093         }
2094         g_free(new_folder);
2095
2096         gtk_clist_freeze(GTK_CLIST(ctree));
2097
2098         folderview_update_node(folderview, folderview->selected);
2099         folderview_sort_folders(folderview,
2100                                 GTK_CTREE_ROW(folderview->selected)->parent,
2101                                 item->folder);
2102         if (folderview->opened == folderview->selected) {
2103                 if (!GTK_CTREE_ROW(folderview->opened)->children)
2104                         gtk_ctree_expand(ctree, folderview->opened);
2105                 summary_show(folderview->summaryview, item);
2106         }
2107
2108         gtk_clist_thaw(GTK_CLIST(ctree));
2109
2110         folder_write_list();
2111 }
2112
2113 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
2114                                         GtkWidget *widget)
2115 {
2116         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2117         FolderItem *item;
2118         gchar *message, *name, *name_;
2119         AlertValue avalue;
2120         gchar *old_path;
2121         gchar *old_id;
2122
2123         if (!folderview->selected) return;
2124
2125         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2126         g_return_if_fail(item != NULL);
2127         g_return_if_fail(item->path != NULL);
2128         g_return_if_fail(item->folder != NULL);
2129
2130         name_ = trim_string(item->name, 32);
2131         Xstrdup_a(name, name_, return);
2132         g_free(name_);
2133         message = g_strdup_printf
2134                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
2135                    "Do you really want to delete?"), name);
2136         avalue = alertpanel(_("Delete folder"), message,
2137                             _("Yes"), _("+No"), NULL);
2138         g_free(message);
2139         if (avalue != G_ALERTDEFAULT) return;
2140
2141         Xstrdup_a(old_path, item->path, return);
2142         old_id = folder_item_get_identifier(item);
2143
2144         if (item->folder->remove_folder(item->folder, item) < 0) {
2145                 alertpanel_error(_("Can't remove the folder `%s'."), name);
2146                 if (folderview->opened == folderview->selected)
2147                         summary_show(folderview->summaryview,
2148                                      folderview->summaryview->folder_item);
2149                 g_free(old_id);
2150                 return;
2151         }
2152
2153         /* if (FOLDER_TYPE(item->folder) == F_MH)
2154                 prefs_filtering_delete_path(old_path); */
2155
2156         if (folderview->opened == folderview->selected ||
2157             gtk_ctree_is_ancestor(ctree,
2158                                   folderview->selected,
2159                                   folderview->opened)) {
2160                 summary_clear_all(folderview->summaryview);
2161                 folderview->opened = NULL;
2162         }
2163
2164         gtk_ctree_remove_node(ctree, folderview->selected);
2165         folder_write_list();
2166
2167         prefs_filtering_delete_path(old_id);
2168         g_free(old_id);
2169
2170 }
2171
2172 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
2173                                          GtkWidget *widget)
2174 {
2175         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2176         GtkCTreeNode *node;
2177         FolderItem *item;
2178         gchar *name, *name_;
2179         gchar *message;
2180         AlertValue avalue;
2181
2182         if (!folderview->selected) return;
2183         node = folderview->selected;
2184         item = gtk_ctree_node_get_row_data(ctree, node);
2185         g_return_if_fail(item != NULL);
2186         g_return_if_fail(item->folder != NULL);
2187         if (item->parent) return;
2188
2189         name_ = trim_string(item->folder->name, 32);
2190         Xstrdup_a(name, name_, return);
2191         g_free(name_);
2192         message = g_strdup_printf
2193                 (_("Really remove the mailbox `%s' ?\n"
2194                    "(The messages are NOT deleted from the disk)"), name);
2195         avalue = alertpanel(_("Remove mailbox"), message,
2196                             _("Yes"), _("+No"), NULL);
2197         g_free(message);
2198         if (avalue != G_ALERTDEFAULT) return;
2199
2200         folder_destroy(item->folder);
2201         summary_clear_all(folderview->summaryview);
2202         folderview_unselect(folderview);
2203         gtk_ctree_remove_node(ctree, node);
2204         folder_write_list();
2205 }
2206
2207 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
2208                                           GtkWidget *widget)
2209 {
2210         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2211         FolderItem *item;
2212         FolderItem *new_item;
2213         gchar *new_folder;
2214         gchar *name, *name_;
2215         gchar *p;
2216
2217         if (!folderview->selected) return;
2218
2219         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2220         g_return_if_fail(item != NULL);
2221         g_return_if_fail(item->folder != NULL);
2222         g_return_if_fail(item->folder->type == F_IMAP);
2223         g_return_if_fail(item->folder->account != NULL);
2224
2225         new_folder = input_dialog
2226                 (_("New folder"),
2227                  _("Input the name of new folder:\n"
2228                    "(if you want to create a folder to store subfolders,\n"
2229                    " append `/' at the end of the name)"),
2230                  _("NewFolder"));
2231         if (!new_folder) return;
2232
2233         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2234             *(p + 1) != '\0') {
2235                 alertpanel_error(_("`%c' can't be included in folder name."),
2236                                  G_DIR_SEPARATOR);
2237                 g_free(new_folder);
2238                 return;
2239         }
2240
2241         name_ = trim_string(new_folder, 32);
2242         Xstrdup_a(name, name_, return);
2243         g_free(name_);
2244
2245         /* find whether the directory already exists */
2246         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2247                 alertpanel_error(_("The folder `%s' already exists."), name);
2248                 g_free(new_folder);
2249                 return;
2250         }
2251
2252         new_item = folder_create_folder(item, new_folder);
2253         if (!new_item) {
2254                 alertpanel_error(_("Can't create the folder `%s'."), name);
2255                 g_free(new_folder);
2256                 return;
2257         }
2258         g_free(new_folder);
2259
2260         folderview_create_folder_node(folderview, new_item);
2261
2262         folder_write_list();
2263 }
2264
2265 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2266                                          GtkWidget *widget)
2267 {
2268         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2269         FolderItem *item;
2270         PrefsAccount *account;
2271         gchar *name, *name_;
2272         gchar *message;
2273         AlertValue avalue;
2274
2275         if (!folderview->selected) return;
2276
2277         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2278         g_return_if_fail(item != NULL);
2279         g_return_if_fail(item->folder != NULL);
2280         g_return_if_fail(item->folder->type == F_IMAP);
2281         g_return_if_fail(item->folder->account != NULL);
2282
2283         name_ = trim_string(item->folder->name, 32);
2284         Xstrdup_a(name, name_, return);
2285         g_free(name_);
2286         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
2287         avalue = alertpanel(_("Delete IMAP4 account"), message,
2288                             _("Yes"), _("+No"), NULL);
2289         g_free(message);
2290
2291         if (avalue != G_ALERTDEFAULT) return;
2292
2293         if (folderview->opened == folderview->selected ||
2294             gtk_ctree_is_ancestor(ctree,
2295                                   folderview->selected,
2296                                   folderview->opened)) {
2297                 summary_clear_all(folderview->summaryview);
2298                 folderview->opened = NULL;
2299         }
2300
2301         account = item->folder->account;
2302         folder_destroy(item->folder);
2303         account_destroy(account);
2304         gtk_ctree_remove_node(ctree, folderview->selected);
2305         account_set_menu();
2306         main_window_reflect_prefs_all();
2307         folder_write_list();
2308 }
2309
2310 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2311                                          GtkWidget *widget)
2312 {
2313         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2314         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2315         GtkCTreeNode *servernode, *node;
2316         Folder *folder;
2317         FolderItem *item;
2318         FolderItem *rootitem;
2319         FolderItem *newitem;
2320         GSList *new_subscr;
2321         GSList *cur;
2322         GNode *gnode;
2323
2324         if (!folderview->selected) return;
2325
2326         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2327         g_return_if_fail(item != NULL);
2328         folder = item->folder;
2329         g_return_if_fail(folder != NULL);
2330         g_return_if_fail(folder->type == F_NEWS);
2331         g_return_if_fail(folder->account != NULL);
2332
2333         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2334                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2335         else
2336                 servernode = folderview->selected;
2337
2338         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2339
2340         new_subscr = grouplist_dialog(folder);
2341
2342         /* remove unsubscribed newsgroups */
2343         for (gnode = folder->node->children; gnode != NULL; ) {
2344                 GNode *next = gnode->next;
2345
2346                 item = FOLDER_ITEM(gnode->data);
2347                 if (g_slist_find_custom(new_subscr, item->path,
2348                                         (GCompareFunc)g_strcasecmp) != NULL) {
2349                         gnode = next;
2350                         continue;
2351                 }
2352
2353                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2354                 if (!node) {
2355                         gnode = next;
2356                         continue;
2357                 }
2358
2359                 if (folderview->opened == node) {
2360                         summary_clear_all(folderview->summaryview);
2361                         folderview->opened = NULL;
2362                 }
2363
2364                 folder_item_remove(item);
2365                 gtk_ctree_remove_node(ctree, node);
2366
2367                 gnode = next;
2368         }
2369
2370         gtk_clist_freeze(GTK_CLIST(ctree));
2371
2372         /* add subscribed newsgroups */
2373         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2374                 gchar *name = (gchar *)cur->data;
2375
2376                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2377                         continue;
2378
2379                 text[COL_FOLDER] = name;
2380                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2381                                              FOLDER_SPACING,
2382                                              folderxpm, folderxpmmask,
2383                                              folderopenxpm, folderopenxpmmask,
2384                                              FALSE, FALSE);
2385                 gtk_ctree_expand(ctree, servernode);
2386
2387                 newitem = folder_item_new(folder, name, name);
2388                 folder_item_append(rootitem, newitem);
2389                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2390         }
2391
2392         folderview_sort_folders(folderview, servernode, folder);
2393         gtk_clist_thaw(GTK_CLIST(ctree));
2394
2395         slist_free_strings(new_subscr);
2396         g_slist_free(new_subscr);
2397
2398         folder_write_list();
2399 }
2400
2401 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2402                                         GtkWidget *widget)
2403 {
2404         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2405         FolderItem *item;
2406         gchar *name, *name_;
2407         gchar *message;
2408         AlertValue avalue;
2409
2410         if (!folderview->selected) return;
2411
2412         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2413         g_return_if_fail(item != NULL);
2414         g_return_if_fail(item->folder != NULL);
2415         g_return_if_fail(item->folder->type == F_NEWS);
2416         g_return_if_fail(item->folder->account != NULL);
2417
2418         name_ = trim_string(item->path, 32);
2419         Xstrdup_a(name, name_, return);
2420         g_free(name_);
2421         message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
2422         avalue = alertpanel(_("Delete newsgroup"), message,
2423                             _("Yes"), _("+No"), NULL);
2424         g_free(message);
2425         if (avalue != G_ALERTDEFAULT) return;
2426
2427         if (folderview->opened == folderview->selected) {
2428                 summary_clear_all(folderview->summaryview);
2429                 folderview->opened = NULL;
2430         }
2431
2432         folder_item_remove(item);
2433         gtk_ctree_remove_node(ctree, folderview->selected);
2434         folder_write_list();
2435 }
2436
2437 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2438                                          GtkWidget *widget)
2439 {
2440         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2441         FolderItem *item;
2442         PrefsAccount *account;
2443         gchar *name, *name_;
2444         gchar *message;
2445         AlertValue avalue;
2446
2447         if (!folderview->selected) return;
2448
2449         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2450         g_return_if_fail(item != NULL);
2451         g_return_if_fail(item->folder != NULL);
2452         g_return_if_fail(item->folder->type == F_NEWS);
2453         g_return_if_fail(item->folder->account != NULL);
2454
2455         name_ = trim_string(item->folder->name, 32);
2456         Xstrdup_a(name, name_, return);
2457         g_free(name_);
2458         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
2459         avalue = alertpanel(_("Delete news account"), message,
2460                             _("Yes"), _("+No"), NULL);
2461         g_free(message);
2462
2463         if (avalue != G_ALERTDEFAULT) return;
2464
2465         if (folderview->opened == folderview->selected ||
2466             gtk_ctree_is_ancestor(ctree,
2467                                   folderview->selected,
2468                                   folderview->opened)) {
2469                 summary_clear_all(folderview->summaryview);
2470                 folderview->opened = NULL;
2471         }
2472
2473         account = item->folder->account;
2474         folder_destroy(item->folder);
2475         account_destroy(account);
2476         gtk_ctree_remove_node(ctree, folderview->selected);
2477         account_set_menu();
2478         main_window_reflect_prefs_all();
2479         folder_write_list();
2480 }
2481
2482 static void folderview_search_cb(FolderView *folderview, guint action,
2483                                  GtkWidget *widget)
2484 {
2485         summary_search(folderview->summaryview);
2486 }
2487
2488 static void folderview_property_cb(FolderView *folderview, guint action,
2489                                    GtkWidget *widget)
2490 {
2491         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2492         FolderItem *item;
2493
2494         if (!folderview->selected) return;
2495
2496         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2497         g_return_if_fail(item != NULL);
2498         g_return_if_fail(item->folder != NULL);
2499
2500 #if CLAWS
2501         prefs_folder_item_create(folderview, item);
2502 #else
2503         /*
2504          * CLAWS: wait till Hiro has completed his stuff
2505          */
2506         if (item->parent == NULL && item->folder->account)
2507                 account_open(item->folder->account);
2508         else
2509                 prefs_folder_item_open(item);
2510 #endif  
2511 }
2512
2513 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2514 {
2515         GSList *list = NULL;
2516         GSList *done = NULL;
2517         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2518         
2519         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2520                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2521                 &&  list->data != node) {
2522                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2523                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2524                 }
2525         }
2526         for (list = done; list != NULL; list = g_slist_next(list)) {
2527                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2528                                                                  list->data);
2529         }
2530         g_slist_free(done);
2531 }
2532
2533 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2534                                           GdkDragContext *context,
2535                                           gint            x,
2536                                           gint            y,
2537                                           guint           time,
2538                                           FolderView     *folderview)
2539 {
2540         gint row, column;
2541         FolderItem *item, *src_item;
2542         GtkCTreeNode *node = NULL;
2543         gboolean acceptable = FALSE;
2544
2545         if (gtk_clist_get_selection_info
2546                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2547                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2548                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2549                 src_item = folderview->summaryview->folder_item;
2550
2551                 if (item && item->folder && item->path &&
2552                     src_item && src_item != item) {
2553                         switch (item->folder->type) {
2554                         case F_MH:
2555                         case F_IMAP:
2556                                 acceptable = TRUE;
2557                                 break;
2558                         default:
2559                                 break;
2560                         }
2561                 } else if (item && item->folder && folder_item_get_path(item) &&
2562                            src_item && src_item != item) {
2563                         /* a root folder - acceptable only from folderview */
2564                         if (item->folder->type == F_MH || item->folder->type == F_IMAP)
2565                                 acceptable = TRUE;
2566                 }
2567                         
2568         }
2569
2570         if (acceptable || (src_item && src_item == item)) {
2571                 folderview_recollapse_nodes(folderview, node);
2572                 if (item->collapsed) {
2573                         gtk_ctree_expand(GTK_CTREE(widget), node);
2574                         folderview->nodes_to_recollapse = g_slist_append(
2575                                                                 folderview->nodes_to_recollapse,
2576                                                                 node);
2577                 }
2578         }
2579                 
2580         if (acceptable) {
2581                 gtk_signal_handler_block_by_func
2582                         (GTK_OBJECT(widget),
2583                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2584                 gtk_ctree_select(GTK_CTREE(widget), node);
2585                 gtk_signal_handler_unblock_by_func
2586                         (GTK_OBJECT(widget),
2587                          GTK_SIGNAL_FUNC(folderview_selected), folderview);
2588                 gdk_drag_status(context, 
2589                                         (context->actions == GDK_ACTION_COPY ?
2590                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2591         } else {
2592                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2593                 gdk_drag_status(context, 0, time);
2594         }
2595
2596         return acceptable;
2597 }
2598
2599 static void folderview_drag_leave_cb(GtkWidget      *widget,
2600                                      GdkDragContext *context,
2601                                      guint           time,
2602                                      FolderView     *folderview)
2603 {
2604         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2605 }
2606
2607 static void folderview_drag_received_cb(GtkWidget        *widget,
2608                                         GdkDragContext   *drag_context,
2609                                         gint              x,
2610                                         gint              y,
2611                                         GtkSelectionData *data,
2612                                         guint             info,
2613                                         guint             time,
2614                                         FolderView       *folderview)
2615 {
2616         gint row, column;
2617         FolderItem *item, *src_item;
2618         GtkCTreeNode *node;
2619
2620         if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2621                 /* comes from summaryview */
2622                 if (gtk_clist_get_selection_info
2623                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2624                         return;
2625
2626                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2627                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2628                 src_item = folderview->summaryview->folder_item;
2629                 
2630                 /* re-check (due to acceptable possibly set for folder moves */
2631                 if (!(item && item->folder && item->path &&
2632                       src_item && src_item != item && 
2633                       (item->folder->type == F_MH || item->folder->type == F_IMAP))) {
2634                         return;
2635                 }
2636                 if (item && src_item) {
2637                         switch (drag_context->action) {
2638                                 case GDK_ACTION_COPY:
2639                                         summary_copy_selected_to(folderview->summaryview, item);
2640                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2641                                         break;
2642                                 case GDK_ACTION_MOVE:
2643                                 case GDK_ACTION_DEFAULT:
2644                                 default:
2645                         if (src_item->folder->type != item->folder->type ||
2646                             (item->folder->type == F_IMAP &&
2647                              src_item->folder != item->folder))
2648                                 summary_copy_selected_to(folderview->summaryview, item);
2649                         else
2650                                 summary_move_selected_to(folderview->summaryview, item);
2651                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2652                         }
2653                 } else
2654                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2655         } else {
2656                 /* comes from folderview */
2657                 char *source;
2658                 char *buf;
2659                 gint status;
2660                 GtkCTreeNode *src_node;
2661                 FolderItem *new_item, *src_parent;
2662                 
2663                 source = data->data + 17;
2664                 if (gtk_clist_get_selection_info
2665                     (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2666                     || *source == 0) {
2667                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2668                         return;
2669                 }
2670                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2671                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2672                 src_item = folder_find_item_from_identifier(source);
2673
2674                 if (!item || !src_item || src_item->stype != F_NORMAL) {
2675                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2676                         return;
2677                 }
2678                 src_parent = src_item->parent;
2679                 src_node = gtk_ctree_find_by_row_data(GTK_CTREE(widget), NULL, src_item);
2680
2681                 buf = g_strdup_printf(_("Moving %s to %s..."), src_item->name, item->name);
2682                 STATUSBAR_PUSH(folderview->mainwin, buf);
2683                 g_free(buf);
2684                 summary_clear_all(folderview->summaryview);
2685                 folderview->opened = NULL;
2686                 folderview->selected = NULL;
2687                 gtk_widget_set_sensitive(folderview->ctree, FALSE);
2688                 inc_lock();
2689                 main_window_cursor_wait(folderview->mainwin);
2690                 if ((status = folder_item_move_to(src_item, item, &new_item)) == F_MOVE_OK) {
2691                         main_window_cursor_normal(folderview->mainwin);
2692                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2693                 
2694                         if (src_node)
2695                                 gtk_ctree_remove_node(GTK_CTREE(widget), src_node);
2696                         else 
2697                                 debug_print("can't remove src node: is null\n");
2698
2699                         STATUSBAR_POP(folderview->mainwin);
2700                         folderview_create_folder_node_recursive(folderview, new_item);
2701                         folder_update_item(src_parent, TRUE);
2702                         folder_update_item_recursive(new_item, TRUE); 
2703                         folderview_sort_folders(folderview, 
2704                                 gtk_ctree_find_by_row_data(GTK_CTREE(widget), 
2705                                         NULL, new_item->parent), new_item->folder);
2706                         folderview_select(folderview, new_item);
2707                 } else {
2708                         main_window_cursor_normal(folderview->mainwin);
2709                         gtk_drag_finish(drag_context, FALSE, FALSE, time);
2710                         STATUSBAR_POP(folderview->mainwin);
2711                         switch (status) {
2712                         case F_MOVE_FAILED_DEST_IS_PARENT:
2713                                 alertpanel_error(_("Source and destination are the same."));
2714                                 break;
2715                         case F_MOVE_FAILED_DEST_IS_CHILD:
2716                                 alertpanel_error(_("Can't move a folder to one of its children."));
2717                                 break;
2718                         default:
2719                                 alertpanel_error(_("Move failed!"));
2720                                 break;
2721                         }
2722                 }       
2723                 inc_unlock();           
2724                 gtk_widget_set_sensitive(folderview->ctree, TRUE);
2725         }
2726         folderview->nodes_to_recollapse = NULL;
2727 }
2728
2729 static gint folderview_clist_compare(GtkCList *clist,
2730                                      gconstpointer ptr1, gconstpointer ptr2)
2731 {
2732         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2733         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2734
2735         if (!item1->name)
2736                 return (item2->name != NULL);
2737         if (!item2->name)
2738                 return -1;
2739
2740         return g_strcasecmp(item1->name, item2->name);
2741 }
2742
2743 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2744 {
2745         const FolderItem *item = a;
2746         const gchar *name = b;
2747
2748         if (!item->path) return -1;
2749         return strcmp2(g_basename(item->path), name);
2750 }
2751
2752 static void folderview_scoring_cb(FolderView *folderview, guint action,
2753                                    GtkWidget *widget)
2754 {
2755         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2756         FolderItem *item;
2757
2758         if (!folderview->selected) return;
2759
2760         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2761         g_return_if_fail(item != NULL);
2762         g_return_if_fail(item->folder != NULL);
2763
2764         prefs_scoring_open(item);
2765 }
2766
2767 static void folderview_processing_cb(FolderView *folderview, guint action,
2768                                      GtkWidget *widget)
2769 {
2770         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2771         FolderItem *item;
2772
2773         if (!folderview->selected) return;
2774
2775         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2776         g_return_if_fail(item != NULL);
2777         g_return_if_fail(item->folder != NULL);
2778
2779         prefs_filtering_open(item, NULL, NULL);
2780 }
2781
2782 void folderview_set_target_folder_color(gint color_op) 
2783 {
2784         gint firstone = 1;
2785         GList *list;
2786         FolderView *folderview;
2787
2788         for (list = folderview_list; list != NULL; list = list->next) {
2789                 folderview = (FolderView *)list->data;
2790                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2791                 if (firstone) {
2792                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2793                                 folderview->color_op;
2794                         firstone = 0;
2795                 }
2796         }
2797 }
2798
2799 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2800 {
2801         folderview_init(folderview);
2802         folderview_set_all();
2803 }
2804
2805 GtkTargetEntry folderview_drag_types[1] =
2806 {
2807         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
2808 };
2809
2810
2811 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2812                                   FolderView       *folderview)
2813 {
2814         GtkTargetList *list;
2815         GdkDragContext *context;
2816
2817         g_return_if_fail(folderview != NULL);
2818         if (folderview->selected == NULL) return;
2819
2820         list = gtk_target_list_new(folderview_drag_types, 1);
2821
2822         folderview->nodes_to_recollapse = NULL; /* in case the last drag has been cancelled */
2823         
2824         context = gtk_drag_begin(widget, list,
2825                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2826         gtk_drag_set_icon_default(context);
2827 }
2828
2829 static void folderview_drag_data_get(GtkWidget        *widget,
2830                                      GdkDragContext   *drag_context,
2831                                      GtkSelectionData *selection_data,
2832                                      guint             info,
2833                                      guint             time,
2834                                      FolderView       *folderview)
2835 {
2836         FolderItem *item;
2837         GList *cur;
2838         gchar *source=NULL;
2839         
2840         for (cur = GTK_CLIST(folderview->ctree)->selection;
2841              cur != NULL; cur = cur->next) {
2842                 item = gtk_ctree_node_get_row_data
2843                         (GTK_CTREE(folderview->ctree), 
2844                          GTK_CTREE_NODE(cur->data));
2845                 if (item) {
2846                         source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2847                         gtk_selection_data_set(selection_data,
2848                                                selection_data->target, 8,
2849                                                source, strlen(source));
2850                         break;
2851                 } else
2852                         return;
2853         }
2854 }
2855