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