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