132d285c8916674bc666530a3971205b161e0251
[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 || item->no_select ||
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         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
940         menu_set_sensitive(ifactory, "/Forward as attachment",    TRUE);
941         
942         menu_set_sensitive(ifactory, "/Open in new window", sens);
943         menu_set_sensitive(ifactory, "/View source", sens);
944         menu_set_sensitive(ifactory, "/Show all header", sens);
945         if ((summaryview->folder_item->stype == F_DRAFT) ||
946             (summaryview->folder_item->stype == F_OUTBOX) ||
947             (summaryview->folder_item->stype == F_QUEUE))
948                 menu_set_sensitive(ifactory, "/Re-edit", sens);
949
950         menu_set_sensitive(ifactory, "/Save as...", sens);
951         menu_set_sensitive(ifactory, "/Print...",   TRUE);
952
953         menu_set_sensitive(ifactory, "/Mark", TRUE);
954
955         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
956         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
957
958         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
959         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
960
961         menu_set_sensitive(ifactory, "/Select all", TRUE);
962
963         if (summaryview->folder_item->folder->account)
964                 sens = summaryview->folder_item->folder->account->protocol
965                         == A_NNTP;
966         else
967                 sens = FALSE;
968         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
969 }
970
971 void summary_select_next_unread(SummaryView *summaryview)
972 {
973         GtkCTreeNode *node;
974         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
975
976         node = summary_find_next_unread_msg(summaryview,
977                                             summaryview->selected);
978
979         if (node) {
980                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
981                 gtk_sctree_select(GTK_SCTREE(ctree), node);
982                 if (summaryview->displayed == node)
983                         summaryview->displayed = NULL;
984                 summary_display_msg(summaryview, node, FALSE);
985         } else {
986                 AlertValue val;
987
988                 val = alertpanel(_("No unread message"),
989                                  _("No unread message found. Go to next folder?"),
990                                  _("Yes"), _("No"), NULL);
991                 if (val == G_ALERTDEFAULT) {
992                         if (gtk_signal_n_emissions_by_name
993                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
994                                         gtk_signal_emit_stop_by_name
995                                                 (GTK_OBJECT(ctree),
996                                                  "key_press_event");
997                         folderview_select_next_unread(summaryview->folderview);
998                 }
999         }
1000 }
1001
1002 void summary_select_next_marked(SummaryView *summaryview)
1003 {
1004         GtkCTreeNode *node;
1005         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1006
1007         node = summary_find_next_marked_msg(summaryview,
1008                                             summaryview->selected);
1009
1010         if (!node) {
1011                 AlertValue val;
1012
1013                 val = alertpanel(_("No more marked messages"),
1014                                  _("No marked message found. "
1015                                    "Search from the beginning?"),
1016                                  _("Yes"), _("No"), NULL);
1017                 if (val != G_ALERTDEFAULT) return;
1018                 node = summary_find_next_marked_msg(summaryview,
1019                                                     NULL);
1020         }
1021         if (!node) {
1022                 alertpanel_notice(_("No marked messages."));
1023         } else {
1024                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1025                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1026                 if (summaryview->displayed == node)
1027                         summaryview->displayed = NULL;
1028                 summary_display_msg(summaryview, node, FALSE);
1029         }
1030 }
1031
1032 void summary_select_prev_marked(SummaryView *summaryview)
1033 {
1034         GtkCTreeNode *node;
1035         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1036
1037         node = summary_find_prev_marked_msg(summaryview,
1038                                             summaryview->selected);
1039
1040         if (!node) {
1041                 AlertValue val;
1042
1043                 val = alertpanel(_("No more marked messages"),
1044                                  _("No marked message found. "
1045                                    "Search from the end?"),
1046                                  _("Yes"), _("No"), NULL);
1047                 if (val != G_ALERTDEFAULT) return;
1048                 node = summary_find_prev_marked_msg(summaryview,
1049                                                     NULL);
1050         }
1051         if (!node) {
1052                 alertpanel_notice(_("No marked messages."));
1053         } else {
1054                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1055                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1056                 if (summaryview->displayed == node)
1057                         summaryview->displayed = NULL;
1058                 summary_display_msg(summaryview, node, FALSE);
1059         }
1060 }
1061
1062 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1063 {
1064         GtkCTreeNode *node;
1065         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1066
1067         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1068
1069         if (node) {
1070                 GTK_EVENTS_FLUSH();
1071                 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1072                 gtk_widget_grab_focus(GTK_WIDGET(ctree));
1073                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1074                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1075                 if (summaryview->msg_is_toggled_on) {
1076                         if (summaryview->displayed == node)
1077                                 summaryview->displayed = NULL;
1078                         summary_display_msg(summaryview, node, FALSE);
1079                 }
1080         }
1081 }
1082
1083 guint summary_get_current_msgnum(SummaryView *summaryview)
1084 {
1085         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1086         MsgInfo *msginfo;
1087
1088         if (!summaryview->selected)
1089                 return 0;
1090         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1091         return msginfo->msgnum;
1092 }
1093
1094 static GtkCTreeNode *summary_find_next_unread_msg(SummaryView *summaryview,
1095                                                   GtkCTreeNode *current_node)
1096 {
1097         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1098         GtkCTreeNode *node;
1099         MsgInfo *msginfo;
1100
1101         if (current_node)
1102                 node = current_node;
1103                 /*node = GTK_CTREE_NODE_NEXT(current_node);*/
1104         else
1105                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1106
1107         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1108                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1109                 if (MSG_IS_UNREAD(msginfo->flags)) break;
1110         }
1111
1112         return node;
1113 }
1114
1115 static GtkCTreeNode *summary_find_next_marked_msg(SummaryView *summaryview,
1116                                                   GtkCTreeNode *current_node)
1117 {
1118         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1119         GtkCTreeNode *node;
1120         MsgInfo *msginfo;
1121
1122         if (current_node)
1123                 node = GTK_CTREE_NODE_NEXT(current_node);
1124         else
1125                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1126
1127         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1128                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1129                 if (MSG_IS_MARKED(msginfo->flags)) break;
1130         }
1131
1132         return node;
1133 }
1134
1135 static GtkCTreeNode *summary_find_prev_marked_msg(SummaryView *summaryview,
1136                                                   GtkCTreeNode *current_node)
1137 {
1138         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1139         GtkCTreeNode *node;
1140         MsgInfo *msginfo;
1141
1142         if (current_node)
1143                 node = GTK_CTREE_NODE_PREV(current_node);
1144         else
1145                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1146
1147         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1148                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1149                 if (MSG_IS_MARKED(msginfo->flags)) break;
1150         }
1151
1152         return node;
1153 }
1154
1155 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1156                                                 guint msgnum)
1157 {
1158         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1159         GtkCTreeNode *node;
1160         MsgInfo *msginfo;
1161
1162         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1163
1164         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1165                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1166                 if (msginfo->msgnum == msgnum) break;
1167         }
1168
1169         return node;
1170 }
1171
1172 #if 0
1173 static GtkCTreeNode *summary_find_prev_unread_msg(SummaryView *summaryview,
1174                                                   GtkCTreeNode *current_node)
1175 {
1176         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1177         GtkCTreeNode *node;
1178         MsgInfo *msginfo;
1179
1180         if (current_node)
1181                 node = current_node;
1182                 /*node = GTK_CTREE_NODE_PREV(current_node);*/
1183         else
1184                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1185
1186         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1187                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1188                 if (MSG_IS_UNREAD(msginfo->flags)) break;
1189         }
1190
1191         return node;
1192 }
1193 #endif
1194
1195 static guint attract_hash_func(gconstpointer key)
1196 {
1197         gchar *str;
1198         gchar *p;
1199         guint h;
1200
1201         Xstrdup_a(str, (const gchar *)key, return 0);
1202         trim_subject(str);
1203
1204         p = str;
1205         h = *p;
1206
1207         if (h) {
1208                 for (p += 1; *p != '\0'; p++)
1209                         h = (h << 5) - h + *p;
1210         }
1211
1212         return h;
1213 }
1214
1215 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1216 {
1217         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1218 }
1219
1220 void summary_attract_by_subject(SummaryView *summaryview)
1221 {
1222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1223         GtkCList *clist = GTK_CLIST(ctree);
1224         GtkCTreeNode *src_node;
1225         GtkCTreeNode *dst_node, *sibling;
1226         GtkCTreeNode *tmp;
1227         MsgInfo *src_msginfo, *dst_msginfo;
1228         GHashTable *subject_table;
1229
1230         debug_print(_("Attracting messages by subject..."));
1231         STATUSBAR_PUSH(summaryview->mainwin,
1232                        _("Attracting messages by subject..."));
1233
1234         main_window_cursor_wait(summaryview->mainwin);
1235         gtk_clist_freeze(clist);
1236
1237         subject_table = g_hash_table_new(attract_hash_func,
1238                                          attract_compare_func);
1239
1240         for (src_node = GTK_CTREE_NODE(clist->row_list);
1241              src_node != NULL;
1242              src_node = tmp) {
1243                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1244                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1245                 if (!src_msginfo) continue;
1246                 if (!src_msginfo->subject) continue;
1247
1248                 /* find attracting node */
1249                 dst_node = g_hash_table_lookup(subject_table,
1250                                                src_msginfo->subject);
1251
1252                 if (dst_node) {
1253                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1254
1255                         /* if the time difference is more than 20 days,
1256                            don't attract */
1257                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1258                             > 60 * 60 * 24 * 20)
1259                                 continue;
1260
1261                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1262                         if (src_node != sibling)
1263                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1264                 }
1265
1266                 g_hash_table_insert(subject_table,
1267                                     src_msginfo->subject, src_node);
1268         }
1269
1270         g_hash_table_destroy(subject_table);
1271
1272         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1273
1274         gtk_clist_thaw(clist);
1275
1276         debug_print(_("done.\n"));
1277         STATUSBAR_POP(summaryview->mainwin);
1278
1279         main_window_cursor_normal(summaryview->mainwin);
1280 }
1281
1282 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1283                                       gpointer data)
1284 {
1285         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1286
1287         if (msginfo)
1288                 procmsg_msginfo_free(msginfo);
1289 }
1290
1291 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1292                                    gpointer data)
1293 {
1294         SummaryView *summaryview = data;
1295         MsgInfo *msginfo;
1296
1297         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1298
1299         if (MSG_IS_NEW(msginfo->flags))
1300                 summaryview->newmsgs++;
1301         if (MSG_IS_UNREAD(msginfo->flags))
1302                 summaryview->unread++;
1303         if (MSG_IS_DELETED(msginfo->flags))
1304                 summaryview->deleted++;
1305
1306         summaryview->messages++;
1307         summaryview->total_size += msginfo->size;
1308
1309         summary_set_row_marks(summaryview, node);
1310 }
1311
1312 static void summary_update_status(SummaryView *summaryview)
1313 {
1314         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1315         GtkCTreeNode *node;
1316         MsgInfo *msginfo;
1317
1318         summaryview->newmsgs = summaryview->unread =
1319         summaryview->messages = summaryview->total_size =
1320         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1321
1322         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1323              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1324                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1325
1326                 if (MSG_IS_NEW(msginfo->flags))
1327                         summaryview->newmsgs++;
1328                 if (MSG_IS_UNREAD(msginfo->flags))
1329                         summaryview->unread++;
1330                 if (MSG_IS_DELETED(msginfo->flags))
1331                         summaryview->deleted++;
1332                 if (MSG_IS_MOVE(msginfo->flags))
1333                         summaryview->moved++;
1334                 if (MSG_IS_COPY(msginfo->flags))
1335                         summaryview->copied++;
1336                 summaryview->messages++;
1337                 summaryview->total_size += msginfo->size;
1338         }
1339 }
1340
1341 static void summary_status_show(SummaryView *summaryview)
1342 {
1343         gchar *str;
1344         gchar *del, *mv, *cp;
1345         gchar *sel;
1346         gchar *spc;
1347         GList *rowlist, *cur;
1348         guint n_selected = 0;
1349         off_t sel_size = 0;
1350         MsgInfo *msginfo;
1351
1352         if (!summaryview->folder_item) {
1353                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1354                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1355                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1356                 return;
1357         }
1358
1359         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1360         for (cur = rowlist; cur != NULL; cur = cur->next) {
1361                 msginfo = gtk_ctree_node_get_row_data
1362                         (GTK_CTREE(summaryview->ctree),
1363                          GTK_CTREE_NODE(cur->data));
1364                 sel_size += msginfo->size;
1365                 n_selected++;
1366         }
1367
1368         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1369                       summaryview->folder_item &&
1370                       summaryview->folder_item->folder->type == F_NEWS
1371                       ? g_basename(summaryview->folder_item->path)
1372                       : summaryview->folder_item->path);
1373
1374         if (summaryview->deleted)
1375                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1376         else
1377                 del = g_strdup("");
1378         if (summaryview->moved)
1379                 mv = g_strdup_printf(_("%s%d moved"),
1380                                      summaryview->deleted ? _(", ") : "",
1381                                      summaryview->moved);
1382         else
1383                 mv = g_strdup("");
1384         if (summaryview->copied)
1385                 cp = g_strdup_printf(_("%s%d copied"),
1386                                      summaryview->deleted ||
1387                                      summaryview->moved ? _(", ") : "",
1388                                      summaryview->copied);
1389         else
1390                 cp = g_strdup("");
1391
1392         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1393                 spc = "    ";
1394         else
1395                 spc = "";
1396
1397         if (n_selected)
1398                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1399         else
1400                 sel = g_strdup("");
1401         str = g_strconcat(n_selected ? itos(n_selected) : "",
1402                           n_selected ? _(" item(s) selected") : "",
1403                           sel, spc, del, mv, cp, NULL);
1404         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1405         g_free(str);
1406         g_free(sel);
1407         g_free(del);
1408         g_free(mv);
1409         g_free(cp);
1410
1411         if (summaryview->folder_item &&
1412             summaryview->folder_item->folder->type == F_MH) {
1413                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1414                                       summaryview->newmsgs,
1415                                       summaryview->unread,
1416                                       summaryview->messages,
1417                                       to_human_readable(summaryview->total_size));
1418         } else {
1419                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1420                                       summaryview->newmsgs,
1421                                       summaryview->unread,
1422                                       summaryview->messages);
1423         }
1424         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1425         g_free(str);
1426
1427         folderview_update_msg_num(summaryview->folderview,
1428                                   summaryview->folderview->opened,
1429                                   summaryview->newmsgs,
1430                                   summaryview->unread,
1431                                   summaryview->messages);
1432 }
1433
1434 void summary_sort(SummaryView *summaryview, SummarySortType type)
1435 {
1436         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1437         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1438         GtkCListCompareFunc cmp_func;
1439
1440         if (!summaryview->folder_item)
1441                 return;
1442
1443         switch (type) {
1444         case SORT_BY_NUMBER:
1445                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1446                 break;
1447         case SORT_BY_SIZE:
1448                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1449                 break;
1450         case SORT_BY_DATE:
1451                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1452                 break;
1453         case SORT_BY_FROM:
1454                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1455                 break;
1456         case SORT_BY_SUBJECT:
1457                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1458                 break;
1459         case SORT_BY_SCORE:
1460                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1461                 break;
1462         default:
1463                 return;
1464         }
1465
1466         debug_print(_("Sorting summary..."));
1467         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1468
1469         main_window_cursor_wait(summaryview->mainwin);
1470
1471         gtk_clist_set_compare_func(clist, cmp_func);
1472
1473         /* toggle sort type if the same column is selected */
1474         if (summaryview->sort_mode == type)
1475                 summaryview->sort_type =
1476                         summaryview->sort_type == GTK_SORT_ASCENDING
1477                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1478         else
1479                 summaryview->sort_type = GTK_SORT_ASCENDING;
1480         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1481         summaryview->sort_mode = type;
1482
1483         gtk_ctree_sort_node(ctree, NULL);
1484
1485         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1486         /*gtkut_ctree_set_focus_row(ctree, summaryview->selected);*/
1487
1488         prefs_folder_item_set_config(summaryview->folder_item,
1489                                      summaryview->sort_type,
1490                                      summaryview->sort_mode);
1491         prefs_folder_item_save_config(summaryview->folder_item);
1492
1493         debug_print(_("done.\n"));
1494         STATUSBAR_POP(summaryview->mainwin);
1495
1496         main_window_cursor_normal(summaryview->mainwin);
1497 }
1498
1499 static GtkCTreeNode * subject_table_lookup(GHashTable *subject_table,
1500                                            gchar * subject)
1501 {
1502         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1503                 return g_hash_table_lookup(subject_table, subject + 4);
1504         else
1505                 return g_hash_table_lookup(subject_table, subject);
1506 }
1507
1508 static void subject_table_insert(GHashTable *subject_table, gchar * subject,
1509                                  GtkCTreeNode * node)
1510 {
1511         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1512                 g_hash_table_insert(subject_table, subject + 4, node);
1513         else
1514                 g_hash_table_insert(subject_table, subject, node);
1515 }
1516
1517 static void summary_set_ctree_from_list(SummaryView *summaryview,
1518                                         GSList *mlist)
1519 {
1520         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1521         MsgInfo *msginfo;
1522         MsgInfo *parentinfo;
1523         MsgInfo *cur_msginfo;
1524         GtkCTreeNode *node, *parent;
1525         gchar *text[N_SUMMARY_COLS];
1526         GHashTable *msgid_table;
1527         GHashTable *subject_table;
1528         GSList * cur;
1529         GtkCTreeNode *cur_parent;
1530
1531         if (!mlist) return;
1532
1533         debug_print(_("\tSetting summary from message data..."));
1534         STATUSBAR_PUSH(summaryview->mainwin,
1535                        _("Setting summary from message data..."));
1536
1537         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1538         summaryview->msgid_table = msgid_table;
1539         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
1540         summaryview->subject_table = subject_table;
1541
1542         if (prefs_common.use_addr_book)
1543                 start_address_completion();
1544         
1545         main_window_set_thread_option(summaryview->mainwin);
1546
1547         for (cur = mlist ; cur != NULL; cur = cur->next) {
1548                 msginfo = (MsgInfo *)cur->data;
1549                 msginfo->threadscore = msginfo->score;
1550         }
1551
1552         if (global_scoring || summaryview->folder_item->prefs->scoring) {
1553                 summaryview->important_score = prefs_common.important_score;
1554                 if (summaryview->folder_item->prefs->important_score >
1555                     summaryview->important_score)
1556                         summaryview->important_score =
1557                                 summaryview->folder_item->prefs->important_score;
1558         }
1559         
1560         /*      if (prefs_common.enable_thread) { */
1561         if (summaryview->folder_item->prefs->enable_thread) {
1562                 for (; mlist != NULL; mlist = mlist->next) {
1563                         msginfo = (MsgInfo *)mlist->data;
1564                         parent = NULL;
1565
1566                         summary_set_header(text, msginfo);
1567
1568                         /* search parent node for threading */
1569                         if (msginfo->inreplyto && *msginfo->inreplyto) {
1570                                 parent = g_hash_table_lookup
1571                                         (msgid_table, msginfo->inreplyto);
1572                         }
1573                         if (parent == NULL && msginfo->subject) {
1574                                 parent = subject_table_lookup
1575                                         (subject_table, msginfo->subject);
1576                         }
1577
1578                         node = gtk_ctree_insert_node
1579                                 (ctree, parent, NULL, text, 2,
1580                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1581                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1582
1583                         summary_set_marks_func(ctree, node, summaryview);
1584
1585                         /* preserve previous node if the message is
1586                            duplicated */
1587                         if (msginfo->msgid && *msginfo->msgid &&
1588                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1589                             == NULL)
1590                                 g_hash_table_insert(msgid_table,
1591                                                     msginfo->msgid, node);
1592                         if (msginfo->subject &&
1593                             subject_table_lookup(subject_table,
1594                                                  msginfo->subject) == NULL) {
1595                                 subject_table_insert(subject_table,
1596                                                      msginfo->subject, node);
1597                         }
1598
1599                         cur_parent = parent;
1600                         cur_msginfo = msginfo;
1601                         while (cur_parent != NULL) {
1602                                 parentinfo = gtk_ctree_node_get_row_data(ctree, cur_parent);
1603
1604                                 if (!parentinfo)
1605                                         break;
1606                                 
1607                                 if (parentinfo->threadscore <
1608                                     cur_msginfo->threadscore) {
1609                                         gchar * s;
1610                                         parentinfo->threadscore =
1611                                                 cur_msginfo->threadscore;
1612 #if 0
1613                                         s = itos(parentinfo->threadscore);
1614                                         gtk_ctree_node_set_text(ctree, cur_parent, S_COL_SCORE, s);
1615 #endif
1616                                 }
1617                                 else break;
1618                                 
1619                                 cur_msginfo = parentinfo;
1620                                 if (cur_msginfo->inreplyto &&
1621                                     *cur_msginfo->inreplyto) {
1622                                         cur_parent = g_hash_table_lookup(msgid_table, cur_msginfo->inreplyto);
1623                                 }
1624                                 if (cur_parent == NULL && cur_msginfo->subject) {
1625                                         cur_parent = subject_table_lookup(subject_table, cur_msginfo->subject);
1626                                 }
1627                         }
1628                 }
1629
1630                 /* complete the thread */
1631                 summary_thread_build(summaryview);
1632         } else {
1633                 for (; mlist != NULL; mlist = mlist->next) {
1634                         msginfo = (MsgInfo *)mlist->data;
1635
1636                         summary_set_header(text, msginfo);
1637
1638                         node = gtk_ctree_insert_node
1639                                 (ctree, NULL, NULL, text, 2,
1640                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1641                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1642                         summary_set_marks_func(ctree, node, summaryview);
1643
1644                         if (msginfo->msgid && *msginfo->msgid &&
1645                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1646                             == NULL)
1647                                 g_hash_table_insert(msgid_table,
1648                                                     msginfo->msgid, node);
1649
1650                         if (msginfo->subject &&
1651                             subject_table_lookup(subject_table,
1652                                                  msginfo->subject) == NULL)
1653                                 subject_table_insert(subject_table,
1654                                                      msginfo->subject, node);
1655                 }
1656         }
1657
1658         if (prefs_common.enable_hscrollbar) {
1659                 gint optimal_width;
1660
1661                 optimal_width = gtk_clist_optimal_column_width
1662                         (GTK_CLIST(ctree), S_COL_SUBJECT);
1663                 gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
1664                                            optimal_width);
1665         }
1666
1667         if (prefs_common.use_addr_book)
1668                 end_address_completion();
1669
1670         debug_print(_("done.\n"));
1671         STATUSBAR_POP(summaryview->mainwin);
1672         if (debug_mode) {
1673                 debug_print("\tmsgid hash table size = %d\n",
1674                             g_hash_table_size(msgid_table));
1675                 debug_print("\tsubject hash table size = %d\n",
1676                             g_hash_table_size(subject_table));
1677         }
1678 }
1679
1680 struct wcachefp
1681 {
1682         FILE *cache_fp;
1683         FILE *mark_fp;
1684 };
1685
1686 gint summary_write_cache(SummaryView *summaryview)
1687 {
1688         struct wcachefp fps;
1689         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1690         gint ver = CACHE_VERSION;
1691         gchar *buf;
1692         gchar *cachefile, *markfile;
1693         GSList * cur;
1694
1695         if (!summaryview->folder_item || !summaryview->folder_item->path)
1696                 return -1;
1697
1698         if (summaryview->folder_item->folder->update_mark != NULL)
1699                 summaryview->folder_item->folder->update_mark(summaryview->folder_item->folder, summaryview->folder_item);
1700
1701         cachefile = folder_item_get_cache_file(summaryview->folder_item);
1702         g_return_val_if_fail(cachefile != NULL, -1);
1703         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
1704                 FILE_OP_ERROR(cachefile, "fopen");
1705                 g_free(cachefile);
1706                 return -1;
1707         }
1708         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
1709                 FILE_OP_ERROR(cachefile, "chmod");
1710         g_free(cachefile);
1711
1712         markfile = folder_item_get_mark_file(summaryview->folder_item);
1713         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
1714                 FILE_OP_ERROR(markfile, "fopen");
1715                 fclose(fps.cache_fp);
1716                 g_free(markfile);
1717                 return -1;
1718         }
1719         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
1720                 FILE_OP_ERROR(markfile, "chmod");
1721         g_free(markfile);
1722
1723         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
1724                               summaryview->folder_item->path);
1725         debug_print(buf);
1726         STATUSBAR_PUSH(summaryview->mainwin, buf);
1727         g_free(buf);
1728
1729         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
1730         ver = MARK_VERSION;
1731         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
1732
1733         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
1734
1735         for(cur = summaryview->killed_messages ; cur != NULL ;
1736             cur = g_slist_next(cur)) {
1737                 MsgInfo *msginfo = (MsgInfo *) cur->data;
1738
1739                 procmsg_write_cache(msginfo, fps.cache_fp);
1740                 procmsg_write_flags(msginfo, fps.mark_fp);
1741         }
1742
1743         fclose(fps.cache_fp);
1744         fclose(fps.mark_fp);
1745
1746         debug_print(_("done.\n"));
1747         STATUSBAR_POP(summaryview->mainwin);
1748
1749         return 0;
1750 }
1751
1752 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
1753                                      gpointer data)
1754 {
1755         struct wcachefp *fps = data;
1756         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1757
1758         if (msginfo == NULL) return;
1759
1760         procmsg_write_cache(msginfo, fps->cache_fp);
1761         procmsg_write_flags(msginfo, fps->mark_fp);
1762 }
1763
1764 static void summary_set_header(gchar *text[], MsgInfo *msginfo)
1765 {
1766         static gchar date_modified[80];
1767         static gchar *to = NULL;
1768         static gchar *from_name = NULL;
1769         static gchar col_number[11];
1770         static gchar col_score[11];
1771
1772         text[S_COL_MARK]   = NULL;
1773         text[S_COL_UNREAD] = NULL;
1774         text[S_COL_MIME]   = NULL;
1775         text[S_COL_NUMBER] = itos_buf(col_number, msginfo->msgnum);
1776         text[S_COL_SIZE]   = to_human_readable(msginfo->size);
1777 #if 0
1778         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->threadscore);
1779 #else
1780         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->score);
1781 #endif
1782
1783         if (msginfo->date_t) {
1784                 procheader_date_get_localtime(date_modified,
1785                                               sizeof(date_modified),
1786                                               msginfo->date_t);
1787                 text[S_COL_DATE] = date_modified;
1788         } else if (msginfo->date)
1789                 text[S_COL_DATE] = msginfo->date;
1790         else
1791                 text[S_COL_DATE] = _("(No Date)");
1792
1793         text[S_COL_FROM] = msginfo->fromname ? msginfo->fromname :
1794                 _("(No From)");
1795         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
1796             !MSG_IS_NEWS(msginfo->flags)) {
1797                 gchar *from;
1798
1799                 Xalloca(from, strlen(msginfo->from) + 1, return);
1800                 strcpy(from, msginfo->from);
1801                 extract_address(from);
1802                 if (account_find_mail_from_address(from)) {
1803                         g_free(to);
1804                         to = g_strconcat("-->", msginfo->to, NULL);
1805                         text[S_COL_FROM] = to;
1806                 }
1807         }
1808
1809         if ((text[S_COL_FROM] != to) && prefs_common.use_addr_book &&
1810             msginfo->from) {
1811                 gint count;
1812                 gchar *from;
1813   
1814                 Xalloca(from, strlen(msginfo->from) + 1, return);
1815                 strcpy(from, msginfo->from);
1816                 extract_address(from);
1817                 count = complete_address(from);
1818                 if (count > 1) {
1819                         g_free(from_name);
1820                         from = get_complete_address(1);
1821                         from_name = procheader_get_fromname(from);
1822                         g_free(from);
1823                         text[S_COL_FROM] = from_name;
1824                 }
1825         }
1826
1827         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
1828                 _("(No Subject)");
1829 }
1830
1831 #define CHANGE_FLAGS(msginfo) \
1832 { \
1833 if (msginfo->folder->folder->change_flags != NULL) \
1834 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
1835                                       msginfo->folder, \
1836                                       msginfo); \
1837 }
1838
1839 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
1840                                 gboolean new_window)
1841 {
1842         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1843         MsgInfo *msginfo;
1844         gchar *filename;
1845         static gboolean lock = FALSE;
1846
1847         if (!new_window && summaryview->displayed == row) return;
1848         g_return_if_fail(row != NULL);
1849
1850         if (lock) return;
1851         lock = TRUE;
1852
1853         STATUSBAR_POP(summaryview->mainwin);
1854
1855         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1856
1857         filename = procmsg_get_message_file(msginfo);
1858         if (!filename) {
1859                 lock = FALSE;
1860                 return;
1861         }
1862         g_free(filename);
1863
1864         if (MSG_IS_NEW(msginfo->flags))
1865                 summaryview->newmsgs--;
1866         if (MSG_IS_UNREAD(msginfo->flags))
1867                 summaryview->unread--;
1868         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
1869                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1870
1871                 CHANGE_FLAGS(msginfo);
1872
1873                 summary_set_row_marks(summaryview, row);
1874                 gtk_clist_thaw(GTK_CLIST(ctree));
1875                 summary_status_show(summaryview);
1876         }
1877
1878         if (new_window) {
1879                 MessageView *msgview;
1880
1881                 msgview = messageview_create_with_new_window();
1882                 messageview_show(msgview, msginfo);
1883         } else {
1884                 MessageView *msgview;
1885
1886                 msgview = summaryview->messageview;
1887
1888                 summaryview->displayed = row;
1889                 if (!summaryview->msg_is_toggled_on)
1890                         summary_toggle_view(summaryview);
1891                 messageview_show(msgview, msginfo);
1892                 if (msgview->type == MVIEW_TEXT ||
1893                     (msgview->type == MVIEW_MIME &&
1894                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
1895                         gtk_widget_grab_focus(summaryview->ctree);
1896                 GTK_EVENTS_FLUSH();
1897                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
1898         }
1899
1900         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
1901                 header_window_show(summaryview->headerwin, msginfo);
1902
1903         lock = FALSE;
1904 }
1905
1906 void summary_redisplay_msg(SummaryView *summaryview)
1907 {
1908         GtkCTreeNode *node;
1909
1910         if (summaryview->displayed) {
1911                 node = summaryview->displayed;
1912                 summaryview->displayed = NULL;
1913                 summary_display_msg(summaryview, node, FALSE);
1914         }
1915 }
1916
1917 void summary_open_msg(SummaryView *summaryview)
1918 {
1919         if (!summaryview->selected) return;
1920
1921         summary_display_msg(summaryview, summaryview->selected, TRUE);
1922 }
1923
1924 void summary_view_source(SummaryView * summaryview)
1925 {
1926         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1927         MsgInfo *msginfo;
1928         SourceWindow *srcwin;
1929
1930         if (!summaryview->selected) return;
1931
1932         srcwin = source_window_create();
1933         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1934         source_window_show_msg(srcwin, msginfo);
1935         source_window_show(srcwin);
1936 }
1937
1938 void summary_reedit(SummaryView *summaryview)
1939 {
1940         MsgInfo *msginfo;
1941
1942         if (!summaryview->selected) return;
1943         if (!summaryview->folder_item ||
1944             (summaryview->folder_item->stype != F_DRAFT &&
1945              summaryview->folder_item->stype != F_OUTBOX &&
1946              summaryview->folder_item->stype != F_QUEUE)) return;
1947
1948         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
1949                                               summaryview->selected);
1950         if (!msginfo) return;
1951
1952         compose_reedit(msginfo);
1953 }
1954
1955 void summary_step(SummaryView *summaryview, GtkScrollType type)
1956 {
1957         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1958
1959         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
1960                                 type, 0.0);
1961
1962         if (summaryview->msg_is_toggled_on)
1963                 summary_display_msg(summaryview, summaryview->selected, FALSE);
1964 }
1965
1966 static void summary_toggle_view(SummaryView *summaryview)
1967 {
1968         MainWindow *mainwin = summaryview->mainwin;
1969         union CompositeWin *cwin = &mainwin->win;
1970         GtkWidget *vpaned = NULL;
1971         GtkWidget *container = NULL;
1972
1973         switch (mainwin->type) {
1974         case SEPARATE_NONE:
1975                 vpaned = cwin->sep_none.vpaned;
1976                 container = cwin->sep_none.hpaned;
1977                 break;
1978         case SEPARATE_FOLDER:
1979                 vpaned = cwin->sep_folder.vpaned;
1980                 container = mainwin->vbox_body;
1981                 break;
1982         case SEPARATE_MESSAGE:
1983         case SEPARATE_BOTH:
1984                 return;
1985         }
1986
1987         if (vpaned->parent != NULL) {
1988                 summaryview->msg_is_toggled_on = FALSE;
1989                 summaryview->displayed = NULL;
1990                 gtk_widget_ref(vpaned);
1991                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
1992                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1993                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1994                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1995         } else {
1996                 summaryview->msg_is_toggled_on = TRUE;
1997                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1998                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1999                 gtk_widget_unref(vpaned);
2000                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2001                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2002         }
2003
2004         gtk_widget_grab_focus(summaryview->ctree);
2005 }
2006
2007 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2008 {
2009         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2010         MsgInfo *msginfo;
2011         MsgFlags flags;
2012
2013         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2014         if (!msginfo) return;
2015
2016         flags = msginfo->flags;
2017
2018         gtk_ctree_node_set_foreground(ctree, row, &summaryview->color_normal);
2019
2020         /* set new/unread column */
2021         if (MSG_IS_NEW(flags)) {
2022                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2023                                           newxpm, newxpmmask);
2024         } else if (MSG_IS_UNREAD(flags)) {
2025                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2026                                           unreadxpm, unreadxpmmask);
2027         } else if (MSG_IS_REPLIED(flags)) {
2028                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2029                                           repliedxpm, repliedxpmmask);
2030         } else if (MSG_IS_FORWARDED(flags)) {
2031                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2032                                           forwardedxpm, forwardedxpmmask);
2033         } else {
2034                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
2035         }
2036
2037         /* set mark column */
2038         if (MSG_IS_DELETED(flags)) {
2039                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2040                                           deletedxpm, deletedxpmmask);
2041                 gtk_ctree_node_set_foreground(ctree, row,
2042                                               &summaryview->color_dim);
2043         } else if (MSG_IS_MARKED(flags)) {
2044                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2045                                           markxpm, markxpmmask);
2046         } else if (MSG_IS_MOVE(flags)) {
2047                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "o");
2048                 gtk_ctree_node_set_foreground(ctree, row,
2049                                               &summaryview->color_marked);
2050         } else if (MSG_IS_COPY(flags)) {
2051                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
2052                 gtk_ctree_node_set_foreground(ctree, row,
2053                                               &summaryview->color_marked);
2054         }
2055         else if ((global_scoring ||
2056                   summaryview->folder_item->prefs->scoring) &&
2057                  (msginfo->score >= summaryview->important_score) &&
2058                  ((msginfo->flags &
2059                    (MSG_MARKED | MSG_MOVE | MSG_COPY)) == 0)) {
2060                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2061                 gtk_ctree_node_set_foreground(ctree, row,
2062                                               &summaryview->color_important);
2063         } else {
2064                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
2065         }
2066
2067         if (MSG_IS_MIME(flags)) {
2068                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
2069                                           clipxpm, clipxpmmask);
2070         } else {
2071                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
2072         }
2073 }
2074
2075 void summary_set_marks_selected(SummaryView *summaryview)
2076 {
2077         summary_set_row_marks(summaryview, summaryview->selected);
2078 }
2079
2080 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2081 {
2082         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2083         MsgInfo *msginfo;
2084
2085         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2086         msginfo->to_folder = NULL;
2087         if (MSG_IS_DELETED(msginfo->flags))
2088                 summaryview->deleted--;
2089         if (MSG_IS_MOVE(msginfo->flags))
2090                 summaryview->moved--;
2091         if (MSG_IS_COPY(msginfo->flags))
2092                 summaryview->copied--;
2093         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
2094         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
2095
2096         CHANGE_FLAGS(msginfo);
2097
2098         summary_set_row_marks(summaryview, row);
2099         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
2100 }
2101
2102 void summary_mark(SummaryView *summaryview)
2103 {
2104         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2105         GList *cur;
2106
2107         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2108                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2109
2110         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2111         summary_status_show(summaryview);
2112 }
2113
2114 static void summary_mark_row_as_read(SummaryView *summaryview,
2115                                      GtkCTreeNode *row)
2116 {
2117         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2118         MsgInfo *msginfo;
2119
2120         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2121         if (MSG_IS_NEW(msginfo->flags))
2122                 summaryview->newmsgs--;
2123         if (MSG_IS_UNREAD(msginfo->flags))
2124                 summaryview->unread--;
2125         if (MSG_IS_NEW(msginfo->flags) ||
2126             MSG_IS_UNREAD(msginfo->flags)) {
2127                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2128
2129                 CHANGE_FLAGS(msginfo);
2130                 
2131                 summary_set_row_marks(summaryview, row);
2132                 debug_print(_("Message %d is marked as read\n"),
2133                             msginfo->msgnum);
2134         }
2135 }
2136
2137 void summary_mark_as_read(SummaryView *summaryview)
2138 {
2139         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2140         GList *cur;
2141
2142         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2143                 summary_mark_row_as_read(summaryview,
2144                                          GTK_CTREE_NODE(cur->data));
2145
2146         summary_status_show(summaryview);
2147 }
2148
2149 static void summary_mark_row_as_unread(SummaryView *summaryview,
2150                                        GtkCTreeNode *row)
2151 {
2152         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2153         MsgInfo *msginfo;
2154
2155         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2156         if (MSG_IS_DELETED(msginfo->flags)) {
2157                 msginfo->to_folder = NULL;
2158                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2159                 summaryview->deleted--;
2160         }
2161         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2162         if (!MSG_IS_UNREAD(msginfo->flags)) {
2163                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
2164                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2165                                           unreadxpm, unreadxpmmask);
2166                 summaryview->unread++;
2167                 debug_print(_("Message %d is marked as unread\n"),
2168                             msginfo->msgnum);
2169         }
2170
2171         CHANGE_FLAGS(msginfo);
2172
2173         summary_set_row_marks(summaryview, row);
2174 }
2175
2176 void summary_mark_as_unread(SummaryView *summaryview)
2177 {
2178         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2179         GList *cur;
2180
2181         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2182                 summary_mark_row_as_unread(summaryview,
2183                                            GTK_CTREE_NODE(cur->data));
2184
2185         summary_status_show(summaryview);
2186 }
2187
2188 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2189 {
2190         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2191         MsgInfo *msginfo;
2192
2193         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2194
2195         if (MSG_IS_DELETED(msginfo->flags)) return;
2196
2197         msginfo->to_folder = NULL;
2198         if (MSG_IS_MOVE(msginfo->flags))
2199                 summaryview->moved--;
2200         if (MSG_IS_COPY(msginfo->flags))
2201                 summaryview->copied--;
2202         MSG_UNSET_FLAGS(msginfo->flags,
2203                         MSG_MARKED |
2204                         MSG_MOVE |
2205                         MSG_COPY);
2206         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
2207
2208         CHANGE_FLAGS(msginfo);
2209
2210         summaryview->deleted++;
2211
2212         if (!prefs_common.immediate_exec)
2213                 summary_set_row_marks(summaryview, row);
2214
2215         debug_print(_("Message %s/%d is set to delete\n"),
2216                     msginfo->folder->path, msginfo->msgnum);
2217 }
2218
2219 void summary_delete(SummaryView *summaryview)
2220 {
2221         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2222         GList *cur;
2223
2224         if (!summaryview->folder_item ||
2225             summaryview->folder_item->folder->type == F_NEWS) return;
2226
2227         /* if current folder is trash, don't delete */
2228         if (summaryview->folder_item->stype == F_TRASH) {
2229                 alertpanel_notice(_("Current folder is Trash."));
2230                 return;
2231         }
2232
2233         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2234                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
2235
2236         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2237
2238         if (prefs_common.immediate_exec)
2239                 summary_execute(summaryview);
2240         else
2241                 summary_status_show(summaryview);
2242 }
2243
2244 void summary_delete_duplicated(SummaryView *summaryview)
2245 {
2246         if (!summaryview->folder_item ||
2247             summaryview->folder_item->folder->type == F_NEWS) return;
2248         if (summaryview->folder_item->stype == F_TRASH) return;
2249
2250         main_window_cursor_wait(summaryview->mainwin);
2251         debug_print(_("Deleting duplicated messages..."));
2252         STATUSBAR_PUSH(summaryview->mainwin,
2253                        _("Deleting duplicated messages..."));
2254
2255         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2256                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2257                                 summaryview);
2258
2259         if (prefs_common.immediate_exec)
2260                 summary_execute(summaryview);
2261         else
2262                 summary_status_show(summaryview);
2263
2264         debug_print(_("done.\n"));
2265         STATUSBAR_POP(summaryview->mainwin);
2266         main_window_cursor_normal(summaryview->mainwin);
2267 }
2268
2269 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2270                                            SummaryView *summaryview)
2271 {
2272         GtkCTreeNode *found;
2273         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2274
2275         if (!msginfo->msgid || !*msginfo->msgid) return;
2276
2277         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2278
2279         if (found && found != node)
2280                 summary_delete_row(summaryview, node);
2281 }
2282
2283 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2284 {
2285         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2286         MsgInfo *msginfo;
2287
2288         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2289         msginfo->to_folder = NULL;
2290         if (MSG_IS_DELETED(msginfo->flags))
2291                 summaryview->deleted--;
2292         if (MSG_IS_MOVE(msginfo->flags))
2293                 summaryview->moved--;
2294         if (MSG_IS_COPY(msginfo->flags))
2295                 summaryview->copied--;
2296         MSG_UNSET_FLAGS(msginfo->flags,
2297                         MSG_MARKED |
2298                         MSG_DELETED |
2299                         MSG_MOVE |
2300                         MSG_COPY);
2301
2302         CHANGE_FLAGS(msginfo);
2303
2304         summary_set_row_marks(summaryview, row);
2305
2306         debug_print(_("Message %s/%d is unmarked\n"),
2307                     msginfo->folder->path, msginfo->msgnum);
2308 }
2309
2310 void summary_unmark(SummaryView *summaryview)
2311 {
2312         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2313         GList *cur;
2314
2315         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2316              cur = cur->next)
2317                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2318
2319         summary_status_show(summaryview);
2320 }
2321
2322 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2323                                 FolderItem *to_folder)
2324 {
2325         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2326         MsgInfo *msginfo;
2327
2328         g_return_if_fail(to_folder != NULL);
2329
2330         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2331         msginfo->to_folder = to_folder;
2332         if (MSG_IS_DELETED(msginfo->flags))
2333                 summaryview->deleted--;
2334         MSG_UNSET_FLAGS(msginfo->flags,
2335                         MSG_MARKED | MSG_DELETED | MSG_COPY);
2336         if (!MSG_IS_MOVE(msginfo->flags)) {
2337                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
2338                 summaryview->moved++;
2339         }
2340
2341         if (!prefs_common.immediate_exec)
2342                 summary_set_row_marks(summaryview, row);
2343
2344         debug_print(_("Message %d is set to move to %s\n"),
2345                     msginfo->msgnum, to_folder->path);
2346 }
2347
2348 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2349 {
2350         GList *cur;
2351
2352         if (!to_folder) return;
2353         if (!summaryview->folder_item ||
2354             summaryview->folder_item->folder->type == F_NEWS) return;
2355         if (summaryview->folder_item == to_folder) {
2356                 alertpanel_notice(_("Destination is same as current folder."));
2357                 return;
2358         }
2359
2360         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2361              cur != NULL; cur = cur->next)
2362                 summary_move_row_to
2363                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2364
2365         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2366
2367         if (prefs_common.immediate_exec)
2368                 summary_execute(summaryview);
2369         else
2370                 summary_status_show(summaryview);
2371 }
2372
2373 void summary_move_to(SummaryView *summaryview)
2374 {
2375         FolderItem *to_folder;
2376
2377         if (!summaryview->folder_item ||
2378             summaryview->folder_item->folder->type == F_NEWS) return;
2379
2380         to_folder = foldersel_folder_sel(NULL, NULL);
2381         summary_move_selected_to(summaryview, to_folder);
2382 }
2383
2384 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2385                                 FolderItem *to_folder)
2386 {
2387         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2388         MsgInfo *msginfo;
2389
2390         g_return_if_fail(to_folder != NULL);
2391
2392         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2393         msginfo->to_folder = to_folder;
2394         if (MSG_IS_DELETED(msginfo->flags))
2395                 summaryview->deleted--;
2396         MSG_UNSET_FLAGS(msginfo->flags,
2397                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
2398         if (!MSG_IS_COPY(msginfo->flags)) {
2399                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
2400                 summaryview->copied++;
2401         }
2402
2403         if (!prefs_common.immediate_exec)
2404                 summary_set_row_marks(summaryview, row);
2405
2406         debug_print(_("Message %d is set to copy to %s\n"),
2407                     msginfo->msgnum, to_folder->path);
2408 }
2409
2410 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2411 {
2412         GList *cur;
2413
2414         if (!to_folder) return;
2415         if (!summaryview->folder_item ||
2416             summaryview->folder_item->folder->type == F_NEWS) return;
2417         if (summaryview->folder_item == to_folder) {
2418                 alertpanel_notice
2419                         (_("Destination to copy is same as current folder."));
2420                 return;
2421         }
2422
2423         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2424              cur != NULL; cur = cur->next)
2425                 summary_copy_row_to
2426                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2427
2428         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2429
2430         if (prefs_common.immediate_exec)
2431                 summary_execute(summaryview);
2432         else
2433                 summary_status_show(summaryview);
2434 }
2435
2436 void summary_copy_to(SummaryView *summaryview)
2437 {
2438         FolderItem *to_folder;
2439
2440         if (!summaryview->folder_item ||
2441             summaryview->folder_item->folder->type == F_NEWS) return;
2442
2443         to_folder = foldersel_folder_sel(NULL, NULL);
2444         summary_copy_selected_to(summaryview, to_folder);
2445 }
2446
2447 void summary_select_all(SummaryView *summaryview)
2448 {
2449         if (summaryview->messages >= 500) {
2450                 STATUSBAR_PUSH(summaryview->mainwin,
2451                                _("Selecting all messages..."));
2452                 main_window_cursor_wait(summaryview->mainwin);
2453         }
2454
2455         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2456
2457         if (summaryview->messages >= 500) {
2458                 STATUSBAR_POP(summaryview->mainwin);
2459                 main_window_cursor_normal(summaryview->mainwin);
2460         }
2461 }
2462
2463 void summary_unselect_all(SummaryView *summaryview)
2464 {
2465         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2466 }
2467
2468 void summary_save_as(SummaryView *summaryview)
2469 {
2470         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2471         MsgInfo *msginfo;
2472         gchar *src, *dest;
2473
2474         if (!summaryview->selected) return;
2475         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2476         if (!msginfo) return;
2477
2478         dest = filesel_select_file(_("Save as"), NULL);
2479         if (!dest) return;
2480         if (is_file_exist(dest)) {
2481                 AlertValue aval;
2482
2483                 aval = alertpanel(_("Overwrite"),
2484                                   _("Overwrite existing file?"),
2485                                   _("OK"), _("Cancel"), NULL);
2486                 if (G_ALERTDEFAULT != aval) return;
2487         }
2488
2489         src = procmsg_get_message_file(msginfo);
2490         copy_file(src, dest);
2491         g_free(src);
2492 }
2493
2494 void summary_print(SummaryView *summaryview)
2495 {
2496         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2497         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2498         MsgInfo *msginfo;
2499         GList *cur;
2500         gchar *cmdline;
2501         gchar *p;
2502
2503         if (clist->selection == NULL) return;
2504
2505         cmdline = input_dialog(_("Print"),
2506                                _("Enter the print command line:\n"
2507                                  "(`%s' will be replaced with file name)"),
2508                                prefs_common.print_cmd);
2509         if (!cmdline) return;
2510         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2511             strchr(p + 2, '%')) {
2512                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2513                                  cmdline);
2514                 g_free(cmdline);
2515                 return;
2516         }
2517
2518         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2519                 msginfo = gtk_ctree_node_get_row_data
2520                         (ctree, GTK_CTREE_NODE(cur->data));
2521                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2522         }
2523         
2524         g_free(cmdline);
2525 }
2526
2527 void summary_execute(SummaryView *summaryview)
2528 {
2529         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2530         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2531         GtkCTreeNode *node, *next;
2532
2533         if (!summaryview->folder_item ||
2534             summaryview->folder_item->folder->type == F_NEWS) return;
2535
2536         gtk_clist_freeze(clist);
2537
2538         if (summaryview->folder_item->prefs->enable_thread)
2539                 /*      if (prefs_common.enable_thread) */
2540                 summary_unthread_for_exec(summaryview);
2541
2542         summary_execute_move(summaryview);
2543         summary_execute_copy(summaryview);
2544         summary_execute_delete(summaryview);
2545
2546         node = GTK_CTREE_NODE(clist->row_list);
2547         while (node != NULL) {
2548                 next = GTK_CTREE_NODE_NEXT(node);
2549                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2550                         if (node == summaryview->displayed) {
2551                                 messageview_clear(summaryview->messageview);
2552                                 summaryview->displayed = NULL;
2553                         }
2554                         if (GTK_CTREE_ROW(node)->children != NULL)
2555                                 g_warning("summary_execute(): children != NULL\n");
2556                         else
2557                                 gtk_ctree_remove_node(ctree, node);
2558                 }
2559                 node = next;
2560         }
2561
2562         if (summaryview->folder_item->prefs->enable_thread)
2563                 /*      if (prefs_common.enable_thread) */
2564                 summary_thread_build(summaryview);
2565
2566         summaryview->selected = clist->selection ?
2567                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2568
2569         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2570                 menu_set_insensitive_all
2571                         (GTK_MENU_SHELL(summaryview->popupmenu));
2572                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2573         } else
2574                 gtk_widget_grab_focus(summaryview->ctree);
2575
2576         summary_update_status(summaryview);
2577         summary_status_show(summaryview);
2578
2579         summary_write_cache(summaryview);
2580
2581         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2582
2583         gtk_clist_thaw(clist);
2584 }
2585
2586 static void summary_execute_move(SummaryView *summaryview)
2587 {
2588         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2589         GSList *cur;
2590
2591         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2592
2593         /* search moving messages and execute */
2594         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2595                                 summaryview);
2596
2597         if (summaryview->mlist) {
2598                 procmsg_move_messages(summaryview->mlist);
2599
2600                 folder_item_scan_foreach(summaryview->folder_table);
2601                 folderview_update_item_foreach(summaryview->folder_table);
2602
2603                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2604                         procmsg_msginfo_free((MsgInfo *)cur->data);
2605                 g_slist_free(summaryview->mlist);
2606                 summaryview->mlist = NULL;
2607         }
2608
2609         g_hash_table_destroy(summaryview->folder_table);
2610         summaryview->folder_table = NULL;
2611 }
2612
2613 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2614                                       gpointer data)
2615 {
2616         SummaryView *summaryview = data;
2617         MsgInfo *msginfo;
2618
2619         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2620
2621         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2622                 g_hash_table_insert(summaryview->folder_table,
2623                                     msginfo->to_folder, GINT_TO_POINTER(1));
2624
2625                 summaryview->mlist =
2626                         g_slist_append(summaryview->mlist, msginfo);
2627                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2628
2629                 if (msginfo->msgid && *msginfo->msgid &&
2630                     node == g_hash_table_lookup(summaryview->msgid_table,
2631                                                 msginfo->msgid))
2632                         g_hash_table_remove(summaryview->msgid_table,
2633                                             msginfo->msgid);
2634         }
2635 }
2636
2637 static void summary_execute_copy(SummaryView *summaryview)
2638 {
2639         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2640
2641         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2642
2643         /* search copying messages and execute */
2644         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2645                                 summaryview);
2646
2647         if (summaryview->mlist) {
2648                 procmsg_copy_messages(summaryview->mlist);
2649
2650                 folder_item_scan_foreach(summaryview->folder_table);
2651                 folderview_update_item_foreach(summaryview->folder_table);
2652
2653                 g_slist_free(summaryview->mlist);
2654                 summaryview->mlist = NULL;
2655         }
2656
2657         g_hash_table_destroy(summaryview->folder_table);
2658         summaryview->folder_table = NULL;
2659 }
2660
2661 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
2662                                       gpointer data)
2663 {
2664         SummaryView *summaryview = data;
2665         MsgInfo *msginfo;
2666
2667         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2668
2669         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
2670                 g_hash_table_insert(summaryview->folder_table,
2671                                     msginfo->to_folder, GINT_TO_POINTER(1));
2672
2673                 summaryview->mlist =
2674                         g_slist_append(summaryview->mlist, msginfo);
2675
2676                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
2677                 
2678                 summary_set_row_marks(summaryview, node);
2679         }
2680 }
2681
2682 static void summary_execute_delete(SummaryView *summaryview)
2683 {
2684         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2685         FolderItem *trash;
2686         GSList *cur;
2687
2688         trash = summaryview->folder_item->folder->trash;
2689         if (summaryview->folder_item->folder->type == F_MH) {
2690                 g_return_if_fail(trash != NULL);
2691         }
2692         if (summaryview->folder_item == trash) return;
2693
2694         /* search deleting messages and execute */
2695         gtk_ctree_pre_recursive
2696                 (ctree, NULL, summary_execute_delete_func, summaryview);
2697
2698         if (!summaryview->mlist) return;
2699
2700         for(cur = summaryview->mlist ; cur != NULL ; cur = cur->next) {
2701                 MsgInfo * msginfo = cur->data;
2702                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2703         }
2704
2705         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
2706
2707         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2708                 procmsg_msginfo_free((MsgInfo *)cur->data);
2709
2710         g_slist_free(summaryview->mlist);
2711         summaryview->mlist = NULL;
2712
2713         folder_item_scan(trash);
2714         folderview_update_item(trash, FALSE);
2715 }
2716
2717 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
2718                                         gpointer data)
2719 {
2720         SummaryView *summaryview = data;
2721         MsgInfo *msginfo;
2722
2723         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2724
2725         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
2726                 summaryview->mlist =
2727                         g_slist_append(summaryview->mlist, msginfo);
2728                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2729
2730                 if (msginfo->msgid && *msginfo->msgid &&
2731                     node == g_hash_table_lookup(summaryview->msgid_table,
2732                                                 msginfo->msgid))
2733                         g_hash_table_remove(summaryview->msgid_table,
2734                                             msginfo->msgid);
2735
2736                 if (msginfo->subject && 
2737                     node == subject_table_lookup(summaryview->subject_table, 
2738                                                 msginfo->subject)) {
2739                         gchar *s = msginfo->subject + (g_strncasecmp(msginfo->subject, "Re: ", 4) == 0 ? 4 : 0);
2740                         g_hash_table_remove(summaryview->subject_table, s);
2741                 }                       
2742         }
2743 }
2744
2745 /* thread functions */
2746
2747 void summary_thread_build(SummaryView *summaryview)
2748 {
2749         debug_print(_("Building threads..."));
2750         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
2751         main_window_cursor_wait(summaryview->mainwin);
2752
2753         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2754
2755         gtk_ctree_pre_recursive_to_depth
2756                 (GTK_CTREE(summaryview->ctree), NULL, 1,
2757                  GTK_CTREE_FUNC(summary_thread_func),
2758                  summaryview);
2759
2760         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2761
2762         debug_print(_("done.\n"));
2763         STATUSBAR_POP(summaryview->mainwin);
2764         main_window_cursor_normal(summaryview->mainwin);
2765 }
2766
2767 void summary_unthread(SummaryView *summaryview)
2768 {
2769         GtkCTreeNode *node;
2770         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2771
2772         debug_print(_("Unthreading..."));
2773         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
2774         main_window_cursor_wait(summaryview->mainwin);
2775
2776         gtk_clist_freeze(GTK_CLIST(ctree));
2777
2778         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2779              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2780                 summary_unthread_func(ctree, node, NULL);
2781         }
2782
2783         gtk_clist_thaw(GTK_CLIST(ctree));
2784
2785         debug_print(_("done.\n"));
2786         STATUSBAR_POP(summaryview->mainwin);
2787         main_window_cursor_normal(summaryview->mainwin);
2788 }
2789
2790 static void summary_unthread_for_exec(SummaryView *summaryview)
2791 {
2792         GtkCTreeNode *node;
2793         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2794
2795         debug_print(_("Unthreading for execution..."));
2796
2797         gtk_clist_freeze(GTK_CLIST(ctree));
2798
2799         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2800              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2801                 summary_unthread_for_exec_func(ctree, node, NULL);
2802         }
2803
2804         gtk_clist_thaw(GTK_CLIST(ctree));
2805
2806         debug_print(_("done.\n"));
2807 }
2808
2809 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
2810                                 gpointer data)
2811 {
2812         MsgInfo *msginfo;
2813         GtkCTreeNode *parent = NULL;
2814         
2815         SummaryView * summaryview = (SummaryView *) data;
2816         GHashTable *msgid_table = summaryview->msgid_table;
2817         GHashTable *subject_table = summaryview->subject_table;
2818
2819         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2820
2821         if (!msginfo) return;
2822
2823         if(msginfo->inreplyto) {
2824             parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
2825         }
2826         if (parent == NULL && msginfo->subject) {
2827                 parent = subject_table_lookup(subject_table, msginfo->subject);
2828         }
2829
2830         if (parent && parent != node) {
2831                 gtk_ctree_move(ctree, node, parent, NULL);
2832                 gtk_ctree_expand(ctree, parent);
2833         }
2834 }
2835
2836 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
2837                                   gpointer data)
2838 {
2839         GtkCTreeNode *child;
2840         GtkCTreeNode *sibling;
2841
2842         child = GTK_CTREE_ROW(node)->children;
2843         sibling = GTK_CTREE_ROW(node)->sibling;
2844
2845         while (child != NULL) {
2846                 GtkCTreeNode *next_child;
2847
2848                 next_child = GTK_CTREE_ROW(child)->sibling;
2849                 gtk_ctree_move(ctree, child, NULL, sibling);
2850                 child = next_child;
2851         }
2852 }
2853
2854 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
2855                                            gpointer data)
2856 {
2857         MsgInfo *msginfo;
2858         GtkCTreeNode *top_parent;
2859         GtkCTreeNode *child;
2860         GtkCTreeNode *sibling;
2861
2862         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2863
2864         if (!msginfo ||
2865             (!MSG_IS_MOVE(msginfo->flags) &&
2866              !MSG_IS_DELETED(msginfo->flags)))
2867                 return;
2868         child = GTK_CTREE_ROW(node)->children;
2869         if (!child) return;
2870
2871         for (top_parent = node;
2872              GTK_CTREE_ROW(top_parent)->parent != NULL;
2873              top_parent = GTK_CTREE_ROW(top_parent)->parent)
2874                 ;
2875         sibling = GTK_CTREE_ROW(top_parent)->sibling;
2876
2877         while (child != NULL) {
2878                 GtkCTreeNode *next_child;
2879
2880                 next_child = GTK_CTREE_ROW(child)->sibling;
2881                 gtk_ctree_move(ctree, child, NULL, sibling);
2882                 child = next_child;
2883         }
2884 }
2885
2886 void summary_filter(SummaryView *summaryview)
2887 {
2888         if (!prefs_common.fltlist) return;
2889
2890         debug_print(_("filtering..."));
2891         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
2892         main_window_cursor_wait(summaryview->mainwin);
2893
2894         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2895
2896         if (prefs_filtering == NULL) {
2897                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2898                                         GTK_CTREE_FUNC(summary_filter_func),
2899                                         summaryview);
2900                 
2901                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2902
2903                 if (prefs_common.immediate_exec)
2904                         summary_execute(summaryview);
2905                 else
2906                         summary_status_show(summaryview);
2907         }
2908         else {
2909                 summaryview->folder_table = g_hash_table_new(NULL, NULL);
2910
2911                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2912                                         GTK_CTREE_FUNC(summary_filter_func),
2913                                         summaryview);
2914
2915                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2916
2917                 folder_item_scan_foreach(summaryview->folder_table);
2918                 folderview_update_item_foreach(summaryview->folder_table);
2919
2920                 g_hash_table_destroy(summaryview->folder_table);
2921                 summaryview->folder_table = NULL;
2922
2923                 summary_show(summaryview, summaryview->folder_item, FALSE);
2924         }
2925
2926         debug_print(_("done.\n"));
2927         STATUSBAR_POP(summaryview->mainwin);
2928         main_window_cursor_normal(summaryview->mainwin);
2929 }
2930
2931 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
2932                                 gpointer data)
2933 {
2934         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2935         SummaryView *summaryview = data;
2936         gchar *file;
2937         FolderItem *dest;
2938
2939         if (prefs_filtering == NULL) {
2940                 /* old filtering */
2941                 file = procmsg_get_message_file_path(msginfo);
2942                 dest = filter_get_dest_folder(prefs_common.fltlist, file);
2943                 g_free(file);
2944
2945                 if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
2946                     summaryview->folder_item != dest)
2947                         summary_move_row_to(summaryview, node, dest);
2948         }
2949         else
2950                 filter_msginfo_move_or_delete(prefs_filtering, msginfo,
2951                                               summaryview->folder_table);
2952 }
2953
2954 /* callback functions */
2955
2956 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
2957                                    SummaryView *summaryview)
2958 {
2959         if (!event)
2960                 return;
2961
2962         if (!summaryview->msg_is_toggled_on && summaryview->selected)
2963                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2964         else
2965                 summary_toggle_view(summaryview);
2966 }
2967
2968 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2969                                    SummaryView *summaryview)
2970 {
2971         if (!event) return;
2972
2973         if (event->button == 3) {
2974                 /* right clicked */
2975                 summary_add_sender_to_cb(summaryview, 0, 0);    
2976                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
2977                                NULL, NULL, event->button, event->time);
2978         } else if (event->button == 2) {
2979                 summaryview->display_msg = TRUE;
2980         } else if (event->button == 1) {
2981                 if (!prefs_common.emulate_emacs &&
2982                     summaryview->msg_is_toggled_on)
2983                         summaryview->display_msg = TRUE;
2984         }
2985 }
2986
2987 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
2988                                     SummaryView *summaryview)
2989 {
2990 }
2991
2992 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
2993 {
2994         summary_key_pressed(summaryview->ctree, event, summaryview);
2995 }
2996
2997 #define BREAK_ON_MODIFIER_KEY() \
2998         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2999
3000 #define KEY_PRESS_EVENT_STOP() \
3001         if (gtk_signal_n_emissions_by_name \
3002                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
3003                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
3004                                              "key_press_event"); \
3005         }
3006
3007 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
3008                                 SummaryView *summaryview)
3009 {
3010         GtkCTree *ctree = GTK_CTREE(widget);
3011         GtkCTreeNode *node;
3012         FolderItem *to_folder;
3013
3014         if (!event) return;
3015
3016         switch (event->keyval) {
3017         case GDK_g:             /* Go */
3018         case GDK_G:
3019                 BREAK_ON_MODIFIER_KEY();
3020                 KEY_PRESS_EVENT_STOP();
3021                 to_folder = foldersel_folder_sel(NULL, NULL);
3022                 if (to_folder) {
3023                         debug_print(_("Go to %s\n"), to_folder->path);
3024                         folderview_select(summaryview->folderview, to_folder);
3025                 }
3026                 return;
3027         case GDK_w:             /* Write new message */
3028                 BREAK_ON_MODIFIER_KEY();
3029                 if (summaryview->folder_item) {
3030                         PrefsAccount *ac;
3031                         ac = summaryview->folder_item->folder->account;
3032                         if (ac && ac->protocol == A_NNTP)
3033                                 compose_new_with_recipient
3034                                         (ac, summaryview->folder_item->path);
3035                         else
3036                                 compose_new(ac);
3037                 } else
3038                         compose_new(NULL);
3039                 return;
3040         case GDK_D:             /* Empty trash */
3041                 BREAK_ON_MODIFIER_KEY();
3042                 KEY_PRESS_EVENT_STOP();
3043                 main_window_empty_trash(summaryview->mainwin, TRUE);
3044                 return;
3045         case GDK_Q:             /* Quit */
3046                 BREAK_ON_MODIFIER_KEY();
3047
3048                 if (prefs_common.confirm_on_exit) {
3049                         if (alertpanel(_("Exit"), _("Exit this program?"),
3050                                        _("OK"), _("Cancel"), NULL)
3051                                        == G_ALERTDEFAULT) {
3052                                 manage_window_focus_in
3053                                         (summaryview->mainwin->window,
3054                                          NULL, NULL);
3055                                 app_will_exit(NULL, summaryview->mainwin);
3056                         }
3057                 }
3058                 return;
3059         case GDK_Left:          /* Move focus */
3060         case GDK_Escape:
3061                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3062                 return;
3063         default:
3064                 break;
3065         }
3066
3067         if (!summaryview->selected) {
3068                 node = gtk_ctree_node_nth(ctree, 0);
3069                 if (node)
3070                         gtk_ctree_select(ctree, node);
3071                 else
3072                         return;
3073         }
3074
3075         switch (event->keyval) {
3076         case GDK_space:         /* Page down or go to the next */
3077                 if (summaryview->displayed != summaryview->selected) {
3078                         summary_display_msg(summaryview,
3079                                             summaryview->selected, FALSE);
3080                         break;
3081                 }
3082                 if (!textview_scroll_page(summaryview->messageview->textview,
3083                                           FALSE))
3084                         summary_select_next_unread(summaryview);
3085                 break;
3086         case GDK_n:             /* Next */
3087         case GDK_N:
3088                 BREAK_ON_MODIFIER_KEY();
3089                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3090                 break;
3091         case GDK_BackSpace:     /* Page up */
3092         case GDK_Delete:
3093                 textview_scroll_page(summaryview->messageview->textview, TRUE);
3094                 break;
3095         case GDK_p:             /* Prev */
3096         case GDK_P:
3097                 BREAK_ON_MODIFIER_KEY();
3098                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
3099                 break;
3100         case GDK_v:             /* Toggle summary mode / message mode */
3101         case GDK_V:
3102                 BREAK_ON_MODIFIER_KEY();
3103
3104                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
3105                         summary_display_msg(summaryview,
3106                                             summaryview->selected, FALSE);
3107                 else
3108                         summary_toggle_view(summaryview);
3109                 break;
3110         case GDK_Return:        /* Scroll up/down one line */
3111                 if (summaryview->displayed != summaryview->selected) {
3112                         summary_display_msg(summaryview,
3113                                             summaryview->selected, FALSE);
3114                         break;
3115                 }
3116                 textview_scroll_one_line(summaryview->messageview->textview,
3117                                          (event->state & GDK_MOD1_MASK) != 0);
3118                 break;
3119         case GDK_asterisk:      /* Mark */
3120                 summary_mark(summaryview);
3121                 break;
3122         case GDK_exclam:        /* Mark as unread */
3123                 summary_mark_as_unread(summaryview);
3124                 break;
3125         case GDK_d:             /* Delete */
3126                 BREAK_ON_MODIFIER_KEY();
3127                 summary_delete(summaryview);
3128                 break;
3129         case GDK_u:             /* Unmark */
3130         case GDK_U:
3131                 BREAK_ON_MODIFIER_KEY();
3132                 summary_unmark(summaryview);
3133                 break;
3134         case GDK_o:             /* Move */
3135                 BREAK_ON_MODIFIER_KEY();
3136                 summary_move_to(summaryview);
3137                 break;
3138         case GDK_O:             /* Copy */
3139                 BREAK_ON_MODIFIER_KEY();
3140                 summary_copy_to(summaryview);
3141                 break;
3142         case GDK_x:             /* Execute */
3143         case GDK_X:
3144                 BREAK_ON_MODIFIER_KEY();
3145                 KEY_PRESS_EVENT_STOP();
3146                 summary_execute(summaryview);
3147                 break;
3148         case GDK_a:             /* Reply to the message */
3149                 BREAK_ON_MODIFIER_KEY();
3150                 summary_reply_cb(summaryview,
3151                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
3152                 break;
3153         case GDK_A:             /* Reply to the message with quotation */
3154                 BREAK_ON_MODIFIER_KEY();
3155                 summary_reply_cb(summaryview,
3156                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
3157                 break;
3158         case GDK_f:             /* Forward the message */
3159                 BREAK_ON_MODIFIER_KEY();
3160                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
3161                 break;
3162         case GDK_F:
3163                 BREAK_ON_MODIFIER_KEY();
3164                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
3165                 break;
3166         case GDK_y:             /* Save the message */
3167                 BREAK_ON_MODIFIER_KEY();
3168                 summary_save_as(summaryview);
3169                 break;
3170         default:
3171                 break;
3172         }
3173 }
3174
3175 #undef BREAK_ON_MODIFIER_KEY
3176 #undef KEY_PRESS_EVENT_STOP
3177
3178 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
3179 {
3180         if (summaryview->folder_item->stype == F_DRAFT)
3181                 summary_reedit(summaryview);
3182         else
3183                 summary_open_msg(summaryview);
3184
3185         summaryview->display_msg = FALSE;
3186 }
3187
3188 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
3189                              gint column, SummaryView *summaryview)
3190 {
3191         MsgInfo *msginfo;
3192
3193         summary_status_show(summaryview);
3194         summary_set_menu_sensitive(summaryview);
3195
3196         if (GTK_CLIST(ctree)->selection &&
3197              GTK_CLIST(ctree)->selection->next) {
3198                 summaryview->display_msg = FALSE;
3199                 return;
3200         }
3201
3202         summaryview->selected = row;
3203
3204         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3205
3206         switch (column) {
3207         case S_COL_MARK:
3208                 if (MSG_IS_MARKED(msginfo->flags)) {
3209                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
3210
3211                         CHANGE_FLAGS(msginfo);
3212                         
3213                         summary_set_row_marks(summaryview, row);
3214                 } else
3215                         summary_mark_row(summaryview, row);
3216                 break;
3217         case S_COL_UNREAD:
3218                 if (MSG_IS_UNREAD(msginfo->flags)) {
3219                         summary_mark_row_as_read(summaryview, row);
3220                         summary_status_show(summaryview);
3221                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
3222                          !MSG_IS_FORWARDED(msginfo->flags)) {
3223                         summary_mark_row_as_unread(summaryview, row);
3224                         summary_status_show(summaryview);
3225                 }
3226                 break;
3227         default:
3228                 break;
3229         }
3230
3231         if (summaryview->display_msg)
3232                 summary_display_msg(summaryview, row, FALSE);
3233
3234         summaryview->display_msg = FALSE;
3235 }
3236
3237 static void summary_col_resized(GtkCList *clist, gint column, gint width,
3238                                 SummaryView *summaryview)
3239 {
3240         switch (column) {
3241         case S_COL_MARK:
3242                 prefs_common.summary_col_mark = width;
3243                 break;
3244         case S_COL_UNREAD:
3245                 prefs_common.summary_col_unread = width;
3246                 break;
3247         case S_COL_MIME:
3248                 prefs_common.summary_col_mime = width;
3249                 break;
3250         case S_COL_NUMBER:
3251                 prefs_common.summary_col_number = width;
3252                 break;
3253         case S_COL_SCORE:
3254                 prefs_common.summary_col_score = width;
3255                 break;
3256         case S_COL_SIZE:
3257                 prefs_common.summary_col_size = width;
3258                 break;
3259         case S_COL_DATE:
3260                 prefs_common.summary_col_date = width;
3261                 break;
3262         case S_COL_FROM:
3263                 prefs_common.summary_col_from = width;
3264                 break;
3265         case S_COL_SUBJECT:
3266                 prefs_common.summary_col_subject = width;
3267                 break;
3268         default:
3269                 break;
3270         }
3271 }
3272
3273 static void summary_reply_cb(SummaryView *summaryview, guint action,
3274                              GtkWidget *widget)
3275 {
3276         MsgInfo *msginfo;
3277         GList  *sel = GTK_CLIST(summaryview->ctree)->selection;
3278
3279         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3280                                               summaryview->selected);
3281         if (!msginfo) return;
3282
3283         switch ((ComposeReplyMode)action) {
3284         case COMPOSE_REPLY:
3285                 compose_reply(msginfo, prefs_common.reply_with_quote,
3286                               FALSE, FALSE);
3287                 break;
3288         case COMPOSE_REPLY_WITH_QUOTE:
3289                 compose_reply(msginfo, TRUE, FALSE, FALSE);
3290                 break;
3291         case COMPOSE_REPLY_WITHOUT_QUOTE:
3292                 compose_reply(msginfo, FALSE, FALSE, FALSE);
3293                 break;
3294         case COMPOSE_REPLY_TO_SENDER:
3295                 compose_reply(msginfo, prefs_common.reply_with_quote,
3296                               FALSE, TRUE);
3297                 break;
3298         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
3299                 compose_followup_and_reply_to(msginfo,
3300                                               prefs_common.reply_with_quote,
3301                                               FALSE, TRUE);
3302                 break;
3303         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
3304                 compose_reply(msginfo, TRUE, FALSE, TRUE);
3305                 break;
3306         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
3307                 compose_reply(msginfo, FALSE, FALSE, TRUE);
3308                 break;
3309         case COMPOSE_REPLY_TO_ALL:
3310                 compose_reply(msginfo, prefs_common.reply_with_quote,
3311                               TRUE, FALSE);
3312                 break;
3313         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
3314                 compose_reply(msginfo, TRUE, TRUE, FALSE);
3315                 break;
3316         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
3317                 compose_reply(msginfo, FALSE, TRUE, FALSE);
3318                 break;
3319         case COMPOSE_FORWARD:
3320                 if (!sel->next) {
3321                         compose_forward(NULL, msginfo, FALSE);
3322                         break;
3323                 }
3324                 /* if (sel->next) FALL THROUGH */
3325         case COMPOSE_FORWARD_AS_ATTACH:
3326                 {
3327                         GSList *msginfo_list = NULL;
3328                         for ( ; sel != NULL; sel = sel->next)
3329                                 msginfo_list = g_slist_append(msginfo_list, 
3330                                         gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3331                                                 GTK_CTREE_NODE(sel->data)));
3332                         compose_forward_multiple(NULL, msginfo_list);
3333                         g_slist_free(msginfo_list);
3334                 }                       
3335                 break;
3336         default:
3337                 g_warning("summary_reply_cb(): invalid action: %d\n", action);
3338         }
3339
3340         summary_set_marks_selected(summaryview);
3341 }
3342
3343 static void summary_show_all_header_cb(SummaryView *summaryview,
3344                                        guint action, GtkWidget *widget)
3345 {
3346         header_window_show_cb(summaryview->mainwin, action, widget);
3347 }
3348
3349 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
3350                                          guint                   action,
3351                                          GtkWidget              *widget_)
3352 {
3353         GtkWidget               *submenu;                                               
3354         GList                   *groups, *tmp;
3355         GtkMenuShell    *menushell;
3356         GtkWidget               *menu;
3357         GtkWidget               *menuitem;
3358         GList                   *child;
3359         gboolean                found = FALSE;
3360         MsgInfo                 *msginfo;
3361         gchar                   *from_address;
3362
3363         menushell = GTK_MENU_SHELL(summaryview->popupmenu);
3364         g_return_if_fail(menushell != NULL);
3365         child = menushell->children;
3366         g_return_if_fail(child);
3367
3368         /* we're iterating each menu item searching for the one with 
3369          * a "contacts" object data. if not found add the menu,
3370          * else update it */
3371         for (child = g_list_first(menushell->children); child; child = g_list_next(child)) {
3372                 if (gtk_object_get_data(GTK_OBJECT(child->data), "contacts")) {