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