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