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