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