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