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