c7a83578f636eea9776a9ced7ed1c6afcab01dfd
[claws.git] / src / summaryview.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "defs.h"
20
21 #include <glib.h>
22 #include <glib/gi18n.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtk.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "main.h"
32 #include "menu.h"
33 #include "mbox.h"
34 #include "mainwindow.h"
35 #include "folderview.h"
36 #include "summaryview.h"
37 #include "messageview.h"
38 #include "mimeview.h"
39 #include "foldersel.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "sourcewindow.h"
43 #include "prefs_common.h"
44 #include "prefs_summary_column.h"
45 #include "prefs_summary_open.h"
46 #include "prefs_filtering.h"
47 #include "account.h"
48 #include "compose.h"
49 #include "file-utils.h"
50 #include "utils.h"
51 #include "gtkutils.h"
52 #include "stock_pixmap.h"
53 #include "filesel.h"
54 #include "alertpanel.h"
55 #include "inputdialog.h"
56 #include "statusbar.h"
57 #include "folder.h"
58 #include "colorlabel.h"
59 #include "inc.h"
60 #include "imap.h"
61 #ifndef USE_ALT_ADDRBOOK
62         #include "addressbook.h"
63 #else
64         #include "addressbook-dbus.h"
65         #include "addressadd.h"
66 #endif
67 #include "addr_compl.h"
68 #include "folder_item_prefs.h"
69 #include "filtering.h"
70 #include "string_match.h"
71 #include "toolbar.h"
72 #include "news.h"
73 #include "hooks.h"
74 #include "description_window.h"
75 #include "folderutils.h"
76 #include "quicksearch.h"
77 #include "partial_download.h"
78 #include "tags.h"
79 #include "timing.h"
80 #include "log.h"
81 #include "edittags.h"
82 #include "manual.h"
83 #include "manage_window.h"
84 #include "avatars.h"
85
86 #define SUMMARY_COL_MARK_WIDTH          10
87 #define SUMMARY_COL_STATUS_WIDTH        13
88 #define SUMMARY_COL_LOCKED_WIDTH        13
89 #define SUMMARY_COL_MIME_WIDTH          11
90
91 static int normal_row_height = -1;
92 static GtkStyle *bold_style;
93 static GtkStyle *bold_marked_style;
94 static GtkStyle *bold_deleted_style;
95 static GtkStyle *small_style;
96 static GtkStyle *small_marked_style;
97 static GtkStyle *small_deleted_style;
98
99 static GdkPixbuf *markxpm;
100 static GdkPixbuf *deletedxpm;
101 static GdkPixbuf *movedxpm;
102 static GdkPixbuf *copiedxpm;
103
104 static GdkPixbuf *newxpm;
105 static GdkPixbuf *unreadxpm;
106 static GdkPixbuf *repliedxpm;
107 static GdkPixbuf *forwardedxpm;
108 static GdkPixbuf *repliedandforwardedxpm;
109 static GdkPixbuf *ignorethreadxpm;
110 static GdkPixbuf *watchthreadxpm;
111 static GdkPixbuf *lockedxpm;
112 static GdkPixbuf *spamxpm;
113
114 static GdkPixbuf *clipxpm;
115 static GdkPixbuf *keyxpm;
116 static GdkPixbuf *clipkeyxpm;
117 static GdkPixbuf *keysignxpm;
118 static GdkPixbuf *gpgsignedxpm;
119 static GdkPixbuf *clipgpgsignedxpm;
120
121 static void summary_free_msginfo_func   (GtkCMCTree             *ctree,
122                                          GtkCMCTreeNode         *node,
123                                          gpointer                data);
124 static void summary_set_marks_func      (GtkCMCTree             *ctree,
125                                          GtkCMCTreeNode         *node,
126                                          gpointer                data);
127
128 void  summary_set_menu_sensitive        (SummaryView            *summaryview);
129 guint summary_get_msgnum                (SummaryView            *summaryview,
130                                          GtkCMCTreeNode         *node);
131
132
133 static void summary_set_hide_menu (SummaryView *summaryview,
134                                    const gchar *menu_item,
135                                    guint action);
136
137 static GtkCMCTreeNode *summary_find_prev_msg
138                                         (SummaryView            *summaryview,
139                                          GtkCMCTreeNode         *current_node,
140                                          gboolean                start_from_prev);
141 static GtkCMCTreeNode *summary_find_next_msg
142                                         (SummaryView            *summaryview,
143                                          GtkCMCTreeNode         *current_node,
144                                          gboolean                start_from_next);
145
146 static GtkCMCTreeNode *summary_find_prev_flagged_msg
147                                         (SummaryView    *summaryview,
148                                          GtkCMCTreeNode *current_node,
149                                          MsgPermFlags    flags,
150                                          gboolean        start_from_prev);
151 static GtkCMCTreeNode *summary_find_next_flagged_msg
152                                         (SummaryView    *summaryview,
153                                          GtkCMCTreeNode *current_node,
154                                          MsgPermFlags    flags,
155                                          gboolean        start_from_next);
156
157 static GtkCMCTreeNode *summary_find_msg_by_msgnum
158                                         (SummaryView            *summaryview,
159                                          guint                   msgnum);
160
161 static void summary_update_status       (SummaryView            *summaryview);
162
163 /* display functions */
164 static void summary_status_show         (SummaryView            *summaryview);
165 static void summary_set_column_titles   (SummaryView            *summaryview);
166 static void summary_set_ctree_from_list (SummaryView            *summaryview,
167                                          GSList                 *mlist,
168                                          guint                  selected_msgnum);
169 static inline void summary_set_header   (SummaryView            *summaryview,
170                                          gchar                  *text[],
171                                          MsgInfo                *msginfo);
172 static void summary_display_msg         (SummaryView            *summaryview,
173                                          GtkCMCTreeNode         *row);
174 static void summary_display_msg_full    (SummaryView            *summaryview,
175                                          GtkCMCTreeNode         *row,
176                                          gboolean                new_window,
177                                          gboolean                all_headers);
178 static void summary_set_row_marks       (SummaryView            *summaryview,
179                                          GtkCMCTreeNode         *row);
180
181 static gboolean summary_set_row_tag     (SummaryView            *summaryview, 
182                                          GtkCMCTreeNode                 *row, 
183                                          gboolean                refresh,
184                                          gboolean                set, 
185                                          gint                    id);
186 /* message handling */
187 static void summary_mark_row            (SummaryView            *summaryview,
188                                          GtkCMCTreeNode         *row);
189 static void summary_lock_row            (SummaryView            *summaryview,
190                                          GtkCMCTreeNode         *row);
191 static void summary_unlock_row          (SummaryView            *summaryview,
192                                          GtkCMCTreeNode         *row);
193 static void summary_mark_row_as_read    (SummaryView            *summaryview,
194                                          GtkCMCTreeNode         *row);
195 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
196                                          GtkCMCTreeNode         *row);
197 static gboolean summary_mark_all_read_confirm(gboolean ask_if_needed);
198 static gboolean summary_mark_all_unread_confirm(gboolean ask_if_needed);
199 static void summary_delete_row          (SummaryView            *summaryview,
200                                          GtkCMCTreeNode         *row);
201 static void summary_unmark_row          (SummaryView            *summaryview,
202                                          GtkCMCTreeNode         *row);
203 static void summary_move_row_to         (SummaryView            *summaryview,
204                                          GtkCMCTreeNode         *row,
205                                          FolderItem             *to_folder);
206 static void summary_copy_row_to         (SummaryView            *summaryview,
207                                          GtkCMCTreeNode         *row,
208                                          FolderItem             *to_folder);
209
210 static gint summary_execute_move        (SummaryView            *summaryview);
211 static void summary_execute_move_func   (GtkCMCTree             *ctree,
212                                          GtkCMCTreeNode         *node,
213                                          gpointer                data);
214 static void summary_execute_copy        (SummaryView            *summaryview);
215 static void summary_execute_copy_func   (GtkCMCTree             *ctree,
216                                          GtkCMCTreeNode         *node,
217                                          gpointer                data);
218 static void summary_execute_delete      (SummaryView            *summaryview);
219 static void summary_execute_delete_func (GtkCMCTree             *ctree,
220                                          GtkCMCTreeNode         *node,
221                                          gpointer                data);
222 static void summary_execute_expunge     (SummaryView            *summaryview);
223
224 static void summary_thread_init         (SummaryView            *summaryview);
225
226 static void summary_unthread_for_exec           (SummaryView    *summaryview);
227 static void summary_unthread_for_exec_func      (GtkCMCTree     *ctree,
228                                                  GtkCMCTreeNode *node,
229                                                  gpointer        data);
230
231 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
232                               GSList * mlist);
233
234 static void summary_filter_func         (MsgInfo                *msginfo,
235                                          PrefsAccount           *ac_prefs);
236
237 static void summary_colorlabel_menu_item_activate_cb
238                                           (GtkWidget    *widget,
239                                            gpointer      data);
240 static void summary_colorlabel_menu_item_activate_item_cb
241                                           (GtkMenuItem  *label_menu_item,
242                                            gpointer      data);
243 static void summary_colorlabel_menu_create(SummaryView  *summaryview,
244                                            gboolean  refresh);
245 static void summary_tags_menu_item_activate_cb
246                                           (GtkWidget    *widget,
247                                            gpointer      data);
248 static void summary_tags_menu_item_activate_item_cb
249                                           (GtkMenuItem  *label_menu_item,
250                                            gpointer      data);
251 static void summary_tags_menu_create(SummaryView        *summaryview,
252                                            gboolean  refresh);
253
254 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
255
256 /* callback functions */
257 static gint summary_toggle_pressed      (GtkWidget              *eventbox,
258                                          GdkEventButton         *event,
259                                          SummaryView            *summaryview);
260 #ifdef GENERIC_UMPC
261 static void summary_toggle_multiple_pressed
262                                         (GtkWidget              *widget,
263                                          SummaryView            *summaryview);
264 #endif
265 static gint summary_folder_eventbox_pressed     
266                                         (GtkWidget              *eventbox,
267                                          GdkEventButton         *event,
268                                          SummaryView            *summaryview);
269 static gboolean summary_button_pressed  (GtkWidget              *ctree,
270                                          GdkEventButton         *event,
271                                          SummaryView            *summaryview);
272 static gboolean summary_button_released (GtkWidget              *ctree,
273                                          GdkEventButton         *event,
274                                          SummaryView            *summaryview);
275 static gboolean summary_key_pressed     (GtkWidget              *ctree,
276                                          GdkEventKey            *event,
277                                          SummaryView            *summaryview);
278 static void summary_tree_expanded       (GtkCMCTree             *ctree,
279                                          GtkCMCTreeNode         *node,
280                                          SummaryView            *summaryview);
281 static void summary_tree_collapsed      (GtkCMCTree             *ctree,
282                                          GtkCMCTreeNode         *node,
283                                          SummaryView            *summaryview);
284 static void summary_selected            (GtkCMCTree             *ctree,
285                                          GtkCMCTreeNode         *row,
286                                          gint                    column,
287                                          SummaryView            *summaryview);
288 static void summary_unselected          (GtkCMCTree             *ctree,
289                                          GtkCMCTreeNode         *row,
290                                          gint                    column,
291                                          SummaryView            *summaryview);
292 static void summary_col_resized         (GtkCMCList             *clist,
293                                          gint                    column,
294                                          gint                    width,
295                                          SummaryView            *summaryview);
296 static void summary_mark_clicked        (GtkWidget              *button,
297                                          SummaryView            *summaryview);
298 static void summary_status_clicked      (GtkWidget              *button,
299                                          SummaryView            *summaryview);
300 static void summary_mime_clicked        (GtkWidget              *button,
301                                          SummaryView            *summaryview);
302 static void summary_num_clicked         (GtkWidget              *button,
303                                          SummaryView            *summaryview);
304 static void summary_score_clicked       (GtkWidget *button,
305                                          SummaryView *summaryview);
306 static void summary_size_clicked        (GtkWidget              *button,
307                                          SummaryView            *summaryview);
308 static void summary_date_clicked        (GtkWidget              *button,
309                                          SummaryView            *summaryview);
310 static void summary_from_clicked        (GtkWidget              *button,
311                                          SummaryView            *summaryview);
312 static void summary_to_clicked          (GtkWidget              *button,
313                                          SummaryView            *summaryview);
314 static void summary_subject_clicked     (GtkWidget              *button,
315                                          SummaryView            *summaryview);
316 static void summary_score_clicked       (GtkWidget              *button,
317                                          SummaryView            *summaryview);
318 static void summary_locked_clicked      (GtkWidget              *button,
319                                          SummaryView            *summaryview);
320 static void summary_tags_clicked        (GtkWidget              *button,
321                                          SummaryView            *summaryview);
322
323 static void summary_start_drag          (GtkWidget        *widget, 
324                                          int button,
325                                          GdkEvent *event,
326                                          SummaryView      *summaryview);
327 static void summary_drag_data_get       (GtkWidget        *widget,
328                                          GdkDragContext   *drag_context,
329                                          GtkSelectionData *selection_data,
330                                          guint             info,
331                                          guint             time,
332                                          SummaryView      *summaryview);
333 static void summary_drag_data_received(GtkWidget        *widget,
334                                         GdkDragContext   *drag_context,
335                                         gint              x,
336                                         gint              y,
337                                         GtkSelectionData *data,
338                                         guint             info,
339                                         guint             time,
340                                         SummaryView       *summaryview);
341 static gboolean summary_drag_motion_cb(GtkWidget      *widget,
342                                           GdkDragContext *context,
343                                           gint            x,
344                                           gint            y,
345                                           guint           time,
346                                           SummaryView    *summaryview);
347 static void summary_drag_end(GtkWidget *widget,
348                                           GdkDragContext *drag_context,
349                                           SummaryView    *summaryview);
350
351 /* custom compare functions for sorting */
352 static gint summary_cmp_by_mark         (GtkCMCList             *clist,
353                                          gconstpointer           ptr1,
354                                          gconstpointer           ptr2);
355 static gint summary_cmp_by_status       (GtkCMCList             *clist,
356                                          gconstpointer           ptr1,
357                                          gconstpointer           ptr2);
358 static gint summary_cmp_by_mime         (GtkCMCList             *clist,
359                                          gconstpointer           ptr1,
360                                          gconstpointer           ptr2);
361 static gint summary_cmp_by_num          (GtkCMCList             *clist,
362                                          gconstpointer           ptr1,
363                                          gconstpointer           ptr2);
364 static gint summary_cmp_by_size         (GtkCMCList             *clist,
365                                          gconstpointer           ptr1,
366                                          gconstpointer           ptr2);
367 static gint summary_cmp_by_date         (GtkCMCList             *clist,
368                                          gconstpointer           ptr1,
369                                          gconstpointer           ptr2);
370 static gint summary_cmp_by_thread_date  (GtkCMCList             *clist,
371                                          gconstpointer           ptr1,
372                                          gconstpointer           ptr2);
373 static gint summary_cmp_by_from         (GtkCMCList             *clist,
374                                          gconstpointer           ptr1,
375                                          gconstpointer           ptr2);
376 static gint summary_cmp_by_simplified_subject
377                                         (GtkCMCList             *clist, 
378                                          gconstpointer           ptr1, 
379                                          gconstpointer           ptr2);
380 static gint summary_cmp_by_score        (GtkCMCList             *clist,
381                                          gconstpointer           ptr1,
382                                          gconstpointer           ptr2);
383 static gint summary_cmp_by_label        (GtkCMCList             *clist,
384                                          gconstpointer           ptr1,
385                                          gconstpointer           ptr2);
386 static gint summary_cmp_by_to           (GtkCMCList             *clist,
387                                          gconstpointer           ptr1,
388                                          gconstpointer           ptr2);
389 static gint summary_cmp_by_subject      (GtkCMCList             *clist,
390                                          gconstpointer           ptr1,
391                                          gconstpointer           ptr2);
392 static gint summary_cmp_by_locked       (GtkCMCList             *clist,
393                                          gconstpointer           ptr1, 
394                                          gconstpointer           ptr2);
395 static gint summary_cmp_by_tags         (GtkCMCList             *clist,
396                                          gconstpointer           ptr1, 
397                                          gconstpointer           ptr2);
398
399 static void quicksearch_execute_cb      (QuickSearch    *quicksearch,
400                                          gpointer        data);
401
402 static void tog_searchbar_cb            (GtkWidget      *w,
403                                          gpointer        data);
404
405 static void summary_find_answers        (SummaryView    *summaryview, 
406                                          MsgInfo        *msg);
407
408 static gboolean summary_update_msg      (gpointer source, gpointer data);
409 static gboolean summary_update_folder_item_hook(gpointer source, gpointer data);
410 static gboolean summary_update_folder_hook(gpointer source, gpointer data);
411 static void summary_set_colorlabel_color (GtkCMCTree            *ctree,
412                                    GtkCMCTreeNode               *node,
413                                    guint                 labelcolor);
414 static void summary_thread_build(SummaryView *summaryview);
415
416 GtkTargetEntry summary_drag_types[3] =
417 {
418         {"text/uri-list", 0, TARGET_MAIL_URI_LIST},
419         {"claws-mail/internal", GTK_TARGET_SAME_APP, TARGET_DUMMY},
420         {"claws-mail/msg-path-list", 0, TARGET_MAIL_CM_PATH_LIST},
421 };
422
423 static void summary_reply_cb(GtkAction *gaction, gpointer data);
424
425 /* Only submenus and specifically-handled menu entries here */
426 static GtkActionEntry summary_popup_entries[] =
427 {
428         {"SummaryViewPopup",                      NULL, "SummaryViewPopup", NULL, NULL, NULL },
429         {"SummaryViewPopup/Reply",                NULL, N_("_Reply"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_REPLY */
430         {"SummaryViewPopup/ReplyTo",              NULL, N_("Repl_y to"), NULL, NULL, NULL },
431         {"SummaryViewPopup/ReplyTo/All",          NULL, N_("_All"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_REPLY_TO_ALL */
432         {"SummaryViewPopup/ReplyTo/Sender",       NULL, N_("_Sender"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_REPLY_TO_SENDER */
433         {"SummaryViewPopup/ReplyTo/List",         NULL, N_("Mailing _list"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_REPLY_TO_LIST */
434
435         {"SummaryViewPopup/Forward",              NULL, N_("_Forward"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_FORWARD_INLINE */
436         {"SummaryViewPopup/ForwardAtt",           NULL, N_("For_ward as attachment"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_FORWARD_AS_ATTACH */
437         {"SummaryViewPopup/Redirect",             NULL, N_("Redirec_t"), NULL, NULL, G_CALLBACK(summary_reply_cb) }, /* COMPOSE_REDIRECT */
438         {"SummaryViewPopup/Mark",                 NULL, N_("_Mark"), NULL, NULL, NULL },
439         {"SummaryViewPopup/ColorLabel",           NULL, N_("Color la_bel"), NULL, NULL, NULL },
440         {"SummaryViewPopup/Tags",                 NULL, N_("Ta_gs"), NULL, NULL, NULL },
441         {"SummaryViewPopup/CreateFilterRule",     NULL, N_("Create _filter rule"), NULL, NULL, NULL },
442 #ifndef GENERIC_UMPC
443         {"SummaryViewPopup/CreateProcessingRule", NULL, N_("Create processing rule"), NULL, NULL, NULL },
444 #endif
445         {"SummaryViewPopup/View",                 NULL, N_("_View"), NULL, NULL, NULL },
446 };
447
448 static void summary_header_lock_sorting_cb(GtkAction *gaction, gpointer data);
449 static void summary_header_set_displayed_columns_cb(GtkAction *gaction, gpointer data);
450
451 static GtkActionEntry summary_header_popup_entries[] =
452 {
453         {"SummaryViewHeaderPopup",                     NULL, "SummaryViewHeaderPopup", NULL, NULL, NULL },
454         {"SummaryViewHeaderPopup/SetDisplayedColumns", NULL, N_("_Set displayed columns"), NULL, NULL, G_CALLBACK(summary_header_set_displayed_columns_cb) }
455 };
456
457 static GtkToggleActionEntry summary_header_popup_toggle_entries[] =
458 {
459         {"SummaryViewHeaderPopup/LockColumnHeaders",     NULL, N_("_Lock column headers"), NULL, NULL, G_CALLBACK(summary_header_lock_sorting_cb), FALSE }
460 };
461
462 static const gchar *const col_label[N_SUMMARY_COLS] = {
463         "",            /* S_COL_MARK    */
464         N_("S"),       /* S_COL_STATUS  */
465         "",            /* S_COL_MIME    */
466         N_("Subject"), /* S_COL_SUBJECT */
467         N_("From"),    /* S_COL_FROM    */
468         N_("To"),      /* S_COL_TO      */
469         N_("Date"),    /* S_COL_DATE    */
470         N_("Size"),    /* S_COL_SIZE    */
471         N_("#"),       /* S_COL_NUMBER  */
472         N_("Score"),   /* S_COL_SCORE   */
473         "",            /* S_COL_LOCKED  */
474         N_("Tags"),    /* S_COL_TAGS    */
475 };
476
477 void summary_freeze(SummaryView *summaryview)
478 {
479         if (summaryview)
480                 gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
481 }
482
483 void summary_thaw(SummaryView *summaryview)
484 {
485         if (summaryview)
486                 gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
487 }
488
489 void summary_thaw_with_status(SummaryView *summaryview)
490 {
491         if (summaryview) {
492                 summary_status_show(summaryview);
493                 gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
494         }
495 }
496
497 void summary_grab_focus(SummaryView *summaryview)
498 {
499         if (summaryview)
500                 gtk_widget_grab_focus(summaryview->ctree);
501 }
502
503 GtkWidget *summary_get_main_widget(SummaryView *summaryview)
504 {
505         if (summaryview)
506                 return summaryview->ctree;
507         else
508                 return NULL;
509 }
510
511 #define START_LONG_OPERATION(summaryview,force_freeze) {        \
512         summary_lock(summaryview);                              \
513         main_window_cursor_wait(summaryview->mainwin);          \
514         if (force_freeze || sc_g_list_bigger(GTK_CMCLIST(summaryview->ctree)->selection, 1)) {\
515                 froze = TRUE;                                           \
516                 summary_freeze(summaryview);    \
517         }                                                       \
518         folder_item_update_freeze();                            \
519         inc_lock();                                             \
520         hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,          \
521                       summaryview->msginfo_update_callback_id); \
522 }
523 #define END_LONG_OPERATION(summaryview) {                       \
524         inc_unlock();                                           \
525         folder_item_update_thaw();                              \
526         if (froze)                                              \
527                 summary_thaw(summaryview);      \
528         main_window_cursor_normal(summaryview->mainwin);        \
529         summary_unlock(summaryview);                            \
530         summaryview->msginfo_update_callback_id =               \
531                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST,    \
532                 summary_update_msg, (gpointer) summaryview);    \
533 }
534
535 static void popup_menu_selection_done(GtkMenuShell *shell, gpointer user_data)
536 {
537         SummaryView *summaryview = (SummaryView *)user_data;
538
539         cm_return_if_fail(summaryview != NULL);
540
541         /* If a message is displayed, place cursor back on the message. */
542         if (summaryview->displayed != NULL &&
543                         summaryview->displayed != summaryview->selected) {
544                 gtk_sctree_select(GTK_SCTREE(summaryview->ctree), summaryview->displayed);
545         }
546 }
547
548 SummaryView *summary_create(MainWindow *mainwin)
549 {
550         SummaryView *summaryview;
551         GtkWidget *vbox;
552         GtkWidget *scrolledwin;
553         GtkWidget *ctree;
554         GtkWidget *hbox;
555         GtkWidget *hbox_l;
556         GtkWidget *stat_box;
557         GtkWidget *stat_box2;
558         GtkWidget *stat_vbox;
559         GtkWidget *statlabel_folder;
560         GtkWidget *statlabel_select;
561         GtkWidget *statlabel_msgs;
562         GtkWidget *hbox_spc;
563         GtkWidget *toggle_eventbox;
564 #ifdef GENERIC_UMPC
565         GtkWidget *multiple_sel_togbtn;
566 #endif
567         GtkWidget *toggle_arrow;
568         GtkWidget *toggle_search;
569         QuickSearch *quicksearch;
570
571         debug_print("Creating summary view...\n");
572         summaryview = g_new0(SummaryView, 1);
573
574 #define SUMMARY_VBOX_SPACING 3
575         vbox = gtk_vbox_new(FALSE, SUMMARY_VBOX_SPACING);
576         
577         /* create status label */
578         hbox = gtk_hbox_new(FALSE, 0);
579         gtk_widget_show(hbox);
580
581         stat_vbox = gtk_vbox_new(FALSE, 0);
582         gtk_widget_show(stat_vbox);
583
584         stat_box = gtk_hbox_new(FALSE, 0);
585         gtk_widget_show(stat_box);
586         
587         stat_box2 = gtk_hbox_new(FALSE, 0);
588         gtk_widget_show(stat_box2);
589         
590         toggle_search = gtk_toggle_button_new();
591         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_search),
592                                      prefs_common.show_searchbar);
593         gtk_widget_set_can_focus(toggle_search, FALSE);
594         gtk_widget_show(toggle_search);
595
596         CLAWS_SET_TIP(toggle_search, _("Toggle quick search bar"));
597         
598         gtk_box_pack_start(GTK_BOX(hbox), toggle_search, FALSE, FALSE, 2);      
599
600         gtk_box_pack_start(GTK_BOX(hbox), stat_vbox, TRUE, TRUE, 0);    
601         gtk_box_pack_start(GTK_BOX(stat_vbox), stat_box, TRUE, TRUE, 0);        
602         gtk_box_pack_start(GTK_BOX(stat_vbox), stat_box2, TRUE, TRUE, 0);       
603
604         hbox_l = gtk_hbox_new(FALSE, 0);
605         gtk_widget_show(hbox_l);
606         gtk_box_pack_start(GTK_BOX(stat_box), hbox_l, TRUE, TRUE, 0);
607  
608         statlabel_folder = gtk_label_new("");
609         gtk_widget_show(statlabel_folder);
610         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
611         statlabel_select = gtk_label_new("");
612         gtk_widget_show(statlabel_select);
613         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
614  
615         /* toggle view button */
616         toggle_eventbox = gtk_event_box_new();
617         gtk_widget_show(toggle_eventbox);
618         
619         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
620
621         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
622         gtk_widget_show(toggle_arrow);
623         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
624         g_signal_connect(G_OBJECT(toggle_eventbox), "button_press_event",
625                          G_CALLBACK(summary_toggle_pressed),
626                          summaryview);
627
628 #ifdef GENERIC_UMPC
629         multiple_sel_togbtn = gtk_toggle_button_new();
630         gtk_widget_show(multiple_sel_togbtn);
631         gtk_box_pack_end(GTK_BOX(hbox), multiple_sel_togbtn, FALSE, FALSE, 4);
632         CLAWS_SET_TIP(multiple_sel_togbtn,
633                              _("Toggle multiple selection"));
634         g_signal_connect(G_OBJECT(multiple_sel_togbtn), "toggled",
635                          G_CALLBACK(summary_toggle_multiple_pressed),
636                          summaryview);
637 #endif
638         
639         statlabel_msgs = gtk_label_new("");
640         gtk_widget_show(statlabel_msgs);
641         gtk_box_pack_end(GTK_BOX(stat_box), statlabel_msgs, FALSE, FALSE, 4);
642
643         hbox_spc = gtk_hbox_new(FALSE, 0);
644         gtk_widget_show(hbox_spc);
645         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
646
647         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
648         gtk_widget_show(scrolledwin);
649         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
650                                        GTK_POLICY_AUTOMATIC,
651                                        GTK_POLICY_AUTOMATIC);
652         summaryview->mainwidget_book = gtk_notebook_new();
653         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(summaryview->mainwidget_book), FALSE);
654         gtk_notebook_set_show_border(GTK_NOTEBOOK(summaryview->mainwidget_book), FALSE);
655 #ifndef GENERIC_UMPC
656         gtk_container_add(GTK_CONTAINER(summaryview->mainwidget_book),
657                 scrolledwin);
658         gtk_box_pack_start(GTK_BOX(vbox), summaryview->mainwidget_book, TRUE, TRUE, 0);
659 #endif
660         gtk_widget_set_size_request(vbox,
661                              prefs_common.summaryview_width,
662                              prefs_common.summaryview_height);
663
664         ctree = summary_ctree_create(summaryview);
665         gtk_widget_show(ctree);
666
667         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
668                                             GTK_CMCLIST(ctree)->hadjustment);
669         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
670                                             GTK_CMCLIST(ctree)->vadjustment);
671         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
672
673         /* status label */
674         gtk_widget_show_all(stat_vbox);
675         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
676
677         /* quick search */
678         quicksearch = quicksearch_new();
679         gtk_box_pack_start(GTK_BOX(vbox), quicksearch_get_widget(quicksearch), FALSE, FALSE, 0);
680
681 #ifdef GENERIC_UMPC
682         gtk_container_add(GTK_CONTAINER(summaryview->mainwidget_book),
683                 scrolledwin);
684         gtk_box_pack_start(GTK_BOX(vbox), summaryview->mainwidget_book, TRUE, TRUE, 0);
685 #endif
686         quicksearch_set_execute_callback(quicksearch, quicksearch_execute_cb, summaryview);
687
688         g_signal_connect (G_OBJECT(toggle_search), "toggled",
689                           G_CALLBACK(tog_searchbar_cb), summaryview);
690
691         /* create popup menu */
692         
693         gtk_action_group_add_actions(mainwin->action_group,
694                         summary_popup_entries,
695                         G_N_ELEMENTS(summary_popup_entries),
696                         (gpointer)summaryview);
697
698         gtk_action_group_add_actions(mainwin->action_group,
699                         summary_header_popup_entries,
700                         G_N_ELEMENTS(summary_header_popup_entries),
701                         (gpointer)summaryview);
702
703         gtk_action_group_add_toggle_actions(mainwin->action_group,
704                         summary_header_popup_toggle_entries,
705                         G_N_ELEMENTS(summary_header_popup_toggle_entries),
706                         (gpointer)summaryview);
707
708         summaryview->ui_manager = gtk_ui_manager_new();
709         summaryview->action_group = cm_menu_create_action_group_full(summaryview->ui_manager,"Menu", summary_popup_entries,
710                         G_N_ELEMENTS(summary_popup_entries), (gpointer)summaryview);
711
712         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus", "SummaryViewPopup", "SummaryViewPopup", GTK_UI_MANAGER_MENU)
713         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Reply", "SummaryViewPopup/Reply", GTK_UI_MANAGER_MENUITEM)
714 #ifndef GENERIC_UMPC
715         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ReplyTo", "SummaryViewPopup/ReplyTo", GTK_UI_MANAGER_MENU)
716         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator1", "Message/---", GTK_UI_MANAGER_SEPARATOR)
717 #endif
718         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Forward", "SummaryViewPopup/Forward", GTK_UI_MANAGER_MENUITEM)
719 #ifndef GENERIC_UMPC
720         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ForwardAtt", "SummaryViewPopup/ForwardAtt", GTK_UI_MANAGER_MENUITEM)
721         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Redirect", "SummaryViewPopup/Redirect", GTK_UI_MANAGER_MENUITEM)
722 #endif
723         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator2", "Message/---", GTK_UI_MANAGER_SEPARATOR)
724         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Move", "Message/Move", GTK_UI_MANAGER_MENUITEM)
725         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Copy", "Message/Copy", GTK_UI_MANAGER_MENUITEM)
726         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Trash", "Message/Trash", GTK_UI_MANAGER_MENUITEM)
727 #ifndef GENERIC_UMPC
728         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Delete", "Message/Delete", GTK_UI_MANAGER_MENUITEM)
729 #endif
730         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator3", "Message/---", GTK_UI_MANAGER_SEPARATOR)
731         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Mark", "SummaryViewPopup/Mark", GTK_UI_MANAGER_MENU)
732         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ColorLabel", "SummaryViewPopup/ColorLabel", GTK_UI_MANAGER_MENU)
733         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Tags", "SummaryViewPopup/Tags", GTK_UI_MANAGER_MENU)
734
735         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator4", "Message/---", GTK_UI_MANAGER_SEPARATOR)
736 #ifndef GENERIC_UMPC
737         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "AddSenderToAB", "Tools/AddSenderToAB", GTK_UI_MANAGER_MENUITEM)
738 #endif
739         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "CreateFilterRule", "SummaryViewPopup/CreateFilterRule", GTK_UI_MANAGER_MENU)
740 #ifndef GENERIC_UMPC
741         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "CreateProcessingRule", "SummaryViewPopup/CreateProcessingRule", GTK_UI_MANAGER_MENU)
742 #endif
743         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator5", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
744         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "View", "SummaryViewPopup/View", GTK_UI_MANAGER_MENU)
745         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "SaveAs", "File/SaveAs", GTK_UI_MANAGER_MENUITEM)
746 #ifndef GENERIC_UMPC
747         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Print", "File/Print", GTK_UI_MANAGER_MENUITEM)
748 #endif
749         /* last separator, for plugins */
750         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator6", "File/---", GTK_UI_MANAGER_SEPARATOR)
751
752         /* submenus - replyto */
753         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/ReplyTo", "All", "SummaryViewPopup/ReplyTo/All", GTK_UI_MANAGER_MENUITEM)
754         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/ReplyTo", "Sender", "SummaryViewPopup/ReplyTo/Sender", GTK_UI_MANAGER_MENUITEM)
755         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/ReplyTo", "MailingList", "SummaryViewPopup/ReplyTo/List", GTK_UI_MANAGER_MENUITEM)
756
757         /* submenus - mark */
758         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Mark", "Message/Mark/Mark", GTK_UI_MANAGER_MENUITEM)
759         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Unmark", "Message/Mark/Unmark", GTK_UI_MANAGER_MENUITEM)
760         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator1", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
761         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkRead", "Message/Mark/MarkRead", GTK_UI_MANAGER_MENUITEM)
762         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkUnread", "Message/Mark/MarkUnread", GTK_UI_MANAGER_MENUITEM)
763         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator2", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
764         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkAllRead", "Message/Mark/MarkAllRead", GTK_UI_MANAGER_MENUITEM)
765         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkAllUnread", "Message/Mark/MarkAllUnread", GTK_UI_MANAGER_MENUITEM)
766         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator3", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
767         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "IgnoreThread", "Message/Mark/IgnoreThread", GTK_UI_MANAGER_MENUITEM)
768         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "UnignoreThread", "Message/Mark/UnignoreThread", GTK_UI_MANAGER_MENUITEM)
769         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "WatchThread", "Message/Mark/WatchThread", GTK_UI_MANAGER_MENUITEM)
770         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "UnwatchThread", "Message/Mark/UnwatchThread", GTK_UI_MANAGER_MENUITEM)
771         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator4", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
772         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkSpam", "Message/Mark/MarkSpam", GTK_UI_MANAGER_MENUITEM)
773         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkHam", "Message/Mark/MarkHam", GTK_UI_MANAGER_MENUITEM)
774         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator5", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
775         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Lock", "Message/Mark/Lock", GTK_UI_MANAGER_MENUITEM)
776         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Unlock", "Message/Mark/Unlock", GTK_UI_MANAGER_MENUITEM)
777
778         /* submenus - colorlabel and tags are dynamic */
779         /* submenus - createfilterrule */
780         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "Automatically", "Tools/CreateFilterRule/Automatically", GTK_UI_MANAGER_MENUITEM)
781         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "ByFrom", "Tools/CreateFilterRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
782         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "ByTo", "Tools/CreateFilterRule/ByTo", GTK_UI_MANAGER_MENUITEM)
783         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "BySubject", "Tools/CreateFilterRule/BySubject", GTK_UI_MANAGER_MENUITEM)
784                 
785 #ifndef GENERIC_UMPC
786         /* submenus - createprocessingrule */
787         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "Automatically", "Tools/CreateProcessingRule/Automatically", GTK_UI_MANAGER_MENUITEM)
788         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "ByFrom", "Tools/CreateProcessingRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
789         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "ByTo", "Tools/CreateProcessingRule/ByTo", GTK_UI_MANAGER_MENUITEM)
790         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "BySubject", "Tools/CreateProcessingRule/BySubject", GTK_UI_MANAGER_MENUITEM)
791 #endif
792                 
793         /* submenus - view */
794         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/View", "OpenNewWindow", "View/OpenNewWindow", GTK_UI_MANAGER_MENUITEM)
795         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/View", "MessageSource", "View/MessageSource", GTK_UI_MANAGER_MENUITEM)
796 #ifndef GENERIC_UMPC
797         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/View", "AllHeaders", "View/AllHeaders", GTK_UI_MANAGER_MENUITEM)
798 #endif          
799
800         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus", "SummaryViewHeaderPopup", "SummaryViewHeaderPopup", GTK_UI_MANAGER_MENU)
801         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewHeaderPopup", "LockColumnHeaders", "SummaryViewHeaderPopup/LockColumnHeaders", GTK_UI_MANAGER_MENUITEM)
802         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewHeaderPopup", "SetDisplayedColumns", "SummaryViewHeaderPopup/SetDisplayedColumns", GTK_UI_MANAGER_MENUITEM)
803
804         summaryview->popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
805                                 gtk_ui_manager_get_widget(mainwin->ui_manager, "/Menus/SummaryViewPopup")) );
806         summaryview->headerpopupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
807                                 gtk_ui_manager_get_widget(mainwin->ui_manager, "/Menus/SummaryViewHeaderPopup")) );
808
809         summaryview->vbox = vbox;
810         summaryview->scrolledwin = scrolledwin;
811         summaryview->ctree = ctree;
812         summaryview->hbox = hbox;
813         summaryview->hbox_l = hbox_l;
814         summaryview->hbox_spc = hbox_spc;
815         summaryview->stat_box = stat_box;
816         summaryview->stat_box2 = stat_box2;
817         summaryview->statlabel_folder = statlabel_folder;
818         summaryview->statlabel_select = statlabel_select;
819         summaryview->statlabel_msgs = statlabel_msgs;
820         summaryview->toggle_eventbox = toggle_eventbox;
821         summaryview->toggle_arrow = toggle_arrow;
822 #ifdef GENERIC_UMPC
823         summaryview->multiple_sel_togbtn = multiple_sel_togbtn;
824 #endif
825         summaryview->toggle_search = toggle_search;
826         summaryview->lock_count = 0;
827         summaryview->msginfo_update_callback_id =
828                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
829         summaryview->folder_item_update_callback_id =
830                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST,
831                                 summary_update_folder_item_hook,
832                                 (gpointer) summaryview);
833         summaryview->folder_update_callback_id =
834                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
835                                 summary_update_folder_hook,
836                                 (gpointer) summaryview);
837
838         summaryview->target_list = gtk_target_list_new(summary_drag_types, 3);
839
840         summaryview->quicksearch = quicksearch;
841
842         /* CLAWS: need this to get the SummaryView * from
843          * the CList */
844         g_object_set_data(G_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
845
846         gtk_widget_show_all(vbox);
847
848         gtk_widget_show(vbox);
849
850         if (prefs_common.show_searchbar)
851                 quicksearch_show(quicksearch);
852         else
853                 quicksearch_hide(quicksearch);
854         
855         if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT ||
856             prefs_common.layout_mode == SMALL_LAYOUT)
857                 gtk_widget_hide(summaryview->toggle_eventbox);
858
859         return summaryview;
860 }
861
862 void summary_relayout(SummaryView *summaryview)
863 {
864         gtk_widget_realize(summaryview->stat_box);
865
866         g_object_ref(summaryview->hbox_l);
867         g_object_ref(summaryview->statlabel_msgs);
868         
869         gtkut_container_remove(GTK_CONTAINER(
870                 gtk_widget_get_parent(summaryview->hbox_l)), summaryview->hbox_l);
871         gtkut_container_remove(GTK_CONTAINER(
872                 gtk_widget_get_parent(summaryview->statlabel_msgs)), summaryview->statlabel_msgs);
873
874         switch (prefs_common.layout_mode) {
875         case NORMAL_LAYOUT:
876         case WIDE_LAYOUT:
877         case WIDE_MSGLIST_LAYOUT:
878                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
879                 gtk_box_pack_end(GTK_BOX(summaryview->stat_box), summaryview->statlabel_msgs, FALSE, FALSE, 4);
880                 gtk_widget_show_all(summaryview->stat_box);
881                 gtk_widget_show_all(summaryview->stat_box2);
882                 if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT ||
883                     prefs_common.layout_mode == SMALL_LAYOUT)
884                         gtk_widget_hide(summaryview->toggle_eventbox);
885                 else
886                         gtk_widget_show(summaryview->toggle_eventbox);
887                 break;
888         case VERTICAL_LAYOUT:
889         case SMALL_LAYOUT:
890                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
891                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box2), summaryview->statlabel_msgs, FALSE, FALSE, 4);
892                 gtk_widget_show_all(summaryview->stat_box);
893                 gtk_widget_show_all(summaryview->stat_box2);
894                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
895                         gtk_widget_hide(summaryview->toggle_eventbox);
896                         gtk_widget_hide(summaryview->statlabel_msgs);
897                 } else {
898                         gtk_widget_show(summaryview->toggle_eventbox);
899                         gtk_widget_show(summaryview->statlabel_msgs);
900                 }
901                         
902                 break;
903         }
904         summary_set_column_order(summaryview);
905
906         g_object_unref(summaryview->hbox_l);
907         g_object_unref(summaryview->statlabel_msgs);
908         quicksearch_relayout(summaryview->quicksearch);
909         if (prefs_common.show_searchbar)
910                 quicksearch_show(summaryview->quicksearch);
911         else
912                 quicksearch_hide(summaryview->quicksearch);
913 }
914
915 static void summary_set_fonts(SummaryView *summaryview)
916 {
917         PangoFontDescription *font_desc;
918         gint size;
919
920         font_desc = pango_font_description_from_string(NORMAL_FONT);
921         if (font_desc) {
922                 gtk_widget_modify_font(summaryview->ctree, font_desc);
923                 pango_font_description_free(font_desc);
924         }
925
926         if (!bold_style) {
927                 bold_style = gtk_style_copy
928                         (gtk_widget_get_style(summaryview->ctree));
929
930                 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
931                         font_desc = pango_font_description_from_string(NORMAL_FONT);
932                         if (font_desc) {
933                                 pango_font_description_free(bold_style->font_desc);
934                                 bold_style->font_desc = font_desc;
935                         }
936                         pango_font_description_set_weight
937                                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
938                 } else {
939                         font_desc = pango_font_description_from_string(BOLD_FONT);
940                         if (font_desc) {
941                                 pango_font_description_free(bold_style->font_desc);
942                                 bold_style->font_desc = font_desc;
943                         }
944                 }
945                 bold_marked_style = gtk_style_copy(bold_style);
946                 bold_marked_style->text[GTK_STATE_NORMAL] =
947                         summaryview->color_marked;
948                 bold_deleted_style = gtk_style_copy(bold_style);
949                 bold_deleted_style->text[GTK_STATE_NORMAL] =
950                         summaryview->color_dim;
951         }
952
953         if (prefs_common.derive_from_normal_font || !SMALL_FONT) {
954                 font_desc = pango_font_description_new();
955                 size = pango_font_description_get_size
956                         (gtk_widget_get_style(summaryview->ctree)->font_desc);
957                 pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
958         } else {
959                 font_desc = pango_font_description_from_string(SMALL_FONT);
960         }
961         if (font_desc) {
962                 gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
963                 gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
964                 gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
965                 pango_font_description_free(font_desc);
966         }
967
968 }
969
970 static void summary_set_folder_pixmap(SummaryView *summaryview, StockPixmap icon)
971 {
972         GtkWidget *pixmap; 
973         if (!summaryview->folder_pixmap_eventbox) {
974                 summaryview->folder_pixmap_eventbox = gtk_event_box_new();
975                 gtk_widget_show(summaryview->folder_pixmap_eventbox);
976                 gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), summaryview->folder_pixmap_eventbox, FALSE, FALSE, 4);
977                 gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), summaryview->folder_pixmap_eventbox, 0); /* search_toggle before */
978                 g_signal_connect(G_OBJECT(summaryview->folder_pixmap_eventbox), "button_press_event",
979                          G_CALLBACK(summary_folder_eventbox_pressed),
980                          summaryview);
981         }
982         if (summaryview->folder_pixmap)
983                 gtk_widget_destroy(summaryview->folder_pixmap);
984
985         pixmap = stock_pixmap_widget(icon);
986         gtk_container_add(GTK_CONTAINER(summaryview->folder_pixmap_eventbox), pixmap);
987         gtk_widget_show(pixmap);
988         summaryview->folder_pixmap = pixmap; 
989 }
990
991 void summary_init(SummaryView *summaryview)
992 {
993         GtkWidget *pixmap;
994
995         gtk_widget_realize(summaryview->ctree);
996         stock_pixbuf_gdk(STOCK_PIXMAP_MARK, &markxpm);
997         stock_pixbuf_gdk(STOCK_PIXMAP_DELETED, &deletedxpm);
998         stock_pixbuf_gdk(STOCK_PIXMAP_NEW, &newxpm);
999         stock_pixbuf_gdk(STOCK_PIXMAP_UNREAD, &unreadxpm);
1000         stock_pixbuf_gdk(STOCK_PIXMAP_REPLIED, &repliedxpm);
1001         stock_pixbuf_gdk(STOCK_PIXMAP_FORWARDED, &forwardedxpm);
1002         stock_pixbuf_gdk(STOCK_PIXMAP_REPLIED_AND_FORWARDED, &repliedandforwardedxpm);
1003         stock_pixbuf_gdk(STOCK_PIXMAP_CLIP, &clipxpm);
1004         stock_pixbuf_gdk(STOCK_PIXMAP_LOCKED, &lockedxpm);
1005         stock_pixbuf_gdk(STOCK_PIXMAP_IGNORETHREAD, &ignorethreadxpm);
1006         stock_pixbuf_gdk(STOCK_PIXMAP_WATCHTHREAD, &watchthreadxpm);
1007         stock_pixbuf_gdk(STOCK_PIXMAP_CLIP_KEY, &clipkeyxpm);
1008         stock_pixbuf_gdk(STOCK_PIXMAP_KEY_SIGN, &keysignxpm);
1009         stock_pixbuf_gdk(STOCK_PIXMAP_KEY, &keyxpm);
1010         stock_pixbuf_gdk(STOCK_PIXMAP_GPG_SIGNED, &gpgsignedxpm);
1011         stock_pixbuf_gdk(STOCK_PIXMAP_CLIP_GPG_SIGNED, &clipgpgsignedxpm);
1012         stock_pixbuf_gdk(STOCK_PIXMAP_SPAM, &spamxpm);
1013         stock_pixbuf_gdk(STOCK_PIXMAP_MOVED, &movedxpm);
1014         stock_pixbuf_gdk(STOCK_PIXMAP_COPIED, &copiedxpm);
1015
1016         summary_set_fonts(summaryview);
1017
1018         summary_set_folder_pixmap(summaryview, STOCK_PIXMAP_DIR_OPEN);
1019
1020         pixmap = stock_pixmap_widget(STOCK_PIXMAP_QUICKSEARCH);
1021         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
1022         gtk_widget_show(pixmap);
1023         summaryview->quick_search_pixmap = pixmap;
1024         
1025 #ifdef GENERIC_UMPC
1026         pixmap = stock_pixmap_widget(STOCK_PIXMAP_SELECTION);
1027         gtk_container_add(GTK_CONTAINER(summaryview->multiple_sel_togbtn), pixmap);
1028         gtk_widget_show(pixmap);
1029         summaryview->multiple_sel_image = pixmap;
1030 #endif
1031
1032         /* Init summaryview prefs */
1033         summaryview->sort_key = SORT_BY_NONE;
1034         summaryview->sort_type = SORT_ASCENDING;
1035
1036         /* Init summaryview extra data */
1037         summaryview->simplify_subject_preg = NULL;
1038         summary_clear_list(summaryview);
1039         summary_set_column_titles(summaryview);
1040         summary_colorlabel_menu_create(summaryview, FALSE);
1041         summary_tags_menu_create(summaryview, FALSE);
1042         main_create_mailing_list_menu (summaryview->mainwin, NULL);     
1043         summary_set_menu_sensitive(summaryview);
1044
1045         summaryview->header_menu_lock = FALSE;
1046 }
1047
1048 #define CURRENTLY_DISPLAYED(m) \
1049 ( (m->msgnum == displayed_msgnum) \
1050   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
1051
1052 #define FOLDER_SHOWS_TO_HDR(i) \
1053 ( i && (folder_has_parent_of_type(i, F_OUTBOX) \
1054         || folder_has_parent_of_type(i, F_DRAFT) \
1055         || folder_has_parent_of_type(i, F_QUEUE)) )
1056   
1057 static void summary_switch_from_to(SummaryView *summaryview, FolderItem *item)
1058 {
1059         gboolean show_from = FALSE, show_to = FALSE;
1060         gboolean showing_from = FALSE, showing_to = FALSE;
1061         gint from_pos = 0, to_pos = 0;
1062         SummaryColumnState *col_state = summaryview->col_state;
1063         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1064         
1065         if (!item || ((prefs_common.layout_mode == VERTICAL_LAYOUT || prefs_common.layout_mode == SMALL_LAYOUT) && prefs_common.two_line_vert) )
1066                 return;
1067         if (FOLDER_SHOWS_TO_HDR(item))
1068                 show_to = TRUE;
1069         else
1070                 show_from = TRUE;
1071         
1072         from_pos = summaryview->col_pos[S_COL_FROM];
1073         to_pos = summaryview->col_pos[S_COL_TO];
1074         showing_from = col_state[from_pos].visible;
1075         showing_to = col_state[to_pos].visible;
1076         
1077         if (showing_from && showing_to) {
1078                 debug_print("showing both\n");
1079                 return;
1080         }
1081
1082         if (!showing_from && !showing_to) {
1083                 debug_print("showing none\n");
1084                 return;
1085         }
1086
1087         debug_print("showing %s %s, must show %s %s\n", 
1088                 showing_from?"From":"",
1089                 showing_to?"To":"",
1090                 show_from?"From":"",
1091                 show_to?"To":"");
1092
1093         if (showing_from == show_from && showing_to == show_to)
1094                 return;
1095         /* else we'll switch both */
1096
1097         debug_print("switching columns\n");
1098         col_state[from_pos].type = S_COL_TO;
1099         col_state[from_pos].visible = show_to;
1100
1101         col_state[to_pos].type = S_COL_FROM;
1102         col_state[to_pos].visible = show_from;
1103
1104         summaryview->col_pos[S_COL_TO] = from_pos;
1105         summaryview->col_pos[S_COL_FROM] = to_pos;
1106
1107         gtk_cmclist_set_column_visibility
1108                 (GTK_CMCLIST(ctree), from_pos, col_state[from_pos].visible);
1109         gtk_cmclist_set_column_visibility
1110                 (GTK_CMCLIST(ctree), to_pos, col_state[to_pos].visible);
1111
1112         summary_set_column_titles(summaryview);
1113 }
1114
1115 static void summaryview_reset_recursive_folder_match(SummaryView *summaryview)
1116 {
1117         GSList *cur;
1118
1119         for (cur = summaryview->recursive_matched_folders; cur != NULL; cur = cur->next) {
1120                 folderview_update_search_icon(cur->data, FALSE);
1121         }
1122
1123         g_slist_free(summaryview->recursive_matched_folders);
1124         summaryview->recursive_matched_folders = NULL;
1125         summaryview->search_root_folder = NULL;
1126 }
1127
1128 static gboolean summaryview_quicksearch_recursive_progress(gpointer data, guint at, guint matched, guint total)
1129 {
1130         QuickSearch *search = (QuickSearch*) data;
1131         gint interval = quicksearch_is_fast(search) ? 5000 : 100;
1132
1133         statusbar_progress_all(at, total, interval);
1134         if (at % interval == 0)
1135                 GTK_EVENTS_FLUSH();
1136
1137         if (matched > 0)
1138                 return FALSE;
1139
1140         return TRUE;
1141 }
1142
1143 static void summaryview_quicksearch_recurse_step(SummaryView *summaryview, FolderItem *item)
1144 {
1145         MsgInfoList *msgs = NULL;
1146         gboolean result = TRUE;
1147
1148         statusbar_print_all(_("Searching in %s... \n"),
1149                 item->path ? item->path : "(null)");
1150         folder_item_update_freeze();
1151
1152         quicksearch_set_on_progress_cb(summaryview->quicksearch, summaryview_quicksearch_recursive_progress, summaryview->quicksearch);
1153         if (!quicksearch_run_on_folder(summaryview->quicksearch, item, &msgs))
1154                 result = FALSE;
1155
1156         result = result && msgs != NULL;
1157
1158         if (msgs != NULL)
1159                 procmsg_msg_list_free(msgs);
1160
1161         folder_item_update_thaw();
1162         statusbar_progress_all(0, 0, 0);
1163         statusbar_pop_all();
1164
1165         if (result) {
1166                 summaryview->recursive_matched_folders = g_slist_prepend(
1167                                 summaryview->recursive_matched_folders, item);
1168         
1169                 folderview_update_search_icon(item, TRUE);
1170         }
1171 }
1172
1173 static void summaryview_quicksearch_search_subfolders(SummaryView *summaryview, FolderItem *folder_item)
1174 {
1175         FolderItem *cur = NULL;
1176         GNode *node = folder_item->node->children;
1177
1178         if (!prefs_common.summary_quicksearch_recurse
1179                         || !quicksearch_has_sat_predicate(summaryview->quicksearch)
1180                         || quicksearch_is_in_typing(summaryview->quicksearch))
1181                 return;
1182
1183         for (; node != NULL; node = node->next) {
1184                 if (!quicksearch_has_sat_predicate(summaryview->quicksearch))
1185                         return;
1186
1187                 cur = FOLDER_ITEM(node->data);
1188                 summaryview_quicksearch_recurse_step(summaryview, cur);
1189                 if (cur->node->children)
1190                         summaryview_quicksearch_search_subfolders(summaryview, cur);
1191         }
1192 }
1193
1194 static void summaryview_quicksearch_recurse(SummaryView *summaryview)
1195 {
1196         if (!prefs_common.summary_quicksearch_recurse
1197                 || !quicksearch_has_sat_predicate(summaryview->quicksearch)
1198                 || summaryview->folder_item == NULL) {
1199                 return;
1200         }
1201         START_TIMING("");
1202         main_window_cursor_wait(summaryview->mainwin);
1203
1204         summaryview_reset_recursive_folder_match(summaryview);
1205         summaryview->search_root_folder = summaryview->folder_item;
1206
1207         summaryview_quicksearch_search_subfolders(summaryview, summaryview->folder_item);
1208         
1209         main_window_cursor_normal(summaryview->mainwin);
1210         END_TIMING();
1211 }
1212
1213 static gboolean summary_check_consistency(FolderItem *item, GSList *mlist)
1214 {
1215         int u = 0, n = 0, m = 0, t = 0, r = 0, f = 0, l = 0, i = 0, w = 0;
1216         GSList *cur;
1217         START_TIMING("");
1218         for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1219                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1220                 t++;
1221                 if (MSG_IS_NEW(msginfo->flags))
1222                         n++;
1223                 if (MSG_IS_UNREAD(msginfo->flags))
1224                         u++;
1225                 if (MSG_IS_MARKED(msginfo->flags))
1226                         m++;            
1227                 if (MSG_IS_REPLIED(msginfo->flags))
1228                         r++;
1229                 if (MSG_IS_FORWARDED(msginfo->flags))
1230                         f++;
1231                 if (MSG_IS_LOCKED(msginfo->flags))
1232                         l++;
1233                 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
1234                         i++;
1235                 if (MSG_IS_WATCH_THREAD(msginfo->flags))
1236                         w++;
1237         }
1238         if (t != item->total_msgs
1239         ||  n != item->new_msgs
1240         ||  u != item->unread_msgs
1241         ||  m != item->marked_msgs
1242         ||  r != item->replied_msgs
1243         ||  f != item->forwarded_msgs
1244         ||  l != item->locked_msgs
1245         ||  i != item->ignored_msgs
1246         ||  w != item->watched_msgs
1247         ||  (m == 0 && item->unreadmarked_msgs != 0)
1248         ||  item->unreadmarked_msgs < 0) {
1249                 debug_print("Inconsistency\n");
1250                 folder_item_scan_full(item, FALSE);
1251                 END_TIMING();
1252                 return FALSE;
1253         } 
1254         END_TIMING();
1255         return TRUE;
1256 }
1257
1258 gboolean summaryview_search_root_progress(gpointer data, guint at, guint matched, guint total)
1259 {
1260         SummaryView *summaryview = (SummaryView*) data;
1261
1262         gint interval = quicksearch_is_fast(summaryview->quicksearch) ? 5000 : 100;
1263         
1264         statusbar_progress_all(at, total, interval);
1265
1266         if (at % interval == 0)
1267                 GTK_EVENTS_FLUSH();
1268
1269         return TRUE;
1270 }
1271
1272 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
1273 {
1274         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1275         GtkCMCTreeNode *node = NULL;
1276         GSList *mlist = NULL;
1277         gchar *buf;
1278         gboolean is_refresh;
1279         guint selected_msgnum = 0;
1280         guint displayed_msgnum = 0;
1281         GSList *cur;
1282         GSList *not_killed;
1283         gboolean hidden_removed = FALSE;
1284
1285         if (summary_is_locked(summaryview)) return FALSE;
1286
1287         if (!summaryview->mainwin)
1288                 return FALSE;
1289         START_TIMING("");
1290         summary_switch_from_to(summaryview, item);
1291
1292         inc_lock();
1293         summary_lock(summaryview);
1294         
1295         menu_set_sensitive_all(GTK_MENU_SHELL(summaryview->popupmenu), TRUE);
1296         
1297         utils_free_regex();
1298
1299         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
1300
1301         if (item && item->folder->klass->item_opened) {
1302                 item->folder->klass->item_opened(item);
1303         }
1304
1305         if (!is_refresh) {
1306                 main_create_mailing_list_menu (summaryview->mainwin, NULL);
1307                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
1308                         if (item) {
1309                                 mainwindow_enter_folder(summaryview->mainwin);
1310                                 gtk_widget_grab_focus(summaryview->ctree);
1311                         }
1312                 }
1313         }
1314         if (!prefs_common.summary_quicksearch_sticky
1315          && (!prefs_common.summary_quicksearch_recurse
1316           || !quicksearch_has_sat_predicate(summaryview->quicksearch)
1317           || (item && !folder_is_child_of(item, summaryview->search_root_folder)))
1318          && !quicksearch_is_running(summaryview->quicksearch)
1319          && !is_refresh) {
1320                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
1321         }
1322
1323         /* STATUSBAR_POP(summaryview->mainwin); */
1324
1325         if (is_refresh) {
1326                 selected_msgnum = summary_get_msgnum(summaryview,
1327                                                      summaryview->selected);
1328                 displayed_msgnum = summary_get_msgnum(summaryview,
1329                                                       summaryview->displayed);
1330         }
1331
1332         /* process the marks if any */
1333         if (!is_refresh &&
1334                         (summaryview->mainwin->lock_count == 0 &&
1335                          (summaryview->moved > 0 || summaryview->copied > 0))) {
1336                 AlertValue val;
1337                 gboolean changed = FALSE;
1338
1339                 val = alertpanel(_("Process mark"),
1340                                  _("Some marks are left. Process them?"),
1341                                  GTK_STOCK_NO, GTK_STOCK_YES, GTK_STOCK_CANCEL, ALERTFOCUS_FIRST);
1342                 if (G_ALERTALTERNATE == val) {
1343                         summary_unlock(summaryview);
1344                         summary_execute(summaryview);
1345                         summary_lock(summaryview);
1346                         changed = TRUE;
1347                 } else if (G_ALERTDEFAULT == val) {
1348                         /* DO NOTHING */
1349                 } else {
1350                         summary_unlock(summaryview);
1351                         inc_unlock();
1352                         END_TIMING();
1353                         return FALSE;
1354                 }
1355                 if (changed || !quicksearch_has_sat_predicate(summaryview->quicksearch))
1356                         folder_update_op_count();
1357         }
1358         
1359         summary_freeze(summaryview);
1360
1361         summary_clear_list(summaryview);
1362
1363         buf = NULL;
1364         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
1365                 g_free(buf);
1366                 debug_print("empty folder (%p %s %p %d)\n",
1367                                 item, 
1368                                 (item && item->path)?item->path:"(null)",
1369                                 item?folder_item_parent(item):0x0,
1370                                 item?item->no_select:FALSE);
1371                 summary_set_hide_menu(summaryview, "/Menu/View/HideReadMessages", FALSE);
1372                 summary_set_hide_menu(summaryview, "/Menu/View/HideDelMessages", FALSE);
1373                 summary_set_hide_menu(summaryview, "/Menu/View/HideReadThreads", FALSE);
1374                 summary_clear_all(summaryview);
1375                 summaryview->folder_item = item;
1376                 summary_thaw(summaryview);
1377                 summary_unlock(summaryview);
1378                 inc_unlock();
1379                 END_TIMING();
1380                 return TRUE;
1381         }
1382         g_free(buf);
1383
1384         if (!is_refresh)
1385                 messageview_clear(summaryview->messageview);
1386
1387         summaryview->folder_item = item;
1388         item->opened = TRUE;
1389
1390         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
1391         debug_print("%s\n", buf);
1392         STATUSBAR_PUSH(summaryview->mainwin, buf);
1393         g_free(buf);
1394
1395         main_window_cursor_wait(summaryview->mainwin);
1396
1397         mlist = folder_item_get_msg_list(item);
1398
1399         if (!summary_check_consistency(item, mlist)) {
1400                 debug_print("reloading due to inconsistency\n");
1401                 procmsg_msg_list_free(mlist);
1402                 mlist = folder_item_get_msg_list(item);
1403         }
1404
1405         if (quicksearch_has_sat_predicate(summaryview->quicksearch)) {
1406                 procmsg_msg_list_free(mlist);
1407                 mlist = NULL;
1408
1409                 START_TIMING("quicksearch");
1410
1411                 statusbar_print_all(_("Searching in %s... \n"), 
1412                         summaryview->folder_item->path ? 
1413                         summaryview->folder_item->path : "(null)");
1414
1415                 folder_item_update_freeze();
1416
1417                 quicksearch_set_on_progress_cb(summaryview->quicksearch, summaryview_search_root_progress, summaryview);
1418                 quicksearch_run_on_folder(summaryview->quicksearch, summaryview->folder_item, &mlist);
1419
1420                 folder_item_update_thaw();
1421                 statusbar_progress_all(0, 0, 0);
1422                 statusbar_pop_all();
1423
1424                 if (!quicksearch_has_sat_predicate(summaryview->quicksearch)) {
1425                         debug_print("search cancelled!\n");
1426                         summary_thaw(summaryview);
1427                         STATUSBAR_POP(summaryview->mainwin);
1428                         main_window_cursor_normal(summaryview->mainwin);
1429                         summary_unlock(summaryview);
1430                         inc_unlock();
1431                         summary_show(summaryview, summaryview->folder_item);
1432                         END_TIMING();
1433                         return FALSE;
1434                 }
1435                 END_TIMING();
1436         }
1437
1438         if ((summaryview->folder_item->hide_read_msgs
1439              || summaryview->folder_item->hide_del_msgs
1440              || summaryview->folder_item->hide_read_threads) &&
1441             quicksearch_has_sat_predicate(summaryview->quicksearch) == FALSE) {
1442                 GSList *not_killed;
1443                 
1444                 summary_set_hide_menu(summaryview, "/Menu/View/HideReadMessages",
1445                                                 summaryview->folder_item->hide_read_msgs);
1446                 summary_set_hide_menu(summaryview, "/Menu/View/HideDelMessages",
1447                                                 summaryview->folder_item->hide_del_msgs);
1448                 summary_set_hide_menu(summaryview, "/Menu/View/HideReadThreads",
1449                                                 summaryview->folder_item->hide_read_threads);
1450                 not_killed = NULL;
1451                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1452                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1453                         
1454                         if (!msginfo->hidden) {
1455                                 if (MSG_IS_DELETED(msginfo->flags) && summaryview->folder_item->hide_del_msgs) {
1456                                         procmsg_msginfo_free(&msginfo);
1457                                         continue;
1458                                 }
1459                                 if (summaryview->folder_item->hide_read_msgs) {
1460                                         if (MSG_IS_UNREAD(msginfo->flags) &&
1461                                             !MSG_IS_IGNORE_THREAD(msginfo->flags))
1462                                                 not_killed = g_slist_prepend(not_killed, msginfo);
1463                                         else if (MSG_IS_MARKED(msginfo->flags) ||
1464                                                  MSG_IS_LOCKED(msginfo->flags))
1465                                                 not_killed = g_slist_prepend(not_killed, msginfo);
1466                                         else if (is_refresh &&
1467                                                 (msginfo->msgnum == selected_msgnum ||
1468                                                  msginfo->msgnum == displayed_msgnum))
1469                                                 not_killed = g_slist_prepend(not_killed, msginfo);
1470                                         else
1471                                                 procmsg_msginfo_free(&msginfo);
1472                                 } else {
1473                                         not_killed = g_slist_prepend(not_killed, msginfo);
1474                                 }
1475                          } else
1476                                 procmsg_msginfo_free(&msginfo);
1477                 }
1478                 hidden_removed = TRUE;
1479                 g_slist_free(mlist);
1480                 mlist = not_killed;
1481         } else {
1482                 summary_set_hide_menu(summaryview, "/Menu/View/HideReadMessages",
1483                                                 FALSE);
1484                 summary_set_hide_menu(summaryview, "/Menu/View/HideDelMessages",
1485                                                 FALSE);
1486                 summary_set_hide_menu(summaryview, "/Menu/View/HideReadThreads",
1487                                                 FALSE);
1488         }
1489
1490         if (!hidden_removed) {
1491                 START_TIMING("removing hidden");
1492                 not_killed = NULL;
1493                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1494                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1495
1496                         if (!msginfo->hidden)
1497                                 not_killed = g_slist_prepend(not_killed, msginfo);
1498                         else
1499                                 procmsg_msginfo_free(&msginfo);
1500                 }
1501                 g_slist_free(mlist);
1502                 mlist = not_killed;
1503                 END_TIMING();
1504         }
1505
1506         STATUSBAR_POP(summaryview->mainwin);
1507
1508         /* set ctree and hash table from the msginfo list, and
1509            create the thread */
1510         summary_set_ctree_from_list(summaryview, mlist, selected_msgnum);
1511
1512         g_slist_free(mlist);
1513
1514         if (quicksearch_is_in_typing(summaryview->quicksearch) ||
1515             quicksearch_is_running(summaryview->quicksearch)) {
1516                 summaryview->displayed =
1517                         summary_find_msg_by_msgnum(summaryview,
1518                                                    displayed_msgnum);
1519                 if (!summaryview->displayed)
1520                         messageview_clear(summaryview->messageview);
1521                 summary_unlock(summaryview);
1522
1523                 if (quicksearch_is_running(summaryview->quicksearch))
1524                         summary_select_by_msgnum(summaryview, selected_msgnum,
1525                                         OPEN_SELECTED_ON_SEARCH_RESULTS);
1526                 else
1527                         summary_select_by_msgnum(summaryview, selected_msgnum,
1528                                         FALSE);
1529
1530                 summary_lock(summaryview);
1531                 if (!summaryview->selected) {
1532                         /* no selected message - select first unread
1533                            message, but do not display it */
1534                         node = summary_find_next_flagged_msg(summaryview, NULL,
1535                                                              MSG_UNREAD, FALSE);
1536                         if (node == NULL && GTK_CMCLIST(ctree)->row_list != NULL)
1537                                 node = gtk_cmctree_node_nth
1538                                         (ctree,
1539                                          item->sort_type == SORT_DESCENDING
1540                                          ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1541                         summary_unlock(summaryview);
1542
1543                         if (quicksearch_is_running(summaryview->quicksearch))
1544                                 summary_select_node(summaryview, node,
1545                                                 OPEN_SELECTED_ON_SEARCH_RESULTS);
1546                         else
1547                                 summary_select_node(summaryview, node,
1548                                                 OPEN_SELECTED_ON_FOLDER_OPEN);
1549
1550                         summary_lock(summaryview);
1551                 }
1552         } else {
1553                 /* backward compat */
1554                 int i = 0;
1555                 gboolean set = FALSE, stop = FALSE;
1556                 for (i = 0; i < 6; i++) {
1557                         EntryAction act = prefs_common.summary_select_prio[i];
1558
1559                         if (act != ACTION_UNSET) {
1560                                 set = TRUE;
1561                                 break;
1562                         }
1563                 }
1564                 if (!set)
1565                         prefs_summary_open_set_defaults();
1566
1567                 for (i = 0; i < 6 && node == NULL; i++) {
1568                         EntryAction act = prefs_common.summary_select_prio[i];
1569                         
1570                         switch(act) {
1571                         case ACTION_OLDEST_MARKED:
1572                                 if (summaryview->sort_type == SORT_ASCENDING)
1573                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1574                                              MSG_MARKED, FALSE);
1575                                 else
1576                                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1577                                              MSG_MARKED, FALSE);
1578                                 break;
1579                         case ACTION_NEWEST_MARKED:
1580                                 if (summaryview->sort_type == SORT_ASCENDING)
1581                                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1582                                              MSG_MARKED, FALSE);
1583                                 else
1584                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1585                                              MSG_MARKED, FALSE);
1586                                 break;
1587                         case ACTION_OLDEST_NEW:
1588                                 if (summaryview->sort_type == SORT_ASCENDING)
1589                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1590                                              MSG_NEW, FALSE);
1591                                 else
1592                                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1593                                              MSG_NEW, FALSE);
1594                                 break;
1595                         case ACTION_NEWEST_NEW:
1596                                 if (summaryview->sort_type == SORT_ASCENDING)
1597                                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1598                                              MSG_NEW, FALSE);
1599                                 else
1600                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1601                                              MSG_NEW, FALSE);
1602                                 break;
1603                         case ACTION_OLDEST_UNREAD:
1604                                 if (summaryview->sort_type == SORT_ASCENDING)
1605                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1606                                              MSG_UNREAD, FALSE);
1607                                 else
1608                                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1609                                              MSG_UNREAD, FALSE);
1610                                 break;
1611                         case ACTION_NEWEST_UNREAD:
1612                                 if (summaryview->sort_type == SORT_ASCENDING)
1613                                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1614                                              MSG_UNREAD, FALSE);
1615                                 else
1616                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1617                                              MSG_UNREAD, FALSE);
1618                                 break;
1619                         case ACTION_LAST_OPENED:
1620                                 if (summaryview->folder_item) {
1621                                         node = summary_find_msg_by_msgnum(summaryview, 
1622                                                         summaryview->folder_item->last_seen);
1623                                 }
1624                                 break;
1625                         case ACTION_NEWEST_LIST:
1626                                 if (GTK_CMCLIST(ctree)->row_list != NULL) {
1627                                         node = gtk_cmctree_node_nth
1628                                                 (ctree,
1629                                                  item->sort_type == SORT_DESCENDING
1630                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1631                                 }
1632                                 break;
1633                         case ACTION_OLDEST_LIST:
1634                                 if (GTK_CMCLIST(ctree)->row_list != NULL) {
1635                                         node = gtk_cmctree_node_nth
1636                                                 (ctree,
1637                                                  item->sort_type == SORT_ASCENDING
1638                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1639                                 }
1640                                 break;
1641                         case ACTION_NOTHING:
1642                         case ACTION_UNSET:
1643                                 node = NULL;
1644                                 stop = TRUE;
1645                                 break;
1646                         }
1647                         
1648                         if (stop || node)
1649                                 break;
1650                 }
1651
1652                 summary_unlock(summaryview);
1653
1654                 if (node) {
1655                         gint open_selected = -1;
1656                         if (OPEN_SELECTED_ON_FOLDER_OPEN)
1657                                 open_selected = 1;
1658                         else
1659                                 open_selected = 0;
1660                         summary_select_node(summaryview, node, open_selected);
1661                 }
1662
1663                 summary_lock(summaryview);
1664         }
1665
1666         summary_status_show(summaryview);
1667         summary_set_menu_sensitive(summaryview);
1668         toolbar_main_set_sensitive(summaryview->mainwin);
1669         
1670         summary_thaw(summaryview);
1671         debug_print("\n");
1672         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1673         STATUSBAR_POP(summaryview->mainwin);
1674         main_window_cursor_normal(summaryview->mainwin);
1675         summary_unlock(summaryview);
1676         inc_unlock();
1677         END_TIMING();
1678         return TRUE;
1679 }
1680
1681 #undef CURRENTLY_DISPLAYED
1682
1683 static void summary_cancel_mark_read_timeout(SummaryView *summaryview) {
1684         if (summaryview->mark_as_read_timeout_tag != 0) {
1685                 g_source_remove(summaryview->mark_as_read_timeout_tag);
1686                 summaryview->mark_as_read_timeout_tag = 0;
1687         }
1688 }
1689
1690 void summary_clear_list(SummaryView *summaryview)
1691 {
1692         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
1693         gint optimal_width;
1694
1695         summary_freeze(summaryview);
1696
1697         gtk_cmctree_pre_recursive(GTK_CMCTREE(summaryview->ctree),
1698                                 NULL, summary_free_msginfo_func, NULL);
1699
1700         if (summaryview->folder_item) {
1701                 summaryview->folder_item->opened = FALSE;
1702                 summaryview->folder_item = NULL;
1703         }
1704
1705         summary_cancel_mark_read_timeout(summaryview);
1706
1707         summaryview->display_msg = FALSE;
1708
1709         summaryview->selected = NULL;
1710         summaryview->displayed = NULL;
1711         summaryview->total_size = 0;
1712         summaryview->deleted = summaryview->moved = 0;
1713         summaryview->copied = 0;
1714         if (summaryview->msgid_table) {
1715                 g_hash_table_destroy(summaryview->msgid_table);
1716                 summaryview->msgid_table = NULL;
1717         }
1718         if (summaryview->subject_table) {
1719                 g_hash_table_destroy(summaryview->subject_table);
1720                 summaryview->subject_table = NULL;
1721         }
1722         summaryview->mlist = NULL;
1723
1724         gtk_cmclist_clear(clist);
1725         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1726                 optimal_width = gtk_cmclist_optimal_column_width
1727                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1728                 gtk_cmclist_set_column_width
1729                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1730                          optimal_width);
1731         }
1732
1733         summary_thaw(summaryview);
1734 }
1735
1736 void summary_clear_all(SummaryView *summaryview)
1737 {
1738         messageview_clear(summaryview->messageview);
1739         summary_clear_list(summaryview);
1740         summary_set_menu_sensitive(summaryview);
1741         toolbar_main_set_sensitive(summaryview->mainwin);
1742         summary_status_show(summaryview);
1743 }
1744
1745 void summary_lock(SummaryView *summaryview)
1746 {
1747         summaryview->lock_count++;
1748 }
1749
1750 void summary_unlock(SummaryView *summaryview)
1751 {
1752         if (summaryview->lock_count)
1753                 summaryview->lock_count--;
1754 }
1755
1756 gboolean summary_is_locked(SummaryView *summaryview)
1757 {
1758         return summaryview->lock_count > 0;
1759 }
1760
1761 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1762 {
1763         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
1764         SummarySelection selection;
1765
1766         if (!clist->row_list)
1767                 selection = SUMMARY_NONE;
1768         else if (!clist->selection)
1769                 selection = SUMMARY_SELECTED_NONE;
1770         else if (!clist->selection->next)
1771                 selection = SUMMARY_SELECTED_SINGLE;
1772         else
1773                 selection = SUMMARY_SELECTED_MULTIPLE;
1774
1775         return selection;
1776 }
1777
1778 /*!
1779  *\return       MsgInfo * Selected message if there's one selected;
1780  *              if multiple selected, or none, return NULL.
1781  */ 
1782 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1783 {
1784         /* summaryview->selected may be valid when multiple 
1785          * messages were selected */
1786         GList *sellist = GTK_CMCLIST(summaryview->ctree)->selection;
1787
1788         if (sellist == NULL || sellist->next) 
1789                 return NULL;
1790         
1791         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1792 }
1793
1794 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1795 {
1796         GSList *mlist = NULL;
1797         GList *cur;
1798         MsgInfo *msginfo;
1799
1800         for (cur = GTK_CMCLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1801              cur = cur->next) {
1802                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1803                 mlist = g_slist_prepend(mlist,
1804                                 procmsg_msginfo_new_ref(msginfo));
1805         }
1806
1807         mlist = g_slist_reverse(mlist);
1808
1809         return mlist;
1810 }
1811
1812 void summary_set_menu_sensitive(SummaryView *summaryview)
1813 {
1814         SensitiveCondMask state;
1815         gboolean sensitive;
1816         gint i;
1817
1818 #ifndef GENERIC_UMPC
1819 #define N_ENTRIES 39
1820 #else
1821 #define N_ENTRIES 28
1822 #endif
1823         static struct {
1824                 const gchar *entry;
1825                 SensitiveCondMask cond;
1826         } entry[N_ENTRIES];
1827
1828         i = 0;
1829 #define FILL_TABLE(entry_str, ...) \
1830 do { \
1831         entry[i].entry = (const gchar *) entry_str; entry[i++].cond = main_window_get_mask(__VA_ARGS__, -1); \
1832 } while (0)
1833
1834         FILL_TABLE("Menus/SummaryViewPopup/Reply", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1835 #ifndef GENERIC_UMPC
1836         FILL_TABLE("Menus/SummaryViewPopup/ReplyTo", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1837         FILL_TABLE("Menus/SummaryViewPopup/ReplyTo/All", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1838         FILL_TABLE("Menus/SummaryViewPopup/ReplyTo/Sender", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1839         FILL_TABLE("Menus/SummaryViewPopup/ReplyTo/MailingList", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1840 #endif
1841
1842         FILL_TABLE("Menus/SummaryViewPopup/Forward", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1843 #ifndef GENERIC_UMPC
1844         FILL_TABLE("Menus/SummaryViewPopup/ForwardAtt", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1845         FILL_TABLE("Menus/SummaryViewPopup/Redirect", M_HAVE_ACCOUNT, M_TARGET_EXIST);
1846 #endif
1847
1848         FILL_TABLE("Menus/SummaryViewPopup/Move", M_TARGET_EXIST, M_ALLOW_DELETE, M_NOT_NEWS);
1849         FILL_TABLE("Menus/SummaryViewPopup/Copy", M_TARGET_EXIST, M_EXEC);
1850         FILL_TABLE("Menus/SummaryViewPopup/Trash", M_TARGET_EXIST, M_ALLOW_DELETE, M_NOT_NEWS, M_NOT_TRASH);
1851 #ifndef GENERIC_UMPC
1852         FILL_TABLE("Menus/SummaryViewPopup/Delete", M_TARGET_EXIST, M_ALLOW_DELETE);
1853 #endif
1854
1855         FILL_TABLE("Menus/SummaryViewPopup/Mark", M_TARGET_EXIST);
1856         FILL_TABLE("Menus/SummaryViewPopup/Mark/Mark", M_TARGET_EXIST);
1857         FILL_TABLE("Menus/SummaryViewPopup/Mark/Unmark", M_TARGET_EXIST);
1858         FILL_TABLE("Menus/SummaryViewPopup/Mark/MarkRead", M_TARGET_EXIST);
1859         FILL_TABLE("Menus/SummaryViewPopup/Mark/MarkUnread", M_TARGET_EXIST);
1860         FILL_TABLE("Menus/SummaryViewPopup/Mark/MarkAllRead", M_TARGET_EXIST);
1861         FILL_TABLE("Menus/SummaryViewPopup/Mark/MarkAllUnread", M_TARGET_EXIST);
1862         FILL_TABLE("Menus/SummaryViewPopup/Mark/IgnoreThread", M_TARGET_EXIST);
1863         FILL_TABLE("Menus/SummaryViewPopup/Mark/UnignoreThread", M_TARGET_EXIST);
1864         FILL_TABLE("Menus/SummaryViewPopup/Mark/WatchThread", M_TARGET_EXIST);
1865         FILL_TABLE("Menus/SummaryViewPopup/Mark/UnwatchThread", M_TARGET_EXIST);
1866         FILL_TABLE("Menus/SummaryViewPopup/Mark/Lock", M_TARGET_EXIST);
1867         FILL_TABLE("Menus/SummaryViewPopup/Mark/Unlock", M_TARGET_EXIST);
1868         FILL_TABLE("Menus/SummaryViewPopup/Mark/MarkSpam", M_TARGET_EXIST, M_CAN_LEARN_SPAM);
1869         FILL_TABLE("Menus/SummaryViewPopup/Mark/MarkHam", M_TARGET_EXIST, M_CAN_LEARN_SPAM);
1870         FILL_TABLE("Menus/SummaryViewPopup/ColorLabel", M_TARGET_EXIST);
1871         FILL_TABLE("Menus/SummaryViewPopup/Tags", M_TARGET_EXIST);
1872
1873 #ifndef GENERIC_UMPC
1874         FILL_TABLE("Menus/SummaryViewPopup/AddSenderToAB", M_SINGLE_TARGET_EXIST);
1875 #endif
1876         FILL_TABLE("Menus/SummaryViewPopup/CreateFilterRule", M_SINGLE_TARGET_EXIST, M_UNLOCKED);
1877 #ifndef GENERIC_UMPC
1878         FILL_TABLE("Menus/SummaryViewPopup/CreateProcessingRule", M_SINGLE_TARGET_EXIST, M_UNLOCKED);
1879 #endif
1880
1881         FILL_TABLE("Menus/SummaryViewPopup/View", M_SINGLE_TARGET_EXIST);
1882         FILL_TABLE("Menus/SummaryViewPopup/View/OpenNewWindow", M_SINGLE_TARGET_EXIST);
1883         FILL_TABLE("Menus/SummaryViewPopup/View/MessageSource", M_SINGLE_TARGET_EXIST);
1884 #ifndef GENERIC_UMPC
1885         FILL_TABLE("Menus/SummaryViewPopup/View/AllHeaders", M_SINGLE_TARGET_EXIST);
1886 #endif
1887         FILL_TABLE("Menus/SummaryViewPopup/SaveAs", M_TARGET_EXIST);
1888 #ifndef GENERIC_UMPC
1889         FILL_TABLE("Menus/SummaryViewPopup/Print", M_TARGET_EXIST);
1890 #endif
1891         FILL_TABLE(NULL, -1);
1892 #undef FILL_TABLE
1893         if (i != N_ENTRIES)
1894                 g_error("summaryview menu entry table size mismatch (%d/%d)", i, N_ENTRIES);
1895 #undef ENTRIES
1896
1897         main_window_set_menu_sensitive(summaryview->mainwin);
1898
1899         state = main_window_get_current_state(summaryview->mainwin);
1900
1901         for (i = 0; entry[i].entry != NULL; i++) {
1902                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1903                 cm_menu_set_sensitive_full(summaryview->mainwin->ui_manager, entry[i].entry, sensitive);
1904         }
1905
1906         summary_lock(summaryview);
1907 #ifndef GENERIC_UMPC
1908         if (summaryview->messageview 
1909         &&  summaryview->messageview->mimeview
1910         &&  summaryview->messageview->mimeview->textview)
1911                 cm_toggle_menu_set_active_full(summaryview->mainwin->ui_manager, "Menus/SummaryViewPopup/View/AllHeaders",
1912                         prefs_common.show_all_headers);
1913 #endif
1914         summary_unlock(summaryview);
1915 }
1916 void summary_select_prev(SummaryView *summaryview)
1917 {
1918         GtkCMCTreeNode *node = summaryview->selected;
1919         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1920
1921         if (summaryview->sort_type == SORT_ASCENDING)
1922                 node = gtkut_ctree_node_prev(ctree, node);
1923         else
1924                 node = gtkut_ctree_node_next(ctree, node);
1925
1926         if (node && node != summaryview->selected)
1927                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
1928 }
1929
1930 void summary_select_next(SummaryView *summaryview)
1931 {
1932         GtkCMCTreeNode *node = summaryview->selected;
1933         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1934
1935         if (summaryview->sort_type == SORT_ASCENDING)
1936                 node = gtkut_ctree_node_next(ctree, node);
1937         else
1938                 node = gtkut_ctree_node_prev(ctree, node);
1939
1940         if (node && node != summaryview->selected)
1941                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
1942 }
1943
1944 void summary_select_prev_unread(SummaryView *summaryview)
1945 {
1946         GtkCMCTreeNode *node;
1947
1948         if (summaryview->sort_type == SORT_ASCENDING)
1949                 node = summary_find_prev_flagged_msg
1950                         (summaryview, summaryview->selected, MSG_UNREAD, TRUE);
1951         else
1952                 node = summary_find_next_flagged_msg
1953                         (summaryview, summaryview->selected, MSG_UNREAD, TRUE);
1954
1955         if (!node || node == summaryview->selected) {
1956                 AlertValue val = 0;
1957
1958                 switch (prefs_common.next_unread_msg_dialog) {
1959                         case NEXTUNREADMSGDIALOG_ALWAYS:
1960                                 val = alertpanel(_("No more unread messages"),
1961                                                  _("No unread message found. "
1962                                                    "Search from the end?"),
1963                                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
1964                                 break;
1965                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1966                                 val = G_ALERTALTERNATE;
1967                                 break;
1968                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1969                                 val = !G_ALERTALTERNATE;
1970                                 break;
1971                         default:
1972                                 debug_print(
1973                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1974                 }
1975                 if (val != G_ALERTALTERNATE) return;
1976                 if (summaryview->sort_type == SORT_ASCENDING)
1977                         node = summary_find_prev_flagged_msg(summaryview, NULL,
1978                                                              MSG_UNREAD, FALSE);
1979                 else
1980                         node = summary_find_next_flagged_msg(summaryview, NULL,
1981                                                              MSG_UNREAD, FALSE);
1982         }
1983
1984         if (!node)
1985                 alertpanel_notice(_("No unread messages."));
1986         else
1987                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
1988 }
1989
1990 void summary_select_next_unread(SummaryView *summaryview)
1991 {
1992         GtkCMCTreeNode *node = summaryview->selected;
1993
1994         if (summaryview->sort_type == SORT_ASCENDING)
1995                 node = summary_find_next_flagged_msg
1996                         (summaryview, node, MSG_UNREAD, TRUE);
1997         else
1998                 node = summary_find_prev_flagged_msg
1999                         (summaryview, node, MSG_UNREAD, TRUE);
2000         
2001         if (node)
2002                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2003         else {
2004                 AlertValue val = 0;
2005
2006                 switch (prefs_common.next_unread_msg_dialog) {
2007                         case NEXTUNREADMSGDIALOG_ALWAYS:
2008                                 val = alertpanel(_("No more unread messages"),
2009                                                  _("No unread message found. "
2010                                                    "Go to next folder?"),
2011                                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2012                                 break;
2013                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
2014                                 val = G_ALERTALTERNATE;
2015                                 break;
2016                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
2017                                 val = G_ALERTOTHER;
2018                                 break;
2019                         default:
2020                                 debug_print(
2021                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
2022                 }
2023
2024                 if (val == G_ALERTALTERNATE)
2025                         folderview_select_next_with_flag(summaryview->folderview, MSG_UNREAD);
2026         }
2027 }
2028
2029 void summary_select_prev_new(SummaryView *summaryview)
2030 {
2031         GtkCMCTreeNode *node;
2032
2033         if (summaryview->sort_type == SORT_ASCENDING)
2034                 node = summary_find_prev_flagged_msg
2035                         (summaryview, summaryview->selected, MSG_NEW, TRUE);
2036         else
2037                 node = summary_find_next_flagged_msg
2038                         (summaryview, summaryview->selected, MSG_NEW, TRUE);
2039
2040         if (!node || node == summaryview->selected) {
2041                 AlertValue val = 0;
2042
2043                 switch (prefs_common.next_unread_msg_dialog) {
2044                         case NEXTUNREADMSGDIALOG_ALWAYS:
2045                                 val = alertpanel(_("No more new messages"),
2046                                                  _("No new message found. "
2047                                                    "Search from the end?"),
2048                                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2049                                 break;
2050                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
2051                                 val = G_ALERTALTERNATE;
2052                                 break;
2053                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
2054                                 val = !G_ALERTALTERNATE;
2055                                 break;
2056                         default:
2057                                 debug_print(
2058                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
2059                 }
2060                 if (val != G_ALERTALTERNATE) return;
2061                 if (summaryview->sort_type == SORT_ASCENDING)
2062                         node = summary_find_prev_flagged_msg(summaryview, NULL,
2063                                                              MSG_NEW, FALSE);
2064                 else
2065                         node = summary_find_next_flagged_msg(summaryview, NULL,
2066                                                              MSG_NEW, FALSE);
2067         }
2068
2069         if (!node)
2070                 alertpanel_notice(_("No new messages."));
2071         else
2072                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2073 }
2074
2075 void summary_select_next_new(SummaryView *summaryview)
2076 {
2077         GtkCMCTreeNode *node = summaryview->selected;
2078
2079         if (summaryview->sort_type == SORT_ASCENDING)
2080                 node = summary_find_next_flagged_msg
2081                         (summaryview, node, MSG_NEW, TRUE);
2082         else
2083                 node = summary_find_prev_flagged_msg
2084                         (summaryview, node, MSG_NEW, TRUE);
2085         
2086         if (node)
2087                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2088         else {
2089                 AlertValue val = 0;
2090
2091                 switch (prefs_common.next_unread_msg_dialog) {
2092                         case NEXTUNREADMSGDIALOG_ALWAYS:
2093                                 val = alertpanel(_("No more new messages"),
2094                                                  _("No new message found. "
2095                                                    "Go to next folder?"),
2096                                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2097                                 break;
2098                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
2099                                 val = G_ALERTALTERNATE;
2100                                 break;
2101                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
2102                                 val = G_ALERTOTHER;
2103                                 break;
2104                         default:
2105                                 debug_print(
2106                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
2107                 }
2108                 if (val == G_ALERTALTERNATE)
2109                         folderview_select_next_with_flag(summaryview->folderview, MSG_NEW);
2110         }
2111 }
2112
2113 void summary_select_prev_marked(SummaryView *summaryview)
2114 {
2115         GtkCMCTreeNode *node;
2116
2117         if (summaryview->sort_type == SORT_ASCENDING)
2118                 node = summary_find_prev_flagged_msg
2119                         (summaryview, summaryview->selected, MSG_MARKED, TRUE);
2120         else
2121                 node = summary_find_next_flagged_msg
2122                         (summaryview, summaryview->selected, MSG_MARKED, TRUE);
2123
2124         if (!node) {
2125                 AlertValue val;
2126
2127                 val = alertpanel(_("No more marked messages"),
2128                                  _("No marked message found. "
2129                                    "Search from the end?"),
2130                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2131                 if (val != G_ALERTALTERNATE) return;
2132                 node = summary_find_prev_flagged_msg(summaryview, NULL,
2133                                                      MSG_MARKED, TRUE);
2134         }
2135
2136         if (!node)
2137                 alertpanel_notice(_("No marked messages."));
2138         else
2139                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2140 }
2141
2142 void summary_select_next_marked(SummaryView *summaryview)
2143 {
2144         GtkCMCTreeNode *node = summaryview->selected;
2145
2146         if (summaryview->sort_type == SORT_ASCENDING)
2147                 node = summary_find_next_flagged_msg
2148                         (summaryview, node, MSG_MARKED, TRUE);
2149         else
2150                 node = summary_find_prev_flagged_msg
2151                         (summaryview, node, MSG_MARKED, TRUE);
2152         
2153         if (node)
2154                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2155         else {
2156                 AlertValue val = 0;
2157
2158                 switch (prefs_common.next_unread_msg_dialog) {
2159                         case NEXTUNREADMSGDIALOG_ALWAYS:
2160                                 val = alertpanel(_("No more marked messages"),
2161                                                  _("No marked message found. "
2162                                                    "Go to next folder?"),
2163                                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2164                                 break;
2165                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
2166                                 val = G_ALERTALTERNATE;
2167                                 break;
2168                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
2169                                 val = G_ALERTOTHER;
2170                                 break;
2171                         default:
2172                                 debug_print(
2173                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
2174                 }
2175                 if (val == G_ALERTALTERNATE)
2176                         folderview_select_next_with_flag(summaryview->folderview, MSG_MARKED);
2177         }
2178 }
2179
2180 void summary_select_prev_labeled(SummaryView *summaryview)
2181 {
2182         GtkCMCTreeNode *node;
2183
2184         if (summaryview->sort_type == SORT_ASCENDING)
2185                 node = summary_find_prev_flagged_msg
2186                         (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2187         else
2188                 node = summary_find_next_flagged_msg
2189                         (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2190
2191         if (!node) {
2192                 AlertValue val;
2193
2194                 val = alertpanel(_("No more labeled messages"),
2195                                  _("No labeled message found. "
2196                                    "Search from the end?"),
2197                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2198                 if (val != G_ALERTALTERNATE) return;
2199                 node = summary_find_prev_flagged_msg(summaryview, NULL,
2200                                                      MSG_CLABEL_FLAG_MASK, TRUE);
2201         }
2202
2203         if (!node)
2204                 alertpanel_notice(_("No labeled messages."));
2205         else
2206                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2207 }
2208
2209 void summary_select_next_labeled(SummaryView *summaryview)
2210 {
2211         GtkCMCTreeNode *node;
2212
2213         if (summaryview->sort_type == SORT_ASCENDING)
2214                 node = summary_find_next_flagged_msg
2215                         (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2216         else
2217                 node = summary_find_prev_flagged_msg
2218                         (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2219
2220         if (!node) {
2221                 AlertValue val;
2222
2223                 val = alertpanel(_("No more labeled messages"),
2224                                  _("No labeled message found. "
2225                                    "Search from the beginning?"),
2226                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
2227                 if (val != G_ALERTALTERNATE) return;
2228                 if (summaryview->sort_type == SORT_ASCENDING)
2229                         node = summary_find_next_flagged_msg(summaryview, NULL,
2230                                                              MSG_CLABEL_FLAG_MASK, TRUE);
2231                 else
2232                         node = summary_find_prev_flagged_msg(summaryview, NULL,
2233                                                              MSG_CLABEL_FLAG_MASK, TRUE);
2234         }
2235
2236         if (!node)
2237                 alertpanel_notice(_("No labeled messages."));
2238         else
2239                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2240 }
2241
2242 void summary_select_parent(SummaryView *summaryview)
2243 {
2244         GtkCMCTreeNode *node = NULL;
2245
2246         if (summaryview->selected)
2247                 node = GTK_CMCTREE_ROW(summaryview->selected)->parent;
2248         if (node)
2249                 summary_select_node(summaryview, node, OPEN_SELECTED_ON_PREVNEXT);
2250 }
2251
2252 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum,
2253                 gboolean show)
2254 {
2255         GtkCMCTreeNode *node;
2256
2257         node = summary_find_msg_by_msgnum(summaryview, msgnum);
2258         summary_select_node(summaryview, node, show);
2259 }
2260
2261 void summary_select_by_msg_list(SummaryView     *summaryview, GSList *msginfos)
2262 {
2263         GtkCMCTree *ctree;
2264         GSList *msgnum_list, *walk;
2265         gboolean froze = FALSE;
2266
2267         ctree = GTK_CMCTREE(summaryview->ctree);
2268
2269         msgnum_list = procmsg_get_number_list_for_msgs(msginfos);
2270
2271         START_LONG_OPERATION(summaryview, FALSE);
2272         for(walk = msgnum_list; walk; walk = walk->next) {
2273                 GtkCMCTreeNode *node;
2274                 node = summary_find_msg_by_msgnum(summaryview, GPOINTER_TO_UINT(walk->data));
2275                 if (node != NULL)
2276                         gtk_cmctree_select(ctree, node);
2277         }
2278         END_LONG_OPERATION(summaryview);
2279         g_slist_free(msgnum_list);
2280 }
2281
2282 typedef struct _PostponedSelectData
2283 {
2284         GtkCMCTree *ctree;
2285         GtkCMCTreeNode *row;
2286         GtkCMCTreeNode *node;
2287         GtkScrollType type;
2288         gint column;
2289         SummaryView *summaryview;
2290         gboolean display_msg;
2291 } PostponedSelectData;
2292
2293 static gboolean summary_select_retry(void *data)
2294 {
2295         PostponedSelectData *psdata = (PostponedSelectData *)data;
2296         debug_print("trying again\n");
2297         if (psdata->row)
2298                 summary_selected(psdata->ctree, psdata->row,
2299                             psdata->column, psdata->summaryview);
2300         else if (psdata->node)
2301                 summary_select_node(psdata->summaryview, psdata->node,
2302                             psdata->display_msg);
2303         g_free(psdata);
2304         return FALSE;
2305 }
2306
2307 /**
2308  * summary_select_node:
2309  * @summaryview: Summary view.
2310  * @node: Summary tree node.
2311  * @display_msg: whether to also display the message
2312  *
2313  * Select @node (bringing it into view by scrolling and expanding its
2314  * thread, if necessary) and unselect all others.
2315  **/
2316
2317 void summary_select_node(SummaryView *summaryview, GtkCMCTreeNode *node,
2318                          gboolean display_msg)
2319 {
2320         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2321
2322         /* If msgview is hidden, we never want to automatically display
2323          * a selected message, since that would unhide the msgview. */
2324         if (!messageview_is_visible(summaryview->messageview))
2325                 display_msg = FALSE;
2326
2327         if (summary_is_locked(summaryview)
2328         && !GTK_SCTREE(ctree)->selecting_range
2329         && summaryview->messageview->mimeview
2330         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
2331         && summaryview->messageview->mimeview->textview->loading) {
2332                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2333                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
2334                 
2335                 data->ctree = ctree;
2336                 data->row = NULL;
2337                 data->node = node;
2338                 data->summaryview = summaryview;
2339                 data->display_msg = display_msg;
2340                 debug_print("postponing open of message till end of load\n");
2341                 g_timeout_add(100, summary_select_retry, data);
2342                 return;
2343         }
2344         if (summary_is_locked(summaryview)) {
2345                 return;
2346         }
2347         if (!summaryview->folder_item)
2348                 return;
2349         if (node) {
2350                 gtkut_ctree_expand_parent_all(ctree, node);
2351
2352                 summary_lock(summaryview);
2353                 GTK_EVENTS_FLUSH();
2354                 summary_unlock(summaryview);
2355
2356                 /* If quicksearch has focus, let's keep it there. */
2357                 if (!quicksearch_has_focus(summaryview->quicksearch))
2358                         summary_grab_focus(summaryview);
2359
2360                 gtkut_ctree_node_move_if_on_the_edge(ctree, node, -1);
2361
2362                 if (display_msg && summaryview->displayed == node)
2363                         summaryview->displayed = NULL;
2364                 summaryview->display_msg = display_msg;
2365                 gtk_sctree_select(GTK_SCTREE(ctree), node);
2366                 if (summaryview->selected == NULL)
2367                         summaryview->selected = node;
2368         }
2369 }
2370
2371 guint summary_get_msgnum(SummaryView *summaryview, GtkCMCTreeNode *node)
2372 {
2373         GtkCMCTree *ctree =NULL;
2374         MsgInfo *msginfo;
2375
2376         if (!summaryview)
2377                 return 0;
2378         ctree = GTK_CMCTREE(summaryview->ctree);
2379         if (!node)
2380                 return 0;
2381         msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2382         if (msginfo)
2383                 return msginfo->msgnum;
2384         else 
2385                 return -1;
2386 }
2387
2388 static GtkCMCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
2389                                              GtkCMCTreeNode *current_node,
2390                                              gboolean start_from_prev)
2391 {
2392         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2393         GtkCMCTreeNode *node;
2394         MsgInfo *msginfo;
2395
2396         if (current_node) {
2397                 if (start_from_prev)
2398                         node = gtkut_ctree_node_prev(ctree, current_node);
2399                 else
2400                         node = current_node;
2401         } else
2402                 node = gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->rows - 1);
2403
2404         for (; node != NULL; node = gtkut_ctree_node_prev(ctree, node)) {
2405                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2406                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
2407         }
2408
2409         return node;
2410 }
2411
2412 static GtkCMCTreeNode *summary_find_next_msg(SummaryView *summaryview,
2413                                             GtkCMCTreeNode *current_node,
2414                                             gboolean start_from_next)
2415 {
2416         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2417         GtkCMCTreeNode *node;
2418         MsgInfo *msginfo;
2419
2420         if (current_node) {
2421                 if (start_from_next)
2422                         node = gtkut_ctree_node_next(ctree, current_node);
2423                 else
2424                         node = current_node;
2425         } else
2426                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2427
2428         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2429                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2430                 if (msginfo && !MSG_IS_DELETED(msginfo->flags) 
2431                 && !MSG_IS_MOVE(msginfo->flags)) break;
2432         }
2433
2434         return node;
2435 }
2436
2437 static GtkCMCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
2438                                                    GtkCMCTreeNode *current_node,
2439                                                    MsgPermFlags flags,
2440                                                    gboolean start_from_prev)
2441 {
2442         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2443         GtkCMCTreeNode *node;
2444         MsgInfo *msginfo;
2445
2446         if (current_node) {
2447                 if (start_from_prev)
2448                         node = gtkut_ctree_node_prev(ctree, current_node);
2449                 else
2450                         node = current_node;
2451         } else
2452                 node = gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->rows - 1);
2453
2454         for (; node != NULL; node = gtkut_ctree_node_prev(ctree, node)) {
2455                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2456                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
2457         }
2458
2459         return node;
2460 }
2461
2462 static GtkCMCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
2463                                                    GtkCMCTreeNode *current_node,
2464                                                    MsgPermFlags flags,
2465                                                    gboolean start_from_next)
2466 {
2467         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2468         GtkCMCTreeNode *node;
2469         MsgInfo *msginfo;
2470
2471         if (current_node) {
2472                 if (start_from_next)
2473                         node = gtkut_ctree_node_next(ctree, current_node);
2474                 else
2475                         node = current_node;
2476         } else
2477                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2478
2479         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2480                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2481                 /* Find msg with matching flags but ignore messages with
2482                    ignore flags, if searching for new or unread messages */
2483                 if ((msginfo && (msginfo->flags.perm_flags & flags) != 0) &&
2484                     !(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) 
2485                         )
2486                         break;
2487         }
2488
2489         return node;
2490 }
2491
2492 static GtkCMCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
2493                                                 guint msgnum)
2494 {
2495         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2496         GtkCMCTreeNode *node;
2497         MsgInfo *msginfo;
2498
2499         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2500
2501         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2502                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2503                 if (msginfo && msginfo->msgnum == msgnum) break;
2504         }
2505
2506         return node;
2507 }
2508
2509 static guint attract_hash_func(gconstpointer key)
2510 {
2511         gchar *str;
2512         gchar *p;
2513         guint h;
2514
2515         Xstrdup_a(str, (const gchar *)key, return 0);
2516         trim_subject(str);
2517
2518         p = str;
2519         h = *p;
2520
2521         if (h) {
2522                 for (p += 1; *p != '\0'; p++)
2523                         h = (h << 5) - h + *p;
2524         }
2525
2526         return h;
2527 }
2528
2529 static gint attract_compare_func(gconstpointer a, gconstpointer b)
2530 {
2531         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
2532 }
2533
2534 void summary_attract_by_subject(SummaryView *summaryview)
2535 {
2536         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2537         GtkCMCList *clist = GTK_CMCLIST(ctree);
2538         GtkCMCTreeNode *src_node;
2539         GtkCMCTreeNode *dst_node, *sibling;
2540         GtkCMCTreeNode *tmp;
2541         MsgInfo *src_msginfo, *dst_msginfo;
2542         GHashTable *subject_table;
2543
2544         debug_print("Attracting messages by subject...\n");
2545         STATUSBAR_PUSH(summaryview->mainwin,
2546                        _("Attracting messages by subject..."));
2547
2548         main_window_cursor_wait(summaryview->mainwin);
2549         summary_freeze(summaryview);
2550
2551         subject_table = g_hash_table_new(attract_hash_func,
2552                                          attract_compare_func);
2553
2554         for (src_node = GTK_CMCTREE_NODE(clist->row_list);
2555              src_node != NULL;
2556              src_node = tmp) {
2557                 tmp = GTK_CMCTREE_ROW(src_node)->sibling;
2558                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
2559                 if (!src_msginfo) continue;
2560                 if (!src_msginfo->subject) continue;
2561
2562                 /* find attracting node */
2563                 dst_node = g_hash_table_lookup(subject_table,
2564                                                src_msginfo->subject);
2565
2566                 if (dst_node) {
2567                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
2568
2569                         /* if the time difference is more than 20 days,
2570                            don't attract */
2571                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
2572                             > 60 * 60 * 24 * 20)
2573                                 continue;
2574
2575                         sibling = GTK_CMCTREE_ROW(dst_node)->sibling;
2576                         if (src_node != sibling)
2577                                 gtk_cmctree_move(ctree, src_node, NULL, sibling);
2578                 }
2579
2580                 g_hash_table_insert(subject_table,
2581                                     src_msginfo->subject, src_node);
2582         }
2583
2584         g_hash_table_destroy(subject_table);
2585
2586         gtk_cmctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2587
2588         summary_thaw(summaryview);
2589
2590         debug_print("Attracting messages by subject done.\n");
2591         STATUSBAR_POP(summaryview->mainwin);
2592
2593         main_window_cursor_normal(summaryview->mainwin);
2594 }
2595
2596 static void summary_free_msginfo_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2597                                       gpointer data)
2598 {
2599         MsgInfo *msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2600
2601         if (msginfo)
2602                 procmsg_msginfo_free(&msginfo);
2603 }
2604
2605 static void summary_set_marks_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2606                                    gpointer data)
2607 {
2608         SummaryView *summaryview = data;
2609         MsgInfo *msginfo;
2610
2611         msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2612
2613         cm_return_if_fail(msginfo != NULL);
2614
2615         if (MSG_IS_DELETED(msginfo->flags))
2616                 summaryview->deleted++;
2617
2618         summaryview->total_size += msginfo->size;
2619
2620         summary_set_row_marks(summaryview, node);
2621 }
2622
2623 static void summary_update_status(SummaryView *summaryview)
2624 {
2625         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2626         GtkCMCTreeNode *node;
2627         MsgInfo *msginfo;
2628
2629         summaryview->total_size =
2630         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
2631
2632         for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2633              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2634                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2635                 
2636                 if (!msginfo)
2637                         continue;
2638
2639                 if (MSG_IS_DELETED(msginfo->flags))
2640                         summaryview->deleted++;
2641                 if (MSG_IS_MOVE(msginfo->flags))
2642                         summaryview->moved++;
2643                 if (MSG_IS_COPY(msginfo->flags))
2644                         summaryview->copied++;
2645                 summaryview->total_size += msginfo->size;
2646         }
2647 }
2648
2649 static void summary_status_show(SummaryView *summaryview)
2650 {
2651         gchar *str;
2652         gchar *del, *mv, *cp;
2653         gchar *sel;
2654         gchar *spc;
2655         gchar *itstr;
2656         GList *rowlist, *cur;
2657         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
2658         guint n_marked = 0, n_replied = 0, n_forwarded = 0, n_locked = 0, n_ignored = 0, n_watched = 0;
2659         goffset sel_size = 0, n_size = 0;
2660         MsgInfo *msginfo;
2661         gchar *name;
2662         gchar *tooltip;
2663         
2664         if (!summaryview->folder_item) {
2665                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
2666                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
2667                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
2668                 toolbar_main_set_sensitive(summaryview->mainwin);
2669                 return;
2670         }
2671
2672         rowlist = GTK_CMCLIST(summaryview->ctree)->selection;
2673         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2674                 msginfo = gtk_cmctree_node_get_row_data
2675                         (GTK_CMCTREE(summaryview->ctree),
2676                          GTK_CMCTREE_NODE(cur->data));
2677                 if (msginfo) {
2678                         sel_size += msginfo->size;
2679                         n_selected++;
2680                 }
2681         }
2682         
2683         if (summaryview->folder_item->hide_read_msgs 
2684         || summaryview->folder_item->hide_del_msgs
2685         || summaryview->folder_item->hide_read_threads
2686         || quicksearch_has_sat_predicate(summaryview->quicksearch)) {
2687                 rowlist = GTK_CMCLIST(summaryview->ctree)->row_list;
2688                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2689                         msginfo = gtk_cmctree_node_get_row_data
2690                                 (GTK_CMCTREE(summaryview->ctree),
2691                                  GTK_CMCTREE_NODE(cur));
2692                         if (msginfo) {
2693                                 n_size += msginfo->size;
2694                                 n_total++;
2695                                 if (MSG_IS_NEW(msginfo->flags))
2696                                         n_new++;
2697                                 if (MSG_IS_UNREAD(msginfo->flags))
2698                                         n_unread++;
2699                                 if (MSG_IS_MARKED(msginfo->flags))
2700                                         n_marked++;
2701                                 if (MSG_IS_REPLIED(msginfo->flags))
2702                                         n_replied++;
2703                                 if (MSG_IS_FORWARDED(msginfo->flags))
2704                                         n_forwarded++;
2705                                 if (MSG_IS_LOCKED(msginfo->flags))
2706                                         n_locked++;
2707                                 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2708                                         n_ignored++;
2709                                 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2710                                         n_watched++;
2711                         }
2712                 }
2713         } else {
2714                 n_new = summaryview->folder_item->new_msgs;
2715                 n_unread = summaryview->folder_item->unread_msgs;
2716                 n_marked = summaryview->folder_item->marked_msgs;
2717                 n_replied = summaryview->folder_item->replied_msgs;
2718                 n_forwarded = summaryview->folder_item->forwarded_msgs;
2719                 n_locked = summaryview->folder_item->locked_msgs;
2720                 n_ignored = summaryview->folder_item->ignored_msgs;
2721                 n_watched = summaryview->folder_item->watched_msgs;
2722                 n_total = summaryview->folder_item->total_msgs;
2723                 n_size = summaryview->total_size;
2724         }
2725
2726         name = folder_item_get_name(summaryview->folder_item);
2727         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
2728         g_free(name);
2729
2730         if (summaryview->deleted)
2731                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
2732         else
2733                 del = g_strdup("");
2734         if (summaryview->moved)
2735                 mv = g_strdup_printf(_("%s%d moved"),
2736                                      summaryview->deleted ? _(", ") : "",
2737                                      summaryview->moved);
2738         else
2739                 mv = g_strdup("");
2740         if (summaryview->copied)
2741                 cp = g_strdup_printf(_("%s%d copied"),
2742                                      summaryview->deleted ||
2743                                      summaryview->moved ? _(", ") : "",
2744                                      summaryview->copied);
2745         else
2746                 cp = g_strdup("");
2747
2748         if (summaryview->deleted || summaryview->moved || summaryview->copied)
2749                 spc = "    ";
2750         else
2751                 spc = "";
2752
2753         if (n_selected) {
2754                 sel = g_strdup_printf(" (%s)", to_human_readable((goffset)sel_size));
2755                 itstr = g_strdup_printf(ngettext(" item selected"," items selected", n_selected));
2756         } else {
2757                 sel = g_strdup("");
2758                 itstr = g_strdup("");
2759         }
2760                 
2761         if (prefs_common.layout_mode != SMALL_LAYOUT) {
2762                 str = g_strconcat(n_selected ? itos(n_selected) : "",
2763                                                 itstr, sel, spc, del, mv, cp, NULL);
2764                 g_free(sel);
2765                 g_free(del);
2766                 g_free(mv);
2767                 g_free(cp);
2768                 g_free(itstr);
2769                 
2770                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2771                 g_free(str);
2772
2773                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2774                                               n_new, n_unread, n_total,
2775                                               to_human_readable((goffset)n_size));
2776
2777         g_signal_connect(G_OBJECT(summaryview->popupmenu), "selection-done",
2778                         G_CALLBACK(popup_menu_selection_done), summaryview);
2779
2780                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2781                 g_free(str);
2782                 tooltip = g_strdup_printf("<b>%s</b>\n"
2783                                         "<b>%s</b> %d\n"
2784                                         "<b>%s</b> %d\n"
2785                                         "<b>%s</b> %d\n"
2786                                         "<b>%s</b> %s\n\n"
2787                                         "<b>%s</b> %d\n"
2788                                         "<b>%s</b> %d\n"
2789                                         "<b>%s</b> %d\n"
2790                                         "<b>%s</b> %d\n"
2791                                         "<b>%s</b> %d\n"
2792                                         "<b>%s</b> %d",
2793                                         _("Message summary"),
2794                                         _("New:"), n_new,
2795                                         _("Unread:"), n_unread,
2796                                         _("Total:"), n_total,
2797                                         _("Size:"), to_human_readable((goffset)n_size),
2798                                         _("Marked:"), n_marked,
2799                                         _("Replied:"), n_replied,
2800                                         _("Forwarded:"), n_forwarded,
2801                                         _("Locked:"), n_locked,
2802                                         _("Ignored:"), n_ignored,
2803                                         _("Watched:"), n_watched);
2804
2805                 gtk_widget_set_tooltip_markup(GTK_WIDGET(summaryview->statlabel_msgs),
2806                                             tooltip); 
2807                 g_free(tooltip);
2808         } else {
2809                 gchar *ssize, *tsize;
2810                 if (n_selected) {
2811                         ssize = g_strdup(to_human_readable((goffset)sel_size));
2812                         tsize = g_strdup(to_human_readable((goffset)n_size));
2813                         str = g_strdup_printf(_("%d/%d selected (%s/%s), %d unread"),
2814                                 n_selected, n_total, ssize, tsize, n_unread);
2815                         g_free(ssize);
2816                         g_free(tsize);
2817                 } else
2818                         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2819                                 n_new, n_unread, n_total, to_human_readable((goffset)n_size));
2820                 g_free(sel);
2821                 g_free(del);
2822                 g_free(mv);
2823                 g_free(cp);
2824                 g_free(itstr);
2825                 
2826                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2827                 g_free(str);
2828         }
2829
2830         summary_set_menu_sensitive(summaryview);
2831         toolbar_main_set_sensitive(summaryview->mainwin);
2832 }
2833
2834 static void summary_set_column_titles(SummaryView *summaryview)
2835 {
2836         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
2837         FolderItem *item = summaryview->folder_item;
2838         GtkWidget *hbox;
2839         GtkWidget *label;
2840         GtkWidget *arrow;
2841         gint pos;
2842         const gchar *title;
2843         SummaryColumnType type;
2844         GtkJustification justify;
2845
2846         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2847                 SORT_BY_MARK,
2848                 SORT_BY_STATUS,
2849                 SORT_BY_MIME,
2850                 SORT_BY_SUBJECT,
2851                 SORT_BY_FROM,
2852                 SORT_BY_TO,
2853                 SORT_BY_DATE,
2854                 SORT_BY_SIZE,
2855                 SORT_BY_NUMBER,
2856                 SORT_BY_SCORE,
2857                 SORT_BY_LOCKED,
2858                 SORT_BY_TAGS
2859         };
2860
2861         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2862                 type = summaryview->col_state[pos].type;
2863
2864                 /* CLAWS: mime and unread are single char headers */
2865                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2866                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2867
2868                 switch (type) {
2869                 case S_COL_SUBJECT:
2870                 case S_COL_FROM:
2871                 case S_COL_TO:
2872                 case S_COL_DATE:
2873                 case S_COL_NUMBER:
2874                         if(type == S_COL_FROM && item != NULL &&
2875                                         FOLDER_SHOWS_TO_HDR(item) &&
2876                                         !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2877                                 type = S_COL_TO;
2878                         if(type == S_COL_NUMBER)
2879                                 title = gettext(col_label[type]);
2880                         else
2881                                 title = prefs_common_translated_header_name(col_label[type]);
2882                         break;
2883                 default:
2884                         title = gettext(col_label[type]);
2885                 }
2886
2887                 if (type == S_COL_MIME) {
2888                         label = gtk_image_new_from_pixbuf(clipxpm);
2889                         gtk_widget_show(label);
2890                         gtk_cmclist_set_column_widget(clist, pos, label);
2891                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Attachment"));
2892                         continue;
2893                 } else if (type == S_COL_MARK) {
2894                         label = gtk_image_new_from_pixbuf(markxpm);
2895                         gtk_widget_show(label);
2896                         gtk_cmclist_set_column_widget(clist, pos, label);
2897                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Mark"));
2898                         continue;
2899                 } else if (type == S_COL_LOCKED) {
2900                         label = gtk_image_new_from_pixbuf(lockedxpm);
2901                         gtk_widget_show(label);
2902                         gtk_cmclist_set_column_widget(clist, pos, label);
2903                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Locked"));
2904                         continue;
2905                 } else if (type == S_COL_STATUS) {
2906                         gtk_cmclist_set_column_title(clist, pos, title);
2907                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Status"));
2908                         continue;
2909                 }
2910
2911                 hbox  = gtk_hbox_new(FALSE, 4);
2912                 label = gtk_label_new(title);
2913 #ifdef GENERIC_UMPC
2914         gtk_widget_set_size_request(hbox, -1, 20);
2915 #endif
2916
2917                 if (justify == GTK_JUSTIFY_RIGHT)
2918                         gtk_box_pack_end(GTK_BOX(hbox), label,
2919                                          FALSE, FALSE, 0);
2920                 else
2921                         gtk_box_pack_start(GTK_BOX(hbox), label,
2922                                            FALSE, FALSE, 0);
2923
2924                 if (summaryview->sort_key == sort_by[type] ||
2925                     (summaryview->sort_key == SORT_BY_THREAD_DATE &&
2926                      sort_by[SORT_BY_DATE] && type == S_COL_DATE)) {
2927                         arrow = gtk_arrow_new
2928                                 (summaryview->sort_type == SORT_ASCENDING
2929                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2930                                  GTK_SHADOW_IN);
2931                         gtk_widget_set_size_request(GTK_WIDGET(arrow), 10, 10);
2932                         if (justify == GTK_JUSTIFY_RIGHT)
2933                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2934                                                    FALSE, FALSE, 0);
2935                         else
2936                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2937                                                  FALSE, FALSE, 0);
2938                 }
2939
2940                 gtk_widget_show_all(hbox);
2941                 gtk_cmclist_set_column_widget(clist, pos, hbox);
2942         }
2943 }
2944
2945 void summary_reflect_tags_changes(SummaryView *summaryview)
2946 {
2947         GtkMenuShell *menu;
2948         GList *children, *cur;
2949         GtkCMCTreeNode *node;
2950         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2951         gboolean froze = FALSE;
2952         gboolean redisplay = FALSE;
2953
2954         /* re-create colorlabel submenu */
2955         menu = GTK_MENU_SHELL(summaryview->tags_menu);
2956         cm_return_if_fail(menu != NULL);
2957
2958         /* clear items. get item pointers. */
2959         children = gtk_container_get_children(GTK_CONTAINER(menu));
2960         for (cur = children; cur != NULL && cur->data != NULL; cur = cur->next) {
2961                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(cur->data), NULL);
2962         }
2963         g_list_free(children);
2964         summary_tags_menu_create(summaryview, TRUE);
2965
2966         START_LONG_OPERATION(summaryview, TRUE);
2967         for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
2968              node = gtkut_ctree_node_next(ctree, node)) {
2969                 redisplay |= summary_set_row_tag(summaryview,
2970                                            node, TRUE, FALSE, 0);
2971         }
2972         END_LONG_OPERATION(summaryview);
2973         if (redisplay)
2974                 summary_redisplay_msg(summaryview);
2975 }
2976
2977
2978 void summary_reflect_prefs(void)
2979 {
2980         static gchar *last_smallfont = NULL;
2981         static gchar *last_normalfont = NULL;
2982         static gchar *last_boldfont = NULL;
2983         static gboolean last_derive = 0;
2984         gboolean update_font = FALSE;
2985         SummaryView *summaryview = NULL;
2986
2987         if (!mainwindow_get_mainwindow())
2988                 return;
2989         summaryview = mainwindow_get_mainwindow()->summaryview;
2990
2991         if (!last_smallfont || strcmp(last_smallfont, SMALL_FONT) ||
2992                         !last_normalfont || strcmp(last_normalfont, NORMAL_FONT) ||
2993                         !last_boldfont || strcmp(last_boldfont, BOLD_FONT) ||
2994                         last_derive != prefs_common.derive_from_normal_font)
2995                 update_font = TRUE;
2996
2997         g_free(last_smallfont);
2998         last_smallfont = g_strdup(SMALL_FONT);
2999         g_free(last_normalfont);
3000         last_normalfont = g_strdup(NORMAL_FONT);
3001         g_free(last_boldfont);
3002         last_boldfont = g_strdup(BOLD_FONT);
3003         last_derive = prefs_common.derive_from_normal_font;
3004
3005 #define STYLE_FREE(s)                   \
3006         if (s != NULL) {                \
3007                 g_object_unref(s);      \
3008                 s = NULL;               \
3009         }
3010
3011         if (update_font) {
3012                 STYLE_FREE(bold_style);
3013                 STYLE_FREE(bold_style);
3014                 STYLE_FREE(bold_marked_style);
3015                 STYLE_FREE(bold_deleted_style);
3016                 STYLE_FREE(small_style);
3017                 STYLE_FREE(small_marked_style);
3018                 STYLE_FREE(small_deleted_style);
3019                 summary_set_fonts(summaryview);
3020         }
3021
3022 #undef STYLE_FREE
3023
3024         summary_set_column_titles(summaryview);
3025         summary_relayout(summaryview);
3026         
3027         if (summaryview->folder_item)
3028                 summary_show(summaryview, summaryview->folder_item);
3029 }
3030
3031 void summary_sort(SummaryView *summaryview,
3032                   FolderSortKey sort_key, FolderSortType sort_type)
3033 {
3034         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3035         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
3036         GtkCMCListCompareFunc cmp_func = NULL;
3037         START_TIMING("");
3038         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
3039                                        G_CALLBACK(summary_tree_expanded), summaryview);
3040         summary_freeze(summaryview);
3041
3042         switch (sort_key) {
3043         case SORT_BY_MARK:
3044                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_mark;
3045                 break;
3046         case SORT_BY_STATUS:
3047                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_status;
3048                 break;
3049         case SORT_BY_MIME:
3050                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_mime;
3051                 break;
3052         case SORT_BY_NUMBER:
3053                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_num;
3054                 break;
3055         case SORT_BY_SIZE:
3056                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_size;
3057                 break;
3058         case SORT_BY_DATE:
3059                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_date;
3060                 break;
3061         case SORT_BY_THREAD_DATE:
3062                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_thread_date;
3063                 break;
3064         case SORT_BY_FROM:
3065                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_from;
3066                 break;
3067         case SORT_BY_SUBJECT:
3068                 if (summaryview->simplify_subject_preg)
3069                         cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_simplified_subject;
3070                 else
3071                         cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_subject;
3072                 break;
3073         case SORT_BY_SCORE:
3074                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_score;
3075                 break;
3076         case SORT_BY_LABEL:
3077                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_label;
3078                 break;
3079         case SORT_BY_TO:
3080                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_to;
3081                 break;
3082         case SORT_BY_LOCKED:
3083                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_locked;
3084                 break;
3085         case SORT_BY_TAGS:
3086                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_tags;
3087                 break;
3088         case SORT_BY_NONE:
3089                 break;
3090         default:
3091                 goto unlock;
3092         }
3093
3094         summaryview->sort_key = sort_key;
3095         summaryview->sort_type = sort_type;
3096
3097         summary_set_column_titles(summaryview);
3098         summary_set_menu_sensitive(summaryview);
3099
3100         /* allow fallback to don't sort */
3101         if (summaryview->sort_key == SORT_BY_NONE)
3102                 goto unlock;
3103
3104         if (cmp_func != NULL) {
3105                 debug_print("Sorting summary...\n");
3106                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
3107
3108                 main_window_cursor_wait(summaryview->mainwin);
3109
3110                 gtk_cmclist_set_compare_func(clist, cmp_func);
3111
3112                 gtk_cmclist_set_sort_type(clist, (GtkSortType)sort_type);
3113                 gtk_sctree_sort_recursive(ctree, NULL);
3114
3115                 gtk_cmctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
3116
3117                 main_window_cursor_normal(summaryview->mainwin);
3118
3119                 debug_print("Sorting summary done.\n");
3120                 STATUSBAR_POP(summaryview->mainwin);
3121         }
3122 unlock:
3123         summary_thaw(summaryview);
3124         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
3125                                        G_CALLBACK(summary_tree_expanded), summaryview);
3126         END_TIMING();
3127 }
3128
3129 static gboolean summary_update_thread_age(GNode *node, gpointer data)
3130 {
3131         MsgInfo *msginfo = node->data;
3132         time_t *most_recent = (time_t *)data;
3133
3134         if (msginfo->date_t > *most_recent) {
3135                 *most_recent = msginfo->date_t;
3136         }
3137         return FALSE;
3138 }       
3139
3140 static void summary_find_thread_age(GNode *gnode)
3141 {
3142         MsgInfo *msginfo = (MsgInfo *)gnode->data;
3143         time_t most_recent;
3144
3145         if (!msginfo)
3146                 return;
3147         most_recent = msginfo->thread_date = msginfo->date_t;
3148
3149         g_node_traverse(gnode, G_IN_ORDER, G_TRAVERSE_ALL, -1, summary_update_thread_age, &most_recent);
3150
3151         msginfo->thread_date = most_recent;
3152 }
3153
3154 static gboolean summary_update_is_read(GNode *node, gpointer data)
3155 {
3156         MsgInfo *msginfo = node->data;
3157         gboolean *all_read = (gboolean *)data;
3158
3159         if (MSG_IS_UNREAD(msginfo->flags)) {
3160                 *all_read = FALSE;
3161                 return TRUE;
3162         }
3163         return FALSE;
3164 }       
3165
3166 static gboolean summary_thread_is_read(GNode *gnode)
3167 {
3168         MsgInfo *msginfo = (MsgInfo *)gnode->data;
3169         gboolean all_read = TRUE;
3170
3171         if (!msginfo)
3172                 return all_read;
3173
3174         g_node_traverse(gnode, G_IN_ORDER, G_TRAVERSE_ALL, -1, summary_update_is_read, &all_read);
3175     return all_read;
3176 }
3177
3178 typedef struct  _ThreadSelectedData {
3179         guint msgnum;
3180         gboolean is_selected;
3181 } ThreadSelectedData;
3182
3183 static gboolean summary_update_is_selected(GNode *gnode, gpointer data)
3184 {
3185         ThreadSelectedData *selected = (ThreadSelectedData *)data;
3186         MsgInfo *msginfo = (MsgInfo *)gnode->data;
3187
3188         if (msginfo->msgnum == selected->msgnum) {
3189                 selected->is_selected = TRUE;
3190                 return TRUE;
3191         }
3192
3193         return FALSE;
3194 }
3195
3196 static gboolean summary_thread_is_selected(GNode *gnode, guint selected_msgnum)
3197 {
3198         ThreadSelectedData selected;
3199
3200         selected.msgnum = selected_msgnum;
3201         selected.is_selected = FALSE;
3202         g_node_traverse(gnode, G_IN_ORDER, G_TRAVERSE_ALL, -1,
3203                         summary_update_is_selected, &selected);
3204         return selected.is_selected;
3205 }
3206
3207 static gboolean summary_insert_gnode_func(GtkCMCTree *ctree, guint depth, GNode *gnode,
3208                                    GtkCMCTreeNode *cnode, gpointer data)
3209 {
3210         SummaryView *summaryview = (SummaryView *)data;
3211         MsgInfo *msginfo = (MsgInfo *)gnode->data;
3212         gchar *text[N_SUMMARY_COLS];
3213         gint *col_pos = summaryview->col_pos;
3214         const gchar *msgid = msginfo->msgid;
3215         GHashTable *msgid_table = summaryview->msgid_table;
3216         gboolean vert_layout = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3217         gboolean small_layout = (prefs_common.layout_mode == SMALL_LAYOUT);
3218
3219         summary_set_header(summaryview, text, msginfo);
3220
3221         gtk_cmctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
3222                                 NULL, NULL, FALSE, summaryview->threaded && !summaryview->thread_collapsed);
3223 #define SET_TEXT(col) {                                         \
3224         gtk_cmctree_node_set_text(ctree, cnode, col_pos[col],   \
3225                                 text[col_pos[col]]);            \
3226 }
3227
3228         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
3229                 SET_TEXT(S_COL_NUMBER);
3230         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
3231                 SET_TEXT(S_COL_SCORE);
3232         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
3233                 SET_TEXT(S_COL_SIZE);
3234         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible)
3235                 SET_TEXT(S_COL_DATE);
3236         if (summaryview->col_state[summaryview->col_pos[S_COL_FROM]].visible)
3237                 SET_TEXT(S_COL_FROM);
3238         if (summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
3239                 SET_TEXT(S_COL_TO);
3240         if (summaryview->col_state[summaryview->col_pos[S_COL_TAGS]].visible)
3241                 SET_TEXT(S_COL_TAGS);
3242
3243         if ((vert_layout || small_layout) && prefs_common.two_line_vert)
3244                 g_free(text[summaryview->col_pos[S_COL_SUBJECT]]);
3245
3246 #undef SET_TEXT
3247
3248         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
3249         summary_set_marks_func(ctree, cnode, summaryview);
3250
3251         if (msgid && msgid[0] != '\0')
3252                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
3253
3254         return TRUE;
3255 }
3256
3257 static void summary_set_ctree_from_list(SummaryView *summaryview,
3258                                         GSList *mlist, guint selected_msgnum)
3259 {
3260         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3261         MsgInfo *msginfo;
3262         GtkCMCTreeNode *node = NULL;
3263         GHashTable *msgid_table;
3264         GHashTable *subject_table = NULL;
3265         GSList * cur;
3266         gboolean vert_layout = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3267         gboolean small_layout = (prefs_common.layout_mode == SMALL_LAYOUT);
3268         START_TIMING("");
3269         
3270         if (!mlist) return;
3271
3272         debug_print("Setting summary from message data...\n");
3273         STATUSBAR_PUSH(summaryview->mainwin,
3274                        _("Setting summary from message data..."));
3275         gdk_flush();
3276
3277         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3278                                        G_CALLBACK(summary_tree_expanded), summaryview);
3279
3280         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
3281         summaryview->msgid_table = msgid_table;
3282
3283         if (prefs_common.thread_by_subject) {
3284                 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
3285                 summaryview->subject_table = subject_table;
3286         } else {
3287                 summaryview->subject_table = NULL;
3288         }
3289
3290         if (prefs_common.use_addr_book)
3291                 start_address_completion(NULL);
3292         
3293         if (summaryview->threaded) {
3294                 GNode *root, *gnode;
3295                 START_TIMING("threaded");
3296                 root = procmsg_get_thread_tree(mlist);
3297
3298                 
3299                 for (gnode = root->children; gnode != NULL;
3300                      gnode = gnode->next) {
3301                         if (!summaryview->folder_item->hide_read_threads ||
3302                             !summary_thread_is_read(gnode) ||
3303                             summary_thread_is_selected(gnode, selected_msgnum)) {
3304                                 summary_find_thread_age(gnode);
3305                                 node = gtk_sctree_insert_gnode
3306                                         (ctree, NULL, node, gnode,
3307                                          summary_insert_gnode_func, summaryview);
3308                         }
3309                 }
3310
3311                 g_node_destroy(root);
3312                 
3313                 END_TIMING();
3314         } else {
3315                 gchar *text[N_SUMMARY_COLS];
3316                 START_TIMING("unthreaded");
3317                 cur = mlist;
3318                 for (; mlist != NULL; mlist = mlist->next) {
3319                         msginfo = (MsgInfo *)mlist->data;
3320
3321                         summary_set_header(summaryview, text, msginfo);
3322
3323                         node = gtk_sctree_insert_node
3324                                 (ctree, NULL, node, text, 2,
3325                                  NULL, NULL,
3326                                  FALSE, FALSE);
3327                         if ((vert_layout || small_layout) && prefs_common.two_line_vert)
3328                                 g_free(text[summaryview->col_pos[S_COL_SUBJECT]]);
3329
3330                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
3331                         summary_set_marks_func(ctree, node, summaryview);
3332
3333                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
3334                                 g_hash_table_insert(msgid_table,
3335                                                     msginfo->msgid, node);
3336
3337                         if (prefs_common.thread_by_subject)
3338                                 subject_table_insert(subject_table,
3339                                              msginfo->subject,
3340                                              node);
3341                 }
3342                 mlist = cur;
3343                 END_TIMING();
3344         }
3345
3346         if (prefs_common.enable_hscrollbar &&
3347             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
3348                 gint optimal_width;
3349
3350                 optimal_width = gtk_cmclist_optimal_column_width
3351                         (GTK_CMCLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
3352                 gtk_cmclist_set_column_width(GTK_CMCLIST(ctree),
3353                                            summaryview->col_pos[S_COL_SUBJECT],
3354                                            optimal_width);
3355         }
3356
3357         if (prefs_common.use_addr_book)
3358                 end_address_completion();
3359
3360         debug_print("Setting summary from message data done.\n");
3361         STATUSBAR_POP(summaryview->mainwin);
3362         if (debug_get_mode()) {
3363                 debug_print("\tmsgid hash table size = %d\n",
3364                             g_hash_table_size(msgid_table));
3365                 if (prefs_common.thread_by_subject)
3366                         debug_print("\tsubject hash table size = %d\n",
3367                             g_hash_table_size(subject_table));
3368         }
3369
3370         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
3371
3372         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
3373
3374         if (prefs_common.bold_unread) {
3375                 START_TIMING("bold_unread");
3376                 while (node) {
3377                         GtkCMCTreeNode *next = GTK_CMCTREE_NODE_NEXT(node);
3378                         if (GTK_CMCTREE_ROW(node)->children)
3379                                 summary_set_row_marks(summaryview, node);
3380                         node = next;
3381                 }
3382                 END_TIMING();
3383         }
3384
3385         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3386                                        G_CALLBACK(summary_tree_expanded), summaryview);
3387         END_TIMING();
3388 }
3389
3390 static gchar *summary_complete_address(const gchar *addr)
3391 {
3392         gint count;
3393         gchar *res, *tmp, *email_addr;
3394         
3395         if (addr == NULL || !strchr(addr, '@'))
3396                 return NULL;
3397
3398         Xstrdup_a(email_addr, addr, return NULL);
3399         extract_address(email_addr);
3400         if (!*email_addr)
3401                 return NULL;
3402
3403         /*
3404          * completion stuff must be already initialized
3405          */
3406         res = NULL;
3407         if (1 < (count = complete_address(email_addr))) {
3408                 tmp = get_complete_address(1);
3409                 res = procheader_get_fromname(tmp);
3410                 g_free(tmp);
3411         }
3412
3413         return res;
3414 }
3415
3416 static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
3417                                MsgInfo *msginfo)
3418 {
3419         static gchar date_modified[80];
3420         static gchar col_score[11];
3421         static gchar from_buf[BUFFSIZE], to_buf[BUFFSIZE];
3422         static gchar tmp1[BUFFSIZE], tmp2[BUFFSIZE], tmp3[BUFFSIZE];
3423         gint *col_pos = summaryview->col_pos;
3424         gchar *from_text = NULL, *to_text = NULL, *tags_text = NULL;
3425         gboolean should_swap = FALSE;
3426         gboolean vert_layout = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3427         gboolean small_layout = (prefs_common.layout_mode == SMALL_LAYOUT);
3428         static const gchar *color_dim_rgb = NULL;
3429         if (!color_dim_rgb)
3430                 color_dim_rgb = gdk_color_to_string(&summaryview->color_dim);
3431         text[col_pos[S_COL_FROM]]   = "";
3432         text[col_pos[S_COL_TO]]     = "";
3433         text[col_pos[S_COL_SUBJECT]]= "";
3434         text[col_pos[S_COL_MARK]]   = "";
3435         text[col_pos[S_COL_STATUS]] = "";
3436         text[col_pos[S_COL_MIME]]   = "";
3437         text[col_pos[S_COL_LOCKED]] = "";
3438         text[col_pos[S_COL_DATE]]   = "";
3439         text[col_pos[S_COL_TAGS]]   = "";
3440         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
3441                 text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
3442         else
3443                 text[col_pos[S_COL_NUMBER]] = "";
3444
3445         /* slow! */
3446         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
3447                 text[col_pos[S_COL_SIZE]] = to_human_readable(msginfo->size);
3448         else
3449                 text[col_pos[S_COL_SIZE]] = "";
3450
3451         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
3452                 text[col_pos[S_COL_SCORE]] = itos_buf(col_score, msginfo->score);
3453         else
3454                 text[col_pos[S_COL_SCORE]] = "";
3455
3456         if (summaryview->col_state[summaryview->col_pos[S_COL_TAGS]].visible) {
3457                 tags_text = procmsg_msginfo_get_tags_str(msginfo);
3458                 if (!tags_text) {
3459                         text[col_pos[S_COL_TAGS]] = "-";
3460                 } else {
3461                         strncpy2(tmp1, tags_text, sizeof(tmp1));
3462                         tmp1[sizeof(tmp1)-1]='\0';
3463                         g_free(tags_text);
3464                         text[col_pos[S_COL_TAGS]] = tmp1;
3465                 }
3466         } else
3467                 text[col_pos[S_COL_TAGS]] = "";
3468
3469         /* slow! */
3470         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible || 
3471             ((vert_layout || small_layout) && prefs_common.two_line_vert)) {
3472                 if (msginfo->date_t && msginfo->date_t > 0) {
3473                         procheader_date_get_localtime(date_modified,
3474                                                       sizeof(date_modified),
3475                                                       msginfo->date_t);
3476                         text[col_pos[S_COL_DATE]] = date_modified;
3477                 } else if (msginfo->date)
3478                         text[col_pos[S_COL_DATE]] = msginfo->date;
3479                 else
3480                         text[col_pos[S_COL_DATE]] = _("(No Date)");
3481         }
3482         
3483         if (prefs_common.swap_from && msginfo->from && msginfo->to
3484         &&  !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible) {
3485                 gchar *addr = NULL;
3486                 
3487                 addr = g_strdup(msginfo->from);
3488
3489                 if (addr) {
3490                         extract_address(addr);
3491                         if (account_find_from_address(addr, FALSE)) {
3492                                 should_swap = TRUE;
3493                         }
3494                         g_free(addr);
3495                 }
3496         }
3497
3498         if (!prefs_common.use_addr_book) {
3499                 if (prefs_common.summary_from_show == SHOW_NAME)
3500                         from_text = msginfo->fromname;
3501                 else if (prefs_common.summary_from_show == SHOW_BOTH)
3502                         from_text = msginfo->from;
3503                 else {
3504                         from_text = msginfo->from;
3505                         extract_address(from_text);
3506                 }
3507                 if (!from_text)
3508                         from_text = _("(No From)");
3509         } else {
3510                 gchar *tmp = summary_complete_address(msginfo->from);
3511                 if (tmp) {
3512                         strncpy2(from_buf, tmp, sizeof(from_buf));
3513                         g_free(tmp);
3514                         from_text = from_buf;
3515                 } else {
3516                         if (prefs_common.summary_from_show == SHOW_NAME)
3517                                 from_text = msginfo->fromname;
3518                         else if (prefs_common.summary_from_show == SHOW_BOTH)
3519                                 from_text = msginfo->from;
3520                         else {
3521                                 from_text = msginfo->from;
3522                                 if (from_text)
3523                                         extract_address(from_text);
3524                         }
3525                         if (!from_text)
3526                                 from_text = _("(No From)");
3527                 }
3528         }
3529         
3530         to_text = msginfo->to ? msginfo->to : 
3531                    (msginfo->cc ? msginfo->cc :
3532                      (msginfo->newsgroups ? msginfo->newsgroups : NULL
3533                      )
3534                    );
3535
3536         if (!to_text)
3537                 to_text = _("(No Recipient)");
3538         else {
3539                 if (prefs_common.summary_from_show == SHOW_NAME) {
3540                         gchar *tmp = procheader_get_fromname(to_text);
3541                         /* need to keep to_text pointing to stack, so heap-allocated
3542                          * string from procheader_get_fromname() will be copied to to_buf */
3543                         if (tmp != NULL) {
3544                                 strncpy2(to_buf, tmp, sizeof(to_buf));
3545                                 g_free(tmp);
3546                                 to_text = to_buf;
3547                         }
3548                 } else if (prefs_common.summary_from_show == SHOW_ADDR)
3549                         extract_address(to_text);
3550         }
3551
3552         text[col_pos[S_COL_TO]] = to_text;
3553         if (!should_swap) {
3554                 text[col_pos[S_COL_FROM]] = from_text;
3555         } else {
3556                 if (prefs_common.use_addr_book) {
3557                         gchar *tmp = summary_complete_address(to_text);
3558                         /* need to keep to_text pointing to stack, so heap-allocated
3559                          * string from summary_complete_address() will be copied to to_buf */
3560                         if (tmp) {
3561                                 strncpy2(to_buf, tmp, sizeof(to_buf));
3562                                 g_free(tmp);
3563                                 to_text = to_buf;
3564                         } else {
3565                                 to_text = to_text ? to_text : _("(No From)");
3566                         }
3567                 }
3568                 snprintf(tmp2, BUFFSIZE+4, "➜ %s", to_text);
3569                 tmp2[BUFFSIZE-1]='\0';
3570                 text[col_pos[S_COL_FROM]] = tmp2;
3571         }
3572         
3573         if (summaryview->simplify_subject_preg != NULL)
3574                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
3575                         string_remove_match(tmp3, BUFFSIZE, msginfo->subject, 
3576                                         summaryview->simplify_subject_preg) : 
3577                         _("(No Subject)");
3578         else 
3579                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
3580                         _("(No Subject)");
3581         if ((vert_layout || small_layout) && prefs_common.two_line_vert) {
3582                 if (!FOLDER_SHOWS_TO_HDR(summaryview->folder_item)) {
3583                         gchar *tmp = g_markup_printf_escaped(g_strconcat("%s\n",
3584                                                                         "<span color='%s' style='italic'>",
3585                                                                         _("From: %s, on %s"), "</span>", NULL),
3586                                         text[col_pos[S_COL_SUBJECT]],
3587                                         color_dim_rgb,
3588                                         text[col_pos[S_COL_FROM]],
3589                                         text[col_pos[S_COL_DATE]]);
3590                         text[col_pos[S_COL_SUBJECT]] = tmp;
3591                 } else {
3592                         gchar *tmp = g_markup_printf_escaped(g_strconcat("%s\n",
3593                                                                          "<span color='%s' style='italic'>",
3594                                                                         _("To: %s, on %s"), "</span>", NULL),
3595                                         text[col_pos[S_COL_SUBJECT]],
3596                                         color_dim_rgb,
3597                                         text[col_pos[S_COL_TO]],
3598                                         text[col_pos[S_COL_DATE]]);
3599                         text[col_pos[S_COL_SUBJECT]] = tmp;
3600                 }
3601         }
3602 }
3603
3604 static void summary_display_msg(SummaryView *summaryview, GtkCMCTreeNode *row)
3605 {
3606         summary_display_msg_full(summaryview, row, FALSE, FALSE);
3607 }
3608
3609 static gboolean defer_change(gpointer data);
3610 typedef enum {
3611         FLAGS_UNSET,
3612         FLAGS_SET,
3613         FLAGS_CHANGE
3614 } ChangeType;
3615 typedef struct _ChangeData {
3616         MsgInfo *info;
3617         ChangeType op;
3618         MsgPermFlags set_flags;
3619         MsgTmpFlags  set_tmp_flags;
3620         MsgPermFlags unset_flags;
3621         MsgTmpFlags  unset_tmp_flags;
3622 } ChangeData;
3623
3624 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
3625 {
3626         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3627                 debug_print("flags: doing unset now\n");
3628                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
3629         } else {
3630                 ChangeData *unset_data = g_new0(ChangeData, 1);
3631                 unset_data->info = msginfo;
3632                 unset_data->op = FLAGS_UNSET;
3633                 unset_data->unset_flags = flags;
3634                 unset_data->unset_tmp_flags = tmp_flags;
3635                 debug_print("flags: deferring unset\n");
3636                 g_timeout_add(100, defer_change, unset_data);
3637         }
3638 }
3639
3640 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
3641 {
3642         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3643                 debug_print("flags: doing set now\n");
3644                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
3645         } else {
3646                 ChangeData *set_data = g_new0(ChangeData, 1);
3647                 set_data->info = msginfo;
3648                 set_data->op = FLAGS_SET;
3649                 set_data->set_flags = flags;
3650                 set_data->set_tmp_flags = tmp_flags;
3651                 debug_print("flags: deferring set\n");
3652                 g_timeout_add(100, defer_change, set_data);
3653         }
3654 }
3655
3656 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
3657                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
3658                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
3659 {
3660         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3661                 debug_print("flags: doing change now\n");
3662                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
3663                         rem_flags, rem_tmp_flags);
3664         } else {
3665                 ChangeData *change_data = g_new0(ChangeData, 1);
3666                 change_data->info = msginfo;
3667                 change_data->op = FLAGS_CHANGE;
3668                 change_data->set_flags = add_flags;
3669                 change_data->set_tmp_flags = add_tmp_flags;
3670                 change_data->unset_flags = rem_flags;
3671                 change_data->unset_tmp_flags = rem_tmp_flags;
3672                 debug_print("flags: deferring change\n");
3673                 g_timeout_add(100, defer_change, change_data);
3674         }
3675 }
3676
3677 gboolean defer_change(gpointer data)
3678 {
3679         ChangeData *chg = (ChangeData *)data;
3680         if (chg->info->folder && chg->info->folder->processing_pending) {
3681                 debug_print("flags: trying later\n");
3682                 return TRUE; /* try again */
3683         } else {
3684                 debug_print("flags: finally doing it\n");
3685                 switch(chg->op) {
3686                 case FLAGS_UNSET:
3687                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
3688                         break;
3689                 case FLAGS_SET:
3690                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
3691                         break;
3692                 case FLAGS_CHANGE:
3693                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
3694                                 chg->unset_flags, chg->unset_tmp_flags);
3695                         break;