291466ded83e26a415a329831cffafb96b231934
[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 *stat_box;
555         GtkWidget *stat_box2;
556         GtkWidget *stat_vbox;
557         GtkWidget *statlabel_folder;
558         GtkWidget *statlabel_select;
559         GtkWidget *statlabel_msgs;
560         GtkWidget *hbox_spc;
561         GtkWidget *toggle_eventbox;
562         GtkWidget *toggle_arrow;
563         GtkWidget *popupmenu;
564         GtkWidget *toggle_search;
565         GtkTooltips *search_tip;
566         GtkItemFactory *popupfactory;
567         gint n_entries;
568         QuickSearch *quicksearch;
569
570         debug_print("Creating summary view...\n");
571         summaryview = g_new0(SummaryView, 1);
572
573 #define SUMMARY_VBOX_SPACING 3
574         vbox = gtk_vbox_new(FALSE, SUMMARY_VBOX_SPACING);
575         
576         /* create status label */
577         hbox = gtk_hbox_new(FALSE, 0);
578         gtk_widget_show(hbox);
579
580         stat_vbox = gtk_vbox_new(FALSE, 0);
581         gtk_widget_show(stat_vbox);
582
583         stat_box = gtk_hbox_new(FALSE, 0);
584         gtk_widget_show(stat_box);
585         
586         stat_box2 = gtk_hbox_new(FALSE, 0);
587         gtk_widget_show(stat_box2);
588         
589         search_tip = gtk_tooltips_new();
590         toggle_search = gtk_toggle_button_new();
591         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_search),
592                                      prefs_common.show_searchbar);
593         gtk_widget_show(toggle_search);
594
595         gtk_tooltips_set_tip(GTK_TOOLTIPS(search_tip),
596                              toggle_search,
597                              _("Toggle quick search bar"), NULL);
598         
599         gtk_box_pack_start(GTK_BOX(hbox), toggle_search, FALSE, FALSE, 2);      
600
601         gtk_box_pack_start(GTK_BOX(hbox), stat_vbox, TRUE, TRUE, 0);    
602         gtk_box_pack_start(GTK_BOX(stat_vbox), stat_box, TRUE, TRUE, 0);        
603         gtk_box_pack_start(GTK_BOX(stat_vbox), stat_box2, TRUE, TRUE, 0);       
604
605         hbox_l = gtk_hbox_new(FALSE, 0);
606         gtk_widget_show(hbox_l);
607         gtk_box_pack_start(GTK_BOX(stat_box), hbox_l, TRUE, TRUE, 0);
608  
609         statlabel_folder = gtk_label_new("");
610         gtk_widget_show(statlabel_folder);
611         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
612         statlabel_select = gtk_label_new("");
613         gtk_widget_show(statlabel_select);
614         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
615  
616         /* toggle view button */
617         toggle_eventbox = gtk_event_box_new();
618         gtk_widget_show(toggle_eventbox);
619         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
620         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
621         gtk_widget_show(toggle_arrow);
622         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
623         g_signal_connect(G_OBJECT(toggle_eventbox), "button_press_event",
624                          G_CALLBACK(summary_toggle_pressed),
625                          summaryview);
626         
627         
628         statlabel_msgs = gtk_label_new("");
629         gtk_widget_show(statlabel_msgs);
630         gtk_box_pack_end(GTK_BOX(stat_box), statlabel_msgs, FALSE, FALSE, 4);
631
632         hbox_spc = gtk_hbox_new(FALSE, 0);
633         gtk_widget_show(hbox_spc);
634         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
635
636         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
637         gtk_widget_show(scrolledwin);
638         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
639                                        GTK_POLICY_AUTOMATIC,
640                                        GTK_POLICY_AUTOMATIC);
641         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
642         gtk_widget_set_size_request(vbox,
643                              prefs_common.summaryview_width,
644                              prefs_common.summaryview_height);
645
646         ctree = summary_ctree_create(summaryview);
647         gtk_widget_show(ctree);
648
649         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
650                                             GTK_CLIST(ctree)->hadjustment);
651         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
652                                             GTK_CLIST(ctree)->vadjustment);
653         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
654
655         /* status label */
656         gtk_widget_show_all(stat_vbox);
657         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
658
659         /* quick search */
660         quicksearch = quicksearch_new();
661         gtk_box_pack_start(GTK_BOX(vbox), quicksearch_get_widget(quicksearch), FALSE, FALSE, 0);
662
663         quicksearch_set_execute_callback(quicksearch, quicksearch_execute_cb, summaryview);
664
665         g_signal_connect (G_OBJECT(toggle_search), "toggled",
666                           G_CALLBACK(tog_searchbar_cb), summaryview);
667
668         /* create popup menu */
669         n_entries = sizeof(summary_popup_entries) /
670                 sizeof(summary_popup_entries[0]);
671         popupmenu = menu_create_items(summary_popup_entries, n_entries,
672                                       "<SummaryView>", &popupfactory,
673                                       summaryview);
674
675         summaryview->vbox = vbox;
676         summaryview->scrolledwin = scrolledwin;
677         summaryview->ctree = ctree;
678         summaryview->hbox = hbox;
679         summaryview->hbox_l = hbox_l;
680         summaryview->hbox_spc = hbox_spc;
681         summaryview->stat_box = stat_box;
682         summaryview->stat_box2 = stat_box2;
683         summaryview->statlabel_folder = statlabel_folder;
684         summaryview->statlabel_select = statlabel_select;
685         summaryview->statlabel_msgs = statlabel_msgs;
686         summaryview->toggle_eventbox = toggle_eventbox;
687         summaryview->toggle_arrow = toggle_arrow;
688         summaryview->toggle_search = toggle_search;
689         summaryview->popupmenu = popupmenu;
690         summaryview->popupfactory = popupfactory;
691         summaryview->lock_count = 0;
692         summaryview->msginfo_update_callback_id =
693                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
694         summaryview->folder_item_update_callback_id =
695                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST,
696                                 summary_update_folder_item_name,
697                                 (gpointer) summaryview);
698
699         summaryview->target_list = gtk_target_list_new(summary_drag_types, 2);
700
701         summaryview->quicksearch = quicksearch;
702
703         /* CLAWS: need this to get the SummaryView * from
704          * the CList */
705         g_object_set_data(G_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
706
707         gtk_widget_show_all(vbox);
708
709         gtk_widget_show(vbox);
710
711         if (prefs_common.show_searchbar)
712                 quicksearch_show(quicksearch);
713         else
714                 quicksearch_hide(quicksearch);
715         
716         if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT)
717                 gtk_widget_hide(summaryview->toggle_eventbox);
718
719         return summaryview;
720 }
721
722 void summary_relayout(SummaryView *summaryview)
723 {
724         gtk_widget_realize(summaryview->stat_box);
725
726         gtk_widget_ref(summaryview->hbox_l);
727         gtk_widget_ref(summaryview->statlabel_msgs);
728         
729         gtkut_container_remove(GTK_CONTAINER(summaryview->hbox_l->parent), summaryview->hbox_l);
730         gtkut_container_remove(GTK_CONTAINER(summaryview->statlabel_msgs->parent), summaryview->statlabel_msgs);
731
732         switch (prefs_common.layout_mode) {
733         case NORMAL_LAYOUT:
734         case WIDE_LAYOUT:
735         case WIDE_MSGLIST_LAYOUT:
736                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
737                 gtk_box_pack_end(GTK_BOX(summaryview->stat_box), summaryview->statlabel_msgs, FALSE, FALSE, 4);
738                 gtk_widget_show_all(summaryview->stat_box);
739                 gtk_widget_show_all(summaryview->stat_box2);
740                 if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT)
741                         gtk_widget_hide(summaryview->toggle_eventbox);
742                 else
743                         gtk_widget_show(summaryview->toggle_eventbox);
744                 break;
745         case VERTICAL_LAYOUT:
746                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
747                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box2), summaryview->statlabel_msgs, FALSE, FALSE, 4);
748                 gtk_widget_show_all(summaryview->stat_box);
749                 gtk_widget_show_all(summaryview->stat_box2);
750                 gtk_widget_show(summaryview->toggle_eventbox);
751                 break;
752         }
753         gtk_widget_unref(summaryview->hbox_l);
754         gtk_widget_unref(summaryview->statlabel_msgs);
755         quicksearch_relayout(summaryview->quicksearch);
756 }
757
758 static void summary_set_fonts(SummaryView *summaryview)
759 {
760         PangoFontDescription *font_desc;
761         gint size;
762
763         font_desc = pango_font_description_from_string(NORMAL_FONT);
764         gtk_widget_modify_font(summaryview->ctree, font_desc);
765         pango_font_description_free(font_desc);
766
767         if (!bold_style) {
768                 bold_style = gtk_style_copy
769                         (gtk_widget_get_style(summaryview->ctree));
770                 font_desc = pango_font_description_from_string(NORMAL_FONT);
771                 if (font_desc) {
772                         pango_font_description_free(bold_style->font_desc);
773                         bold_style->font_desc = font_desc;
774                 }
775                 
776                 pango_font_description_set_weight
777                                 (bold_style->font_desc, PANGO_WEIGHT_BOLD);
778                 bold_marked_style = gtk_style_copy(bold_style);
779                 bold_marked_style->fg[GTK_STATE_NORMAL] =
780                         summaryview->color_marked;
781                 bold_deleted_style = gtk_style_copy(bold_style);
782                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
783                         summaryview->color_dim;
784         }
785
786         font_desc = pango_font_description_new();
787         size = pango_font_description_get_size
788                 (summaryview->ctree->style->font_desc);
789         pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
790         gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
791         gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
792         gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
793         pango_font_description_free(font_desc);
794 }
795
796 void summary_init(SummaryView *summaryview)
797 {
798         GtkWidget *pixmap;
799
800         gtk_widget_realize(summaryview->ctree);
801         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
802                          &markxpm, &markxpmmask);
803         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
804                          &deletedxpm, &deletedxpmmask);
805         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
806                          &newxpm, &newxpmmask);
807         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
808                          &unreadxpm, &unreadxpmmask);
809         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
810                          &repliedxpm, &repliedxpmmask);
811         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
812                          &forwardedxpm, &forwardedxpmmask);
813         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
814                          &clipxpm, &clipxpmmask);
815         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
816                          &lockedxpm, &lockedxpmmask);
817         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
818                          &ignorethreadxpm, &ignorethreadxpmmask);
819         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
820                          &clipkeyxpm, &clipkeyxpmmask);
821         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
822                          &keyxpm, &keyxpmmask);
823         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_GPG_SIGNED,
824                          &gpgsignedxpm, &gpgsignedxpmmask);
825         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_GPG_SIGNED,
826                          &clipgpgsignedxpm, &clipgpgsignedxpmmask);
827         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_SPAM,
828                          &spamxpm, &spamxpmmask);
829         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MOVED,
830                          &movedxpm, &movedxpmmask);
831         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_COPIED,
832                          &copiedxpm, &copiedxpmmask);
833
834         summary_set_fonts(summaryview);
835
836         pixmap = stock_pixmap_widget(summaryview->hbox_l, STOCK_PIXMAP_DIR_OPEN);
837         gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), pixmap, FALSE, FALSE, 4);
838         gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), pixmap, 0);
839         gtk_widget_show(pixmap);
840         summaryview->folder_pixmap = pixmap;
841
842         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
843         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
844         gtk_widget_show(pixmap);
845         summaryview->quick_search_pixmap = pixmap;
846
847         /* Init summaryview prefs */
848         summaryview->sort_key = SORT_BY_NONE;
849         summaryview->sort_type = SORT_ASCENDING;
850
851         /* Init summaryview extra data */
852         summaryview->simplify_subject_preg = NULL;
853
854         summary_clear_list(summaryview);
855         summary_set_column_titles(summaryview);
856         summary_colorlabel_menu_create(summaryview, FALSE);
857         main_create_mailing_list_menu (summaryview->mainwin, NULL);     
858         summary_set_menu_sensitive(summaryview);
859
860 }
861
862 #define CURRENTLY_DISPLAYED(m) \
863 ( (m->msgnum == displayed_msgnum) \
864   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
865
866 #define FOLDER_SHOWS_TO_HDR(i) \
867 ( folder_has_parent_of_type(i, F_OUTBOX) \
868   ||  folder_has_parent_of_type(i, F_DRAFT) \
869   ||  folder_has_parent_of_type(i, F_QUEUE) )
870   
871 static void summary_switch_from_to(SummaryView *summaryview, FolderItem *item)
872 {
873         gboolean show_from = FALSE, show_to = FALSE;
874         gboolean showing_from = FALSE, showing_to = FALSE;
875         gint from_pos = 0, to_pos = 0;
876         SummaryColumnState *col_state = summaryview->col_state;
877         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
878         
879         if (!item)
880                 return;
881         if (FOLDER_SHOWS_TO_HDR(item))
882                 show_to = TRUE;
883         else
884                 show_from = TRUE;
885         
886         from_pos = summaryview->col_pos[S_COL_FROM];
887         to_pos = summaryview->col_pos[S_COL_TO];
888         showing_from = col_state[from_pos].visible;
889         showing_to = col_state[to_pos].visible;
890         
891         if (showing_from && showing_to) {
892                 debug_print("showing both\n");
893                 return;
894         }
895
896         if (!showing_from && !showing_to) {
897                 debug_print("showing none\n");
898                 return;
899         }
900
901         debug_print("showing %s %s, must show %s %s\n", 
902                 showing_from?"From":"",
903                 showing_to?"To":"",
904                 show_from?"From":"",
905                 show_to?"To":"");
906
907         if (showing_from == show_from && showing_to == show_to)
908                 return;
909         /* else we'll switch both */
910
911         debug_print("switching columns\n");
912         col_state[from_pos].type = S_COL_TO;
913         col_state[from_pos].visible = show_to;
914
915         col_state[to_pos].type = S_COL_FROM;
916         col_state[to_pos].visible = show_from;
917
918         summaryview->col_pos[S_COL_TO] = from_pos;
919         summaryview->col_pos[S_COL_FROM] = to_pos;
920
921         gtk_clist_set_column_visibility
922                 (GTK_CLIST(ctree), from_pos, col_state[from_pos].visible);
923         gtk_clist_set_column_visibility
924                 (GTK_CLIST(ctree), to_pos, col_state[to_pos].visible);
925
926         summary_set_column_titles(summaryview);
927 }
928
929 static gboolean summaryview_quicksearch_recurse(gpointer data)
930 {
931         SummaryView *summaryview = (SummaryView *)data;
932         main_window_cursor_wait(summaryview->mainwin);
933         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
934         quicksearch_search_subfolders(summaryview->quicksearch, 
935                               summaryview->folderview,
936                               summaryview->folder_item);
937         main_window_cursor_normal(summaryview->mainwin);
938         return FALSE;
939 }
940
941 static gboolean summary_check_consistency(FolderItem *item, GSList *mlist)
942 {
943         int u = 0, n = 0, m = 0, t = 0;
944         GSList *cur;
945         START_TIMING("");
946         for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
947                 MsgInfo * msginfo = (MsgInfo *) cur->data;
948                 t++;
949                 if (MSG_IS_NEW(msginfo->flags))
950                         n++;
951                 if (MSG_IS_UNREAD(msginfo->flags))
952                         u++;
953                 if (MSG_IS_MARKED(msginfo->flags))
954                         m++;            
955         }
956         if (t != item->total_msgs
957         ||  n != item->new_msgs
958         ||  u != item->unread_msgs
959         ||  m != item->marked_msgs
960         ||  (m == 0 && item->unreadmarked_msgs != 0)
961         ||  item->unreadmarked_msgs < 0) {
962                 debug_print("Inconsistency\n");
963                 folder_item_scan_full(item, FALSE);
964                 END_TIMING();
965                 return FALSE;
966         } 
967         END_TIMING();
968         return TRUE;
969 }
970
971 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
972 {
973         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
974         GtkCTreeNode *node = NULL;
975         GSList *mlist = NULL;
976         gchar *buf;
977         gboolean is_refresh;
978         guint selected_msgnum = 0;
979         guint displayed_msgnum = 0;
980         GSList *cur;
981         GSList *not_killed;
982         gboolean hidden_removed = FALSE;
983
984         if (summary_is_locked(summaryview)) return FALSE;
985
986         if (!summaryview->mainwin)
987                 return FALSE;
988         START_TIMING("");
989         summaryview->last_displayed = NULL;
990         summary_switch_from_to(summaryview, item);
991
992         inc_lock();
993         summary_lock(summaryview);
994
995         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
996
997         if (!is_refresh)
998                 main_create_mailing_list_menu (summaryview->mainwin, NULL);
999
1000         if (!prefs_common.summary_quicksearch_sticky
1001          && !prefs_common.summary_quicksearch_recurse
1002          && !quicksearch_is_running(summaryview->quicksearch)
1003          && !is_refresh) {
1004                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
1005         }
1006
1007         /* STATUSBAR_POP(summaryview->mainwin); */
1008
1009         if (is_refresh) {
1010                 selected_msgnum = summary_get_msgnum(summaryview,
1011                                                      summaryview->selected);
1012                 displayed_msgnum = summary_get_msgnum(summaryview,
1013                                                       summaryview->displayed);
1014         }
1015
1016         /* process the marks if any */
1017         if (summaryview->mainwin->lock_count == 0 &&
1018             (summaryview->moved > 0 || summaryview->copied > 0)) {
1019                 AlertValue val;
1020                 gboolean changed = FALSE;
1021
1022                 val = alertpanel(_("Process mark"),
1023                                  _("Some marks are left. Process them?"),
1024                                  GTK_STOCK_NO, GTK_STOCK_YES, GTK_STOCK_CANCEL);
1025                 if (G_ALERTALTERNATE == val) {
1026                         summary_unlock(summaryview);
1027                         summary_execute(summaryview);
1028                         summary_lock(summaryview);
1029                         changed = TRUE;
1030                 } else if (G_ALERTDEFAULT == val) {
1031                         /* DO NOTHING */
1032                 } else {
1033                         summary_unlock(summaryview);
1034                         inc_unlock();
1035                         END_TIMING();
1036                         return FALSE;
1037                 }
1038                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
1039                         folder_update_op_count();
1040         }
1041         
1042         summary_freeze(summaryview);
1043
1044         summary_clear_list(summaryview);
1045
1046         buf = NULL;
1047         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
1048                 g_free(buf);
1049                 debug_print("empty folder (%p %s %p %d)\n\n",
1050                                 item, 
1051                                 item?item->path:"NULL",
1052                                 item?folder_item_parent(item):0x0,
1053                                 item?item->no_select:FALSE);
1054                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
1055                 summary_clear_all(summaryview);
1056                 summaryview->folder_item = item;
1057                 summary_thaw(summaryview);
1058                 summary_unlock(summaryview);
1059                 inc_unlock();
1060                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
1061                         main_window_cursor_wait(summaryview->mainwin);
1062                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
1063                         if (quicksearch_is_active(summaryview->quicksearch))
1064                                 quicksearch_search_subfolders(summaryview->quicksearch, 
1065                                               summaryview->folderview,
1066                                               summaryview->folder_item);
1067                         main_window_cursor_normal(summaryview->mainwin);
1068                 }       
1069                 END_TIMING();           
1070                 return TRUE;
1071         }
1072         g_free(buf);
1073
1074         if (!is_refresh)
1075                 messageview_clear(summaryview->messageview);
1076
1077         summaryview->folder_item = item;
1078         item->opened = TRUE;
1079
1080         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
1081         debug_print("%s\n", buf);
1082         STATUSBAR_PUSH(summaryview->mainwin, buf);
1083         g_free(buf);
1084
1085         main_window_cursor_wait(summaryview->mainwin);
1086
1087         mlist = folder_item_get_msg_list(item);
1088
1089         if (!summary_check_consistency(item, mlist)) {
1090                 debug_print("reloading due to inconsistency\n");
1091                 procmsg_msg_list_free(mlist);
1092                 mlist = folder_item_get_msg_list(item);
1093         }
1094
1095         if (summaryview->folder_item->hide_read_msgs &&
1096             quicksearch_is_active(summaryview->quicksearch) == FALSE) {
1097                 GSList *not_killed;
1098                 
1099                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
1100                 not_killed = NULL;
1101                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1102                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1103                         
1104                         if (!msginfo->hidden) {
1105                                 if (MSG_IS_UNREAD(msginfo->flags) &&
1106                                     !MSG_IS_IGNORE_THREAD(msginfo->flags))
1107                                         not_killed = g_slist_prepend(not_killed, msginfo);
1108                                 else if (MSG_IS_MARKED(msginfo->flags) ||
1109                                          MSG_IS_LOCKED(msginfo->flags))
1110                                         not_killed = g_slist_prepend(not_killed, msginfo);
1111                                 else if (is_refresh &&
1112                                         (msginfo->msgnum == selected_msgnum ||
1113                                          msginfo->msgnum == displayed_msgnum))
1114                                         not_killed = g_slist_prepend(not_killed, msginfo);
1115                                 else
1116                                         procmsg_msginfo_free(msginfo);
1117                          } else
1118                                 procmsg_msginfo_free(msginfo);
1119                 }
1120                 hidden_removed = TRUE;
1121                 g_slist_free(mlist);
1122                 mlist = not_killed;
1123         } else {
1124                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
1125         }
1126
1127         if (quicksearch_is_active(summaryview->quicksearch)) {
1128                 GSList *not_killed;
1129                 gint interval = quicksearch_is_fast(summaryview->quicksearch) ? 5000:100;
1130                 START_TIMING("quicksearch");
1131                 gint num = 0, total = summaryview->folder_item->total_msgs;
1132                 statusbar_print_all(_("Searching in %s... \n"), 
1133                         summaryview->folder_item->path ? 
1134                         summaryview->folder_item->path : "(null)");
1135                 not_killed = NULL;
1136                 folder_item_update_freeze();
1137                 for (cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1138                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1139
1140                         statusbar_progress_all(num++,total, interval);
1141
1142                         if (!msginfo->hidden && quicksearch_match(summaryview->quicksearch, msginfo))
1143                                 not_killed = g_slist_prepend(not_killed, msginfo);
1144                         else
1145                                 procmsg_msginfo_free(msginfo);
1146                         if (num % interval == 0)
1147                                 GTK_EVENTS_FLUSH();
1148                         if (!quicksearch_is_active(summaryview->quicksearch)) {
1149                                 break;
1150                         }
1151                 }
1152                 folder_item_update_thaw();
1153                 statusbar_progress_all(0,0,0);
1154                 statusbar_pop_all();
1155                 
1156                 hidden_removed = TRUE;
1157                 if (!quicksearch_is_active(summaryview->quicksearch)) {
1158                         debug_print("search cancelled!\n");
1159                         summary_thaw(summaryview);
1160                         STATUSBAR_POP(summaryview->mainwin);
1161                         main_window_cursor_normal(summaryview->mainwin);
1162                         summary_unlock(summaryview);
1163                         inc_unlock();
1164                         summary_show(summaryview, summaryview->folder_item);
1165                         END_TIMING();
1166                         return FALSE;
1167                 }
1168                 g_slist_free(mlist);
1169                 mlist = not_killed;
1170                 END_TIMING();
1171         }
1172
1173         if (!hidden_removed) {
1174                 START_TIMING("removing hidden");
1175                 not_killed = NULL;
1176                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1177                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1178
1179                         if (!msginfo->hidden)
1180                                 not_killed = g_slist_prepend(not_killed, msginfo);
1181                         else
1182                                 procmsg_msginfo_free(msginfo);
1183                 }
1184                 g_slist_free(mlist);
1185                 mlist = not_killed;
1186                 END_TIMING();
1187         }
1188
1189         STATUSBAR_POP(summaryview->mainwin);
1190
1191         /* set ctree and hash table from the msginfo list, and
1192            create the thread */
1193         summary_set_ctree_from_list(summaryview, mlist);
1194
1195         g_slist_free(mlist);
1196
1197         if (quicksearch_is_active(summaryview->quicksearch) &&
1198             quicksearch_is_running(summaryview->quicksearch)) {
1199                 /* only scan subfolders when quicksearch changed,
1200                  * not when search is the same and folder changed */
1201                 g_timeout_add(100, summaryview_quicksearch_recurse, summaryview);
1202         }
1203
1204         if (is_refresh) {
1205                 if (!quicksearch_is_in_typing(summaryview->quicksearch)) {
1206                         summaryview->last_displayed = summaryview->displayed;
1207                         summaryview->displayed =
1208                                 summary_find_msg_by_msgnum(summaryview,
1209                                                            displayed_msgnum);
1210                         if (!summaryview->displayed)
1211                                 messageview_clear(summaryview->messageview);
1212                         summary_unlock(summaryview);
1213                         summary_select_by_msgnum(summaryview, selected_msgnum);
1214                         summary_lock(summaryview);
1215                         if (!summaryview->selected) {
1216                                 /* no selected message - select first unread
1217                                    message, but do not display it */
1218                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1219                                                                      MSG_UNREAD, FALSE);
1220                                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
1221                                         node = gtk_ctree_node_nth
1222                                                 (ctree,
1223                                                  item->sort_type == SORT_DESCENDING
1224                                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1225                                 summary_unlock(summaryview);
1226                                 summary_select_node(summaryview, node, FALSE, TRUE);
1227                                 summary_lock(summaryview);
1228                         }
1229                 } else {
1230                         /* just select first/last */
1231                         if (GTK_CLIST(ctree)->row_list != NULL)
1232                                 node = gtk_ctree_node_nth
1233                                         (ctree,
1234                                          item->sort_type == SORT_DESCENDING
1235                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
1236                         gtk_sctree_select(GTK_SCTREE(ctree), node);
1237                         gtk_ctree_node_moveto(ctree, node, 0, 0.5, 0);
1238                 }
1239         } else {
1240                 switch (prefs_common.select_on_entry) {
1241                         case SELECTONENTRY_MNU:
1242                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1243                                                                      MSG_MARKED, FALSE);
1244                                 if (node == NULL)
1245                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1246                                                                      MSG_NEW, FALSE);
1247                                 if (node == NULL)
1248                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1249                                                                      MSG_UNREAD, FALSE);
1250                                 break;
1251                         case SELECTONENTRY_MUN:
1252                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1253                                                                      MSG_MARKED, FALSE);
1254                                 if (node == NULL)
1255                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1256                                                                      MSG_UNREAD, FALSE);
1257                                 if (node == NULL)
1258                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1259                                                                      MSG_NEW, FALSE);
1260                                 break;
1261                         case SELECTONENTRY_NMU:
1262                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1263                                                                      MSG_NEW, FALSE);
1264                                 if (node == NULL)
1265                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1266                                                                      MSG_MARKED, FALSE);
1267                                 if (node == NULL)
1268                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1269                                                                      MSG_UNREAD, FALSE);
1270                                 break;
1271                         case SELECTONENTRY_NUM:
1272                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1273                                                                      MSG_NEW, FALSE);
1274                                 if (node == NULL)
1275                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1276                                                                      MSG_UNREAD, FALSE);
1277                                 if (node == NULL)
1278                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1279                                                                      MSG_MARKED, FALSE);
1280                                 break;
1281                         case SELECTONENTRY_UNM:
1282                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1283                                                                      MSG_UNREAD, FALSE);
1284                                 if (node == NULL)
1285                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1286                                                                      MSG_NEW, FALSE);
1287                                 if (node == NULL)
1288                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1289                                                                      MSG_MARKED, FALSE);
1290                                 break;
1291                         case SELECTONENTRY_UMN:
1292                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1293                                                                      MSG_UNREAD, FALSE);
1294                                 if (node == NULL)
1295                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1296                                                                      MSG_MARKED, FALSE);
1297                                 if (node == NULL)
1298                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1299                                                                      MSG_NEW, FALSE);
1300                                 break;
1301                         default:
1302                                 node = NULL;
1303                 }
1304
1305                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1306                         node = gtk_ctree_node_nth
1307                                 (ctree,
1308                                  item->sort_type == SORT_DESCENDING
1309                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1310                 }
1311                 summary_unlock(summaryview);
1312                 summary_select_node(summaryview, node,
1313                                     prefs_common.always_show_msg,
1314                                     TRUE);
1315                 summary_lock(summaryview);
1316         }
1317
1318         summary_status_show(summaryview);
1319         summary_set_menu_sensitive(summaryview);
1320         toolbar_main_set_sensitive(summaryview->mainwin);
1321         
1322         summary_thaw(summaryview);
1323         debug_print("\n");
1324         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1325         STATUSBAR_POP(summaryview->mainwin);
1326         main_window_cursor_normal(summaryview->mainwin);
1327         summary_unlock(summaryview);
1328         inc_unlock();
1329         END_TIMING();
1330         return TRUE;
1331 }
1332
1333 #undef CURRENTLY_DISPLAYED
1334
1335
1336 void summary_clear_list(SummaryView *summaryview)
1337 {
1338         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1339         gint optimal_width;
1340
1341         summary_freeze(summaryview);
1342
1343         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1344                                 NULL, summary_free_msginfo_func, NULL);
1345
1346         if (summaryview->folder_item) {
1347                 summaryview->folder_item->opened = FALSE;
1348                 summaryview->folder_item = NULL;
1349         }
1350
1351         summaryview->display_msg = FALSE;
1352
1353         summaryview->selected = NULL;
1354         summaryview->displayed = NULL;
1355         summaryview->total_size = 0;
1356         summaryview->deleted = summaryview->moved = 0;
1357         summaryview->copied = 0;
1358         if (summaryview->msgid_table) {
1359                 g_hash_table_destroy(summaryview->msgid_table);
1360                 summaryview->msgid_table = NULL;
1361         }
1362         if (summaryview->subject_table) {
1363                 g_hash_table_destroy(summaryview->subject_table);
1364                 summaryview->subject_table = NULL;
1365         }
1366         summaryview->mlist = NULL;
1367
1368         gtk_clist_clear(clist);
1369         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1370                 optimal_width = gtk_clist_optimal_column_width
1371                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1372                 gtk_clist_set_column_width
1373                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1374                          optimal_width);
1375         }
1376
1377         summary_thaw(summaryview);
1378 }
1379
1380 void summary_clear_all(SummaryView *summaryview)
1381 {
1382         messageview_clear(summaryview->messageview);
1383         summary_clear_list(summaryview);
1384         summary_set_menu_sensitive(summaryview);
1385         toolbar_main_set_sensitive(summaryview->mainwin);
1386         summary_status_show(summaryview);
1387 }
1388
1389 void summary_lock(SummaryView *summaryview)
1390 {
1391         summaryview->lock_count++;
1392 }
1393
1394 void summary_unlock(SummaryView *summaryview)
1395 {
1396         if (summaryview->lock_count)
1397                 summaryview->lock_count--;
1398 }
1399
1400 gboolean summary_is_locked(SummaryView *summaryview)
1401 {
1402         return summaryview->lock_count > 0;
1403 }
1404
1405 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1406 {
1407         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1408         SummarySelection selection;
1409
1410         if (!clist->row_list)
1411                 selection = SUMMARY_NONE;
1412         else if (!clist->selection)
1413                 selection = SUMMARY_SELECTED_NONE;
1414         else if (!clist->selection->next)
1415                 selection = SUMMARY_SELECTED_SINGLE;
1416         else
1417                 selection = SUMMARY_SELECTED_MULTIPLE;
1418
1419         return selection;
1420 }
1421
1422 /*!
1423  *\return       MsgInfo * Selected message if there's one selected;
1424  *              if multiple selected, or none, return NULL.
1425  */ 
1426 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1427 {
1428         /* summaryview->selected may be valid when multiple 
1429          * messages were selected */
1430         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1431
1432         if (sellist == NULL || sellist->next) 
1433                 return NULL;
1434         
1435         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1436 }
1437
1438 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1439 {
1440         GSList *mlist = NULL;
1441         GList *cur;
1442         MsgInfo *msginfo;
1443
1444         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1445              cur = cur->next) {
1446                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1447                 mlist = g_slist_prepend(mlist, msginfo);
1448         }
1449
1450         mlist = g_slist_reverse(mlist);
1451
1452         return mlist;
1453 }
1454
1455 void summary_set_menu_sensitive(SummaryView *summaryview)
1456 {
1457         GtkItemFactory *ifactory = summaryview->popupfactory;
1458         SensitiveCond state;
1459         gboolean sensitive;
1460         GtkWidget *menuitem;
1461         gint i;
1462
1463         static const struct {
1464                 gchar *const entry;
1465                 SensitiveCond cond;
1466         } entry[] = {
1467                 {"/Reply"                       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1468                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1469                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1470                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1471                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1472
1473                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1474                 {"/Forward as attachment"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1475                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1476
1477                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1478                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC},
1479                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1480                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE},
1481
1482                 {"/Mark"                        , M_TARGET_EXIST},
1483                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1484                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1485                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1486                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1487                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1488                 {"/Mark/Unignore thread"        , M_TARGET_EXIST},
1489                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1490                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1491                 {"/Mark/Mark as spam"           , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1492                 {"/Mark/Mark as ham"            , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1493                 {"/Color label"                 , M_TARGET_EXIST},
1494
1495                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1496                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1497                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1498
1499                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1500                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1501                 {"/View/Message source"         , M_SINGLE_TARGET_EXIST},
1502                 {"/View/All headers"            , M_SINGLE_TARGET_EXIST},
1503                 {"/Save as..."                  , M_TARGET_EXIST},
1504                 {"/Print..."                    , M_TARGET_EXIST},
1505                 {NULL, 0}
1506         };
1507
1508         main_window_set_menu_sensitive(summaryview->mainwin);
1509
1510         state = main_window_get_current_state(summaryview->mainwin);
1511
1512         for (i = 0; entry[i].entry != NULL; i++) {
1513                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1514                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1515         }
1516
1517
1518         summary_lock(summaryview);
1519         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All headers");
1520         if (summaryview->messageview 
1521         &&  summaryview->messageview->mimeview
1522         &&  summaryview->messageview->mimeview->textview)
1523                 gtk_check_menu_item_set_active
1524                         (GTK_CHECK_MENU_ITEM(menuitem),
1525                          summaryview->messageview->mimeview->textview->show_all_headers);
1526         summary_unlock(summaryview);
1527 }
1528
1529 void summary_select_prev_unread(SummaryView *summaryview)
1530 {
1531         GtkCTreeNode *node;
1532         gboolean skip_cur = FALSE;
1533
1534         if (summaryview->displayed 
1535         &&  summaryview->selected == summaryview->displayed) {
1536                 debug_print("skipping current\n");
1537                 skip_cur = TRUE;
1538         }
1539
1540         node = summary_find_prev_flagged_msg
1541                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1542
1543         if (!node || node == summaryview->selected) {
1544                 AlertValue val = 0;
1545
1546                 switch (prefs_common.next_unread_msg_dialog) {
1547                         case NEXTUNREADMSGDIALOG_ALWAYS:
1548                                 val = alertpanel(_("No more unread messages"),
1549                                                  _("No unread message found. "
1550                                                    "Search from the end?"),
1551                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1552                                 break;
1553                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1554                                 val = G_ALERTALTERNATE;
1555                                 break;
1556                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1557                                 val = !G_ALERTALTERNATE;
1558                                 break;
1559                         default:
1560                                 debug_print(
1561                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1562                 }
1563                 if (val != G_ALERTALTERNATE) return;
1564                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1565                                                      MSG_UNREAD, FALSE);
1566         }
1567
1568         if (!node)
1569                 alertpanel_notice(_("No unread messages."));
1570         else
1571                 summary_select_node(summaryview, node, TRUE, FALSE);
1572 }
1573
1574 void summary_select_next_unread(SummaryView *summaryview)
1575 {
1576         GtkCTreeNode *node = summaryview->selected;
1577         gboolean skip_cur = FALSE;
1578         
1579         if (summaryview->displayed 
1580         &&  summaryview->selected == summaryview->displayed) {
1581                 debug_print("skipping cur (%p %p)\n",
1582                         summaryview->displayed, summaryview->selected);
1583                 skip_cur = TRUE;
1584         }
1585
1586
1587         node = summary_find_next_flagged_msg
1588                 (summaryview, node, MSG_UNREAD, skip_cur);
1589         
1590         if (node)
1591                 summary_select_node(summaryview, node, TRUE, FALSE);
1592         else {
1593                 node = summary_find_next_flagged_msg
1594                         (summaryview, NULL, MSG_UNREAD, FALSE);
1595                 if (node == NULL || node == summaryview->selected) {
1596                         AlertValue val = 0;
1597
1598                         switch (prefs_common.next_unread_msg_dialog) {
1599                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1600                                         val = alertpanel(_("No more unread messages"),
1601                                                          _("No unread message found. "
1602                                                            "Go to next folder?"),
1603                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1604                                         break;
1605                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1606                                         val = G_ALERTALTERNATE;
1607                                         break;
1608                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1609                                         val = G_ALERTOTHER;
1610                                         break;
1611                                 default:
1612                                         debug_print(
1613                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1614                         }
1615
1616                         if (val == G_ALERTALTERNATE) {
1617                                 folderview_select_next_unread(summaryview->folderview, TRUE);
1618                                 return;
1619                         } 
1620                         else
1621                                 return;
1622                 } else
1623                         summary_select_node(summaryview, node, TRUE, FALSE);
1624
1625         }
1626 }
1627
1628 void summary_select_prev_new(SummaryView *summaryview)
1629 {
1630         GtkCTreeNode *node;
1631         gboolean skip_cur = FALSE;
1632
1633         if (summaryview->displayed 
1634         &&  summaryview->selected == summaryview->displayed) {
1635                 debug_print("skipping current\n");
1636                 skip_cur = TRUE;
1637         }
1638
1639         node = summary_find_prev_flagged_msg
1640                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1641
1642         if (!node || node == summaryview->selected) {
1643                 AlertValue val = 0;
1644
1645                 switch (prefs_common.next_unread_msg_dialog) {
1646                         case NEXTUNREADMSGDIALOG_ALWAYS:
1647                                 val = alertpanel(_("No more new messages"),
1648                                                  _("No new message found. "
1649                                                    "Search from the end?"),
1650                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1651                                 break;
1652                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1653                                 val = G_ALERTALTERNATE;
1654                                 break;
1655                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1656                                 val = !G_ALERTALTERNATE;
1657                                 break;
1658                         default:
1659                                 debug_print(
1660                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1661                 }
1662                 if (val != G_ALERTALTERNATE) return;
1663                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1664                                                      MSG_NEW, FALSE);
1665         }
1666
1667         if (!node)
1668                 alertpanel_notice(_("No new messages."));
1669         else
1670                 summary_select_node(summaryview, node, TRUE, FALSE);
1671 }
1672
1673 void summary_select_next_new(SummaryView *summaryview)
1674 {
1675         GtkCTreeNode *node = summaryview->selected;
1676         gboolean skip_cur = FALSE;
1677         
1678         if (summaryview->displayed 
1679         &&  summaryview->selected == summaryview->displayed) {
1680                 debug_print("skipping cur (%p %p)\n",
1681                         summaryview->displayed, summaryview->selected);
1682                 skip_cur = TRUE;
1683         }
1684
1685
1686         node = summary_find_next_flagged_msg
1687                 (summaryview, node, MSG_NEW, skip_cur);
1688         
1689         if (node)
1690                 summary_select_node(summaryview, node, TRUE, FALSE);
1691         else {
1692                 node = summary_find_next_flagged_msg
1693                         (summaryview, NULL, MSG_NEW, FALSE);
1694                 if (node == NULL || node == summaryview->selected) {
1695                         AlertValue val = 0;
1696
1697                         switch (prefs_common.next_unread_msg_dialog) {
1698                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1699                                         val = alertpanel(_("No more new messages"),
1700                                                          _("No new message found. "
1701                                                            "Go to next folder?"),
1702                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1703                                         break;
1704                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1705                                         val = G_ALERTALTERNATE;
1706                                         break;
1707                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1708                                         val = G_ALERTOTHER;
1709                                         break;
1710                                 default:
1711                                         debug_print(
1712                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1713                         }
1714
1715                         if (val == G_ALERTALTERNATE) {
1716                                 folderview_select_next_new(summaryview->folderview);
1717                                 return;
1718                         } 
1719                         else
1720                                 return;
1721                 } else
1722                         summary_select_node(summaryview, node, TRUE, FALSE);
1723
1724         }
1725 }
1726
1727 void summary_select_prev_marked(SummaryView *summaryview)
1728 {
1729         GtkCTreeNode *node;
1730
1731         node = summary_find_prev_flagged_msg
1732                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1733
1734         if (!node) {
1735                 AlertValue val;
1736
1737                 val = alertpanel(_("No more marked messages"),
1738                                  _("No marked message found. "
1739                                    "Search from the end?"),
1740                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1741                 if (val != G_ALERTALTERNATE) return;
1742                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1743                                                      MSG_MARKED, TRUE);
1744         }
1745
1746         if (!node)
1747                 alertpanel_notice(_("No marked messages."));
1748         else
1749                 summary_select_node(summaryview, node, TRUE, FALSE);
1750 }
1751
1752 void summary_select_next_marked(SummaryView *summaryview)
1753 {
1754         GtkCTreeNode *node = summaryview->selected;
1755         gboolean skip_cur = FALSE;
1756         
1757         if (summaryview->displayed 
1758         &&  summaryview->selected == summaryview->displayed) {
1759                 debug_print("skipping cur (%p %p)\n",
1760                         summaryview->displayed, summaryview->selected);
1761                 skip_cur = TRUE;
1762         }
1763
1764
1765         node = summary_find_next_flagged_msg
1766                 (summaryview, node, MSG_MARKED, skip_cur);
1767         
1768         if (node)
1769                 summary_select_node(summaryview, node, TRUE, FALSE);
1770         else {
1771                 node = summary_find_next_flagged_msg
1772                         (summaryview, NULL, MSG_MARKED, FALSE);
1773                 if (node == NULL || node == summaryview->selected) {
1774                         AlertValue val = 0;
1775
1776                         switch (prefs_common.next_unread_msg_dialog) {
1777                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1778                                         val = alertpanel(_("No more marked messages"),
1779                                                          _("No marked message found. "
1780                                                            "Go to next folder?"),
1781                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1782                                         break;
1783                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1784                                         val = G_ALERTALTERNATE;
1785                                         break;
1786                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1787                                         val = G_ALERTOTHER;
1788                                         break;
1789                                 default:
1790                                         debug_print(
1791                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1792                         }
1793
1794                         if (val == G_ALERTALTERNATE) {
1795                                 folderview_select_next_marked(summaryview->folderview);
1796                                 return;
1797                         } 
1798                         else
1799                                 return;
1800                 } else
1801                         summary_select_node(summaryview, node, TRUE, FALSE);
1802
1803         }
1804 }
1805
1806 void summary_select_prev_labeled(SummaryView *summaryview)
1807 {
1808         GtkCTreeNode *node;
1809
1810         node = summary_find_prev_flagged_msg
1811                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1812
1813         if (!node) {
1814                 AlertValue val;
1815
1816                 val = alertpanel(_("No more labeled messages"),
1817                                  _("No labeled message found. "
1818                                    "Search from the end?"),
1819                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1820                 if (val != G_ALERTALTERNATE) return;
1821                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1822                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1823         }
1824
1825         if (!node)
1826                 alertpanel_notice(_("No labeled messages."));
1827         else
1828                 summary_select_node(summaryview, node, TRUE, FALSE);
1829 }
1830
1831 void summary_select_next_labeled(SummaryView *summaryview)
1832 {
1833         GtkCTreeNode *node;
1834
1835         node = summary_find_next_flagged_msg
1836                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1837
1838         if (!node) {
1839                 AlertValue val;
1840
1841                 val = alertpanel(_("No more labeled messages"),
1842                                  _("No labeled message found. "
1843                                    "Search from the beginning?"),
1844                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1845                 if (val != G_ALERTALTERNATE) return;
1846                 node = summary_find_next_flagged_msg(summaryview, NULL,
1847                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1848         }
1849
1850         if (!node)
1851                 alertpanel_notice(_("No labeled messages."));
1852         else
1853                 summary_select_node(summaryview, node, TRUE, FALSE);
1854 }
1855
1856 void summary_select_last_read(SummaryView *summaryview)
1857 {
1858         if (summaryview->last_displayed)
1859                 summary_select_node(summaryview, summaryview->last_displayed, TRUE, FALSE);
1860 }
1861
1862 void summary_select_parent(SummaryView *summaryview)
1863 {
1864         GtkCTreeNode *node = NULL;
1865
1866         if (summaryview->selected)
1867                 node = GTK_CTREE_ROW(summaryview->selected)->parent;
1868         if (node)
1869                 summary_select_node(summaryview, node, TRUE, FALSE);
1870 }
1871
1872 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1873 {
1874         GtkCTreeNode *node;
1875
1876         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1877         summary_select_node(summaryview, node, FALSE, TRUE);
1878 }
1879
1880 typedef struct _PostponedSelectData
1881 {
1882         GtkCTree *ctree;
1883         GtkCTreeNode *row;
1884         GtkCTreeNode *node;
1885         GtkScrollType type;
1886         gint column;
1887         SummaryView *summaryview;
1888         gboolean display_msg;
1889         gboolean do_refresh;
1890 } PostponedSelectData;
1891
1892 static gboolean summary_select_retry(void *data)
1893 {
1894         PostponedSelectData *psdata = (PostponedSelectData *)data;
1895         debug_print("trying again\n");
1896         if (psdata->row)
1897                 summary_selected(psdata->ctree, psdata->row,
1898                             psdata->column, psdata->summaryview);
1899         else if (psdata->node)
1900                 summary_select_node(psdata->summaryview, psdata->node,
1901                             psdata->display_msg, psdata->do_refresh);
1902         else
1903                 summary_step(psdata->summaryview, psdata->type);
1904         g_free(psdata);
1905         return FALSE;
1906 }
1907
1908 /**
1909  * summary_select_node:
1910  * @summaryview: Summary view.
1911  * @node: Summary tree node.
1912  * @display_msg: TRUE to display the selected message.
1913  * @do_refresh: TRUE to refresh the widget.
1914  *
1915  * Select @node (bringing it into view by scrolling and expanding its
1916  * thread, if necessary) and unselect all others.  If @display_msg is
1917  * TRUE, display the corresponding message in the message view.
1918  * If @do_refresh is TRUE, the widget is refreshed.
1919  **/
1920 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1921                          gboolean display_msg, gboolean do_refresh)
1922 {
1923         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1924         
1925         if (summary_is_locked(summaryview)
1926         && !GTK_SCTREE(ctree)->selecting_range
1927         && summaryview->messageview->mimeview
1928         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
1929         && summaryview->messageview->mimeview->textview->loading) {
1930                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
1931                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
1932                 
1933                 data->ctree = ctree;
1934                 data->row = NULL;
1935                 data->node = node;
1936                 data->summaryview = summaryview;
1937                 data->display_msg = display_msg;
1938                 data->do_refresh = do_refresh;
1939                 debug_print("postponing open of message till end of load\n");
1940                 g_timeout_add(100, summary_select_retry, data);
1941                 return;
1942         }
1943         if (summary_is_locked(summaryview)) {
1944                 return;
1945         }
1946         if (!summaryview->folder_item)
1947                 return;
1948         if (node) {
1949                 gtkut_ctree_expand_parent_all(ctree, node);
1950                 if (do_refresh) {
1951                         summary_lock(summaryview);
1952                         GTK_EVENTS_FLUSH();
1953                         summary_unlock(summaryview);
1954                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1955                         gtk_ctree_node_moveto(ctree, node, 0, 0.5, 0);
1956                 }
1957                 if (display_msg && summaryview->displayed == node)
1958                         summaryview->displayed = NULL;
1959                 summaryview->display_msg = display_msg;
1960                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1961                 if (summaryview->selected == NULL)
1962                         summaryview->selected = node;
1963         }
1964 }
1965
1966 guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1967 {
1968         GtkCTree *ctree =NULL;
1969         MsgInfo *msginfo;
1970
1971         if (!summaryview)
1972                 return 0;
1973         ctree = GTK_CTREE(summaryview->ctree);
1974         if (!node)
1975                 return 0;
1976         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1977         if (msginfo)
1978                 return msginfo->msgnum;
1979         else 
1980                 return -1;
1981 }
1982
1983 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1984                                            GtkCTreeNode *current_node)
1985 {
1986         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1987         GtkCTreeNode *node;
1988         MsgInfo *msginfo;
1989
1990         if (current_node)
1991                 node = current_node;
1992         else
1993                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1994
1995         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1996                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1997                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1998         }
1999
2000         return node;
2001 }
2002
2003 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
2004                                            GtkCTreeNode *current_node)
2005 {
2006         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2007         GtkCTreeNode *node;
2008         MsgInfo *msginfo;
2009
2010         if (current_node)
2011                 node = current_node;
2012         else
2013                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2014
2015         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2016                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2017                 if (msginfo && !MSG_IS_DELETED(msginfo->flags) 
2018                 && !MSG_IS_MOVE(msginfo->flags)) break;
2019         }
2020
2021         return node;
2022 }
2023
2024 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
2025                                                    GtkCTreeNode *current_node,
2026                                                    MsgPermFlags flags,
2027                                                    gboolean start_from_prev)
2028 {
2029         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2030         GtkCTreeNode *node;
2031         MsgInfo *msginfo;
2032
2033         if (current_node) {
2034                 if (start_from_prev)
2035                         node = GTK_CTREE_NODE_PREV(current_node);
2036                 else
2037                         node = current_node;
2038         } else
2039                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
2040
2041         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
2042                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2043                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
2044         }
2045
2046         return node;
2047 }
2048
2049 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
2050                                                    GtkCTreeNode *current_node,
2051                                                    MsgPermFlags flags,
2052                                                    gboolean start_from_next)
2053 {
2054         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2055         GtkCTreeNode *node;
2056         MsgInfo *msginfo;
2057
2058         if (current_node) {
2059                 if (start_from_next)
2060                         node = gtkut_ctree_node_next(ctree, current_node);
2061                 else
2062                         node = current_node;
2063         } else
2064                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2065
2066         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2067                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2068                 /* Find msg with matching flags but ignore messages with
2069                    ignore flags, if searching for new or unread messages */
2070                 if ((msginfo && (msginfo->flags.perm_flags & flags) != 0) &&
2071                     !(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) 
2072                         )
2073                         break;
2074         }
2075
2076         return node;
2077 }
2078
2079 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
2080                                                 guint msgnum)
2081 {
2082         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2083         GtkCTreeNode *node;
2084         MsgInfo *msginfo;
2085
2086         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2087
2088         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2089                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2090                 if (msginfo && msginfo->msgnum == msgnum) break;
2091         }
2092
2093         return node;
2094 }
2095
2096 static guint attract_hash_func(gconstpointer key)
2097 {
2098         gchar *str;
2099         gchar *p;
2100         guint h;
2101
2102         Xstrdup_a(str, (const gchar *)key, return 0);
2103         trim_subject(str);
2104
2105         p = str;
2106         h = *p;
2107
2108         if (h) {
2109                 for (p += 1; *p != '\0'; p++)
2110                         h = (h << 5) - h + *p;
2111         }
2112
2113         return h;
2114 }
2115
2116 static gint attract_compare_func(gconstpointer a, gconstpointer b)
2117 {
2118         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
2119 }
2120
2121 void summary_attract_by_subject(SummaryView *summaryview)
2122 {
2123         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2124         GtkCList *clist = GTK_CLIST(ctree);
2125         GtkCTreeNode *src_node;
2126         GtkCTreeNode *dst_node, *sibling;
2127         GtkCTreeNode *tmp;
2128         MsgInfo *src_msginfo, *dst_msginfo;
2129         GHashTable *subject_table;
2130
2131         debug_print("Attracting messages by subject...");
2132         STATUSBAR_PUSH(summaryview->mainwin,
2133                        _("Attracting messages by subject..."));
2134
2135         main_window_cursor_wait(summaryview->mainwin);
2136         summary_freeze(summaryview);
2137
2138         subject_table = g_hash_table_new(attract_hash_func,
2139                                          attract_compare_func);
2140
2141         for (src_node = GTK_CTREE_NODE(clist->row_list);
2142              src_node != NULL;
2143              src_node = tmp) {
2144                 tmp = GTK_CTREE_ROW(src_node)->sibling;
2145                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
2146                 if (!src_msginfo) continue;
2147                 if (!src_msginfo->subject) continue;
2148
2149                 /* find attracting node */
2150                 dst_node = g_hash_table_lookup(subject_table,
2151                                                src_msginfo->subject);
2152
2153                 if (dst_node) {
2154                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
2155
2156                         /* if the time difference is more than 20 days,
2157                            don't attract */
2158                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
2159                             > 60 * 60 * 24 * 20)
2160                                 continue;
2161
2162                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
2163                         if (src_node != sibling)
2164                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
2165                 }
2166
2167                 g_hash_table_insert(subject_table,
2168                                     src_msginfo->subject, src_node);
2169         }
2170
2171         g_hash_table_destroy(subject_table);
2172
2173         gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2174
2175         summary_thaw(summaryview);
2176
2177         debug_print("done.\n");
2178         STATUSBAR_POP(summaryview->mainwin);
2179
2180         main_window_cursor_normal(summaryview->mainwin);
2181 }
2182
2183 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
2184                                       gpointer data)
2185 {
2186         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
2187
2188         if (msginfo)
2189                 procmsg_msginfo_free(msginfo);
2190 }
2191
2192 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
2193                                    gpointer data)
2194 {
2195         SummaryView *summaryview = data;
2196         MsgInfo *msginfo;
2197
2198         msginfo = gtk_ctree_node_get_row_data(ctree, node);
2199
2200         if (MSG_IS_DELETED(msginfo->flags))
2201                 summaryview->deleted++;
2202
2203         summaryview->total_size += msginfo->size;
2204
2205         summary_set_row_marks(summaryview, node);
2206 }
2207
2208 static void summary_update_status(SummaryView *summaryview)
2209 {
2210         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2211         GtkCTreeNode *node;
2212         MsgInfo *msginfo;
2213
2214         summaryview->total_size =
2215         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
2216
2217         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2218              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2219                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2220
2221                 if (MSG_IS_DELETED(msginfo->flags))
2222                         summaryview->deleted++;
2223                 if (MSG_IS_MOVE(msginfo->flags))
2224                         summaryview->moved++;
2225                 if (MSG_IS_COPY(msginfo->flags))
2226                         summaryview->copied++;
2227                 summaryview->total_size += msginfo->size;
2228         }
2229 }
2230
2231 static void summary_status_show(SummaryView *summaryview)
2232 {
2233         gchar *str;
2234         gchar *del, *mv, *cp;
2235         gchar *sel;
2236         gchar *spc;
2237         gchar *itstr;
2238         GList *rowlist, *cur;
2239         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
2240         off_t sel_size = 0, n_size = 0;
2241         MsgInfo *msginfo;
2242         gchar *name;
2243         
2244         if (!summaryview->folder_item) {
2245                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
2246                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
2247                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
2248                 toolbar_main_set_sensitive(summaryview->mainwin);
2249                 return;
2250         }
2251
2252         rowlist = GTK_CLIST(summaryview->ctree)->selection;
2253         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2254                 msginfo = gtk_ctree_node_get_row_data
2255                         (GTK_CTREE(summaryview->ctree),
2256                          GTK_CTREE_NODE(cur->data));
2257                 if (msginfo) {
2258                         sel_size += msginfo->size;
2259                         n_selected++;
2260                 }
2261         }
2262         
2263         if (summaryview->folder_item->hide_read_msgs 
2264         || quicksearch_is_active(summaryview->quicksearch)) {
2265                 rowlist = GTK_CLIST(summaryview->ctree)->row_list;
2266                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2267                         msginfo = gtk_ctree_node_get_row_data
2268                                 (GTK_CTREE(summaryview->ctree),
2269                                  GTK_CTREE_NODE(cur));
2270                         if (msginfo) {
2271                                 n_size += msginfo->size;
2272                                 n_total++;
2273                                 if (MSG_IS_NEW(msginfo->flags))
2274                                         n_new++;
2275                                 if (MSG_IS_UNREAD(msginfo->flags))
2276                                         n_unread++;
2277                         }
2278                 }
2279         } else {
2280                 n_new = summaryview->folder_item->new_msgs;
2281                 n_unread = summaryview->folder_item->unread_msgs;
2282                 n_total = summaryview->folder_item->total_msgs;
2283                 n_size = summaryview->total_size;
2284         }
2285
2286         name = folder_item_get_name(summaryview->folder_item);
2287         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
2288         g_free(name);
2289
2290         if (summaryview->deleted)
2291                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
2292         else
2293                 del = g_strdup("");
2294         if (summaryview->moved)
2295                 mv = g_strdup_printf(_("%s%d moved"),
2296                                      summaryview->deleted ? _(", ") : "",
2297                                      summaryview->moved);
2298         else
2299                 mv = g_strdup("");
2300         if (summaryview->copied)
2301                 cp = g_strdup_printf(_("%s%d copied"),
2302                                      summaryview->deleted ||
2303                                      summaryview->moved ? _(", ") : "",
2304                                      summaryview->copied);
2305         else
2306                 cp = g_strdup("");
2307
2308         if (summaryview->deleted || summaryview->moved || summaryview->copied)
2309                 spc = "    ";
2310         else
2311                 spc = "";
2312
2313         if (n_selected) {
2314                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
2315                 if (n_selected == 1)
2316                         itstr = g_strdup(_(" item selected"));
2317                 else
2318                         itstr = g_strdup(_(" items selected"));
2319         } else {
2320                 sel = g_strdup("");
2321                 itstr = g_strdup("");
2322         }
2323                 
2324         str = g_strconcat(n_selected ? itos(n_selected) : "",
2325                                         itstr, sel, spc, del, mv, cp, NULL);
2326         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2327         g_free(str);
2328         g_free(sel);
2329         g_free(del);
2330         g_free(mv);
2331         g_free(cp);
2332         g_free(itstr);
2333
2334         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2335
2336                                       n_new, n_unread, n_total,
2337                                       to_human_readable(n_size));
2338         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2339         g_free(str);
2340         toolbar_main_set_sensitive(summaryview->mainwin);
2341 }
2342
2343 static void summary_set_column_titles(SummaryView *summaryview)
2344 {
2345         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2346         FolderItem *item = summaryview->folder_item;
2347         GtkWidget *hbox;
2348         GtkWidget *label;
2349         GtkWidget *arrow;
2350         gint pos;
2351         const gchar *title;
2352         SummaryColumnType type;
2353         GtkJustification justify;
2354
2355         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2356                 SORT_BY_MARK,
2357                 SORT_BY_STATUS,
2358                 SORT_BY_MIME,
2359                 SORT_BY_SUBJECT,
2360                 SORT_BY_FROM,
2361                 SORT_BY_TO,
2362                 SORT_BY_DATE,
2363                 SORT_BY_SIZE,
2364                 SORT_BY_NUMBER,
2365                 SORT_BY_SCORE,
2366                 SORT_BY_LOCKED
2367         };
2368
2369         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2370                 type = summaryview->col_state[pos].type;
2371
2372                 /* CLAWS: mime and unread are single char headers */
2373                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2374                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2375
2376                 switch (type) {
2377                 case S_COL_SUBJECT:
2378                 case S_COL_FROM:
2379                 case S_COL_TO:
2380                 case S_COL_DATE:
2381                 case S_COL_NUMBER:
2382                         if(type == S_COL_FROM && item != NULL &&
2383                                         FOLDER_SHOWS_TO_HDR(item) &&
2384                                         !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2385                                 type = S_COL_TO;
2386                         if (prefs_common.trans_hdr)
2387                                 title = gettext(col_label[type]);
2388                         else
2389                                 title = col_label[type];
2390                         break;
2391                 default:
2392                         title = gettext(col_label[type]);
2393                 }
2394
2395                 if (type == S_COL_MIME) {
2396                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2397                         gtk_widget_show(label);
2398                         gtk_clist_set_column_widget(clist, pos, label);
2399                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Attachment"));
2400                         continue;
2401                 } else if (type == S_COL_MARK) {
2402                         label = gtk_image_new_from_pixmap(markxpm, markxpmmask);
2403                         gtk_widget_show(label);
2404                         gtk_clist_set_column_widget(clist, pos, label);
2405                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Mark"));
2406                         continue;
2407                 } else if (type == S_COL_LOCKED) {
2408                         label = gtk_image_new_from_pixmap(lockedxpm, lockedxpmmask);
2409                         gtk_widget_show(label);
2410                         gtk_clist_set_column_widget(clist, pos, label);
2411                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Locked"));
2412                         continue;
2413                 } else if (type == S_COL_STATUS) {
2414                         gtk_clist_set_column_title(clist, pos, title);
2415                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Status"));
2416                         continue;
2417                 }
2418
2419                 hbox  = gtk_hbox_new(FALSE, 4);
2420                 label = gtk_label_new(title);
2421
2422                 if (justify == GTK_JUSTIFY_RIGHT)
2423                         gtk_box_pack_end(GTK_BOX(hbox), label,
2424                                          FALSE, FALSE, 0);
2425                 else
2426                         gtk_box_pack_start(GTK_BOX(hbox), label,
2427                                            FALSE, FALSE, 0);
2428
2429                 if (summaryview->sort_key == sort_by[type]) {
2430                         arrow = gtk_arrow_new
2431                                 (summaryview->sort_type == SORT_ASCENDING
2432                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2433                                  GTK_SHADOW_IN);
2434                         if (justify == GTK_JUSTIFY_RIGHT)
2435                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2436                                                    FALSE, FALSE, 0);
2437                         else
2438                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2439                                                  FALSE, FALSE, 0);
2440                 }
2441
2442                 gtk_widget_show_all(hbox);
2443                 gtk_clist_set_column_widget(clist, pos, hbox);
2444         }
2445 }
2446
2447 void summary_reflect_prefs(void)
2448 {
2449         static gchar *last_font = NULL;
2450         gboolean update_font = TRUE;
2451         SummaryView *summaryview = NULL;
2452
2453         if (!mainwindow_get_mainwindow())
2454                 return;
2455         summaryview = mainwindow_get_mainwindow()->summaryview;
2456
2457         if (last_font && !strcmp(last_font, NORMAL_FONT))
2458                 update_font = FALSE;
2459
2460         g_free(last_font);
2461         
2462         last_font = g_strdup(NORMAL_FONT);
2463
2464         if (update_font) {      
2465                 bold_style = bold_marked_style = bold_deleted_style = 
2466                         small_style = small_marked_style = small_deleted_style = NULL;
2467                 summary_set_fonts(summaryview);
2468         }
2469
2470         summary_set_column_titles(summaryview);
2471         summary_show(summaryview, summaryview->folder_item);
2472 }
2473
2474 void summary_sort(SummaryView *summaryview,
2475                   FolderSortKey sort_key, FolderSortType sort_type)
2476 {
2477         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2478         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2479         GtkCListCompareFunc cmp_func = NULL;
2480         START_TIMING("");
2481         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2482                                        G_CALLBACK(summary_tree_expanded), summaryview);
2483         summary_freeze(summaryview);
2484
2485         switch (sort_key) {
2486         case SORT_BY_MARK:
2487                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2488                 break;
2489         case SORT_BY_STATUS:
2490                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2491                 break;
2492         case SORT_BY_MIME:
2493                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2494                 break;
2495         case SORT_BY_NUMBER:
2496                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2497                 break;
2498         case SORT_BY_SIZE:
2499                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2500                 break;
2501         case SORT_BY_DATE:
2502                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2503                 break;
2504         case SORT_BY_FROM:
2505                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2506                 break;
2507         case SORT_BY_SUBJECT:
2508                 if (summaryview->simplify_subject_preg)
2509                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2510                 else
2511                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2512                 break;
2513         case SORT_BY_SCORE:
2514                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2515                 break;
2516         case SORT_BY_LABEL:
2517                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2518                 break;
2519         case SORT_BY_TO:
2520                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2521                 break;
2522         case SORT_BY_LOCKED:
2523                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2524                 break;
2525         case SORT_BY_NONE:
2526                 break;
2527         default:
2528                 goto unlock;
2529         }
2530
2531         summaryview->sort_key = sort_key;
2532         summaryview->sort_type = sort_type;
2533
2534         summary_set_column_titles(summaryview);
2535         summary_set_menu_sensitive(summaryview);
2536
2537         /* allow fallback to don't sort */
2538         if (summaryview->sort_key == SORT_BY_NONE)
2539                 goto unlock;
2540
2541         if (cmp_func != NULL) {
2542                 debug_print("Sorting summary...");
2543                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2544
2545                 main_window_cursor_wait(summaryview->mainwin);
2546
2547                 gtk_clist_set_compare_func(clist, cmp_func);
2548
2549                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2550                 gtk_sctree_sort_recursive(ctree, NULL);
2551
2552                 gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2553
2554                 main_window_cursor_normal(summaryview->mainwin);
2555
2556                 debug_print("done.\n");
2557                 STATUSBAR_POP(summaryview->mainwin);
2558         }
2559 unlock:
2560         summary_thaw(summaryview);
2561         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2562                                        G_CALLBACK(summary_tree_expanded), summaryview);
2563         END_TIMING();
2564 }
2565
2566 static gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2567                                    GtkCTreeNode *cnode, gpointer data)
2568 {
2569         SummaryView *summaryview = (SummaryView *)data;
2570         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2571         gchar *text[N_SUMMARY_COLS];
2572         gint *col_pos = summaryview->col_pos;
2573         const gchar *msgid = msginfo->msgid;
2574         GHashTable *msgid_table = summaryview->msgid_table;
2575         
2576         summary_set_header(summaryview, text, msginfo);
2577
2578         gtk_sctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2579                                 NULL, NULL, NULL, NULL, FALSE, summaryview->threaded && !summaryview->thread_collapsed);
2580 #define SET_TEXT(col) {                                         \
2581         gtk_ctree_node_set_text(ctree, cnode, col_pos[col],     \
2582                                 text[col_pos[col]]);            \
2583 }
2584
2585         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
2586                 SET_TEXT(S_COL_NUMBER);
2587         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
2588                 SET_TEXT(S_COL_SCORE);
2589         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
2590                 SET_TEXT(S_COL_SIZE);
2591         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible)
2592                 SET_TEXT(S_COL_DATE);
2593         if (summaryview->col_state[summaryview->col_pos[S_COL_FROM]].visible)
2594                 SET_TEXT(S_COL_FROM);
2595         if (summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2596                 SET_TEXT(S_COL_TO);
2597
2598 #undef SET_TEXT
2599
2600         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2601         summary_set_marks_func(ctree, cnode, summaryview);
2602
2603         if (msgid && msgid[0] != '\0')
2604                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2605
2606         return TRUE;
2607 }
2608
2609 static void summary_set_ctree_from_list(SummaryView *summaryview,
2610                                         GSList *mlist)
2611 {
2612         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2613         MsgInfo *msginfo;
2614         GtkCTreeNode *node = NULL;
2615         GHashTable *msgid_table;
2616         GHashTable *subject_table = NULL;
2617         GSList * cur;
2618         START_TIMING("");
2619         
2620         if (!mlist) return;
2621
2622         debug_print("\tSetting summary from message data...\n");
2623         STATUSBAR_PUSH(summaryview->mainwin,
2624                        _("Setting summary from message data..."));
2625         gdk_flush();
2626
2627         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2628                                        G_CALLBACK(summary_tree_expanded), summaryview);
2629
2630         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2631         summaryview->msgid_table = msgid_table;
2632
2633         if (prefs_common.thread_by_subject) {
2634                 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2635                 summaryview->subject_table = subject_table;
2636         } else {
2637                 summaryview->subject_table = NULL;
2638         }
2639
2640         if (prefs_common.use_addr_book)
2641                 start_address_completion(NULL);
2642         
2643         if (summaryview->threaded) {
2644                 GNode *root, *gnode;
2645                 START_TIMING("threaded");
2646                 root = procmsg_get_thread_tree(mlist);
2647
2648                 for (gnode = root->children; gnode != NULL;
2649                      gnode = gnode->next) {
2650                         node = gtk_sctree_insert_gnode
2651                                 (ctree, NULL, node, gnode,
2652                                  summary_insert_gnode_func, summaryview);
2653                 }
2654
2655                 g_node_destroy(root);
2656                 
2657                 END_TIMING();
2658         } else {
2659                 gchar *text[N_SUMMARY_COLS];
2660                 START_TIMING("unthreaded");
2661                 cur = mlist;
2662                 for (; mlist != NULL; mlist = mlist->next) {
2663                         msginfo = (MsgInfo *)mlist->data;
2664
2665                         summary_set_header(summaryview, text, msginfo);
2666
2667                         node = gtk_sctree_insert_node
2668                                 (ctree, NULL, node, text, 2,
2669                                  NULL, NULL, NULL, NULL,
2670                                  FALSE, FALSE);
2671
2672                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2673                         summary_set_marks_func(ctree, node, summaryview);
2674
2675                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2676                                 g_hash_table_insert(msgid_table,
2677                                                     msginfo->msgid, node);
2678
2679                         if (prefs_common.thread_by_subject)
2680                                 subject_table_insert(subject_table,
2681                                              msginfo->subject,
2682                                              node);
2683                 }
2684                 mlist = cur;
2685                 END_TIMING();
2686         }
2687
2688         if (prefs_common.enable_hscrollbar &&
2689             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2690                 gint optimal_width;
2691
2692                 optimal_width = gtk_clist_optimal_column_width
2693                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2694                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2695                                            summaryview->col_pos[S_COL_SUBJECT],
2696                                            optimal_width);
2697         }
2698
2699         if (prefs_common.use_addr_book)
2700                 end_address_completion();
2701
2702         debug_print("done.\n");
2703         STATUSBAR_POP(summaryview->mainwin);
2704         if (debug_get_mode()) {
2705                 debug_print("\tmsgid hash table size = %d\n",
2706                             g_hash_table_size(msgid_table));
2707                 if (prefs_common.thread_by_subject)
2708                         debug_print("\tsubject hash table size = %d\n",
2709                             g_hash_table_size(subject_table));
2710         }
2711
2712         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2713
2714         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2715
2716         if (prefs_common.bold_unread) {
2717                 START_TIMING("bold_unread");
2718                 while (node) {
2719                         GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2720                         if (GTK_CTREE_ROW(node)->children)
2721                                 summary_set_row_marks(summaryview, node);
2722                         node = next;
2723                 }
2724                 END_TIMING();
2725         }
2726
2727         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2728                                        G_CALLBACK(summary_tree_expanded), summaryview);
2729         END_TIMING();
2730 }
2731
2732 static gchar *summary_complete_address(const gchar *addr)
2733 {
2734         gint count;
2735         gchar *res, *tmp, *email_addr;
2736         
2737         if (addr == NULL)
2738                 return NULL;
2739
2740         Xstrdup_a(email_addr, addr, return NULL);
2741         extract_address(email_addr);
2742         if (!*email_addr)
2743                 return NULL;
2744
2745         /*
2746          * completion stuff must be already initialized
2747          */
2748         res = NULL;
2749         if (1 < (count = complete_address(email_addr))) {
2750                 tmp = get_complete_address(1);
2751                 res = procheader_get_fromname(tmp);
2752                 g_free(tmp);
2753         }
2754
2755         return res;
2756 }
2757
2758 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2759                                MsgInfo *msginfo)
2760 {
2761         static gchar date_modified[80];
2762         static gchar col_score[11];
2763         static gchar buf[BUFFSIZE];
2764         gint *col_pos = summaryview->col_pos;
2765         gchar *from_text = NULL, *to_text = NULL;
2766         gboolean should_swap = FALSE;
2767
2768         text[col_pos[S_COL_FROM]]   = "";
2769         text[col_pos[S_COL_TO]]     = "";
2770         text[col_pos[S_COL_SUBJECT]]= "";
2771         text[col_pos[S_COL_MARK]]   = "";
2772         text[col_pos[S_COL_STATUS]] = "";
2773         text[col_pos[S_COL_MIME]]   = "";
2774         text[col_pos[S_COL_LOCKED]] = "";
2775         text[col_pos[S_COL_DATE]]   = "";
2776         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
2777                 text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2778         else
2779                 text[col_pos[S_COL_NUMBER]] = "";
2780
2781         /* slow! */
2782         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
2783                 text[col_pos[S_COL_SIZE]] = to_human_readable(msginfo->size);
2784         else
2785                 text[col_pos[S_COL_SIZE]] = "";
2786
2787         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
2788                 text[col_pos[S_COL_SCORE]] = itos_buf(col_score, msginfo->score);
2789         else
2790                 text[col_pos[S_COL_SCORE]] = "";
2791
2792         /* slow! */
2793         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible) {
2794                 if (msginfo->date_t) {
2795                         procheader_date_get_localtime(date_modified,
2796                                                       sizeof(date_modified),
2797                                                       msginfo->date_t);
2798                         text[col_pos[S_COL_DATE]] = date_modified;
2799                 } else if (msginfo->date)
2800                         text[col_pos[S_COL_DATE]] = msginfo->date;
2801                 else
2802                         text[col_pos[S_COL_DATE]] = _("(No Date)");
2803         }
2804         
2805         if (prefs_common.swap_from && msginfo->from && msginfo->to
2806         &&  !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible) {
2807                 gchar *addr = NULL;
2808                 
2809                 addr = g_strdup(msginfo->from);
2810
2811                 if (addr) {
2812                         extract_address(addr);
2813                         if (account_find_from_address(addr)) {
2814                                 should_swap = TRUE;
2815                         }
2816                         g_free(addr);
2817                 }
2818         }
2819
2820         if (!prefs_common.use_addr_book) {
2821                 from_text = msginfo->fromname ? 
2822                                 msginfo->fromname :
2823                                 _("(No From)");
2824         } else {
2825                 gchar buf[BUFFSIZE];
2826                 gchar *tmp = summary_complete_address(msginfo->from);
2827                 if (tmp) {
2828                         strncpy2(buf, tmp, sizeof(buf));
2829                         g_free(tmp);
2830                         from_text = buf;
2831                 } else {
2832                         from_text = (msginfo->fromname) ?
2833                                         msginfo->fromname: 
2834                                         _("(No From)");
2835                 }
2836         }
2837         
2838         to_text = msginfo->to ? msginfo->to : 
2839                    (msginfo->cc ? msginfo->cc :
2840                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2841                      )
2842                    );
2843
2844         text[col_pos[S_COL_TO]] = to_text;
2845         if (!should_swap) {
2846                 text[col_pos[S_COL_FROM]] = from_text;
2847         } else {
2848                 gchar tmp[BUFFSIZE];
2849                 snprintf(tmp, BUFFSIZE-1, "--> %s", to_text);
2850                 tmp[BUFFSIZE-1]='\0';
2851                 text[col_pos[S_COL_FROM]] = tmp;
2852         }
2853         
2854         if (summaryview->simplify_subject_preg != NULL)
2855                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2856                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2857                                         summaryview->simplify_subject_preg) : 
2858                         _("(No Subject)");
2859         else 
2860                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2861                         _("(No Subject)");
2862 }
2863
2864 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2865 {
2866         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2867 }
2868
2869 static gboolean defer_change(gpointer data);
2870 typedef struct _ChangeData {
2871         MsgInfo *info;
2872         gint op; /* 0, 1, 2 for unset, set, change */
2873         MsgPermFlags set_flags;
2874         MsgTmpFlags  set_tmp_flags;
2875         MsgPermFlags unset_flags;
2876         MsgTmpFlags  unset_tmp_flags;
2877 } ChangeData;
2878
2879 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2880 {
2881         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2882                 debug_print("flags: doing unset now\n");
2883                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
2884         } else {
2885                 ChangeData *unset_data = g_new0(ChangeData, 1);
2886                 unset_data->info = msginfo;
2887                 unset_data->op = 0;
2888                 unset_data->unset_flags = flags;
2889                 unset_data->unset_tmp_flags = tmp_flags;
2890                 debug_print("flags: deferring unset\n");
2891                 g_timeout_add(100, defer_change, unset_data);
2892         }
2893 }
2894
2895 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2896 {
2897         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2898                 debug_print("flags: doing set now\n");
2899                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
2900         } else {
2901                 ChangeData *set_data = g_new0(ChangeData, 1);
2902                 set_data->info = msginfo;
2903                 set_data->op = 1;
2904                 set_data->set_flags = flags;
2905                 set_data->set_tmp_flags = tmp_flags;
2906                 debug_print("flags: deferring set\n");
2907                 g_timeout_add(100, defer_change, set_data);
2908         }
2909 }
2910
2911 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
2912                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
2913                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
2914 {
2915         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2916                 debug_print("flags: doing change now\n");
2917                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
2918                         rem_flags, rem_tmp_flags);
2919         } else {
2920                 ChangeData *change_data = g_new0(ChangeData, 1);
2921                 change_data->info = msginfo;
2922                 change_data->op = 2;
2923                 change_data->set_flags = add_flags;
2924                 change_data->set_tmp_flags = add_tmp_flags;
2925                 change_data->unset_flags = rem_flags;
2926                 change_data->unset_tmp_flags = rem_tmp_flags;
2927                 debug_print("flags: deferring change\n");
2928                 g_timeout_add(100, defer_change, change_data);
2929         }
2930 }
2931
2932 gboolean defer_change(gpointer data)
2933 {
2934         ChangeData *chg = (ChangeData *)data;
2935         if (chg->info->folder && chg->info->folder->processing_pending) {
2936                 debug_print("flags: trying later\n");
2937                 return TRUE; /* try again */
2938         } else {
2939                 debug_print("flags: finally doing it\n");
2940                 switch(chg->op) {
2941                 case 0:
2942                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
2943                         break;
2944                 case 1:
2945                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
2946                         break;
2947                 case 2:
2948                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
2949                                 chg->unset_flags, chg->unset_tmp_flags);
2950                         break;
2951                 default:
2952                         g_warning("shouldn't happen\n");
2953                 }
2954                 g_free(chg);
2955         }
2956         return FALSE;
2957 }
2958
2959 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2960                                       GtkCTreeNode *row)
2961 {
2962         g_return_if_fail(summaryview != NULL);
2963         g_return_if_fail(msginfo != NULL);
2964         g_return_if_fail(row != NULL);
2965
2966         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2967                 summary_msginfo_unset_flags
2968                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2969                 summary_set_row_marks(summaryview, row);
2970                 summary_status_show(summaryview);
2971         }
2972 }
2973
2974 typedef struct  {
2975         MsgInfo *msginfo;
2976         SummaryView *summaryview;
2977 } MarkAsReadData;
2978
2979 static int msginfo_mark_as_read_timeout(void *data)
2980 {
2981         MarkAsReadData *mdata = (MarkAsReadData *)data;
2982         if (!mdata)
2983                 return FALSE;
2984         
2985         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2986                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2987                                      mdata->summaryview->selected); 
2988
2989         g_free(mdata);
2990
2991         return FALSE;   
2992 }
2993
2994 static void summary_display_msg_full(SummaryView *summaryview,
2995                                      GtkCTreeNode *row,
2996                                      gboolean new_window, gboolean all_headers)
2997 {
2998         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2999         MsgInfo *msginfo;
3000         gint val;
3001         START_TIMING("");
3002         if (!new_window) {
3003                 if (summaryview->displayed == row)
3004                         return;
3005                 else
3006                         summaryview->messageview->filtered = FALSE;
3007         }                       
3008         g_return_if_fail(row != NULL);
3009
3010         if (summary_is_locked(summaryview)) return;
3011         summary_lock(summaryview);
3012
3013         STATUSBAR_POP(summaryview->mainwin);
3014         GTK_EVENTS_FLUSH();
3015
3016         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3017         
3018         g_return_if_fail(msginfo);
3019
3020         if (new_window) {
3021                 MessageView *msgview;
3022
3023                 msgview = messageview_create_with_new_window(summaryview->mainwin);
3024                 val = messageview_show(msgview, msginfo, all_headers);
3025         } else {
3026                 MessageView *msgview;
3027
3028                 msgview = summaryview->messageview;
3029                 summaryview->last_displayed = summaryview->displayed;
3030                 summaryview->displayed = row;
3031                 if (!messageview_is_visible(msgview)) {
3032                         main_window_toggle_message_view(summaryview->mainwin);
3033                         GTK_EVENTS_FLUSH();
3034                 }
3035                 val = messageview_show(msgview, msginfo, all_headers);
3036                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
3037                         gtk_widget_grab_focus(summaryview->ctree);
3038                 gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3039                         GTK_CLIST(summaryview->ctree)->focus_row);
3040         }
3041
3042         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
3043                 if (prefs_common.mark_as_read_delay) {
3044                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
3045                         data->summaryview = summaryview;
3046                         data->msginfo = msginfo;
3047                         g_timeout_add(prefs_common.mark_as_read_delay * 1000,
3048                                 msginfo_mark_as_read_timeout, data);
3049                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
3050                         msginfo_mark_as_read(summaryview, msginfo, row);
3051                 }
3052         }
3053
3054         summary_set_menu_sensitive(summaryview);
3055         toolbar_main_set_sensitive(summaryview->mainwin);
3056         messageview_set_menu_sensitive(summaryview->messageview);
3057
3058         summary_unlock(summaryview);
3059         END_TIMING();
3060 }
3061
3062 void summary_display_msg_selected(SummaryView *summaryview,
3063                                   gboolean all_headers)
3064 {
3065         if (summary_is_locked(summaryview)) return;
3066         summaryview->displayed = NULL;
3067         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
3068                                  all_headers);
3069 }
3070
3071 void summary_redisplay_msg(SummaryView *summaryview)
3072 {
3073         GtkCTreeNode *node;
3074
3075         if (summaryview->displayed) {
3076                 node = summaryview->displayed;
3077                 summaryview->displayed = NULL;
3078                 summary_display_msg(summaryview, node);
3079         }
3080 }
3081
3082 void summary_open_msg(SummaryView *summaryview)
3083 {
3084         if (!summaryview->selected) return;
3085         
3086         /* CLAWS: if separate message view, don't open a new window
3087          * but rather use the current separated message view */
3088         summary_display_msg_full(summaryview, summaryview->selected, 
3089                                  TRUE, FALSE);
3090 }
3091
3092 void summary_view_source(SummaryView * summaryview)
3093 {
3094         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3095         MsgInfo *msginfo;
3096         SourceWindow *srcwin;
3097
3098         if (!summaryview->selected) return;
3099
3100         srcwin = source_window_create();
3101         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3102         source_window_show_msg(srcwin, msginfo);
3103         source_window_show(srcwin);
3104 }
3105
3106 void summary_reedit(SummaryView *summaryview)
3107 {
3108         MsgInfo *msginfo;
3109
3110         if (!summaryview->selected) return;
3111         if (!summaryview->folder_item) return;
3112         if (!FOLDER_SHOWS_TO_HDR(summaryview->folder_item))
3113                 return;
3114
3115         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3116                                               summaryview->selected);
3117         if (!msginfo) return;
3118
3119         compose_reedit(msginfo, FALSE);
3120 }
3121
3122 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
3123 {
3124         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3125         GtkCTreeNode *node;
3126
3127         if (summary_is_locked(summaryview)
3128         && !GTK_SCTREE(ctree)->selecting_range
3129         && summaryview->messageview->mimeview
3130         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
3131         && summaryview->messageview->mimeview->textview->loading) {
3132                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
3133                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
3134                 
3135                 data->ctree = ctree;
3136                 data->row = NULL;
3137                 data->node = NULL;
3138                 data->type = type;
3139                 data->summaryview = summaryview;
3140                 debug_print("postponing open of message till end of load\n");
3141                 g_timeout_add(100, summary_select_retry, data);
3142                 return FALSE;
3143         }
3144         if (summary_is_locked(summaryview))
3145                 return FALSE;
3146         if (type == GTK_SCROLL_STEP_FORWARD) {
3147                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
3148                 if (node)
3149                         gtkut_ctree_expand_parent_all(ctree, node);
3150                 else
3151                         return FALSE;
3152         } else {
3153                 if (summaryview->selected) {
3154                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
3155                         if (!node) return FALSE;
3156                 }
3157         }
3158
3159         if (messageview_is_visible(summaryview->messageview))
3160                 summaryview->display_msg = TRUE;
3161
3162         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
3163
3164         if (GTK_CLIST(ctree)->selection)
3165                 gtk_sctree_set_anchor_row
3166                         (GTK_SCTREE(ctree),
3167                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
3168
3169         return TRUE;
3170 }
3171
3172 void summary_toggle_view(SummaryView *summaryview)
3173 {
3174         if (!messageview_is_visible(summaryview->messageview) &&
3175             summaryview->selected)
3176                 summary_display_msg(summaryview,
3177                                     summaryview->selected);
3178         else
3179                 main_window_toggle_message_view(summaryview->mainwin);
3180 }
3181
3182 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
3183                                                 GtkCTreeNode *node)
3184 {
3185         MsgInfo *msginfo;
3186
3187         if (node) {
3188                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
3189                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3190                         return TRUE;
3191                 node = GTK_CTREE_ROW(node)->children;
3192         } else
3193                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3194
3195         while (node) {
3196                 if (summary_search_unread_recursive(ctree, node) == TRUE)
3197                         return TRUE;
3198                 node = GTK_CTREE_ROW(node)->sibling;
3199         }
3200
3201         return FALSE;
3202 }
3203
3204 static gboolean summary_have_unread_children(SummaryView *summaryview,
3205                                              GtkCTreeNode *node)
3206 {
3207         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3208
3209         if (!node) return FALSE;
3210
3211         node = GTK_CTREE_ROW(node)->children;
3212
3213         while (node) {
3214                 if (summary_search_unread_recursive(ctree, node) == TRUE)
3215                         return TRUE;
3216                 node = GTK_CTREE_ROW(node)->sibling;
3217         }
3218         return FALSE;
3219 }
3220
3221 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
3222 {
3223         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3224         GtkStyle *style = NULL;
3225         MsgInfo *msginfo;
3226         MsgFlags flags;
3227         gint *col_pos = summaryview->col_pos;
3228
3229         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3230         if (!msginfo) return;
3231
3232         flags = msginfo->flags;
3233
3234         gtk_ctree_node_set_foreground(ctree, row, NULL);
3235
3236         /* set new/unread column */
3237         if (MSG_IS_IGNORE_THREAD(flags)) {
3238                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
3239                                           ignorethreadxpm, ignorethreadxpmmask);
3240         } else if (MSG_IS_SPAM(flags)) {
3241                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
3242                                           spamxpm, spamxpmmask);
3243         } else if (MSG_IS_NEW(flags)) {
3244                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
3245                                           newxpm, newxpmmask);
3246         } else if (MSG_IS_UNREAD(flags)) {
3247                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
3248                                           unreadxpm, unreadxpmmask);
3249         } else if (MSG_IS_REPLIED(flags)) {
3250                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
3251                                           repliedxpm, repliedxpmmask);
3252         } else if (MSG_IS_FORWARDED(flags)) {
3253                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
3254                                           forwardedxpm, forwardedxpmmask);
3255         } else {
3256                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
3257                                         "");
3258         }
3259
3260         if (prefs_common.bold_unread &&
3261             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
3262              (!GTK_CTREE_ROW(row)->expanded &&
3263               GTK_CTREE_ROW(row)->children &&
3264               summary_have_unread_children(summaryview, row))))
3265                 style = bold_style;
3266
3267         /* set mark column */
3268         if (MSG_IS_DELETED(flags)) {
3269                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
3270                                           deletedxpm, deletedxpmmask);
3271                 if (style)
3272                         style = bold_deleted_style;
3273                 else {
3274                         style = small_deleted_style;
3275                 }
3276                         gtk_ctree_node_set_foreground
3277                                 (ctree, row, &summaryview->color_dim);
3278         } else if (MSG_IS_MARKED(flags)) {
3279                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
3280                                           markxpm, markxpmmask);
3281         } else if (MSG_IS_MOVE(flags)) {
3282                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
3283                                           movedxpm, movedxpmmask);
3284                 if (style)
3285                         style = bold_marked_style;
3286                 else {
3287                         style = small_marked_style;
3288                 }
3289                         gtk_ctree_node_set_foreground
3290                                 (ctree, row, &summaryview->color_marked);
3291         } else if (MSG_IS_COPY(flags)) {
3292                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
3293                                           copiedxpm, copiedxpmmask);
3294                 if (style)
3295                         style = bold_marked_style;
3296                 else {
3297                         style = small_marked_style;
3298                 }
3299                         gtk_ctree_node_set_foreground
3300                                 (ctree, row, &summaryview->color_marked);
3301         } else {
3302                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "");
3303         }
3304
3305         if (MSG_IS_LOCKED(flags)) {
3306                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
3307                                           lockedxpm, lockedxpmmask);
3308         }
3309         else {
3310                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], "");
3311         }
3312
3313         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
3314                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
3315                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
3316         } else if (MSG_IS_SIGNED(flags)) {
3317                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
3318                                           gpgsignedxpm, gpgsignedxpmmask);
3319         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
3320                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
3321                                           clipkeyxpm, clipkeyxpmmask);
3322         } else if (MSG_IS_ENCRYPTED(flags)) {
3323                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
3324                                           keyxpm, keyxpmmask);
3325         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
3326                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
3327                                           clipxpm, clipxpmmask);
3328         } else {
3329                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], "");
3330         }
3331         if (!style)
3332                 style = small_style;
3333
3334         gtk_ctree_node_set_row_style(ctree, row, style);
3335
3336         if (MSG_GET_COLORLABEL(flags))
3337                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
3338 }
3339
3340 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
3341 {
3342         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3343         MsgInfo *msginfo;
3344
3345         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3346         g_return_if_fail(msginfo);
3347         if (MSG_IS_DELETED(msginfo->flags))
3348                 summaryview->deleted--;
3349         if (MSG_IS_MOVE(msginfo->flags))
3350                 summaryview->moved--;
3351         if (MSG_IS_COPY(msginfo->flags))
3352                 summaryview->copied--;
3353
3354         procmsg_msginfo_set_to_folder(msginfo, NULL);
3355         summary_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, 
3356                 MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
3357         summary_set_row_marks(summaryview, row);
3358         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
3359 }
3360
3361 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
3362 {
3363         gboolean changed = FALSE;
3364         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3365         MsgInfo *msginfo;
3366
3367         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3368         g_return_if_fail(msginfo);
3369         if (MSG_IS_DELETED(msginfo->flags))
3370                 summaryview->deleted--;
3371         if (MSG_IS_MOVE(msginfo->flags)) {
3372                 summaryview->moved--;
3373                 changed = TRUE;
3374         }
3375         if (MSG_IS_COPY(msginfo->flags)) {
3376                 summaryview->copied--;
3377                 changed = TRUE;
3378         }
3379         procmsg_msginfo_set_to_folder(msginfo, NULL);
3380         summary_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, 
3381                 MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
3382         
3383         summary_set_row_marks(summaryview, row);
3384         debug_print("Message %d is locked\n", msginfo->msgnum);
3385 }
3386
3387 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
3388 {
3389         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3390         MsgInfo *msginfo;
3391
3392         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3393         g_return_if_fail(msginfo);
3394         if (!MSG_IS_LOCKED(msginfo->flags))
3395                 return;
3396         procmsg_msginfo_set_to_folder(msginfo, NULL);
3397         summary_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
3398         summary_set_row_marks(summaryview, row);
3399         debug_print("Message %d is unlocked\n", msginfo->msgnum);
3400 }
3401
3402 void summary_mark(SummaryView *summaryview)
3403 {
3404         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3405         GList *cur;
3406         gboolean froze = FALSE;
3407
3408         START_LONG_OPERATION(summaryview, FALSE);
3409         folder_item_set_batch(summaryview->folder_item, TRUE);
3410         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3411                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
3412         folder_item_set_batch(summaryview->folder_item, FALSE);
3413         END_LONG_OPERATION(summaryview);
3414
3415         summary_status_show(summaryview);
3416 }
3417
3418 static void summary_mark_row_as_read(SummaryView *summaryview,
3419                                      GtkCTreeNode *row)
3420 {
3421         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3422         MsgInfo *msginfo;
3423
3424         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3425         g_return_if_fail(msginfo);
3426
3427         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
3428                 return;
3429
3430         summary_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
3431         summary_set_row_marks(summaryview, row);
3432         debug_print("Message %d is marked as read\n",
3433                 msginfo->msgnum);
3434 }
3435
3436 void summary_mark_as_read(SummaryView *summaryview)
3437 {
3438         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3439         GList *cur;
3440         gboolean froze = FALSE;
3441
3442         START_LONG_OPERATION(summaryview, FALSE);
3443         folder_item_set_batch(summaryview->folder_item, TRUE);
3444         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3445                 summary_mark_row_as_read(summaryview,
3446                                          GTK_CTREE_NODE(cur->data));
3447         folder_item_set_batch(summaryview->folder_item, FALSE);
3448         END_LONG_OPERATION(summaryview);
3449         
3450         summary_status_show(summaryview);
3451 }
3452
3453 void summary_msgs_lock(SummaryView *summaryview)
3454 {
3455         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3456         GList *cur;
3457         gboolean froze = FALSE;
3458
3459         START_LONG_OPERATION(summaryview, FALSE);
3460         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3461                 summary_lock_row(summaryview,
3462                                          GTK_CTREE_NODE(cur->data));
3463         END_LONG_OPERATION(summaryview);
3464         
3465         summary_status_show(summaryview);
3466 }
3467
3468 void summary_msgs_unlock(SummaryView *summaryview)
3469 {
3470         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3471         GList *cur;
3472         gboolean froze = FALSE;
3473
3474         START_LONG_OPERATION(summaryview, FALSE);
3475         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3476                 summary_unlock_row(summaryview,
3477                                    GTK_CTREE_NODE(cur->data));
3478         END_LONG_OPERATION(summaryview);
3479         
3480         summary_status_show(summaryview);
3481 }
3482
3483 void summary_mark_all_read(SummaryView *summaryview)
3484 {
3485         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3486         GtkCTreeNode *node;
3487         AlertValue val;
3488         gboolean froze = FALSE;
3489
3490         if (prefs_common.ask_mark_all_read) {
3491                 val = alertpanel_full(_("Mark all as read"),
3492                         _("Do you really want to mark all mails in this "
3493                           "folder as read ?"), GTK_STOCK_NO, GTK_STOCK_YES, NULL,
3494                           TRUE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
3495
3496                 if ((val & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
3497                         return;
3498                 else if (val & G_ALERTDISABLE)
3499                         prefs_common.ask_mark_all_read = FALSE;
3500         }
3501         
3502         START_LONG_OPERATION(summaryview, TRUE);
3503         folder_item_set_batch(summaryview->folder_item, TRUE);
3504         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3505              node = gtkut_ctree_node_next(ctree, node))
3506                 summary_mark_row_as_read(summaryview, node);
3507         folder_item_set_batch(summaryview->folder_item, FALSE);
3508         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3509              node = gtkut_ctree_node_next(ctree, node)) {
3510                 if (!GTK_CTREE_ROW(node)->expanded)
3511                         summary_set_row_marks(summaryview, node);
3512         }
3513         END_LONG_OPERATION(summaryview);
3514         
3515         summary_status_show(summaryview);
3516 }
3517
3518 void summary_mark_as_spam(SummaryView *summaryview, guint action, GtkWidget *widget)
3519 {
3520         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3521         GList *cur;
3522         gboolean is_spam = action;
3523         GSList *msgs = NULL;
3524         gboolean immediate_exec = prefs_common.immediate_exec;
3525         gboolean moved = FALSE;
3526         gboolean froze = FALSE;
3527
3528         prefs_common.immediate_exec = FALSE;
3529
3530         START_LONG_OPERATION(summaryview, FALSE);
3531
3532         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3533                 GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3534                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, row);
3535                 if (msginfo)
3536                         msgs = g_slist_prepend(msgs, msginfo);
3537         }
3538         
3539         if (procmsg_spam_learner_learn(NULL, msgs, is_spam) == 0) {
3540                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3541                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3542                         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, row);
3543                         if (!msginfo)
3544                                 continue;
3545                         if (is_spam) {
3546                                 summary_msginfo_change_flags(msginfo, MSG_SPAM, 0, MSG_NEW|MSG_UNREAD, 0);
3547                                 if (procmsg_spam_get_folder(msginfo) != summaryview->folder_item) {
3548                                         summary_move_row_to(summaryview, row,
3549                                                         procmsg_spam_get_folder(msginfo));
3550                                         moved = TRUE;
3551                                 }
3552                         } else {
3553                                 summary_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
3554                         }
3555                         summaryview->display_msg = prefs_common.always_show_msg;
3556         
3557                         summary_set_row_marks(summaryview, row);
3558                 }
3559         } else {
3560                 log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
3561         }
3562
3563         prefs_common.immediate_exec = immediate_exec;
3564
3565         END_LONG_OPERATION(summaryview);
3566
3567         if (prefs_common.immediate_exec && moved) {
3568                 summary_execute(summaryview);
3569         }
3570
3571         if (!moved && msgs) {
3572                 MsgInfo *msginfo = (MsgInfo *)msgs->data;
3573                 toolbar_set_learn_button
3574                         (summaryview->mainwin->toolbar,
3575                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
3576         }
3577         g_slist_free(msgs);
3578         
3579         summary_status_show(summaryview);       
3580 }
3581
3582
3583 static void summary_mark_row_as_unread(SummaryView *summaryview,
3584                                        GtkCTreeNode *row)
3585 {
3586         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3587         MsgInfo *msginfo;
3588
3589         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3590         g_return_if_fail(msginfo);
3591         if (MSG_IS_DELETED(msginfo->flags)) {
3592                 procmsg_msginfo_set_to_folder(msginfo, NULL);
3593                 summary_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
3594                 summaryview->deleted--;
3595         }
3596
3597         summary_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
3598         debug_print("Message %d is marked as unread\n",
3599                 msginfo->msgnum);
3600
3601         summary_set_row_marks(summaryview, row);
3602 }
3603
3604 void summary_mark_as_unread(SummaryView *summaryview)
3605 {
3606         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3607         GList *cur;
3608         gboolean froze = FALSE;
3609
3610         START_LONG_OPERATION(summaryview, FALSE);
3611         folder_item_set_batch(summaryview->folder_item, TRUE);
3612         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3613                 cur = cur->next)
3614                 summary_mark_row_as_unread(summaryview,
3615                                            GTK_CTREE_NODE(cur->data));
3616         folder_item_set_batch(summaryview->folder_item, FALSE);
3617         END_LONG_OPERATION(summaryview);
3618         
3619         summary_status_show(summaryview);
3620 }
3621
3622 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
3623 {
3624         GList * cur;
3625         gboolean found;
3626
3627         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
3628
3629         case F_NEWS:
3630
3631                 /*
3632                   security : checks if one the accounts correspond to
3633                   the author of the post
3634                 */
3635
3636                 found = FALSE;
3637                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3638                         PrefsAccount * account;
3639                         gchar * from_name;
3640                         
3641                         account = cur->data;
3642                         if (account->name && *account->name)
3643                                 from_name =
3644                                         g_strdup_printf("%s <%s>",
3645                                                         account->name,
3646                                                         account->address);
3647                         else
3648                                 from_name =
3649                                         g_strdup_printf("%s",
3650                                                         account->address);
3651                         
3652                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3653                                 g_free(from_name);
3654                                 found = TRUE;
3655                                 break;
3656                         }
3657                         g_free(from_name);
3658                 }
3659
3660                 if (!found) {
3661                         alertpanel_error(_("You're not the author of the article.\n"));
3662                 }
3663                 
3664                 return found;
3665
3666         default:
3667                 return TRUE;
3668         }
3669 }
3670
3671 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3672 {
3673         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3674         MsgInfo *msginfo;
3675
3676         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3677         g_return_if_fail(msginfo);
3678
3679         if (MSG_IS_LOCKED(msginfo->flags)) return;
3680
3681         if (MSG_IS_DELETED(msginfo->flags)) return;
3682
3683         if (MSG_IS_MOVE(msginfo->flags))
3684                 summaryview->moved--;
3685         if (MSG_IS_COPY(msginfo->flags))
3686                 summaryview->copied--;
3687
3688         procmsg_msginfo_set_to_folder(msginfo, NULL);
3689         summary_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, 
3690                 MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
3691         summaryview->deleted++;
3692
3693         if (!prefs_common.immediate_exec && 
3694             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3695                 summary_set_row_marks(summaryview, row);
3696
3697         debug_print("Message %s/%d is set to delete\n",
3698                     msginfo->folder->path, msginfo->msgnum);
3699 }
3700
3701 void summary_cancel(SummaryView *summaryview)
3702 {
3703         MsgInfo * msginfo;
3704
3705         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3706                                               summaryview->selected);
3707         if (!msginfo) return;
3708
3709         if (!check_permission(summaryview, msginfo))
3710                 return;
3711
3712         news_cancel_article(summaryview->folder_item->folder, msginfo);
3713         
3714         if (summary_is_locked(summaryview)) return;
3715
3716         summary_lock(summaryview);
3717
3718         summary_freeze(summaryview);
3719
3720         summary_update_status(summaryview);
3721         summary_status_show(summaryview);
3722
3723         summary_thaw(summaryview);
3724
3725         summary_unlock(summaryview);
3726 }
3727
3728 void summary_delete(SummaryView *summaryview)
3729 {