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