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