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