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