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