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