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