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