8ead09c90e9c23b9753548e76ed056dc7757dd62
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 "inputdialog.h"
46 #include "manage_window.h"
47 #include "alertpanel.h"
48 #include "menu.h"
49 #include "procmsg.h"
50 #include "utils.h"
51 #include "gtkutils.h"
52 #include "prefs_common.h"
53 #include "prefs_account.h"
54 #include "account.h"
55 #include "folder.h"
56 #include "grouplist_dialog.h"
57
58 #include "pixmaps/inbox.xpm"
59 #include "pixmaps/outbox.xpm"
60 #include "pixmaps/dir-close.xpm"
61 #include "pixmaps/dir-open.xpm"
62 #include "pixmaps/trash.xpm"
63
64 typedef enum
65 {
66         COL_FOLDER      = 0,
67         COL_NEW         = 1,
68         COL_UNREAD      = 2,
69         COL_TOTAL       = 3
70 } FolderColumnPos;
71
72 #define N_FOLDER_COLS           4
73 #define COL_FOLDER_WIDTH        150
74 #define COL_NUM_WIDTH           32
75
76 #define STATUSBAR_PUSH(mainwin, str) \
77 { \
78         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
79                            mainwin->folderview_cid, str); \
80         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
81 }
82
83 #define STATUSBAR_POP(mainwin) \
84 { \
85         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
86                           mainwin->folderview_cid); \
87 }
88
89 static GList *folderview_list = NULL;
90
91 static GdkFont *normalfont;
92 static GdkFont *boldfont;
93
94 static GdkPixmap *inboxxpm;
95 static GdkBitmap *inboxxpmmask;
96 static GdkPixmap *outboxxpm;
97 static GdkBitmap *outboxxpmmask;
98 static GdkPixmap *folderxpm;
99 static GdkBitmap *folderxpmmask;
100 static GdkPixmap *folderopenxpm;
101 static GdkBitmap *folderopenxpmmask;
102 static GdkPixmap *trashxpm;
103 static GdkBitmap *trashxpmmask;
104
105 static void folderview_select_node       (FolderView    *folderview,
106                                           GtkCTreeNode  *node);
107 static void folderview_set_folders       (FolderView    *folderview);
108 static void folderview_sort_folders      (FolderView    *folderview,
109                                           GtkCTreeNode  *root,
110                                           Folder        *folder);
111 static void folderview_append_folder     (FolderView    *folderview,
112                                           Folder        *folder);
113 static void folderview_update_node       (FolderView    *folderview,
114                                           GtkCTreeNode  *node);
115
116 static GtkCTreeNode *folderview_find_by_name    (GtkCTree       *ctree,
117                                                  GtkCTreeNode   *node,
118                                                  const gchar    *name);
119
120 static gint folderview_compare_name     (gconstpointer   a,
121                                          gconstpointer   b);
122
123 /* callback functions */
124 static void folderview_button_pressed   (GtkWidget      *ctree,
125                                          GdkEventButton *event,
126                                          FolderView     *folderview);
127 static void folderview_button_released  (GtkWidget      *ctree,
128                                          GdkEventButton *event,
129                                          FolderView     *folderview);
130 static void folderview_key_pressed      (GtkWidget      *widget,
131                                          GdkEventKey    *event,
132                                          FolderView     *folderview);
133 static void folderview_selected         (GtkCTree       *ctree,
134                                          GtkCTreeNode   *row,
135                                          gint            column,
136                                          FolderView     *folderview);
137 static void folderview_tree_expanded    (GtkCTree       *ctree,
138                                          GtkCTreeNode   *node,
139                                          FolderView     *folderview);
140 static void folderview_tree_collapsed   (GtkCTree       *ctree,
141                                          GtkCTreeNode   *node,
142                                          FolderView     *folderview);
143 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
144                                          FolderView     *folderview);
145 static void folderview_col_resized      (GtkCList       *clist,
146                                          gint            column,
147                                          gint            width,
148                                          FolderView     *folderview);
149
150 static void folderview_new_folder_cb    (FolderView     *folderview,
151                                          guint           action,
152                                          GtkWidget      *widget);
153 static void folderview_new_mbox_folder_cb(FolderView *folderview,
154                                           guint action,
155                                           GtkWidget *widget);
156 static void folderview_rename_folder_cb (FolderView     *folderview,
157                                          guint           action,
158                                          GtkWidget      *widget);
159 static void folderview_delete_folder_cb (FolderView     *folderview,
160                                          guint           action,
161                                          GtkWidget      *widget);
162 static void folderview_remove_mailbox_cb(FolderView     *folderview,
163                                          guint           action,
164                                          GtkWidget      *widget);
165
166 static void folderview_new_imap_folder_cb(FolderView    *folderview,
167                                           guint          action,
168                                           GtkWidget     *widget);
169 static void folderview_rm_imap_folder_cb (FolderView    *folderview,
170                                           guint          action,
171                                           GtkWidget     *widget);
172 static void folderview_rm_imap_server_cb (FolderView    *folderview,
173                                           guint          action,
174                                           GtkWidget     *widget);
175
176 static void folderview_new_news_group_cb(FolderView     *folderview,
177                                          guint           action,
178                                          GtkWidget      *widget);
179 static void folderview_rm_news_group_cb (FolderView     *folderview,
180                                          guint           action,
181                                          GtkWidget      *widget);
182 static void folderview_rm_news_server_cb(FolderView     *folderview,
183                                          guint           action,
184                                          GtkWidget      *widget);
185
186 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
187                                           GdkDragContext *context,
188                                           gint            x,
189                                           gint            y,
190                                           guint           time,
191                                           FolderView     *folderview);
192 static void folderview_drag_leave_cb     (GtkWidget        *widget,
193                                           GdkDragContext   *context,
194                                           guint             time,
195                                           FolderView       *folderview);
196 static void folderview_drag_received_cb  (GtkWidget        *widget,
197                                           GdkDragContext   *drag_context,
198                                           gint              x,
199                                           gint              y,
200                                           GtkSelectionData *data,
201                                           guint             info,
202                                           guint             time,
203                                           FolderView       *folderview);
204
205 static GtkItemFactoryEntry folderview_mbox_popup_entries[] =
206 {
207         {N_("/Create _new folder..."),  NULL, folderview_new_mbox_folder_cb,    0, NULL},
208         {N_("/_Rename folder..."),      NULL, folderview_rename_folder_cb, 0, NULL},
209         {N_("/_Delete folder"),         NULL, folderview_delete_folder_cb, 0, NULL},
210         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
211         {N_("/Remove _mailbox"),        NULL, folderview_remove_mailbox_cb, 0, NULL},
212         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
213         {N_("/_Property..."),           NULL, NULL, 0, NULL}
214 };
215
216 static GtkItemFactoryEntry folderview_mail_popup_entries[] =
217 {
218         {N_("/Create _new folder..."),  NULL, folderview_new_folder_cb,    0, NULL},
219         {N_("/_Rename folder..."),      NULL, folderview_rename_folder_cb, 0, NULL},
220         {N_("/_Delete folder"),         NULL, folderview_delete_folder_cb, 0, NULL},
221         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
222         {N_("/Remove _mailbox"),        NULL, folderview_remove_mailbox_cb, 0, NULL},
223         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
224         {N_("/_Property..."),           NULL, NULL, 0, NULL}
225 };
226
227 static GtkItemFactoryEntry folderview_imap_popup_entries[] =
228 {
229         {N_("/Create _new folder..."),  NULL, folderview_new_imap_folder_cb, 0, NULL},
230         {N_("/_Rename folder..."),      NULL, NULL, 0, NULL},
231         {N_("/_Delete folder"),         NULL, folderview_rm_imap_folder_cb, 0, NULL},
232         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
233         {N_("/Remove _IMAP4 server"),   NULL, folderview_rm_imap_server_cb, 0, NULL},
234         {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
235         {N_("/_Property..."),           NULL, NULL, 0, NULL}
236 };
237
238 static GtkItemFactoryEntry folderview_news_popup_entries[] =
239 {
240         {N_("/_Subscribe to newsgroup..."),
241                                          NULL, folderview_new_news_group_cb, 0, NULL},
242         {N_("/_Remove newsgroup"),       NULL, folderview_rm_news_group_cb, 0, NULL},
243         {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
244         {N_("/Remove _news server"),     NULL, folderview_rm_news_server_cb, 0, NULL},
245         {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
246         {N_("/_Property..."),            NULL, NULL, 0, NULL}
247 };
248
249
250 FolderView *folderview_create(void)
251 {
252         FolderView *folderview;
253         GtkWidget *scrolledwin;
254         GtkWidget *ctree;
255         gchar *titles[N_FOLDER_COLS] = {_("Folder"), _("New"),
256                                         _("Unread"), _("#")};
257         GtkWidget *mail_popup;
258         GtkWidget *news_popup;
259         GtkWidget *imap_popup;
260         GtkWidget *mbox_popup;
261         GtkItemFactory *mail_factory;
262         GtkItemFactory *news_factory;
263         GtkItemFactory *imap_factory;
264         GtkItemFactory *mbox_factory;
265         gint n_entries;
266         gint i;
267
268         debug_print(_("Creating folder view...\n"));
269         folderview = g_new0(FolderView, 1);
270
271         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
272         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
273                                        GTK_POLICY_AUTOMATIC,
274                                        GTK_POLICY_ALWAYS);
275         gtk_widget_set_usize(scrolledwin,
276                              prefs_common.folderview_width,
277                              prefs_common.folderview_height);
278
279         ctree = gtk_ctree_new_with_titles(N_FOLDER_COLS, COL_FOLDER, titles);
280         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
281         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
282         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_NEW,
283                                            GTK_JUSTIFY_RIGHT);
284         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_UNREAD,
285                                            GTK_JUSTIFY_RIGHT);
286         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_TOTAL,
287                                            GTK_JUSTIFY_RIGHT);
288         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_FOLDER,
289                                    prefs_common.folder_col_folder);
290         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_NEW,
291                                    prefs_common.folder_col_new);
292         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_UNREAD,        
293                                    prefs_common.folder_col_unread);
294         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_TOTAL,
295                                    prefs_common.folder_col_total);
296         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
297         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
298                                      GTK_CTREE_EXPANDER_SQUARE);
299         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
300         
301         /* don't let title buttons take key focus */
302         for (i = 0; i < N_FOLDER_COLS; i++)
303                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
304                                        GTK_CAN_FOCUS);
305
306         /* popup menu */
307         n_entries = sizeof(folderview_mail_popup_entries) /
308                 sizeof(folderview_mail_popup_entries[0]);
309         mail_popup = menu_create_items(folderview_mail_popup_entries,
310                                        n_entries,
311                                        "<MailFolder>", &mail_factory,
312                                        folderview);
313         n_entries = sizeof(folderview_imap_popup_entries) /
314                 sizeof(folderview_imap_popup_entries[0]);
315         imap_popup = menu_create_items(folderview_imap_popup_entries,
316                                        n_entries,
317                                        "<IMAPFolder>", &imap_factory,
318                                        folderview);
319         n_entries = sizeof(folderview_news_popup_entries) /
320                 sizeof(folderview_news_popup_entries[0]);
321         news_popup = menu_create_items(folderview_news_popup_entries,
322                                        n_entries,
323                                        "<NewsFolder>", &news_factory,
324                                        folderview);
325         n_entries = sizeof(folderview_mbox_popup_entries) /
326                 sizeof(folderview_mbox_popup_entries[0]);
327         mbox_popup = menu_create_items(folderview_mbox_popup_entries,
328                                        n_entries,
329                                        "<MailFolder>", &mbox_factory,
330                                        folderview);
331
332         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
333                            GTK_SIGNAL_FUNC(folderview_key_pressed),
334                            folderview);
335         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
336                            GTK_SIGNAL_FUNC(folderview_button_pressed),
337                            folderview);
338         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
339                            GTK_SIGNAL_FUNC(folderview_button_released),
340                            folderview);
341         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
342                            GTK_SIGNAL_FUNC(folderview_selected), folderview);
343
344
345         gtk_signal_connect_after(GTK_OBJECT(ctree), "tree_expand",
346                                  GTK_SIGNAL_FUNC(folderview_tree_expanded),
347                                  folderview);
348         gtk_signal_connect_after(GTK_OBJECT(ctree), "tree_collapse",
349                                  GTK_SIGNAL_FUNC(folderview_tree_collapsed),
350                                  folderview);
351
352         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
353                            GTK_SIGNAL_FUNC(folderview_col_resized),
354                            folderview);
355
356         gtk_signal_connect(GTK_OBJECT(mail_popup), "selection_done",
357                            GTK_SIGNAL_FUNC(folderview_popup_close),
358                            folderview);
359         gtk_signal_connect(GTK_OBJECT(imap_popup), "selection_done",
360                            GTK_SIGNAL_FUNC(folderview_popup_close),
361                            folderview);
362         gtk_signal_connect(GTK_OBJECT(news_popup), "selection_done",
363                            GTK_SIGNAL_FUNC(folderview_popup_close),
364                            folderview);
365         gtk_signal_connect(GTK_OBJECT(mbox_popup), "selection_done",
366                            GTK_SIGNAL_FUNC(folderview_popup_close),
367                            folderview);
368
369         /* drop callback */
370         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL &
371                           ~GTK_DEST_DEFAULT_HIGHLIGHT,
372                           summary_drag_types, 1,
373                           GDK_ACTION_MOVE);
374         gtk_signal_connect(GTK_OBJECT(ctree), "drag_motion",
375                            GTK_SIGNAL_FUNC(folderview_drag_motion_cb),
376                            folderview);
377         gtk_signal_connect(GTK_OBJECT(ctree), "drag_leave",
378                            GTK_SIGNAL_FUNC(folderview_drag_leave_cb),
379                            folderview);
380         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_received",
381                            GTK_SIGNAL_FUNC(folderview_drag_received_cb),
382                            folderview);
383
384         folderview->scrolledwin  = scrolledwin;
385         folderview->ctree        = ctree;
386         folderview->mail_popup   = mail_popup;
387         folderview->mail_factory = mail_factory;
388         folderview->imap_popup   = imap_popup;
389         folderview->imap_factory = imap_factory;
390         folderview->news_popup   = news_popup;
391         folderview->news_factory = news_factory;
392         folderview->mbox_popup   = mbox_popup;
393         folderview->mbox_factory = mbox_factory;
394
395         gtk_widget_show_all(scrolledwin);
396
397         folderview_list = g_list_append(folderview_list, folderview);
398
399         return folderview;
400 }
401
402 void folderview_init(FolderView *folderview)
403 {
404         GtkWidget *ctree = folderview->ctree;
405
406         PIXMAP_CREATE(ctree, inboxxpm, inboxxpmmask, inbox_xpm);
407         PIXMAP_CREATE(ctree, outboxxpm, outboxxpmmask, outbox_xpm);
408         PIXMAP_CREATE(ctree, folderxpm, folderxpmmask, DIRECTORY_CLOSE_XPM);
409         PIXMAP_CREATE(ctree, folderopenxpm, folderopenxpmmask,
410                       DIRECTORY_OPEN_XPM);
411         PIXMAP_CREATE(ctree, trashxpm, trashxpmmask, trash_xpm);
412
413         if (!normalfont)
414                 normalfont = gdk_fontset_load(NORMAL_FONT);
415         if (!boldfont)
416                 boldfont = gdk_fontset_load(BOLD_FONT);
417 }
418
419 void folderview_set(FolderView *folderview)
420 {
421         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
422         MainWindow *mainwin = folderview->mainwin;
423
424         debug_print(_("Setting folder info...\n"));
425         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
426
427         main_window_cursor_wait(mainwin);
428
429         folderview->selected = NULL;
430         folderview->opened = NULL;
431
432         gtk_clist_freeze(GTK_CLIST(ctree));
433         gtk_clist_clear(GTK_CLIST(ctree));
434         gtk_clist_thaw(GTK_CLIST(ctree));
435         gtk_clist_freeze(GTK_CLIST(ctree));
436
437         folderview_set_folders(folderview);
438
439         gtk_clist_thaw(GTK_CLIST(ctree));
440         main_window_cursor_normal(mainwin);
441         STATUSBAR_POP(mainwin);
442 }
443
444 void folderview_select(FolderView *folderview, FolderItem *item)
445 {
446         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
447         GtkCTreeNode *node;
448
449         if (!item) return;
450
451         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
452         if (node) folderview_select_node(folderview, node);
453 }
454
455 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
456 {
457         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
458
459         g_return_if_fail(node != NULL);
460
461         folderview->open_folder = TRUE;
462         gtk_ctree_select(ctree, node);
463         gtkut_ctree_set_focus_row(ctree, node);
464         if (folderview->summaryview->messages > 0)
465                 gtk_widget_grab_focus(folderview->summaryview->ctree);
466         else
467                 gtk_widget_grab_focus(folderview->ctree);
468
469         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node))
470                != NULL)
471                 gtk_ctree_expand(ctree, node);
472 }
473
474 void folderview_unselect(FolderView *folderview)
475 {
476         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
477                 gtk_ctree_collapse
478                         (GTK_CTREE(folderview->ctree), folderview->opened);
479
480         folderview->selected = folderview->opened = NULL;
481 }
482
483 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
484                                                  GtkCTreeNode *node)
485 {
486         FolderItem *item;
487
488         if (node)
489                 node = gtkut_ctree_node_next(ctree, node);
490         else
491                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
492
493         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
494                 item = gtk_ctree_node_get_row_data(ctree, node);
495                 if (item && item->unread > 0 && item->stype != F_TRASH)
496                         return node;
497         }
498
499         return NULL;
500 }
501
502 void folderview_select_next_unread(FolderView *folderview)
503 {
504         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
505         GtkCTreeNode *node = NULL;
506
507         if ((node = folderview_find_next_unread(ctree, folderview->opened))
508             != NULL) {
509                 folderview_select_node(folderview, node);
510                 return;
511         }
512
513         if (!folderview->opened ||
514             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
515                 return;
516         /* search again from the first node */
517         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
518                 folderview_select_node(folderview, node);
519 }
520
521 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row,
522                                gint new, gint unread, gint total)
523 {
524         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
525         static GtkCTreeNode *prev_row = NULL;
526         FolderItem *item;
527
528         if (!row) return;
529
530         item = gtk_ctree_node_get_row_data(ctree, row);
531         if (!item) return;
532         if (prev_row     == row    &&
533             item->new    == new    &&
534             item->unread == unread &&
535             item->total  == total)
536                 return;
537
538         prev_row = row;
539
540         item->new    = new;
541         item->unread = unread;
542         item->total  = total;
543
544         folderview_update_node(folderview, row);
545 }
546
547 static void folderview_set_folders(FolderView *folderview)
548 {
549         GList *list;
550
551         list = folder_get_list();
552
553         for (; list != NULL; list = list->next)
554                 folderview_append_folder(folderview, FOLDER(list->data));
555 }
556
557 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
558                                       gpointer data)
559 {
560         GList *list;
561
562         for (list = folderview_list; list != NULL; list = list->next) {
563                 FolderView *folderview = (FolderView *)list->data;
564                 MainWindow *mainwin = folderview->mainwin;
565                 gchar *str;
566
567                 if (item->path)
568                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
569                                               LOCAL_FOLDER(folder)->rootpath,
570                                               G_DIR_SEPARATOR,
571                                               item->path);
572                 else
573                         str = g_strdup_printf(_("Scanning folder %s ..."),
574                                               LOCAL_FOLDER(folder)->rootpath);
575
576                 STATUSBAR_PUSH(mainwin, str);
577                 STATUSBAR_POP(mainwin);
578                 g_free(str);
579         }
580 }
581
582 static GtkWidget *label_window_create(const gchar *str)
583 {
584         GtkWidget *window;
585         GtkWidget *label;
586
587         window = gtk_window_new(GTK_WINDOW_DIALOG);
588         gtk_widget_set_usize(window, 380, 60);
589         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
590         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
591         gtk_window_set_title(GTK_WINDOW(window), str);
592         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
593         gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
594         manage_window_set_transient(GTK_WINDOW(window));
595
596         label = gtk_label_new(str);
597         gtk_container_add(GTK_CONTAINER(window), label);
598         gtk_widget_show(label);
599
600         gtk_widget_show_now(window);
601
602         return window;
603 }
604
605 void folderview_update_all(void)
606 {
607         GList *list;
608         GtkWidget *window;
609
610         window = label_window_create(_("Updating all folders..."));
611
612         list = folder_get_list();
613         for (; list != NULL; list = list->next) {
614                 Folder *folder = list->data;
615
616                 if (!folder->scan_tree) continue;
617                 folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
618                 folder->scan_tree(folder);
619                 folder_set_ui_func(folder, NULL, NULL);
620         }
621
622         folder_write_list();
623
624         for (list = folderview_list; list != NULL; list = list->next) {
625                 FolderView *folderview = (FolderView *)list->data;
626
627                 folderview_set(folderview);
628         }
629
630         gtk_widget_destroy(window);
631 }
632
633 static gboolean folderview_search_new_recursive(GtkCTree *ctree,
634                                                 GtkCTreeNode *node)
635 {
636         FolderItem *item;
637
638         if (node) {
639                 item = gtk_ctree_node_get_row_data(ctree, node);
640                 if (item) {
641                         if (item->new > 0 ||
642                             (item->stype == F_QUEUE && item->total > 0))
643                                 return TRUE;
644                 }
645                 node = GTK_CTREE_ROW(node)->children;
646         } else
647                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
648
649         while (node) {
650                 if (folderview_search_new_recursive(ctree, node) == TRUE)
651                         return TRUE;
652                 node = GTK_CTREE_ROW(node)->sibling;
653         }
654
655         return FALSE;
656 }
657
658 static gboolean folderview_have_new_children(FolderView *folderview,
659                                              GtkCTreeNode *node)
660 {
661         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
662
663         if (!node)
664                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
665         if (!node)
666                 return FALSE;
667
668         node = GTK_CTREE_ROW(node)->children;
669
670         while (node) {
671                 if (folderview_search_new_recursive(ctree, node) == TRUE)
672                         return TRUE;
673                 node = GTK_CTREE_ROW(node)->sibling;
674         }
675
676         return FALSE;
677 }
678
679 static gboolean folderview_search_unread_recursive(GtkCTree *ctree,
680                                                    GtkCTreeNode *node)
681 {
682         FolderItem *item;
683
684         if (node) {
685                 item = gtk_ctree_node_get_row_data(ctree, node);
686                 if (item) {
687                         if (item->unread > 0 ||
688                             (item->stype == F_QUEUE && item->total > 0))
689                                 return TRUE;
690                 }
691                 node = GTK_CTREE_ROW(node)->children;
692         } else
693                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
694
695         while (node) {
696                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
697                         return TRUE;
698                 node = GTK_CTREE_ROW(node)->sibling;
699         }
700
701         return FALSE;
702 }
703
704 static gboolean folderview_have_unread_children(FolderView *folderview,
705                                                 GtkCTreeNode *node)
706 {
707         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
708
709         if (!node)
710                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
711         if (!node)
712                 return FALSE;
713
714         node = GTK_CTREE_ROW(node)->children;
715
716         while (node) {
717                 if (folderview_search_unread_recursive(ctree, node) == TRUE)
718                         return TRUE;
719                 node = GTK_CTREE_ROW(node)->sibling;
720         }
721
722         return FALSE;
723 }
724
725 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
726 {
727         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
728         GtkStyle *style, *prev_style, *ctree_style;
729         GtkCTreeNode *parent;
730         FolderItem *item;
731         GdkPixmap *xpm, *openxpm;
732         GdkBitmap *mask, *openmask;
733         gchar *name;
734         gchar *str;
735         gboolean add_unread_mark;
736         gboolean use_bold, use_color;
737
738         item = gtk_ctree_node_get_row_data(ctree, node);
739         g_return_if_fail(item != NULL);
740
741         switch (item->stype) {
742         case F_INBOX:
743                 xpm = openxpm = inboxxpm;
744                 mask = openmask = inboxxpmmask;
745                 name = g_strdup(_("Inbox"));
746                 break;
747         case F_OUTBOX:
748                 xpm = openxpm = outboxxpm;
749                 mask = openmask = outboxxpmmask;
750                 name = g_strdup(_("Outbox"));
751                 break;
752         case F_QUEUE:
753                 xpm = openxpm = outboxxpm;
754                 mask = openmask = outboxxpmmask;
755                 name = g_strdup(_("Queue"));
756                 break;
757         case F_TRASH:
758                 xpm = openxpm = trashxpm;
759                 mask = openmask = trashxpmmask;
760                 name = g_strdup(_("Trash"));
761                 break;
762         case F_DRAFT:
763                 xpm = folderxpm;
764                 mask = folderxpmmask;
765                 openxpm = folderopenxpm;
766                 openmask = folderopenxpmmask;
767                 name = g_strdup(_("Draft"));
768                 break;
769         default:
770                 xpm = folderxpm;
771                 mask = folderxpmmask;
772                 openxpm = folderopenxpm;
773                 openmask = folderopenxpmmask;
774                 if (!item->parent) {
775                         switch (item->folder->type) {
776                         case F_MH:
777                                 name = " (MH)"; break;
778                         case F_MBOX:
779                                 name = " (mbox)"; break;
780                         case F_IMAP:
781                                 name = " (IMAP4)"; break;
782                         case F_NEWS:
783                                 name = " (News)"; break;
784                         default:
785                                 name = "";
786                         }
787                         name = g_strconcat(item->name, name, NULL);
788                 } else
789                         name = g_strdup(item->name);
790         }
791
792         if (!GTK_CTREE_ROW(node)->expanded &&
793             folderview_have_unread_children(folderview, node))
794                 add_unread_mark = TRUE;
795         else
796                 add_unread_mark = FALSE;
797
798         if (item->stype == F_QUEUE && item->total > 0 &&
799             prefs_common.display_folder_unread) {
800                 str = g_strdup_printf("%s (%d%s)", name, item->total,
801                                       add_unread_mark ? "+" : "");
802                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
803                                         xpm, mask, openxpm, openmask,
804                                         FALSE, GTK_CTREE_ROW(node)->expanded);
805                 g_free(str);
806         } else if ((item->unread > 0 || add_unread_mark) &&
807                  prefs_common.display_folder_unread) {
808
809                 if (item->unread > 0)
810                         str = g_strdup_printf("%s (%d%s)", name, item->unread,
811                                               add_unread_mark ? "+" : "");
812                 else
813                         str = g_strdup_printf("%s (+)", name);
814                 gtk_ctree_set_node_info(ctree, node, str, FOLDER_SPACING,
815                                         xpm, mask, openxpm, openmask,
816                                         FALSE, GTK_CTREE_ROW(node)->expanded);
817                 g_free(str);
818         } else
819                 gtk_ctree_set_node_info(ctree, node, name, FOLDER_SPACING,
820                                         xpm, mask, openxpm, openmask,
821                                         FALSE, GTK_CTREE_ROW(node)->expanded);
822         g_free(name);
823
824         if (!item->parent) {
825                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    "-");
826                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, "-");
827                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  "-");
828         } else {
829                 gtk_ctree_node_set_text(ctree, node, COL_NEW,    itos(item->new));
830                 gtk_ctree_node_set_text(ctree, node, COL_UNREAD, itos(item->unread));
831                 gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
832         }
833
834         if (item->stype == F_TRASH) return;
835
836         ctree_style = gtk_widget_get_style(GTK_WIDGET(ctree));
837         prev_style = gtk_ctree_node_get_row_style(ctree, node);
838         if (!prev_style)
839                 prev_style = ctree_style;
840         style = gtk_style_copy(prev_style);
841         if (!style) return;
842
843         if (item->stype == F_QUEUE) {
844                 /* highlight queue folder if there are any messages */
845                 use_bold = use_color = (item->total > 0);
846         } else {
847                 /* if unread messages exist, print with bold font */
848                 use_bold = (item->unread > 0) || add_unread_mark;
849                 /* if new messages exist, print with colored letter */
850                 use_color =
851                         (item->new > 0) ||
852                         (add_unread_mark &&
853                          folderview_have_new_children(folderview, node));
854         }
855
856         if (use_bold && boldfont)
857                 style->font = boldfont;
858         else
859                 style->font = ctree_style->font;
860
861         if (use_color) {
862                 style->fg[GTK_STATE_NORMAL]   = folderview->color_new;
863                 style->fg[GTK_STATE_SELECTED] = folderview->color_new;
864         } else {
865                 style->fg[GTK_STATE_NORMAL] =
866                         ctree_style->fg[GTK_STATE_NORMAL];
867                 style->fg[GTK_STATE_SELECTED] =
868                         ctree_style->fg[GTK_STATE_SELECTED];
869         }
870
871         gtk_ctree_node_set_row_style(ctree, node, style);
872
873         parent = node;
874         while ((parent = gtkut_ctree_find_collapsed_parent(ctree, parent))
875                != NULL)
876                 folderview_update_node(folderview, parent);
877 }
878
879 void folderview_update_item(FolderItem *item, gboolean update_summary)
880 {
881         GList *list;
882         FolderView *folderview;
883         GtkCTree *ctree;
884         GtkCTreeNode *node;
885
886         g_return_if_fail(item != NULL);
887
888         for (list = folderview_list; list != NULL; list = list->next) {
889                 folderview = (FolderView *)list->data;
890                 ctree = GTK_CTREE(folderview->ctree);
891
892                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
893                 if (node) {
894                         folderview_update_node(folderview, node);
895                         if (update_summary && folderview->opened == node)
896                                 summary_show(folderview->summaryview,
897                                              item, FALSE);
898                 }
899         }
900 }
901
902 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
903                                                 gpointer data)
904 {
905         folderview_update_item((FolderItem *)key, FALSE);
906 }
907
908 void folderview_update_item_foreach(GHashTable *table)
909 {
910         g_hash_table_foreach(table, folderview_update_item_foreach_func, NULL);
911 }
912
913 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
914                                       GNode *gnode, GtkCTreeNode *cnode,
915                                       gpointer data)
916 {
917         FolderView *folderview = (FolderView *)data;
918         FolderItem *item = FOLDER_ITEM(gnode->data);
919
920         g_return_val_if_fail(item != NULL, FALSE);
921
922         gtk_ctree_node_set_row_data(ctree, cnode, item);
923         folderview_update_node(folderview, cnode);
924
925         return TRUE;
926 }
927
928 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
929                                    gpointer data)
930 {
931         if (GTK_CTREE_ROW(node)->children)
932                 gtk_ctree_expand(ctree, node);
933 }
934
935 #define SET_SPECIAL_FOLDER(ctree, item) \
936 { \
937         if (item) { \
938                 GtkCTreeNode *node, *sibling; \
939  \
940                 node = gtk_ctree_find_by_row_data(ctree, root, item); \
941                 if (!node) \
942                         g_warning("%s not found.\n", item->path); \
943                 else { \
944                         if (!prev) \
945                                 sibling = GTK_CTREE_ROW(root)->children; \
946                         else \
947                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
948                         if (node != sibling) \
949                                 gtk_ctree_move(ctree, node, root, sibling); \
950                 } \
951  \
952                 prev = node; \
953         } \
954 }
955
956 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
957                                     Folder *folder)
958 {
959         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
960         GtkCTreeNode *prev = NULL;
961
962         gtk_ctree_sort_recursive(ctree, root);
963
964         if (GTK_CTREE_ROW(root)->parent) return;
965
966         SET_SPECIAL_FOLDER(ctree, folder->inbox);
967         SET_SPECIAL_FOLDER(ctree, folder->outbox);
968         SET_SPECIAL_FOLDER(ctree, folder->draft);
969         SET_SPECIAL_FOLDER(ctree, folder->queue);
970         SET_SPECIAL_FOLDER(ctree, folder->trash);
971 }
972
973 static void folderview_append_folder(FolderView *folderview, Folder *folder)
974 {
975         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
976         GtkCTreeNode *root;
977
978         g_return_if_fail(folder != NULL);
979
980         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
981                                       folderview_gnode_func, folderview);
982         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
983                                 folderview);
984         folderview_sort_folders(folderview, root, folder);
985 }
986
987 void folderview_new_folder(FolderView *folderview)
988 {
989         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
990         FolderItem *item;
991
992         if (!folderview->selected) return;
993
994         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
995         g_return_if_fail(item != NULL);
996         g_return_if_fail(item->folder != NULL);
997
998         switch (item->folder->type) {
999         case F_MBOX:
1000                 folderview_new_mbox_folder_cb(folderview, 0, NULL);
1001                 break;
1002         case F_MH:
1003         case F_MAILDIR:
1004                 folderview_new_folder_cb(folderview, 0, NULL);
1005                 break;
1006         case F_IMAP:
1007                 folderview_new_imap_folder_cb(folderview, 0, NULL);
1008                 break;
1009         case F_NEWS:
1010         default:
1011                 break;
1012         }
1013 }
1014
1015 void folderview_rename_folder(FolderView *folderview)
1016 {
1017         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1018         FolderItem *item;
1019
1020         if (!folderview->selected) return;
1021
1022         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1023         g_return_if_fail(item != NULL);
1024         g_return_if_fail(item->folder != NULL);
1025         if (!item->path) return;
1026         if (item->stype != F_NORMAL) return;
1027
1028         switch (item->folder->type) {
1029         case F_MH:
1030         case F_MBOX:
1031         case F_MAILDIR:
1032                 folderview_rename_folder_cb(folderview, 0, NULL);
1033                 break;
1034         case F_IMAP:
1035         case F_NEWS:
1036         default:
1037                 break;
1038         }
1039 }
1040
1041 void folderview_delete_folder(FolderView *folderview)
1042 {
1043         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1044         FolderItem *item;
1045
1046         if (!folderview->selected) return;
1047
1048         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1049         g_return_if_fail(item != NULL);
1050         g_return_if_fail(item->folder != NULL);
1051         if (!item->path) return;
1052         if (item->stype != F_NORMAL) return;
1053
1054         switch (item->folder->type) {
1055         case F_MH:
1056         case F_MBOX:
1057         case F_MAILDIR:
1058                 folderview_delete_folder_cb(folderview, 0, NULL);
1059                 break;
1060         case F_IMAP:
1061                 folderview_rm_imap_folder_cb(folderview, 0, NULL);
1062         case F_NEWS:
1063         default:
1064                 break;
1065         }
1066 }
1067
1068
1069 /* callback functions */
1070
1071 static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1072                                       FolderView *folderview)
1073 {
1074         GtkCList *clist = GTK_CLIST(ctree);
1075         gint prev_row = -1, row = -1, column = -1;
1076         FolderItem *item;
1077         Folder *folder;
1078
1079         if (!event) return;
1080
1081         if (event->button == 1) {
1082                 folderview->open_folder = TRUE;
1083                 return;
1084         }
1085
1086         if (event->button == 2 || event->button == 3) {
1087                 /* right clicked */
1088                 if (clist->selection) {
1089                         GtkCTreeNode *node;
1090
1091                         node = GTK_CTREE_NODE(clist->selection->data);
1092                         if (node)
1093                                 prev_row = gtkut_ctree_get_nth_from_node
1094                                         (GTK_CTREE(ctree), node);
1095                 }
1096
1097                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1098                                                   &row, &column))
1099                         return;
1100                 if (prev_row != row) {
1101                         gtk_clist_unselect_all(clist);
1102                         if (event->button == 2)
1103                                 folderview_select_node
1104                                         (folderview,
1105                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1106                                                             row));
1107                         else
1108                                 gtk_clist_select_row(clist, row, column);
1109                 }
1110         }
1111
1112         if (event->button != 3) return;
1113
1114         item = gtk_clist_get_row_data(clist, row);
1115         g_return_if_fail(item != NULL);
1116         g_return_if_fail(item->folder != NULL);
1117         folder = item->folder;
1118
1119         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->mail_popup));
1120         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->imap_popup));
1121         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->news_popup));
1122         menu_set_insensitive_all(GTK_MENU_SHELL(folderview->mbox_popup));
1123
1124         if (folder->type == F_MH && item->parent == NULL) {
1125                 menu_set_sensitive(folderview->mail_factory,
1126                                    "/Create new folder...", TRUE);
1127                 menu_set_sensitive(folderview->mail_factory,
1128                                    "/Remove mailbox", TRUE);
1129         } else if (folder->type == F_MH && item->stype != F_NORMAL) {
1130                 menu_set_sensitive(folderview->mail_factory,
1131                                    "/Create new folder...", TRUE);
1132         } else if (folder->type == F_MH) {
1133                 menu_set_sensitive(folderview->mail_factory,
1134                                    "/Create new folder...", TRUE);
1135                 menu_set_sensitive(folderview->mail_factory,
1136                                    "/Rename folder...", TRUE);
1137                 menu_set_sensitive(folderview->mail_factory,
1138                                    "/Delete folder", TRUE);
1139         } else if (folder->type == F_IMAP && item->parent == NULL) {
1140                 menu_set_sensitive(folderview->imap_factory,
1141                                    "/Create new folder...", TRUE);
1142                 menu_set_sensitive(folderview->imap_factory,
1143                                    "/Remove IMAP4 server", TRUE);
1144         } else if (folder->type == F_IMAP && item->parent != NULL) {
1145                 menu_set_sensitive(folderview->imap_factory,
1146                                    "/Create new folder...", TRUE);
1147                 menu_set_sensitive(folderview->imap_factory,
1148                                    "/Delete folder", TRUE);
1149         } else if (folder->type == F_NEWS && item->parent == NULL) {
1150                 menu_set_sensitive(folderview->news_factory,
1151                                    "/Subscribe to newsgroup...", TRUE);
1152                 menu_set_sensitive(folderview->news_factory,
1153                                    "/Remove news server", TRUE);
1154         } else if (folder->type == F_NEWS && item->parent != NULL) {
1155                 menu_set_sensitive(folderview->news_factory,
1156                                    "/Subscribe to newsgroup...", TRUE);
1157                 menu_set_sensitive(folderview->news_factory,
1158                                    "/Remove newsgroup", TRUE);
1159         }
1160         if (folder->type == F_MBOX && item->parent == NULL) {
1161                 menu_set_sensitive(folderview->mbox_factory,
1162                                    "/Create new folder...", TRUE);
1163                 menu_set_sensitive(folderview->mbox_factory,
1164                                    "/Remove mailbox", TRUE);
1165         } else if (folder->type == F_MBOX && item->stype != F_NORMAL) {
1166                 menu_set_sensitive(folderview->mbox_factory,
1167                                    "/Create new folder...", TRUE);
1168         } else if (folder->type == F_MBOX) {
1169                 menu_set_sensitive(folderview->mbox_factory,
1170                                    "/Create new folder...", TRUE);
1171                 menu_set_sensitive(folderview->mbox_factory,
1172                                    "/Rename folder...", TRUE);
1173                 menu_set_sensitive(folderview->mbox_factory,
1174                                    "/Delete folder", TRUE);
1175         }
1176
1177         if (folder->type == F_MH)
1178                 gtk_menu_popup(GTK_MENU(folderview->mail_popup), NULL, NULL,
1179                                NULL, NULL, event->button, event->time);
1180         else if (folder->type == F_IMAP)
1181                 gtk_menu_popup(GTK_MENU(folderview->imap_popup), NULL, NULL,
1182                                NULL, NULL, event->button, event->time);
1183         else if (folder->type == F_NEWS)
1184                 gtk_menu_popup(GTK_MENU(folderview->news_popup), NULL, NULL,
1185                                NULL, NULL, event->button, event->time);
1186         else if (folder->type == F_MBOX)
1187                 gtk_menu_popup(GTK_MENU(folderview->mbox_popup), NULL, NULL,
1188                                NULL, NULL, event->button, event->time);
1189 }
1190
1191 static void folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1192                                        FolderView *folderview)
1193 {
1194         if (!event) return;
1195
1196         if (event->button == 1 && folderview->opened != NULL) {
1197                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1198                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1199                                           folderview->opened);
1200         }
1201 }
1202
1203 #define BREAK_ON_MODIFIER_KEY() \
1204         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1205
1206 static void folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1207                                    FolderView *folderview)
1208 {
1209         if (!event) return;
1210
1211         switch (event->keyval) {
1212         case GDK_Return:
1213         case GDK_space:
1214                 if (folderview->selected) {
1215                         folderview_select_node(folderview,
1216                                                folderview->selected);
1217                 }
1218                 break;
1219         case GDK_v:
1220         case GDK_V:
1221         case GDK_g:
1222         case GDK_G:
1223         case GDK_x:
1224         case GDK_X:
1225         case GDK_w:
1226         case GDK_D:
1227         case GDK_Q:
1228                 BREAK_ON_MODIFIER_KEY();
1229                 summary_pass_key_press_event(folderview->summaryview, event);
1230         default:
1231         }
1232 }
1233
1234 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1235                                 gint column, FolderView *folderview)
1236 {
1237         static gboolean can_select = TRUE;      /* exclusive lock */
1238         FolderItem *item;
1239         gchar * s = NULL;
1240
1241         folderview->selected = row;
1242
1243         if (folderview->opened == row) {
1244                 folderview->open_folder = FALSE;
1245                 return;
1246         }
1247
1248         if (!can_select) {
1249                 gtk_ctree_select(ctree, folderview->opened);
1250                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1251                 return;
1252         }
1253
1254         if (!folderview->open_folder) return;
1255         folderview->open_folder = FALSE;
1256
1257         item = gtk_ctree_node_get_row_data(ctree, row);
1258         if (!item) return;
1259
1260         can_select = FALSE;
1261
1262         if (item->path)
1263                 debug_print(_("Folder %s is selected\n"), item->path);
1264
1265         if (!GTK_CTREE_ROW(row)->children)
1266                 gtk_ctree_expand(ctree, row);
1267         if (folderview->opened &&
1268             !GTK_CTREE_ROW(folderview->opened)->children)
1269                 gtk_ctree_collapse(ctree, folderview->opened);
1270
1271         folderview->opened = row;
1272
1273         summary_show(folderview->summaryview, item, FALSE);
1274
1275         can_select = TRUE;
1276 }
1277
1278 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1279                                      FolderView *folderview)
1280 {
1281         folderview_update_node(folderview, node);
1282 }
1283
1284 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1285                                       FolderView *folderview)
1286 {
1287         folderview_update_node(folderview, node);
1288 }
1289
1290 static void folderview_popup_close(GtkMenuShell *menu_shell,
1291                                    FolderView *folderview)
1292 {
1293         if (!folderview->opened) return;
1294
1295         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1296         gtkut_ctree_set_focus_row(GTK_CTREE(folderview->ctree),
1297                                   folderview->opened);
1298 }
1299
1300 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1301                                    FolderView *folderview)
1302 {
1303         switch (column) {
1304         case COL_FOLDER:
1305                 prefs_common.folder_col_folder = width;
1306                 break;
1307         case COL_NEW:
1308                 prefs_common.folder_col_new = width;
1309                 break;
1310         case COL_UNREAD:
1311                 prefs_common.folder_col_unread = width;
1312                 break;
1313         case COL_TOTAL:
1314                 prefs_common.folder_col_total = width;
1315                 break;
1316         default:
1317         }
1318 }
1319
1320 static GtkCTreeNode *folderview_find_by_name(GtkCTree *ctree,
1321                                              GtkCTreeNode *node,
1322                                              const gchar *name)
1323 {
1324         FolderItem *item;
1325
1326         if (!node)
1327                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1328         if (!node)
1329                 return NULL;
1330
1331         node = GTK_CTREE_ROW(node)->children;
1332
1333         while (node) {
1334                 item = gtk_ctree_node_get_row_data(ctree, node);
1335                 if (!folderview_compare_name(item, name))
1336                         return node;
1337                 node = GTK_CTREE_ROW(node)->sibling;
1338         }
1339
1340         return NULL;
1341 }
1342
1343 static void folderview_new_folder_cb(FolderView *folderview, guint action,
1344                                      GtkWidget *widget)
1345 {
1346         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1347         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1348         FolderItem *item;
1349         FolderItem *new_item;
1350         gchar *new_folder;
1351         GtkCTreeNode *node;
1352
1353         if (!folderview->selected) return;
1354
1355         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1356         g_return_if_fail(item != NULL);
1357         g_return_if_fail(item->folder != NULL);
1358
1359         new_folder = input_dialog(_("New folder"),
1360                                   _("Input the name of new folder:"),
1361                                   _("NewFolder"));
1362         if (!new_folder) return;
1363
1364         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1365                 alertpanel_error(_("`%c' can't be included in folder name."),
1366                                  G_DIR_SEPARATOR);
1367                 g_free(new_folder);
1368                 return;
1369         }
1370
1371         /* find whether the directory already exists */
1372         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1373                 alertpanel_error(_("The folder `%s' already exists."),
1374                                  new_folder);
1375                 g_free(new_folder);
1376                 return;
1377         }
1378
1379         new_item = item->folder->create_folder(item->folder, item, new_folder);
1380         g_free(new_folder);
1381         if (!new_item) return;
1382
1383         gtk_clist_freeze(GTK_CLIST(ctree));
1384
1385         text[COL_FOLDER] = new_item->name;
1386         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1387                                      FOLDER_SPACING,
1388                                      folderxpm, folderxpmmask,
1389                                      folderopenxpm, folderopenxpmmask,
1390                                      FALSE, FALSE);
1391         gtk_ctree_expand(ctree, folderview->selected);
1392         gtk_ctree_node_set_row_data(ctree, node, new_item);
1393         folderview_sort_folders(folderview, folderview->selected, item->folder);
1394
1395         gtk_clist_thaw(GTK_CLIST(ctree));
1396
1397         folder_write_list();
1398 }
1399
1400 static void folderview_new_mbox_folder_cb(FolderView *folderview, guint action,
1401                                           GtkWidget *widget)
1402 {
1403         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1404         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1405         FolderItem *item;
1406         FolderItem *new_item;
1407         gchar *new_folder;
1408         GtkCTreeNode *node;
1409
1410         if (!folderview->selected) return;
1411
1412         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1413         g_return_if_fail(item != NULL);
1414         g_return_if_fail(item->folder != NULL);
1415
1416         new_folder = input_dialog(_("New folder"),
1417                                   _("Input the name of new folder:"),
1418                                   _("NewFolder"));
1419         if (!new_folder) return;
1420
1421         /* find whether the directory already exists */
1422         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1423                 alertpanel_error(_("The folder `%s' already exists."),
1424                                  new_folder);
1425                 g_free(new_folder);
1426                 return;
1427         }
1428
1429         new_item = item->folder->create_folder(item->folder, item, new_folder);
1430         g_free(new_folder);
1431         if (!new_item) return;
1432
1433         gtk_clist_freeze(GTK_CLIST(ctree));
1434
1435         text[COL_FOLDER] = new_item->name;
1436         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1437                                      FOLDER_SPACING,
1438                                      folderxpm, folderxpmmask,
1439                                      folderopenxpm, folderopenxpmmask,
1440                                      FALSE, FALSE);
1441         gtk_ctree_expand(ctree, folderview->selected);
1442         gtk_ctree_node_set_row_data(ctree, node, new_item);
1443         folderview_sort_folders(folderview, folderview->selected, item->folder);
1444
1445         gtk_clist_thaw(GTK_CLIST(ctree));
1446
1447         folder_write_list();
1448 }
1449
1450 static void folderview_rename_folder_cb(FolderView *folderview, guint action,
1451                                         GtkWidget *widget)
1452 {
1453         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1454         FolderItem *item;
1455         gchar *new_folder;
1456         gchar *message;
1457
1458         if (!folderview->selected) return;
1459
1460         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1461         g_return_if_fail(item != NULL);
1462         g_return_if_fail(item->path != NULL);
1463         g_return_if_fail(item->folder != NULL);
1464
1465         message = g_strdup_printf(_("Input new name for `%s':"),
1466                                   g_basename(item->path));
1467         new_folder = input_dialog(_("Rename folder"), message,
1468                                   g_basename(item->path));
1469         g_free(message);
1470         if (!new_folder) return;
1471
1472         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1473                 alertpanel_error(_("`%c' can't be included in folder name."),
1474                                  G_DIR_SEPARATOR);
1475                 g_free(new_folder);
1476                 return;
1477         }
1478
1479         if (folderview_find_by_name
1480                 (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
1481                  new_folder)) {
1482                 alertpanel_error(_("The folder `%s' already exists."),
1483                                  new_folder);
1484                 g_free(new_folder);
1485                 return;
1486         }
1487
1488         if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
1489                 g_free(new_folder);
1490                 return;
1491         }
1492         g_free(new_folder);
1493
1494         gtk_clist_freeze(GTK_CLIST(ctree));
1495
1496         folderview_update_node(folderview, folderview->selected);
1497         folderview_sort_folders(folderview,
1498                                 GTK_CTREE_ROW(folderview->selected)->parent,
1499                                 item->folder);
1500         if (folderview->opened == folderview->selected) {
1501                 if (!GTK_CTREE_ROW(folderview->opened)->children)
1502                         gtk_ctree_expand(ctree, folderview->opened);
1503                 summary_show(folderview->summaryview, item, FALSE);
1504         }
1505
1506         gtk_clist_thaw(GTK_CLIST(ctree));
1507
1508         folder_write_list();
1509 }
1510
1511 static void folderview_delete_folder_cb(FolderView *folderview, guint action,
1512                                         GtkWidget *widget)
1513 {
1514         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1515         FolderItem *item;
1516         gchar *message;
1517         AlertValue avalue;
1518
1519         if (!folderview->selected) return;
1520
1521         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1522         g_return_if_fail(item != NULL);
1523         g_return_if_fail(item->path != NULL);
1524         g_return_if_fail(item->folder != NULL);
1525
1526         message = g_strdup_printf
1527                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
1528                    "Do you really want to delete?"),
1529                  g_basename(item->path));
1530         avalue = alertpanel(_("Delete folder"), message,
1531                             _("Yes"), _("+No"), NULL);
1532         g_free(message);
1533         if (avalue != G_ALERTDEFAULT) return;
1534
1535         if (item->folder->remove_folder(item->folder, item) < 0) {
1536                 g_warning(_("can't remove folder `%s'\n"), item->path);
1537                 return;
1538         }
1539
1540         if (folderview->opened == folderview->selected ||
1541             gtk_ctree_is_ancestor(ctree,
1542                                   folderview->selected,
1543                                   folderview->opened)) {
1544                 summary_clear_all(folderview->summaryview);
1545                 folderview->opened = NULL;
1546         }
1547
1548         gtk_ctree_remove_node(ctree, folderview->selected);
1549         folder_write_list();
1550 }
1551
1552 static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
1553                                          GtkWidget *widget)
1554 {
1555         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1556         GtkCTreeNode *node;
1557         FolderItem *item;
1558         gchar *message;
1559         AlertValue avalue;
1560
1561         if (!folderview->selected) return;
1562         node = folderview->selected;
1563         item = gtk_ctree_node_get_row_data(ctree, node);
1564         g_return_if_fail(item != NULL);
1565         g_return_if_fail(item->folder != NULL);
1566         if (item->parent) return;
1567
1568         message = g_strdup_printf
1569                 (_("Really remove the mailbox `%s' ?\n"
1570                    "(The messages are NOT deleted from disk)"),
1571                  item->folder->name);
1572         avalue = alertpanel(_("Remove folder"), message,
1573                             _("Yes"), _("+No"), NULL);
1574         g_free(message);
1575         if (avalue != G_ALERTDEFAULT) return;
1576
1577         folder_destroy(item->folder);
1578         summary_clear_all(folderview->summaryview);
1579         folderview_unselect(folderview);
1580         gtk_ctree_remove_node(ctree, node);
1581         folder_write_list();
1582 }
1583
1584 static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
1585                                           GtkWidget *widget)
1586 {
1587         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1588         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1589         GtkCTreeNode *node;
1590         FolderItem *item;
1591         FolderItem *new_item;
1592         gchar *new_folder;
1593
1594         if (!folderview->selected) return;
1595
1596         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1597         g_return_if_fail(item != NULL);
1598         g_return_if_fail(item->folder != NULL);
1599         g_return_if_fail(item->folder->type == F_IMAP);
1600         g_return_if_fail(item->folder->account != NULL);
1601
1602         new_folder = input_dialog(_("New folder"),
1603                                   _("Input the name of new folder:"),
1604                                   _("NewFolder"));
1605         if (!new_folder) return;
1606
1607         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
1608                 alertpanel_error(_("`%c' can't be included in folder name."),
1609                                  G_DIR_SEPARATOR);
1610                 g_free(new_folder);
1611                 return;
1612         }
1613
1614         /* find whether the directory already exists */
1615         if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
1616                 alertpanel_error(_("The folder `%s' already exists."),
1617                                  new_folder);
1618                 g_free(new_folder);
1619                 return;
1620         }
1621
1622         new_item = item->folder->create_folder(item->folder, item, new_folder);
1623         g_free(new_folder);
1624         if (!new_item) return;
1625
1626         gtk_clist_freeze(GTK_CLIST(ctree));
1627
1628         text[COL_FOLDER] = new_item->name;
1629         node = gtk_ctree_insert_node(ctree, folderview->selected, NULL, text,
1630                                      FOLDER_SPACING,
1631                                      folderxpm, folderxpmmask,
1632                                      folderopenxpm, folderopenxpmmask,
1633                                      FALSE, FALSE);
1634         gtk_ctree_expand(ctree, folderview->selected);
1635         gtk_ctree_node_set_row_data(ctree, node, new_item);
1636         folderview_sort_folders(folderview, folderview->selected, item->folder);
1637
1638         gtk_clist_thaw(GTK_CLIST(ctree));
1639
1640         folder_write_list();
1641 }
1642
1643 static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
1644                                          GtkWidget *widget)
1645 {
1646         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1647         FolderItem *item;
1648         gchar *message;
1649         AlertValue avalue;
1650
1651         if (!folderview->selected) return;
1652
1653         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1654         g_return_if_fail(item != NULL);
1655         g_return_if_fail(item->folder != NULL);
1656         g_return_if_fail(item->folder->type == F_IMAP);
1657         g_return_if_fail(item->folder->account != NULL);
1658
1659         message = g_strdup_printf(_("Really delete folder `%s'?"),
1660                                   g_basename(item->path));
1661         avalue = alertpanel(_("Delete folder"), message,
1662                             _("Yes"), _("+No"), NULL);
1663         g_free(message);
1664         if (avalue != G_ALERTDEFAULT) return;
1665
1666         if (item->folder->remove_folder(item->folder, item) < 0) {
1667                 g_warning(_("can't remove folder `%s'\n"), item->path);
1668                 return;
1669         }
1670
1671         if (folderview->opened == folderview->selected ||
1672             gtk_ctree_is_ancestor(ctree,
1673                                   folderview->selected,
1674                                   folderview->opened)) {
1675                 summary_clear_all(folderview->summaryview);
1676                 folderview->opened = NULL;
1677         }
1678
1679         gtk_ctree_remove_node(ctree, folderview->selected);
1680         folder_write_list();
1681 }
1682
1683 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
1684                                          GtkWidget *widget)
1685 {
1686         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1687         FolderItem *item;
1688         gchar *message;
1689         AlertValue avalue;
1690
1691         if (!folderview->selected) return;
1692
1693         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1694         g_return_if_fail(item != NULL);
1695         g_return_if_fail(item->folder != NULL);
1696         g_return_if_fail(item->folder->type == F_IMAP);
1697         g_return_if_fail(item->folder->account != NULL);
1698
1699         message = g_strdup_printf(_("Really delete IMAP4 server `%s'?"),
1700                                   item->folder->name);
1701         avalue = alertpanel(_("Delete IMAP4 server"), message,
1702                             _("Yes"), _("+No"), NULL);
1703         g_free(message);
1704
1705         if (avalue != G_ALERTDEFAULT) return;
1706
1707         if (folderview->opened == folderview->selected ||
1708             gtk_ctree_is_ancestor(ctree,
1709                                   folderview->selected,
1710                                   folderview->opened)) {
1711                 summary_clear_all(folderview->summaryview);
1712                 folderview->opened = NULL;
1713         }
1714
1715         account_destroy(item->folder->account);
1716         folder_destroy(item->folder);
1717         gtk_ctree_remove_node(ctree, folderview->selected);
1718         account_set_menu();
1719         main_window_reflect_prefs_all();
1720         folder_write_list();
1721 }
1722
1723 static void folderview_new_news_group_cb(FolderView *folderview, guint action,
1724                                          GtkWidget *widget)
1725 {
1726         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1727         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
1728         GtkCTreeNode *servernode, *node;
1729         FolderItem *item;
1730         FolderItem *newitem;
1731         gchar *new_group;
1732         const gchar *server;
1733
1734         if (!folderview->selected) return;
1735
1736         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1737         g_return_if_fail(item != NULL);
1738         g_return_if_fail(item->folder != NULL);
1739         g_return_if_fail(item->folder->type == F_NEWS);
1740         g_return_if_fail(item->folder->account != NULL);
1741
1742         new_group = grouplist_dialog(item);
1743         if (!new_group) return;
1744
1745         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
1746                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
1747         else
1748                 servernode = folderview->selected;
1749
1750         if (folderview_find_by_name(ctree, servernode, new_group)) {
1751                 alertpanel_error(_("The newsgroup `%s' already exists."),
1752                                  new_group);
1753                 g_free(new_group);
1754                 return;
1755         }
1756
1757         gtk_clist_freeze(GTK_CLIST(ctree));
1758
1759         text[COL_FOLDER] = new_group;
1760         node = gtk_ctree_insert_node(ctree, servernode, NULL, text,
1761                                      FOLDER_SPACING,
1762                                      folderxpm, folderxpmmask,
1763                                      folderopenxpm, folderopenxpmmask,
1764                                      FALSE, FALSE);
1765         gtk_ctree_expand(ctree, servernode);
1766
1767         item = gtk_ctree_node_get_row_data(ctree, servernode);
1768         server = item->folder->account->nntp_server;
1769
1770         newitem = folder_item_new(new_group, new_group);
1771         g_free(new_group);
1772         folder_item_append(item, newitem);
1773         gtk_ctree_node_set_row_data(ctree, node, newitem);
1774         gtk_ctree_sort_node(ctree, servernode);
1775
1776         gtk_clist_thaw(GTK_CLIST(ctree));
1777
1778         folder_write_list();
1779 }
1780
1781 static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
1782                                         GtkWidget *widget)
1783 {
1784         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1785         FolderItem *item;
1786         gchar *message;
1787         AlertValue avalue;
1788
1789         if (!folderview->selected) return;
1790
1791         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1792         g_return_if_fail(item != NULL);
1793         g_return_if_fail(item->folder != NULL);
1794         g_return_if_fail(item->folder->type == F_NEWS);
1795         g_return_if_fail(item->folder->account != NULL);
1796
1797         message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
1798                                   g_basename(item->path));
1799         avalue = alertpanel(_("Delete newsgroup"), message,
1800                             _("Yes"), _("+No"), NULL);
1801         g_free(message);
1802         if (avalue != G_ALERTDEFAULT) return;
1803
1804         if (folderview->opened == folderview->selected) {
1805                 summary_clear_all(folderview->summaryview);
1806                 folderview->opened = NULL;
1807         }
1808
1809         folder_item_remove(item);
1810         gtk_ctree_remove_node(ctree, folderview->selected);
1811         folder_write_list();
1812 }
1813
1814 static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
1815                                          GtkWidget *widget)
1816 {
1817         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1818         FolderItem *item;
1819         gchar *message;
1820         AlertValue avalue;
1821
1822         if (!folderview->selected) return;
1823
1824         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
1825         g_return_if_fail(item != NULL);
1826         g_return_if_fail(item->folder != NULL);
1827         g_return_if_fail(item->folder->type == F_NEWS);
1828         g_return_if_fail(item->folder->account != NULL);
1829
1830         message = g_strdup_printf(_("Really delete news server `%s'?"),
1831                                   item->folder->name);
1832         avalue = alertpanel(_("Delete news server"), message,
1833                             _("Yes"), _("+No"), NULL);
1834         g_free(message);
1835
1836         if (avalue != G_ALERTDEFAULT) return;
1837
1838         if (folderview->opened == folderview->selected ||
1839             gtk_ctree_is_ancestor(ctree,
1840                                   folderview->selected,
1841                                   folderview->opened)) {
1842                 summary_clear_all(folderview->summaryview);
1843                 folderview->opened = NULL;
1844         }
1845
1846         account_destroy(item->folder->account);
1847         folder_destroy(item->folder);
1848         gtk_ctree_remove_node(ctree, folderview->selected);
1849         account_set_menu();
1850         main_window_reflect_prefs_all();
1851         folder_write_list();
1852 }
1853
1854 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
1855                                           GdkDragContext *context,
1856                                           gint            x,
1857                                           gint            y,
1858                                           guint           time,
1859                                           FolderView     *folderview)
1860 {
1861         gint row, column;
1862         FolderItem *item;
1863         GtkCTreeNode *node;
1864
1865         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
1866                                          x - 24, y - 24, &row, &column)) {
1867                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
1868                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
1869                 if (item != NULL &&
1870                     item->path != NULL &&
1871                     folderview->summaryview->folder_item != item) {
1872                         if (item->folder->type != F_NEWS) {
1873                                 gtk_ctree_select(GTK_CTREE(widget), node);
1874                                 gdk_drag_status(context,
1875                                                 context->suggested_action,
1876                                                 time);
1877                                 return TRUE;
1878                         }
1879                 }
1880         }
1881
1882         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
1883         gdk_drag_status(context, 0, time);
1884
1885         return FALSE;
1886 }
1887
1888 static void folderview_drag_leave_cb(GtkWidget      *widget,
1889                                      GdkDragContext *context,
1890                                      guint           time,
1891                                      FolderView     *folderview)
1892 {
1893         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
1894 }
1895
1896 static void folderview_drag_received_cb(GtkWidget        *widget,
1897                                         GdkDragContext   *drag_context,
1898                                         gint              x,
1899                                         gint              y,
1900                                         GtkSelectionData *data,
1901                                         guint             info,
1902                                         guint             time,
1903                                         FolderView       *folderview)
1904 {
1905         gint row, column;
1906         FolderItem *item;
1907         GtkCTreeNode *node;
1908
1909         if (gtk_clist_get_selection_info(GTK_CLIST(widget),
1910                                          x - 24, y - 24, &row, &column) == 0)
1911                 return;
1912
1913         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
1914         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
1915         if (item != NULL) {
1916                 summary_move_selected_to(folderview->summaryview, item);
1917                 gtk_drag_finish(drag_context, TRUE, TRUE, time);
1918         } else
1919                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
1920 }
1921
1922 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
1923 {
1924         const FolderItem *item = a;
1925         const gchar *name = b;
1926
1927         if (!item->path) return -1;
1928         return strcmp2(g_basename(item->path), name);
1929 }