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