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