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