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