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