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