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