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