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