2006-04-06 [colin] 2.1.0cvs9
[claws.git] / src / folderview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkscrolledwindow.h>
27 #include <gtk/gtkctree.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtkclist.h>
30 #include <gtk/gtkstyle.h>
31 #include <gtk/gtksignal.h>
32 #include <gtk/gtkmain.h>
33 #include <gtk/gtkstatusbar.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkitemfactory.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40
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 "manage_window.h"
48 #include "alertpanel.h"
49 #include "menu.h"
50 #include "stock_pixmap.h"
51 #include "procmsg.h"
52 #include "utils.h"
53 #include "gtkutils.h"
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56 #include "prefs_filtering.h"
57 #include "prefs_folder_item.h"
58 #include "account.h"
59 #include "folder.h"
60 #include "foldersel.h"
61 #include "inc.h"
62 #include "statusbar.h"
63 #include "hooks.h"
64 #include "folderutils.h"
65 #include "partial_download.h"
66 #include "prefs_folder_column.h"
67 #include "filtering.h"
68
69 #define COL_FOLDER_WIDTH        150
70 #define COL_NUM_WIDTH           32
71
72 static GList *folderview_list = NULL;
73
74 static GtkStyle *normal_style;
75 static GtkStyle *normal_color_style;
76 static GtkStyle *bold_style;
77 static GtkStyle *bold_color_style;
78 static GtkStyle *bold_tgtfold_style;
79
80 static GdkPixmap *inboxxpm;
81 static GdkBitmap *inboxxpmmask;
82 static GdkPixmap *inboxhrmxpm;
83 static GdkBitmap *inboxhrmxpmmask;
84 static GdkPixmap *inboxopenxpm;
85 static GdkBitmap *inboxopenxpmmask;
86 static GdkPixmap *inboxopenhrmxpm;
87 static GdkBitmap *inboxopenhrmxpmmask;
88 static GdkPixmap *outboxxpm;
89 static GdkBitmap *outboxxpmmask;
90 static GdkPixmap *outboxhrmxpm;
91 static GdkBitmap *outboxhrmxpmmask;
92 static GdkPixmap *outboxopenxpm;
93 static GdkBitmap *outboxopenxpmmask;
94 static GdkPixmap *outboxopenhrmxpm;
95 static GdkBitmap *outboxopenhrmxpmmask;
96 static GdkPixmap *folderxpm;
97 static GdkBitmap *folderxpmmask;
98 static GdkPixmap *folderhrmxpm;
99 static GdkBitmap *folderhrmxpmmask;
100 static GdkPixmap *folderopenxpm;
101 static GdkBitmap *folderopenxpmmask;
102 static GdkPixmap *folderopenhrmxpm;
103 static GdkBitmap *folderopenhrmxpmmask;
104 static GdkPixmap *trashopenxpm;
105 static GdkBitmap *trashopenxpmmask;
106 static GdkPixmap *trashopenhrmxpm;
107 static GdkBitmap *trashopenhrmxpmmask;
108 static GdkPixmap *trashxpm;
109 static GdkBitmap *trashxpmmask;
110 static GdkPixmap *trashhrmxpm;
111 static GdkBitmap *trashhrmxpmmask;
112 static GdkPixmap *queuexpm;
113 static GdkBitmap *queuexpmmask;
114 static GdkPixmap *queuehrmxpm;
115 static GdkBitmap *queuehrmxpmmask;
116 static GdkPixmap *queueopenxpm;
117 static GdkBitmap *queueopenxpmmask;
118 static GdkPixmap *queueopenhrmxpm;
119 static GdkBitmap *queueopenhrmxpmmask;
120 static GdkPixmap *draftsxpm;
121 static GdkBitmap *draftsxpmmask;
122 static GdkPixmap *draftsopenxpm;
123 static GdkBitmap *draftsopenxpmmask;
124 static GdkPixmap *noselectxpm;
125 static GdkBitmap *noselectxpmmask;
126
127 static GdkPixmap *m_inboxxpm;
128 static GdkBitmap *m_inboxxpmmask;
129 static GdkPixmap *m_inboxhrmxpm;
130 static GdkBitmap *m_inboxhrmxpmmask;
131 static GdkPixmap *m_inboxopenxpm;
132 static GdkBitmap *m_inboxopenxpmmask;
133 static GdkPixmap *m_inboxopenhrmxpm;
134 static GdkBitmap *m_inboxopenhrmxpmmask;
135 static GdkPixmap *m_outboxxpm;
136 static GdkBitmap *m_outboxxpmmask;
137 static GdkPixmap *m_outboxhrmxpm;
138 static GdkBitmap *m_outboxhrmxpmmask;
139 static GdkPixmap *m_outboxopenxpm;
140 static GdkBitmap *m_outboxopenxpmmask;
141 static GdkPixmap *m_outboxopenhrmxpm;
142 static GdkBitmap *m_outboxopenhrmxpmmask;
143 static GdkPixmap *m_folderxpm;
144 static GdkBitmap *m_folderxpmmask;
145 static GdkPixmap *m_folderhrmxpm;
146 static GdkBitmap *m_folderhrmxpmmask;
147 static GdkPixmap *m_folderopenxpm;
148 static GdkBitmap *m_folderopenxpmmask;
149 static GdkPixmap *m_folderopenhrmxpm;
150 static GdkBitmap *m_folderopenhrmxpmmask;
151 static GdkPixmap *m_trashopenxpm;
152 static GdkBitmap *m_trashopenxpmmask;
153 static GdkPixmap *m_trashopenhrmxpm;
154 static GdkBitmap *m_trashopenhrmxpmmask;
155 static GdkPixmap *m_trashxpm;
156 static GdkBitmap *m_trashxpmmask;
157 static GdkPixmap *m_trashhrmxpm;
158 static GdkBitmap *m_trashhrmxpmmask;
159 static GdkPixmap *m_queuexpm;
160 static GdkBitmap *m_queuexpmmask;
161 static GdkPixmap *m_queuehrmxpm;
162 static GdkBitmap *m_queuehrmxpmmask;
163 static GdkPixmap *m_queueopenxpm;
164 static GdkBitmap *m_queueopenxpmmask;
165 static GdkPixmap *m_queueopenhrmxpm;
166 static GdkBitmap *m_queueopenhrmxpmmask;
167 static GdkPixmap *m_draftsxpm;
168 static GdkBitmap *m_draftsxpmmask;
169 static GdkPixmap *m_draftsopenxpm;
170 static GdkBitmap *m_draftsopenxpmmask;
171
172 static GdkPixmap *newxpm;
173 static GdkBitmap *newxpmmask;
174 static GdkPixmap *unreadxpm;
175 static GdkBitmap *unreadxpmmask;
176 static GdkPixmap *readxpm;
177 static GdkBitmap *readxpmmask;
178
179
180 static void folderview_select_node       (FolderView    *folderview,
181                                           GtkCTreeNode  *node);
182 static void folderview_set_folders       (FolderView    *folderview);
183 static void folderview_sort_folders      (FolderView    *folderview,
184                                           GtkCTreeNode  *root,
185                                           Folder        *folder);
186 static void folderview_append_folder     (FolderView    *folderview,
187                                           Folder        *folder);
188 static void folderview_update_node       (FolderView    *folderview,
189                                           GtkCTreeNode  *node);
190
191 static gint folderview_clist_compare    (GtkCList       *clist,
192                                          gconstpointer   ptr1,
193                                          gconstpointer   ptr2);
194
195 /* callback functions */
196 static gboolean folderview_button_pressed       (GtkWidget      *ctree,
197                                                  GdkEventButton *event,
198                                                  FolderView     *folderview);
199 static gboolean folderview_button_released      (GtkWidget      *ctree,
200                                                  GdkEventButton *event,
201                                                  FolderView     *folderview);
202 static gboolean folderview_key_pressed  (GtkWidget      *widget,
203                                          GdkEventKey    *event,
204                                          FolderView     *folderview);
205 static void folderview_selected         (GtkCTree       *ctree,
206                                          GtkCTreeNode   *row,
207                                          gint            column,
208                                          FolderView     *folderview);
209 static void folderview_tree_expanded    (GtkCTree       *ctree,
210                                          GtkCTreeNode   *node,
211                                          FolderView     *folderview);
212 static void folderview_tree_collapsed   (GtkCTree       *ctree,
213                                          GtkCTreeNode   *node,
214                                          FolderView     *folderview);
215 static void folderview_popup_close      (GtkMenuShell   *menu_shell,
216                                          FolderView     *folderview);
217 static void folderview_col_resized      (GtkCList       *clist,
218                                          gint            column,
219                                          gint            width,
220                                          FolderView     *folderview);
221
222 static void mark_all_read_cb            (FolderView    *folderview,
223                                          guint           action,
224                                          GtkWidget      *widget);
225
226 static void folderview_empty_trash_cb   (FolderView     *folderview,
227                                          guint           action,
228                                          GtkWidget      *widget);
229
230 static void folderview_search_cb        (FolderView     *folderview,
231                                          guint           action,
232                                          GtkWidget      *widget);
233
234 static void folderview_property_cb      (FolderView     *folderview,
235                                          guint           action,
236                                          GtkWidget      *widget);
237
238 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
239                                           GdkDragContext *context,
240                                           gint            x,
241                                           gint            y,
242                                           guint           time,
243                                           FolderView     *folderview);
244 static void folderview_drag_leave_cb     (GtkWidget        *widget,
245                                           GdkDragContext   *context,
246                                           guint             time,
247                                           FolderView       *folderview);
248 static void folderview_drag_received_cb  (GtkWidget        *widget,
249                                           GdkDragContext   *drag_context,
250                                           gint              x,
251                                           gint              y,
252                                           GtkSelectionData *data,
253                                           guint             info,
254                                           guint             time,
255                                           FolderView       *folderview);
256 static void folderview_start_drag        (GtkWidget *widget, gint button, GdkEvent *event,
257                                           FolderView       *folderview);
258 static void folderview_drag_data_get     (GtkWidget        *widget,
259                                           GdkDragContext   *drag_context,
260                                           GtkSelectionData *selection_data,
261                                           guint             info,
262                                           guint             time,
263                                           FolderView       *folderview);
264 static void folderview_drag_end_cb       (GtkWidget        *widget,
265                                           GdkDragContext   *drag_context,
266                                           FolderView       *folderview);
267
268 void folderview_create_folder_node       (FolderView       *folderview, 
269                                           FolderItem       *item);
270 gboolean folderview_update_folder        (gpointer          source,
271                                           gpointer          userdata);
272 gboolean folderview_update_item_claws    (gpointer          source,
273                                           gpointer          data);
274 static void folderview_processing_cb(FolderView *folderview, guint action,
275                                      GtkWidget *widget);
276
277 GHashTable *folderview_popups;
278
279 static GtkItemFactoryEntry folderview_common_popup_entries[] =
280 {
281         {N_("/Mark all re_ad"),         NULL, mark_all_read_cb, 0, NULL},
282         {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
283         {N_("/_Properties..."),         NULL, folderview_property_cb, 0, NULL},
284         {N_("/Process_ing..."),         NULL, folderview_processing_cb, 0, NULL},
285 };
286
287 static GtkItemFactoryEntry folder_view_trash_popup_entries[] = {
288         {N_("/------"),                 NULL, NULL, 0, "<Separator>"},
289         {N_("/Empty _trash..."),        NULL, folderview_empty_trash_cb, 0, NULL},
290 };
291
292
293 GtkTargetEntry folderview_drag_types[] =
294 {
295         {"sylpheed-claws/internal", GTK_TARGET_SAME_APP, TARGET_DUMMY},
296         {"text/uri-list", 0, TARGET_MAIL_URI_LIST}
297 };
298
299 void folderview_initialize(void)
300 {
301         FolderViewPopup *fpopup;
302         guint i, n_entries;
303         GSList *entries = NULL;
304
305         fpopup = g_new0(FolderViewPopup, 1);
306
307         n_entries = sizeof(folderview_common_popup_entries) /
308                 sizeof(folderview_common_popup_entries[0]);
309         for (i = 0; i < n_entries; i++)
310                 entries = g_slist_append(entries, &folderview_common_popup_entries[i]);
311
312         fpopup->klass = "common";
313         fpopup->path = "<CommonFolder>";
314         fpopup->entries = entries;
315         fpopup->set_sensitivity = NULL;
316
317         folderview_popups = g_hash_table_new(g_str_hash, g_str_equal);
318         g_hash_table_insert(folderview_popups, "common", fpopup);
319 }
320
321 static GtkItemFactory *create_ifactory(FolderView *folderview, FolderViewPopup *fpopup)
322 {
323         GSList *entries;
324         GtkItemFactory *factory;
325         FolderViewPopup *fpopup_common;
326         GtkWidget *popup;
327
328         factory = gtk_item_factory_new(GTK_TYPE_MENU, fpopup->path, NULL);
329         gtk_item_factory_set_translate_func(factory, menu_translate,
330                                             NULL, NULL);
331
332         for (entries = fpopup->entries; entries != NULL; entries = g_slist_next(entries))
333                 gtk_item_factory_create_item(factory, entries->data, folderview, 1);
334
335         fpopup_common = g_hash_table_lookup(folderview_popups, "common");
336         if (fpopup_common != fpopup)
337                 for (entries = fpopup_common->entries; entries != NULL; entries = g_slist_next(entries))
338                         gtk_item_factory_create_item(factory, entries->data, folderview, 1);
339
340         popup = gtk_item_factory_get_widget(factory, fpopup->path);
341         g_signal_connect(G_OBJECT(popup), "selection_done",
342                          G_CALLBACK(folderview_popup_close),
343                          folderview);
344
345         return factory;
346 }
347
348 static void create_ifactories(gpointer key, gpointer value, gpointer data)
349 {
350         FolderView *folderview = data;
351         FolderViewPopup *fpopup = value;
352         GtkItemFactory *factory;
353
354         factory = create_ifactory(folderview, fpopup);
355         g_hash_table_insert(folderview->popups, fpopup->klass, factory);
356 }
357
358 static void folderview_column_set_titles(FolderView *folderview)
359 {
360         GtkWidget *ctree = folderview->ctree;
361         GtkWidget *label_new;
362         GtkWidget *label_unread;
363         GtkWidget *label_total;
364         GtkWidget *hbox_new;
365         GtkWidget *hbox_unread;
366         GtkWidget *hbox_total;
367         gint *col_pos = folderview->col_pos;
368         
369         debug_print("setting titles...\n");
370         gtk_widget_realize(folderview->ctree);
371         gtk_widget_show_all(folderview->scrolledwin);
372         
373         /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
374          * instead text (text overflows making them unreadable and ugly) */
375         stock_pixmap_gdk(ctree, STOCK_PIXMAP_NEW,
376                          &newxpm, &newxpmmask);
377         stock_pixmap_gdk(ctree, STOCK_PIXMAP_UNREAD,
378                          &unreadxpm, &unreadxpmmask);
379         stock_pixmap_gdk(ctree, STOCK_PIXMAP_READ,
380                          &readxpm, &readxpmmask);
381                 
382         label_new = gtk_pixmap_new(newxpm, newxpmmask);
383         label_unread = gtk_pixmap_new(unreadxpm, unreadxpmmask);
384         label_total = gtk_pixmap_new(readxpm, readxpmmask);
385
386         hbox_new = gtk_hbox_new(FALSE, 4);
387         hbox_unread = gtk_hbox_new(FALSE, 4);
388         hbox_total = gtk_hbox_new(FALSE, 4);
389
390         /* left justified */
391         gtk_box_pack_start(GTK_BOX(hbox_new), label_new, TRUE, TRUE, 0);
392         gtk_misc_set_alignment (GTK_MISC (label_new), 1, 0.5);
393         gtk_box_pack_start(GTK_BOX(hbox_unread), label_unread, TRUE, TRUE, 0);
394         gtk_misc_set_alignment (GTK_MISC (label_unread), 1, 0.5);
395         gtk_box_pack_start(GTK_BOX(hbox_total), label_total, TRUE, TRUE, 0);
396         gtk_misc_set_alignment (GTK_MISC (label_total), 1, 0.5);
397
398         gtk_widget_show_all(hbox_new);
399         gtk_widget_show_all(hbox_unread);
400         gtk_widget_show_all(hbox_total);
401
402         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_NEW],hbox_new);
403         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_UNREAD],hbox_unread);
404         gtk_clist_set_column_widget(GTK_CLIST(ctree),col_pos[F_COL_TOTAL],hbox_total);
405 }
406
407 GtkWidget *folderview_ctree_create(FolderView *folderview)
408 {
409         GtkWidget *ctree;
410         gint *col_pos;
411         FolderColumnState *col_state;
412         FolderColumnType type;
413         gchar *titles[N_FOLDER_COLS];
414         gint i;
415         GtkWidget *scrolledwin = folderview->scrolledwin;
416
417         debug_print("creating tree...\n");
418         memset(titles, 0, sizeof(titles));
419
420         col_state = prefs_folder_column_get_config();
421         memset(titles, 0, sizeof(titles));
422
423         col_pos = folderview->col_pos;
424
425         for (i = 0; i < N_FOLDER_COLS; i++) {
426                 folderview->col_state[i] = col_state[i];
427                 type = col_state[i].type;
428                 col_pos[type] = i;
429         }
430
431         titles[col_pos[F_COL_FOLDER]] = _("Folder");
432         titles[col_pos[F_COL_NEW]]    = _("New");
433         titles[col_pos[F_COL_UNREAD]] = _("Unread");
434         /* TRANSLATORS: This in Number sign in American style */
435         titles[col_pos[F_COL_TOTAL]]  = _("#");
436
437         ctree = gtk_sctree_new_with_titles(N_FOLDER_COLS, col_pos[F_COL_FOLDER],
438                                            titles);
439         
440         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
441         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[F_COL_NEW],
442                                            GTK_JUSTIFY_RIGHT);
443         gtk_clist_set_column_justification(GTK_CLIST(ctree),
444                                            col_pos[F_COL_UNREAD],
445                                            GTK_JUSTIFY_RIGHT);
446         gtk_clist_set_column_justification(GTK_CLIST(ctree), 
447                                            col_pos[F_COL_TOTAL],
448                                            GTK_JUSTIFY_RIGHT);
449         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
450         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
451                                      GTK_CTREE_EXPANDER_SQUARE);
452         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
453         gtk_clist_set_compare_func(GTK_CLIST(ctree), folderview_clist_compare);
454
455         /* don't let title buttons take key focus */
456         for (i = 0; i < N_FOLDER_COLS; i++) {
457                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
458                                        GTK_CAN_FOCUS);
459                 gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[i],
460                                    prefs_common.folder_col_size[i]);
461                 gtk_clist_set_column_visibility
462                         (GTK_CLIST(ctree), i, col_state[i].visible);
463         }
464
465         g_signal_connect(G_OBJECT(ctree), "key_press_event",
466                          G_CALLBACK(folderview_key_pressed),
467                          folderview);
468         g_signal_connect(G_OBJECT(ctree), "button_press_event",
469                          G_CALLBACK(folderview_button_pressed),
470                          folderview);
471         g_signal_connect(G_OBJECT(ctree), "button_release_event",
472                          G_CALLBACK(folderview_button_released),
473                          folderview);
474         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
475                          G_CALLBACK(folderview_selected), folderview);
476         g_signal_connect(G_OBJECT(ctree), "start_drag",
477                          G_CALLBACK(folderview_start_drag), folderview);
478         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
479                          G_CALLBACK(folderview_drag_data_get),
480                          folderview);
481
482         g_signal_connect_after(G_OBJECT(ctree), "tree_expand",
483                                G_CALLBACK(folderview_tree_expanded),
484                                folderview);
485         g_signal_connect_after(G_OBJECT(ctree), "tree_collapse",
486                                G_CALLBACK(folderview_tree_collapsed),
487                                folderview);
488
489         g_signal_connect(G_OBJECT(ctree), "resize_column",
490                          G_CALLBACK(folderview_col_resized),
491                          folderview);
492
493         /* drop callback */
494         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
495                           folderview_drag_types, 2,
496                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
497         g_signal_connect(G_OBJECT(ctree), "drag_motion",
498                          G_CALLBACK(folderview_drag_motion_cb),
499                          folderview);
500         g_signal_connect(G_OBJECT(ctree), "drag_leave",
501                          G_CALLBACK(folderview_drag_leave_cb),
502                          folderview);
503         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
504                          G_CALLBACK(folderview_drag_received_cb),
505                          folderview);
506         g_signal_connect(G_OBJECT(ctree), "drag_end",
507                          G_CALLBACK(folderview_drag_end_cb),
508                          folderview);
509
510         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
511
512         return ctree;
513 }
514
515 void folderview_set_column_order(FolderView *folderview)
516 {
517         GtkWidget *ctree;
518         FolderItem *item = folderview_get_selected_item(folderview);
519         GtkWidget *scrolledwin = folderview->scrolledwin;
520
521         debug_print("recreating tree...\n");
522         gtk_widget_destroy(folderview->ctree);
523
524         folderview->ctree = ctree = folderview_ctree_create(folderview);
525         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
526                                             GTK_CLIST(ctree)->hadjustment);
527         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
528                                             GTK_CLIST(ctree)->vadjustment);
529         gtk_widget_show(ctree);
530         
531         folderview_set(folderview);
532         folderview_column_set_titles(folderview);
533
534         folderview_select(folderview,item);
535 }
536
537 FolderView *folderview_create(void)
538 {
539         FolderView *folderview;
540         GtkWidget *scrolledwin;
541         GtkWidget *ctree;
542
543         debug_print("Creating folder view...\n");
544         folderview = g_new0(FolderView, 1);
545
546         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
547         gtk_scrolled_window_set_policy
548                 (GTK_SCROLLED_WINDOW(scrolledwin),
549                  GTK_POLICY_AUTOMATIC,
550                  prefs_common.folderview_vscrollbar_policy);
551         gtk_widget_set_size_request(scrolledwin,
552                              prefs_common.folderview_width,
553                              prefs_common.folderview_height);
554
555         folderview->scrolledwin  = scrolledwin;
556         ctree = folderview_ctree_create(folderview);
557         
558         /* create popup factories */
559         folderview->popups = g_hash_table_new(g_str_hash, g_str_equal);
560         g_hash_table_foreach(folderview_popups, create_ifactories, folderview);
561
562         folderview->ctree        = ctree;
563
564         folderview->folder_update_callback_id =
565                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST, folderview_update_folder, (gpointer) folderview);
566         folderview->folder_item_update_callback_id =
567                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST, folderview_update_item_claws, (gpointer) folderview);
568
569         gtk_widget_show_all(scrolledwin);
570         
571         folderview->target_list = gtk_target_list_new(folderview_drag_types, 2);
572         folderview_list = g_list_append(folderview_list, folderview);
573
574         return folderview;
575 }
576
577 void folderview_init(FolderView *folderview)
578 {
579         GtkWidget *ctree = folderview->ctree;
580         GdkColor gdk_color;
581
582         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
583         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
584         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
585         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
586         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
587         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
588         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
589         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
590         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
591         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
592         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
593         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
594         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
595         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
596         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
597         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
598         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
599         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
600         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
601         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
602         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE, &draftsxpm, &draftsxpmmask);
603         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN, &draftsopenxpm, &draftsopenxpmmask);
604         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_NOSELECT, &noselectxpm, &noselectxpmmask);
605
606         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_MARK, &m_inboxxpm, &m_inboxxpmmask);
607         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM_MARK, &m_inboxhrmxpm, &m_inboxhrmxpmmask);
608         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_MARK, &m_inboxopenxpm, &m_inboxopenxpmmask);
609         stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM_MARK, &m_inboxopenhrmxpm, &m_inboxopenhrmxpmmask);
610         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_MARK, &m_outboxxpm, &m_outboxxpmmask);
611         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM_MARK, &m_outboxhrmxpm, &m_outboxhrmxpmmask);
612         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_MARK, &m_outboxopenxpm, &m_outboxopenxpmmask);
613         stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM_MARK, &m_outboxopenhrmxpm, &m_outboxopenhrmxpmmask);
614         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_MARK, &m_folderxpm, &m_folderxpmmask);
615         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM_MARK, &m_folderhrmxpm, &m_folderhrmxpmmask);
616         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_MARK, &m_folderopenxpm, &m_folderopenxpmmask);
617         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM_MARK, &m_folderopenhrmxpm, &m_folderopenhrmxpmmask);
618         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_MARK, &m_trashopenxpm, &m_trashopenxpmmask);
619         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM_MARK, &m_trashopenhrmxpm, &m_trashopenhrmxpmmask);
620         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_MARK, &m_trashxpm, &m_trashxpmmask);
621         stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM_MARK, &m_trashhrmxpm, &m_trashhrmxpmmask);
622         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_MARK, &m_queuexpm, &m_queuexpmmask);
623         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM_MARK, &m_queuehrmxpm, &m_queuehrmxpmmask);
624         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_MARK, &m_queueopenxpm, &m_queueopenxpmmask);
625         stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM_MARK, &m_queueopenhrmxpm, &m_queueopenhrmxpmmask);
626         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_CLOSE_MARK, &m_draftsxpm, &m_draftsxpmmask);
627         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DRAFTS_OPEN_MARK, &m_draftsopenxpm, &m_draftsopenxpmmask);
628                         
629         if (!normal_style) {
630                 PangoFontDescription *font_desc;
631                 normal_style = gtk_style_copy(gtk_widget_get_style(ctree));
632                 font_desc = pango_font_description_from_string(NORMAL_FONT);
633                 if (font_desc) {
634                         if (normal_style->font_desc)
635                                 pango_font_description_free
636                                         (normal_style->font_desc);
637                         normal_style->font_desc = font_desc;
638                 }
639                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
640                 normal_color_style = gtk_style_copy(normal_style);
641                 normal_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
642
643                 gtk_widget_set_style(ctree, normal_style);
644         }
645
646         if (!bold_style) {
647                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
648                 bold_style = gtk_style_copy(gtk_widget_get_style(ctree));
649                 pango_font_description_set_weight
650                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
651                 bold_color_style = gtk_style_copy(bold_style);
652                 bold_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
653
654                 bold_tgtfold_style = gtk_style_copy(bold_style);
655                 bold_tgtfold_style->fg[GTK_STATE_NORMAL] = folderview->color_op;
656         }
657 }
658
659 void folderview_set(FolderView *folderview)
660 {
661         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
662         MainWindow *mainwin = folderview->mainwin;
663
664         if (!mainwin)
665                 return;
666
667         debug_print("Setting folder info...\n");
668         STATUSBAR_PUSH(mainwin, _("Setting folder info..."));
669
670         main_window_cursor_wait(mainwin);
671
672         folderview->selected = NULL;
673         folderview->opened = NULL;
674
675         gtk_clist_freeze(GTK_CLIST(ctree));
676         gtk_clist_clear(GTK_CLIST(ctree));
677         gtk_clist_thaw(GTK_CLIST(ctree));
678         gtk_clist_freeze(GTK_CLIST(ctree));
679
680         folderview_set_folders(folderview);
681
682         gtk_clist_thaw(GTK_CLIST(ctree));
683         main_window_cursor_normal(mainwin);
684         STATUSBAR_POP(mainwin);
685 }
686
687 void folderview_set_all(void)
688 {
689         GList *list;
690
691         for (list = folderview_list; list != NULL; list = list->next)
692                 folderview_set((FolderView *)list->data);
693 }
694
695 void folderview_select(FolderView *folderview, FolderItem *item)
696 {
697         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
698         GtkCTreeNode *node;
699         GtkCTreeNode *old_selected = folderview->selected;
700
701         if (!item) return;
702
703         node = gtk_ctree_find_by_row_data(ctree, NULL, item);
704         if (node) folderview_select_node(folderview, node);
705
706         if (old_selected != node)
707                 folder_update_op_count();
708 }
709
710 static void mark_all_read_cb(FolderView *folderview, guint action,
711                              GtkWidget *widget)
712 {
713         FolderItem *item;
714         AlertValue val;
715         
716         item = folderview_get_selected_item(folderview);
717         if (item == NULL)
718                 return;
719
720         if (prefs_common.ask_mark_all_read) {
721                 val = alertpanel_full(_("Mark all as read"),
722                         _("Do you really want to mark all mails in this "
723                           "folder as read ?"), GTK_STOCK_NO, GTK_STOCK_YES, NULL,
724                           TRUE, NULL, ALERT_QUESTION, G_ALERTALTERNATE);
725
726                 if (val == G_ALERTDEFAULT ||
727                     val == (G_ALERTDEFAULT|G_ALERTDISABLE))
728                         return;
729                 else if (val == (G_ALERTALTERNATE|G_ALERTDISABLE))
730                         prefs_common.ask_mark_all_read = FALSE;
731         }
732         
733         summary_lock(folderview->summaryview);
734         folder_item_update_freeze();
735         if (folderview->summaryview->folder_item == item)
736                 gtk_clist_freeze(GTK_CLIST(folderview->summaryview->ctree));
737         folderutils_mark_all_read(item);
738         if (folderview->summaryview->folder_item == item)
739                 gtk_clist_thaw(GTK_CLIST(folderview->summaryview->ctree));
740         folder_item_update_thaw();
741         summary_unlock(folderview->summaryview);
742 }
743
744 static void folderview_select_node(FolderView *folderview, GtkCTreeNode *node)
745 {
746         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
747
748         g_return_if_fail(node != NULL);
749
750         folderview->open_folder = TRUE;
751         gtkut_ctree_set_focus_row(ctree, node);
752         gtk_ctree_select(ctree, node);
753         if (folderview->summaryview->folder_item &&
754             folderview->summaryview->folder_item->total_msgs > 0)
755                 gtk_widget_grab_focus(folderview->summaryview->ctree);
756         else
757                 gtk_widget_grab_focus(folderview->ctree);
758
759         gtkut_ctree_expand_parent_all(ctree, node);
760 }
761
762 void folderview_unselect(FolderView *folderview)
763 {
764         if (folderview->opened && !GTK_CTREE_ROW(folderview->opened)->children)
765                 gtk_ctree_collapse
766                         (GTK_CTREE(folderview->ctree), folderview->opened);
767
768         folderview->selected = folderview->opened = NULL;
769 }
770
771 static GtkCTreeNode *folderview_find_next_unread(GtkCTree *ctree,
772                                                  GtkCTreeNode *node)
773 {
774         FolderItem *item;
775
776         if (node)
777                 node = gtkut_ctree_node_next(ctree, node);
778         else
779                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
780
781         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
782                 item = gtk_ctree_node_get_row_data(ctree, node);
783                 if (item && item->unread_msgs > 0 && item->stype != F_TRASH)
784                         return node;
785         }
786
787         return NULL;
788 }
789
790 void folderview_select_next_unread(FolderView *folderview)
791 {
792         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
793         GtkCTreeNode *node = NULL;
794
795         if ((node = folderview_find_next_unread(ctree, folderview->opened))
796             != NULL) {
797                 folderview_select_node(folderview, node);
798                 return;
799         }
800
801         if (!folderview->opened ||
802             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
803                 return;
804         /* search again from the first node */
805         if ((node = folderview_find_next_unread(ctree, NULL)) != NULL)
806                 folderview_select_node(folderview, node);
807 }
808
809 static GtkCTreeNode *folderview_find_next_new(GtkCTree *ctree,
810                                                  GtkCTreeNode *node)
811 {
812         FolderItem *item;
813
814         if (node)
815                 node = gtkut_ctree_node_next(ctree, node);
816         else
817                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
818
819         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
820                 item = gtk_ctree_node_get_row_data(ctree, node);
821                 if (item && item->new_msgs > 0 && item->stype != F_TRASH)
822                         return node;
823         }
824
825         return NULL;
826 }
827
828 void folderview_select_next_new(FolderView *folderview)
829 {
830         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
831         GtkCTreeNode *node = NULL;
832
833         if ((node = folderview_find_next_new(ctree, folderview->opened))
834             != NULL) {
835                 folderview_select_node(folderview, node);
836                 return;
837         }
838
839         if (!folderview->opened ||
840             folderview->opened == GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list))
841                 return;
842         /* search again from the first node */
843         if ((node = folderview_find_next_new(ctree, NULL)) != NULL)
844                 folderview_select_node(folderview, node);
845 }
846
847 FolderItem *folderview_get_selected_item(FolderView *folderview)
848 {
849         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
850
851         if (!folderview->selected) return NULL;
852         return gtk_ctree_node_get_row_data(ctree, folderview->selected);
853 }
854
855 void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
856 {
857         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
858         static GtkCTreeNode *prev_row = NULL;
859         FolderItem *item;
860         gint new, unread, total;
861         gchar *new_str, *unread_str, *total_str;
862         gint *col_pos = folderview->col_pos;
863
864         if (!row) return;
865
866         item = gtk_ctree_node_get_row_data(ctree, row);
867         if (!item) return;
868
869         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_NEW], &new_str);
870         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_UNREAD], &unread_str);
871         gtk_ctree_node_get_text(ctree, row, col_pos[F_COL_TOTAL], &total_str);
872         new = atoi(new_str);
873         unread = atoi(unread_str);
874         total = atoi(total_str);
875
876         prev_row = row;
877
878         folderview_update_node(folderview, row);
879 }
880
881 void folderview_append_item(FolderItem *item)
882 {
883         GList *list;
884         
885         g_return_if_fail(item != NULL);
886         g_return_if_fail(item->folder != NULL);
887         if (folder_item_parent(item)) return;
888
889         for (list = folderview_list; list != NULL; list = list->next) {
890                 FolderView *folderview = (FolderView *)list->data;
891                 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
892                 GtkCTreeNode *node, *child;
893                 gint *col_pos = folderview->col_pos;
894
895                 node = gtk_ctree_find_by_row_data(ctree, NULL, 
896                                                   folder_item_parent(item));
897                 if (node) {
898                         child = gtk_ctree_find_by_row_data(ctree, node, item);
899                         if (!child) {
900                                 gchar *text[N_FOLDER_COLS] =
901                                         {NULL, "0", "0", "0"};
902
903                                 gtk_clist_freeze(GTK_CLIST(ctree));
904
905                                 text[col_pos[F_COL_FOLDER]] = item->name;
906                                 child = gtk_sctree_insert_node
907                                         (ctree, node, NULL, text,
908                                          FOLDER_SPACING,
909                                          folderxpm, folderxpmmask,
910                                          folderopenxpm, folderopenxpmmask,
911                                          FALSE, FALSE);
912                                 gtk_ctree_node_set_row_data(ctree, child, item);
913                                 gtk_ctree_expand(ctree, node);
914                                 folderview_update_node(folderview, child);
915                                 folderview_sort_folders(folderview, node,
916                                                         item->folder);
917
918                                 gtk_clist_thaw(GTK_CLIST(ctree));
919                         }
920                 }
921         }
922 }
923
924 static void folderview_set_folders(FolderView *folderview)
925 {
926         GList *list;
927         static gboolean missing_imap_warning = TRUE;
928         list = folder_get_list();
929
930         for (; list != NULL; list = list->next) {
931 #ifndef HAVE_LIBETPAN
932                 if ((FOLDER(list->data))
933                 &&  (FOLDER(list->data))->klass
934                 &&  (FOLDER(list->data))->klass->type == F_IMAP
935                 &&  missing_imap_warning) {
936                         missing_imap_warning = FALSE;
937                         alertpanel_error(
938                                 _("You have one or more IMAP accounts "
939                                   "defined. However this version of "
940                                   "Sylpheed-Claws has been built without "
941                                   "IMAP support; your IMAP account(s) are "
942                                   "disabled.\n\n"
943                                   "You probably need to "
944                                   "install libetpan and recompile "
945                                   "Sylpheed-Claws."));
946                 }
947 #endif
948                 folderview_append_folder(folderview, FOLDER(list->data));
949         }
950 }
951
952 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
953                                       gpointer data)
954 {
955         GList *list;
956
957         for (list = folderview_list; list != NULL; list = list->next) {
958                 FolderView *folderview = (FolderView *)list->data;
959                 MainWindow *mainwin = folderview->mainwin;
960                 gchar *str;
961
962                 if (item->path)
963                         str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
964                                               item->folder->name, G_DIR_SEPARATOR,
965                                               item->path);
966                 else
967                         str = g_strdup_printf(_("Scanning folder %s ..."),
968                                               item->folder->name);
969
970                 STATUSBAR_PUSH(mainwin, str);
971                 STATUSBAR_POP(mainwin);
972                 g_free(str);
973         }
974 }
975
976 void folderview_rescan_tree(Folder *folder, gboolean rebuild)
977 {
978         GtkWidget *window;
979
980         g_return_if_fail(folder != NULL);
981
982         if (!folder->klass->scan_tree) return;
983
984         if (rebuild && 
985             alertpanel_full(_("Rebuild folder tree"), 
986                          _("Rebuilding the folder tree will remove "
987                            "local caches. Do you want to continue?"),
988                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, FALSE,
989                          NULL, ALERT_WARNING, G_ALERTDEFAULT) 
990                 != G_ALERTALTERNATE) {
991                 return;
992         }
993
994         inc_lock();
995         if (rebuild)
996                 window = label_window_create(_("Rebuilding folder tree..."));
997         else 
998                 window = label_window_create(_("Scanning folder tree..."));
999
1000         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
1001         folder_scan_tree(folder, rebuild);
1002         folder_set_ui_func(folder, NULL, NULL);
1003
1004         folderview_set_all();
1005
1006         gtk_widget_destroy(window);
1007         inc_unlock();
1008 }
1009
1010 /** folderview_check_new()
1011  *  Scan and update the folder and return the 
1012  *  count the number of new messages since last check. 
1013  *  \param folder the folder to check for new messages
1014  *  \return the number of new messages since last check
1015  */
1016 gint folderview_check_new(Folder *folder)
1017 {
1018         GList *list;
1019         FolderItem *item;
1020         FolderView *folderview;
1021         GtkCTree *ctree;
1022         GtkCTreeNode *node;
1023         gint new_msgs = 0;
1024         gint former_new_msgs = 0;
1025         gint former_new = 0, former_unread = 0, former_total;
1026
1027         for (list = folderview_list; list != NULL; list = list->next) {
1028                 folderview = (FolderView *)list->data;
1029                 ctree = GTK_CTREE(folderview->ctree);
1030
1031                 inc_lock();
1032                 main_window_lock(folderview->mainwin);
1033
1034                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1035                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1036                         item = gtk_ctree_node_get_row_data(ctree, node);
1037                         if (!item || !item->path || !item->folder) continue;
1038                         if (item->no_select) continue;
1039                         if (folder && folder != item->folder) continue;
1040                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1041                         if (!item->prefs->newmailcheck) continue;
1042
1043                         folderview_scan_tree_func(item->folder, item, NULL);
1044                         former_new    = item->new_msgs;
1045                         former_unread = item->unread_msgs;
1046                         former_total  = item->total_msgs;
1047
1048                         if (folder_item_scan(item) < 0) {
1049                                 summaryview_unlock(folderview->summaryview, item);
1050                                 if (folder && !FOLDER_IS_LOCAL(folder))
1051                                         break;
1052                         }
1053
1054                         if (former_new    != item->new_msgs ||
1055                             former_unread != item->unread_msgs ||
1056                             former_total  != item->total_msgs)
1057                                 folderview_update_node(folderview, node);
1058
1059                         new_msgs += item->new_msgs;
1060                         former_new_msgs += former_new;
1061                 }
1062
1063                 main_window_unlock(folderview->mainwin);
1064                 inc_unlock();
1065         }
1066
1067         folder_write_list();
1068         /* Number of new messages since last check is the just the difference 
1069          * between former_new_msgs and new_msgs. If new_msgs is less than
1070          * former_new_msgs, that would mean another session accessed the folder
1071          * and the result is not well defined.
1072          */
1073         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1074         return new_msgs;
1075 }
1076
1077 void folderview_check_new_all(void)
1078 {
1079         GList *list;
1080         GtkWidget *window;
1081         FolderView *folderview;
1082
1083         folderview = (FolderView *)folderview_list->data;
1084
1085         inc_lock();
1086         main_window_lock(folderview->mainwin);
1087         window = label_window_create
1088                 (_("Checking for new messages in all folders..."));
1089
1090         list = folder_get_list();
1091         for (; list != NULL; list = list->next) {
1092                 Folder *folder = list->data;
1093
1094                 folderview_check_new(folder);
1095         }
1096
1097         folder_write_list();
1098         folderview_set_all();
1099
1100         gtk_widget_destroy(window);
1101         main_window_unlock(folderview->mainwin);
1102         inc_unlock();
1103 }
1104
1105 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1106                                                  FolderItem *item,
1107                                                  gboolean in_sub)
1108 {
1109         GNode *node = NULL;
1110         
1111         if (!item || !item->folder || !item->folder->node)
1112                 return FALSE;
1113                 
1114         node = item->folder->node;
1115         
1116         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1117         node = node->children;
1118
1119         if (in_sub &&
1120             (item->new_msgs > 0 ||
1121             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1122                 return TRUE;
1123         }
1124
1125         while (node != NULL) {
1126                 if (node && node->data) {
1127                         FolderItem *next_item = (FolderItem*) node->data;
1128                         node = node->next;
1129                         if (folderview_have_new_children_sub(folderview, 
1130                                                              next_item, TRUE))
1131                                 return TRUE;
1132                 }
1133         }
1134
1135         return FALSE;
1136 }
1137
1138 static gboolean folderview_have_new_children(FolderView *folderview,
1139                                              FolderItem *item)
1140 {
1141         return folderview_have_new_children_sub(folderview, item, FALSE);
1142 }
1143
1144 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1145                                                     FolderItem *item, 
1146                                                     gboolean in_sub)
1147 {
1148         GNode *node = NULL;
1149         
1150         if (!item || !item->folder || !item->folder->node)
1151                 return FALSE;
1152         
1153         node = item->folder->node;
1154         
1155         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1156         node = node->children;
1157
1158         if (in_sub &&
1159             (item->unread_msgs > 0 ||
1160             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1161                 return TRUE;
1162         }
1163
1164         while (node != NULL) {
1165                 if (node && node->data) {
1166                         FolderItem *next_item = (FolderItem*) node->data;
1167                         node = node->next;
1168                         if (folderview_have_unread_children_sub(folderview, 
1169                                                                 next_item, 
1170                                                                 TRUE))
1171                                 return TRUE;
1172                 }
1173         }
1174
1175         return FALSE;
1176 }
1177
1178 static gboolean folderview_have_unread_children(FolderView *folderview,
1179                                                 FolderItem *item)
1180 {
1181         return folderview_have_unread_children_sub(folderview, item, FALSE);
1182 }
1183
1184 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1185                                                       FolderItem *item,
1186                                                       gboolean in_sub)
1187 {
1188         GNode *node = NULL;
1189
1190         if (!item || !item->folder || !item->folder->node)
1191                 return FALSE;
1192
1193         node = item->folder->node;
1194         
1195         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1196         node = node->children;
1197
1198         if (in_sub && item->search_match){
1199                 return TRUE;
1200         }
1201
1202         while (node != NULL) {
1203                 if (node && node->data) {
1204                         FolderItem *next_item = (FolderItem*) node->data;
1205                         node = node->next;
1206                         if (folderview_have_matching_children_sub(folderview, 
1207                                                                   next_item, 
1208                                                                   TRUE))
1209                                 return TRUE;
1210                 }
1211         }
1212
1213         return FALSE;
1214 }
1215
1216 static gboolean folderview_have_matching_children(FolderView *folderview,
1217                                                   FolderItem *item)
1218 {
1219         return folderview_have_matching_children_sub(folderview, item, FALSE);
1220 }
1221
1222 static gboolean folderview_have_marked_children_sub(FolderView *folderview,
1223                                                     FolderItem *item,
1224                                                     gboolean in_sub)
1225 {
1226         GNode *node = NULL;
1227         
1228         if (!item || !item->folder || !item->folder->node)
1229                 return FALSE;
1230                 
1231         node = item->folder->node;
1232         
1233         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1234         node = node->children;
1235
1236         if (item->marked_msgs != 0) {
1237                 return TRUE;
1238         }
1239
1240         while (node != NULL) {
1241                 if (node && node->data) {
1242                         FolderItem *next_item = (FolderItem*) node->data;
1243                         node = node->next;
1244                         if (folderview_have_marked_children_sub(folderview,
1245                                                                 next_item, TRUE))
1246                                 return TRUE;
1247                 }
1248         }
1249
1250         return FALSE;
1251 }
1252
1253 static gboolean folderview_have_marked_children(FolderView *folderview,
1254                                              FolderItem *item)
1255 {
1256         return folderview_have_marked_children_sub(folderview, item, FALSE);
1257 }
1258
1259 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1260 {
1261         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1262         GtkStyle *style = NULL;
1263         GtkStyle *color_style = NULL;
1264         FolderItem *item;
1265         GdkPixmap *xpm, *openxpm;
1266         GdkBitmap *mask, *openmask;
1267         static GdkPixmap *searchicon;
1268         static GdkBitmap *searchmask;
1269         gboolean mark = FALSE;
1270         gchar *name;
1271         gchar *str;
1272         gboolean add_unread_mark;
1273         gboolean add_sub_match_mark;
1274         gboolean use_bold, use_color;
1275         gint *col_pos = folderview->col_pos;
1276         SpecialFolderItemType stype;
1277         
1278         item = gtk_ctree_node_get_row_data(ctree, node);
1279         g_return_if_fail(item != NULL);
1280
1281         if (!GTK_CTREE_ROW(node)->expanded)
1282                 mark = folderview_have_marked_children(folderview, item);
1283         else
1284                 mark = (item->marked_msgs != 0);
1285
1286         stype = item->stype;
1287         if (stype == F_NORMAL) {
1288                 if (folder_has_parent_of_type(item, F_TRASH))
1289                         stype = F_TRASH;
1290                 else if (folder_has_parent_of_type(item, F_DRAFT))
1291                         stype = F_DRAFT;
1292                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1293                         stype = F_OUTBOX;
1294                 else if (folder_has_parent_of_type(item, F_QUEUE))
1295                         stype = F_QUEUE;
1296         }
1297         switch (stype) {
1298         case F_INBOX:
1299                 if (item->hide_read_msgs) {
1300                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1301                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1302                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1303                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1304                 } else {
1305                         xpm = mark?m_inboxxpm:inboxxpm;
1306                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1307                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1308                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1309                 }
1310                 break;
1311         case F_OUTBOX:
1312                 if (item->hide_read_msgs) {
1313                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1314                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1315                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1316                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1317                 } else {
1318                         xpm = mark?m_outboxxpm:outboxxpm;
1319                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1320                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1321                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1322                 }
1323                 break;
1324         case F_QUEUE:
1325                 if (item->hide_read_msgs) {
1326                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1327                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1328                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1329                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1330                 } else {
1331                         xpm = mark?m_queuexpm:queuexpm;
1332                         mask = mark?m_queuexpmmask:queuexpmmask;
1333                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1334                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1335                 }
1336                 break;
1337         case F_TRASH:
1338                 if (item->hide_read_msgs) {
1339                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1340                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1341                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1342                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1343                 } else {
1344                         xpm = mark?m_trashxpm:trashxpm;
1345                         mask = mark?m_trashxpmmask:trashxpmmask;
1346                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1347                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1348                 }
1349                 break;
1350         case F_DRAFT:
1351                 xpm = mark?m_draftsxpm:draftsxpm;
1352                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1353                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1354                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1355                 break;
1356         default:
1357                 if (item->hide_read_msgs) {
1358                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1359                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1360                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1361                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1362                 } else {
1363                         xpm = mark?m_folderxpm:folderxpm;
1364                         mask = mark?m_folderxpmmask:folderxpmmask;
1365                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1366                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1367                 }
1368         }
1369         
1370         if (item->no_select) {
1371                 xpm = openxpm = noselectxpm;
1372                 mask = openmask = noselectxpmmask;
1373         }
1374
1375         name = folder_item_get_name(item);
1376
1377         if (!GTK_CTREE_ROW(node)->expanded) {
1378                 add_unread_mark = folderview_have_unread_children(
1379                                         folderview, item);
1380                 add_sub_match_mark = folderview_have_matching_children(
1381                                         folderview, item);
1382         } else {
1383                 add_unread_mark = FALSE;
1384                 add_sub_match_mark = FALSE;
1385         }
1386
1387         if (item->search_match) {
1388                 if (!searchicon) {
1389                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1390                          &searchicon, &searchmask);
1391                 }
1392                 xpm = openxpm = searchicon;
1393                 mask = openmask = searchmask;
1394         }
1395
1396         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1397             prefs_common.display_folder_unread) {
1398                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1399                                       add_unread_mark ? "+" : "");
1400                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1401                                         xpm, mask, openxpm, openmask,
1402                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1403                 g_free(str);
1404         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1405                     prefs_common.display_folder_unread) 
1406                    || add_sub_match_mark) {
1407
1408                 if (item->unread_msgs > 0)
1409                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1410                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1411                                               item->unreadmarked_msgs > 0 ? "!":"");
1412                 else
1413                         str = g_strdup_printf("%s (+)", name);
1414                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1415                                         xpm, mask, openxpm, openmask,
1416                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1417                 g_free(str);
1418         } else {
1419                 str = g_strdup_printf("%s%s", name, 
1420                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1421         
1422                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1423                                         xpm, mask, openxpm, openmask,
1424                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1425                 g_free(str);
1426         }
1427         g_free(name);
1428
1429         if (!folder_item_parent(item)) {
1430                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1431                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1432                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1433         } else {
1434                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1435                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1436                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1437         }
1438
1439         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1440             folder_has_parent_of_type(item, F_DRAFT) ||
1441             folder_has_parent_of_type(item, F_TRASH)) {
1442                 use_bold = use_color = FALSE;
1443         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1444                 /* highlight queue folder if there are any messages */
1445                 use_bold = use_color = (item->total_msgs > 0);
1446         } else {
1447                 /* if unread messages exist, print with bold font */
1448                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1449                                 || add_unread_mark;
1450                 /* if new messages exist, print with colored letter */
1451                 use_color =
1452                         (item->new_msgs > 0) ||
1453                         (add_unread_mark &&
1454                          folderview_have_new_children(folderview, item));       
1455         }
1456
1457         gtk_ctree_node_set_foreground(ctree, node, NULL);
1458
1459         if (use_bold) {
1460                 GdkColor gdk_color;
1461
1462                 if (item->prefs->color > 0 && !use_color) {
1463                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1464                         color_style = gtk_style_copy(bold_style);
1465                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1466                         style = color_style;
1467                 } else if (use_color) {
1468                         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
1469                         bold_color_style = gtk_style_copy(bold_style);
1470                         bold_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1471                         style = bold_color_style;
1472                 } else
1473                         style = bold_style;
1474                 if (item->op_count > 0) {
1475                         style = bold_tgtfold_style;
1476                 }
1477         } else if (use_color) {
1478                 GdkColor gdk_color;
1479
1480                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
1481                 style = gtk_style_copy(normal_color_style);
1482                 style->fg[GTK_STATE_NORMAL] = gdk_color;
1483                 gtk_ctree_node_set_foreground(ctree, node,
1484                                               &folderview->color_new);
1485         } else if (item->op_count > 0) {
1486                 style = bold_tgtfold_style;
1487         } else if (item->prefs->color > 0) {
1488                 GdkColor gdk_color;
1489                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1490                 color_style = gtk_style_copy(normal_style);
1491                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1492                 style = color_style;
1493         } else {
1494                 style = normal_style;
1495         }
1496
1497         gtk_ctree_node_set_row_style(ctree, node, style);
1498
1499         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1500                 folderview_update_node(folderview, node);
1501 }
1502
1503 #if !CLAWS /* keep it here for syncs */
1504 void folderview_update_item(FolderItem *item, gboolean update_summary)
1505 {
1506         GList *list;
1507         FolderView *folderview;
1508         GtkCTree *ctree;
1509         GtkCTreeNode *node;
1510
1511         g_return_if_fail(item != NULL);
1512
1513         for (list = folderview_list; list != NULL; list = list->next) {
1514                 folderview = (FolderView *)list->data;
1515                 ctree = GTK_CTREE(folderview->ctree);
1516
1517                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1518                 if (node) {
1519                         folderview_update_node(folderview, node);
1520                         if (update_summary && folderview->opened == node)
1521                                 summary_show(folderview->summaryview,
1522                                              item, FALSE);
1523                 }
1524         }
1525 }
1526 #endif
1527
1528 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1529 {
1530         GList *list;
1531         FolderView *folderview;
1532         GtkCTree *ctree;
1533         GtkCTreeNode *node;
1534
1535         g_return_if_fail(item != NULL);
1536
1537         for (list = folderview_list; list != NULL; list = list->next) {
1538                 folderview = (FolderView *)list->data;
1539                 ctree = GTK_CTREE(folderview->ctree);
1540
1541                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1542                 if (node) {
1543                         item->search_match = matches;
1544                         folderview_update_node(folderview, node);
1545                 }
1546         }
1547 }
1548
1549 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1550 {
1551         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1552         FolderView *folderview = (FolderView *)data;
1553         GtkCTree *ctree;
1554         GtkCTreeNode *node;
1555         g_return_val_if_fail(update_info != NULL, TRUE);
1556         g_return_val_if_fail(update_info->item != NULL, TRUE);
1557         g_return_val_if_fail(folderview != NULL, FALSE);
1558
1559         ctree = GTK_CTREE(folderview->ctree);
1560
1561         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1562         if (node) {
1563                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1564                         folderview_update_node(folderview, node);
1565                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1566                         summary_show(folderview->summaryview, update_info->item);
1567         }
1568         
1569         return FALSE;
1570 }
1571
1572 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1573                                                 gpointer data)
1574 {
1575         /* CLAWS: share this joy with other hook functions ... */
1576         folder_item_update((FolderItem *)key, 
1577                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1578 }
1579
1580 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1581 {
1582         GList *list;
1583         FolderItemUpdateFlags flags;
1584         
1585         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1586                 : 0;
1587         for (list = folderview_list; list != NULL; list = list->next)
1588                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1589                                      GINT_TO_POINTER(flags));
1590 }
1591
1592 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1593                                       GNode *gnode, GtkCTreeNode *cnode,
1594                                       gpointer data)
1595 {
1596         FolderView *folderview = (FolderView *)data;
1597         FolderItem *item = FOLDER_ITEM(gnode->data);
1598
1599         g_return_val_if_fail(item != NULL, FALSE);
1600
1601         gtk_ctree_node_set_row_data(ctree, cnode, item);
1602         folderview_update_node(folderview, cnode);
1603
1604         return TRUE;
1605 }
1606
1607 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1608                                    gpointer data)
1609 {
1610         FolderView *folderview = (FolderView *)data;
1611         FolderItem *item;
1612
1613         if (GTK_CTREE_ROW(node)->children) {
1614                 item = gtk_ctree_node_get_row_data(ctree, node);
1615                 g_return_if_fail(item != NULL);
1616
1617                 if (!item->collapsed)
1618                         gtk_ctree_expand(ctree, node);
1619                 else
1620                         folderview_update_node(folderview, node);
1621         }
1622 }
1623
1624 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1625                                GtkCTreeNode *root, GtkCTreeNode **prev)
1626 {
1627         if (item) {
1628                 GtkCTreeNode *node, *parent, *sibling;
1629
1630                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1631                 if (!node)
1632                         g_warning("%s not found.\n", item->path);
1633                 else {
1634                         parent = GTK_CTREE_ROW(node)->parent;
1635                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1636                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1637                         else
1638                                 sibling = GTK_CTREE_ROW(parent)->children;
1639                         while (sibling) {
1640                                 FolderItem *tmp;
1641
1642                                 tmp = gtk_ctree_node_get_row_data
1643                                         (ctree, sibling);
1644                                 if (tmp->stype != F_NORMAL)
1645                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1646                                 else
1647                                         break;
1648                         }
1649                         if (node != sibling)
1650                                 gtk_ctree_move(ctree, node, parent, sibling);
1651                 }
1652
1653                 *prev = node;
1654         }
1655 }
1656
1657 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1658                                     Folder *folder)
1659 {
1660         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1661         GtkCTreeNode *prev = NULL;
1662
1663         gtk_sctree_sort_recursive(ctree, root);
1664
1665         if (root && GTK_CTREE_ROW(root)->parent) return;
1666
1667         set_special_folder(ctree, folder->inbox, root, &prev);
1668         set_special_folder(ctree, folder->outbox, root, &prev);
1669         set_special_folder(ctree, folder->draft, root, &prev);
1670         set_special_folder(ctree, folder->queue, root, &prev);
1671         set_special_folder(ctree, folder->trash, root, &prev);
1672 }
1673
1674 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1675 {
1676         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1677         GtkCTreeNode *root;
1678
1679         g_return_if_fail(folder != NULL);
1680
1681         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1682                                       folderview_gnode_func, folderview);
1683         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1684                                 folderview);
1685         folderview_sort_folders(folderview, root, folder);
1686 }
1687
1688 /* callback functions */
1689
1690 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1691                                           FolderView *folderview)
1692 {
1693         GtkCList *clist = GTK_CLIST(ctree);
1694         gint prev_row = -1, row = -1, column = -1;
1695         FolderItem *item;
1696         Folder *folder;
1697         FolderViewPopup *fpopup;
1698         GtkItemFactory *fpopup_factory;
1699         GtkWidget *popup;
1700         FolderItem *special_trash = NULL;
1701         PrefsAccount *ac;
1702
1703         if (!event) return FALSE;
1704
1705         if (event->button == 1 || event->button == 2) {
1706                 folderview->open_folder = TRUE;
1707
1708                 if (event->type == GDK_2BUTTON_PRESS) {
1709                         if (clist->selection) {
1710                                 GtkCTreeNode *node;
1711
1712                                 node = GTK_CTREE_NODE(clist->selection->data);
1713                                 if (node)
1714                                         gtk_ctree_toggle_expansion(
1715                                                 GTK_CTREE(ctree),
1716                                                 node);
1717                         }
1718                 }
1719                 return FALSE;
1720         }
1721
1722         if (event->button == 2 || event->button == 3) {
1723                 /* right clicked */
1724                 if (clist->selection) {
1725                         GtkCTreeNode *node;
1726
1727                         node = GTK_CTREE_NODE(clist->selection->data);
1728                         if (node)
1729                                 prev_row = gtkut_ctree_get_nth_from_node
1730                                         (GTK_CTREE(ctree), node);
1731                 }
1732
1733                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1734                                                   &row, &column))
1735                         return FALSE;
1736                 if (prev_row != row) {
1737                         gtk_clist_unselect_all(clist);
1738                         if (event->button == 2)
1739                                 folderview_select_node
1740                                         (folderview,
1741                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1742                                                             row));
1743                         else
1744                                 gtk_clist_select_row(clist, row, column);
1745                 }
1746         }
1747
1748         if (event->button != 3) return FALSE;
1749
1750         item = gtk_clist_get_row_data(clist, row);
1751         g_return_val_if_fail(item != NULL, FALSE);
1752         g_return_val_if_fail(item->folder != NULL, FALSE);
1753         folder = item->folder;
1754
1755         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1756         if (fpopup != NULL)
1757                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1758         else {
1759                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1760                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1761         }
1762
1763         if (fpopup->set_sensitivity != NULL)
1764                 fpopup->set_sensitivity(fpopup_factory, item);
1765
1766         if (NULL != (ac = account_find_from_item(item)))
1767                 special_trash = account_get_special_folder(ac, F_TRASH);
1768
1769         if ((item == folder->trash || item == special_trash
1770              || folder_has_parent_of_type(item, F_TRASH)) &&
1771             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1772                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1773                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1774         } else if (item != folder->trash && (special_trash == NULL || item != special_trash)
1775                 && !folder_has_parent_of_type(item, F_TRASH)) {
1776                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1777                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1778         }
1779         
1780 #define SET_SENS(name, sens) \
1781         menu_set_sensitive(fpopup_factory, name, sens)
1782
1783         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1784         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1785                  folderview->selected == folderview->opened);
1786         SET_SENS("/Properties...", item->node->parent != NULL);
1787         SET_SENS("/Processing...", item->node->parent != NULL);
1788         if (item == folder->trash || item == special_trash
1789             || folder_has_parent_of_type(item, F_TRASH)) {
1790                 GSList *msglist = folder_item_get_msg_list(item);
1791                 SET_SENS("/Empty trash...", msglist != NULL);
1792                 procmsg_msg_list_free(msglist);
1793         }
1794 #undef SET_SENS
1795
1796         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1797         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1798                        event->button, event->time);
1799
1800         return FALSE;
1801 }
1802
1803 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1804                                            FolderView *folderview)
1805 {
1806         if (!event) return FALSE;
1807
1808         if (event->button == 1 && folderview->open_folder == FALSE &&
1809             folderview->opened != NULL) {
1810                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1811                                           folderview->opened);
1812                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1813         }
1814
1815         return FALSE;
1816 }
1817
1818 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1819                                        FolderView *folderview)
1820 {
1821         if (!event) return FALSE;
1822
1823         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1824                 return FALSE;
1825
1826         switch (event->keyval) {
1827         case GDK_Return:
1828                 if (folderview->selected) {
1829                         folderview_select_node(folderview,
1830                                                folderview->selected);
1831                 }
1832                 break;
1833         case GDK_space:
1834                 if (folderview->selected) {
1835                         if (folderview->opened == folderview->selected &&
1836                             (!folderview->summaryview->folder_item ||
1837                              folderview->summaryview->folder_item->total_msgs == 0))
1838                                 folderview_select_next_unread(folderview);
1839                         else
1840                                 folderview_select_node(folderview,
1841                                                        folderview->selected);
1842                 }
1843                 break;
1844         default:
1845                 break;
1846         }
1847
1848         return FALSE;
1849 }
1850
1851 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1852                                 gint column, FolderView *folderview)
1853 {
1854         static gboolean can_select = TRUE;      /* exclusive lock */
1855         gboolean opened;
1856         FolderItem *item;
1857         gchar *buf;
1858
1859         folderview->selected = row;
1860
1861         if (folderview->opened == row) {
1862                 folderview->open_folder = FALSE;
1863                 return;
1864         }
1865         
1866         if (!can_select || summary_is_locked(folderview->summaryview)) {
1867                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1868                 gtk_ctree_select(ctree, folderview->opened);
1869                 return;
1870         }
1871
1872         if (!folderview->open_folder) return;
1873
1874         item = gtk_ctree_node_get_row_data(ctree, row);
1875         if (!item || item->no_select) return;
1876
1877         can_select = FALSE;
1878
1879         /* Save cache for old folder */
1880         /* We don't want to lose all caches if sylpheed crashed */
1881         if (folderview->opened) {
1882                 FolderItem *olditem;
1883                 
1884                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1885                 if (olditem) {
1886                         /* will be null if we just moved the previously opened folder */
1887                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1888                         summary_show(folderview->summaryview, NULL);
1889                         folder_item_close(olditem);
1890                 }
1891         }
1892
1893         /* CLAWS: set compose button type: news folder items 
1894          * always have a news folder as parent */
1895         if (item->folder) 
1896                 toolbar_set_compose_button
1897                         (folderview->mainwin->toolbar,
1898                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1899                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1900
1901         if (item->path)
1902                 debug_print("Folder %s is selected\n", item->path);
1903
1904         if (!GTK_CTREE_ROW(row)->children)
1905                 gtk_ctree_expand(ctree, row);
1906         if (folderview->opened &&
1907             !GTK_CTREE_ROW(folderview->opened)->children)
1908                 gtk_ctree_collapse(ctree, folderview->opened);
1909
1910         /* ungrab the mouse event */
1911         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1912                 gtk_grab_remove(GTK_WIDGET(ctree));
1913                 if (gdk_pointer_is_grabbed())
1914                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1915         }
1916
1917         /* Open Folder */
1918         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1919                                         item->path : "(null)");
1920         debug_print("%s\n", buf);
1921         STATUSBAR_PUSH(folderview->mainwin, buf);
1922         g_free(buf);
1923
1924         main_window_cursor_wait(folderview->mainwin);
1925
1926         if (folder_item_open(item) != 0) {
1927                 main_window_cursor_normal(folderview->mainwin);
1928                 STATUSBAR_POP(folderview->mainwin);
1929
1930                 alertpanel_error(_("Folder could not be opened."));
1931
1932                 folderview->open_folder = FALSE;
1933                 can_select = TRUE;
1934
1935                 return;
1936         }
1937         
1938         main_window_cursor_normal(folderview->mainwin);
1939
1940         /* Show messages */
1941         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1942         opened = summary_show(folderview->summaryview, item);
1943         
1944         folder_clean_cache_memory();
1945
1946         if (!opened) {
1947                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1948                 gtk_ctree_select(ctree, folderview->opened);
1949         } else {
1950                 folderview->opened = row;
1951                 if (gtk_ctree_node_is_visible(ctree, row)
1952                     != GTK_VISIBILITY_FULL)
1953                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1954         }
1955
1956         STATUSBAR_POP(folderview->mainwin);
1957
1958         folderview->open_folder = FALSE;
1959         can_select = TRUE;
1960 }
1961
1962 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1963                                      FolderView *folderview)
1964 {
1965         FolderItem *item;
1966
1967         item = gtk_ctree_node_get_row_data(ctree, node);
1968         g_return_if_fail(item != NULL);
1969         item->collapsed = FALSE;
1970         folderview_update_node(folderview, node);
1971 }
1972
1973 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
1974                                       FolderView *folderview)
1975 {
1976         FolderItem *item;
1977
1978         item = gtk_ctree_node_get_row_data(ctree, node);
1979         g_return_if_fail(item != NULL);
1980         item->collapsed = TRUE;
1981         folderview_update_node(folderview, node);
1982 }
1983
1984 static void folderview_popup_close(GtkMenuShell *menu_shell,
1985                                    FolderView *folderview)
1986 {
1987         if (!folderview->opened) return;
1988
1989         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
1990 }
1991
1992 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
1993                                    FolderView *folderview)
1994 {
1995         FolderColumnType type = folderview->col_state[column].type;
1996
1997         prefs_common.folder_col_size[type] = width;
1998 }
1999
2000 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
2001 {
2002         GNode *srcnode;
2003
2004         folderview_create_folder_node(folderview, item);
2005
2006         if (!item || !item->folder || !item->folder->node)
2007                 return;
2008
2009         srcnode = item->folder->node;   
2010         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2011         srcnode = srcnode->children;
2012         while (srcnode != NULL) {
2013                 if (srcnode && srcnode->data) {
2014                         FolderItem *next_item = (FolderItem*) srcnode->data;
2015                         folderview_create_folder_node_recursive(folderview, next_item);
2016                 }
2017                 srcnode = srcnode->next;
2018         }
2019 }
2020
2021 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2022 {
2023         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2024         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2025         GtkCTreeNode *node, *parent_node;
2026         gint *col_pos = folderview->col_pos;
2027         FolderItemUpdateData hookdata;
2028
2029         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2030         if (parent_node == NULL)
2031                 return;
2032
2033         gtk_clist_freeze(GTK_CLIST(ctree));
2034
2035         text[col_pos[F_COL_FOLDER]] = item->name;
2036         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2037                                      FOLDER_SPACING,
2038                                      folderxpm, folderxpmmask,
2039                                      folderopenxpm, folderopenxpmmask,
2040                                      FALSE, FALSE);
2041         gtk_ctree_expand(ctree, parent_node);
2042         gtk_ctree_node_set_row_data(ctree, node, item);
2043         if (normal_style)
2044                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
2045         folderview_sort_folders(folderview, parent_node, item->folder);
2046
2047         hookdata.item = item;
2048         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2049         hookdata.msg = NULL;
2050         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2051
2052         gtk_clist_thaw(GTK_CLIST(ctree));
2053 }
2054
2055 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
2056                                       GtkWidget *widget)
2057 {
2058         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2059         FolderItem *item;
2060         GSList *mlist = NULL;
2061         GSList *cur = NULL;
2062         FolderItem *special_trash = NULL;
2063         PrefsAccount *ac;
2064
2065         if (!folderview->selected) return;
2066         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2067         g_return_if_fail(item != NULL);
2068         g_return_if_fail(item->folder != NULL);
2069
2070         if (NULL != (ac = account_find_from_item(item)))
2071                 special_trash = account_get_special_folder(ac, F_TRASH);
2072
2073         if (item != item->folder->trash && item != special_trash
2074         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2075         
2076         if (prefs_common.ask_on_clean) {
2077                 if (alertpanel(_("Empty trash"),
2078                                _("Delete all messages in trash?"),
2079                                GTK_STOCK_CANCEL, _("+_Empty trash"), NULL) != G_ALERTALTERNATE)
2080                         return;
2081         }
2082         
2083         mlist = folder_item_get_msg_list(item);
2084         
2085         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2086                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2087                 if (MSG_IS_LOCKED(msginfo->flags))
2088                         continue;
2089                 /* is it partially received? (partial_recv isn't cached) */
2090                 if (msginfo->total_size != 0 && 
2091                     msginfo->size != (off_t)msginfo->total_size)
2092                         partial_mark_for_delete(msginfo);
2093         }
2094         procmsg_msg_list_free(mlist);
2095
2096         folder_item_remove_all_msg(item);
2097 }
2098
2099 static void folderview_search_cb(FolderView *folderview, guint action,
2100                                  GtkWidget *widget)
2101 {
2102         summary_search(folderview->summaryview);
2103 }
2104
2105 static void folderview_property_cb(FolderView *folderview, guint action,
2106                                    GtkWidget *widget)
2107 {
2108         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2109         FolderItem *item;
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
2117         if (folder_item_parent(item) == NULL)
2118                 return;
2119
2120         prefs_folder_item_open(item);
2121 }
2122
2123 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2124 {
2125         GSList *list = NULL;
2126         GSList *done = NULL;
2127         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2128         
2129         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2130                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2131                 &&  list->data != node) {
2132                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2133                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2134                 }
2135         }
2136         for (list = done; list != NULL; list = g_slist_next(list)) {
2137                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2138                                                                  list->data);
2139         }
2140         g_slist_free(done);
2141 }
2142
2143 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2144                             FolderItem *to_folder)
2145 {
2146         FolderItem *from_parent = NULL;
2147         FolderItem *new_folder = NULL;
2148         GtkCTreeNode *src_node = NULL;
2149         gchar *buf;
2150         gint status;
2151
2152         g_return_if_fail(folderview != NULL);
2153         g_return_if_fail(from_folder != NULL);
2154         g_return_if_fail(to_folder != NULL);
2155
2156         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2157         from_parent = folder_item_parent(from_folder);
2158         
2159         if (prefs_common.warn_dnd) {
2160                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2161                                         "sub-folder of '%s' ?"), from_folder->name,
2162                                         to_folder->name);
2163                 status = alertpanel_full(_("Move folder"), buf,
2164                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, TRUE,
2165                                          NULL, ALERT_QUESTION, G_ALERTDEFAULT);
2166                 g_free(buf);
2167
2168                 if (status != G_ALERTALTERNATE
2169                  && status != (G_ALERTALTERNATE | G_ALERTDISABLE))
2170                         return;
2171                 if (status & G_ALERTDISABLE)
2172                         prefs_common.warn_dnd = FALSE;
2173         }
2174
2175         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2176         STATUSBAR_PUSH(folderview->mainwin, buf);
2177         g_free(buf);
2178         summary_clear_all(folderview->summaryview);
2179         folderview->opened = NULL;
2180         folderview->selected = NULL;
2181         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2182         inc_lock();
2183         main_window_cursor_wait(folderview->mainwin);
2184         statusbar_verbosity_set(TRUE);
2185         folder_item_update_freeze();
2186         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2187                 statusbar_verbosity_set(FALSE);
2188                 main_window_cursor_normal(folderview->mainwin);
2189                 STATUSBAR_POP(folderview->mainwin);
2190                 folder_item_update_thaw();
2191                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2192
2193                 folderview_sort_folders(folderview, 
2194                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2195                                 NULL, to_folder), new_folder->folder);
2196                 folderview_select(folderview, new_folder);
2197         } else {
2198                 statusbar_verbosity_set(FALSE);         
2199                 main_window_cursor_normal(folderview->mainwin);
2200                 STATUSBAR_POP(folderview->mainwin);
2201                 folder_item_update_thaw();
2202                 switch (status) {
2203                 case F_MOVE_FAILED_DEST_IS_PARENT:
2204                         alertpanel_error(_("Source and destination are the same."));
2205                         break;
2206                 case F_MOVE_FAILED_DEST_IS_CHILD:
2207                         alertpanel_error(_("Can't move a folder to one of its children."));
2208                         break;
2209                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2210                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2211                         break;
2212                 default:
2213                         alertpanel_error(_("Move failed!"));
2214                         break;
2215                 }
2216         }       
2217         inc_unlock();           
2218         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2219 }
2220
2221 static gint folderview_clist_compare(GtkCList *clist,
2222                                      gconstpointer ptr1, gconstpointer ptr2)
2223 {
2224         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2225         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2226
2227         if (!item1->name)
2228                 return (item2->name != NULL);
2229         if (!item2->name)
2230                 return -1;
2231
2232         return g_utf8_collate(item1->name, item2->name);
2233 }
2234
2235 static void folderview_processing_cb(FolderView *folderview, guint action,
2236                                      GtkWidget *widget)
2237 {
2238         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2239         FolderItem *item;
2240         gchar *id, *title;
2241
2242         if (!folderview->selected) return;
2243
2244         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2245         g_return_if_fail(item != NULL);
2246         g_return_if_fail(item->folder != NULL);
2247
2248         id = folder_item_get_identifier(item);
2249         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2250         g_free (id);
2251
2252         prefs_filtering_open(&item->prefs->processing, title, NULL, NULL);
2253         g_free (title);
2254 }
2255
2256 void folderview_set_target_folder_color(gint color_op) 
2257 {
2258         gint firstone = 1;
2259         GList *list;
2260         FolderView *folderview;
2261
2262         for (list = folderview_list; list != NULL; list = list->next) {
2263                 folderview = (FolderView *)list->data;
2264                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2265                 if (firstone) {
2266                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2267                                 folderview->color_op;
2268                         firstone = 0;
2269                 }
2270         }
2271 }
2272
2273 static gchar *last_font = NULL;
2274 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2275 {
2276         /* force reinit */
2277         g_free(last_font);
2278         last_font = NULL;
2279         
2280 }
2281
2282 void folderview_reflect_prefs(void)
2283 {
2284         gboolean update_font = TRUE;
2285         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2286         FolderItem *item = folderview_get_selected_item(folderview);
2287         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2288                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2289         gint height = pos->value;
2290
2291         if (last_font && !strcmp(last_font, NORMAL_FONT))
2292                 update_font = FALSE;
2293
2294         g_free(last_font);
2295         last_font = g_strdup(NORMAL_FONT);
2296
2297         if (update_font) {              
2298                 normal_style = normal_color_style = bold_style = 
2299                         bold_color_style = bold_tgtfold_style = NULL;
2300
2301                 folderview_init(folderview);
2302         }
2303         gtk_clist_freeze(GTK_CLIST(folderview->ctree));
2304         folderview_column_set_titles(folderview);
2305         folderview_set_all();
2306
2307         g_signal_handlers_block_by_func
2308                 (G_OBJECT(folderview->ctree),
2309                  G_CALLBACK(folderview_selected), folderview);
2310
2311         if (item) {
2312                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(
2313                         GTK_CTREE(folderview->ctree), NULL, item);
2314
2315                 folderview_select(folderview, item);
2316                 folderview->selected = node;
2317         }
2318
2319         g_signal_handlers_unblock_by_func
2320                 (G_OBJECT(folderview->ctree),
2321                  G_CALLBACK(folderview_selected), folderview);
2322
2323         pos = gtk_scrolled_window_get_vadjustment(
2324                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2325         gtk_adjustment_set_value(pos, height);
2326         gtk_clist_thaw(GTK_CLIST(folderview->ctree));
2327 }
2328
2329 static void drag_state_stop(FolderView *folderview)
2330 {
2331         if (folderview->drag_timer)
2332                 gtk_timeout_remove(folderview->drag_timer);
2333         folderview->drag_timer = 0;
2334         folderview->drag_node = NULL;
2335 }
2336
2337 static gint folderview_defer_expand(FolderView *folderview)
2338 {
2339         if (folderview->drag_node) {
2340                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2341                 if (folderview->drag_item->collapsed) {
2342                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2343                         folderview->nodes_to_recollapse = g_slist_append
2344                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2345                 }
2346         }
2347         folderview->drag_item  = NULL;
2348         folderview->drag_timer = 0;
2349         return FALSE;
2350 }
2351
2352 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2353 {
2354         /* the idea is that we call drag_state_start() whenever we want expansion to
2355          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2356          * we need to call drag_state_stop() */
2357         drag_state_stop(folderview);
2358         /* request expansion */
2359         if (0 != (folderview->drag_timer = gtk_timeout_add
2360                         (prefs_common.hover_timeout, 
2361                          (GtkFunction)folderview_defer_expand,
2362                          folderview))) {
2363                 folderview->drag_node = node;
2364                 folderview->drag_item = item;
2365         }                        
2366 }
2367
2368 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2369                                   FolderView       *folderview)
2370 {
2371         GdkDragContext *context;
2372
2373         g_return_if_fail(folderview != NULL);
2374         if (folderview->selected == NULL) return;
2375         if (folderview->nodes_to_recollapse) 
2376                 g_slist_free(folderview->nodes_to_recollapse);
2377         folderview->nodes_to_recollapse = NULL;
2378         context = gtk_drag_begin(widget, folderview->target_list,
2379                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2380         gtk_drag_set_icon_default(context);
2381 }
2382
2383 static void folderview_drag_data_get(GtkWidget        *widget,
2384                                      GdkDragContext   *drag_context,
2385                                      GtkSelectionData *selection_data,
2386                                      guint             info,
2387                                      guint             time,
2388                                      FolderView       *folderview)
2389 {
2390         FolderItem *item;
2391         GList *cur;
2392         gchar *source = NULL;
2393         if (info == TARGET_DUMMY) {
2394                 for (cur = GTK_CLIST(folderview->ctree)->selection;
2395                      cur != NULL; cur = cur->next) {
2396                         item = gtk_ctree_node_get_row_data
2397                                 (GTK_CTREE(folderview->ctree), 
2398                                  GTK_CTREE_NODE(cur->data));
2399                         if (item) {
2400                                 source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2401                                 gtk_selection_data_set(selection_data,
2402                                                        selection_data->target, 8,
2403                                                        source, strlen(source));
2404                                 break;
2405                         } else
2406                                 return;
2407                 }
2408         } else {
2409                 g_warning("unknown info %d\n", info);
2410         }
2411 }
2412
2413 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2414 {
2415         FolderUpdateData *hookdata;
2416         FolderView *folderview;
2417         GtkWidget *ctree;
2418
2419         hookdata = source;
2420         folderview = (FolderView *) userdata;   
2421         g_return_val_if_fail(hookdata != NULL, FALSE);
2422         g_return_val_if_fail(folderview != NULL, FALSE);
2423
2424         ctree = folderview->ctree;
2425         g_return_val_if_fail(ctree != NULL, FALSE);
2426
2427         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2428                 folderview_create_folder_node(folderview, hookdata->item);
2429         else if (hookdata->update_flags & FOLDER_RENAME_FOLDERITEM) {
2430                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree),
2431                                 NULL, folder_item_parent(hookdata->item));
2432                 folderview_sort_folders(folderview, node, hookdata->folder);
2433         } else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2434                 GtkCTreeNode *node;
2435
2436                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2437                 if (node != NULL)
2438                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2439         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2440                 folderview_set(folderview);
2441
2442         return FALSE;
2443 }
2444
2445 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2446                                           GdkDragContext *context,
2447                                           gint            x,
2448                                           gint            y,
2449                                           guint           time,
2450                                           FolderView     *folderview)
2451 {
2452         gint row, column;
2453         FolderItem *item = NULL, *src_item = NULL;
2454         GtkCTreeNode *node = NULL;
2455         gboolean acceptable = FALSE;
2456         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2457                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2458         int height = (int)pos->page_size;
2459         int total_height = (int)pos->upper;
2460         int vpos = (int) pos->value;
2461
2462         if (gtk_clist_get_selection_info
2463                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2464                 GtkWidget *srcwidget;
2465
2466                 if (y > height - 24 && height + vpos < total_height)
2467                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2468
2469                 if (y < 48 && y > 0)
2470                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2471
2472                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2473                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2474                 src_item = folderview->summaryview->folder_item;
2475
2476                 srcwidget = gtk_drag_get_source_widget(context);
2477                 if (srcwidget == folderview->summaryview->ctree) {
2478                         /* comes from summaryview */
2479                         /* we are copying messages, so only accept folder items that are not
2480                            the source item, are no root items and can copy messages */
2481                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2482                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2483                                 acceptable = TRUE;
2484                 } else if (srcwidget == folderview->ctree) {
2485                         /* comes from folderview */
2486                         /* we are moving folder items, only accept folders that are not
2487                            the source items and can copy messages and create folder items */
2488                         if (item && item->folder && src_item && src_item != item &&
2489                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2490                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2491                                 acceptable = TRUE;
2492                 } else {
2493                         /* comes from another app */
2494                         /* we are adding messages, so only accept folder items that are 
2495                            no root items and can copy messages */
2496                         if (item && item->folder && folder_item_parent(item) != NULL
2497                             && FOLDER_CLASS(item->folder)->add_msg != NULL)
2498                                 acceptable = TRUE;
2499                 }
2500         }
2501
2502         if (acceptable || (src_item && src_item == item))
2503                 drag_state_start(folderview, node, item);
2504         
2505         if (acceptable) {
2506                 g_signal_handlers_block_by_func
2507                         (G_OBJECT(widget),
2508                          G_CALLBACK(folderview_selected), folderview);
2509                 gtk_ctree_select(GTK_CTREE(widget), node);
2510                 g_signal_handlers_unblock_by_func
2511                         (G_OBJECT(widget),
2512                          G_CALLBACK(folderview_selected), folderview);
2513                 gdk_drag_status(context, 
2514                                         (context->actions == GDK_ACTION_COPY ?
2515                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2516         } else {
2517                 if (folderview->opened)
2518                         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2519                 gdk_drag_status(context, 0, time);
2520         }
2521
2522         return acceptable;
2523 }
2524
2525 static void folderview_drag_leave_cb(GtkWidget      *widget,
2526                                      GdkDragContext *context,
2527                                      guint           time,
2528                                      FolderView     *folderview)
2529 {
2530         drag_state_stop(folderview);
2531         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2532 }
2533
2534 static void free_info (gpointer stuff, gpointer data)
2535 {
2536         g_free(stuff);
2537 }
2538
2539 void folderview_finish_dnd(const gchar *data, GdkDragContext *drag_context,
2540                            guint time, FolderItem *item)
2541 {
2542         GList *list, *tmp;
2543         GSList *msglist = NULL;
2544         list = uri_list_extract_filenames(data);
2545         if (!(item && item->folder && folder_item_parent(item) != NULL
2546                     && FOLDER_CLASS(item->folder)->add_msg != NULL))
2547         {
2548                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2549                 return;
2550         }       
2551         if (!list) {
2552                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2553                 return;
2554         }
2555         for (tmp = list; tmp != NULL; tmp = tmp->next) {
2556                 MsgFileInfo *info = NULL;
2557
2558                 if (file_is_email((gchar *)tmp->data)) {
2559                         info = g_new0(MsgFileInfo, 1);
2560                         info->msginfo = NULL;
2561                         info->file = (gchar *)tmp->data;
2562                         msglist = g_slist_prepend(msglist, info);
2563                 }
2564         }
2565         if (msglist) {
2566                 msglist = g_slist_reverse(msglist);
2567                 folder_item_add_msgs(item, msglist, FALSE);
2568                 g_slist_foreach(msglist, free_info, NULL);
2569                 g_slist_free(msglist);
2570                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2571         } else {
2572                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2573         }
2574         list_free_strings(list);
2575         g_list_free(list);
2576 }
2577
2578 static void folderview_drag_received_cb(GtkWidget        *widget,
2579                                         GdkDragContext   *drag_context,
2580                                         gint              x,
2581                                         gint              y,
2582                                         GtkSelectionData *data,
2583                                         guint             info,
2584                                         guint             time,
2585                                         FolderView       *folderview)
2586 {
2587         gint row, column;
2588         FolderItem *item = NULL, *src_item;
2589         GtkCTreeNode *node;
2590
2591         if (info == TARGET_DUMMY) {
2592                 drag_state_stop(folderview);
2593                 if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2594                         /* comes from summaryview */
2595                         if (gtk_clist_get_selection_info
2596                                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2597                                 return;
2598
2599                         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2600                         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2601                         src_item = folderview->summaryview->folder_item;
2602
2603                         /* re-check (due to acceptable possibly set for folder moves */
2604                         if (!(item && item->folder && item->path && !item->no_select && 
2605                               src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2606                                 return;
2607                         }
2608                         if (item && src_item) {
2609                                 switch (drag_context->action) {
2610                                 case GDK_ACTION_COPY:
2611                                         summary_copy_selected_to(folderview->summaryview, item);
2612                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2613                                         break;
2614                                 case GDK_ACTION_MOVE:
2615                                 case GDK_ACTION_DEFAULT:
2616                                 default:
2617                                         if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2618                                                 summary_copy_selected_to(folderview->summaryview, item);
2619                                         else
2620                                                 summary_move_selected_to(folderview->summaryview, item);
2621                                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2622                                 }
2623                         } else
2624                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2625                 } else {
2626                         /* comes from folderview */
2627                         char *source;
2628                         gboolean folder_is_normal = TRUE;
2629
2630                         source = data->data + 17;
2631                         if (gtk_clist_get_selection_info
2632                             (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2633                             || *source == 0) {
2634                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2635                                 return;
2636                         }
2637                         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2638                         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2639                         src_item = folder_find_item_from_identifier(source);
2640
2641                         folder_is_normal = 
2642                                 src_item != NULL &&
2643                                 src_item->stype == F_NORMAL &&
2644                                 !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2645                                 !folder_has_parent_of_type(src_item, F_DRAFT) &&
2646                                 !folder_has_parent_of_type(src_item, F_QUEUE) &&
2647                                 !folder_has_parent_of_type(src_item, F_TRASH);
2648                         if (!item || item->no_select || !src_item 
2649                         ||  !folder_is_normal) {
2650                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2651                                 return;
2652                         }
2653
2654                         folderview_move_folder(folderview, src_item, item);
2655                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2656                 }
2657                 folderview->nodes_to_recollapse = NULL;
2658         } else if (info == TARGET_MAIL_URI_LIST) {
2659                 if (gtk_clist_get_selection_info
2660                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2661                         return;
2662
2663                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2664                 if (!node) {
2665                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2666                         return;
2667                 }
2668                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2669                 if (!item) {
2670                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2671                         return;
2672                 }
2673                 folderview_finish_dnd(data->data, drag_context, time, item);
2674         }
2675 }
2676
2677 static void folderview_drag_end_cb(GtkWidget        *widget, 
2678                                    GdkDragContext   *drag_context,
2679                                    FolderView       *folderview)
2680 {
2681         drag_state_stop(folderview);
2682         g_slist_free(folderview->nodes_to_recollapse);
2683         folderview->nodes_to_recollapse = NULL;
2684 }
2685
2686 void folderview_register_popup(FolderViewPopup *fpopup)
2687 {
2688         GList *folderviews;
2689
2690         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2691                 FolderView *folderview = folderviews->data;
2692                 GtkItemFactory *factory;
2693
2694                 factory = create_ifactory(folderview, fpopup);
2695                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2696         }       
2697         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2698 }
2699
2700 void folderview_unregister_popup(FolderViewPopup *fpopup)
2701 {
2702         GList *folderviews;
2703
2704         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2705                 FolderView *folderview = folderviews->data;
2706
2707                 g_hash_table_remove(folderview->popups, fpopup->klass);
2708         }       
2709         g_hash_table_remove(folderview_popups, fpopup->klass);
2710 }