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