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