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