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