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