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