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