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