sync with sylpheed 0.7.0cvs38
[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, "/Delete", TRUE);
1041         }
1042
1043         menu_set_sensitive(ifactory, "/Copy...", TRUE);
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_display_msg_selected(SummaryView *summaryview,
2382                                   gboolean all_headers)
2383 {
2384         summaryview->displayed = NULL;
2385         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2386                                  all_headers);
2387 }
2388
2389 void summary_redisplay_msg(SummaryView *summaryview)
2390 {
2391         GtkCTreeNode *node;
2392
2393         if (summaryview->displayed) {
2394                 node = summaryview->displayed;
2395                 summaryview->displayed = NULL;
2396                 summary_display_msg(summaryview, node);
2397         }
2398 }
2399
2400 void summary_open_msg(SummaryView *summaryview)
2401 {
2402         if (!summaryview->selected) return;
2403
2404         summary_display_msg_full(summaryview, summaryview->selected,
2405                                  TRUE, FALSE);
2406 }
2407
2408 void summary_view_source(SummaryView * summaryview)
2409 {
2410         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2411         MsgInfo *msginfo;
2412         SourceWindow *srcwin;
2413
2414         if (!summaryview->selected) return;
2415
2416         srcwin = source_window_create();
2417         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2418         source_window_show_msg(srcwin, msginfo);
2419         source_window_show(srcwin);
2420 }
2421
2422 void summary_reedit(SummaryView *summaryview)
2423 {
2424         MsgInfo *msginfo;
2425
2426         if (!summaryview->selected) return;
2427         if (!summaryview->folder_item) return;
2428         if (summaryview->folder_item->stype != F_OUTBOX &&
2429             summaryview->folder_item->stype != F_DRAFT  &&
2430             summaryview->folder_item->stype != F_QUEUE) return;
2431
2432         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2433                                               summaryview->selected);
2434         if (!msginfo) return;
2435
2436         compose_reedit(msginfo);
2437 }
2438
2439 void summary_step(SummaryView *summaryview, GtkScrollType type)
2440 {
2441         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2442
2443         if (type == GTK_SCROLL_STEP_FORWARD) {
2444                 GtkCTreeNode *node;
2445                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2446                 if (node)
2447                         gtkut_ctree_expand_parent_all(ctree, node);
2448         }
2449
2450         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2451                                 type, 0.0);
2452
2453         if (summaryview->msg_is_toggled_on)
2454                 summary_display_msg(summaryview, summaryview->selected);
2455 }
2456
2457 static void summary_toggle_view(SummaryView *summaryview)
2458 {
2459         MainWindow *mainwin = summaryview->mainwin;
2460         GtkItemFactory *ifactory;
2461         
2462         if (!mainwin) return;
2463         
2464         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2465         menu_toggle_toggle(ifactory, "/View/Expand Summary View");
2466 }
2467
2468 void summary_toggle_view_real(SummaryView *summaryview)
2469 {
2470         MainWindow *mainwin = summaryview->mainwin;
2471         union CompositeWin *cwin = &mainwin->win;
2472         GtkWidget *vpaned = NULL;
2473         GtkWidget *container = NULL;
2474         GtkWidget *toggle_view_btn;
2475         GtkWidget *toggle_view_arrow;
2476         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2477         
2478         switch (mainwin->type) {
2479         case SEPARATE_NONE:
2480                 vpaned = cwin->sep_none.vpaned;
2481                 container = cwin->sep_none.hpaned;
2482                 break;
2483         case SEPARATE_FOLDER:
2484                 vpaned = cwin->sep_folder.vpaned;
2485                 container = mainwin->vbox_body;
2486                 break;
2487         case SEPARATE_MESSAGE:
2488         case SEPARATE_BOTH:
2489                 return;
2490         }
2491
2492         if (vpaned->parent != NULL) {
2493                 summaryview->msg_is_toggled_on = FALSE;
2494                 summaryview->displayed = NULL;
2495                 gtk_widget_ref(vpaned);
2496                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
2497                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
2498                 
2499                 gtk_widget_destroy(summaryview->toggle_view_arrow);
2500                 gtk_widget_destroy(summaryview->toggle_view_btn);
2501                 
2502                 toggle_view_btn = gtk_button_new();
2503                 gtk_box_pack_end(GTK_BOX(summaryview->hbox), toggle_view_btn, FALSE, FALSE, 0);
2504                 gtk_box_reorder_child(GTK_BOX(summaryview->hbox), toggle_view_btn, 0);
2505                 gtk_button_set_relief(GTK_BUTTON(toggle_view_btn), GTK_RELIEF_NONE);
2506                 toggle_view_arrow=gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_OUT);
2507                 gtk_container_add(GTK_CONTAINER(toggle_view_btn), toggle_view_arrow);
2508                 gtk_signal_connect(GTK_OBJECT(toggle_view_btn), "clicked",
2509                                         GTK_SIGNAL_FUNC(summary_toggle_view_cb), summaryview);
2510                 gtk_widget_show_all(toggle_view_btn);
2511                 menu_set_sensitive(ifactory, "/View/Expand Message View", FALSE);
2512         } else {
2513                 summaryview->msg_is_toggled_on = TRUE;
2514                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
2515                 gtk_container_add(GTK_CONTAINER(container), vpaned);
2516                 gtk_widget_unref(vpaned);
2517                 
2518                 gtk_widget_destroy(summaryview->toggle_view_arrow);
2519                 gtk_widget_destroy(summaryview->toggle_view_btn);
2520                 
2521                 toggle_view_btn = gtk_button_new();
2522                 gtk_box_pack_end(GTK_BOX(summaryview->hbox), toggle_view_btn, FALSE, FALSE, 0);
2523                 gtk_box_reorder_child(GTK_BOX(summaryview->hbox), toggle_view_btn, 0);
2524                 gtk_button_set_relief(GTK_BUTTON(toggle_view_btn), GTK_RELIEF_NONE);
2525                 toggle_view_arrow=gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2526                 gtk_container_add(GTK_CONTAINER(toggle_view_btn), toggle_view_arrow);
2527                 gtk_signal_connect(GTK_OBJECT(toggle_view_btn), "clicked",
2528                                         GTK_SIGNAL_FUNC(summary_toggle_view_cb), summaryview);
2529                 gtk_widget_show_all(toggle_view_btn);
2530                 menu_set_sensitive(ifactory, "/View/Expand Message View", TRUE);
2531         }
2532
2533         gtk_widget_grab_focus(summaryview->ctree);
2534         
2535         summaryview->toggle_view_btn = toggle_view_btn;
2536         summaryview->toggle_view_arrow = toggle_view_arrow;
2537 }
2538
2539 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2540                                                 GtkCTreeNode *node)
2541 {
2542         MsgInfo *msginfo;
2543
2544         if (node) {
2545                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2546                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2547                         return TRUE;
2548                 node = GTK_CTREE_ROW(node)->children;
2549         } else
2550                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2551
2552         while (node) {
2553                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2554                         return TRUE;
2555                 node = GTK_CTREE_ROW(node)->sibling;
2556         }
2557
2558         return FALSE;
2559 }
2560
2561 static gboolean summary_have_unread_children(SummaryView *summaryview,
2562                                              GtkCTreeNode *node)
2563 {
2564         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2565
2566         if (!node) return FALSE;
2567
2568         node = GTK_CTREE_ROW(node)->children;
2569
2570         while (node) {
2571                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2572                         return TRUE;
2573                 node = GTK_CTREE_ROW(node)->sibling;
2574         }
2575
2576         return FALSE;
2577 }
2578
2579 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2580 {
2581         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2582         GtkStyle *style = NULL;
2583         MsgInfo *msginfo;
2584         MsgFlags flags;
2585         gint *col_pos = summaryview->col_pos;
2586
2587         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2588         if (!msginfo) return;
2589
2590         flags = msginfo->flags;
2591
2592         gtk_ctree_node_set_foreground(ctree, row, NULL);
2593
2594         /* set new/unread column */
2595         if (MSG_IS_IGNORE_THREAD(flags)) {
2596                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2597                                           ignorethreadxpm, ignorethreadxpmmask);
2598         } else if (MSG_IS_NEW(flags)) {
2599                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2600                                           newxpm, newxpmmask);
2601         } else if (MSG_IS_UNREAD(flags)) {
2602                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2603                                           unreadxpm, unreadxpmmask);
2604         } else if (MSG_IS_REPLIED(flags)) {
2605                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2606                                           repliedxpm, repliedxpmmask);
2607         } else if (MSG_IS_FORWARDED(flags)) {
2608                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2609                                           forwardedxpm, forwardedxpmmask);
2610         } else {
2611                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2612                                         NULL);
2613         }
2614
2615         if (prefs_common.bold_unread &&
2616             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2617              (!GTK_CTREE_ROW(row)->expanded &&
2618               GTK_CTREE_ROW(row)->children &&
2619               summary_have_unread_children(summaryview, row))))
2620                 style = bold_style;
2621
2622         /* set mark column */
2623         if (MSG_IS_DELETED(flags)) {
2624                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2625                                           deletedxpm, deletedxpmmask);
2626                 if (style)
2627                         style = bold_deleted_style;
2628                 else {
2629                         style = small_deleted_style;
2630                 }
2631                         gtk_ctree_node_set_foreground
2632                                 (ctree, row, &summaryview->color_dim);
2633         } else if (MSG_IS_MARKED(flags)) {
2634                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2635                                           markxpm, markxpmmask);
2636         } else if (MSG_IS_MOVE(flags)) {
2637                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2638                 if (style)
2639                         style = bold_marked_style;
2640                 else {
2641                         style = small_marked_style;
2642                 }
2643                         gtk_ctree_node_set_foreground
2644                                 (ctree, row, &summaryview->color_marked);
2645         } else if (MSG_IS_COPY(flags)) {
2646                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2647                 if (style)
2648                         style = bold_marked_style;
2649                 else {
2650                         style = small_marked_style;
2651                 }
2652                         gtk_ctree_node_set_foreground
2653                                 (ctree, row, &summaryview->color_marked);
2654         }
2655         else if ((global_scoring ||
2656                   summaryview->folder_item->prefs->scoring) &&
2657                  (msginfo->score >= summaryview->important_score) &&
2658                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2659                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2660                 gtk_ctree_node_set_foreground(ctree, row,
2661                                               &summaryview->color_important);
2662         } else {
2663                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2664         }
2665
2666         if (MSG_IS_LOCKED(flags)) {
2667                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2668                                           lockedxpm, lockedxpmmask);
2669         }
2670         else {
2671                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2672         }
2673
2674         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2675                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2676                                           clipkeyxpm, clipkeyxpmmask);
2677         } else if (MSG_IS_ENCRYPTED(flags)) {
2678                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2679                                           keyxpm, keyxpmmask);
2680         } else if (MSG_IS_MIME(flags)) {
2681                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2682                                           clipxpm, clipxpmmask);
2683         } else {
2684                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2685         }
2686         if (!style)
2687                 style = small_style;
2688
2689         gtk_ctree_node_set_row_style(ctree, row, style);
2690
2691         if (MSG_GET_COLORLABEL(flags))
2692                 summary_set_colorlabel_color(ctree, row,
2693                                              MSG_GET_COLORLABEL_VALUE(flags));
2694 }
2695
2696 void summary_set_marks_selected(SummaryView *summaryview)
2697 {
2698         GList *cur;
2699
2700         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2701              cur = cur->next)
2702                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2703 }
2704
2705 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2706 {
2707         gboolean changed = FALSE;
2708         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2709         MsgInfo *msginfo;
2710
2711         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2712         if (MSG_IS_DELETED(msginfo->flags))
2713                 summaryview->deleted--;
2714         if (MSG_IS_MOVE(msginfo->flags)) {
2715                 summaryview->moved--;
2716                 changed = TRUE;
2717         }
2718         if (MSG_IS_COPY(msginfo->flags)) {
2719                 summaryview->copied--;
2720                 changed = TRUE;
2721         }
2722         if (changed && !prefs_common.immediate_exec) {
2723                 msginfo->to_folder->op_count--;
2724                 if (msginfo->to_folder->op_count == 0)
2725                         folderview_update_item(msginfo->to_folder, 0);
2726         }
2727         msginfo->to_folder = NULL;
2728         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2729         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2730         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2731         if (MSG_IS_IMAP(msginfo->flags))
2732                 imap_msg_set_perm_flags(msginfo, MSG_MARKED);
2733         CHANGE_FLAGS(msginfo);
2734         summary_set_row_marks(summaryview, row);
2735         debug_print(_("Message %s/%d is marked\n"), msginfo->folder->path, msginfo->msgnum);
2736 }
2737
2738 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2739 {
2740         /* almost verbatim summary_mark_row(); may want a menu action? */
2741         gboolean changed = FALSE;
2742         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2743         MsgInfo *msginfo;
2744
2745         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2746         if (MSG_IS_DELETED(msginfo->flags))
2747                 summaryview->deleted--;
2748         if (MSG_IS_MOVE(msginfo->flags)) {
2749                 summaryview->moved--;
2750                 changed = TRUE;
2751         }
2752         if (MSG_IS_COPY(msginfo->flags)) {
2753                 summaryview->copied--;
2754                 changed = TRUE;
2755         }
2756         if (changed && !prefs_common.immediate_exec) {
2757                 msginfo->to_folder->op_count--;
2758                 if (msginfo->to_folder->op_count == 0)
2759                         folderview_update_item(msginfo->to_folder, 0);
2760         }
2761         msginfo->to_folder = NULL;
2762         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2763         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2764         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_LOCKED);
2765         if (MSG_IS_IMAP(msginfo->flags))
2766                 imap_msg_set_perm_flags(msginfo, MSG_LOCKED);
2767         CHANGE_FLAGS(msginfo);
2768         summary_set_row_marks(summaryview, row);
2769         debug_print(_("Message %d is locked\n"), msginfo->msgnum);
2770 }
2771
2772 void summary_mark(SummaryView *summaryview)
2773 {
2774         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2775         GList *cur;
2776
2777         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2778                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2779
2780         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2781         summary_status_show(summaryview);
2782 }
2783
2784 static void summary_mark_row_as_read(SummaryView *summaryview,
2785                                      GtkCTreeNode *row)
2786 {
2787         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2788         MsgInfo *msginfo;
2789
2790         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2791         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2792                 summaryview->newmsgs--;
2793         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2794                 summaryview->unread--;
2795         if (MSG_IS_NEW(msginfo->flags) ||
2796             MSG_IS_UNREAD(msginfo->flags)) {
2797                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2798                 if (MSG_IS_IMAP(msginfo->flags))
2799                         imap_msg_unset_perm_flags(msginfo, MSG_NEW | MSG_UNREAD);
2800                 CHANGE_FLAGS(msginfo);
2801                 summary_set_row_marks(summaryview, row);
2802                 debug_print(_("Message %d is marked as read\n"),
2803                             msginfo->msgnum);
2804         }
2805 }
2806
2807 void summary_mark_as_read(SummaryView *summaryview)
2808 {
2809         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2810         GList *cur;
2811
2812         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2813                 summary_mark_row_as_read(summaryview,
2814                                          GTK_CTREE_NODE(cur->data));
2815
2816         summary_status_show(summaryview);
2817 }
2818
2819 void summary_mark_all_read(SummaryView *summaryview)
2820 {
2821         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2822         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2823         GtkCTreeNode *node;
2824
2825         gtk_clist_freeze(clist);
2826         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2827              node = gtkut_ctree_node_next(ctree, node))
2828                 summary_mark_row_as_read(summaryview, node);
2829         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2830              node = gtkut_ctree_node_next(ctree, node)) {
2831                 if (!GTK_CTREE_ROW(node)->expanded)
2832                         summary_set_row_marks(summaryview, node);
2833         }
2834         gtk_clist_thaw(clist);
2835
2836         summary_status_show(summaryview);
2837 }
2838
2839 static void summary_mark_row_as_unread(SummaryView *summaryview,
2840                                        GtkCTreeNode *row)
2841 {
2842         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2843         MsgInfo *msginfo;
2844
2845         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2846         if (MSG_IS_DELETED(msginfo->flags)) {
2847                 msginfo->to_folder = NULL;
2848                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2849                 summaryview->deleted--;
2850         }
2851         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2852         if (MSG_IS_IMAP(msginfo->flags))
2853                 imap_msg_unset_perm_flags(msginfo, MSG_REPLIED);
2854         if (!MSG_IS_UNREAD(msginfo->flags)) {
2855                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
2856                 if (MSG_IS_IMAP(msginfo->flags))
2857                         imap_msg_set_perm_flags(msginfo, MSG_UNREAD);
2858                 summaryview->unread++;
2859                 debug_print(_("Message %d is marked as unread\n"),
2860                             msginfo->msgnum);
2861         }
2862
2863         CHANGE_FLAGS(msginfo);
2864
2865         summary_set_row_marks(summaryview, row);
2866 }
2867
2868 void summary_mark_as_unread(SummaryView *summaryview)
2869 {
2870         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2871         GList *cur;
2872
2873         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2874                 summary_mark_row_as_unread(summaryview,
2875                                            GTK_CTREE_NODE(cur->data));
2876
2877         summary_status_show(summaryview);
2878 }
2879
2880 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2881 {
2882         gboolean changed = FALSE;
2883         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2884         MsgInfo *msginfo;
2885
2886         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2887
2888         if (MSG_IS_LOCKED(msginfo->flags)) return;
2889
2890         if (MSG_IS_DELETED(msginfo->flags)) return;
2891
2892         if (MSG_IS_MOVE(msginfo->flags)) {
2893                 summaryview->moved--;
2894                 changed = TRUE;
2895         }
2896         if (MSG_IS_COPY(msginfo->flags)) {
2897                 summaryview->copied--;
2898                 changed = TRUE;
2899         }
2900         if (changed && !prefs_common.immediate_exec) {
2901                 msginfo->to_folder->op_count--;
2902                 if (msginfo->to_folder->op_count == 0)
2903                         folderview_update_item(msginfo->to_folder, 0);
2904         }
2905         msginfo->to_folder = NULL;
2906         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2907         if (MSG_IS_IMAP(msginfo->flags))
2908                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
2909         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2910         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2911         CHANGE_FLAGS(msginfo);
2912         summaryview->deleted++;
2913
2914         if (!prefs_common.immediate_exec && 
2915             summaryview->folder_item->stype != F_TRASH)
2916                 summary_set_row_marks(summaryview, row);
2917
2918         debug_print(_("Message %s/%d is set to delete\n"),
2919                     msginfo->folder->path, msginfo->msgnum);
2920 }
2921
2922 void summary_delete(SummaryView *summaryview)
2923 {
2924         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2925         FolderItem *item = summaryview->folder_item;
2926         GList *cur;
2927         GtkCTreeNode *sel_last = NULL;
2928         GtkCTreeNode *node;
2929
2930         if (!item || item->folder->type == F_NEWS) return;
2931
2932         if (summary_is_locked(summaryview)) return;
2933
2934         /* if current folder is trash, ask for confirmation */
2935         if (item->stype == F_TRASH) {
2936                 AlertValue aval;
2937
2938                 aval = alertpanel(_("Delete message(s)"),
2939                                   _("Do you really want to delete message(s) from the trash?"),
2940                                   _("Yes"), _("No"), NULL);
2941                 if (aval != G_ALERTDEFAULT) return;
2942         }
2943
2944         /* next code sets current row focus right. We need to find a row
2945          * that is not deleted. */
2946         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2947                 sel_last = GTK_CTREE_NODE(cur->data);
2948                 summary_delete_row(summaryview, sel_last);
2949         }
2950
2951         node = summary_find_next_msg(summaryview, sel_last);
2952         if (!node)
2953                 node = summary_find_prev_msg(summaryview, sel_last);
2954
2955         if (node) {
2956                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2957                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2958                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2959                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2960                 else
2961                         summary_select_node(summaryview, node,
2962                                             summaryview->msg_is_toggled_on);
2963         }
2964
2965         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2966                 summary_execute(summaryview);
2967         else
2968                 summary_status_show(summaryview);
2969 }
2970
2971 void summary_delete_duplicated(SummaryView *summaryview)
2972 {
2973         if (!summaryview->folder_item ||
2974             summaryview->folder_item->folder->type == F_NEWS) return;
2975         if (summaryview->folder_item->stype == F_TRASH) return;
2976
2977         main_window_cursor_wait(summaryview->mainwin);
2978         debug_print(_("Deleting duplicated messages..."));
2979         STATUSBAR_PUSH(summaryview->mainwin,
2980                        _("Deleting duplicated messages..."));
2981
2982         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2983                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2984                                 summaryview);
2985
2986         if (prefs_common.immediate_exec)
2987                 summary_execute(summaryview);
2988         else
2989                 summary_status_show(summaryview);
2990
2991         debug_print(_("done.\n"));
2992         STATUSBAR_POP(summaryview->mainwin);
2993         main_window_cursor_normal(summaryview->mainwin);
2994 }
2995
2996 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2997                                            SummaryView *summaryview)
2998 {
2999         GtkCTreeNode *found;
3000         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
3001
3002         if (!msginfo->msgid || !*msginfo->msgid) return;
3003
3004         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
3005
3006         if (found && found != node)
3007                 summary_delete_row(summaryview, node);
3008 }
3009
3010 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3011 {
3012         gboolean changed = FALSE;
3013         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3014         MsgInfo *msginfo;
3015
3016         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3017         if (MSG_IS_DELETED(msginfo->flags))
3018                 summaryview->deleted--;
3019         if (MSG_IS_MOVE(msginfo->flags)) {
3020                 summaryview->moved--;
3021                 changed = TRUE;
3022         }
3023         if (MSG_IS_COPY(msginfo->flags)) {
3024                 summaryview->copied--;
3025                 changed = TRUE;
3026         }
3027         if (changed && !prefs_common.immediate_exec) {
3028                 msginfo->to_folder->op_count--;
3029                 if (msginfo->to_folder->op_count == 0)
3030                         folderview_update_item(msginfo->to_folder, 0);
3031         }
3032         msginfo->to_folder = NULL;
3033         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
3034         if (MSG_IS_IMAP(msginfo->flags))
3035                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
3036         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
3037         CHANGE_FLAGS(msginfo);
3038         summary_set_row_marks(summaryview, row);
3039         debug_print(_("Message %s/%d is unmarked\n"),
3040                     msginfo->folder->path, msginfo->msgnum);
3041 }
3042
3043 void summary_unmark(SummaryView *summaryview)
3044 {
3045         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3046         GList *cur;
3047
3048         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3049                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3050
3051         summary_status_show(summaryview);
3052 }
3053
3054 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3055                                 FolderItem *to_folder)
3056 {
3057         gboolean changed = FALSE;
3058         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3059         MsgInfo *msginfo;
3060
3061         g_return_if_fail(to_folder != NULL);
3062
3063         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3064         if (MSG_IS_MOVE(msginfo->flags)) {
3065                 if (!prefs_common.immediate_exec) {
3066                         msginfo->to_folder->op_count--;
3067                         if (msginfo->to_folder->op_count == 0) {
3068                                 folderview_update_item(msginfo->to_folder, 0);
3069                                 changed = TRUE;
3070                         }
3071                 }
3072         }
3073         msginfo->to_folder = to_folder;
3074         if (MSG_IS_DELETED(msginfo->flags))
3075                 summaryview->deleted--;
3076         if (MSG_IS_COPY(msginfo->flags)) {
3077                 summaryview->copied--;
3078                 if (!prefs_common.immediate_exec)
3079                         msginfo->to_folder->op_count--;
3080         }
3081         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
3082         if (MSG_IS_IMAP(msginfo->flags))
3083                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
3084         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_COPY);
3085         if (!MSG_IS_MOVE(msginfo->flags)) {
3086                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
3087                 summaryview->moved++;
3088                 changed = TRUE;
3089         }
3090         if (!prefs_common.immediate_exec) {
3091                 summary_set_row_marks(summaryview, row);
3092                 if (changed) {
3093                         msginfo->to_folder->op_count++;
3094                         if (msginfo->to_folder->op_count == 1)
3095                                 folderview_update_item(msginfo->to_folder, 0);
3096                 }
3097         }
3098
3099         debug_print(_("Message %d is set to move to %s\n"),
3100                     msginfo->msgnum, to_folder->path);
3101 }
3102
3103 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3104 {
3105         GList *cur;
3106
3107         if (!to_folder) return;
3108         if (!summaryview->folder_item ||
3109             summaryview->folder_item->folder->type == F_NEWS) return;
3110
3111         if (summary_is_locked(summaryview)) return;
3112
3113         if (summaryview->folder_item == to_folder) {
3114                 alertpanel_notice(_("Destination is same as current folder."));
3115                 return;
3116         }
3117
3118         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3119              cur != NULL; cur = cur->next)
3120                 summary_move_row_to
3121                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3122
3123         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3124
3125         if (prefs_common.immediate_exec)
3126                 summary_execute(summaryview);
3127         else {
3128                 summary_status_show(summaryview);
3129
3130                 folderview_update_item(to_folder, 0);
3131         }
3132 }
3133
3134 void summary_move_to(SummaryView *summaryview)
3135 {
3136         FolderItem *to_folder;
3137
3138         if (!summaryview->folder_item ||
3139             summaryview->folder_item->folder->type == F_NEWS) return;
3140
3141         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3142                                          FOLDER_SEL_MOVE, NULL);
3143         summary_move_selected_to(summaryview, to_folder);
3144 }
3145
3146 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3147                                 FolderItem *to_folder)
3148 {
3149         gboolean changed = FALSE;
3150         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3151         MsgInfo *msginfo;
3152
3153         g_return_if_fail(to_folder != NULL);
3154
3155         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3156         if (MSG_IS_COPY(msginfo->flags)) {
3157                 if (!prefs_common.immediate_exec) {
3158                         msginfo->to_folder->op_count--;
3159                         if (msginfo->to_folder->op_count == 0) {
3160                                 folderview_update_item(msginfo->to_folder, 0);
3161                                 changed = TRUE;
3162                         }
3163                 }
3164         }
3165         msginfo->to_folder = to_folder;
3166         if (MSG_IS_DELETED(msginfo->flags))
3167                 summaryview->deleted--;
3168         if (MSG_IS_MOVE(msginfo->flags)) {
3169                 summaryview->moved--;
3170                 if (!prefs_common.immediate_exec)
3171                         msginfo->to_folder->op_count--;
3172         }
3173         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
3174         if (MSG_IS_IMAP(msginfo->flags))
3175                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
3176         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
3177         if (!MSG_IS_COPY(msginfo->flags)) {
3178                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_COPY);
3179                 summaryview->copied++;
3180                 changed = TRUE;
3181         }
3182         if (!prefs_common.immediate_exec) {
3183                 summary_set_row_marks(summaryview, row);
3184                 if (changed) {
3185                         msginfo->to_folder->op_count++;
3186                         if (msginfo->to_folder->op_count == 1)
3187                                 folderview_update_item(msginfo->to_folder, 0);
3188                 }
3189         }
3190
3191         debug_print(_("Message %d is set to copy to %s\n"),
3192                     msginfo->msgnum, to_folder->path);
3193 }
3194
3195 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3196 {
3197         GList *cur;
3198
3199         if (!to_folder) return;
3200         if (!summaryview->folder_item) return;
3201
3202         if (summary_is_locked(summaryview)) return;
3203
3204         if (summaryview->folder_item == to_folder) {
3205                 alertpanel_notice
3206                         (_("Destination to copy is same as current folder."));
3207                 return;
3208         }
3209
3210         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3211              cur != NULL; cur = cur->next)
3212                 summary_copy_row_to
3213                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3214
3215         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3216
3217         if (prefs_common.immediate_exec)
3218                 summary_execute(summaryview);
3219         else {
3220                 summary_status_show(summaryview);
3221
3222                 folderview_update_item(to_folder, 0);
3223         }
3224 }
3225
3226 void summary_copy_to(SummaryView *summaryview)
3227 {
3228         FolderItem *to_folder;
3229
3230         if (!summaryview->folder_item) return;
3231
3232         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3233                                          FOLDER_SEL_COPY, NULL);
3234         summary_copy_selected_to(summaryview, to_folder);
3235 }
3236
3237 void summary_add_address(SummaryView *summaryview)
3238 {
3239         MsgInfo *msginfo;
3240         gchar *from;
3241
3242         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3243                                               summaryview->selected);
3244         if (!msginfo) return;
3245
3246         Xstrdup_a(from, msginfo->from, return);
3247         eliminate_address_comment(from);
3248         extract_address(from);
3249         addressbook_add_contact(msginfo->fromname, from, NULL);
3250 }
3251
3252 void summary_select_all(SummaryView *summaryview)
3253 {
3254         if (summaryview->messages >= 500) {
3255                 STATUSBAR_PUSH(summaryview->mainwin,
3256                                _("Selecting all messages..."));
3257                 main_window_cursor_wait(summaryview->mainwin);
3258         }
3259
3260         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3261
3262         if (summaryview->messages >= 500) {
3263                 STATUSBAR_POP(summaryview->mainwin);
3264                 main_window_cursor_normal(summaryview->mainwin);
3265         }
3266 }
3267
3268 void summary_unselect_all(SummaryView *summaryview)
3269 {
3270         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3271 }
3272
3273 void summary_save_as(SummaryView *summaryview)
3274 {
3275         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3276         MsgInfo *msginfo;
3277         gchar *filename = NULL;
3278         gchar *src, *dest;
3279
3280         if (!summaryview->selected) return;
3281         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3282         if (!msginfo) return;
3283
3284         if (msginfo->subject) {
3285                 Xstrdup_a(filename, msginfo->subject, return);
3286                 subst_for_filename(filename);
3287         }
3288         dest = filesel_select_file(_("Save as"), filename);
3289         if (!dest) return;
3290         if (is_file_exist(dest)) {
3291                 AlertValue aval;
3292
3293                 aval = alertpanel(_("Overwrite"),
3294                                   _("Overwrite existing file?"),
3295                                   _("OK"), _("Cancel"), NULL);
3296                 if (G_ALERTDEFAULT != aval) return;
3297         }
3298
3299         src = procmsg_get_message_file(msginfo);
3300         copy_file(src, dest);
3301         g_free(src);
3302 }
3303
3304 void summary_print(SummaryView *summaryview)
3305 {
3306         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3307         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3308         MsgInfo *msginfo;
3309         GList *cur;
3310         gchar *cmdline;
3311         gchar *p;
3312
3313         if (clist->selection == NULL) return;
3314
3315         cmdline = input_dialog(_("Print"),
3316                                _("Enter the print command line:\n"
3317                                  "(`%s' will be replaced with file name)"),
3318                                prefs_common.print_cmd);
3319         if (!cmdline) return;
3320         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3321             strchr(p + 2, '%')) {
3322                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3323                                  cmdline);
3324                 g_free(cmdline);
3325                 return;
3326         }
3327
3328         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3329                 msginfo = gtk_ctree_node_get_row_data
3330                         (ctree, GTK_CTREE_NODE(cur->data));
3331                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3332         }
3333
3334         g_free(cmdline);
3335 }
3336
3337 gboolean summary_execute(SummaryView *summaryview)
3338 {
3339         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3340         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3341         GtkCTreeNode *node, *next;
3342