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