50ac3e3487c8c6cf20a14555c246ea9cc2e51a12
[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 #ifndef HAVE_LIBETPAN
928         static gboolean missing_imap_warning = TRUE;
929 #endif
930         list = folder_get_list();
931
932         for (; list != NULL; list = list->next) {
933 #ifndef HAVE_LIBETPAN
934                 if ((FOLDER(list->data))
935                 &&  (FOLDER(list->data))->klass
936                 &&  (FOLDER(list->data))->klass->type == F_IMAP
937                 &&  missing_imap_warning) {
938                         missing_imap_warning = FALSE;
939                         alertpanel_error(
940                                 _("You have one or more IMAP accounts "
941                                   "defined. However this version of "
942                                   "Sylpheed-Claws has been built without "
943                                   "IMAP support; your IMAP account(s) are "
944                                   "disabled.\n\n"
945                                   "You probably need to "
946                                   "install libetpan and recompile "
947                                   "Sylpheed-Claws."));
948                 }
949 #endif
950                 folderview_append_folder(folderview, FOLDER(list->data));
951         }
952 }
953
954 static gchar *get_scan_str(FolderItem *item)
955 {
956         if (item->path)
957                 return g_strdup_printf(_("Scanning folder %s%c%s ..."),
958                                       item->folder->name, G_DIR_SEPARATOR,
959                                       item->path);
960         else
961                 return g_strdup_printf(_("Scanning folder %s ..."),
962                                       item->folder->name);      
963 }
964 static void folderview_scan_tree_func(Folder *folder, FolderItem *item,
965                                       gpointer data)
966 {
967         GList *list;
968         for (list = folderview_list; list != NULL; list = list->next) {
969                 FolderView *folderview = (FolderView *)list->data;
970                 MainWindow *mainwin = folderview->mainwin;
971                 gchar *str = get_scan_str(item);
972
973                 STATUSBAR_PUSH(mainwin, str);
974                 STATUSBAR_POP(mainwin);
975                 g_free(str);
976         }
977 }
978
979 void folderview_rescan_tree(Folder *folder, gboolean rebuild)
980 {
981         GtkWidget *window;
982
983         g_return_if_fail(folder != NULL);
984
985         if (!folder->klass->scan_tree) return;
986
987         if (rebuild && 
988             alertpanel_full(_("Rebuild folder tree"), 
989                          _("Rebuilding the folder tree will remove "
990                            "local caches. Do you want to continue?"),
991                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, FALSE,
992                          NULL, ALERT_WARNING, G_ALERTDEFAULT) 
993                 != G_ALERTALTERNATE) {
994                 return;
995         }
996
997         inc_lock();
998         if (rebuild)
999                 window = label_window_create(_("Rebuilding folder tree..."));
1000         else 
1001                 window = label_window_create(_("Scanning folder tree..."));
1002
1003         folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
1004         folder_scan_tree(folder, rebuild);
1005         folder_set_ui_func(folder, NULL, NULL);
1006
1007         folderview_set_all();
1008
1009         gtk_widget_destroy(window);
1010         inc_unlock();
1011 }
1012
1013 /** folderview_check_new()
1014  *  Scan and update the folder and return the 
1015  *  count the number of new messages since last check. 
1016  *  \param folder the folder to check for new messages
1017  *  \return the number of new messages since last check
1018  */
1019 gint folderview_check_new(Folder *folder)
1020 {
1021         GList *list;
1022         FolderItem *item;
1023         FolderView *folderview;
1024         GtkCTree *ctree;
1025         GtkCTreeNode *node;
1026         gint new_msgs = 0;
1027         gint former_new_msgs = 0;
1028         gint former_new = 0, former_unread = 0, former_total;
1029
1030         for (list = folderview_list; list != NULL; list = list->next) {
1031                 folderview = (FolderView *)list->data;
1032                 ctree = GTK_CTREE(folderview->ctree);
1033
1034                 inc_lock();
1035                 main_window_lock(folderview->mainwin);
1036
1037                 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1038                      node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1039                         gchar *str = NULL;
1040                         item = gtk_ctree_node_get_row_data(ctree, node);
1041                         if (!item || !item->path || !item->folder) continue;
1042                         if (item->no_select) continue;
1043                         if (folder && folder != item->folder) continue;
1044                         if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
1045                         if (!item->prefs->newmailcheck) continue;
1046                         
1047                         str = get_scan_str(item);
1048
1049                         STATUSBAR_PUSH(folderview->mainwin, str);
1050
1051                         g_free(str);
1052
1053                         folderview_scan_tree_func(item->folder, item, NULL);
1054                         former_new    = item->new_msgs;
1055                         former_unread = item->unread_msgs;
1056                         former_total  = item->total_msgs;
1057
1058                         if (item->folder->klass->scan_required &&
1059                             (item->folder->klass->scan_required(item->folder, item) ||
1060                              item->folder->inbox == item ||
1061                              item->opened == TRUE ||
1062                              item->processing_pending == TRUE)) {
1063                                 if (folder_item_scan(item) < 0) {
1064                                         summaryview_unlock(folderview->summaryview, item);
1065                                         if (folder && !FOLDER_IS_LOCAL(folder)) {
1066                                                 STATUSBAR_POP(folderview->mainwin);
1067                                                 break;
1068                                         }
1069                                 }
1070                         } else if (!item->folder->klass->scan_required) {
1071                                 if (folder_item_scan(item) < 0) {
1072                                         summaryview_unlock(folderview->summaryview, item);
1073                                         if (folder && !FOLDER_IS_LOCAL(folder)) {
1074                                                 STATUSBAR_POP(folderview->mainwin);
1075                                                 break;
1076                                         }
1077                                 }
1078                         }
1079                         if (former_new    != item->new_msgs ||
1080                             former_unread != item->unread_msgs ||
1081                             former_total  != item->total_msgs)
1082                                 folderview_update_node(folderview, node);
1083
1084                         new_msgs += item->new_msgs;
1085                         former_new_msgs += former_new;
1086                         STATUSBAR_POP(folderview->mainwin);
1087                 }
1088
1089                 main_window_unlock(folderview->mainwin);
1090                 inc_unlock();
1091         }
1092
1093         folder_write_list();
1094         /* Number of new messages since last check is the just the difference 
1095          * between former_new_msgs and new_msgs. If new_msgs is less than
1096          * former_new_msgs, that would mean another session accessed the folder
1097          * and the result is not well defined.
1098          */
1099         new_msgs = (former_new_msgs < new_msgs ? new_msgs - former_new_msgs : 0);
1100         return new_msgs;
1101 }
1102
1103 void folderview_check_new_all(void)
1104 {
1105         GList *list;
1106         GtkWidget *window;
1107         FolderView *folderview;
1108
1109         folderview = (FolderView *)folderview_list->data;
1110
1111         inc_lock();
1112         main_window_lock(folderview->mainwin);
1113         window = label_window_create
1114                 (_("Checking for new messages in all folders..."));
1115
1116         list = folder_get_list();
1117         for (; list != NULL; list = list->next) {
1118                 Folder *folder = list->data;
1119
1120                 folderview_check_new(folder);
1121         }
1122
1123         folder_write_list();
1124         folderview_set_all();
1125
1126         gtk_widget_destroy(window);
1127         main_window_unlock(folderview->mainwin);
1128         inc_unlock();
1129 }
1130
1131 static gboolean folderview_have_new_children_sub(FolderView *folderview,
1132                                                  FolderItem *item,
1133                                                  gboolean in_sub)
1134 {
1135         GNode *node = NULL;
1136         
1137         if (!item || !item->folder || !item->folder->node)
1138                 return FALSE;
1139                 
1140         node = item->folder->node;
1141         
1142         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1143         node = node->children;
1144
1145         if (in_sub &&
1146             (item->new_msgs > 0 ||
1147             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1148                 return TRUE;
1149         }
1150
1151         while (node != NULL) {
1152                 if (node && node->data) {
1153                         FolderItem *next_item = (FolderItem*) node->data;
1154                         node = node->next;
1155                         if (folderview_have_new_children_sub(folderview, 
1156                                                              next_item, TRUE))
1157                                 return TRUE;
1158                 }
1159         }
1160
1161         return FALSE;
1162 }
1163
1164 static gboolean folderview_have_new_children(FolderView *folderview,
1165                                              FolderItem *item)
1166 {
1167         return folderview_have_new_children_sub(folderview, item, FALSE);
1168 }
1169
1170 static gboolean folderview_have_unread_children_sub(FolderView *folderview,
1171                                                     FolderItem *item, 
1172                                                     gboolean in_sub)
1173 {
1174         GNode *node = NULL;
1175         
1176         if (!item || !item->folder || !item->folder->node)
1177                 return FALSE;
1178         
1179         node = item->folder->node;
1180         
1181         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1182         node = node->children;
1183
1184         if (in_sub &&
1185             (item->unread_msgs > 0 ||
1186             (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0))) {
1187                 return TRUE;
1188         }
1189
1190         while (node != NULL) {
1191                 if (node && node->data) {
1192                         FolderItem *next_item = (FolderItem*) node->data;
1193                         node = node->next;
1194                         if (folderview_have_unread_children_sub(folderview, 
1195                                                                 next_item, 
1196                                                                 TRUE))
1197                                 return TRUE;
1198                 }
1199         }
1200
1201         return FALSE;
1202 }
1203
1204 static gboolean folderview_have_unread_children(FolderView *folderview,
1205                                                 FolderItem *item)
1206 {
1207         return folderview_have_unread_children_sub(folderview, item, FALSE);
1208 }
1209
1210 static gboolean folderview_have_matching_children_sub(FolderView *folderview,
1211                                                       FolderItem *item,
1212                                                       gboolean in_sub)
1213 {
1214         GNode *node = NULL;
1215
1216         if (!item || !item->folder || !item->folder->node)
1217                 return FALSE;
1218
1219         node = item->folder->node;
1220         
1221         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1222         node = node->children;
1223
1224         if (in_sub && item->search_match){
1225                 return TRUE;
1226         }
1227
1228         while (node != NULL) {
1229                 if (node && node->data) {
1230                         FolderItem *next_item = (FolderItem*) node->data;
1231                         node = node->next;
1232                         if (folderview_have_matching_children_sub(folderview, 
1233                                                                   next_item, 
1234                                                                   TRUE))
1235                                 return TRUE;
1236                 }
1237         }
1238
1239         return FALSE;
1240 }
1241
1242 static gboolean folderview_have_matching_children(FolderView *folderview,
1243                                                   FolderItem *item)
1244 {
1245         return folderview_have_matching_children_sub(folderview, item, FALSE);
1246 }
1247
1248 static gboolean folderview_have_marked_children_sub(FolderView *folderview,
1249                                                     FolderItem *item,
1250                                                     gboolean in_sub)
1251 {
1252         GNode *node = NULL;
1253         
1254         if (!item || !item->folder || !item->folder->node)
1255                 return FALSE;
1256                 
1257         node = item->folder->node;
1258         
1259         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
1260         node = node->children;
1261
1262         if (item->marked_msgs != 0) {
1263                 return TRUE;
1264         }
1265
1266         while (node != NULL) {
1267                 if (node && node->data) {
1268                         FolderItem *next_item = (FolderItem*) node->data;
1269                         node = node->next;
1270                         if (folderview_have_marked_children_sub(folderview,
1271                                                                 next_item, TRUE))
1272                                 return TRUE;
1273                 }
1274         }
1275
1276         return FALSE;
1277 }
1278
1279 static gboolean folderview_have_marked_children(FolderView *folderview,
1280                                              FolderItem *item)
1281 {
1282         return folderview_have_marked_children_sub(folderview, item, FALSE);
1283 }
1284
1285 static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
1286 {
1287         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1288         GtkStyle *style = NULL;
1289         GtkStyle *color_style = NULL;
1290         FolderItem *item;
1291         GdkPixmap *xpm, *openxpm;
1292         GdkBitmap *mask, *openmask;
1293         static GdkPixmap *searchicon;
1294         static GdkBitmap *searchmask;
1295         gboolean mark = FALSE;
1296         gchar *name;
1297         gchar *str;
1298         gboolean add_unread_mark;
1299         gboolean add_sub_match_mark;
1300         gboolean use_bold, use_color;
1301         gint *col_pos = folderview->col_pos;
1302         SpecialFolderItemType stype;
1303         
1304         item = gtk_ctree_node_get_row_data(ctree, node);
1305         g_return_if_fail(item != NULL);
1306
1307         if (!GTK_CTREE_ROW(node)->expanded)
1308                 mark = folderview_have_marked_children(folderview, item);
1309         else
1310                 mark = (item->marked_msgs != 0);
1311
1312         stype = item->stype;
1313         if (stype == F_NORMAL) {
1314                 if (folder_has_parent_of_type(item, F_TRASH))
1315                         stype = F_TRASH;
1316                 else if (folder_has_parent_of_type(item, F_DRAFT))
1317                         stype = F_DRAFT;
1318                 else if (folder_has_parent_of_type(item, F_OUTBOX))
1319                         stype = F_OUTBOX;
1320                 else if (folder_has_parent_of_type(item, F_QUEUE))
1321                         stype = F_QUEUE;
1322         }
1323         switch (stype) {
1324         case F_INBOX:
1325                 if (item->hide_read_msgs) {
1326                         xpm = mark?m_inboxhrmxpm:inboxhrmxpm;
1327                         mask = mark?m_inboxhrmxpmmask:inboxhrmxpmmask;
1328                         openxpm = mark?m_inboxopenhrmxpm:inboxopenhrmxpm;
1329                         openmask = mark?m_inboxopenhrmxpmmask:inboxopenhrmxpmmask;
1330                 } else {
1331                         xpm = mark?m_inboxxpm:inboxxpm;
1332                         mask = mark?m_inboxxpmmask:inboxxpmmask;
1333                         openxpm = mark?m_inboxopenxpm:inboxopenxpm;
1334                         openmask = mark?m_inboxopenxpmmask:inboxopenxpmmask;
1335                 }
1336                 break;
1337         case F_OUTBOX:
1338                 if (item->hide_read_msgs) {
1339                         xpm = mark?m_outboxhrmxpm:outboxhrmxpm;
1340                         mask = mark?m_outboxhrmxpmmask:outboxhrmxpmmask;
1341                         openxpm = mark?m_outboxopenhrmxpm:outboxopenhrmxpm;
1342                         openmask = mark?m_outboxopenhrmxpmmask:outboxopenhrmxpmmask;
1343                 } else {
1344                         xpm = mark?m_outboxxpm:outboxxpm;
1345                         mask = mark?m_outboxxpmmask:outboxxpmmask;
1346                         openxpm = mark?m_outboxopenxpm:outboxopenxpm;
1347                         openmask = mark?m_outboxopenxpmmask:outboxopenxpmmask;
1348                 }
1349                 break;
1350         case F_QUEUE:
1351                 if (item->hide_read_msgs) {
1352                         xpm = mark?m_queuehrmxpm:queuehrmxpm;
1353                         mask = mark?m_queuehrmxpmmask:queuehrmxpmmask;
1354                         openxpm = mark?m_queueopenhrmxpm:queueopenhrmxpm;
1355                         openmask = mark?m_queueopenhrmxpmmask:queueopenhrmxpmmask;
1356                 } else {
1357                         xpm = mark?m_queuexpm:queuexpm;
1358                         mask = mark?m_queuexpmmask:queuexpmmask;
1359                         openxpm = mark?m_queueopenxpm:queueopenxpm;
1360                         openmask = mark?m_queueopenxpmmask:queueopenxpmmask;
1361                 }
1362                 break;
1363         case F_TRASH:
1364                 if (item->hide_read_msgs) {
1365                         xpm = mark?m_trashhrmxpm:trashhrmxpm;
1366                         mask = mark?m_trashhrmxpmmask:trashhrmxpmmask;
1367                         openxpm = mark?m_trashopenhrmxpm:trashopenhrmxpm;
1368                         openmask = mark?m_trashopenhrmxpmmask:trashopenhrmxpmmask;
1369                 } else {
1370                         xpm = mark?m_trashxpm:trashxpm;
1371                         mask = mark?m_trashxpmmask:trashxpmmask;
1372                         openxpm = mark?m_trashopenxpm:trashopenxpm;
1373                         openmask = mark?m_trashopenxpmmask:trashopenxpmmask;
1374                 }
1375                 break;
1376         case F_DRAFT:
1377                 xpm = mark?m_draftsxpm:draftsxpm;
1378                 mask = mark?m_draftsxpmmask:draftsxpmmask;
1379                 openxpm = mark?m_draftsopenxpm:draftsopenxpm;
1380                 openmask = mark?m_draftsopenxpmmask:draftsopenxpmmask;
1381                 break;
1382         default:
1383                 if (item->hide_read_msgs) {
1384                         xpm = mark?m_folderhrmxpm:folderhrmxpm;
1385                         mask = mark?m_folderhrmxpmmask:folderhrmxpmmask;
1386                         openxpm = mark?m_folderopenhrmxpm:folderopenhrmxpm;
1387                         openmask = mark?m_folderopenhrmxpmmask:folderopenhrmxpmmask;
1388                 } else {
1389                         xpm = mark?m_folderxpm:folderxpm;
1390                         mask = mark?m_folderxpmmask:folderxpmmask;
1391                         openxpm = mark?m_folderopenxpm:folderopenxpm;
1392                         openmask = mark?m_folderopenxpmmask:folderopenxpmmask;
1393                 }
1394         }
1395         
1396         if (item->no_select) {
1397                 xpm = openxpm = noselectxpm;
1398                 mask = openmask = noselectxpmmask;
1399         }
1400
1401         name = folder_item_get_name(item);
1402
1403         if (!GTK_CTREE_ROW(node)->expanded) {
1404                 add_unread_mark = folderview_have_unread_children(
1405                                         folderview, item);
1406                 add_sub_match_mark = folderview_have_matching_children(
1407                                         folderview, item);
1408         } else {
1409                 add_unread_mark = FALSE;
1410                 add_sub_match_mark = FALSE;
1411         }
1412
1413         if (item->search_match) {
1414                 if (!searchicon) {
1415                         stock_pixmap_gdk(folderview->ctree, STOCK_PIXMAP_QUICKSEARCH,
1416                          &searchicon, &searchmask);
1417                 }
1418                 xpm = openxpm = searchicon;
1419                 mask = openmask = searchmask;
1420         }
1421
1422         if (folder_has_parent_of_type(item, F_QUEUE) && item->total_msgs > 0 &&
1423             prefs_common.display_folder_unread) {
1424                 str = g_strdup_printf("%s (%d%s)", name, item->total_msgs,
1425                                       add_unread_mark ? "+" : "");
1426                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1427                                         xpm, mask, openxpm, openmask,
1428                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1429                 g_free(str);
1430         } else if (((item->unread_msgs > 0 || add_unread_mark) &&
1431                     prefs_common.display_folder_unread) 
1432                    || add_sub_match_mark) {
1433
1434                 if (item->unread_msgs > 0)
1435                         str = g_strdup_printf("%s (%d%s%s)", name, item->unread_msgs,
1436                                               add_unread_mark || add_sub_match_mark ? "+" : "", 
1437                                               item->unreadmarked_msgs > 0 ? "!":"");
1438                 else
1439                         str = g_strdup_printf("%s (+)", name);
1440                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1441                                         xpm, mask, openxpm, openmask,
1442                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1443                 g_free(str);
1444         } else {
1445                 str = g_strdup_printf("%s%s", name, 
1446                                       item->unreadmarked_msgs > 0 ? " (!)":"");
1447         
1448                 gtk_sctree_set_node_info(ctree, node, str, FOLDER_SPACING,
1449                                         xpm, mask, openxpm, openmask,
1450                                         FALSE, GTK_CTREE_ROW(node)->expanded);
1451                 g_free(str);
1452         }
1453         g_free(name);
1454
1455         if (!folder_item_parent(item)) {
1456                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    "-");
1457                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], "-");
1458                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  "-");
1459         } else {
1460                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_NEW],    itos(item->new_msgs));
1461                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_UNREAD], itos(item->unread_msgs));
1462                 gtk_ctree_node_set_text(ctree, node, col_pos[F_COL_TOTAL],  itos(item->total_msgs));
1463         }
1464
1465         if (folder_has_parent_of_type(item, F_OUTBOX) ||
1466             folder_has_parent_of_type(item, F_DRAFT) ||
1467             folder_has_parent_of_type(item, F_TRASH)) {
1468                 use_bold = use_color = FALSE;
1469         } else if (folder_has_parent_of_type(item, F_QUEUE)) {
1470                 /* highlight queue folder if there are any messages */
1471                 use_bold = use_color = (item->total_msgs > 0);
1472         } else {
1473                 /* if unread messages exist, print with bold font */
1474                 use_bold = (item->unread_msgs > 0|| item->new_msgs > 0) 
1475                                 || add_unread_mark;
1476                 /* if new messages exist, print with colored letter */
1477                 use_color =
1478                         (item->new_msgs > 0) ||
1479                         (add_unread_mark &&
1480                          folderview_have_new_children(folderview, item));       
1481         }
1482
1483         gtk_ctree_node_set_foreground(ctree, node, NULL);
1484
1485         if (use_bold) {
1486                 GdkColor gdk_color;
1487
1488                 if (item->prefs->color > 0 && !use_color) {
1489                         gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1490                         color_style = gtk_style_copy(bold_style);
1491                         color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1492                         style = color_style;
1493                 } else if (use_color) {
1494                         gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
1495                         bold_color_style = gtk_style_copy(bold_style);
1496                         bold_color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1497                         style = bold_color_style;
1498                 } else
1499                         style = bold_style;
1500                 if (item->op_count > 0) {
1501                         style = bold_tgtfold_style;
1502                 }
1503         } else if (use_color) {
1504                 GdkColor gdk_color;
1505
1506                 gtkut_convert_int_to_gdk_color(prefs_common.color_new, &gdk_color);
1507                 style = gtk_style_copy(normal_color_style);
1508                 style->fg[GTK_STATE_NORMAL] = gdk_color;
1509                 gtk_ctree_node_set_foreground(ctree, node,
1510                                               &folderview->color_new);
1511         } else if (item->op_count > 0) {
1512                 style = bold_tgtfold_style;
1513         } else if (item->prefs->color > 0) {
1514                 GdkColor gdk_color;
1515                 gtkut_convert_int_to_gdk_color(item->prefs->color, &gdk_color);
1516                 color_style = gtk_style_copy(normal_style);
1517                 color_style->fg[GTK_STATE_NORMAL] = gdk_color;
1518                 style = color_style;
1519         } else {
1520                 style = normal_style;
1521         }
1522
1523         gtk_ctree_node_set_row_style(ctree, node, style);
1524
1525         if ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
1526                 folderview_update_node(folderview, node);
1527 }
1528
1529 #if !CLAWS /* keep it here for syncs */
1530 void folderview_update_item(FolderItem *item, gboolean update_summary)
1531 {
1532         GList *list;
1533         FolderView *folderview;
1534         GtkCTree *ctree;
1535         GtkCTreeNode *node;
1536
1537         g_return_if_fail(item != NULL);
1538
1539         for (list = folderview_list; list != NULL; list = list->next) {
1540                 folderview = (FolderView *)list->data;
1541                 ctree = GTK_CTREE(folderview->ctree);
1542
1543                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1544                 if (node) {
1545                         folderview_update_node(folderview, node);
1546                         if (update_summary && folderview->opened == node)
1547                                 summary_show(folderview->summaryview,
1548                                              item, FALSE);
1549                 }
1550         }
1551 }
1552 #endif
1553
1554 void folderview_update_search_icon(FolderItem *item, gboolean matches)
1555 {
1556         GList *list;
1557         FolderView *folderview;
1558         GtkCTree *ctree;
1559         GtkCTreeNode *node;
1560
1561         g_return_if_fail(item != NULL);
1562
1563         for (list = folderview_list; list != NULL; list = list->next) {
1564                 folderview = (FolderView *)list->data;
1565                 ctree = GTK_CTREE(folderview->ctree);
1566
1567                 node = gtk_ctree_find_by_row_data(ctree, NULL, item);
1568                 if (node) {
1569                         item->search_match = matches;
1570                         folderview_update_node(folderview, node);
1571                 }
1572         }
1573 }
1574
1575 gboolean folderview_update_item_claws(gpointer source, gpointer data)
1576 {
1577         FolderItemUpdateData *update_info = (FolderItemUpdateData *)source;
1578         FolderView *folderview = (FolderView *)data;
1579         GtkCTree *ctree;
1580         GtkCTreeNode *node;
1581         g_return_val_if_fail(update_info != NULL, TRUE);
1582         g_return_val_if_fail(update_info->item != NULL, TRUE);
1583         g_return_val_if_fail(folderview != NULL, FALSE);
1584
1585         ctree = GTK_CTREE(folderview->ctree);
1586
1587         node = gtk_ctree_find_by_row_data(ctree, NULL, update_info->item);
1588         if (node) {
1589                 if (update_info->update_flags & (F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_NAME))
1590                         folderview_update_node(folderview, node);
1591                 if ((update_info->update_flags & F_ITEM_UPDATE_CONTENT) && (folderview->opened == node))
1592                         summary_show(folderview->summaryview, update_info->item);
1593         }
1594         
1595         return FALSE;
1596 }
1597
1598 static void folderview_update_item_foreach_func(gpointer key, gpointer val,
1599                                                 gpointer data)
1600 {
1601         /* CLAWS: share this joy with other hook functions ... */
1602         folder_item_update((FolderItem *)key, 
1603                            (FolderItemUpdateFlags)GPOINTER_TO_INT(data));
1604 }
1605
1606 void folderview_update_item_foreach(GHashTable *table, gboolean update_summary)
1607 {
1608         GList *list;
1609         FolderItemUpdateFlags flags;
1610         
1611         flags = update_summary ?  F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_MSGCNT
1612                 : 0;
1613         for (list = folderview_list; list != NULL; list = list->next)
1614                 g_hash_table_foreach(table, folderview_update_item_foreach_func, 
1615                                      GINT_TO_POINTER(flags));
1616 }
1617
1618 static gboolean folderview_gnode_func(GtkCTree *ctree, guint depth,
1619                                       GNode *gnode, GtkCTreeNode *cnode,
1620                                       gpointer data)
1621 {
1622         FolderView *folderview = (FolderView *)data;
1623         FolderItem *item = FOLDER_ITEM(gnode->data);
1624
1625         g_return_val_if_fail(item != NULL, FALSE);
1626
1627         gtk_ctree_node_set_row_data(ctree, cnode, item);
1628         folderview_update_node(folderview, cnode);
1629
1630         return TRUE;
1631 }
1632
1633 static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
1634                                    gpointer data)
1635 {
1636         FolderView *folderview = (FolderView *)data;
1637         FolderItem *item;
1638
1639         if (GTK_CTREE_ROW(node)->children) {
1640                 item = gtk_ctree_node_get_row_data(ctree, node);
1641                 g_return_if_fail(item != NULL);
1642
1643                 if (!item->collapsed)
1644                         gtk_ctree_expand(ctree, node);
1645                 else
1646                         folderview_update_node(folderview, node);
1647         }
1648 }
1649
1650 static void set_special_folder(GtkCTree *ctree, FolderItem *item,
1651                                GtkCTreeNode *root, GtkCTreeNode **prev)
1652 {
1653         if (item) {
1654                 GtkCTreeNode *node, *parent, *sibling;
1655
1656                 node = gtk_ctree_find_by_row_data(ctree, root, item);
1657                 if (!node)
1658                         g_warning("%s not found.\n", item->path);
1659                 else {
1660                         parent = GTK_CTREE_ROW(node)->parent;
1661                         if (*prev && parent == GTK_CTREE_ROW(*prev)->parent)
1662                                 sibling = GTK_CTREE_ROW(*prev)->sibling;
1663                         else
1664                                 sibling = GTK_CTREE_ROW(parent)->children;
1665                         while (sibling) {
1666                                 FolderItem *tmp;
1667
1668                                 tmp = gtk_ctree_node_get_row_data
1669                                         (ctree, sibling);
1670                                 if (tmp->stype != F_NORMAL)
1671                                         sibling = GTK_CTREE_ROW(sibling)->sibling;
1672                                 else
1673                                         break;
1674                         }
1675                         if (node != sibling)
1676                                 gtk_ctree_move(ctree, node, parent, sibling);
1677                 }
1678
1679                 *prev = node;
1680         }
1681 }
1682
1683 static void folderview_sort_folders(FolderView *folderview, GtkCTreeNode *root,
1684                                     Folder *folder)
1685 {
1686         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1687         GtkCTreeNode *prev = NULL;
1688
1689         gtk_sctree_sort_recursive(ctree, root);
1690
1691         if (root && GTK_CTREE_ROW(root)->parent) return;
1692
1693         set_special_folder(ctree, folder->inbox, root, &prev);
1694         set_special_folder(ctree, folder->outbox, root, &prev);
1695         set_special_folder(ctree, folder->draft, root, &prev);
1696         set_special_folder(ctree, folder->queue, root, &prev);
1697         set_special_folder(ctree, folder->trash, root, &prev);
1698 }
1699
1700 static void folderview_append_folder(FolderView *folderview, Folder *folder)
1701 {
1702         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
1703         GtkCTreeNode *root;
1704
1705         g_return_if_fail(folder != NULL);
1706
1707         root = gtk_ctree_insert_gnode(ctree, NULL, NULL, folder->node,
1708                                       folderview_gnode_func, folderview);
1709         gtk_ctree_pre_recursive(ctree, root, folderview_expand_func,
1710                                 folderview);
1711         folderview_sort_folders(folderview, root, folder);
1712 }
1713
1714 /* callback functions */
1715
1716 static gboolean folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
1717                                           FolderView *folderview)
1718 {
1719         GtkCList *clist = GTK_CLIST(ctree);
1720         gint prev_row = -1, row = -1, column = -1;
1721         FolderItem *item;
1722         Folder *folder;
1723         FolderViewPopup *fpopup;
1724         GtkItemFactory *fpopup_factory;
1725         GtkWidget *popup;
1726         FolderItem *special_trash = NULL;
1727         PrefsAccount *ac;
1728
1729         if (!event) return FALSE;
1730
1731         if (event->button == 1 || event->button == 2) {
1732                 folderview->open_folder = TRUE;
1733
1734                 if (event->type == GDK_2BUTTON_PRESS) {
1735                         if (clist->selection) {
1736                                 GtkCTreeNode *node;
1737
1738                                 node = GTK_CTREE_NODE(clist->selection->data);
1739                                 if (node)
1740                                         gtk_ctree_toggle_expansion(
1741                                                 GTK_CTREE(ctree),
1742                                                 node);
1743                         }
1744                 }
1745                 return FALSE;
1746         }
1747
1748         if (event->button == 2 || event->button == 3) {
1749                 /* right clicked */
1750                 if (clist->selection) {
1751                         GtkCTreeNode *node;
1752
1753                         node = GTK_CTREE_NODE(clist->selection->data);
1754                         if (node)
1755                                 prev_row = gtkut_ctree_get_nth_from_node
1756                                         (GTK_CTREE(ctree), node);
1757                 }
1758
1759                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1760                                                   &row, &column))
1761                         return FALSE;
1762                 if (prev_row != row) {
1763                         gtk_clist_unselect_all(clist);
1764                         if (event->button == 2)
1765                                 folderview_select_node
1766                                         (folderview,
1767                                          gtk_ctree_node_nth(GTK_CTREE(ctree),
1768                                                             row));
1769                         else
1770                                 gtk_clist_select_row(clist, row, column);
1771                 }
1772         }
1773
1774         if (event->button != 3) return FALSE;
1775
1776         item = gtk_clist_get_row_data(clist, row);
1777         g_return_val_if_fail(item != NULL, FALSE);
1778         g_return_val_if_fail(item->folder != NULL, FALSE);
1779         folder = item->folder;
1780
1781         fpopup = g_hash_table_lookup(folderview_popups, folder->klass->idstr);
1782         if (fpopup != NULL)
1783                 fpopup_factory = g_hash_table_lookup(folderview->popups, folder->klass->idstr);
1784         else {
1785                 fpopup = g_hash_table_lookup(folderview_popups, "common");
1786                 fpopup_factory = g_hash_table_lookup(folderview->popups, "common");
1787         }
1788
1789         if (fpopup->set_sensitivity != NULL)
1790                 fpopup->set_sensitivity(fpopup_factory, item);
1791
1792         if (NULL != (ac = account_find_from_item(item)))
1793                 special_trash = account_get_special_folder(ac, F_TRASH);
1794
1795         if ((item == folder->trash || item == special_trash
1796              || folder_has_parent_of_type(item, F_TRASH)) &&
1797             gtk_item_factory_get_item(fpopup_factory, "/Empty trash...") == NULL) {
1798                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[0], folderview, 1);
1799                 gtk_item_factory_create_item(fpopup_factory, &folder_view_trash_popup_entries[1], folderview, 1);
1800         } else if (item != folder->trash && (special_trash == NULL || item != special_trash)
1801                 && !folder_has_parent_of_type(item, F_TRASH)) {
1802                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[0]);
1803                 gtk_item_factory_delete_entry(fpopup_factory, &folder_view_trash_popup_entries[1]);
1804         }
1805         
1806 #define SET_SENS(name, sens) \
1807         menu_set_sensitive(fpopup_factory, name, sens)
1808
1809         SET_SENS("/Mark all read", item->unread_msgs >= 1);
1810         SET_SENS("/Search folder...", item->total_msgs >= 1 && 
1811                  folderview->selected == folderview->opened);
1812         SET_SENS("/Properties...", item->node->parent != NULL);
1813         SET_SENS("/Processing...", item->node->parent != NULL);
1814         if (item == folder->trash || item == special_trash
1815             || folder_has_parent_of_type(item, F_TRASH)) {
1816                 GSList *msglist = folder_item_get_msg_list(item);
1817                 SET_SENS("/Empty trash...", msglist != NULL);
1818                 procmsg_msg_list_free(msglist);
1819         }
1820 #undef SET_SENS
1821
1822         popup = gtk_item_factory_get_widget(fpopup_factory, fpopup->path);
1823         gtk_menu_popup(GTK_MENU(popup), NULL, NULL, NULL, NULL,
1824                        event->button, event->time);
1825
1826         return FALSE;
1827 }
1828
1829 static gboolean folderview_button_released(GtkWidget *ctree, GdkEventButton *event,
1830                                            FolderView *folderview)
1831 {
1832         if (!event) return FALSE;
1833
1834         if (event->button == 1 && folderview->open_folder == FALSE &&
1835             folderview->opened != NULL) {
1836                 gtkut_ctree_set_focus_row(GTK_CTREE(ctree),
1837                                           folderview->opened);
1838                 gtk_ctree_select(GTK_CTREE(ctree), folderview->opened);
1839         }
1840
1841         return FALSE;
1842 }
1843
1844 static gboolean folderview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1845                                        FolderView *folderview)
1846 {
1847         if (!event) return FALSE;
1848
1849         if (quicksearch_has_focus(folderview->summaryview->quicksearch))
1850                 return FALSE;
1851
1852         switch (event->keyval) {
1853         case GDK_Return:
1854                 if (folderview->selected) {
1855                         folderview_select_node(folderview,
1856                                                folderview->selected);
1857                 }
1858                 break;
1859         case GDK_space:
1860                 if (folderview->selected) {
1861                         if (folderview->opened == folderview->selected &&
1862                             (!folderview->summaryview->folder_item ||
1863                              folderview->summaryview->folder_item->total_msgs == 0))
1864                                 folderview_select_next_unread(folderview);
1865                         else
1866                                 folderview_select_node(folderview,
1867                                                        folderview->selected);
1868                 }
1869                 break;
1870         default:
1871                 break;
1872         }
1873
1874         return FALSE;
1875 }
1876
1877 static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
1878                                 gint column, FolderView *folderview)
1879 {
1880         static gboolean can_select = TRUE;      /* exclusive lock */
1881         gboolean opened;
1882         FolderItem *item;
1883         gchar *buf;
1884
1885         folderview->selected = row;
1886
1887         if (folderview->opened == row) {
1888                 folderview->open_folder = FALSE;
1889                 return;
1890         }
1891         
1892         if (!can_select || summary_is_locked(folderview->summaryview)) {
1893                 if (folderview->opened) {
1894                         gtkut_ctree_set_focus_row(ctree, folderview->opened);
1895                         gtk_ctree_select(ctree, folderview->opened);
1896                 }
1897                 return;
1898         }
1899
1900         if (!folderview->open_folder) return;
1901
1902         item = gtk_ctree_node_get_row_data(ctree, row);
1903         if (!item || item->no_select) return;
1904
1905         can_select = FALSE;
1906
1907         /* Save cache for old folder */
1908         /* We don't want to lose all caches if sylpheed crashed */
1909         if (folderview->opened) {
1910                 FolderItem *olditem;
1911                 
1912                 olditem = gtk_ctree_node_get_row_data(ctree, folderview->opened);
1913                 if (olditem) {
1914                         /* will be null if we just moved the previously opened folder */
1915                         summary_save_prefs_to_folderitem(folderview->summaryview, olditem);
1916                         summary_show(folderview->summaryview, NULL);
1917                         folder_item_close(olditem);
1918                 }
1919         }
1920
1921         /* CLAWS: set compose button type: news folder items 
1922          * always have a news folder as parent */
1923         if (item->folder) 
1924                 toolbar_set_compose_button
1925                         (folderview->mainwin->toolbar,
1926                          FOLDER_TYPE(item->folder) == F_NEWS ? 
1927                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
1928
1929         if (item->path)
1930                 debug_print("Folder %s is selected\n", item->path);
1931
1932         if (!GTK_CTREE_ROW(row)->children)
1933                 gtk_ctree_expand(ctree, row);
1934         if (folderview->opened &&
1935             !GTK_CTREE_ROW(folderview->opened)->children)
1936                 gtk_ctree_collapse(ctree, folderview->opened);
1937
1938         /* ungrab the mouse event */
1939         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1940                 gtk_grab_remove(GTK_WIDGET(ctree));
1941                 if (gdk_pointer_is_grabbed())
1942                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1943         }
1944
1945         /* Open Folder */
1946         buf = g_strdup_printf(_("Opening Folder %s..."), item->path ? 
1947                                         item->path : "(null)");
1948         debug_print("%s\n", buf);
1949         STATUSBAR_PUSH(folderview->mainwin, buf);
1950         g_free(buf);
1951
1952         main_window_cursor_wait(folderview->mainwin);
1953
1954         if (folder_item_open(item) != 0) {
1955                 main_window_cursor_normal(folderview->mainwin);
1956                 STATUSBAR_POP(folderview->mainwin);
1957
1958                 alertpanel_error(_("Folder could not be opened."));
1959
1960                 folderview->open_folder = FALSE;
1961                 can_select = TRUE;
1962
1963                 return;
1964         }
1965         
1966         main_window_cursor_normal(folderview->mainwin);
1967
1968         /* Show messages */
1969         summary_set_prefs_from_folderitem(folderview->summaryview, item);
1970         opened = summary_show(folderview->summaryview, item);
1971         
1972         folder_clean_cache_memory();
1973
1974         if (!opened) {
1975                 gtkut_ctree_set_focus_row(ctree, folderview->opened);
1976                 gtk_ctree_select(ctree, folderview->opened);
1977         } else {
1978                 folderview->opened = row;
1979                 if (gtk_ctree_node_is_visible(ctree, row)
1980                     != GTK_VISIBILITY_FULL)
1981                         gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
1982         }
1983
1984         STATUSBAR_POP(folderview->mainwin);
1985
1986         folderview->open_folder = FALSE;
1987         can_select = TRUE;
1988 }
1989
1990 static void folderview_tree_expanded(GtkCTree *ctree, GtkCTreeNode *node,
1991                                      FolderView *folderview)
1992 {
1993         FolderItem *item;
1994
1995         item = gtk_ctree_node_get_row_data(ctree, node);
1996         g_return_if_fail(item != NULL);
1997         item->collapsed = FALSE;
1998         folderview_update_node(folderview, node);
1999 }
2000
2001 static void folderview_tree_collapsed(GtkCTree *ctree, GtkCTreeNode *node,
2002                                       FolderView *folderview)
2003 {
2004         FolderItem *item;
2005
2006         item = gtk_ctree_node_get_row_data(ctree, node);
2007         g_return_if_fail(item != NULL);
2008         item->collapsed = TRUE;
2009         folderview_update_node(folderview, node);
2010 }
2011
2012 static void folderview_popup_close(GtkMenuShell *menu_shell,
2013                                    FolderView *folderview)
2014 {
2015         if (!folderview->opened) return;
2016
2017         gtk_ctree_select(GTK_CTREE(folderview->ctree), folderview->opened);
2018 }
2019
2020 static void folderview_col_resized(GtkCList *clist, gint column, gint width,
2021                                    FolderView *folderview)
2022 {
2023         FolderColumnType type = folderview->col_state[column].type;
2024
2025         prefs_common.folder_col_size[type] = width;
2026 }
2027
2028 void folderview_create_folder_node_recursive(FolderView *folderview, FolderItem *item)
2029 {
2030         GNode *srcnode;
2031
2032         folderview_create_folder_node(folderview, item);
2033
2034         if (!item || !item->folder || !item->folder->node)
2035                 return;
2036
2037         srcnode = item->folder->node;   
2038         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2039         srcnode = srcnode->children;
2040         while (srcnode != NULL) {
2041                 if (srcnode && srcnode->data) {
2042                         FolderItem *next_item = (FolderItem*) srcnode->data;
2043                         folderview_create_folder_node_recursive(folderview, next_item);
2044                 }
2045                 srcnode = srcnode->next;
2046         }
2047 }
2048
2049 void folderview_create_folder_node(FolderView *folderview, FolderItem *item)
2050 {
2051         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2052         gchar *text[N_FOLDER_COLS] = {NULL, "0", "0", "0"};
2053         GtkCTreeNode *node, *parent_node;
2054         gint *col_pos = folderview->col_pos;
2055         FolderItemUpdateData hookdata;
2056
2057         parent_node = gtk_ctree_find_by_row_data(ctree, NULL, folder_item_parent(item));
2058         if (parent_node == NULL)
2059                 return;
2060
2061         gtk_clist_freeze(GTK_CLIST(ctree));
2062
2063         text[col_pos[F_COL_FOLDER]] = item->name;
2064         node = gtk_sctree_insert_node(ctree, parent_node, NULL, text,
2065                                      FOLDER_SPACING,
2066                                      folderxpm, folderxpmmask,
2067                                      folderopenxpm, folderopenxpmmask,
2068                                      FALSE, FALSE);
2069         gtk_ctree_expand(ctree, parent_node);
2070         gtk_ctree_node_set_row_data(ctree, node, item);
2071         if (normal_style)
2072                 gtk_ctree_node_set_row_style(ctree, node, normal_style);
2073         folderview_sort_folders(folderview, parent_node, item->folder);
2074
2075         hookdata.item = item;
2076         hookdata.update_flags = F_ITEM_UPDATE_NAME;
2077         hookdata.msg = NULL;
2078         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
2079
2080         gtk_clist_thaw(GTK_CLIST(ctree));
2081 }
2082
2083 static void folderview_empty_trash_cb(FolderView *folderview, guint action,
2084                                       GtkWidget *widget)
2085 {
2086         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2087         FolderItem *item;
2088         GSList *mlist = NULL;
2089         GSList *cur = NULL;
2090         FolderItem *special_trash = NULL;
2091         PrefsAccount *ac;
2092
2093         if (!folderview->selected) return;
2094         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2095         g_return_if_fail(item != NULL);
2096         g_return_if_fail(item->folder != NULL);
2097
2098         if (NULL != (ac = account_find_from_item(item)))
2099                 special_trash = account_get_special_folder(ac, F_TRASH);
2100
2101         if (item != item->folder->trash && item != special_trash
2102         &&  !folder_has_parent_of_type(item, F_TRASH)) return;
2103         
2104         if (prefs_common.ask_on_clean) {
2105                 if (alertpanel(_("Empty trash"),
2106                                _("Delete all messages in trash?"),
2107                                GTK_STOCK_CANCEL, _("+_Empty trash"), NULL) != G_ALERTALTERNATE)
2108                         return;
2109         }
2110         
2111         mlist = folder_item_get_msg_list(item);
2112         
2113         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2114                 MsgInfo * msginfo = (MsgInfo *) cur->data;
2115                 if (MSG_IS_LOCKED(msginfo->flags))
2116                         continue;
2117                 /* is it partially received? (partial_recv isn't cached) */
2118                 if (msginfo->total_size != 0 && 
2119                     msginfo->size != (off_t)msginfo->total_size)
2120                         partial_mark_for_delete(msginfo);
2121         }
2122         procmsg_msg_list_free(mlist);
2123
2124         folder_item_remove_all_msg(item);
2125 }
2126
2127 static void folderview_search_cb(FolderView *folderview, guint action,
2128                                  GtkWidget *widget)
2129 {
2130         summary_search(folderview->summaryview);
2131 }
2132
2133 static void folderview_property_cb(FolderView *folderview, guint action,
2134                                    GtkWidget *widget)
2135 {
2136         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2137         FolderItem *item;
2138
2139         if (!folderview->selected) return;
2140
2141         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2142         g_return_if_fail(item != NULL);
2143         g_return_if_fail(item->folder != NULL);
2144
2145         if (folder_item_parent(item) == NULL)
2146                 return;
2147
2148         prefs_folder_item_open(item);
2149 }
2150
2151 static void folderview_recollapse_nodes(FolderView *folderview, GtkCTreeNode *node)
2152 {
2153         GSList *list = NULL;
2154         GSList *done = NULL;
2155         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2156         
2157         for (list = folderview->nodes_to_recollapse; list != NULL; list = g_slist_next(list)) {
2158                 if (!gtkut_ctree_node_is_parent(GTK_CTREE_NODE(list->data), node)
2159                 &&  list->data != node) {
2160                         gtk_ctree_collapse(ctree, GTK_CTREE_NODE(list->data));
2161                         done = g_slist_append(done, GTK_CTREE_NODE(list->data));
2162                 }
2163         }
2164         for (list = done; list != NULL; list = g_slist_next(list)) {
2165                 folderview->nodes_to_recollapse = g_slist_remove(folderview->nodes_to_recollapse, 
2166                                                                  list->data);
2167         }
2168         g_slist_free(done);
2169 }
2170
2171 void folderview_move_folder(FolderView *folderview, FolderItem *from_folder,
2172                             FolderItem *to_folder)
2173 {
2174         FolderItem *from_parent = NULL;
2175         FolderItem *new_folder = NULL;
2176         GtkCTreeNode *src_node = NULL;
2177         gchar *buf;
2178         gint status;
2179
2180         g_return_if_fail(folderview != NULL);
2181         g_return_if_fail(from_folder != NULL);
2182         g_return_if_fail(to_folder != NULL);
2183
2184         src_node = gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), NULL, from_folder);
2185         from_parent = folder_item_parent(from_folder);
2186         
2187         if (prefs_common.warn_dnd) {
2188                 buf = g_strdup_printf(_("Do you really want to make folder '%s' a "
2189                                         "sub-folder of '%s' ?"), from_folder->name,
2190                                         to_folder->name);
2191                 status = alertpanel_full(_("Move folder"), buf,
2192                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, TRUE,
2193                                          NULL, ALERT_QUESTION, G_ALERTDEFAULT);
2194                 g_free(buf);
2195
2196                 if (status != G_ALERTALTERNATE
2197                  && status != (G_ALERTALTERNATE | G_ALERTDISABLE))
2198                         return;
2199                 if (status & G_ALERTDISABLE)
2200                         prefs_common.warn_dnd = FALSE;
2201         }
2202
2203         buf = g_strdup_printf(_("Moving %s to %s..."), from_folder->name, to_folder->name);
2204         STATUSBAR_PUSH(folderview->mainwin, buf);
2205         g_free(buf);
2206         summary_clear_all(folderview->summaryview);
2207         folderview->opened = NULL;
2208         folderview->selected = NULL;
2209         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), FALSE);
2210         inc_lock();
2211         main_window_cursor_wait(folderview->mainwin);
2212         statusbar_verbosity_set(TRUE);
2213         folder_item_update_freeze();
2214         if ((status = folder_item_move_to(from_folder, to_folder, &new_folder)) == F_MOVE_OK) {
2215                 statusbar_verbosity_set(FALSE);
2216                 main_window_cursor_normal(folderview->mainwin);
2217                 STATUSBAR_POP(folderview->mainwin);
2218                 folder_item_update_thaw();
2219                 folder_item_update_recursive(new_folder, F_ITEM_UPDATE_MSGCNT);
2220
2221                 folderview_sort_folders(folderview, 
2222                         gtk_ctree_find_by_row_data(GTK_CTREE(folderview->ctree), 
2223                                 NULL, to_folder), new_folder->folder);
2224                 folderview_select(folderview, new_folder);
2225         } else {
2226                 statusbar_verbosity_set(FALSE);         
2227                 main_window_cursor_normal(folderview->mainwin);
2228                 STATUSBAR_POP(folderview->mainwin);
2229                 folder_item_update_thaw();
2230                 switch (status) {
2231                 case F_MOVE_FAILED_DEST_IS_PARENT:
2232                         alertpanel_error(_("Source and destination are the same."));
2233                         break;
2234                 case F_MOVE_FAILED_DEST_IS_CHILD:
2235                         alertpanel_error(_("Can't move a folder to one of its children."));
2236                         break;
2237                 case F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX:
2238                         alertpanel_error(_("Folder moving cannot be done between different mailboxes."));
2239                         break;
2240                 default:
2241                         alertpanel_error(_("Move failed!"));
2242                         break;
2243                 }
2244         }       
2245         inc_unlock();           
2246         gtk_widget_set_sensitive(GTK_WIDGET(folderview->ctree), TRUE);
2247 }
2248
2249 static gint folderview_clist_compare(GtkCList *clist,
2250                                      gconstpointer ptr1, gconstpointer ptr2)
2251 {
2252         FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
2253         FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
2254
2255         if (!item1->name)
2256                 return (item2->name != NULL);
2257         if (!item2->name)
2258                 return -1;
2259
2260         return g_utf8_collate(item1->name, item2->name);
2261 }
2262
2263 static void folderview_processing_cb(FolderView *folderview, guint action,
2264                                      GtkWidget *widget)
2265 {
2266         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
2267         FolderItem *item;
2268         gchar *id, *title;
2269
2270         if (!folderview->selected) return;
2271
2272         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
2273         g_return_if_fail(item != NULL);
2274         g_return_if_fail(item->folder != NULL);
2275
2276         id = folder_item_get_identifier(item);
2277         title = g_strdup_printf (_("Processing configuration for folder %s"), id);
2278         g_free (id);
2279
2280         prefs_filtering_open(&item->prefs->processing, title, NULL, NULL);
2281         g_free (title);
2282 }
2283
2284 void folderview_set_target_folder_color(gint color_op) 
2285 {
2286         gint firstone = 1;
2287         GList *list;
2288         FolderView *folderview;
2289
2290         for (list = folderview_list; list != NULL; list = list->next) {
2291                 folderview = (FolderView *)list->data;
2292                 gtkut_convert_int_to_gdk_color(color_op, &folderview->color_op);
2293                 if (firstone) {
2294                         bold_tgtfold_style->fg[GTK_STATE_NORMAL] =
2295                                 folderview->color_op;
2296                         firstone = 0;
2297                 }
2298         }
2299 }
2300
2301 static gchar *last_font = NULL;
2302 void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
2303 {
2304         /* force reinit */
2305         g_free(last_font);
2306         last_font = NULL;
2307         
2308 }
2309
2310 void folderview_reflect_prefs(void)
2311 {
2312         gboolean update_font = TRUE;
2313         FolderView *folderview = mainwindow_get_mainwindow()->folderview;
2314         FolderItem *item = folderview_get_selected_item(folderview);
2315         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2316                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2317         gint height = pos->value;
2318
2319         if (last_font && !strcmp(last_font, NORMAL_FONT))
2320                 update_font = FALSE;
2321
2322         g_free(last_font);
2323         last_font = g_strdup(NORMAL_FONT);
2324
2325         if (update_font) {              
2326                 normal_style = normal_color_style = bold_style = 
2327                         bold_color_style = bold_tgtfold_style = NULL;
2328
2329                 folderview_init(folderview);
2330         }
2331         gtk_clist_freeze(GTK_CLIST(folderview->ctree));
2332         folderview_column_set_titles(folderview);
2333         folderview_set_all();
2334
2335         g_signal_handlers_block_by_func
2336                 (G_OBJECT(folderview->ctree),
2337                  G_CALLBACK(folderview_selected), folderview);
2338
2339         if (item) {
2340                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(
2341                         GTK_CTREE(folderview->ctree), NULL, item);
2342
2343                 folderview_select(folderview, item);
2344                 folderview->selected = node;
2345         }
2346
2347         g_signal_handlers_unblock_by_func
2348                 (G_OBJECT(folderview->ctree),
2349                  G_CALLBACK(folderview_selected), folderview);
2350
2351         pos = gtk_scrolled_window_get_vadjustment(
2352                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2353         gtk_adjustment_set_value(pos, height);
2354         gtk_clist_thaw(GTK_CLIST(folderview->ctree));
2355 }
2356
2357 static void drag_state_stop(FolderView *folderview)
2358 {
2359         if (folderview->drag_timer)
2360                 gtk_timeout_remove(folderview->drag_timer);
2361         folderview->drag_timer = 0;
2362         folderview->drag_node = NULL;
2363 }
2364
2365 static gint folderview_defer_expand(FolderView *folderview)
2366 {
2367         if (folderview->drag_node) {
2368                 folderview_recollapse_nodes(folderview, folderview->drag_node);
2369                 if (folderview->drag_item->collapsed) {
2370                         gtk_ctree_expand(GTK_CTREE(folderview->ctree), folderview->drag_node);
2371                         folderview->nodes_to_recollapse = g_slist_append
2372                                 (folderview->nodes_to_recollapse, folderview->drag_node);
2373                 }
2374         }
2375         folderview->drag_item  = NULL;
2376         folderview->drag_timer = 0;
2377         return FALSE;
2378 }
2379
2380 static void drag_state_start(FolderView *folderview, GtkCTreeNode *node, FolderItem *item)
2381 {
2382         /* the idea is that we call drag_state_start() whenever we want expansion to
2383          * start after 'prefs_common.hover_time' msecs. if we want to cancel expansion,
2384          * we need to call drag_state_stop() */
2385         drag_state_stop(folderview);
2386         /* request expansion */
2387         if (0 != (folderview->drag_timer = gtk_timeout_add
2388                         (prefs_common.hover_timeout, 
2389                          (GtkFunction)folderview_defer_expand,
2390                          folderview))) {
2391                 folderview->drag_node = node;
2392                 folderview->drag_item = item;
2393         }                        
2394 }
2395
2396 static void folderview_start_drag(GtkWidget *widget, gint button, GdkEvent *event,
2397                                   FolderView       *folderview)
2398 {
2399         GdkDragContext *context;
2400
2401         g_return_if_fail(folderview != NULL);
2402         if (folderview->selected == NULL) return;
2403         if (folderview->nodes_to_recollapse) 
2404                 g_slist_free(folderview->nodes_to_recollapse);
2405         folderview->nodes_to_recollapse = NULL;
2406         context = gtk_drag_begin(widget, folderview->target_list,
2407                                  GDK_ACTION_MOVE|GDK_ACTION_COPY|GDK_ACTION_DEFAULT, button, event);
2408         gtk_drag_set_icon_default(context);
2409 }
2410
2411 static void folderview_drag_data_get(GtkWidget        *widget,
2412                                      GdkDragContext   *drag_context,
2413                                      GtkSelectionData *selection_data,
2414                                      guint             info,
2415                                      guint             time,
2416                                      FolderView       *folderview)
2417 {
2418         FolderItem *item;
2419         GList *cur;
2420         gchar *source = NULL;
2421         if (info == TARGET_DUMMY) {
2422                 for (cur = GTK_CLIST(folderview->ctree)->selection;
2423                      cur != NULL; cur = cur->next) {
2424                         item = gtk_ctree_node_get_row_data
2425                                 (GTK_CTREE(folderview->ctree), 
2426                                  GTK_CTREE_NODE(cur->data));
2427                         if (item) {
2428                                 source = g_strdup_printf ("FROM_OTHER_FOLDER%s", folder_item_get_identifier(item));
2429                                 gtk_selection_data_set(selection_data,
2430                                                        selection_data->target, 8,
2431                                                        source, strlen(source));
2432                                 break;
2433                         } else
2434                                 return;
2435                 }
2436         } else {
2437                 g_warning("unknown info %d\n", info);
2438         }
2439 }
2440
2441 gboolean folderview_update_folder(gpointer source, gpointer userdata)
2442 {
2443         FolderUpdateData *hookdata;
2444         FolderView *folderview;
2445         GtkWidget *ctree;
2446
2447         hookdata = source;
2448         folderview = (FolderView *) userdata;   
2449         g_return_val_if_fail(hookdata != NULL, FALSE);
2450         g_return_val_if_fail(folderview != NULL, FALSE);
2451
2452         ctree = folderview->ctree;
2453         g_return_val_if_fail(ctree != NULL, FALSE);
2454
2455         if (hookdata->update_flags & FOLDER_ADD_FOLDERITEM)
2456                 folderview_create_folder_node(folderview, hookdata->item);
2457         else if (hookdata->update_flags & FOLDER_RENAME_FOLDERITEM) {
2458                 GtkCTreeNode *node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree),
2459                                 NULL, folder_item_parent(hookdata->item));
2460                 folderview_sort_folders(folderview, node, hookdata->folder);
2461         } else if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM) {
2462                 GtkCTreeNode *node;
2463
2464                 node = gtk_ctree_find_by_row_data(GTK_CTREE(ctree), NULL, hookdata->item);
2465                 if (node != NULL)
2466                         gtk_ctree_remove_node(GTK_CTREE(ctree), node);
2467         } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
2468                 folderview_set(folderview);
2469
2470         return FALSE;
2471 }
2472
2473 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
2474                                           GdkDragContext *context,
2475                                           gint            x,
2476                                           gint            y,
2477                                           guint           time,
2478                                           FolderView     *folderview)
2479 {
2480         gint row, column;
2481         FolderItem *item = NULL, *src_item = NULL;
2482         GtkCTreeNode *node = NULL;
2483         gboolean acceptable = FALSE;
2484         GtkAdjustment *pos = gtk_scrolled_window_get_vadjustment(
2485                                 GTK_SCROLLED_WINDOW(folderview->scrolledwin));
2486         int height = (int)pos->page_size;
2487         int total_height = (int)pos->upper;
2488         int vpos = (int) pos->value;
2489
2490         if (gtk_clist_get_selection_info
2491                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column)) {
2492                 GtkWidget *srcwidget;
2493
2494                 if (y > height - 24 && height + vpos < total_height)
2495                         gtk_adjustment_set_value(pos, (vpos+5 > height ? height : vpos+5));
2496
2497                 if (y < 48 && y > 0)
2498                         gtk_adjustment_set_value(pos, (vpos-5 < 0 ? 0 : vpos-5));
2499
2500                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2501                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2502                 src_item = folderview->summaryview->folder_item;
2503
2504                 srcwidget = gtk_drag_get_source_widget(context);
2505                 if (srcwidget == folderview->summaryview->ctree) {
2506                         /* comes from summaryview */
2507                         /* we are copying messages, so only accept folder items that are not
2508                            the source item, are no root items and can copy messages */
2509                         if (item && item->folder && folder_item_parent(item) != NULL && src_item &&
2510                             src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)
2511                                 acceptable = TRUE;
2512                 } else if (srcwidget == folderview->ctree) {
2513                         /* comes from folderview */
2514                         /* we are moving folder items, only accept folders that are not
2515                            the source items and can copy messages and create folder items */
2516                         if (item && item->folder && src_item && src_item != item &&
2517                             FOLDER_CLASS(item->folder)->copy_msg != NULL &&
2518                             FOLDER_CLASS(item->folder)->create_folder != NULL)
2519                                 acceptable = TRUE;
2520                 } else {
2521                         /* comes from another app */
2522                         /* we are adding messages, so only accept folder items that are 
2523                            no root items and can copy messages */
2524                         if (item && item->folder && folder_item_parent(item) != NULL
2525                             && FOLDER_CLASS(item->folder)->add_msg != NULL)
2526                                 acceptable = TRUE;
2527                 }
2528         }
2529
2530         if (acceptable || (src_item && src_item == item))
2531                 drag_state_start(folderview, node, item);
2532         
2533         if (acceptable) {
2534                 g_signal_handlers_block_by_func
2535                         (G_OBJECT(widget),
2536                          G_CALLBACK(folderview_selected), folderview);
2537                 gtk_ctree_select(GTK_CTREE(widget), node);
2538                 g_signal_handlers_unblock_by_func
2539                         (G_OBJECT(widget),
2540                          G_CALLBACK(folderview_selected), folderview);
2541                 gdk_drag_status(context, 
2542                                         (context->actions == GDK_ACTION_COPY ?
2543                                         GDK_ACTION_COPY : GDK_ACTION_MOVE) , time);
2544         } else {
2545                 if (folderview->opened)
2546                         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2547                 gdk_drag_status(context, 0, time);
2548         }
2549
2550         return acceptable;
2551 }
2552
2553 static void folderview_drag_leave_cb(GtkWidget      *widget,
2554                                      GdkDragContext *context,
2555                                      guint           time,
2556                                      FolderView     *folderview)
2557 {
2558         drag_state_stop(folderview);
2559         gtk_ctree_select(GTK_CTREE(widget), folderview->opened);
2560 }
2561
2562 static void free_info (gpointer stuff, gpointer data)
2563 {
2564         g_free(stuff);
2565 }
2566
2567 void folderview_finish_dnd(const gchar *data, GdkDragContext *drag_context,
2568                            guint time, FolderItem *item)
2569 {
2570         GList *list, *tmp;
2571         GSList *msglist = NULL;
2572         list = uri_list_extract_filenames(data);
2573         if (!(item && item->folder && folder_item_parent(item) != NULL
2574                     && FOLDER_CLASS(item->folder)->add_msg != NULL))
2575         {
2576                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2577                 return;
2578         }       
2579         if (!list) {
2580                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2581                 return;
2582         }
2583         for (tmp = list; tmp != NULL; tmp = tmp->next) {
2584                 MsgFileInfo *info = NULL;
2585
2586                 if (file_is_email((gchar *)tmp->data)) {
2587                         info = g_new0(MsgFileInfo, 1);
2588                         info->msginfo = NULL;
2589                         info->file = (gchar *)tmp->data;
2590                         msglist = g_slist_prepend(msglist, info);
2591                 }
2592         }
2593         if (msglist) {
2594                 msglist = g_slist_reverse(msglist);
2595                 folder_item_add_msgs(item, msglist, FALSE);
2596                 g_slist_foreach(msglist, free_info, NULL);
2597                 g_slist_free(msglist);
2598                 gtk_drag_finish(drag_context, TRUE, FALSE, time);
2599         } else {
2600                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2601         }
2602         list_free_strings(list);
2603         g_list_free(list);
2604 }
2605
2606 static void folderview_drag_received_cb(GtkWidget        *widget,
2607                                         GdkDragContext   *drag_context,
2608                                         gint              x,
2609                                         gint              y,
2610                                         GtkSelectionData *data,
2611                                         guint             info,
2612                                         guint             time,
2613                                         FolderView       *folderview)
2614 {
2615         gint row, column;
2616         FolderItem *item = NULL, *src_item;
2617         GtkCTreeNode *node;
2618
2619         if (info == TARGET_DUMMY) {
2620                 drag_state_stop(folderview);
2621                 if ((void *)strstr(data->data, "FROM_OTHER_FOLDER") != (void *)data->data) {
2622                         /* comes from summaryview */
2623                         if (gtk_clist_get_selection_info
2624                                 (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2625                                 return;
2626
2627                         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2628                         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2629                         src_item = folderview->summaryview->folder_item;
2630
2631                         /* re-check (due to acceptable possibly set for folder moves */
2632                         if (!(item && item->folder && item->path && !item->no_select && 
2633                               src_item && src_item != item && FOLDER_CLASS(item->folder)->copy_msg != NULL)) {
2634                                 return;
2635                         }
2636                         if (item && src_item) {
2637                                 switch (drag_context->action) {
2638                                 case GDK_ACTION_COPY:
2639                                         summary_copy_selected_to(folderview->summaryview, item);
2640                                         gtk_drag_finish(drag_context, TRUE, FALSE, time);
2641                                         break;
2642                                 case GDK_ACTION_MOVE:
2643                                 case GDK_ACTION_DEFAULT:
2644                                 default:
2645                                         if (FOLDER_CLASS(src_item->folder)->remove_msg == NULL)
2646                                                 summary_copy_selected_to(folderview->summaryview, item);
2647                                         else
2648                                                 summary_move_selected_to(folderview->summaryview, item);
2649                                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2650                                 }
2651                         } else
2652                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);
2653                 } else {
2654                         /* comes from folderview */
2655                         char *source;
2656                         gboolean folder_is_normal = TRUE;
2657
2658                         source = data->data + 17;
2659                         if (gtk_clist_get_selection_info
2660                             (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0
2661                             || *source == 0) {
2662                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2663                                 return;
2664                         }
2665                         node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2666                         item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2667                         src_item = folder_find_item_from_identifier(source);
2668
2669                         folder_is_normal = 
2670                                 src_item != NULL &&
2671                                 src_item->stype == F_NORMAL &&
2672                                 !folder_has_parent_of_type(src_item, F_OUTBOX) &&
2673                                 !folder_has_parent_of_type(src_item, F_DRAFT) &&
2674                                 !folder_has_parent_of_type(src_item, F_QUEUE) &&
2675                                 !folder_has_parent_of_type(src_item, F_TRASH);
2676                         if (!item || item->no_select || !src_item 
2677                         ||  !folder_is_normal) {
2678                                 gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2679                                 return;
2680                         }
2681
2682                         folderview_move_folder(folderview, src_item, item);
2683                         gtk_drag_finish(drag_context, TRUE, TRUE, time);
2684                 }
2685                 folderview->nodes_to_recollapse = NULL;
2686         } else if (info == TARGET_MAIL_URI_LIST) {
2687                 if (gtk_clist_get_selection_info
2688                         (GTK_CLIST(widget), x - 24, y - 24, &row, &column) == 0)
2689                         return;
2690
2691                 node = gtk_ctree_node_nth(GTK_CTREE(widget), row);
2692                 if (!node) {
2693                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2694                         return;
2695                 }
2696                 item = gtk_ctree_node_get_row_data(GTK_CTREE(widget), node);
2697                 if (!item) {
2698                         gtk_drag_finish(drag_context, FALSE, FALSE, time);                      
2699                         return;
2700                 }
2701                 folderview_finish_dnd(data->data, drag_context, time, item);
2702         }
2703 }
2704
2705 static void folderview_drag_end_cb(GtkWidget        *widget, 
2706                                    GdkDragContext   *drag_context,
2707                                    FolderView       *folderview)
2708 {
2709         drag_state_stop(folderview);
2710         g_slist_free(folderview->nodes_to_recollapse);
2711         folderview->nodes_to_recollapse = NULL;
2712 }
2713
2714 void folderview_register_popup(FolderViewPopup *fpopup)
2715 {
2716         GList *folderviews;
2717
2718         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2719                 FolderView *folderview = folderviews->data;
2720                 GtkItemFactory *factory;
2721
2722                 factory = create_ifactory(folderview, fpopup);
2723                 g_hash_table_insert(folderview->popups, fpopup->klass, factory);
2724         }       
2725         g_hash_table_insert(folderview_popups, fpopup->klass, fpopup);
2726 }
2727
2728 void folderview_unregister_popup(FolderViewPopup *fpopup)
2729 {
2730         GList *folderviews;
2731
2732         for (folderviews = folderview_list; folderviews != NULL; folderviews = g_list_next(folderviews)) {
2733                 FolderView *folderview = folderviews->data;
2734
2735                 g_hash_table_remove(folderview->popups, fpopup->klass);
2736         }       
2737         g_hash_table_remove(folderview_popups, fpopup->klass);
2738 }