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