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