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