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