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