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