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