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