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