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