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