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