Improved and fixed folder view 'Mark all read'
[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 #define BREAK_ON_MODIFIER_KEY() \
1480         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1481
1482 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1483                                    FolderView *folderview)
1484 {
1485         if (!event) return;
1486
1487         switch (event->keyval) {
1488         case GDK_Return:
1489                 if (folderview->selected) {
1490                         folderview_select_node(folderview,
1491                                                folderview->selected);
1492                 }
1493                 break;
1494         case GDK_space:
1495                 if (folderview->selected) {
1496                         if (folderview->opened == folderview->selected &&
1497                             folderview->summaryview->messages == 0)
1498                                 folderview_select_next_unread(folderview);
1499                         else
1500                                 folderview_select_node(folderview,
1501                                                        folderview->selected);
1502                 }
1503                 break;
1504         case GDK_v:
1505         case GDK_V:
1506         case GDK_g:
1507         case GDK_G:
1508         case GDK_x:
1509         case GDK_X:
1510         case GDK_w:
1511         case GDK_D:
1512         case GDK_Q:
1513                 BREAK_ON_MODIFIER_KEY();
1514                 summary_pass_key_press_event(folderview->summaryview, event);
1515         default:
1516         }
1517 }
1518
1519 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1520                                 gint column, FolderView *folderview)
1521 {
1522         static gboolean can_select = TRUE;      /* exclusive lock */
1523         gboolean opened;
1524         FolderItem *item;
1525
1526         folderview->selected = row;
1527
1528         if (folderview->opened == row) {
1529                 folderview->open_folder = FALSE;
1530                 return;
1531         }
1532
1533         if (!can_select) {
1534                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1535                 gtk_ctree_select(ctree, folderview->opened);
1536                 return;
1537         }
1538
1539         if (!folderview->open_folder) return;
1540
1541         item = gtk_ctree_node_get_row_data(ctree, row);
1542         if (!item) return;
1543
1544         can_select = FALSE;
1545
1546         /* CLAWS: set compose button type: news folder items 
1547          * always have a news folder as parent */
1548         if (item->folder) 
1549                 main_window_toolbar_set_compose_button
1550                         (folderview->mainwin,
1551                          item->folder->type == F_NEWS ? 
1552                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1553          
1554         if (item->path)
1555                 debug_print(_("Folder %s is selected\n"), item->path);
1556
1557         if (!GTK_CTREE_ROW(row)->children)
1558                 gtk_ctree_expand(ctree, row);
1559         if (folderview->opened &&
1560             !GTK_CTREE_ROW(folderview->opened)->children)
1561                 gtk_ctree_collapse(ctree, folderview->opened);
1562
1563         /* ungrab the mouse event */
1564         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1565                 gtk_grab_remove(GTK_WIDGET(ctree));
1566                 if (gdk_pointer_is_grabbed())
1567                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1568         }
1569
1570         opened = summary_show(folderview->summaryview, item, FALSE);
1571
1572         if (!opened) {
1573                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1574                 gtk_ctree_select(ctree, folderview->opened);
1575         } else
1576                 folderview->opened = row;
1577
1578         folderview->open_folder = FALSE;
1579         can_select = TRUE;
1580 }
1581
1582 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1583                                      FolderView *folderview)
1584 {
1585         FolderItem *item;
1586
1587         item = gtk_ctree_node_get_row_data(ctree, node);
1588         g_return_if_fail(item != NULL);
1589         item->collapsed = FALSE;
1590         folderview_update_node(folderview, node);
1591 }
1592
1593 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1594                                       FolderView *folderview)
1595 {
1596         FolderItem *item;
1597
1598         item = gtk_ctree_node_get_row_data(ctree, node);
1599         g_return_if_fail(item != NULL);
1600         item->collapsed= TRUE;
1601         folderview_update_node(folderview, node);
1602 }
1603
1604 static void folderview_popup_close(GtkMenuShell *menu_shell,
1605                                    FolderView *folderview)
1606 {
1607         if (!folderview->opened) return;
1608
1609         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1610                                   folderview->opened);
1611         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1612 }
1613
1614 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1615                                    FolderView *folderview)
1616 {
1617         switch (column) {
1618         case COL_FOLDER:
1619                 prefs_common.folder_col_folder = width;
1620                 break;
1621         case COL_NEW:
1622                 prefs_common.folder_col_new = width;
1623                 break;
1624         case COL_UNREAD:
1625                 prefs_common.folder_col_unread = width;
1626                 break;
1627         case COL_TOTAL:
1628                 prefs_common.folder_col_total = width;
1629                 break;
1630         default:
1631         }
1632 }
1633
1634 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1635                                              GtkCTreeNode *node,
1636                                              const gchar *name)
1637 {
1638         FolderItem *item;
1639
1640         if (!node)
1641                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1642         if (!node)
1643                 return NULL;
1644
1645         node = GTK_CTREE_ROW(node)->children;
1646
1647         while (node) {
1648                 item = gtk_ctree_node_get_row_data(ctree, node);
1649                 if (!folderview_compare_name(item, name))
1650                         return node;
1651                 node = GTK_CTREE_ROW(node)->sibling;
1652         }
1653
1654         return NULL;
1655 }
1656
1657 static void folderview_update_tree_cb(FolderView *folderview, guint action,
1658                                       GtkWidget *widget)
1659 {
1660         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1661         FolderItem *item;
1662
1663         if (!folderview->selected) return;
1664
1665         summary_show(folderview->summaryview, NULL, FALSE);
1666
1667         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1668         g_return_if_fail(item != NULL);
1669         g_return_if_fail(item->folder != NULL);
1670
1671         if (action == 0)
1672                 folderview_update_all_node();
1673         else
1674                 folderview_update_tree(item->folder);
1675 }
1676
1677 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1678                                      GtkWidget *widget)
1679 {
1680         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1681         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1682         FolderItem *item;
1683         FolderItem *new_item;
1684         gchar *new_folder;
1685         GtkCTreeNode *node;
1686
1687         if (!folderview->selected) return;
1688
1689         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1690         g_return_if_fail(item != NULL);
1691         g_return_if_fail(item->folder != NULL);
1692
1693         new_folder = input_dialog(_("New folder"),
1694                                   _("Input the name of new folder:"),
1695                                   _("NewFolder"));
1696         if (!new_folder) return;
1697
1698         if (item->folder->type != F_MBOX) {
1699                 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1700                         alertpanel_error(_("`%c' can't be included in folder name."),
1701                                          G_DIR_SEPARATOR);
1702                         g_free(new_folder);
1703                         return;
1704                 }
1705         }
1706
1707         /* find whether the directory already exists */
1708         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1709                 alertpanel_error(_("The folder `%s' already exists."),
1710                                  new_folder);
1711                 g_free(new_folder);
1712                 return;
1713         }
1714
1715         new_item = item->folder->create_folder(item->folder, item, new_folder);
1716         if (!new_item) {
1717                 alertpanel_error(_("The folder `%s' could not be created."), 
1718                                  new_folder);
1719                 g_free(new_folder);
1720                 return;
1721         } 
1722         g_free(new_folder);
1723
1724         gtk_clist_freeze(GTK_CLIST(ctree));
1725
1726         text[COL_FOLDER] = new_item->name;
1727         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1728                                      FOLDER_SPACING,
1729                                      folderxpm, folderxpmmask,
1730                                      folderopenxpm, folderopenxpmmask,
1731                                      FALSE, FALSE);
1732         gtk_ctree_expand(ctree, folderview->selected);
1733         gtk_ctree_node_set_row_data(ctree, node, new_item);
1734         folderview_sort_folders(folderview, folderview->selected, item->folder);
1735
1736         gtk_clist_thaw(GTK_CLIST(ctree));
1737
1738         folder_write_list();
1739 }
1740
1741 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1742                                           GtkWidget *widget)
1743 {
1744         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1745         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1746         FolderItem *item;
1747         FolderItem *new_item;
1748         gchar *new_folder;
1749         GtkCTreeNode *node;
1750
1751         if (!folderview->selected) return;
1752
1753         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1754         g_return_if_fail(item != NULL);
1755         g_return_if_fail(item->folder != NULL);
1756
1757         new_folder = input_dialog(_("New folder"),
1758                                   _("Input the name of new folder:"),
1759                                   _("NewFolder"));
1760         if (!new_folder) return;
1761
1762         /* find whether the directory already exists */
1763         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1764                 alertpanel_error(_("The folder `%s' already exists."),
1765                                  new_folder);
1766                 g_free(new_folder);
1767                 return;
1768         }
1769
1770         new_item = item->folder->create_folder(item->folder, item, new_folder);
1771         g_free(new_folder);
1772         if (!new_item) return;
1773
1774         gtk_clist_freeze(GTK_CLIST(ctree));
1775
1776         text[COL_FOLDER] = new_item->name;
1777         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1778                                      FOLDER_SPACING,
1779                                      folderxpm, folderxpmmask,
1780                                      folderopenxpm, folderopenxpmmask,
1781                                      FALSE, FALSE);
1782         gtk_ctree_expand(ctree, folderview->selected);
1783         gtk_ctree_node_set_row_data(ctree, node, new_item);
1784         folderview_sort_folders(folderview, folderview->selected, item->folder);
1785
1786         gtk_clist_thaw(GTK_CLIST(ctree));
1787
1788         folder_write_list();
1789 }
1790
1791 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1792                                         GtkWidget *widget)
1793 {
1794         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1795         FolderItem *item;
1796         gchar *new_folder;
1797         gchar *message;
1798
1799         if (!folderview->selected) return;
1800
1801         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1802         g_return_if_fail(item != NULL);
1803         g_return_if_fail(item->path != NULL);
1804         g_return_if_fail(item->folder != NULL);
1805
1806         message = g_strdup_printf(_("Input new name for `%s':"),
1807                                   g_basename(item->path));
1808         new_folder = input_dialog(_("Rename folder"), message,
1809                                   g_basename(item->path));
1810         g_free(message);
1811         if (!new_folder) return;
1812
1813         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1814                 alertpanel_error(_("`%c' can't be included in folder name."),
1815                                  G_DIR_SEPARATOR);
1816                 g_free(new_folder);
1817                 return;
1818         }
1819
1820         if (folderview_find_by_name
1821                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1822                  new_folder)) {
1823                 alertpanel_error(_("The folder `%s' already exists."),
1824                                  new_folder);
1825                 g_free(new_folder);
1826                 return;
1827         }
1828
1829         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1830                 g_free(new_folder);
1831                 return;
1832         }
1833         g_free(new_folder);
1834
1835         gtk_clist_freeze(GTK_CLIST(ctree));
1836
1837         folderview_update_node(folderview, folderview->selected);
1838         folderview_sort_folders(folderview,
1839                                 GTK_CTREE_ROW(folderview->selected)->parent,
1840                                 item->folder);
1841         if (folderview->opened == folderview->selected) {
1842                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1843                         gtk_ctree_expand(ctree, folderview->opened);
1844                 summary_show(folderview->summaryview, item, FALSE);
1845         }
1846
1847         gtk_clist_thaw(GTK_CLIST(ctree));
1848
1849         folder_write_list();
1850 }
1851
1852 static void folderview_rename_mbox_folder_cb(FolderView *folderview,
1853                                              guint action,
1854                                              GtkWidget *widget)
1855 {
1856         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1857         FolderItem *item;
1858         gchar *new_folder;
1859         gchar *message;
1860
1861         if (!folderview->selected) return;
1862
1863         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1864         g_return_if_fail(item != NULL);
1865         g_return_if_fail(item->path != NULL);
1866         g_return_if_fail(item->folder != NULL);
1867
1868         message = g_strdup_printf(_("Input new name for `%s':"),
1869                                   g_basename(item->path));
1870         new_folder = input_dialog(_("Rename folder"), message,
1871                                   g_basename(item->path));
1872         g_free(message);
1873         if (!new_folder) return;
1874
1875         if (folderview_find_by_name
1876                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1877                  new_folder)) {
1878                 alertpanel_error(_("The folder `%s' already exists."),
1879                                  new_folder);
1880                 g_free(new_folder);
1881                 return;
1882         }
1883
1884         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1885                 g_free(new_folder);
1886                 return;
1887         }
1888         g_free(new_folder);
1889
1890         gtk_clist_freeze(GTK_CLIST(ctree));
1891
1892         folderview_update_node(folderview, folderview->selected);
1893         folderview_sort_folders(folderview,
1894                                 GTK_CTREE_ROW(folderview->selected)->parent,
1895                                 item->folder);
1896         if (folderview->opened == folderview->selected) {
1897                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1898                         gtk_ctree_expand(ctree, folderview->opened);
1899                 summary_show(folderview->summaryview, item, FALSE);
1900         }
1901
1902         gtk_clist_thaw(GTK_CLIST(ctree));
1903
1904         folder_write_list();
1905 }
1906
1907 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1908                                         GtkWidget *widget)
1909 {
1910         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1911         FolderItem *item;
1912         gchar *message;
1913         AlertValue avalue;
1914
1915         if (!folderview->selected) return;
1916
1917         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1918         g_return_if_fail(item != NULL);
1919         g_return_if_fail(item->path != NULL);
1920         g_return_if_fail(item->folder != NULL);
1921
1922         message = g_strdup_printf
1923                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1924                    "Do you really want to delete?"),
1925                  g_basename(item->path));
1926         avalue = alertpanel(_("Delete folder"), message,
1927                             _("Yes"), _("+No"), NULL);
1928         g_free(message);
1929         if (avalue != G_ALERTDEFAULT) return;
1930
1931         if (item->folder->remove_folder(item->folder, item) < 0) {
1932                 alertpanel_error(_("Can't remove the folder `%s'."),
1933                                  item->path);
1934                 return;
1935         }
1936
1937         if (folderview->opened == folderview->selected ||
1938             gtk_ctree_is_ancestor(ctree,
1939                                   folderview->selected,
1940                                   folderview->opened)) {
1941                 summary_clear_all(folderview->summaryview);
1942                 folderview->opened = NULL;
1943         }
1944
1945         gtk_ctree_remove_node(ctree, folderview->selected);
1946         folder_write_list();
1947 }
1948
1949 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
1950                                          GtkWidget *widget)
1951 {
1952         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1953         GtkCTreeNode *node;
1954         FolderItem *item;
1955         gchar *message;
1956         AlertValue avalue;
1957
1958         if (!folderview->selected) return;
1959         node = folderview->selected;
1960         item = gtk_ctree_node_get_row_data(ctree, node);
1961         g_return_if_fail(item != NULL);
1962         g_return_if_fail(item->folder != NULL);
1963         if (item->parent) return;
1964
1965         message = g_strdup_printf
1966                 (_("Really remove the mailbox `%s' ?\n"
1967                    "(The messages are NOT deleted from disk)"),
1968                  item->folder->name);
1969         avalue = alertpanel(_("Remove folder"), message,
1970                             _("Yes"), _("+No"), NULL);
1971         g_free(message);
1972         if (avalue != G_ALERTDEFAULT) return;
1973
1974         folder_destroy(item->folder);
1975         summary_clear_all(folderview->summaryview);
1976         folderview_unselect(folderview);
1977         gtk_ctree_remove_node(ctree, node);
1978         folder_write_list();
1979 }
1980
1981 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
1982                                           GtkWidget *widget)
1983 {
1984         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1985         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1986         GtkCTreeNode *node;
1987         FolderItem *item;
1988         FolderItem *new_item;
1989         gchar *new_folder;
1990         gchar *p;
1991
1992         if (!folderview->selected) return;
1993
1994         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1995         g_return_if_fail(item != NULL);
1996         g_return_if_fail(item->folder != NULL);
1997         g_return_if_fail(item->folder->type == F_IMAP);
1998         g_return_if_fail(item->folder->account != NULL);
1999
2000         new_folder = input_dialog
2001                 (_("New folder"),
2002                  _("Input the name of new folder:\n"
2003                    "(if you want to create a folder to store subfolders,\n"
2004                    " append `/' at the end of the name)"),
2005                  _("NewFolder"));
2006         if (!new_folder) return;
2007
2008         if ((p = strchr(new_folder, G_DIR_SEPARATOR)) != NULL &&
2009             *(p + 1) != '\0') {
2010                 alertpanel_error(_("`%c' can't be included in folder name."),
2011                                  G_DIR_SEPARATOR);
2012                 g_free(new_folder);
2013                 return;
2014         }
2015
2016         /* find whether the directory already exists */
2017         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
2018                 alertpanel_error(_("The folder `%s' already exists."),
2019                                  new_folder);
2020                 g_free(new_folder);
2021                 return;
2022         }
2023
2024         new_item = item->folder->create_folder(item->folder, item, new_folder);
2025         if (!new_item) {
2026                 alertpanel_error(_("Can't create the folder `%s'."),
2027                                  new_folder);
2028                 g_free(new_folder);
2029                 return;
2030         }
2031         g_free(new_folder);
2032
2033         gtk_clist_freeze(GTK_CLIST(ctree));
2034
2035         text[COL_FOLDER] = new_item->name;
2036         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
2037                                      FOLDER_SPACING,
2038                                      folderxpm, folderxpmmask,
2039                                      folderopenxpm, folderopenxpmmask,
2040                                      FALSE, FALSE);
2041         gtk_ctree_expand(ctree, folderview->selected);
2042         gtk_ctree_node_set_row_data(ctree, node, new_item);
2043         folderview_sort_folders(folderview, folderview->selected, item->folder);
2044
2045         gtk_clist_thaw(GTK_CLIST(ctree));
2046
2047         folder_write_list();
2048 }
2049
2050 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
2051                                          GtkWidget *widget)
2052 {
2053         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2054         FolderItem *item;
2055         gchar *message;
2056         AlertValue avalue;
2057
2058         if (!folderview->selected) return;
2059
2060         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2061         g_return_if_fail(item != NULL);
2062         g_return_if_fail(item->folder != NULL);
2063         g_return_if_fail(item->folder->type == F_IMAP);
2064         g_return_if_fail(item->folder->account != NULL);
2065
2066         message = g_strdup_printf(_("Really delete folder `%s'?"),
2067                                   g_basename(item->path));
2068         avalue = alertpanel(_("Delete folder"), message,
2069                             _("Yes"), _("+No"), NULL);
2070         g_free(message);
2071         if (avalue != G_ALERTDEFAULT) return;
2072
2073         if (item->folder->remove_folder(item->folder, item) < 0) {
2074                 alertpanel_error(_("Can't remove the folder `%s'."),
2075                                  item->path);
2076                 if (folderview->opened == folderview->selected)
2077                         summary_show(folderview->summaryview,
2078                                      folderview->summaryview->folder_item,
2079                                      FALSE);
2080                 return;
2081         }
2082
2083         if (folderview->opened == folderview->selected ||
2084             gtk_ctree_is_ancestor(ctree,
2085                                   folderview->selected,
2086                                   folderview->opened)) {
2087                 summary_clear_all(folderview->summaryview);
2088                 folderview->opened = NULL;
2089         }
2090
2091         gtk_ctree_remove_node(ctree, folderview->selected);
2092         folder_write_list();
2093 }
2094
2095 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
2096                                          GtkWidget *widget)
2097 {
2098         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2099         FolderItem *item;
2100         gchar *message;
2101         AlertValue avalue;
2102
2103         if (!folderview->selected) return;
2104
2105         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2106         g_return_if_fail(item != NULL);
2107         g_return_if_fail(item->folder != NULL);
2108         g_return_if_fail(item->folder->type == F_IMAP);
2109         g_return_if_fail(item->folder->account != NULL);
2110
2111         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"),
2112                                   item->folder->name);
2113         avalue = alertpanel(_("Delete IMAP4 account"), message,
2114                             _("Yes"), _("+No"), NULL);
2115         g_free(message);
2116
2117         if (avalue != G_ALERTDEFAULT) return;
2118
2119         if (folderview->opened == folderview->selected ||
2120             gtk_ctree_is_ancestor(ctree,
2121                                   folderview->selected,
2122                                   folderview->opened)) {
2123                 summary_clear_all(folderview->summaryview);
2124                 folderview->opened = NULL;
2125         }
2126
2127         account_destroy(item->folder->account);
2128         folder_destroy(item->folder);
2129         gtk_ctree_remove_node(ctree, folderview->selected);
2130         account_set_menu();
2131         main_window_reflect_prefs_all();
2132         folder_write_list();
2133 }
2134
2135 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
2136                                          GtkWidget *widget)
2137 {
2138         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2139         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2140         GtkCTreeNode *servernode, *node;
2141         Folder *folder;
2142         FolderItem *item;
2143         FolderItem *rootitem;
2144         FolderItem *newitem;
2145         GSList *new_subscr;
2146         GSList *cur;
2147         GNode *gnode;
2148
2149         if (!folderview->selected) return;
2150
2151         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2152         g_return_if_fail(item != NULL);
2153         folder = item->folder;
2154         g_return_if_fail(folder != NULL);
2155         g_return_if_fail(folder->type == F_NEWS);
2156         g_return_if_fail(folder->account != NULL);
2157
2158         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
2159                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
2160         else
2161                 servernode = folderview->selected;
2162
2163         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
2164
2165         new_subscr = grouplist_dialog(folder);
2166
2167         /* remove unsubscribed newsgroups */
2168         for (gnode = folder->node->children; gnode != NULL; ) {
2169                 GNode *next = gnode->next;
2170
2171                 item = FOLDER_ITEM(gnode->data);
2172                 if (g_slist_find_custom(new_subscr, item->path,
2173                                         (GCompareFunc)g_strcasecmp) != NULL) {
2174                         gnode = next;
2175                         continue;
2176                 }
2177
2178                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
2179                 if (!node) {
2180                         gnode = next;
2181                         continue;
2182                 }
2183
2184                 if (folderview->opened == node) {
2185                         summary_clear_all(folderview->summaryview);
2186                         folderview->opened = NULL;
2187                 }
2188
2189                 folder_item_remove(item);
2190                 gtk_ctree_remove_node(ctree, node);
2191
2192                 gnode = next;
2193         }
2194
2195         gtk_clist_freeze(GTK_CLIST(ctree));
2196
2197         /* add subscribed newsgroups */
2198         for (cur = new_subscr; cur != NULL; cur = cur->next) {
2199                 gchar *name = (gchar *)cur->data;
2200
2201                 if (folderview_find_by_name(ctree, servernode, name) != NULL)
2202                         continue;
2203
2204                 text[COL_FOLDER] = name;
2205                 node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
2206                                              FOLDER_SPACING,
2207                                              folderxpm, folderxpmmask,
2208                                              folderopenxpm, folderopenxpmmask,
2209                                              FALSE, FALSE);
2210                 gtk_ctree_expand(ctree, servernode);
2211
2212                 newitem = folder_item_new(name, name);
2213                 folder_item_append(rootitem, newitem);
2214                 gtk_ctree_node_set_row_data(ctree, node, newitem);
2215         }
2216
2217         gtk_ctree_sort_node(ctree, servernode);
2218         gtk_clist_thaw(GTK_CLIST(ctree));
2219
2220         slist_free_strings(new_subscr);
2221         g_slist_free(new_subscr);
2222
2223         folder_write_list();
2224 }
2225
2226 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
2227                                         GtkWidget *widget)
2228 {
2229         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2230         FolderItem *item;
2231         gchar *message;
2232         AlertValue avalue;
2233
2234         if (!folderview->selected) return;
2235
2236         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2237         g_return_if_fail(item != NULL);
2238         g_return_if_fail(item->folder != NULL);
2239         g_return_if_fail(item->folder->type == F_NEWS);
2240         g_return_if_fail(item->folder->account != NULL);
2241
2242         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
2243                                   g_basename(item->path));
2244         avalue = alertpanel(_("Delete newsgroup"), message,
2245                             _("Yes"), _("+No"), NULL);
2246         g_free(message);
2247         if (avalue != G_ALERTDEFAULT) return;
2248
2249         if (folderview->opened == folderview->selected) {
2250                 summary_clear_all(folderview->summaryview);
2251                 folderview->opened = NULL;
2252         }
2253
2254         folder_item_remove(item);
2255         gtk_ctree_remove_node(ctree, folderview->selected);
2256         folder_write_list();
2257 }
2258
2259 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
2260                                          GtkWidget *widget)
2261 {
2262         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2263         FolderItem *item;
2264         gchar *message;
2265         AlertValue avalue;
2266
2267         if (!folderview->selected) return;
2268
2269         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2270         g_return_if_fail(item != NULL);
2271         g_return_if_fail(item->folder != NULL);
2272         g_return_if_fail(item->folder->type == F_NEWS);
2273         g_return_if_fail(item->folder->account != NULL);
2274
2275         message = g_strdup_printf(_("Really delete news account `%s'?"),
2276                                   item->folder->name);
2277         avalue = alertpanel(_("Delete news account"), message,
2278                             _("Yes"), _("+No"), NULL);
2279         g_free(message);
2280
2281         if (avalue != G_ALERTDEFAULT) return;
2282
2283         if (folderview->opened == folderview->selected ||
2284             gtk_ctree_is_ancestor(ctree,
2285                                   folderview->selected,
2286                                   folderview->opened)) {
2287                 summary_clear_all(folderview->summaryview);
2288                 folderview->opened = NULL;
2289         }
2290
2291         account_destroy(item->folder->account);
2292         folder_destroy(item->folder);
2293         gtk_ctree_remove_node(ctree, folderview->selected);
2294         account_set_menu();
2295         main_window_reflect_prefs_all();
2296         folder_write_list();
2297 }
2298
2299 static void folderview_search_cb(FolderView *folderview, guint action,
2300                                  GtkWidget *widget)
2301 {
2302         summary_search(folderview->summaryview);
2303 }
2304
2305 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2306                                           GdkDragContext *context,
2307                                           gint            x,
2308                                           gint            y,
2309                                           guint           time,
2310                                           FolderView     *folderview)
2311 {
2312         gint row, column;
2313         FolderItem *item, *current_item;
2314         GtkCTreeNode *node = NULL;
2315         gboolean acceptable = FALSE;
2316
2317         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
2318                                          x - 24, y - 24, &row, &column)) {
2319                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2320                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2321                 current_item = folderview->summaryview->folder_item;
2322                 if (item != NULL &&
2323                     item->path != NULL &&
2324                     current_item != NULL &&
2325                     current_item != item) {
2326                         switch (item->folder->type){
2327                         case F_MH:
2328                                 if (current_item->folder->type == F_MH)
2329                                     acceptable = TRUE;
2330                                 break;
2331                         case F_IMAP:
2332                                 if (current_item->folder->account == item->folder->account)
2333                                     acceptable = TRUE;
2334                                 break;
2335                         default:
2336                         }
2337                 }
2338         }
2339
2340         if (acceptable) {
2341                 gtk_signal_handler_block_by_func(GTK_OBJECT(widget),GTK_SIGNAL_FUNC(folderview_selected), folderview);
2342                 gtk_ctree_select(GTK_CTREE(widget), node);
2343                 gtk_signal_handler_unblock_by_func(GTK_OBJECT(widget),GTK_SIGNAL_FUNC(folderview_selected), folderview);
2344                 gdk_drag_status(context, 
2345                                         (context->actions == GDK_ACTION_COPY ?
2346                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2347         } else {
2348                 gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2349                 gdk_drag_status(context, 0, time);
2350         }
2351
2352         return acceptable;
2353 }
2354
2355 static void folderview_drag_leave_cb(GtkWidget      *widget,
2356                                      GdkDragContext *context,
2357                                      guint           time,
2358                                      FolderView     *folderview)
2359 {
2360         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2361 }
2362
2363 static void folderview_drag_received_cb(GtkWidget        *widget,
2364                                         GdkDragContext   *drag_context,
2365                                         gint              x,
2366                                         gint              y,
2367                                         GtkSelectionData *data,
2368                                         guint             info,
2369                                         guint             time,
2370                                         FolderView       *folderview)
2371 {
2372         gint row, column;
2373         FolderItem *item;
2374         GtkCTreeNode *node;
2375
2376         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
2377                                          x - 24, y - 24, &row, &column) == 0)
2378                 return;
2379
2380         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2381         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2382         if (item != NULL) {
2383                 switch (drag_context->action) {
2384                         case GDK_ACTION_COPY:
2385                                 summary_copy_selected_to(folderview->summaryview, item);
2386                                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2387                                 break;
2388                         case GDK_ACTION_MOVE:
2389                         case GDK_ACTION_DEFAULT:
2390                         default:
2391                                 summary_move_selected_to(folderview->summaryview, item);
2392                                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
2393                                 break;
2394                 }
2395         } else
2396                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2397 }
2398
2399 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
2400 {
2401         const FolderItem *item = a;
2402         const gchar *name = b;
2403
2404         if (!item->path) return -1;
2405         return strcmp2(g_basename(item->path), name);
2406 }
2407
2408 static void folderview_scoring_cb(FolderView *folderview, guint action,
2409                                    GtkWidget *widget)
2410 {
2411         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2412         FolderItem *item;
2413
2414         if (!folderview->selected) return;
2415
2416         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2417         g_return_if_fail(item != NULL);
2418         g_return_if_fail(item->folder != NULL);
2419
2420         prefs_scoring_open(item);
2421 }
2422
2423 static void folderview_processing_cb(FolderView *folderview, guint action,
2424                                      GtkWidget *widget)
2425 {
2426         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2427         FolderItem *item;
2428
2429         if (!folderview->selected) return;
2430
2431         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2432         g_return_if_fail(item != NULL);
2433         g_return_if_fail(item->folder != NULL);
2434
2435         prefs_filtering_open(item);
2436 }
2437
2438 static void folderview_property_cb(FolderView *folderview, guint action, 
2439                                    GtkWidget *widget) 
2440 {
2441         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2442         FolderItem *item;
2443
2444         if (!folderview->selected) return;
2445
2446         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2447         g_return_if_fail(item != NULL);
2448         g_return_if_fail(item->folder != NULL);
2449
2450         prefs_folder_item_create(folderview, item);
2451 }
2452
2453 void folderview_set_target_folder_color(gint color_op) 
2454 {
2455         gint firstone = 1;
2456         GList *list;
2457         FolderView *folderview;
2458
2459         for (list = folderview_list; list != NULL; list = list->next) {
2460                 folderview = (FolderView *)list->data;
2461                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2462                 if (firstone) {
2463                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2464                                 folderview->color_op;
2465                         firstone = 0;
2466                 }
2467         }
2468 }