correct display recipient on From...
[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                 Xstrdup_a(addr, msginfo->from, return);
2263                 extract_address(addr);
2264
2265                 if (prefs_common.use_addr_book) {
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                 } else {
2273                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2274                                 g_free(to);
2275                                 to = g_strconcat("-->", msginfo->to, NULL);
2276                                 text[col_pos[S_COL_FROM]] = to;
2277                         }
2278                 }
2279         }
2280
2281         /*
2282          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2283          * checking whether the above block (which handles the special case of
2284          * the --> in sent boxes) was executed.
2285          */
2286         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2287                 gchar *from;
2288                 from = summary_complete_address(msginfo->from);
2289                 if (from) {
2290                         /*
2291                          * FIXME: this text[col_pos[S_COL_FROM]] should be freed
2292                          * but may have been assigned _("No From"). Should be
2293                          * freed??? 
2294                          */
2295                         text[col_pos[S_COL_FROM]] = from;
2296                 }       
2297         }
2298
2299         if (prefs->enable_simplify_subject 
2300             && prefs->simplify_subject_preg != NULL )
2301                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2302                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2303                                         prefs->simplify_subject_preg) : 
2304                         
2305                         _("(No Subject)");
2306         else 
2307                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2308                         _("(No Subject)");
2309 }
2310
2311 #define CHANGE_FLAGS(msginfo) \
2312 { \
2313 if (msginfo->folder->folder->change_flags != NULL) \
2314 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2315                                       msginfo->folder, \
2316                                       msginfo); \
2317 }
2318
2319 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2320 {
2321         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2322 }
2323
2324 static void summary_display_msg_full(SummaryView *summaryview,
2325                                      GtkCTreeNode *row,
2326                                      gboolean new_window, gboolean all_headers)
2327 {
2328         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2329         MsgInfo *msginfo;
2330         MsgFlags flags;
2331         gchar *filename;
2332
2333         if (!new_window && summaryview->displayed == row) return;
2334         g_return_if_fail(row != NULL);
2335
2336         if (summary_is_locked(summaryview)) return;
2337         summary_lock(summaryview);
2338
2339         STATUSBAR_POP(summaryview->mainwin);
2340         GTK_EVENTS_FLUSH();
2341
2342         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2343
2344         filename = procmsg_get_message_file(msginfo);
2345         if (!filename) {
2346                 summary_unlock(summaryview);
2347                 return;
2348         }
2349         g_free(filename);
2350
2351 /* NOT NEEDED ANYMORE
2352         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2353                 summaryview->newmsgs--;
2354         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2355                 summaryview->unread--;
2356 */
2357         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2358         summary_set_row_marks(summaryview, row);
2359         gtk_clist_thaw(GTK_CLIST(ctree));
2360         summary_status_show(summaryview);
2361
2362         flags = msginfo->flags;
2363
2364         if (new_window) {
2365                 MessageView *msgview;
2366
2367                 msgview = messageview_create_with_new_window();
2368                 messageview_show(msgview, msginfo, all_headers);
2369         } else {
2370                 MessageView *msgview;
2371
2372                 msgview = summaryview->messageview;
2373
2374                 summaryview->displayed = row;
2375                 if (!messageview_is_visible(msgview))
2376                         main_window_toggle_message_view(summaryview->mainwin);
2377                 messageview_show(msgview, msginfo, all_headers);
2378                 if (msgview->type == MVIEW_TEXT ||
2379                     (msgview->type == MVIEW_MIME &&
2380                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2381                       gtk_notebook_get_current_page
2382                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2383                         gtk_widget_grab_focus(summaryview->ctree);
2384                 GTK_EVENTS_FLUSH();
2385                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2386         }
2387
2388         summary_set_menu_sensitive(summaryview);
2389         main_window_set_toolbar_sensitive(summaryview->mainwin);
2390
2391         summary_unlock(summaryview);
2392 }
2393
2394 void summary_display_msg_selected(SummaryView *summaryview,
2395                                   gboolean all_headers)
2396 {
2397         if (summary_is_locked(summaryview)) return;
2398         summaryview->displayed = NULL;
2399         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2400                                  all_headers);
2401 }
2402
2403 void summary_redisplay_msg(SummaryView *summaryview)
2404 {
2405         GtkCTreeNode *node;
2406
2407         if (summaryview->displayed) {
2408                 node = summaryview->displayed;
2409                 summaryview->displayed = NULL;
2410                 summary_display_msg(summaryview, node);
2411         }
2412 }
2413
2414 void summary_open_msg(SummaryView *summaryview)
2415 {
2416         if (!summaryview->selected) return;
2417
2418         summary_display_msg_full(summaryview, summaryview->selected,
2419                                  TRUE, FALSE);
2420 }
2421
2422 void summary_view_source(SummaryView * summaryview)
2423 {
2424         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2425         MsgInfo *msginfo;
2426         SourceWindow *srcwin;
2427
2428         if (!summaryview->selected) return;
2429
2430         srcwin = source_window_create();
2431         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2432         source_window_show_msg(srcwin, msginfo);
2433         source_window_show(srcwin);
2434 }
2435
2436 void summary_reedit(SummaryView *summaryview)
2437 {
2438         MsgInfo *msginfo;
2439
2440         if (!summaryview->selected) return;
2441         if (!summaryview->folder_item) return;
2442         if (summaryview->folder_item->stype != F_OUTBOX &&
2443             summaryview->folder_item->stype != F_DRAFT  &&
2444             summaryview->folder_item->stype != F_QUEUE) return;
2445
2446         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2447                                               summaryview->selected);
2448         if (!msginfo) return;
2449
2450         compose_reedit(msginfo);
2451 }
2452
2453 void summary_step(SummaryView *summaryview, GtkScrollType type)
2454 {
2455         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2456         GtkCTreeNode *node;
2457
2458         if (summary_is_locked(summaryview)) return;
2459
2460         if (type == GTK_SCROLL_STEP_FORWARD) {
2461                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2462                 if (node)
2463                         gtkut_ctree_expand_parent_all(ctree, node);
2464                 else
2465                         return;
2466         } else {
2467                 if (summaryview->selected) {
2468                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2469                         if (!node) return;
2470                 }
2471         }
2472
2473         if (messageview_is_visible(summaryview->messageview))
2474                 summaryview->display_msg = TRUE;
2475
2476         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2477                                 type, 0.0);
2478 }
2479
2480 void summary_toggle_view(SummaryView *summaryview)
2481 {
2482         if (!messageview_is_visible(summaryview->messageview) &&
2483             summaryview->selected)
2484                 summary_display_msg(summaryview,
2485                                     summaryview->selected);
2486         else
2487                 main_window_toggle_message_view(summaryview->mainwin);
2488 }
2489
2490 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2491                                                 GtkCTreeNode *node)
2492 {
2493         MsgInfo *msginfo;
2494
2495         if (node) {
2496                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2497                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2498                         return TRUE;
2499                 node = GTK_CTREE_ROW(node)->children;
2500         } else
2501                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2502
2503         while (node) {
2504                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2505                         return TRUE;
2506                 node = GTK_CTREE_ROW(node)->sibling;
2507         }
2508
2509         return FALSE;
2510 }
2511
2512 static gboolean summary_have_unread_children(SummaryView *summaryview,
2513                                              GtkCTreeNode *node)
2514 {
2515         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2516
2517         if (!node) return FALSE;
2518
2519         node = GTK_CTREE_ROW(node)->children;
2520
2521         while (node) {
2522                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2523                         return TRUE;
2524                 node = GTK_CTREE_ROW(node)->sibling;
2525         }
2526
2527         return FALSE;
2528 }
2529
2530 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2531 {
2532         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2533         GtkStyle *style = NULL;
2534         MsgInfo *msginfo;
2535         MsgFlags flags;
2536         gint *col_pos = summaryview->col_pos;
2537
2538         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2539         if (!msginfo) return;
2540
2541         flags = msginfo->flags;
2542
2543         gtk_ctree_node_set_foreground(ctree, row, NULL);
2544
2545         /* set new/unread column */
2546         if (MSG_IS_IGNORE_THREAD(flags)) {
2547                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2548                                           ignorethreadxpm, ignorethreadxpmmask);
2549         } else if (MSG_IS_NEW(flags)) {
2550                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2551                                           newxpm, newxpmmask);
2552         } else if (MSG_IS_UNREAD(flags)) {
2553                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2554                                           unreadxpm, unreadxpmmask);
2555         } else if (MSG_IS_REPLIED(flags)) {
2556                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2557                                           repliedxpm, repliedxpmmask);
2558         } else if (MSG_IS_FORWARDED(flags)) {
2559                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2560                                           forwardedxpm, forwardedxpmmask);
2561         } else {
2562                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2563                                         NULL);
2564         }
2565
2566         if (prefs_common.bold_unread &&
2567             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2568              (!GTK_CTREE_ROW(row)->expanded &&
2569               GTK_CTREE_ROW(row)->children &&
2570               summary_have_unread_children(summaryview, row))))
2571                 style = bold_style;
2572
2573         /* set mark column */
2574         if (MSG_IS_DELETED(flags)) {
2575                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2576                                           deletedxpm, deletedxpmmask);
2577                 if (style)
2578                         style = bold_deleted_style;
2579                 else {
2580                         style = small_deleted_style;
2581                 }
2582                         gtk_ctree_node_set_foreground
2583                                 (ctree, row, &summaryview->color_dim);
2584         } else if (MSG_IS_MARKED(flags)) {
2585                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2586                                           markxpm, markxpmmask);
2587         } else if (MSG_IS_MOVE(flags)) {
2588                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2589                 if (style)
2590                         style = bold_marked_style;
2591                 else {
2592                         style = small_marked_style;
2593                 }
2594                         gtk_ctree_node_set_foreground
2595                                 (ctree, row, &summaryview->color_marked);
2596         } else if (MSG_IS_COPY(flags)) {
2597                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2598                 if (style)
2599                         style = bold_marked_style;
2600                 else {
2601                         style = small_marked_style;
2602                 }
2603                         gtk_ctree_node_set_foreground
2604                                 (ctree, row, &summaryview->color_marked);
2605         }
2606         else if ((global_scoring ||
2607                   summaryview->folder_item->prefs->scoring) &&
2608                  (msginfo->score >= summaryview->important_score) &&
2609                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2610                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2611                 gtk_ctree_node_set_foreground(ctree, row,
2612                                               &summaryview->color_important);
2613         } else {
2614                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2615         }
2616
2617         if (MSG_IS_LOCKED(flags)) {
2618                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2619                                           lockedxpm, lockedxpmmask);
2620         }
2621         else {
2622                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2623         }
2624
2625         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2626                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2627                                           clipkeyxpm, clipkeyxpmmask);
2628         } else if (MSG_IS_ENCRYPTED(flags)) {
2629                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2630                                           keyxpm, keyxpmmask);
2631         } else if (MSG_IS_MIME(flags)) {
2632                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2633                                           clipxpm, clipxpmmask);
2634         } else {
2635                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2636         }
2637         if (!style)
2638                 style = small_style;
2639
2640         gtk_ctree_node_set_row_style(ctree, row, style);
2641
2642         if (MSG_GET_COLORLABEL(flags))
2643                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2644 }
2645
2646 void summary_set_marks_selected(SummaryView *summaryview)
2647 {
2648         GList *cur;
2649
2650         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2651              cur = cur->next)
2652                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2653 }
2654
2655 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2656 {
2657         gboolean changed = FALSE;
2658         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2659         MsgInfo *msginfo;
2660
2661         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2662         if (MSG_IS_DELETED(msginfo->flags))
2663                 summaryview->deleted--;
2664         if (MSG_IS_MOVE(msginfo->flags)) {
2665                 summaryview->moved--;
2666                 changed = TRUE;
2667         }
2668         if (MSG_IS_COPY(msginfo->flags)) {
2669                 summaryview->copied--;
2670                 changed = TRUE;
2671         }
2672         if (changed && !prefs_common.immediate_exec) {
2673                 msginfo->to_folder->op_count--;
2674                 if (msginfo->to_folder->op_count == 0)
2675                         folderview_update_item(msginfo->to_folder, 0);
2676         }
2677         msginfo->to_folder = NULL;
2678         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2679         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2680         summary_set_row_marks(summaryview, row);
2681         debug_print(_("Message %s/%d is marked\n"), msginfo->folder->path, msginfo->msgnum);
2682 }
2683
2684 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2685 {
2686         /* almost verbatim summary_mark_row(); may want a menu action? */
2687         gboolean changed = FALSE;
2688         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2689         MsgInfo *msginfo;
2690
2691         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2692         if (MSG_IS_DELETED(msginfo->flags))
2693                 summaryview->deleted--;
2694         if (MSG_IS_MOVE(msginfo->flags)) {
2695                 summaryview->moved--;
2696                 changed = TRUE;
2697         }
2698         if (MSG_IS_COPY(msginfo->flags)) {
2699                 summaryview->copied--;
2700                 changed = TRUE;
2701         }
2702         if (changed && !prefs_common.immediate_exec) {
2703                 msginfo->to_folder->op_count--;
2704                 if (msginfo->to_folder->op_count == 0)
2705                         folderview_update_item(msginfo->to_folder, 0);
2706         }
2707         msginfo->to_folder = NULL;
2708         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2709         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2710         summary_set_row_marks(summaryview, row);
2711         debug_print(_("Message %d is locked\n"), msginfo->msgnum);
2712 }
2713
2714 void summary_mark(SummaryView *summaryview)
2715 {
2716         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2717         GList *cur;
2718
2719         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2720                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2721
2722         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2723         summary_status_show(summaryview);
2724 }
2725
2726 static void summary_mark_row_as_read(SummaryView *summaryview,
2727                                      GtkCTreeNode *row)
2728 {
2729         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2730         MsgInfo *msginfo;
2731
2732         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2733 /* NOT NEEDED ANYMORE
2734         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2735                 summaryview->newmsgs--;
2736         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2737                 summaryview->unread--;
2738 */
2739         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2740         summary_set_row_marks(summaryview, row);
2741         debug_print(_("Message %d is marked as read\n"),
2742                 msginfo->msgnum);
2743 }
2744
2745 void summary_mark_as_read(SummaryView *summaryview)
2746 {
2747         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2748         GList *cur;
2749
2750         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2751                 summary_mark_row_as_read(summaryview,
2752                                          GTK_CTREE_NODE(cur->data));
2753
2754         summary_status_show(summaryview);
2755 }
2756
2757 void summary_mark_all_read(SummaryView *summaryview)
2758 {
2759         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2760         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2761         GtkCTreeNode *node;
2762
2763         gtk_clist_freeze(clist);
2764         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2765              node = gtkut_ctree_node_next(ctree, node))
2766                 summary_mark_row_as_read(summaryview, node);
2767         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2768              node = gtkut_ctree_node_next(ctree, node)) {
2769                 if (!GTK_CTREE_ROW(node)->expanded)
2770                         summary_set_row_marks(summaryview, node);
2771         }
2772         gtk_clist_thaw(clist);
2773
2774         summary_status_show(summaryview);
2775 }
2776
2777 static void summary_mark_row_as_unread(SummaryView *summaryview,
2778                                        GtkCTreeNode *row)
2779 {
2780         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2781         MsgInfo *msginfo;
2782
2783         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2784         if (MSG_IS_DELETED(msginfo->flags)) {
2785                 msginfo->to_folder = NULL;
2786                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2787                 summaryview->deleted--;
2788         }
2789 /* NOT NEEDED ANYMORE
2790         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2791                 summaryview->unread++;
2792 */
2793
2794         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2795         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2796         debug_print(_("Message %d is marked as unread\n"),
2797                 msginfo->msgnum);
2798
2799         summary_set_row_marks(summaryview, row);
2800 }
2801
2802 void summary_mark_as_unread(SummaryView *summaryview)
2803 {
2804         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2805         GList *cur;
2806
2807         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2808                 summary_mark_row_as_unread(summaryview,
2809                                            GTK_CTREE_NODE(cur->data));
2810
2811         summary_status_show(summaryview);
2812 }
2813
2814 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2815 {
2816         GList * cur;
2817         gboolean found;
2818
2819         switch (summaryview->folder_item->folder->type) {
2820
2821         case F_NEWS:
2822
2823                 /*
2824                   security : checks if one the accounts correspond to
2825                   the author of the post
2826                 */
2827
2828                 found = FALSE;
2829                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2830                         PrefsAccount * account;
2831                         gchar * from_name;
2832                         
2833                         account = cur->data;
2834                         if (account->name && *account->name)
2835                                 from_name =
2836                                         g_strdup_printf("%s <%s>",
2837                                                         account->name,
2838                                                         account->address);
2839                         else
2840                                 from_name =
2841                                         g_strdup_printf("%s",
2842                                                         account->address);
2843                         
2844                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2845                                 g_free(from_name);
2846                                 found = TRUE;
2847                                 break;
2848                         }
2849                         g_free(from_name);
2850                 }
2851
2852                 if (!found) {
2853                         alertpanel_error(_("You're not the author of the article\n"));
2854                 }
2855                 
2856                 return found;
2857
2858         default:
2859                 return TRUE;
2860         }
2861 }
2862
2863 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2864 {
2865         gboolean changed = FALSE;
2866         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2867         MsgInfo *msginfo;
2868
2869         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2870
2871         if (!check_permission(summaryview, msginfo))
2872                 return;
2873
2874         if (MSG_IS_LOCKED(msginfo->flags)) return;
2875
2876         if (MSG_IS_DELETED(msginfo->flags)) return;
2877
2878         if (MSG_IS_MOVE(msginfo->flags)) {
2879                 summaryview->moved--;
2880                 changed = TRUE;
2881         }
2882         if (MSG_IS_COPY(msginfo->flags)) {
2883                 summaryview->copied--;
2884                 changed = TRUE;
2885         }
2886         if (changed && !prefs_common.immediate_exec) {
2887                 msginfo->to_folder->op_count--;
2888                 if (msginfo->to_folder->op_count == 0)
2889                         folderview_update_item(msginfo->to_folder, 0);
2890         }
2891         msginfo->to_folder = NULL;
2892         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2893         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2894         summaryview->deleted++;
2895
2896         if (!prefs_common.immediate_exec && 
2897             summaryview->folder_item->stype != F_TRASH)
2898                 summary_set_row_marks(summaryview, row);
2899
2900         debug_print(_("Message %s/%d is set to delete\n"),
2901                     msginfo->folder->path, msginfo->msgnum);
2902 }
2903
2904 void summary_delete(SummaryView *summaryview)
2905 {
2906         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2907         FolderItem *item = summaryview->folder_item;
2908         GList *cur;
2909         GtkCTreeNode *sel_last = NULL;
2910         GtkCTreeNode *node;
2911
2912         if (!item) return;
2913 #if 0
2914         if (!item || item->folder->type == F_NEWS) return;
2915 #endif
2916
2917         if (summary_is_locked(summaryview)) return;
2918
2919         /* if current folder is trash, ask for confirmation */
2920         if (item->stype == F_TRASH) {
2921                 AlertValue aval;
2922
2923                 aval = alertpanel(_("Delete message(s)"),
2924                                   _("Do you really want to delete message(s) from the trash?"),
2925                                   _("Yes"), _("No"), NULL);
2926                 if (aval != G_ALERTDEFAULT) return;
2927         }
2928
2929         /* next code sets current row focus right. We need to find a row
2930          * that is not deleted. */
2931         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2932                 sel_last = GTK_CTREE_NODE(cur->data);
2933                 summary_delete_row(summaryview, sel_last);
2934         }
2935
2936         node = summary_find_next_msg(summaryview, sel_last);
2937         if (!node)
2938                 node = summary_find_prev_msg(summaryview, sel_last);
2939
2940         if (node) {
2941                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2942                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2943                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2944                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2945                 else
2946                         summary_select_node
2947                                 (summaryview, node,
2948                                  messageview_is_visible(summaryview->messageview),
2949                                  FALSE);
2950         }
2951
2952         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2953                 summary_execute(summaryview);
2954         else
2955                 summary_status_show(summaryview);
2956 }
2957
2958 void summary_delete_duplicated(SummaryView *summaryview)
2959 {
2960         if (!summaryview->folder_item ||
2961             summaryview->folder_item->folder->type == F_NEWS) return;
2962         if (summaryview->folder_item->stype == F_TRASH) return;
2963
2964         main_window_cursor_wait(summaryview->mainwin);
2965         debug_print(_("Deleting duplicated messages..."));
2966         STATUSBAR_PUSH(summaryview->mainwin,
2967                        _("Deleting duplicated messages..."));
2968
2969         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2970                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2971                                 summaryview);
2972
2973         if (prefs_common.immediate_exec)
2974                 summary_execute(summaryview);
2975         else
2976                 summary_status_show(summaryview);
2977
2978         debug_print(_("done.\n"));
2979         STATUSBAR_POP(summaryview->mainwin);
2980         main_window_cursor_normal(summaryview->mainwin);
2981 }
2982
2983 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2984                                            SummaryView *summaryview)
2985 {
2986         GtkCTreeNode *found;
2987         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2988
2989         if (!msginfo->msgid || !*msginfo->msgid) return;
2990
2991         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2992
2993         if (found && found != node)
2994                 summary_delete_row(summaryview, node);
2995 }
2996
2997 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2998 {
2999         gboolean changed = FALSE;
3000         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3001         MsgInfo *msginfo;
3002
3003         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3004         if (MSG_IS_DELETED(msginfo->flags))
3005                 summaryview->deleted--;
3006         if (MSG_IS_MOVE(msginfo->flags)) {
3007                 summaryview->moved--;
3008                 changed = TRUE;
3009         }
3010         if (MSG_IS_COPY(msginfo->flags)) {
3011                 summaryview->copied--;
3012                 changed = TRUE;
3013         }
3014         if (changed && !prefs_common.immediate_exec) {
3015                 msginfo->to_folder->op_count--;
3016                 if (msginfo->to_folder->op_count == 0)
3017                         folderview_update_item(msginfo->to_folder, 0);
3018         }
3019         msginfo->to_folder = NULL;
3020         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3021         summary_set_row_marks(summaryview, row);
3022
3023         debug_print(_("Message %s/%d is unmarked\n"),
3024                     msginfo->folder->path, msginfo->msgnum);
3025 }
3026
3027 void summary_unmark(SummaryView *summaryview)
3028 {
3029         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3030         GList *cur;
3031
3032         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3033                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3034
3035         summary_status_show(summaryview);
3036 }
3037
3038 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3039                                 FolderItem *to_folder)
3040 {
3041         gboolean changed = FALSE;
3042         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3043         MsgInfo *msginfo;
3044
3045         g_return_if_fail(to_folder != NULL);
3046
3047         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3048         if (MSG_IS_MOVE(msginfo->flags)) {
3049                 if (!prefs_common.immediate_exec) {
3050                         msginfo->to_folder->op_count--;
3051                         if (msginfo->to_folder->op_count == 0) {
3052                                 folderview_update_item(msginfo->to_folder, 0);
3053                                 changed = TRUE;
3054                         }
3055                 }
3056         }
3057         msginfo->to_folder = to_folder;
3058         if (MSG_IS_DELETED(msginfo->flags))
3059                 summaryview->deleted--;
3060         if (MSG_IS_COPY(msginfo->flags)) {
3061                 summaryview->copied--;
3062                 if (!prefs_common.immediate_exec)
3063                         msginfo->to_folder->op_count--;
3064         }
3065         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3066         if (!MSG_IS_MOVE(msginfo->flags)) {
3067                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3068                 summaryview->moved++;
3069                 changed = TRUE;
3070         }
3071         if (!prefs_common.immediate_exec) {
3072                 summary_set_row_marks(summaryview, row);
3073                 if (changed) {
3074                         msginfo->to_folder->op_count++;
3075                         if (msginfo->to_folder->op_count == 1)
3076                                 folderview_update_item(msginfo->to_folder, 0);
3077                 }
3078         }
3079
3080         debug_print(_("Message %d is set to move to %s\n"),
3081                     msginfo->msgnum, to_folder->path);
3082 }
3083
3084 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3085 {
3086         GList *cur;
3087
3088         if (!to_folder) return;
3089         if (!summaryview->folder_item ||
3090             summaryview->folder_item->folder->type == F_NEWS) return;
3091
3092         if (summary_is_locked(summaryview)) return;
3093
3094         if (summaryview->folder_item == to_folder) {
3095                 alertpanel_notice(_("Destination is same as current folder."));
3096                 return;
3097         }
3098
3099         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3100              cur != NULL; cur = cur->next)
3101                 summary_move_row_to
3102                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3103
3104         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3105
3106         if (prefs_common.immediate_exec)
3107                 summary_execute(summaryview);
3108         else {
3109                 summary_status_show(summaryview);
3110
3111                 folderview_update_item(to_folder, 0);
3112         }
3113 }
3114
3115 void summary_move_to(SummaryView *summaryview)
3116 {
3117         FolderItem *to_folder;
3118
3119         if (!summaryview->folder_item ||
3120             summaryview->folder_item->folder->type == F_NEWS) return;
3121
3122         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3123                                          FOLDER_SEL_MOVE, NULL);
3124         summary_move_selected_to(summaryview, to_folder);
3125 }
3126
3127 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3128                                 FolderItem *to_folder)
3129 {
3130         gboolean changed = FALSE;
3131         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3132         MsgInfo *msginfo;
3133
3134         g_return_if_fail(to_folder != NULL);
3135
3136         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3137         if (MSG_IS_COPY(msginfo->flags)) {
3138                 if (!prefs_common.immediate_exec) {
3139                         msginfo->to_folder->op_count--;
3140                         if (msginfo->to_folder->op_count == 0) {
3141                                 folderview_update_item(msginfo->to_folder, 0);
3142                                 changed = TRUE;
3143                         }
3144                 }
3145         }
3146         msginfo->to_folder = to_folder;
3147         if (MSG_IS_DELETED(msginfo->flags))
3148                 summaryview->deleted--;
3149         if (MSG_IS_MOVE(msginfo->flags)) {
3150                 summaryview->moved--;
3151                 if (!prefs_common.immediate_exec)
3152                         msginfo->to_folder->op_count--;
3153         }
3154         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3155         if (!MSG_IS_COPY(msginfo->flags)) {
3156                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3157                 summaryview->copied++;
3158                 changed = TRUE;
3159         }
3160         if (!prefs_common.immediate_exec) {
3161                 summary_set_row_marks(summaryview, row);
3162                 if (changed) {
3163                         msginfo->to_folder->op_count++;
3164                         if (msginfo->to_folder->op_count == 1)
3165                                 folderview_update_item(msginfo->to_folder, 0);
3166                 }
3167         }
3168
3169         debug_print(_("Message %d is set to copy to %s\n"),
3170                     msginfo->msgnum, to_folder->path);
3171 }
3172
3173 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3174 {
3175         GList *cur;
3176
3177         if (!to_folder) return;
3178         if (!summaryview->folder_item) return;
3179
3180         if (summary_is_locked(summaryview)) return;
3181
3182         if (summaryview->folder_item == to_folder) {
3183                 alertpanel_notice
3184                         (_("Destination to copy is same as current folder."));
3185                 return;
3186         }
3187
3188         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3189              cur != NULL; cur = cur->next)
3190                 summary_copy_row_to
3191                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3192
3193         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3194
3195         if (prefs_common.immediate_exec)
3196                 summary_execute(summaryview);
3197         else {
3198                 summary_status_show(summaryview);
3199
3200                 folderview_update_item(to_folder, 0);
3201         }
3202 }
3203
3204 void summary_copy_to(SummaryView *summaryview)
3205 {
3206         FolderItem *to_folder;
3207
3208         if (!summaryview->folder_item) return;
3209
3210         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3211                                          FOLDER_SEL_COPY, NULL);
3212         summary_copy_selected_to(summaryview, to_folder);
3213 }
3214
3215 void summary_add_address(SummaryView *summaryview)
3216 {
3217         MsgInfo *msginfo;
3218         gchar *from;
3219
3220         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3221                                               summaryview->selected);
3222         if (!msginfo) return;
3223
3224         Xstrdup_a(from, msginfo->from, return);
3225         eliminate_address_comment(from);
3226         extract_address(from);
3227         addressbook_add_contact(msginfo->fromname, from, NULL);
3228 }
3229
3230 void summary_select_all(SummaryView *summaryview)
3231 {
3232         if (summaryview->messages >= 500) {
3233                 STATUSBAR_PUSH(summaryview->mainwin,
3234                                _("Selecting all messages..."));
3235                 main_window_cursor_wait(summaryview->mainwin);
3236         }
3237
3238         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3239
3240         if (summaryview->messages >= 500) {
3241                 STATUSBAR_POP(summaryview->mainwin);
3242                 main_window_cursor_normal(summaryview->mainwin);
3243         }
3244 }
3245
3246 void summary_unselect_all(SummaryView *summaryview)
3247 {
3248         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3249 }
3250
3251 void summary_save_as(SummaryView *summaryview)
3252 {
3253         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3254         MsgInfo *msginfo;
3255         gchar *filename = NULL;
3256         gchar *src, *dest;
3257
3258         if (!summaryview->selected) return;
3259         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3260         if (!msginfo) return;
3261
3262         if (msginfo->subject) {
3263                 Xstrdup_a(filename, msginfo->subject, return);
3264                 subst_for_filename(filename);
3265         }
3266         dest = filesel_select_file(_("Save as"), filename);
3267         if (!dest) return;
3268         if (is_file_exist(dest)) {
3269                 AlertValue aval;
3270
3271                 aval = alertpanel(_("Overwrite"),
3272                                   _("Overwrite existing file?"),
3273                                   _("OK"), _("Cancel"), NULL);
3274                 if (G_ALERTDEFAULT != aval) return;
3275         }
3276
3277         src = procmsg_get_message_file(msginfo);
3278         if (copy_file(src, dest) < 0) {
3279                 alertpanel_error(_("Can't save the file `%s'."),
3280                                  g_basename(dest));
3281         }
3282         g_free(src);
3283 }
3284
3285 void summary_print(SummaryView *summaryview)
3286 {
3287         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3288         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3289         MsgInfo *msginfo;
3290         GList *cur;
3291         gchar *cmdline;
3292         gchar *p;
3293
3294         if (clist->selection == NULL) return;
3295
3296         cmdline = input_dialog(_("Print"),
3297                                _("Enter the print command line:\n"
3298                                  "(`%s' will be replaced with file name)"),
3299                                prefs_common.print_cmd);
3300         if (!cmdline) return;
3301         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3302             strchr(p + 2, '%')) {
3303                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3304                                  cmdline);
3305                 g_free(cmdline);
3306                 return;
3307         }
3308
3309         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3310                 msginfo = gtk_ctree_node_get_row_data
3311                         (ctree, GTK_CTREE_NODE(cur->data));
3312                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3313         }
3314
3315         g_free(cmdline);
3316 }
3317
3318 gboolean summary_execute(SummaryView *summaryview)
3319 {