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