a32b510d95595730feed1565bf7e69e0882981ca
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2008 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtk.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32
33 #include "main.h"
34 #include "menu.h"
35 #include "mbox.h"
36 #include "mainwindow.h"
37 #include "folderview.h"
38 #include "summaryview.h"
39 #include "messageview.h"
40 #include "foldersel.h"
41 #include "procmsg.h"
42 #include "procheader.h"
43 #include "sourcewindow.h"
44 #include "prefs_common.h"
45 #include "prefs_summary_column.h"
46 #include "prefs_summary_open.h"
47 #include "prefs_filtering.h"
48 #include "account.h"
49 #include "compose.h"
50 #include "utils.h"
51 #include "gtkutils.h"
52 #include "stock_pixmap.h"
53 #include "filesel.h"
54 #include "alertpanel.h"
55 #include "inputdialog.h"
56 #include "statusbar.h"
57 #include "folder.h"
58 #include "colorlabel.h"
59 #include "inc.h"
60 #include "imap.h"
61 #include "addressbook.h"
62 #include "addr_compl.h"
63 #include "folder_item_prefs.h"
64 #include "filtering.h"
65 #include "string_match.h"
66 #include "toolbar.h"
67 #include "news.h"
68 #include "hooks.h"
69 #include "description_window.h"
70 #include "folderutils.h"
71 #include "quicksearch.h"
72 #include "partial_download.h"
73 #include "tags.h"
74 #include "timing.h"
75 #include "gedit-print.h"
76 #include "log.h"
77 #include "edittags.h"
78 #include "manual.h"
79
80 #define SUMMARY_COL_MARK_WIDTH          10
81 #define SUMMARY_COL_STATUS_WIDTH        13
82 #define SUMMARY_COL_LOCKED_WIDTH        13
83 #define SUMMARY_COL_MIME_WIDTH          11
84
85 static int normal_row_height = -1;
86 static GtkStyle *bold_style;
87 static GtkStyle *bold_marked_style;
88 static GtkStyle *bold_deleted_style;
89 static GtkStyle *small_style;
90 static GtkStyle *small_marked_style;
91 static GtkStyle *small_deleted_style;
92
93 static GdkPixmap *markxpm;
94 static GdkBitmap *markxpmmask;
95 static GdkPixmap *deletedxpm;
96 static GdkBitmap *deletedxpmmask;
97 static GdkPixmap *movedxpm;
98 static GdkBitmap *movedxpmmask;
99 static GdkPixmap *copiedxpm;
100 static GdkBitmap *copiedxpmmask;
101
102 static GdkPixmap *newxpm;
103 static GdkBitmap *newxpmmask;
104 static GdkPixmap *unreadxpm;
105 static GdkBitmap *unreadxpmmask;
106 static GdkPixmap *repliedxpm;
107 static GdkBitmap *repliedxpmmask;
108 static GdkPixmap *forwardedxpm;
109 static GdkBitmap *forwardedxpmmask;
110 static GdkPixmap *ignorethreadxpm;
111 static GdkBitmap *ignorethreadxpmmask;
112 static GdkPixmap *watchthreadxpm;
113 static GdkBitmap *watchthreadxpmmask;
114 static GdkPixmap *lockedxpm;
115 static GdkBitmap *lockedxpmmask;
116 static GdkPixmap *spamxpm;
117 static GdkBitmap *spamxpmmask;
118
119 static GdkPixmap *clipxpm;
120 static GdkBitmap *clipxpmmask;
121 static GdkPixmap *keyxpm;
122 static GdkBitmap *keyxpmmask;
123 static GdkPixmap *clipkeyxpm;
124 static GdkBitmap *clipkeyxpmmask;
125 static GdkPixmap *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         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "ReplyTo", "SummaryViewPopup/ReplyTo", GTK_UI_MANAGER_MENU)
814         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Separator1", "SummaryViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
815         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Forward", "SummaryViewPopup/Forward", GTK_UI_MANAGER_MENUITEM)
816 #ifndef GENERIC_UMPC
817         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "ForwardAsAtt", "SummaryViewPopup/ForwardAsAtt", GTK_UI_MANAGER_MENUITEM)
818         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Redirect", "SummaryViewPopup/Redirect", GTK_UI_MANAGER_MENUITEM)
819 #endif
820         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Separator2", "SummaryViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
821         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Move", "SummaryViewPopup/Move", GTK_UI_MANAGER_MENUITEM)
822         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Copy", "SummaryViewPopup/Copy", GTK_UI_MANAGER_MENUITEM)
823         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Trash", "SummaryViewPopup/Trash", GTK_UI_MANAGER_MENUITEM)
824 #ifndef GENERIC_UMPC
825         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Delete", "SummaryViewPopup/Delete", GTK_UI_MANAGER_MENUITEM)
826 #endif
827         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Separator3", "SummaryViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
828         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Mark", "SummaryViewPopup/Mark", GTK_UI_MANAGER_MENU)
829         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "ColorLabel", "SummaryViewPopup/ColorLabel", GTK_UI_MANAGER_MENU)
830         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Tags", "SummaryViewPopup/Tags", GTK_UI_MANAGER_MENU)
831
832         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Separator4", "SummaryViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
833 #ifndef GENERIC_UMPC
834         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "AddSenderToAB", "SummaryViewPopup/AddSenderToAB", GTK_UI_MANAGER_MENUITEM)
835 #endif
836         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "CreateFilterRule", "SummaryViewPopup/CreateFilterRule", GTK_UI_MANAGER_MENU)
837 #ifndef GENERIC_UMPC
838         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "CreateProcessingRule", "SummaryViewPopup/CreateProcessingRule", GTK_UI_MANAGER_MENU)
839 #endif
840         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Separator5", "SummaryViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
841         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "View", "SummaryViewPopup/View", GTK_UI_MANAGER_MENU)
842         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "SaveAs", "SummaryViewPopup/SaveAs", GTK_UI_MANAGER_MENUITEM)
843 #ifndef GENERIC_UMPC
844         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Print", "SummaryViewPopup/Print", GTK_UI_MANAGER_MENUITEM)
845 #endif
846         /* last separator, for plugins */
847         MENUITEM_ADDUI("/Menus/SummaryViewPopup", "Separator6", "SummaryViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
848
849         /* submenus - replyto */
850         MENUITEM_ADDUI("/Menus/SummaryViewPopup/ReplyTo", "All", "SummaryViewPopup/ReplyTo/All", GTK_UI_MANAGER_MENUITEM)
851         MENUITEM_ADDUI("/Menus/SummaryViewPopup/ReplyTo", "Sender", "SummaryViewPopup/ReplyTo/Sender", GTK_UI_MANAGER_MENUITEM)
852         MENUITEM_ADDUI("/Menus/SummaryViewPopup/ReplyTo", "MailingList", "SummaryViewPopup/ReplyTo/MailingList", GTK_UI_MANAGER_MENUITEM)
853
854         /* submenus - mark */
855         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Mark", "SummaryViewPopup/Mark/Mark", GTK_UI_MANAGER_MENUITEM)
856         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Unmark", "SummaryViewPopup/Mark/Unmark", GTK_UI_MANAGER_MENUITEM)
857         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Separator1", "SummaryViewPopup/Mark/---", GTK_UI_MANAGER_SEPARATOR)
858         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "MarkUnread", "SummaryViewPopup/Mark/MarkUnread", GTK_UI_MANAGER_MENUITEM)
859         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "MarkRead", "SummaryViewPopup/Mark/MarkRead", GTK_UI_MANAGER_MENUITEM)
860         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "MarkAllRead", "SummaryViewPopup/Mark/MarkAllRead", GTK_UI_MANAGER_MENUITEM)
861         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "IgnoreThread", "SummaryViewPopup/Mark/IgnoreThread", GTK_UI_MANAGER_MENUITEM)
862         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "UnignoreThread", "SummaryViewPopup/Mark/UnignoreThread", GTK_UI_MANAGER_MENUITEM)
863         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "WatchThread", "SummaryViewPopup/Mark/WatchThread", GTK_UI_MANAGER_MENUITEM)
864         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "UnwatchThread", "SummaryViewPopup/Mark/UnwatchThread", GTK_UI_MANAGER_MENUITEM)
865         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Separator2", "SummaryViewPopup/Mark/---", GTK_UI_MANAGER_SEPARATOR)
866         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "MarkSpam", "SummaryViewPopup/Mark/MarkSpam", GTK_UI_MANAGER_MENUITEM)
867         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "MarkHam", "SummaryViewPopup/Mark/MarkHam", GTK_UI_MANAGER_MENUITEM)
868         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Separator3", "SummaryViewPopup/Mark/---", GTK_UI_MANAGER_SEPARATOR)
869         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Lock", "SummaryViewPopup/Mark/Lock", GTK_UI_MANAGER_MENUITEM)
870         MENUITEM_ADDUI("/Menus/SummaryViewPopup/Mark", "Unlock", "SummaryViewPopup/Mark/Unlock", GTK_UI_MANAGER_MENUITEM)
871
872         /* submenus - colorlabel and tags are dynamic */
873         /* submenus - createfilterrule */
874         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateFilterRule", "Automatically", "SummaryViewPopup/CreateFilterRule/Automatically", GTK_UI_MANAGER_MENUITEM)
875         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateFilterRule", "ByFrom", "SummaryViewPopup/CreateFilterRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
876         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateFilterRule", "ByTo", "SummaryViewPopup/CreateFilterRule/ByTo", GTK_UI_MANAGER_MENUITEM)
877         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateFilterRule", "BySubject", "SummaryViewPopup/CreateFilterRule/BySubject", GTK_UI_MANAGER_MENUITEM)
878                 
879 #ifndef GENERIC_UMPC
880         /* submenus - createprocessingrule */
881         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateProcessingRule", "Automatically", "SummaryViewPopup/CreateProcessingRule/Automatically", GTK_UI_MANAGER_MENUITEM)
882         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateProcessingRule", "ByFrom", "SummaryViewPopup/CreateProcessingRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
883         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateProcessingRule", "ByTo", "SummaryViewPopup/CreateProcessingRule/ByTo", GTK_UI_MANAGER_MENUITEM)
884         MENUITEM_ADDUI("/Menus/SummaryViewPopup/CreateProcessingRule", "BySubject", "SummaryViewPopup/CreateProcessingRule/BySubject", GTK_UI_MANAGER_MENUITEM)
885 #endif
886                 
887         /* submenus - view */
888         MENUITEM_ADDUI("/Menus/SummaryViewPopup/View", "OpenInNewWindow", "SummaryViewPopup/View/OpenInNewWindow", GTK_UI_MANAGER_MENUITEM)
889         MENUITEM_ADDUI("/Menus/SummaryViewPopup/View", "MessageSource", "SummaryViewPopup/View/MessageSource", GTK_UI_MANAGER_MENUITEM)
890 #ifndef GENERIC_UMPC
891         MENUITEM_ADDUI("/Menus/SummaryViewPopup/View", "AllHeaders", "SummaryViewPopup/View/AllHeaders", GTK_UI_MANAGER_MENUITEM)
892 #endif          
893         summaryview->popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
894                                 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/SummaryViewPopup")) );
895
896
897         summaryview->vbox = vbox;
898         summaryview->scrolledwin = scrolledwin;
899         summaryview->ctree = ctree;
900         summaryview->hbox = hbox;
901         summaryview->hbox_l = hbox_l;
902         summaryview->hbox_spc = hbox_spc;
903         summaryview->stat_box = stat_box;
904         summaryview->stat_box2 = stat_box2;
905         summaryview->statlabel_folder = statlabel_folder;
906         summaryview->statlabel_select = statlabel_select;
907         summaryview->statlabel_msgs = statlabel_msgs;
908         summaryview->toggle_eventbox = toggle_eventbox;
909         summaryview->toggle_arrow = toggle_arrow;
910 #ifdef GENERIC_UMPC
911         summaryview->multiple_sel_togbtn = multiple_sel_togbtn;
912 #endif
913         summaryview->toggle_search = toggle_search;
914         summaryview->lock_count = 0;
915         summaryview->msginfo_update_callback_id =
916                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
917         summaryview->folder_item_update_callback_id =
918                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST,
919                                 summary_update_folder_item_hook,
920                                 (gpointer) summaryview);
921         summaryview->folder_update_callback_id =
922                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
923                                 summary_update_folder_hook,
924                                 (gpointer) summaryview);
925
926         summaryview->target_list = gtk_target_list_new(summary_drag_types, 2);
927
928         summaryview->quicksearch = quicksearch;
929
930         /* CLAWS: need this to get the SummaryView * from
931          * the CList */
932         g_object_set_data(G_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
933
934         gtk_widget_show_all(vbox);
935
936         gtk_widget_show(vbox);
937
938         if (prefs_common.show_searchbar)
939                 quicksearch_show(quicksearch);
940         else
941                 quicksearch_hide(quicksearch);
942         
943         if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT ||
944             prefs_common.layout_mode == SMALL_LAYOUT)
945                 gtk_widget_hide(summaryview->toggle_eventbox);
946
947         return summaryview;
948 }
949
950 void summary_relayout(SummaryView *summaryview)
951 {
952         gtk_widget_realize(summaryview->stat_box);
953
954         g_object_ref(summaryview->hbox_l);
955         g_object_ref(summaryview->statlabel_msgs);
956         
957         gtkut_container_remove(GTK_CONTAINER(summaryview->hbox_l->parent), summaryview->hbox_l);
958         gtkut_container_remove(GTK_CONTAINER(summaryview->statlabel_msgs->parent), summaryview->statlabel_msgs);
959
960         switch (prefs_common.layout_mode) {
961         case NORMAL_LAYOUT:
962         case WIDE_LAYOUT:
963         case WIDE_MSGLIST_LAYOUT:
964                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
965                 gtk_box_pack_end(GTK_BOX(summaryview->stat_box), summaryview->statlabel_msgs, FALSE, FALSE, 4);
966                 gtk_widget_show_all(summaryview->stat_box);
967                 gtk_widget_show_all(summaryview->stat_box2);
968                 if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT ||
969                     prefs_common.layout_mode == SMALL_LAYOUT)
970                         gtk_widget_hide(summaryview->toggle_eventbox);
971                 else
972                         gtk_widget_show(summaryview->toggle_eventbox);
973                 break;
974         case VERTICAL_LAYOUT:
975         case SMALL_LAYOUT:
976                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
977                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box2), summaryview->statlabel_msgs, FALSE, FALSE, 4);
978                 gtk_widget_show_all(summaryview->stat_box);
979                 gtk_widget_show_all(summaryview->stat_box2);
980                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
981                         gtk_widget_hide(summaryview->toggle_eventbox);
982                         gtk_widget_hide(summaryview->statlabel_msgs);
983                 } else {
984                         gtk_widget_show(summaryview->toggle_eventbox);
985                         gtk_widget_show(summaryview->statlabel_msgs);
986                 }
987                         
988                 break;
989         }
990         summary_set_column_order(summaryview);
991
992         g_object_unref(summaryview->hbox_l);
993         g_object_unref(summaryview->statlabel_msgs);
994         quicksearch_relayout(summaryview->quicksearch);
995         if (prefs_common.show_searchbar)
996                 quicksearch_show(summaryview->quicksearch);
997         else
998                 quicksearch_hide(summaryview->quicksearch);
999 }
1000
1001 static void summary_set_fonts(SummaryView *summaryview)
1002 {
1003         PangoFontDescription *font_desc;
1004         gint size;
1005
1006         font_desc = pango_font_description_from_string(NORMAL_FONT);
1007         if (font_desc) {
1008                 gtk_widget_modify_font(summaryview->ctree, font_desc);
1009                 pango_font_description_free(font_desc);
1010         }
1011
1012         if (!bold_style) {
1013                 bold_style = gtk_style_copy
1014                         (gtk_widget_get_style(summaryview->ctree));
1015
1016                 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
1017                         font_desc = pango_font_description_from_string(NORMAL_FONT);
1018                         if (font_desc) {
1019                                 pango_font_description_free(bold_style->font_desc);
1020                                 bold_style->font_desc = font_desc;
1021                         }
1022                         pango_font_description_set_weight
1023                                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
1024                 } else {
1025                         font_desc = pango_font_description_from_string(BOLD_FONT);
1026                         if (font_desc) {
1027                                 pango_font_description_free(bold_style->font_desc);
1028                                 bold_style->font_desc = font_desc;
1029                         }
1030                 }
1031                 bold_marked_style = gtk_style_copy(bold_style);
1032                 bold_marked_style->fg[GTK_STATE_NORMAL] =
1033                         summaryview->color_marked;
1034                 bold_deleted_style = gtk_style_copy(bold_style);
1035                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
1036                         summaryview->color_dim;
1037         }
1038
1039         if (prefs_common.derive_from_normal_font || !SMALL_FONT) {
1040                 font_desc = pango_font_description_new();
1041                 size = pango_font_description_get_size
1042                         (summaryview->ctree->style->font_desc);
1043                 pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
1044         } else {
1045                 font_desc = pango_font_description_from_string(SMALL_FONT);
1046         }
1047         if (font_desc) {
1048                 gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
1049                 gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
1050                 gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
1051                 pango_font_description_free(font_desc);
1052         }
1053
1054 }
1055
1056 static void summary_set_folder_pixmap(SummaryView *summaryview, StockPixmap icon)
1057 {
1058         GtkWidget *pixmap; 
1059         if (!summaryview->folder_pixmap_eventbox) {
1060                 summaryview->folder_pixmap_eventbox = gtk_event_box_new();
1061                 gtk_widget_show(summaryview->folder_pixmap_eventbox);
1062                 gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), summaryview->folder_pixmap_eventbox, FALSE, FALSE, 4);
1063                 gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), summaryview->folder_pixmap_eventbox, 0); /* search_toggle before */
1064                 g_signal_connect(G_OBJECT(summaryview->folder_pixmap_eventbox), "button_press_event",
1065                          G_CALLBACK(summary_folder_eventbox_pressed),
1066                          summaryview);
1067         }
1068         if (summaryview->folder_pixmap)
1069                 gtk_widget_destroy(summaryview->folder_pixmap);
1070
1071         pixmap = stock_pixmap_widget(summaryview->hbox_l, icon);
1072         gtk_container_add(GTK_CONTAINER(summaryview->folder_pixmap_eventbox), pixmap);
1073         gtk_widget_show(pixmap);
1074         summaryview->folder_pixmap = pixmap; 
1075 }
1076
1077 void summary_init(SummaryView *summaryview)
1078 {
1079         GtkWidget *pixmap;
1080
1081         gtk_widget_realize(summaryview->ctree);
1082         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
1083                          &markxpm, &markxpmmask);
1084         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
1085                          &deletedxpm, &deletedxpmmask);
1086         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
1087                          &newxpm, &newxpmmask);
1088         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
1089                          &unreadxpm, &unreadxpmmask);
1090         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
1091                          &repliedxpm, &repliedxpmmask);
1092         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
1093                          &forwardedxpm, &forwardedxpmmask);
1094         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
1095                          &clipxpm, &clipxpmmask);
1096         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
1097                          &lockedxpm, &lockedxpmmask);
1098         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
1099                          &ignorethreadxpm, &ignorethreadxpmmask);
1100         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_WATCHTHREAD,
1101                          &watchthreadxpm, &watchthreadxpmmask);
1102         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
1103                          &clipkeyxpm, &clipkeyxpmmask);
1104         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
1105                          &keyxpm, &keyxpmmask);
1106         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_GPG_SIGNED,
1107                          &gpgsignedxpm, &gpgsignedxpmmask);
1108         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_GPG_SIGNED,
1109                          &clipgpgsignedxpm, &clipgpgsignedxpmmask);
1110         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_SPAM,
1111                          &spamxpm, &spamxpmmask);
1112         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MOVED,
1113                          &movedxpm, &movedxpmmask);
1114         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_COPIED,
1115                          &copiedxpm, &copiedxpmmask);
1116
1117         summary_set_fonts(summaryview);
1118
1119         summary_set_folder_pixmap(summaryview, STOCK_PIXMAP_DIR_OPEN);
1120
1121         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
1122         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
1123         gtk_widget_show(pixmap);
1124         summaryview->quick_search_pixmap = pixmap;
1125         
1126 #ifdef GENERIC_UMPC
1127         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_SELECTION);
1128         gtk_container_add(GTK_CONTAINER(summaryview->multiple_sel_togbtn), pixmap);
1129         gtk_widget_show(pixmap);
1130         summaryview->multiple_sel_image = pixmap;
1131 #endif
1132
1133         /* Init summaryview prefs */
1134         summaryview->sort_key = SORT_BY_NONE;
1135         summaryview->sort_type = SORT_ASCENDING;
1136
1137         /* Init summaryview extra data */
1138         summaryview->simplify_subject_preg = NULL;
1139
1140         summary_clear_list(summaryview);
1141         summary_set_column_titles(summaryview);
1142         summary_colorlabel_menu_create(summaryview, FALSE);
1143         summary_tags_menu_create(summaryview, FALSE);
1144         main_create_mailing_list_menu (summaryview->mainwin, NULL);     
1145         summary_set_menu_sensitive(summaryview);
1146
1147 }
1148
1149 #define CURRENTLY_DISPLAYED(m) \
1150 ( (m->msgnum == displayed_msgnum) \
1151   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
1152
1153 #define FOLDER_SHOWS_TO_HDR(i) \
1154 ( i && (folder_has_parent_of_type(i, F_OUTBOX) \
1155         || folder_has_parent_of_type(i, F_DRAFT) \
1156         || folder_has_parent_of_type(i, F_QUEUE)) )
1157   
1158 static void summary_switch_from_to(SummaryView *summaryview, FolderItem *item)
1159 {
1160         gboolean show_from = FALSE, show_to = FALSE;
1161         gboolean showing_from = FALSE, showing_to = FALSE;
1162         gint from_pos = 0, to_pos = 0;
1163         SummaryColumnState *col_state = summaryview->col_state;
1164         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1165         
1166         if (!item || (prefs_common.layout_mode == VERTICAL_LAYOUT && prefs_common.two_line_vert) )
1167                 return;
1168         if (FOLDER_SHOWS_TO_HDR(item))
1169                 show_to = TRUE;
1170         else
1171                 show_from = TRUE;
1172         
1173         from_pos = summaryview->col_pos[S_COL_FROM];
1174         to_pos = summaryview->col_pos[S_COL_TO];
1175         showing_from = col_state[from_pos].visible;
1176         showing_to = col_state[to_pos].visible;
1177         
1178         if (showing_from && showing_to) {
1179                 debug_print("showing both\n");
1180                 return;
1181         }
1182
1183         if (!showing_from && !showing_to) {
1184                 debug_print("showing none\n");
1185                 return;
1186         }
1187
1188         debug_print("showing %s %s, must show %s %s\n", 
1189                 showing_from?"From":"",
1190                 showing_to?"To":"",
1191                 show_from?"From":"",
1192                 show_to?"To":"");
1193
1194         if (showing_from == show_from && showing_to == show_to)
1195                 return;
1196         /* else we'll switch both */
1197
1198         debug_print("switching columns\n");
1199         col_state[from_pos].type = S_COL_TO;
1200         col_state[from_pos].visible = show_to;
1201
1202         col_state[to_pos].type = S_COL_FROM;
1203         col_state[to_pos].visible = show_from;
1204
1205         summaryview->col_pos[S_COL_TO] = from_pos;
1206         summaryview->col_pos[S_COL_FROM] = to_pos;
1207
1208         gtk_cmclist_set_column_visibility
1209                 (GTK_CMCLIST(ctree), from_pos, col_state[from_pos].visible);
1210         gtk_cmclist_set_column_visibility
1211                 (GTK_CMCLIST(ctree), to_pos, col_state[to_pos].visible);
1212
1213         summary_set_column_titles(summaryview);
1214 }
1215
1216 static gboolean summaryview_quicksearch_recurse(gpointer data)
1217 {
1218         SummaryView *summaryview = (SummaryView *)data;
1219         main_window_cursor_wait(summaryview->mainwin);
1220         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
1221         quicksearch_search_subfolders(summaryview->quicksearch, 
1222                               summaryview->folderview,
1223                               summaryview->folder_item);
1224         main_window_cursor_normal(summaryview->mainwin);
1225         return FALSE;
1226 }
1227
1228 static gboolean summary_check_consistency(FolderItem *item, GSList *mlist)
1229 {
1230         int u = 0, n = 0, m = 0, t = 0, r = 0, f = 0, l = 0, i = 0, w = 0;
1231         GSList *cur;
1232         START_TIMING("");
1233         for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1234                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1235                 t++;
1236                 if (MSG_IS_NEW(msginfo->flags))
1237                         n++;
1238                 if (MSG_IS_UNREAD(msginfo->flags))
1239                         u++;
1240                 if (MSG_IS_MARKED(msginfo->flags))
1241                         m++;            
1242                 if (MSG_IS_REPLIED(msginfo->flags))
1243                         r++;
1244                 if (MSG_IS_FORWARDED(msginfo->flags))
1245                         f++;
1246                 if (MSG_IS_LOCKED(msginfo->flags))
1247                         l++;
1248                 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
1249                         i++;
1250                 if (MSG_IS_WATCH_THREAD(msginfo->flags))
1251                         w++;
1252         }
1253         if (t != item->total_msgs
1254         ||  n != item->new_msgs
1255         ||  u != item->unread_msgs
1256         ||  m != item->marked_msgs
1257         ||  r != item->replied_msgs
1258         ||  f != item->forwarded_msgs
1259         ||  l != item->locked_msgs
1260         ||  i != item->ignored_msgs
1261         ||  w != item->watched_msgs
1262         ||  (m == 0 && item->unreadmarked_msgs != 0)
1263         ||  item->unreadmarked_msgs < 0) {
1264                 debug_print("Inconsistency\n");
1265                 folder_item_scan_full(item, FALSE);
1266                 END_TIMING();
1267                 return FALSE;
1268         } 
1269         END_TIMING();
1270         return TRUE;
1271 }
1272
1273 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
1274 {
1275         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1276         GtkCMCTreeNode *node = NULL;
1277         GSList *mlist = NULL;
1278         gchar *buf;
1279         gboolean is_refresh;
1280         guint selected_msgnum = 0;
1281         guint displayed_msgnum = 0;
1282         GSList *cur;
1283         GSList *not_killed;
1284         gboolean hidden_removed = FALSE;
1285
1286         if (summary_is_locked(summaryview)) return FALSE;
1287
1288         if (!summaryview->mainwin)
1289                 return FALSE;
1290         START_TIMING("");
1291         summaryview->last_displayed = NULL;
1292         summary_switch_from_to(summaryview, item);
1293
1294         inc_lock();
1295         summary_lock(summaryview);
1296
1297         utils_free_regex();
1298
1299         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
1300
1301         if (item && item->folder->klass->item_opened) {
1302                 item->folder->klass->item_opened(item);
1303         }
1304
1305         if (!is_refresh) {
1306                 main_create_mailing_list_menu (summaryview->mainwin, NULL);
1307                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
1308                         if (item) {
1309                                 mainwindow_enter_folder(summaryview->mainwin);
1310                                 gtk_widget_grab_focus(summaryview->ctree);
1311                         }
1312                 }
1313         }
1314         if (!prefs_common.summary_quicksearch_sticky
1315          && (!prefs_common.summary_quicksearch_recurse
1316           || !quicksearch_is_active(summaryview->quicksearch)
1317           || (item && !quicksearch_is_in_subfolder(summaryview->quicksearch, item)))
1318          && !quicksearch_is_running(summaryview->quicksearch)
1319          && !is_refresh) {
1320                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
1321         }
1322
1323         /* STATUSBAR_POP(summaryview->mainwin); */
1324
1325         if (is_refresh) {
1326                 selected_msgnum = summary_get_msgnum(summaryview,
1327                                                      summaryview->selected);
1328                 displayed_msgnum = summary_get_msgnum(summaryview,
1329                                                       summaryview->displayed);
1330         }
1331
1332         /* process the marks if any */
1333         if (summaryview->mainwin->lock_count == 0 &&
1334             (summaryview->moved > 0 || summaryview->copied > 0)) {
1335                 AlertValue val;
1336                 gboolean changed = FALSE;
1337
1338                 val = alertpanel(_("Process mark"),
1339                                  _("Some marks are left. Process them?"),
1340                                  GTK_STOCK_NO, GTK_STOCK_YES, GTK_STOCK_CANCEL);
1341                 if (G_ALERTALTERNATE == val) {
1342                         summary_unlock(summaryview);
1343                         summary_execute(summaryview);
1344                         summary_lock(summaryview);
1345                         changed = TRUE;
1346                 } else if (G_ALERTDEFAULT == val) {
1347                         /* DO NOTHING */
1348                 } else {
1349                         summary_unlock(summaryview);
1350                         inc_unlock();
1351                         END_TIMING();
1352                         return FALSE;
1353                 }
1354                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
1355                         folder_update_op_count();
1356         }
1357         
1358         summary_freeze(summaryview);
1359
1360         summary_clear_list(summaryview);
1361
1362         buf = NULL;
1363         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
1364                 g_free(buf);
1365                 debug_print("empty folder (%p %s %p %d)\n\n",
1366                                 item, 
1367                                 (item && item->path)?item->path:"(null)",
1368                                 item?folder_item_parent(item):0x0,
1369                                 item?item->no_select:FALSE);
1370                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
1371                 summary_clear_all(summaryview);
1372                 summaryview->folder_item = item;
1373                 summary_thaw(summaryview);
1374                 summary_unlock(summaryview);
1375                 inc_unlock();
1376                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
1377                         main_window_cursor_wait(summaryview->mainwin);
1378                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
1379                         if (quicksearch_is_active(summaryview->quicksearch))
1380                                 quicksearch_search_subfolders(summaryview->quicksearch, 
1381                                               summaryview->folderview,
1382                                               summaryview->folder_item);
1383                         main_window_cursor_normal(summaryview->mainwin);
1384                 }       
1385                 END_TIMING();           
1386                 return TRUE;
1387         }
1388         g_free(buf);
1389
1390         if (!is_refresh)
1391                 messageview_clear(summaryview->messageview);
1392
1393         summaryview->folder_item = item;
1394         item->opened = TRUE;
1395
1396         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
1397         debug_print("%s\n", buf);
1398         STATUSBAR_PUSH(summaryview->mainwin, buf);
1399         g_free(buf);
1400
1401         main_window_cursor_wait(summaryview->mainwin);
1402
1403         mlist = folder_item_get_msg_list(item);
1404
1405         if (!summary_check_consistency(item, mlist)) {
1406                 debug_print("reloading due to inconsistency\n");
1407                 procmsg_msg_list_free(mlist);
1408                 mlist = folder_item_get_msg_list(item);
1409         }
1410
1411         if (summaryview->folder_item->hide_read_msgs &&
1412             quicksearch_is_active(summaryview->quicksearch) == FALSE) {
1413                 GSList *not_killed;
1414                 
1415                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
1416                 not_killed = NULL;
1417                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1418                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1419                         
1420                         if (!msginfo->hidden) {
1421                                 if (MSG_IS_UNREAD(msginfo->flags) &&
1422                                     !MSG_IS_IGNORE_THREAD(msginfo->flags))
1423                                         not_killed = g_slist_prepend(not_killed, msginfo);
1424                                 else if (MSG_IS_MARKED(msginfo->flags) ||
1425                                          MSG_IS_LOCKED(msginfo->flags))
1426                                         not_killed = g_slist_prepend(not_killed, msginfo);
1427                                 else if (is_refresh &&
1428                                         (msginfo->msgnum == selected_msgnum ||
1429                                          msginfo->msgnum == displayed_msgnum))
1430                                         not_killed = g_slist_prepend(not_killed, msginfo);
1431                                 else
1432                                         procmsg_msginfo_free(msginfo);
1433                          } else
1434                                 procmsg_msginfo_free(msginfo);
1435                 }
1436                 hidden_removed = TRUE;
1437                 g_slist_free(mlist);
1438                 mlist = not_killed;
1439         } else {
1440                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
1441         }
1442
1443         if (quicksearch_is_active(summaryview->quicksearch)) {
1444                 GSList *not_killed;
1445                 gint interval = quicksearch_is_fast(summaryview->quicksearch) ? 5000:100;
1446                 START_TIMING("quicksearch");
1447                 gint num = 0, total = summaryview->folder_item->total_msgs;
1448                 statusbar_print_all(_("Searching in %s... \n"), 
1449                         summaryview->folder_item->path ? 
1450                         summaryview->folder_item->path : "(null)");
1451                 not_killed = NULL;
1452                 folder_item_update_freeze();
1453                 for (cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1454                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1455
1456                         statusbar_progress_all(num++,total, interval);
1457
1458                         if (!msginfo->hidden && quicksearch_match(summaryview->quicksearch, msginfo))
1459                                 not_killed = g_slist_prepend(not_killed, msginfo);
1460                         else
1461                                 procmsg_msginfo_free(msginfo);
1462                         if (num % interval == 0)
1463                                 GTK_EVENTS_FLUSH();
1464                         if (!quicksearch_is_active(summaryview->quicksearch)) {
1465                                 break;
1466                         }
1467                 }
1468                 folder_item_update_thaw();
1469                 statusbar_progress_all(0,0,0);
1470                 statusbar_pop_all();
1471                 
1472                 hidden_removed = TRUE;
1473                 if (!quicksearch_is_active(summaryview->quicksearch)) {
1474                         debug_print("search cancelled!\n");
1475                         summary_thaw(summaryview);
1476                         STATUSBAR_POP(summaryview->mainwin);
1477                         main_window_cursor_normal(summaryview->mainwin);
1478                         summary_unlock(summaryview);
1479                         inc_unlock();
1480                         summary_show(summaryview, summaryview->folder_item);
1481                         END_TIMING();
1482                         return FALSE;
1483                 }
1484                 g_slist_free(mlist);
1485                 mlist = not_killed;
1486                 END_TIMING();
1487         }
1488
1489         if (!hidden_removed) {
1490                 START_TIMING("removing hidden");
1491                 not_killed = NULL;
1492                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1493                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1494
1495                         if (!msginfo->hidden)
1496                                 not_killed = g_slist_prepend(not_killed, msginfo);
1497                         else
1498                                 procmsg_msginfo_free(msginfo);
1499                 }
1500                 g_slist_free(mlist);
1501                 mlist = not_killed;
1502                 END_TIMING();
1503         }
1504
1505         STATUSBAR_POP(summaryview->mainwin);
1506
1507         /* set ctree and hash table from the msginfo list, and
1508            create the thread */
1509         summary_set_ctree_from_list(summaryview, mlist);
1510
1511         g_slist_free(mlist);
1512
1513         if (quicksearch_is_active(summaryview->quicksearch) &&
1514             quicksearch_is_running(summaryview->quicksearch)) {
1515                 /* only scan subfolders when quicksearch changed,
1516                  * not when search is the same and folder changed */
1517                 g_timeout_add(100, summaryview_quicksearch_recurse, summaryview);
1518         }
1519
1520         if (is_refresh) {
1521                 if (!quicksearch_is_in_typing(summaryview->quicksearch)) {
1522                         summaryview->last_displayed = summaryview->displayed;
1523                         summaryview->displayed =
1524                                 summary_find_msg_by_msgnum(summaryview,
1525                                                            displayed_msgnum);
1526                         if (!summaryview->displayed)
1527                                 messageview_clear(summaryview->messageview);
1528                         summary_unlock(summaryview);
1529                         summary_select_by_msgnum(summaryview, selected_msgnum);
1530                         summary_lock(summaryview);
1531                         if (!summaryview->selected) {
1532                                 /* no selected message - select first unread
1533                                    message, but do not display it */
1534                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1535                                                                      MSG_UNREAD, FALSE);
1536                                 if (node == NULL && GTK_CMCLIST(ctree)->row_list != NULL)
1537                                         node = gtk_cmctree_node_nth
1538                                                 (ctree,
1539                                                  item->sort_type == SORT_DESCENDING
1540                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1541                                 summary_unlock(summaryview);
1542                                 summary_select_node(summaryview, node, FALSE, TRUE);
1543                                 summary_lock(summaryview);
1544                         }
1545                 } else {
1546                         /* just select first/last */
1547                         if (GTK_CMCLIST(ctree)->row_list != NULL)
1548                                 node = gtk_cmctree_node_nth
1549                                         (ctree,
1550                                          item->sort_type == SORT_DESCENDING
1551                                          ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1552                         gtk_sctree_select(GTK_SCTREE(ctree), node);
1553                         summaryview->selected = node;
1554                         gtk_cmctree_node_moveto(ctree, node, 0, 0.5, 0);
1555                 }
1556         } else {
1557                 /* backward compat */
1558                 int i = 0;
1559                 gboolean set = FALSE, stop = FALSE;
1560                 for (i = 0; i < 6; i++) {
1561                         EntryAction act = prefs_common.summary_select_prio[i];
1562
1563                         if (act != ACTION_UNSET) {
1564                                 set = TRUE;
1565                                 break;
1566                         }
1567                 }
1568                 if (!set)
1569                         prefs_summary_open_set_defaults();
1570
1571                 for (i = 0; i < 6 && node == NULL; i++) {
1572                         EntryAction act = prefs_common.summary_select_prio[i];
1573                         
1574                         switch(act) {
1575                         case ACTION_MARKED:
1576                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1577                                              MSG_MARKED, FALSE);
1578                                 break;
1579                         case ACTION_NEW:
1580                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1581                                              MSG_NEW, FALSE);
1582                                 break;
1583                         case ACTION_UNREAD:
1584                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1585                                              MSG_UNREAD, FALSE);
1586                                 break;
1587                         case ACTION_LAST_OPENED:
1588                                 if (summaryview->folder_item) {
1589                                         node = summary_find_msg_by_msgnum(summaryview, 
1590                                                         summaryview->folder_item->last_seen);
1591                                 }
1592                                 break;
1593                         case ACTION_LAST_LIST:
1594                                 if (GTK_CMCLIST(ctree)->row_list != NULL) {
1595                                         node = gtk_cmctree_node_nth
1596                                                 (ctree,
1597                                                  item->sort_type == SORT_DESCENDING
1598                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1599                                 }
1600                                 break;
1601                         case ACTION_FIRST_LIST:
1602                                 if (GTK_CMCLIST(ctree)->row_list != NULL) {
1603                                         node = gtk_cmctree_node_nth
1604                                                 (ctree,
1605                                                  item->sort_type == SORT_ASCENDING
1606                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1607                                 }
1608                                 break;
1609                         case ACTION_NOTHING:
1610                         case ACTION_UNSET:
1611                                 node = NULL;
1612                                 stop = TRUE;
1613                                 break;
1614                         }
1615                         
1616                         if (stop || node)
1617                                 break;
1618                 }
1619
1620                 summary_unlock(summaryview);
1621                 if (node) {
1622                         gboolean show = (prefs_common.always_show_msg == OPENMSG_ALWAYS) ||
1623                                 (prefs_common.always_show_msg == OPENMSG_WHEN_VIEW_VISIBLE &&
1624                                                 messageview_is_visible(summaryview->messageview));
1625                         summary_select_node(summaryview, node, show, TRUE);
1626                 }
1627                 summary_lock(summaryview);
1628         }
1629
1630         summary_status_show(summaryview);
1631         summary_set_menu_sensitive(summaryview);
1632         toolbar_main_set_sensitive(summaryview->mainwin);
1633         
1634         summary_thaw(summaryview);
1635         debug_print("\n");
1636         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1637         STATUSBAR_POP(summaryview->mainwin);
1638         main_window_cursor_normal(summaryview->mainwin);
1639         summary_unlock(summaryview);
1640         inc_unlock();
1641         END_TIMING();
1642         return TRUE;
1643 }
1644
1645 #undef CURRENTLY_DISPLAYED
1646
1647
1648 void summary_clear_list(SummaryView *summaryview)
1649 {
1650         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
1651         gint optimal_width;
1652
1653         summary_freeze(summaryview);
1654
1655         gtk_cmctree_pre_recursive(GTK_CMCTREE(summaryview->ctree),
1656                                 NULL, summary_free_msginfo_func, NULL);
1657
1658         if (summaryview->folder_item) {
1659                 summaryview->folder_item->opened = FALSE;
1660                 summaryview->folder_item = NULL;
1661         }
1662
1663         summaryview->display_msg = FALSE;
1664
1665         summaryview->selected = NULL;
1666         summaryview->displayed = NULL;
1667         summaryview->total_size = 0;
1668         summaryview->deleted = summaryview->moved = 0;
1669         summaryview->copied = 0;
1670         if (summaryview->msgid_table) {
1671                 g_hash_table_destroy(summaryview->msgid_table);
1672                 summaryview->msgid_table = NULL;
1673         }
1674         if (summaryview->subject_table) {
1675                 g_hash_table_destroy(summaryview->subject_table);
1676                 summaryview->subject_table = NULL;
1677         }
1678         summaryview->mlist = NULL;
1679
1680         gtk_cmclist_clear(clist);
1681         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1682                 optimal_width = gtk_cmclist_optimal_column_width
1683                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1684                 gtk_cmclist_set_column_width
1685                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1686                          optimal_width);
1687         }
1688
1689         summary_thaw(summaryview);
1690 }
1691
1692 void summary_clear_all(SummaryView *summaryview)
1693 {
1694         messageview_clear(summaryview->messageview);
1695         summary_clear_list(summaryview);
1696         summary_set_menu_sensitive(summaryview);
1697         toolbar_main_set_sensitive(summaryview->mainwin);
1698         summary_status_show(summaryview);
1699 }
1700
1701 void summary_lock(SummaryView *summaryview)
1702 {
1703         summaryview->lock_count++;
1704 }
1705
1706 void summary_unlock(SummaryView *summaryview)
1707 {
1708         if (summaryview->lock_count)
1709                 summaryview->lock_count--;
1710 }
1711
1712 gboolean summary_is_locked(SummaryView *summaryview)
1713 {
1714         return summaryview->lock_count > 0;
1715 }
1716
1717 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1718 {
1719         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
1720         SummarySelection selection;
1721
1722         if (!clist->row_list)
1723                 selection = SUMMARY_NONE;
1724         else if (!clist->selection)
1725                 selection = SUMMARY_SELECTED_NONE;
1726         else if (!clist->selection->next)
1727                 selection = SUMMARY_SELECTED_SINGLE;
1728         else
1729                 selection = SUMMARY_SELECTED_MULTIPLE;
1730
1731         return selection;
1732 }
1733
1734 /*!
1735  *\return       MsgInfo * Selected message if there's one selected;
1736  *              if multiple selected, or none, return NULL.
1737  */ 
1738 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1739 {
1740         /* summaryview->selected may be valid when multiple 
1741          * messages were selected */
1742         GList *sellist = GTK_CMCLIST(summaryview->ctree)->selection;
1743
1744         if (sellist == NULL || sellist->next) 
1745                 return NULL;
1746         
1747         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1748 }
1749
1750 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1751 {
1752         GSList *mlist = NULL;
1753         GList *cur;
1754         MsgInfo *msginfo;
1755
1756         for (cur = GTK_CMCLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1757              cur = cur->next) {
1758                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1759                 mlist = g_slist_prepend(mlist, msginfo);
1760         }
1761
1762         mlist = g_slist_reverse(mlist);
1763
1764         return mlist;
1765 }
1766
1767 void summary_set_menu_sensitive(SummaryView *summaryview)
1768 {
1769         SensitiveCond state;
1770         gboolean sensitive;
1771         gint i;
1772
1773         static const struct {
1774                 gchar *const entry;
1775                 SensitiveCond cond;
1776         } entry[] = {
1777                 {"SummaryViewPopup/Reply"                       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1778 #ifndef GENERIC_UMPC
1779                 {"SummaryViewPopup/ReplyTo"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1780                 {"SummaryViewPopup/ReplyTo/All"         , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1781                 {"SummaryViewPopup/ReplyTo/Sender"             , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1782                 {"SummaryViewPopup/ReplyTo/MailingList"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1783 #endif
1784
1785                 {"SummaryViewPopup/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1786 #ifndef GENERIC_UMPC
1787                 {"SummaryViewPopup/ForwardAsAtt"        , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1788                 {"SummaryViewPopup/Redirect"                    , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1789 #endif
1790
1791                 {"SummaryViewPopup/Move"                        , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1792                 {"SummaryViewPopup/Copy"                        , M_TARGET_EXIST|M_EXEC},
1793                 {"SummaryViewPopup/Trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1794 #ifndef GENERIC_UMPC
1795                 {"SummaryViewPopup/Delete"                      , M_TARGET_EXIST|M_ALLOW_DELETE},
1796 #endif
1797
1798                 {"SummaryViewPopup/Mark"                        , M_TARGET_EXIST},
1799                 {"SummaryViewPopup/Mark/Mark"                   , M_TARGET_EXIST},
1800                 {"SummaryViewPopup/Mark/Unmark"                 , M_TARGET_EXIST},
1801                 {"SummaryViewPopup/Mark/MarkUnread"     , M_TARGET_EXIST},
1802                 {"SummaryViewPopup/Mark/MarkRead"       , M_TARGET_EXIST},
1803                 {"SummaryViewPopup/Mark/MarkAllRead"    , M_TARGET_EXIST},
1804                 {"SummaryViewPopup/Mark/IgnoreThread"           , M_TARGET_EXIST},
1805                 {"SummaryViewPopup/Mark/UnignoreThread"         , M_TARGET_EXIST},
1806                 {"SummaryViewPopup/Mark/WatchThread"            , M_TARGET_EXIST},
1807                 {"SummaryViewPopup/Mark/UnwatchThread"          , M_TARGET_EXIST},
1808                 {"SummaryViewPopup/Mark/Unlock"                 , M_TARGET_EXIST},
1809                 {"SummaryViewPopup/Mark/Lock"                   , M_TARGET_EXIST},
1810                 {"SummaryViewPopup/Mark/MarkSpam"               , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1811                 {"SummaryViewPopup/Mark/MarkHam"                , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1812                 {"SummaryViewPopup/ColorLabel"                  , M_TARGET_EXIST},
1813                 {"SummaryViewPopup/Tags"                        , M_TARGET_EXIST},
1814
1815 #ifndef GENERIC_UMPC
1816                 {"SummaryViewPopup/AddSenderToAB"       , M_SINGLE_TARGET_EXIST},
1817 #endif
1818                 {"SummaryViewPopup/CreateFilterRule"            , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1819 #ifndef GENERIC_UMPC
1820                 {"SummaryViewPopup/CreateProcessingRule"        , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1821 #endif
1822
1823                 {"SummaryViewPopup/View"                        , M_SINGLE_TARGET_EXIST},
1824                 {"SummaryViewPopup/View/OpenInNewWindow"     , M_SINGLE_TARGET_EXIST},
1825                 {"SummaryViewPopup/View/MessageSource"          , M_SINGLE_TARGET_EXIST},
1826 #ifndef GENERIC_UMPC
1827                 {"SummaryViewPopup/View/AllHeaders"             , M_SINGLE_TARGET_EXIST},
1828 #endif
1829                 {"SummaryViewPopup/SaveAs"                      , M_TARGET_EXIST},
1830 #ifndef GENERIC_UMPC
1831                 {"SummaryViewPopup/Print"                       , M_TARGET_EXIST},
1832 #endif
1833                 {NULL, 0}
1834         };
1835
1836         main_window_set_menu_sensitive(summaryview->mainwin);
1837
1838         state = main_window_get_current_state(summaryview->mainwin);
1839
1840         for (i = 0; entry[i].entry != NULL; i++) {
1841                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1842                 cm_menu_set_sensitive(entry[i].entry, sensitive);
1843         }
1844
1845         summary_lock(summaryview);
1846 #ifndef GENERIC_UMPC
1847         if (summaryview->messageview 
1848         &&  summaryview->messageview->mimeview
1849         &&  summaryview->messageview->mimeview->textview)
1850                 cm_toggle_menu_set_active("SummaryViewPopup/View/AllHeaders",
1851                         summaryview->messageview->mimeview->textview->show_all_headers);
1852 #endif
1853         summary_unlock(summaryview);
1854 }
1855
1856 void summary_select_prev_unread(SummaryView *summaryview)
1857 {
1858         GtkCMCTreeNode *node;
1859         gboolean skip_cur = FALSE;
1860
1861         if (summaryview->displayed 
1862         &&  summaryview->selected == summaryview->displayed) {
1863                 debug_print("skipping current\n");
1864                 skip_cur = TRUE;
1865         }
1866
1867         node = summary_find_prev_flagged_msg
1868                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1869
1870         if (!node || node == summaryview->selected) {
1871                 AlertValue val = 0;
1872
1873                 switch (prefs_common.next_unread_msg_dialog) {
1874                         case NEXTUNREADMSGDIALOG_ALWAYS:
1875                                 val = alertpanel(_("No more unread messages"),
1876                                                  _("No unread message found. "
1877                                                    "Search from the end?"),
1878                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1879                                 break;
1880                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1881                                 val = G_ALERTALTERNATE;
1882                                 break;
1883                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1884                                 val = !G_ALERTALTERNATE;
1885                                 break;
1886                         default:
1887                                 debug_print(
1888                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1889                 }
1890                 if (val != G_ALERTALTERNATE) return;
1891                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1892                                                      MSG_UNREAD, FALSE);
1893         }
1894
1895         if (!node)
1896                 alertpanel_notice(_("No unread messages."));
1897         else
1898                 summary_select_node(summaryview, node, TRUE, FALSE);
1899 }
1900
1901 void summary_select_next_unread(SummaryView *summaryview)
1902 {
1903         GtkCMCTreeNode *node = summaryview->selected;
1904         gboolean skip_cur = FALSE;
1905         
1906         if (summaryview->displayed 
1907         &&  summaryview->selected == summaryview->displayed) {
1908                 debug_print("skipping cur (%p %p)\n",
1909                         summaryview->displayed, summaryview->selected);
1910                 skip_cur = TRUE;
1911         }
1912
1913
1914         node = summary_find_next_flagged_msg
1915                 (summaryview, node, MSG_UNREAD, skip_cur);
1916         
1917         if (node)
1918                 summary_select_node(summaryview, node, TRUE, FALSE);
1919         else {
1920                 node = summary_find_next_flagged_msg
1921                         (summaryview, NULL, MSG_UNREAD, FALSE);
1922                 if (node == NULL || node == summaryview->selected) {
1923                         AlertValue val = 0;
1924
1925                         switch (prefs_common.next_unread_msg_dialog) {
1926                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1927                                         val = alertpanel(_("No more unread messages"),
1928                                                          _("No unread message found. "
1929                                                            "Go to next folder?"),
1930                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1931                                         break;
1932                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1933                                         val = G_ALERTALTERNATE;
1934                                         break;
1935                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1936                                         val = G_ALERTOTHER;
1937                                         break;
1938                                 default:
1939                                         debug_print(
1940                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1941                         }
1942
1943                         if (val == G_ALERTALTERNATE) {
1944                                 folderview_select_next_unread(summaryview->folderview, TRUE);
1945                                 return;
1946                         } 
1947                         else
1948                                 return;
1949                 } else
1950                         summary_select_node(summaryview, node, TRUE, FALSE);
1951
1952         }
1953 }
1954
1955 void summary_select_prev_new(SummaryView *summaryview)
1956 {
1957         GtkCMCTreeNode *node;
1958         gboolean skip_cur = FALSE;
1959
1960         if (summaryview->displayed 
1961         &&  summaryview->selected == summaryview->displayed) {
1962                 debug_print("skipping current\n");
1963                 skip_cur = TRUE;
1964         }
1965
1966         node = summary_find_prev_flagged_msg
1967                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1968
1969         if (!node || node == summaryview->selected) {
1970                 AlertValue val = 0;
1971
1972                 switch (prefs_common.next_unread_msg_dialog) {
1973                         case NEXTUNREADMSGDIALOG_ALWAYS:
1974                                 val = alertpanel(_("No more new messages"),
1975                                                  _("No new message found. "
1976                                                    "Search from the end?"),
1977                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1978                                 break;
1979                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1980                                 val = G_ALERTALTERNATE;
1981                                 break;
1982                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1983                                 val = !G_ALERTALTERNATE;
1984                                 break;
1985                         default:
1986                                 debug_print(
1987                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1988                 }
1989                 if (val != G_ALERTALTERNATE) return;
1990                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1991                                                      MSG_NEW, FALSE);
1992         }
1993
1994         if (!node)
1995                 alertpanel_notice(_("No new messages."));
1996         else
1997                 summary_select_node(summaryview, node, TRUE, FALSE);
1998 }
1999
2000 void summary_select_next_new(SummaryView *summaryview)
2001 {
2002         GtkCMCTreeNode *node = summaryview->selected;
2003         gboolean skip_cur = FALSE;
2004         
2005         if (summaryview->displayed 
2006         &&  summaryview->selected == summaryview->displayed) {
2007                 debug_print("skipping cur (%p %p)\n",
2008                         summaryview->displayed, summaryview->selected);
2009                 skip_cur = TRUE;
2010         }
2011
2012
2013         node = summary_find_next_flagged_msg
2014                 (summaryview, node, MSG_NEW, skip_cur);
2015         
2016         if (node)
2017                 summary_select_node(summaryview, node, TRUE, FALSE);
2018         else {
2019                 node = summary_find_next_flagged_msg
2020                         (summaryview, NULL, MSG_NEW, FALSE);
2021                 if (node == NULL || node == summaryview->selected) {
2022                         AlertValue val = 0;
2023
2024                         switch (prefs_common.next_unread_msg_dialog) {
2025                                 case NEXTUNREADMSGDIALOG_ALWAYS:
2026                                         val = alertpanel(_("No more new messages"),
2027                                                          _("No new message found. "
2028                                                            "Go to next folder?"),
2029                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2030                                         break;
2031                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
2032                                         val = G_ALERTALTERNATE;
2033                                         break;
2034                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
2035                                         val = G_ALERTOTHER;
2036                                         break;
2037                                 default:
2038                                         debug_print(
2039                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
2040                         }
2041
2042                         if (val == G_ALERTALTERNATE) {
2043                                 folderview_select_next_new(summaryview->folderview);
2044                                 return;
2045                         } 
2046                         else
2047                                 return;
2048                 } else
2049                         summary_select_node(summaryview, node, TRUE, FALSE);
2050
2051         }
2052 }
2053
2054 void summary_select_prev_marked(SummaryView *summaryview)
2055 {
2056         GtkCMCTreeNode *node;
2057
2058         node = summary_find_prev_flagged_msg
2059                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
2060
2061         if (!node) {
2062                 AlertValue val;
2063
2064                 val = alertpanel(_("No more marked messages"),
2065                                  _("No marked message found. "
2066                                    "Search from the end?"),
2067                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2068                 if (val != G_ALERTALTERNATE) return;
2069                 node = summary_find_prev_flagged_msg(summaryview, NULL,
2070                                                      MSG_MARKED, TRUE);
2071         }
2072
2073         if (!node)
2074                 alertpanel_notice(_("No marked messages."));
2075         else
2076                 summary_select_node(summaryview, node, TRUE, FALSE);
2077 }
2078
2079 void summary_select_next_marked(SummaryView *summaryview)
2080 {
2081         GtkCMCTreeNode *node = summaryview->selected;
2082         gboolean skip_cur = FALSE;
2083         
2084         if (summaryview->displayed 
2085         &&  summaryview->selected == summaryview->displayed) {
2086                 debug_print("skipping cur (%p %p)\n",
2087                         summaryview->displayed, summaryview->selected);
2088                 skip_cur = TRUE;
2089         }
2090
2091
2092         node = summary_find_next_flagged_msg
2093                 (summaryview, node, MSG_MARKED, skip_cur);
2094         
2095         if (node)
2096                 summary_select_node(summaryview, node, TRUE, FALSE);
2097         else {
2098                 node = summary_find_next_flagged_msg
2099                         (summaryview, NULL, MSG_MARKED, FALSE);
2100                 if (node == NULL || node == summaryview->selected) {
2101                         AlertValue val = 0;
2102
2103                         switch (prefs_common.next_unread_msg_dialog) {
2104                                 case NEXTUNREADMSGDIALOG_ALWAYS:
2105                                         val = alertpanel(_("No more marked messages"),
2106                                                          _("No marked message found. "
2107                                                            "Go to next folder?"),
2108                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2109                                         break;
2110                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
2111                                         val = G_ALERTALTERNATE;
2112                                         break;
2113                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
2114                                         val = G_ALERTOTHER;
2115                                         break;
2116                                 default:
2117                                         debug_print(
2118                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
2119                         }
2120
2121                         if (val == G_ALERTALTERNATE) {
2122                                 folderview_select_next_marked(summaryview->folderview);
2123                                 return;
2124                         } 
2125                         else
2126                                 return;
2127                 } else
2128                         summary_select_node(summaryview, node, TRUE, FALSE);
2129
2130         }
2131 }
2132
2133 void summary_select_prev_labeled(SummaryView *summaryview)
2134 {
2135         GtkCMCTreeNode *node;
2136
2137         node = summary_find_prev_flagged_msg
2138                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2139
2140         if (!node) {
2141                 AlertValue val;
2142
2143                 val = alertpanel(_("No more labeled messages"),
2144                                  _("No labeled message found. "
2145                                    "Search from the end?"),
2146                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2147                 if (val != G_ALERTALTERNATE) return;
2148                 node = summary_find_prev_flagged_msg(summaryview, NULL,
2149                                                      MSG_CLABEL_FLAG_MASK, TRUE);
2150         }
2151
2152         if (!node)
2153                 alertpanel_notice(_("No labeled messages."));
2154         else
2155                 summary_select_node(summaryview, node, TRUE, FALSE);
2156 }
2157
2158 void summary_select_next_labeled(SummaryView *summaryview)
2159 {
2160         GtkCMCTreeNode *node;
2161
2162         node = summary_find_next_flagged_msg
2163                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2164
2165         if (!node) {
2166                 AlertValue val;
2167
2168                 val = alertpanel(_("No more labeled messages"),
2169                                  _("No labeled message found. "
2170                                    "Search from the beginning?"),
2171                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2172                 if (val != G_ALERTALTERNATE) return;
2173                 node = summary_find_next_flagged_msg(summaryview, NULL,
2174                                                      MSG_CLABEL_FLAG_MASK, TRUE);
2175         }
2176
2177         if (!node)
2178                 alertpanel_notice(_("No labeled messages."));
2179         else
2180                 summary_select_node(summaryview, node, TRUE, FALSE);
2181 }
2182
2183 void summary_select_last_read(SummaryView *summaryview)
2184 {
2185         if (summaryview->last_displayed)
2186                 summary_select_node(summaryview, summaryview->last_displayed, TRUE, FALSE);
2187 }
2188
2189 void summary_select_parent(SummaryView *summaryview)
2190 {
2191         GtkCMCTreeNode *node = NULL;
2192
2193         if (summaryview->selected)
2194                 node = GTK_CMCTREE_ROW(summaryview->selected)->parent;
2195         if (node)
2196                 summary_select_node(summaryview, node, TRUE, FALSE);
2197 }
2198
2199 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
2200 {
2201         GtkCMCTreeNode *node;
2202
2203         node = summary_find_msg_by_msgnum(summaryview, msgnum);
2204         summary_select_node(summaryview, node, FALSE, TRUE);
2205 }
2206
2207 typedef struct _PostponedSelectData
2208 {
2209         GtkCMCTree *ctree;
2210         GtkCMCTreeNode *row;
2211         GtkCMCTreeNode *node;
2212         GtkScrollType type;
2213         gint column;
2214         SummaryView *summaryview;
2215         gboolean display_msg;
2216         gboolean do_refresh;
2217 } PostponedSelectData;
2218
2219 static gboolean summary_select_retry(void *data)
2220 {
2221         PostponedSelectData *psdata = (PostponedSelectData *)data;
2222         debug_print("trying again\n");
2223         if (psdata->row)
2224                 summary_selected(psdata->ctree, psdata->row,
2225                             psdata->column, psdata->summaryview);
2226         else if (psdata->node)
2227                 summary_select_node(psdata->summaryview, psdata->node,
2228                             psdata->display_msg, psdata->do_refresh);
2229         else
2230                 summary_step(psdata->summaryview, psdata->type);
2231         g_free(psdata);
2232         return FALSE;
2233 }
2234
2235 /**
2236  * summary_select_node:
2237  * @summaryview: Summary view.
2238  * @node: Summary tree node.
2239  * @display_msg: TRUE to display the selected message.
2240  * @do_refresh: TRUE to refresh the widget.
2241  *
2242  * Select @node (bringing it into view by scrolling and expanding its
2243  * thread, if necessary) and unselect all others.  If @display_msg is
2244  * TRUE, display the corresponding message in the message view.
2245  * If @do_refresh is TRUE, the widget is refreshed.
2246  **/
2247 void summary_select_node(SummaryView *summaryview, GtkCMCTreeNode *node,
2248                          gboolean display_msg, gboolean do_refresh)
2249 {
2250         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2251         
2252         if (summary_is_locked(summaryview)
2253         && !GTK_SCTREE(ctree)->selecting_range
2254         && summaryview->messageview->mimeview
2255         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
2256         && summaryview->messageview->mimeview->textview->loading) {
2257                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2258                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
2259                 
2260                 data->ctree = ctree;
2261                 data->row = NULL;
2262                 data->node = node;
2263                 data->summaryview = summaryview;
2264                 data->display_msg = display_msg;
2265                 data->do_refresh = do_refresh;
2266                 debug_print("postponing open of message till end of load\n");
2267                 g_timeout_add(100, summary_select_retry, data);
2268                 return;
2269         }
2270         if (summary_is_locked(summaryview)) {
2271                 return;
2272         }
2273         if (!summaryview->folder_item)
2274                 return;
2275         if (node) {
2276                 gtkut_ctree_expand_parent_all(ctree, node);
2277                 if (do_refresh) {
2278                         summary_lock(summaryview);
2279                         GTK_EVENTS_FLUSH();
2280                         summary_unlock(summaryview);
2281                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
2282                         gtk_cmctree_node_moveto(ctree, node, 0, 0.5, 0);
2283                 }
2284                 if (display_msg && summaryview->displayed == node)
2285                         summaryview->displayed = NULL;
2286                 summaryview->display_msg = display_msg;
2287                 gtk_sctree_select(GTK_SCTREE(ctree), node);
2288                 if (summaryview->selected == NULL)
2289                         summaryview->selected = node;
2290         }
2291 }
2292
2293 guint summary_get_msgnum(SummaryView *summaryview, GtkCMCTreeNode *node)
2294 {
2295         GtkCMCTree *ctree =NULL;
2296         MsgInfo *msginfo;
2297
2298         if (!summaryview)
2299                 return 0;
2300         ctree = GTK_CMCTREE(summaryview->ctree);
2301         if (!node)
2302                 return 0;
2303         msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2304         if (msginfo)
2305                 return msginfo->msgnum;
2306         else 
2307                 return -1;
2308 }
2309
2310 static GtkCMCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
2311                                            GtkCMCTreeNode *current_node)
2312 {
2313         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2314         GtkCMCTreeNode *node;
2315         MsgInfo *msginfo;
2316
2317         if (current_node)
2318                 node = current_node;
2319         else
2320                 node = gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->rows - 1);
2321
2322         for (; node != NULL; node = GTK_CMCTREE_NODE_PREV(node)) {
2323                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2324                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
2325         }
2326
2327         return node;
2328 }
2329
2330 static GtkCMCTreeNode *summary_find_next_msg(SummaryView *summaryview,
2331                                            GtkCMCTreeNode *current_node)
2332 {
2333         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2334         GtkCMCTreeNode *node;
2335         MsgInfo *msginfo;
2336
2337         if (current_node)
2338                 node = current_node;
2339         else
2340                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2341
2342         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2343                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2344                 if (msginfo && !MSG_IS_DELETED(msginfo->flags) 
2345                 && !MSG_IS_MOVE(msginfo->flags)) break;
2346         }
2347
2348         return node;
2349 }
2350
2351 static GtkCMCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
2352                                                    GtkCMCTreeNode *current_node,
2353                                                    MsgPermFlags flags,
2354                                                    gboolean start_from_prev)
2355 {
2356         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2357         GtkCMCTreeNode *node;
2358         MsgInfo *msginfo;
2359
2360         if (current_node) {
2361                 if (start_from_prev)
2362                         node = GTK_CMCTREE_NODE_PREV(current_node);
2363                 else
2364                         node = current_node;
2365         } else
2366                 node = gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->rows - 1);
2367
2368         for (; node != NULL; node = GTK_CMCTREE_NODE_PREV(node)) {
2369                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2370                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
2371         }
2372
2373         return node;
2374 }
2375
2376 static GtkCMCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
2377                                                    GtkCMCTreeNode *current_node,
2378                                                    MsgPermFlags flags,
2379                                                    gboolean start_from_next)
2380 {
2381         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2382         GtkCMCTreeNode *node;
2383         MsgInfo *msginfo;
2384
2385         if (current_node) {
2386                 if (start_from_next)
2387                         node = gtkut_ctree_node_next(ctree, current_node);
2388                 else
2389                         node = current_node;
2390         } else
2391                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2392
2393         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2394                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2395                 /* Find msg with matching flags but ignore messages with
2396                    ignore flags, if searching for new or unread messages */
2397                 if ((msginfo && (msginfo->flags.perm_flags & flags) != 0) &&
2398                     !(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) 
2399                         )
2400                         break;
2401         }
2402
2403         return node;
2404 }
2405
2406 static GtkCMCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
2407                                                 guint msgnum)
2408 {
2409         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2410         GtkCMCTreeNode *node;
2411         MsgInfo *msginfo;
2412
2413         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2414
2415         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2416                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2417                 if (msginfo && msginfo->msgnum == msgnum) break;
2418         }
2419
2420         return node;
2421 }
2422
2423 static guint attract_hash_func(gconstpointer key)
2424 {
2425         gchar *str;
2426         gchar *p;
2427         guint h;
2428
2429         Xstrdup_a(str, (const gchar *)key, return 0);
2430         trim_subject(str);
2431
2432         p = str;
2433         h = *p;
2434
2435         if (h) {
2436                 for (p += 1; *p != '\0'; p++)
2437                         h = (h << 5) - h + *p;
2438         }
2439
2440         return h;
2441 }
2442
2443 static gint attract_compare_func(gconstpointer a, gconstpointer b)
2444 {
2445         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
2446 }
2447
2448 void summary_attract_by_subject(SummaryView *summaryview)
2449 {
2450         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2451         GtkCMCList *clist = GTK_CMCLIST(ctree);
2452         GtkCMCTreeNode *src_node;
2453         GtkCMCTreeNode *dst_node, *sibling;
2454         GtkCMCTreeNode *tmp;
2455         MsgInfo *src_msginfo, *dst_msginfo;
2456         GHashTable *subject_table;
2457
2458         debug_print("Attracting messages by subject...");
2459         STATUSBAR_PUSH(summaryview->mainwin,
2460                        _("Attracting messages by subject..."));
2461
2462         main_window_cursor_wait(summaryview->mainwin);
2463         summary_freeze(summaryview);
2464
2465         subject_table = g_hash_table_new(attract_hash_func,
2466                                          attract_compare_func);
2467
2468         for (src_node = GTK_CMCTREE_NODE(clist->row_list);
2469              src_node != NULL;
2470              src_node = tmp) {
2471                 tmp = GTK_CMCTREE_ROW(src_node)->sibling;
2472                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
2473                 if (!src_msginfo) continue;
2474                 if (!src_msginfo->subject) continue;
2475
2476                 /* find attracting node */
2477                 dst_node = g_hash_table_lookup(subject_table,
2478                                                src_msginfo->subject);
2479
2480                 if (dst_node) {
2481                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
2482
2483                         /* if the time difference is more than 20 days,
2484                            don't attract */
2485                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
2486                             > 60 * 60 * 24 * 20)
2487                                 continue;
2488
2489                         sibling = GTK_CMCTREE_ROW(dst_node)->sibling;
2490                         if (src_node != sibling)
2491                                 gtk_cmctree_move(ctree, src_node, NULL, sibling);
2492                 }
2493
2494                 g_hash_table_insert(subject_table,
2495                                     src_msginfo->subject, src_node);
2496         }
2497
2498         g_hash_table_destroy(subject_table);
2499
2500         gtk_cmctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2501
2502         summary_thaw(summaryview);
2503
2504         debug_print("done.\n");
2505         STATUSBAR_POP(summaryview->mainwin);
2506
2507         main_window_cursor_normal(summaryview->mainwin);
2508 }
2509
2510 static void summary_free_msginfo_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2511                                       gpointer data)
2512 {
2513         MsgInfo *msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2514
2515         if (msginfo)
2516                 procmsg_msginfo_free(msginfo);
2517 }
2518
2519 static void summary_set_marks_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2520                                    gpointer data)
2521 {
2522         SummaryView *summaryview = data;
2523         MsgInfo *msginfo;
2524
2525         msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2526
2527         if (MSG_IS_DELETED(msginfo->flags))
2528                 summaryview->deleted++;
2529
2530         summaryview->total_size += msginfo->size;
2531
2532         summary_set_row_marks(summaryview, node);
2533 }
2534
2535 static void summary_update_status(SummaryView *summaryview)
2536 {
2537         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2538         GtkCMCTreeNode *node;
2539         MsgInfo *msginfo;
2540
2541         summaryview->total_size =
2542         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
2543
2544         for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2545              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2546                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2547                 
2548                 if (!msginfo)
2549                         continue;
2550
2551                 if (MSG_IS_DELETED(msginfo->flags))
2552                         summaryview->deleted++;
2553                 if (MSG_IS_MOVE(msginfo->flags))
2554                         summaryview->moved++;
2555                 if (MSG_IS_COPY(msginfo->flags))
2556                         summaryview->copied++;
2557                 summaryview->total_size += msginfo->size;
2558         }
2559 }
2560
2561 static void summary_status_show(SummaryView *summaryview)
2562 {
2563         gchar *str;
2564         gchar *del, *mv, *cp;
2565         gchar *sel;
2566         gchar *spc;
2567         gchar *itstr;
2568         GList *rowlist, *cur;
2569         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
2570         guint n_marked = 0, n_replied = 0, n_forwarded = 0, n_locked = 0, n_ignored = 0, n_watched = 0;
2571         goffset sel_size = 0, n_size = 0;
2572         MsgInfo *msginfo;
2573         gchar *name;
2574 #if GTK_CHECK_VERSION(2, 12, 0)
2575         gchar *tooltip;
2576 #endif
2577         
2578         if (!summaryview->folder_item) {
2579                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
2580                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
2581                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
2582                 toolbar_main_set_sensitive(summaryview->mainwin);
2583                 return;
2584         }
2585
2586         rowlist = GTK_CMCLIST(summaryview->ctree)->selection;
2587         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2588                 msginfo = gtk_cmctree_node_get_row_data
2589                         (GTK_CMCTREE(summaryview->ctree),
2590                          GTK_CMCTREE_NODE(cur->data));
2591                 if (msginfo) {
2592                         sel_size += msginfo->size;
2593                         n_selected++;
2594                 }
2595         }
2596         
2597         if (summaryview->folder_item->hide_read_msgs 
2598         || quicksearch_is_active(summaryview->quicksearch)) {
2599                 rowlist = GTK_CMCLIST(summaryview->ctree)->row_list;
2600                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2601                         msginfo = gtk_cmctree_node_get_row_data
2602                                 (GTK_CMCTREE(summaryview->ctree),
2603                                  GTK_CMCTREE_NODE(cur));
2604                         if (msginfo) {
2605                                 n_size += msginfo->size;
2606                                 n_total++;
2607                                 if (MSG_IS_NEW(msginfo->flags))
2608                                         n_new++;
2609                                 if (MSG_IS_UNREAD(msginfo->flags))
2610                                         n_unread++;
2611                                 if (MSG_IS_MARKED(msginfo->flags))
2612                                         n_marked++;
2613                                 if (MSG_IS_REPLIED(msginfo->flags))
2614                                         n_replied++;
2615                                 if (MSG_IS_FORWARDED(msginfo->flags))
2616                                         n_forwarded++;
2617                                 if (MSG_IS_LOCKED(msginfo->flags))
2618                                         n_locked++;
2619                                 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2620                                         n_ignored++;
2621                                 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2622                                         n_watched++;
2623                         }
2624                 }
2625         } else {
2626                 n_new = summaryview->folder_item->new_msgs;
2627                 n_unread = summaryview->folder_item->unread_msgs;
2628                 n_marked = summaryview->folder_item->marked_msgs;
2629                 n_replied = summaryview->folder_item->replied_msgs;
2630                 n_forwarded = summaryview->folder_item->forwarded_msgs;
2631                 n_locked = summaryview->folder_item->locked_msgs;
2632                 n_ignored = summaryview->folder_item->ignored_msgs;
2633                 n_watched = summaryview->folder_item->watched_msgs;
2634                 n_total = summaryview->folder_item->total_msgs;
2635                 n_size = summaryview->total_size;
2636         }
2637
2638         name = folder_item_get_name(summaryview->folder_item);
2639         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
2640         g_free(name);
2641
2642         if (summaryview->deleted)
2643                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
2644         else
2645                 del = g_strdup("");
2646         if (summaryview->moved)
2647                 mv = g_strdup_printf(_("%s%d moved"),
2648                                      summaryview->deleted ? _(", ") : "",
2649                                      summaryview->moved);
2650         else
2651                 mv = g_strdup("");
2652         if (summaryview->copied)
2653                 cp = g_strdup_printf(_("%s%d copied"),
2654                                      summaryview->deleted ||
2655                                      summaryview->moved ? _(", ") : "",
2656                                      summaryview->copied);
2657         else
2658                 cp = g_strdup("");
2659
2660         if (summaryview->deleted || summaryview->moved || summaryview->copied)
2661                 spc = "    ";
2662         else
2663                 spc = "";
2664
2665         if (n_selected) {
2666                 sel = g_strdup_printf(" (%s)", to_human_readable((goffset)sel_size));
2667                 if (n_selected == 1)
2668                         itstr = g_strdup(_(" item selected"));
2669                 else
2670                         itstr = g_strdup(_(" items selected"));
2671         } else {
2672                 sel = g_strdup("");
2673                 itstr = g_strdup("");
2674         }
2675                 
2676         if (prefs_common.layout_mode != SMALL_LAYOUT) {
2677                 str = g_strconcat(n_selected ? itos(n_selected) : "",
2678                                                 itstr, sel, spc, del, mv, cp, NULL);
2679                 g_free(sel);
2680                 g_free(del);
2681                 g_free(mv);
2682                 g_free(cp);
2683                 g_free(itstr);
2684                 
2685                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2686                 g_free(str);
2687
2688                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2689                                               n_new, n_unread, n_total,
2690                                               to_human_readable((goffset)n_size));
2691
2692
2693                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2694                 g_free(str);
2695 #if GTK_CHECK_VERSION(2, 12, 0)
2696                 tooltip = g_strdup_printf(_("<b>Message summary</b>\n"
2697                                             "<b>New:</b> %d\n"
2698                                             "<b>Unread:</b> %d\n"
2699                                             "<b>Total:</b> %d\n"
2700                                             "<b>Size:</b> %s\n\n"
2701                                             "<b>Marked:</b> %d\n"
2702                                             "<b>Replied:</b> %d\n"
2703                                             "<b>Forwarded:</b> %d\n"
2704                                             "<b>Locked:</b> %d\n"
2705                                             "<b>Ignored:</b> %d\n"
2706                                             "<b>Watched:</b> %d"),
2707                                               n_new, n_unread, n_total,
2708                                               to_human_readable((goffset)n_size),
2709                                               n_marked,n_replied,n_forwarded,
2710                                               n_locked,n_ignored,n_watched);
2711
2712                 gtk_widget_set_tooltip_markup(GTK_WIDGET(summaryview->statlabel_msgs),
2713                                             tooltip); 
2714                 g_free(tooltip);
2715 #endif
2716         } else {
2717                 gchar *ssize, *tsize;
2718                 if (n_selected) {
2719                         ssize = g_strdup(to_human_readable((goffset)sel_size));
2720                         tsize = g_strdup(to_human_readable((goffset)n_size));
2721                         str = g_strdup_printf(_("%d/%d selected (%s/%s), %d unread"),
2722                                 n_selected, n_total, ssize, tsize, n_unread);
2723                         g_free(ssize);
2724                         g_free(tsize);
2725                 } else
2726                         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2727                                 n_new, n_unread, n_total, to_human_readable((goffset)n_size));
2728                 g_free(sel);
2729                 g_free(del);
2730                 g_free(mv);
2731                 g_free(cp);
2732                 g_free(itstr);
2733                 
2734                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2735                 g_free(str);
2736         }
2737
2738         summary_set_menu_sensitive(summaryview);
2739         toolbar_main_set_sensitive(summaryview->mainwin);
2740 }
2741
2742 static void summary_set_column_titles(SummaryView *summaryview)
2743 {
2744         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
2745         FolderItem *item = summaryview->folder_item;
2746         GtkWidget *hbox;
2747         GtkWidget *label;
2748         GtkWidget *arrow;
2749         gint pos;
2750         const gchar *title;
2751         SummaryColumnType type;
2752         GtkJustification justify;
2753
2754         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2755                 SORT_BY_MARK,
2756                 SORT_BY_STATUS,
2757                 SORT_BY_MIME,
2758                 SORT_BY_SUBJECT,
2759                 SORT_BY_FROM,
2760                 SORT_BY_TO,
2761                 SORT_BY_DATE,
2762                 SORT_BY_SIZE,
2763                 SORT_BY_NUMBER,
2764                 SORT_BY_SCORE,
2765                 SORT_BY_LOCKED,
2766                 SORT_BY_TAGS
2767         };
2768
2769         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2770                 type = summaryview->col_state[pos].type;
2771
2772                 /* CLAWS: mime and unread are single char headers */
2773                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2774                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2775
2776                 switch (type) {
2777                 case S_COL_SUBJECT:
2778                 case S_COL_FROM:
2779                 case S_COL_TO:
2780                 case S_COL_DATE:
2781                 case S_COL_NUMBER:
2782                         if(type == S_COL_FROM && item != NULL &&
2783                                         FOLDER_SHOWS_TO_HDR(item) &&
2784                                         !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2785                                 type = S_COL_TO;
2786                         if(type == S_COL_NUMBER)
2787                                 title = gettext(col_label[type]);
2788                         else
2789                                 title = prefs_common_translated_header_name(col_label[type]);
2790                         break;
2791                 default:
2792                         title = gettext(col_label[type]);
2793                 }
2794
2795                 if (type == S_COL_MIME) {
2796                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2797                         gtk_widget_show(label);
2798                         gtk_cmclist_set_column_widget(clist, pos, label);
2799                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Attachment"));
2800                         continue;
2801                 } else if (type == S_COL_MARK) {
2802                         label = gtk_image_new_from_pixmap(markxpm, markxpmmask);
2803                         gtk_widget_show(label);
2804                         gtk_cmclist_set_column_widget(clist, pos, label);
2805                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Mark"));
2806                         continue;
2807                 } else if (type == S_COL_LOCKED) {
2808                         label = gtk_image_new_from_pixmap(lockedxpm, lockedxpmmask);
2809                         gtk_widget_show(label);
2810                         gtk_cmclist_set_column_widget(clist, pos, label);
2811                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Locked"));
2812                         continue;
2813                 } else if (type == S_COL_STATUS) {
2814                         gtk_cmclist_set_column_title(clist, pos, title);
2815                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Status"));
2816                         continue;
2817                 }
2818
2819                 hbox  = gtk_hbox_new(FALSE, 4);
2820                 label = gtk_label_new(title);
2821 #ifdef GENERIC_UMPC
2822         gtk_widget_set_size_request(hbox, -1, 20);
2823 #endif
2824
2825                 if (justify == GTK_JUSTIFY_RIGHT)
2826                         gtk_box_pack_end(GTK_BOX(hbox), label,
2827                                          FALSE, FALSE, 0);
2828                 else
2829                         gtk_box_pack_start(GTK_BOX(hbox), label,
2830                                            FALSE, FALSE, 0);
2831
2832                 if (summaryview->sort_key == sort_by[type]) {
2833                         arrow = gtk_arrow_new
2834                                 (summaryview->sort_type == SORT_ASCENDING
2835                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2836                                  GTK_SHADOW_IN);
2837                         gtk_widget_set_size_request(GTK_WIDGET(arrow), 10, 10);
2838                         if (justify == GTK_JUSTIFY_RIGHT)
2839                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2840                                                    FALSE, FALSE, 0);
2841                         else
2842                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2843                                                  FALSE, FALSE, 0);
2844                 }
2845
2846                 gtk_widget_show_all(hbox);
2847                 gtk_cmclist_set_column_widget(clist, pos, hbox);
2848         }
2849 }
2850
2851 void summary_reflect_tags_changes(SummaryView *summaryview)
2852 {
2853         GtkMenuShell *menu;
2854         GList *cur;
2855         GtkCMCTreeNode *node;
2856         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2857         gboolean froze = FALSE;
2858         gboolean redisplay = FALSE;
2859
2860         /* re-create colorlabel submenu */
2861         menu = GTK_MENU_SHELL(summaryview->tags_menu);
2862         g_return_if_fail(menu != NULL);
2863
2864         /* clear items. get item pointers. */
2865         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
2866                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(cur->data), NULL);
2867         }
2868         summary_tags_menu_create(summaryview, TRUE);
2869
2870         START_LONG_OPERATION(summaryview, TRUE);
2871         for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
2872              node = gtkut_ctree_node_next(ctree, node)) {
2873                 redisplay |= summary_set_row_tag(summaryview,
2874                                            node, TRUE, FALSE, 0);
2875         }
2876         END_LONG_OPERATION(summaryview);
2877         if (redisplay)
2878                 summary_redisplay_msg(summaryview);
2879 }
2880
2881
2882 void summary_reflect_prefs(void)
2883 {
2884         static gchar *last_smallfont = NULL;
2885         static gchar *last_normalfont = NULL;
2886         static gchar *last_boldfont = NULL;
2887         gboolean update_font = FALSE;
2888         SummaryView *summaryview = NULL;
2889
2890         if (!mainwindow_get_mainwindow())
2891                 return;
2892         summaryview = mainwindow_get_mainwindow()->summaryview;
2893
2894         if (!last_smallfont || strcmp(last_smallfont, SMALL_FONT) ||
2895                         !last_normalfont || strcmp(last_normalfont, NORMAL_FONT) ||
2896                         !last_boldfont || strcmp(last_boldfont, BOLD_FONT))
2897                 update_font = TRUE;
2898
2899         g_free(last_smallfont);
2900         last_smallfont = g_strdup(SMALL_FONT);
2901         g_free(last_normalfont);
2902         last_normalfont = g_strdup(NORMAL_FONT);
2903         g_free(last_boldfont);
2904         last_boldfont = g_strdup(BOLD_FONT);
2905
2906         if (update_font) {      
2907                 bold_style = bold_marked_style = bold_deleted_style = 
2908                         small_style = small_marked_style = small_deleted_style = NULL;
2909                 summary_set_fonts(summaryview);
2910         }
2911
2912         summary_set_column_titles(summaryview);
2913         summary_relayout(summaryview);
2914         
2915         if (summaryview->folder_item)
2916                 summary_show(summaryview, summaryview->folder_item);
2917 }
2918
2919 void summary_sort(SummaryView *summaryview,
2920                   FolderSortKey sort_key, FolderSortType sort_type)
2921 {
2922         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2923         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
2924         GtkCMCListCompareFunc cmp_func = NULL;
2925         START_TIMING("");
2926         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2927                                        G_CALLBACK(summary_tree_expanded), summaryview);
2928         summary_freeze(summaryview);
2929
2930         switch (sort_key) {
2931         case SORT_BY_MARK:
2932                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_mark;
2933                 break;
2934         case SORT_BY_STATUS:
2935                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_status;
2936                 break;
2937         case SORT_BY_MIME:
2938                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_mime;
2939                 break;
2940         case SORT_BY_NUMBER:
2941                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_num;
2942                 break;
2943         case SORT_BY_SIZE:
2944                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_size;
2945                 break;
2946         case SORT_BY_DATE:
2947                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_date;
2948                 break;
2949         case SORT_BY_THREAD_DATE:
2950                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_thread_date;
2951                 break;
2952         case SORT_BY_FROM:
2953                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_from;
2954                 break;
2955         case SORT_BY_SUBJECT:
2956                 if (summaryview->simplify_subject_preg)
2957                         cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_simplified_subject;
2958                 else
2959                         cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_subject;
2960                 break;
2961         case SORT_BY_SCORE:
2962                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_score;
2963                 break;
2964         case SORT_BY_LABEL:
2965                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_label;
2966                 break;
2967         case SORT_BY_TO:
2968                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_to;
2969                 break;
2970         case SORT_BY_LOCKED:
2971                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_locked;
2972                 break;
2973         case SORT_BY_TAGS:
2974                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_tags;
2975                 break;
2976         case SORT_BY_NONE:
2977                 break;
2978         default:
2979                 goto unlock;
2980         }
2981
2982         summaryview->sort_key = sort_key;
2983         summaryview->sort_type = sort_type;
2984
2985         summary_set_column_titles(summaryview);
2986         summary_set_menu_sensitive(summaryview);
2987
2988         /* allow fallback to don't sort */
2989         if (summaryview->sort_key == SORT_BY_NONE)
2990                 goto unlock;
2991
2992         if (cmp_func != NULL) {
2993                 debug_print("Sorting summary...");
2994                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2995
2996                 main_window_cursor_wait(summaryview->mainwin);
2997
2998                 gtk_cmclist_set_compare_func(clist, cmp_func);
2999
3000                 gtk_cmclist_set_sort_type(clist, (GtkSortType)sort_type);
3001                 gtk_sctree_sort_recursive(ctree, NULL);
3002
3003                 gtk_cmctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
3004
3005                 main_window_cursor_normal(summaryview->mainwin);
3006
3007                 debug_print("done.\n");
3008                 STATUSBAR_POP(summaryview->mainwin);
3009         }
3010 unlock:
3011         summary_thaw(summaryview);
3012         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
3013                                        G_CALLBACK(summary_tree_expanded), summaryview);
3014         END_TIMING();
3015 }
3016
3017 static gboolean summary_update_thread_age(GNode *node, gpointer data)
3018 {
3019         MsgInfo *msginfo = node->data;
3020         time_t *most_recent = (time_t *)data;
3021
3022         if (msginfo->date_t > *most_recent) {
3023                 *most_recent = msginfo->date_t;
3024         }
3025         return FALSE;
3026 }       
3027
3028 static void summary_find_thread_age(GNode *gnode)
3029 {
3030         MsgInfo *msginfo = (MsgInfo *)gnode->data;
3031         time_t most_recent;
3032
3033         if (!msginfo)
3034                 return;
3035         most_recent = msginfo->thread_date = msginfo->date_t;
3036
3037         g_node_traverse(gnode, G_IN_ORDER, G_TRAVERSE_ALL, -1, summary_update_thread_age, &most_recent);
3038
3039         msginfo->thread_date = most_recent;
3040 }
3041
3042 static gboolean summary_insert_gnode_func(GtkCMCTree *ctree, guint depth, GNode *gnode,
3043                                    GtkCMCTreeNode *cnode, gpointer data)
3044 {
3045         SummaryView *summaryview = (SummaryView *)data;
3046         MsgInfo *msginfo = (MsgInfo *)gnode->data;
3047         gchar *text[N_SUMMARY_COLS];
3048         gint *col_pos = summaryview->col_pos;
3049         const gchar *msgid = msginfo->msgid;
3050         GHashTable *msgid_table = summaryview->msgid_table;
3051         gboolean vert = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3052
3053         summary_set_header(summaryview, text, msginfo);
3054
3055         gtk_sctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
3056                                 NULL, NULL, NULL, NULL, FALSE, summaryview->threaded && !summaryview->thread_collapsed);
3057 #define SET_TEXT(col) {                                         \
3058         gtk_cmctree_node_set_text(ctree, cnode, col_pos[col],   \
3059                                 text[col_pos[col]]);            \
3060 }
3061
3062         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
3063                 SET_TEXT(S_COL_NUMBER);
3064         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
3065                 SET_TEXT(S_COL_SCORE);
3066         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
3067                 SET_TEXT(S_COL_SIZE);
3068         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible)
3069                 SET_TEXT(S_COL_DATE);
3070         if (summaryview->col_state[summaryview->col_pos[S_COL_FROM]].visible)
3071                 SET_TEXT(S_COL_FROM);
3072         if (summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
3073                 SET_TEXT(S_COL_TO);
3074         if (summaryview->col_state[summaryview->col_pos[S_COL_TAGS]].visible)
3075                 SET_TEXT(S_COL_TAGS);
3076
3077         if (vert && prefs_common.two_line_vert)
3078                 g_free(text[summaryview->col_pos[S_COL_SUBJECT]]);
3079
3080 #undef SET_TEXT
3081
3082         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
3083         summary_set_marks_func(ctree, cnode, summaryview);
3084
3085         if (msgid && msgid[0] != '\0')
3086                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
3087
3088         return TRUE;
3089 }
3090
3091 static void summary_set_ctree_from_list(SummaryView *summaryview,
3092                                         GSList *mlist)
3093 {
3094         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3095         MsgInfo *msginfo;
3096         GtkCMCTreeNode *node = NULL;
3097         GHashTable *msgid_table;
3098         GHashTable *subject_table = NULL;
3099         GSList * cur;
3100         gboolean vert = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3101
3102         START_TIMING("");
3103         
3104         if (!mlist) return;
3105
3106         debug_print("\tSetting summary from message data...\n");
3107         STATUSBAR_PUSH(summaryview->mainwin,
3108                        _("Setting summary from message data..."));
3109         gdk_flush();
3110
3111         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3112                                        G_CALLBACK(summary_tree_expanded), summaryview);
3113
3114         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
3115         summaryview->msgid_table = msgid_table;
3116
3117         if (prefs_common.thread_by_subject) {
3118                 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
3119                 summaryview->subject_table = subject_table;
3120         } else {
3121                 summaryview->subject_table = NULL;
3122         }
3123
3124         if (prefs_common.use_addr_book)
3125                 start_address_completion(NULL);
3126         
3127         if (summaryview->threaded) {
3128                 GNode *root, *gnode;
3129                 START_TIMING("threaded");
3130                 root = procmsg_get_thread_tree(mlist);
3131
3132                 
3133                 for (gnode = root->children; gnode != NULL;
3134                      gnode = gnode->next) {
3135                         summary_find_thread_age(gnode);
3136                         node = gtk_sctree_insert_gnode
3137                                 (ctree, NULL, node, gnode,
3138                                  summary_insert_gnode_func, summaryview);
3139                 }
3140
3141                 g_node_destroy(root);
3142                 
3143                 END_TIMING();
3144         } else {
3145                 gchar *text[N_SUMMARY_COLS];
3146                 START_TIMING("unthreaded");
3147                 cur = mlist;
3148                 for (; mlist != NULL; mlist = mlist->next) {
3149                         msginfo = (MsgInfo *)mlist->data;
3150
3151                         summary_set_header(summaryview, text, msginfo);
3152
3153                         node = gtk_sctree_insert_node
3154                                 (ctree, NULL, node, text, 2,
3155                                  NULL, NULL, NULL, NULL,
3156                                  FALSE, FALSE);
3157                         if (vert && prefs_common.two_line_vert)
3158                                 g_free(text[summaryview->col_pos[S_COL_SUBJECT]]);
3159
3160                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
3161                         summary_set_marks_func(ctree, node, summaryview);
3162
3163                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
3164                                 g_hash_table_insert(msgid_table,
3165                                                     msginfo->msgid, node);
3166
3167                         if (prefs_common.thread_by_subject)
3168                                 subject_table_insert(subject_table,
3169                                              msginfo->subject,
3170                                              node);
3171                 }
3172                 mlist = cur;
3173                 END_TIMING();
3174         }
3175
3176         if (prefs_common.enable_hscrollbar &&
3177             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
3178                 gint optimal_width;
3179
3180                 optimal_width = gtk_cmclist_optimal_column_width
3181                         (GTK_CMCLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
3182                 gtk_cmclist_set_column_width(GTK_CMCLIST(ctree),
3183                                            summaryview->col_pos[S_COL_SUBJECT],
3184                                            optimal_width);
3185         }
3186
3187         if (prefs_common.use_addr_book)
3188                 end_address_completion();
3189
3190         debug_print("done.\n");
3191         STATUSBAR_POP(summaryview->mainwin);
3192         if (debug_get_mode()) {
3193                 debug_print("\tmsgid hash table size = %d\n",
3194                             g_hash_table_size(msgid_table));
3195                 if (prefs_common.thread_by_subject)
3196                         debug_print("\tsubject hash table size = %d\n",
3197                             g_hash_table_size(subject_table));
3198         }
3199
3200         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
3201
3202         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
3203
3204         if (prefs_common.bold_unread) {
3205                 START_TIMING("bold_unread");
3206                 while (node) {
3207                         GtkCMCTreeNode *next = GTK_CMCTREE_NODE_NEXT(node);
3208                         if (GTK_CMCTREE_ROW(node)->children)
3209                                 summary_set_row_marks(summaryview, node);
3210                         node = next;
3211                 }
3212                 END_TIMING();
3213         }
3214
3215         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3216                                        G_CALLBACK(summary_tree_expanded), summaryview);
3217         END_TIMING();
3218 }
3219
3220 static gchar *summary_complete_address(const gchar *addr)
3221 {
3222         gint count;
3223         gchar *res, *tmp, *email_addr;
3224         
3225         if (addr == NULL || !strchr(addr, '@'))
3226                 return NULL;
3227
3228         Xstrdup_a(email_addr, addr, return NULL);
3229         extract_address(email_addr);
3230         if (!*email_addr)
3231                 return NULL;
3232
3233         /*
3234          * completion stuff must be already initialized
3235          */
3236         res = NULL;
3237         if (1 < (count = complete_address(email_addr))) {
3238                 tmp = get_complete_address(1);
3239                 res = procheader_get_fromname(tmp);
3240                 g_free(tmp);
3241         }
3242
3243         return res;
3244 }
3245
3246 static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
3247                                MsgInfo *msginfo)
3248 {
3249         static gchar date_modified[80];
3250         static gchar col_score[11];
3251         static gchar buf[BUFFSIZE], tmp1[BUFFSIZE], tmp2[BUFFSIZE], tmp3[BUFFSIZE];
3252         gint *col_pos = summaryview->col_pos;
3253         gchar *from_text = NULL, *to_text = NULL, *tags_text = NULL;
3254         gboolean should_swap = FALSE;
3255         gboolean vert = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3256 #if GTK_CHECK_VERSION(2,12,0)
3257         static const gchar *color_dim_rgb = NULL;
3258         if (!color_dim_rgb)
3259                 color_dim_rgb = gdk_color_to_string(&summaryview->color_dim);
3260 #else
3261         static const gchar *color_dim_rgb = "#888888";
3262 #endif
3263         text[col_pos[S_COL_FROM]]   = "";
3264         text[col_pos[S_COL_TO]]     = "";
3265         text[col_pos[S_COL_SUBJECT]]= "";
3266         text[col_pos[S_COL_MARK]]   = "";
3267         text[col_pos[S_COL_STATUS]] = "";
3268         text[col_pos[S_COL_MIME]]   = "";
3269         text[col_pos[S_COL_LOCKED]] = "";
3270         text[col_pos[S_COL_DATE]]   = "";
3271         text[col_pos[S_COL_TAGS]]   = "";
3272         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
3273                 text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
3274         else
3275                 text[col_pos[S_COL_NUMBER]] = "";
3276
3277         /* slow! */
3278         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
3279                 text[col_pos[S_COL_SIZE]] = to_human_readable(msginfo->size);
3280         else
3281                 text[col_pos[S_COL_SIZE]] = "";
3282
3283         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
3284                 text[col_pos[S_COL_SCORE]] = itos_buf(col_score, msginfo->score);
3285         else
3286                 text[col_pos[S_COL_SCORE]] = "";
3287
3288         if (summaryview->col_state[summaryview->col_pos[S_COL_TAGS]].visible) {
3289                 tags_text = procmsg_msginfo_get_tags_str(msginfo);
3290                 if (!tags_text) {
3291                         text[col_pos[S_COL_TAGS]] = "-";
3292                 } else {
3293                         strncpy2(tmp1, tags_text, sizeof(tmp1));
3294                         tmp1[sizeof(tmp1)-1]='\0';
3295                         g_free(tags_text);
3296                         text[col_pos[S_COL_TAGS]] = tmp1;
3297                 }
3298         } else
3299                 text[col_pos[S_COL_TAGS]] = "";
3300
3301         /* slow! */
3302         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible || 
3303             (vert && prefs_common.two_line_vert)) {
3304                 if (msginfo->date_t) {
3305                         procheader_date_get_localtime(date_modified,
3306                                                       sizeof(date_modified),
3307                                                       msginfo->date_t);
3308                         text[col_pos[S_COL_DATE]] = date_modified;
3309                 } else if (msginfo->date)
3310                         text[col_pos[S_COL_DATE]] = msginfo->date;
3311                 else
3312                         text[col_pos[S_COL_DATE]] = _("(No Date)");
3313         }
3314         
3315         if (prefs_common.swap_from && msginfo->from && msginfo->to
3316         &&  !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible) {
3317                 gchar *addr = NULL;
3318                 
3319                 addr = g_strdup(msginfo->from);
3320
3321                 if (addr) {
3322                         extract_address(addr);
3323                         if (account_find_from_address(addr, FALSE)) {
3324                                 should_swap = TRUE;
3325                         }
3326                         g_free(addr);
3327                 }
3328         }
3329
3330         if (!prefs_common.use_addr_book) {
3331                 from_text = msginfo->fromname ? 
3332                                 msginfo->fromname :
3333                                 _("(No From)");
3334         } else {
3335                 gchar *tmp = summary_complete_address(msginfo->from);
3336                 if (tmp) {
3337                         strncpy2(buf, tmp, sizeof(buf));
3338                         g_free(tmp);
3339                         from_text = buf;
3340                 } else {
3341                         from_text = (msginfo->fromname) ?
3342                                         msginfo->fromname: 
3343                                         _("(No From)");
3344                 }
3345         }
3346         
3347         to_text = msginfo->to ? msginfo->to : 
3348                    (msginfo->cc ? msginfo->cc :
3349                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
3350                      )
3351                    );
3352
3353         text[col_pos[S_COL_TO]] = to_text;
3354         if (!should_swap) {
3355                 text[col_pos[S_COL_FROM]] = from_text;
3356         } else {
3357                 snprintf(tmp2, BUFFSIZE-1, "--> %s", to_text);
3358                 tmp2[BUFFSIZE-1]='\0';
3359                 text[col_pos[S_COL_FROM]] = tmp2;
3360         }
3361         
3362         if (summaryview->simplify_subject_preg != NULL)
3363                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
3364                         string_remove_match(tmp3, BUFFSIZE, msginfo->subject, 
3365                                         summaryview->simplify_subject_preg) : 
3366                         _("(No Subject)");
3367         else 
3368                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
3369                         _("(No Subject)");
3370         if (vert && prefs_common.two_line_vert) {
3371                 if (!FOLDER_SHOWS_TO_HDR(summaryview->folder_item)) {
3372                         gchar *tmp = g_markup_printf_escaped(_("%s\n<span color='%s' style='italic'>From: %s, on %s</span>"),
3373                                         text[col_pos[S_COL_SUBJECT]],
3374                                         color_dim_rgb,
3375                                         text[col_pos[S_COL_FROM]],
3376                                         text[col_pos[S_COL_DATE]]);
3377                         text[col_pos[S_COL_SUBJECT]] = tmp;
3378                 } else {
3379                         gchar *tmp = g_markup_printf_escaped(_("%s\n<span color='%s' style='italic'>To: %s, on %s</span>"),
3380                                         text[col_pos[S_COL_SUBJECT]],
3381                                         color_dim_rgb,
3382                                         text[col_pos[S_COL_TO]],
3383                                         text[col_pos[S_COL_DATE]]);
3384                         text[col_pos[S_COL_SUBJECT]] = tmp;
3385                 }
3386         }
3387 }
3388
3389 static void summary_display_msg(SummaryView *summaryview, GtkCMCTreeNode *row)
3390 {
3391         summary_display_msg_full(summaryview, row, FALSE, FALSE);
3392 }
3393
3394 static gboolean defer_change(gpointer data);
3395 typedef struct _ChangeData {
3396         MsgInfo *info;
3397         gint op; /* 0, 1, 2 for unset, set, change */
3398         MsgPermFlags set_flags;
3399         MsgTmpFlags  set_tmp_flags;
3400         MsgPermFlags unset_flags;
3401         MsgTmpFlags  unset_tmp_flags;
3402 } ChangeData;
3403
3404 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
3405 {
3406         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3407                 debug_print("flags: doing unset now\n");
3408                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
3409         } else {
3410                 ChangeData *unset_data = g_new0(ChangeData, 1);
3411                 unset_data->info = msginfo;
3412                 unset_data->op = 0;
3413                 unset_data->unset_flags = flags;
3414                 unset_data->unset_tmp_flags = tmp_flags;
3415                 debug_print("flags: deferring unset\n");
3416                 g_timeout_add(100, defer_change, unset_data);
3417         }
3418 }
3419
3420 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
3421 {
3422         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3423                 debug_print("flags: doing set now\n");
3424                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
3425         } else {
3426                 ChangeData *set_data = g_new0(ChangeData, 1);
3427                 set_data->info = msginfo;
3428                 set_data->op = 1;
3429                 set_data->set_flags = flags;
3430                 set_data->set_tmp_flags = tmp_flags;
3431                 debug_print("flags: deferring set\n");
3432                 g_timeout_add(100, defer_change, set_data);
3433         }
3434 }
3435
3436 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
3437                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
3438                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
3439 {
3440         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3441                 debug_print("flags: doing change now\n");
3442                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
3443                         rem_flags, rem_tmp_flags);
3444         } else {
3445                 ChangeData *change_data = g_new0(ChangeData, 1);
3446                 change_data->info = msginfo;
3447                 change_data->op = 2;
3448                 change_data->set_flags = add_flags;
3449                 change_data->set_tmp_flags = add_tmp_flags;
3450                 change_data->unset_flags = rem_flags;
3451                 change_data->unset_tmp_flags = rem_tmp_flags;
3452                 debug_print("flags: deferring change\n");
3453                 g_timeout_add(100, defer_change, change_data);
3454         }
3455 }
3456
3457 gboolean defer_change(gpointer data)
3458 {
3459         ChangeData *chg = (ChangeData *)data;
3460         if (chg->info->folder && chg->info->folder->processing_pending) {
3461                 debug_print("flags: trying later\n");
3462                 return TRUE; /* try again */
3463         } else {
3464                 debug_print("flags: finally doing it\n");
3465                 switch(chg->op) {
3466                 case 0:
3467                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
3468                         break;
3469                 case 1:
3470                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
3471                         break;
3472                 case 2:
3473                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
3474                                 chg->unset_flags, chg->unset_tmp_flags);
3475                         break;
3476                 default:
3477                         g_warning("shouldn't happen\n");
3478                 }
3479                 g_free(chg);
3480         }
3481         return FALSE;
3482 }
3483
3484 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
3485                                       GtkCMCTreeNode *row)
3486 {
3487         g_return_if_fail(summaryview != NULL);
3488         g_return_if_fail(msginfo != NULL);
3489         g_return_if_fail(row != NULL);
3490
3491         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
3492                 summary_msginfo_unset_flags
3493                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
3494                 summary_set_row_marks(summaryview, row);
3495                 summary_status_show(summaryview);
3496         }
3497 }
3498
3499 typedef struct  {
3500         MsgInfo *msginfo;
3501         SummaryView *summaryview;
3502 } MarkAsReadData;
3503
3504 static int msginfo_mark_as_read_timeout(void *data)
3505 {
3506         MarkAsReadData *mdata = (MarkAsReadData *)data;
3507         if (!mdata)
3508                 return FALSE;
3509         
3510         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
3511                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
3512                                      mdata->summaryview->selected); 
3513
3514         g_free(mdata);
3515
3516         return FALSE;   
3517 }
3518
3519 static void summary_display_msg_full(SummaryView *summaryview,
3520                                      GtkCMCTreeNode *row,
3521                                      gboolean new_window, gboolean all_headers)
3522 {
3523         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3524         MsgInfo *msginfo;
3525         gint val;
3526         START_TIMING("");
3527         if (!new_window) {
3528                 if (summaryview->displayed == row)
3529                         return;
3530                 else if (summaryview->messageview)
3531                         summaryview->messageview->filtered = FALSE;
3532         }                       
3533         g_return_if_fail(row != NULL);
3534
3535         if (summary_is_locked(summaryview)) return;
3536         summary_lock(summaryview);
3537
3538         STATUSBAR_POP(summaryview->mainwin);
3539         GTK_EVENTS_FLUSH();
3540
3541         msginfo = gtk_cmctree_node_get_row_data(ctree, row);
3542         
3543         g_return_if_fail(msginfo);
3544
3545         if (new_window && prefs_common.layout_mode != SMALL_LAYOUT) {
3546                 MessageView *msgview;
3547
3548                 msgview = messageview_create_with_new_window(summaryview->mainwin);
3549                 val = messageview_show(msgview, msginfo, all_headers);
3550         } else {
3551                 MessageView *msgview;
3552
3553                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
3554                         if (summaryview->ext_messageview == NULL)
3555                                 summaryview->ext_messageview = messageview_create_with_new_window(summaryview->mainwin);
3556                         else
3557                                 gtkut_window_popup(summaryview->ext_messageview->window);
3558                         msgview = summaryview->ext_messageview;
3559                         summaryview->last_displayed = summaryview->displayed;
3560                         summaryview->displayed = row;
3561                         val = messageview_show(msgview, msginfo, all_headers);
3562                         if (GTK_CMCLIST(msgview->mimeview->ctree)->row_list == NULL)
3563                                 gtk_widget_grab_focus(summaryview->ctree);
3564                         gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3565                                 GTK_CMCLIST(summaryview->ctree)->focus_row);
3566                 } else {
3567                         msgview = summaryview->messageview;
3568                         summaryview->last_displayed = summaryview->displayed;
3569                         summaryview->displayed = row;
3570                         if (!messageview_is_visible(msgview)) {
3571                                 main_window_toggle_message_view(summaryview->mainwin);
3572                                 GTK_EVENTS_FLUSH();
3573                         }
3574                         val = messageview_show(msgview, msginfo, all_headers);
3575                         if (GTK_CMCLIST(msgview->mimeview->ctree)->row_list == NULL)
3576                                 gtk_widget_grab_focus(summaryview->ctree);
3577                         gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3578                                 GTK_CMCLIST(summaryview->ctree)->focus_row);
3579                 }
3580         }
3581
3582         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
3583                 if (!prefs_common.mark_as_read_on_new_window &&
3584                     prefs_common.mark_as_read_delay) {
3585                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
3586                         data->summaryview = summaryview;
3587                         data->msginfo = msginfo;
3588                         g_timeout_add(prefs_common.mark_as_read_delay * 1000,
3589                                 msginfo_mark_as_read_timeout, data);
3590                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
3591                         msginfo_mark_as_read(summaryview, msginfo, row);
3592                 }
3593         }
3594
3595         summary_set_menu_sensitive(summaryview);
3596         toolbar_main_set_sensitive(summaryview->mainwin);
3597         messageview_set_menu_sensitive(summaryview->messageview);
3598
3599         summary_unlock(summaryview);
3600         END_TIMING();
3601 }
3602
3603 void summary_display_msg_selected(SummaryView *summaryview,
3604                                   gboolean all_headers)
3605 {
3606         if (summary_is_locked(summaryview)) return;
3607         summaryview->displayed = NULL;
3608         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
3609                                  all_headers);
3610 }
3611
3612 void summary_redisplay_msg(SummaryView *summaryview)
3613 {
3614         GtkCMCTreeNode *node;
3615
3616         if (summaryview->displayed) {
3617                 node = summaryview->displayed;
3618                 summaryview->displayed = NULL;
3619                 summary_display_msg(summaryview, node);
3620         }
3621 }
3622
3623 void summary_open_msg(SummaryView *summaryview)
3624 {
3625         if (!summaryview->selected) return;
3626         
3627         /* CLAWS: if separate message view, don't open a new window
3628          * but rather use the current separated message view */
3629         summary_display_msg_full(summaryview, summaryview->selected, 
3630                                  TRUE, FALSE);
3631 }
3632
3633 void summary_view_source(SummaryView * summaryview)
3634 {
3635         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3636         MsgInfo *msginfo;
3637         SourceWindow *srcwin;
3638
3639         if (!summaryview->selected) return;
3640
3641         srcwin = source_window_create();
3642         msginfo = gtk_cmctree_node_get_row_data(ctree, summaryview->selected);
3643         source_window_show_msg(srcwin, msginfo);
3644         source_window_show(srcwin);
3645 #ifdef MAEMO
3646         maemo_window_full_screen_if_needed(GTK_WINDOW(srcwin->window));
3647         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(srcwin->window));
3648 #endif
3649 }
3650
3651 void summary_reedit(SummaryView *summaryview)
3652 {
3653         MsgInfo *msginfo;
3654
3655         if (!summaryview->selected) return;
3656         if (!summaryview->folder_item) return;
3657         if (!FOLDER_SHOWS_TO_HDR(summaryview->folder_item))
3658                 return;
3659
3660         msginfo = gtk_cmctree_node_get_row_data(GTK_CMCTREE(summaryview->ctree),
3661                                               summaryview->selected);
3662         if (!msginfo) return;
3663
3664         compose_reedit(msginfo, FALSE);
3665 }
3666
3667 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
3668 {
3669         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3670         GtkCMCTreeNode *node;
3671
3672         if (summary_is_locked(summaryview)
3673         && !GTK_SCTREE(ctree)->selecting_range
3674         && summaryview->messag