470e35340dd1dfad044246cba3881e50a6ccfcad
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkscrolledwindow.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkpixmap.h>
27 #include <gtk/gtkctree.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtktext.h>
31 #include <gtk/gtkmenu.h>
32 #include <gtk/gtkmenuitem.h>
33 #include <gtk/gtkitemfactory.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtkwindow.h>
37 #include <gtk/gtkstyle.h>
38 #include <gtk/gtkarrow.h>
39 #include <gtk/gtkeventbox.h>
40 #include <gtk/gtkstatusbar.h>
41 #include <gtk/gtkmenuitem.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <unistd.h>
48
49 #include "intl.h"
50 #include "main.h"
51 #include "menu.h"
52 #include "mainwindow.h"
53 #include "folderview.h"
54 #include "summaryview.h"
55 #include "messageview.h"
56 #include "foldersel.h"
57 #include "procmsg.h"
58 #include "procheader.h"
59 #include "headerwindow.h"
60 #include "sourcewindow.h"
61 #include "prefs_common.h"
62 #include "account.h"
63 #include "compose.h"
64 #include "utils.h"
65 #include "gtkutils.h"
66 #include "filesel.h"
67 #include "manage_window.h"
68 #include "alertpanel.h"
69 #include "inputdialog.h"
70 #include "statusbar.h"
71 #include "filter.h"
72 #include "folder.h"
73 #include "addressbook.h"
74 #include "addr_compl.h"
75 #include "scoring.h"
76 #include "prefs_folder_item.h"
77 #include "filtering.h"
78
79 #include "pixmaps/dir-open.xpm"
80 #include "pixmaps/mark.xpm"
81 #include "pixmaps/deleted.xpm"
82 #include "pixmaps/new.xpm"
83 #include "pixmaps/unread.xpm"
84 #include "pixmaps/replied.xpm"
85 #include "pixmaps/forwarded.xpm"
86 #include "pixmaps/clip.xpm"
87
88 #define STATUSBAR_PUSH(mainwin, str) \
89 { \
90         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
91                            mainwin->summaryview_cid, str); \
92         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
93 }
94
95 #define STATUSBAR_POP(mainwin) \
96 { \
97         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
98                           mainwin->summaryview_cid); \
99 }
100
101 #define SUMMARY_COL_MARK_WIDTH          10
102 #define SUMMARY_COL_UNREAD_WIDTH        13
103 #define SUMMARY_COL_MIME_WIDTH          10
104
105 static GdkFont *smallfont;
106
107 static GdkPixmap *folderxpm;
108 static GdkBitmap *folderxpmmask;
109
110 static GdkPixmap *markxpm;
111 static GdkBitmap *markxpmmask;
112 static GdkPixmap *deletedxpm;
113 static GdkBitmap *deletedxpmmask;
114
115 static GdkPixmap *newxpm;
116 static GdkBitmap *newxpmmask;
117 static GdkPixmap *unreadxpm;
118 static GdkBitmap *unreadxpmmask;
119 static GdkPixmap *repliedxpm;
120 static GdkBitmap *repliedxpmmask;
121 static GdkPixmap *forwardedxpm;
122 static GdkBitmap *forwardedxpmmask;
123
124 static GdkPixmap *clipxpm;
125 static GdkBitmap *clipxpmmask;
126
127 static void summary_free_msginfo_func   (GtkCTree               *ctree,
128                                          GtkCTreeNode           *node,
129                                          gpointer                data);
130 static void summary_set_marks_func      (GtkCTree               *ctree,
131                                          GtkCTreeNode           *node,
132                                          gpointer                data);
133 static void summary_write_cache_func    (GtkCTree               *ctree,
134                                          GtkCTreeNode           *node,
135                                          gpointer                data);
136
137 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
138
139 static GtkCTreeNode *summary_find_next_unread_msg
140                                         (SummaryView            *summaryview,
141                                          GtkCTreeNode           *current_node);
142 static GtkCTreeNode *summary_find_next_marked_msg
143                                         (SummaryView            *summaryview,
144                                          GtkCTreeNode           *current_node);
145 static GtkCTreeNode *summary_find_prev_marked_msg
146                                         (SummaryView            *summaryview,
147                                          GtkCTreeNode           *current_node);
148 static GtkCTreeNode *summary_find_msg_by_msgnum
149                                         (SummaryView            *summaryview,
150                                          guint                   msgnum);
151 #if 0
152 static GtkCTreeNode *summary_find_prev_unread_msg
153                                         (SummaryView            *summaryview,
154                                          GtkCTreeNode           *current_node);
155 #endif
156
157 static void summary_update_status       (SummaryView            *summaryview);
158
159 /* display functions */
160 static void summary_status_show         (SummaryView            *summaryview);
161 static void summary_set_ctree_from_list (SummaryView            *summaryview,
162                                          GSList                 *mlist);
163 static void summary_set_header          (gchar                  *text[],
164                                          MsgInfo                *msginfo);
165 static void summary_display_msg         (SummaryView            *summaryview,
166                                          GtkCTreeNode           *row,
167                                          gboolean                new_window);
168 static void summary_toggle_view         (SummaryView            *summaryview);
169 static void summary_set_row_marks       (SummaryView            *summaryview,
170                                          GtkCTreeNode           *row);
171
172 /* message handling */
173 static void summary_mark_row            (SummaryView            *summaryview,
174                                          GtkCTreeNode           *row);
175 static void summary_mark_row_as_read    (SummaryView            *summaryview,
176                                          GtkCTreeNode           *row);
177 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
178                                          GtkCTreeNode           *row);
179 static void summary_delete_row          (SummaryView            *summaryview,
180                                          GtkCTreeNode           *row);
181 static void summary_unmark_row          (SummaryView            *summaryview,
182                                          GtkCTreeNode           *row);
183 static void summary_move_row_to         (SummaryView            *summaryview,
184                                          GtkCTreeNode           *row,
185                                          FolderItem             *to_folder);
186 static void summary_copy_row_to         (SummaryView            *summaryview,
187                                          GtkCTreeNode           *row,
188                                          FolderItem             *to_folder);
189
190 static void summary_delete_duplicated_func
191                                         (GtkCTree               *ctree,
192                                          GtkCTreeNode           *node,
193                                          SummaryView            *summaryview);
194
195 static void summary_execute_move        (SummaryView            *summaryview);
196 static void summary_execute_move_func   (GtkCTree               *ctree,
197                                          GtkCTreeNode           *node,
198                                          gpointer                data);
199 static void summary_execute_copy        (SummaryView            *summaryview);
200 static void summary_execute_copy_func   (GtkCTree               *ctree,
201                                          GtkCTreeNode           *node,
202                                          gpointer                data);
203 static void summary_execute_delete      (SummaryView            *summaryview);
204 static void summary_execute_delete_func (GtkCTree               *ctree,
205                                          GtkCTreeNode           *node,
206                                          gpointer                data);
207
208 /* thread functions */
209 static void summary_thread_func                 (GtkCTree       *ctree,
210                                                  GtkCTreeNode   *node,
211                                                  gpointer        data);
212 static void summary_unthread_func               (GtkCTree       *ctree,
213                                                  GtkCTreeNode   *node,
214                                                  gpointer        data);
215 static void summary_unthread_for_exec           (SummaryView    *summaryview);
216 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
217                                                  GtkCTreeNode   *node,
218                                                  gpointer        data);
219
220 static void summary_filter_func         (GtkCTree               *ctree,
221                                          GtkCTreeNode           *node,
222                                          gpointer                data);
223
224 /* callback functions */
225 static void summary_toggle_pressed      (GtkWidget              *eventbox,
226                                          GdkEventButton         *event,
227                                          SummaryView            *summaryview);
228 static void summary_button_pressed      (GtkWidget              *ctree,
229                                          GdkEventButton         *event,
230                                          SummaryView            *summaryview);
231 static void summary_button_released     (GtkWidget              *ctree,
232                                          GdkEventButton         *event,
233                                          SummaryView            *summaryview);
234 static void summary_key_pressed         (GtkWidget              *ctree,
235                                          GdkEventKey            *event,
236                                          SummaryView            *summaryview);
237 static void summary_open_row            (GtkSCTree              *sctree,
238                                          SummaryView            *summaryview);
239 static void summary_selected            (GtkCTree               *ctree,
240                                          GtkCTreeNode           *row,
241                                          gint                    column,
242                                          SummaryView            *summaryview);
243 static void summary_col_resized         (GtkCList               *clist,
244                                          gint                    column,
245                                          gint                    width,
246                                          SummaryView            *summaryview);
247 static void summary_reply_cb            (SummaryView            *summaryview,
248                                          guint                   action,
249                                          GtkWidget              *widget);
250 static void summary_show_all_header_cb  (SummaryView            *summaryview,
251                                          guint                   action,
252                                          GtkWidget              *widget);
253 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
254                                          guint                   action,
255                                          GtkWidget              *widget);
256
257 static void summary_num_clicked         (GtkWidget              *button,
258                                          SummaryView            *summaryview);
259 static void summary_score_clicked       (GtkWidget *button,
260                                          SummaryView *summaryview);
261 static void summary_size_clicked        (GtkWidget              *button,
262                                          SummaryView            *summaryview);
263 static void summary_date_clicked        (GtkWidget              *button,
264                                          SummaryView            *summaryview);
265 static void summary_from_clicked        (GtkWidget              *button,
266                                          SummaryView            *summaryview);
267 static void summary_subject_clicked     (GtkWidget              *button,
268                                          SummaryView            *summaryview);
269
270 static void summary_start_drag          (GtkWidget        *widget, 
271                                          int button,
272                                          GdkEvent *event,
273                                          SummaryView      *summaryview);
274 static void summary_drag_data_get       (GtkWidget        *widget,
275                                          GdkDragContext   *drag_context,
276                                          GtkSelectionData *selection_data,
277                                          guint             info,
278                                          guint             time,
279                                          SummaryView      *summaryview);
280
281 /* custom compare functions for sorting */
282
283 static gint summary_cmp_by_num          (GtkCList               *clist,
284                                          gconstpointer           ptr1,
285                                          gconstpointer           ptr2);
286 static gint summary_cmp_by_size         (GtkCList               *clist,
287                                          gconstpointer           ptr1,
288                                          gconstpointer           ptr2);
289 static gint summary_cmp_by_date         (GtkCList               *clist,
290                                          gconstpointer           ptr1,
291                                          gconstpointer           ptr2);
292 static gint summary_cmp_by_from         (GtkCList               *clist,
293                                          gconstpointer           ptr1,
294                                          gconstpointer           ptr2);
295 static gint summary_cmp_by_subject      (GtkCList               *clist,
296                                          gconstpointer           ptr1,
297                                          gconstpointer           ptr2);
298 static gint summary_cmp_by_score        (GtkCList               *clist,
299                                          gconstpointer           ptr1,
300                                          gconstpointer           ptr2);
301
302 GtkTargetEntry summary_drag_types[1] =
303 {
304         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
305 };
306
307 static GtkItemFactoryEntry summary_popup_entries[] =
308 {
309         {N_("/M_ove..."),               NULL, summary_move_to,  0, NULL},
310         {N_("/_Copy..."),               NULL, summary_copy_to,  0, NULL},
311         {N_("/_Delete"),                NULL, summary_delete,   0, NULL},
312         {N_("/E_xecute"),               NULL, summary_execute,  0, NULL},
313         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
314         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
315         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
316         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
317         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
318         {N_("/_Mark/Mark as rea_d"),
319                                         NULL, summary_mark_as_read, 0, NULL},
320         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
321         {N_("/_Reply"),                 NULL, summary_reply_cb, COMPOSE_REPLY, NULL},
322         {N_("/Repl_y to sender"),       NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
323         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
324         {N_("/Reply to a_ll"),          NULL, summary_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
325         {N_("/_Forward"),               NULL, summary_reply_cb, COMPOSE_FORWARD, NULL},
326         {N_("/Forward as a_ttachment"),
327                                         NULL, summary_reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
328         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
329         {N_("/Open in new _window"),    NULL, summary_open_msg, 0, NULL},
330         {N_("/View so_urce"),           NULL, summary_view_source, 0, NULL},
331         {N_("/Show all _header"),       NULL, summary_show_all_header_cb, 0, NULL},
332         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
333         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
334         {N_("/_Save as..."),            NULL, summary_save_as,  0, NULL},
335         {N_("/_Print..."),              NULL, summary_print,    0, NULL},
336         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
337         {N_("/Select _all"),            NULL, summary_select_all, 0, NULL}
338 };
339
340 SummaryView *summary_create(void)
341 {
342         SummaryView *summaryview;
343         gchar *titles[N_SUMMARY_COLS] = {_("M"), _("U")};
344         GtkWidget *vbox;
345         GtkWidget *scrolledwin;
346         GtkWidget *ctree;
347         GtkWidget *hbox;
348         GtkWidget *statlabel_folder;
349         GtkWidget *statlabel_select;
350         GtkWidget *statlabel_msgs;
351         GtkWidget *toggle_eventbox;
352         GtkWidget *toggle_arrow;
353         GtkWidget *popupmenu;
354         GtkItemFactory *popupfactory;
355         gint n_entries;
356         gint i;
357
358         debug_print(_("Creating summary view...\n"));
359         summaryview = g_new0(SummaryView, 1);
360
361         vbox = gtk_vbox_new(FALSE, 2);
362
363         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
364         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
365                                        GTK_POLICY_AUTOMATIC,
366                                        GTK_POLICY_ALWAYS);
367         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
368         gtk_widget_set_usize(vbox,
369                              prefs_common.summaryview_width,
370                              prefs_common.summaryview_height);
371
372         if (prefs_common.trans_hdr) {
373                 titles[S_COL_NUMBER]  = _("No.");
374                 titles[S_COL_DATE]    = _("Date");
375                 titles[S_COL_FROM]    = _("From");
376                 titles[S_COL_SUBJECT] = _("Subject");
377         } else {
378                 titles[S_COL_NUMBER]  = "No.";
379                 titles[S_COL_DATE]    = "Date";
380                 titles[S_COL_FROM]    = "From";
381                 titles[S_COL_SUBJECT] = "Subject";
382         }
383         titles[S_COL_SIZE]  = _("Size");
384         titles[S_COL_SCORE] = _("Score");
385
386         ctree = gtk_sctree_new_with_titles(N_SUMMARY_COLS, S_COL_SUBJECT, titles);
387         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
388                                             GTK_CLIST(ctree)->hadjustment);
389         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
390                                             GTK_CLIST(ctree)->vadjustment);
391         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
392         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_EXTENDED);
393         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_MARK,
394                                            GTK_JUSTIFY_CENTER);
395         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_UNREAD,
396                                            GTK_JUSTIFY_CENTER);
397         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_MIME,
398                                            GTK_JUSTIFY_CENTER);
399         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_NUMBER,
400                                            GTK_JUSTIFY_RIGHT);
401         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_SCORE,
402                                            GTK_JUSTIFY_RIGHT);
403         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_SIZE,
404                                            GTK_JUSTIFY_RIGHT);
405         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_MARK,
406                                    SUMMARY_COL_MARK_WIDTH);
407         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_UNREAD,
408                                    SUMMARY_COL_UNREAD_WIDTH);
409         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_MIME,
410                                    SUMMARY_COL_MIME_WIDTH);
411         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_NUMBER,
412                                    prefs_common.summary_col_number);
413         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SCORE,
414                                    prefs_common.summary_col_score);
415         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SIZE,
416                                    prefs_common.summary_col_size);
417         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_DATE,
418                                    prefs_common.summary_col_date);
419         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_FROM,
420                                    prefs_common.summary_col_from);
421         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
422                                    prefs_common.summary_col_subject);
423         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
424         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
425                                      GTK_CTREE_EXPANDER_SQUARE);
426 #if 0
427         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
428         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
429                                      GTK_CTREE_EXPANDER_TRIANGLE);
430 #endif
431         gtk_ctree_set_indent(GTK_CTREE(ctree), 18);
432         gtk_object_set_user_data(GTK_OBJECT(ctree), summaryview);
433
434         /* don't let title buttons take key focus */
435         for (i = 0; i < N_SUMMARY_COLS; i++)
436                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
437                                        GTK_CAN_FOCUS);
438
439         /* connect signal to the buttons for sorting */
440         gtk_signal_connect
441                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_NUMBER].button),
442                  "clicked",
443                  GTK_SIGNAL_FUNC(summary_num_clicked),
444                  summaryview);
445         gtk_signal_connect
446                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SCORE].button),
447                  "clicked",
448                  GTK_SIGNAL_FUNC(summary_score_clicked),
449                  summaryview);
450         gtk_signal_connect
451                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SIZE].button),
452                  "clicked",
453                  GTK_SIGNAL_FUNC(summary_size_clicked),
454                  summaryview);
455         gtk_signal_connect
456                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_DATE].button),
457                  "clicked",
458                  GTK_SIGNAL_FUNC(summary_date_clicked),
459                  summaryview);
460         gtk_signal_connect
461                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_FROM].button),
462                  "clicked",
463                  GTK_SIGNAL_FUNC(summary_from_clicked),
464                  summaryview);
465         gtk_signal_connect
466                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SUBJECT].button),
467                  "clicked",
468                  GTK_SIGNAL_FUNC(summary_subject_clicked),
469                  summaryview);
470
471         /* create status label */
472         hbox = gtk_hbox_new(FALSE, 0);
473         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
474
475         statlabel_folder = gtk_label_new("");
476         gtk_box_pack_start(GTK_BOX(hbox), statlabel_folder, FALSE, FALSE, 2);
477         statlabel_select = gtk_label_new("");
478         gtk_box_pack_start(GTK_BOX(hbox), statlabel_select, FALSE, FALSE, 16);
479
480         /* toggle view button */
481         toggle_eventbox = gtk_event_box_new();
482         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
483         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
484         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
485
486         statlabel_msgs = gtk_label_new("");
487         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
488
489         /* create popup menu */
490         n_entries = sizeof(summary_popup_entries) /
491                 sizeof(summary_popup_entries[0]);
492         popupmenu = menu_create_items(summary_popup_entries, n_entries,
493                                       "<SummaryView>", &popupfactory,
494                                       summaryview);
495
496         /* connect signals */
497         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
498                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
499         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
500                            GTK_SIGNAL_FUNC(summary_button_pressed),
501                            summaryview);
502         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
503                            GTK_SIGNAL_FUNC(summary_button_released),
504                            summaryview);
505         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
506                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
507         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
508                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
509         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
510                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
511         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
512                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
513                            summaryview);
514
515         summaryview->vbox = vbox;
516         summaryview->scrolledwin = scrolledwin;
517         summaryview->ctree = ctree;
518         summaryview->hbox = hbox;
519         summaryview->statlabel_folder = statlabel_folder;
520         summaryview->statlabel_select = statlabel_select;
521         summaryview->statlabel_msgs = statlabel_msgs;
522         summaryview->toggle_eventbox = toggle_eventbox;
523         summaryview->toggle_arrow = toggle_arrow;
524         summaryview->popupmenu = popupmenu;
525         summaryview->popupfactory = popupfactory;
526         summaryview->msg_is_toggled_on = TRUE;
527         summaryview->sort_mode = SORT_BY_NONE;
528         summaryview->sort_type = GTK_SORT_ASCENDING;
529
530         summary_change_display_item(summaryview);
531
532         gtk_widget_show_all(vbox);
533
534         return summaryview;
535 }
536
537 void summary_init(SummaryView *summaryview)
538 {
539         GtkStyle *style;
540         GtkWidget *pixmap;
541
542         PIXMAP_CREATE(summaryview->ctree, markxpm, markxpmmask, mark_xpm);
543         PIXMAP_CREATE(summaryview->ctree, deletedxpm, deletedxpmmask,
544                       deleted_xpm);
545         PIXMAP_CREATE(summaryview->ctree, newxpm, newxpmmask, new_xpm);
546         PIXMAP_CREATE(summaryview->ctree, unreadxpm, unreadxpmmask, unread_xpm);
547         PIXMAP_CREATE(summaryview->ctree, repliedxpm, repliedxpmmask,
548                       replied_xpm);
549         PIXMAP_CREATE(summaryview->ctree, forwardedxpm, forwardedxpmmask,
550                       forwarded_xpm);
551         PIXMAP_CREATE(summaryview->ctree, clipxpm, clipxpmmask, clip_xpm);
552         PIXMAP_CREATE(summaryview->hbox, folderxpm, folderxpmmask,
553                       DIRECTORY_OPEN_XPM);
554
555         pixmap = gtk_pixmap_new(clipxpm, clipxpmmask);
556         gtk_clist_set_column_widget(GTK_CLIST(summaryview->ctree),
557                                     S_COL_MIME, pixmap);
558         gtk_widget_show(pixmap);
559
560         if (!smallfont)
561                 smallfont = gdk_fontset_load(SMALL_FONT);
562
563         style = gtk_style_copy(gtk_widget_get_style
564                                 (summaryview->statlabel_folder));
565         if (smallfont) style->font = smallfont;
566         gtk_widget_set_style(summaryview->statlabel_folder, style);
567         gtk_widget_set_style(summaryview->statlabel_select, style);
568         gtk_widget_set_style(summaryview->statlabel_msgs, style);
569
570         pixmap = gtk_pixmap_new(folderxpm, folderxpmmask);
571         gtk_box_pack_start(GTK_BOX(summaryview->hbox), pixmap, FALSE, FALSE, 4);
572         gtk_box_reorder_child(GTK_BOX(summaryview->hbox), pixmap, 0);
573         gtk_widget_show(pixmap);
574
575         summary_clear_list(summaryview);
576         summary_set_menu_sensitive(summaryview);
577 }
578
579 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
580                                                  GtkCTreeNode *current_node)
581 {
582         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
583         GtkCTreeNode *node;
584         MsgInfo *msginfo;
585         gint best_score = MIN_SCORE;
586         GtkCTreeNode *best_node = NULL;
587
588         if (current_node)
589                 //node = current_node;
590                 node = GTK_CTREE_NODE_NEXT(current_node);
591         else
592                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
593
594         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
595                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
596                 if (msginfo->score >= summaryview->important_score)
597                         break;
598                 if (msginfo->score > best_score) {
599                         best_score = msginfo->score;
600                         best_node = node;
601                 }
602         }
603
604         if (node != NULL)
605                 return node;
606         else
607                 return best_node;
608 }
609
610 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
611                                                  GtkCTreeNode *current_node)
612 {
613         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
614         GtkCTreeNode *node;
615         MsgInfo *msginfo;
616         gint best_score = MIN_SCORE;
617         GtkCTreeNode *best_node = NULL;
618
619         if (current_node)
620                 //node = current_node;
621                 node = GTK_CTREE_NODE_PREV(current_node);
622         else
623                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
624
625         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
626                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
627                 if (msginfo->score >= summaryview->important_score)
628                         break;
629                 if (msginfo->score > best_score) {
630                         best_score = msginfo->score;
631                         best_node = node;
632                 }
633         }
634
635         if (node != NULL)
636                 return node;
637         else
638                 return best_node;
639 }
640
641 gboolean summary_show(SummaryView *summaryview, FolderItem *item,
642                       gboolean update_cache)
643 {
644         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
645         GtkCTreeNode *node;
646         GSList *mlist = NULL;
647         gchar *buf;
648         gboolean is_refresh;
649         guint prev_msgnum = 0;
650         GtkCTreeNode *selected_node = summaryview->folderview->selected;
651         GSList *cur;
652         gint sort_mode;
653         gint sort_type;
654
655         STATUSBAR_POP(summaryview->mainwin);
656
657         is_refresh = (!prefs_common.open_inbox_on_inc &&
658                       item == summaryview->folder_item) ? TRUE : FALSE;
659         if (is_refresh) {
660                 prev_msgnum = summary_get_current_msgnum(summaryview);
661                 if (prev_msgnum < 1)
662                         is_refresh = FALSE;
663         }
664
665         /* process the marks if any */
666         if (summaryview->moved > 0 || summaryview->copied > 0) {
667                 AlertValue val;
668
669                 val = alertpanel(_("Process mark"),
670                                  _("Some marks are left. Process it?"),
671                                  _("Yes"), _("No"), _("Cancel"));
672                 if (G_ALERTDEFAULT == val)
673                         summary_execute(summaryview);
674                 else if (G_ALERTALTERNATE == val)
675                         summary_write_cache(summaryview);
676                 else
677                         return FALSE;
678         } else
679                 summary_write_cache(summaryview);
680
681         summaryview->folderview->opened = selected_node;
682
683         gtk_clist_freeze(GTK_CLIST(ctree));
684
685         summary_clear_list(summaryview);
686         summary_set_menu_sensitive(summaryview);
687         messageview_clear(summaryview->messageview);
688
689         buf = NULL;
690         if (!item || !item->path || !item->parent ||
691             (item->folder->type == F_MH &&
692              ((buf = folder_item_get_path(item)) == NULL ||
693               change_dir(buf) < 0))) {
694                 g_free(buf);
695                 debug_print(_("empty folder\n\n"));
696                 summary_clear_all(summaryview);
697                 summaryview->folder_item = item;
698                 gtk_clist_thaw(GTK_CLIST(ctree));
699                 return TRUE;
700         }
701         g_free(buf);
702
703         summaryview->folder_item = item;
704
705         gtk_signal_disconnect_by_data(GTK_OBJECT(ctree), summaryview);
706
707         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
708         debug_print("%s\n", buf);
709         STATUSBAR_PUSH(summaryview->mainwin, buf);
710         g_free(buf);
711
712         main_window_cursor_wait(summaryview->mainwin);
713
714         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
715
716         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
717                 MsgInfo * msginfo = (MsgInfo *) cur->data;
718
719                 msginfo->score = score_message(global_scoring, msginfo);
720                 if (msginfo->score != MAX_SCORE &&
721                     msginfo->score != MIN_SCORE) {
722                         msginfo->score += score_message(item->prefs->scoring,
723                                                         msginfo);
724                 }
725         }
726
727         summaryview->killed_messages = NULL;
728         if ((global_scoring || item->prefs->scoring) &&
729             (item->folder->type == F_NEWS)) {
730                 GSList *not_killed;
731                 gint kill_score;
732
733                 not_killed = NULL;
734                 kill_score = prefs_common.kill_score;
735                 if (item->prefs->kill_score > kill_score)
736                         kill_score = item->prefs->kill_score;
737                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
738                         MsgInfo * msginfo = (MsgInfo *) cur->data;
739
740                         if (MSG_IS_NEWS(msginfo->flags) &&
741                             (msginfo->score <= kill_score))
742                                 summaryview->killed_messages = g_slist_append(summaryview->killed_messages, msginfo);
743                         else
744                                 not_killed = g_slist_append(not_killed,
745                                                             msginfo);
746                 }
747                 g_slist_free(mlist);
748                 mlist = not_killed;
749         }
750
751         STATUSBAR_POP(summaryview->mainwin);
752
753         /* set ctree and hash table from the msginfo list
754            creating thread, and count the number of messages */
755         summary_set_ctree_from_list(summaryview, mlist);
756
757         g_slist_free(mlist);
758
759         summary_write_cache(summaryview);
760
761         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
762                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
763         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
764                            GTK_SIGNAL_FUNC(summary_button_pressed),
765                            summaryview);
766         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
767                            GTK_SIGNAL_FUNC(summary_button_released),
768                            summaryview);
769         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
770                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
771         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
772                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
773         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
774                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
775
776         /*connect drag and drop signal*/
777         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
778                            GTK_SIGNAL_FUNC (summary_start_drag),
779                            summaryview);
780         gtk_signal_connect(GTK_OBJECT (ctree),"drag_data_get",
781                            GTK_SIGNAL_FUNC (summary_drag_data_get),
782                            summaryview);
783
784         gtk_clist_thaw(GTK_CLIST(ctree));
785
786         /* sort before */
787         sort_mode = prefs_folder_item_get_sort_mode(item);
788         sort_type = prefs_folder_item_get_sort_type(item);
789
790         if (sort_mode != SORT_BY_NONE) {
791                 summaryview->sort_mode = sort_mode;
792                 if (sort_type == GTK_SORT_DESCENDING)
793                         summaryview->sort_type = GTK_SORT_ASCENDING;
794                 else
795                         summaryview->sort_type = GTK_SORT_DESCENDING;
796
797                 summary_sort(summaryview, sort_mode);
798         }
799
800         if (is_refresh) {
801                 summary_select_by_msgnum(summaryview, prev_msgnum);
802         } else {
803                 /* select first unread message */
804                 if (sort_mode == SORT_BY_SCORE)
805                         node = summary_find_next_important_score(summaryview,
806                                                                  NULL);
807                 else
808                         node = summary_find_next_unread_msg(summaryview, NULL);
809
810                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
811                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
812                 if (node) {
813                         GTK_EVENTS_FLUSH();
814                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
815                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
816                         if (prefs_common.open_unread_on_enter)
817                                 summaryview->display_msg = TRUE;
818                         gtk_sctree_select(GTK_SCTREE(ctree), node);
819                 }
820         }
821
822         summary_status_show(summaryview);
823
824         summary_set_menu_sensitive(summaryview);
825
826         main_window_set_toolbar_sensitive
827                 (summaryview->mainwin, summaryview->selected ? TRUE : FALSE);
828
829         debug_print("\n");
830         STATUSBAR_PUSH(summaryview->mainwin, _("done."));
831
832         main_window_cursor_normal(summaryview->mainwin);
833
834         return TRUE;
835 }
836
837 void summary_clear_list(SummaryView *summaryview)
838 {
839         GtkCList *clist = GTK_CLIST(summaryview->ctree);
840         gint optimal_width;
841         GSList * cur;
842
843         gtk_clist_freeze(clist);
844
845         for(cur = summaryview->killed_messages ; cur != NULL ; 
846             cur = g_slist_next(cur)) {
847                 MsgInfo * msginfo = (MsgInfo *) cur->data;
848
849                 procmsg_msginfo_free(msginfo);
850         }
851         g_slist_free(summaryview->killed_messages);
852         summaryview->killed_messages = NULL;
853
854         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
855                                 NULL, summary_free_msginfo_func, NULL);
856
857         summaryview->folder_item = NULL;
858
859         summaryview->display_msg = FALSE;
860
861         summaryview->selected  = NULL;
862         summaryview->displayed = NULL;
863         summaryview->newmsgs   = summaryview->unread     = 0;
864         summaryview->messages  = summaryview->total_size = 0;
865         summaryview->deleted   = summaryview->moved      = 0;
866         summaryview->copied    = 0;
867         if (summaryview->msgid_table) {
868                 g_hash_table_destroy(summaryview->msgid_table);
869                 summaryview->msgid_table = NULL;
870         }
871         if (summaryview->subject_table) {
872                 g_hash_table_destroy(summaryview->subject_table);
873                 summaryview->subject_table = NULL;
874         }
875         summaryview->mlist = NULL;
876         if (summaryview->folder_table) {
877                 g_hash_table_destroy(summaryview->folder_table);
878                 summaryview->folder_table = NULL;
879         }
880         summaryview->sort_mode = SORT_BY_NONE;
881         summaryview->sort_type = GTK_SORT_ASCENDING;
882
883         gtk_clist_clear(clist);
884         optimal_width = gtk_clist_optimal_column_width(clist, S_COL_SUBJECT);
885         gtk_clist_set_column_width(clist, S_COL_SUBJECT, optimal_width);
886
887         gtk_clist_thaw(clist);
888 }
889
890 void summary_clear_all(SummaryView *summaryview)
891 {
892         summary_clear_list(summaryview);
893         summary_set_menu_sensitive(summaryview);
894         main_window_set_toolbar_sensitive(summaryview->mainwin, FALSE);
895         summary_status_show(summaryview);
896 }
897
898 static void summary_set_menu_sensitive(SummaryView *summaryview)
899 {
900         GtkItemFactory *ifactory = summaryview->popupfactory;
901         GtkCList *clist = GTK_CLIST(summaryview->ctree);
902         gboolean sens;
903         SummarySelection selection;
904
905         if (!clist->row_list)
906                 selection = SUMMARY_NONE;
907         else if (!clist->selection)
908                 selection = SUMMARY_SELECTED_NONE;
909         else if (!clist->selection->next)
910                 selection = SUMMARY_SELECTED_SINGLE;
911         else
912                 selection = SUMMARY_SELECTED_MULTIPLE;
913
914         main_window_set_menu_sensitive(summaryview->mainwin, selection);
915
916         if (selection == SUMMARY_NONE) {
917                 GtkWidget *submenu;
918
919                 submenu = gtk_item_factory_get_widget
920                         (summaryview->popupfactory, "/Mark");
921                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
922                 menu_set_insensitive_all
923                         (GTK_MENU_SHELL(summaryview->popupmenu));
924                 return;
925         }
926
927         if (summaryview->folder_item->folder->type != F_NEWS) {
928                 if (summaryview->folder_item->stype != F_TRASH)
929                         menu_set_sensitive(ifactory, "/Delete", TRUE);
930                 menu_set_sensitive(ifactory, "/Move...", TRUE);
931                 menu_set_sensitive(ifactory, "/Copy...", TRUE);
932         }
933         menu_set_sensitive(ifactory, "/Execute", TRUE);
934
935         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
936         menu_set_sensitive(ifactory, "/Reply",                    sens);
937         menu_set_sensitive(ifactory, "/Reply to sender",          sens);
938         menu_set_sensitive(ifactory, "/Reply to all",             sens);
939         menu_set_sensitive(ifactory, "/Forward",                  sens);
940         menu_set_sensitive(ifactory, "/Forward as attachment",    sens);
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             cur_account && cur_account->address &&
1797             !MSG_IS_NEWS(msginfo->flags)) {
1798                 gchar *from;
1799
1800                 Xalloca(from, strlen(msginfo->from) + 1, return);
1801                 strcpy(from, msginfo->from);
1802                 extract_address(from);
1803                 if (!strcmp(from, cur_account->address)) {
1804                         g_free(to);
1805                         to = g_strconcat("-->", msginfo->to, NULL);
1806                         text[S_COL_FROM] = to;
1807                 }
1808         }
1809
1810         if ((text[S_COL_FROM] != to) && prefs_common.use_addr_book &&
1811             msginfo->from) {
1812                 gint count;
1813                 gchar *from;
1814   
1815                 Xalloca(from, strlen(msginfo->from) + 1, return);
1816                 strcpy(from, msginfo->from);
1817                 extract_address(from);
1818                 count = complete_address(from);
1819                 if (count > 1) {
1820                         g_free(from_name);
1821                         from = get_complete_address(1);
1822                         from_name = procheader_get_fromname(from);
1823                         g_free(from);
1824                         text[S_COL_FROM] = from_name;
1825                 }
1826         }
1827
1828         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
1829                 _("(No Subject)");
1830 }
1831
1832 #define CHANGE_FLAGS(msginfo) \
1833 { \
1834 if (msginfo->folder->folder->change_flags != NULL) \
1835 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
1836                                       msginfo->folder, \
1837                                       msginfo); \
1838 }
1839
1840 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
1841                                 gboolean new_window)
1842 {
1843         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1844         MsgInfo *msginfo;
1845         gchar *filename;
1846         static gboolean lock = FALSE;
1847
1848         if (!new_window && summaryview->displayed == row) return;
1849         g_return_if_fail(row != NULL);
1850
1851         if (lock) return;
1852         lock = TRUE;
1853
1854         STATUSBAR_POP(summaryview->mainwin);
1855
1856         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1857
1858         filename = procmsg_get_message_file(msginfo);
1859         if (!filename) {
1860                 lock = FALSE;
1861                 return;
1862         }
1863         g_free(filename);
1864
1865         if (MSG_IS_NEW(msginfo->flags))
1866                 summaryview->newmsgs--;
1867         if (MSG_IS_UNREAD(msginfo->flags))
1868                 summaryview->unread--;
1869         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
1870                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1871
1872                 CHANGE_FLAGS(msginfo);
1873
1874                 summary_set_row_marks(summaryview, row);
1875                 gtk_clist_thaw(GTK_CLIST(ctree));
1876                 summary_status_show(summaryview);
1877         }
1878
1879         if (new_window) {
1880                 MessageView *msgview;
1881
1882                 msgview = messageview_create_with_new_window();
1883                 messageview_show(msgview, msginfo);
1884         } else {
1885                 MessageView *msgview;
1886
1887                 msgview = summaryview->messageview;
1888
1889                 summaryview->displayed = row;
1890                 if (!summaryview->msg_is_toggled_on)
1891                         summary_toggle_view(summaryview);
1892                 messageview_show(msgview, msginfo);
1893                 if (msgview->type == MVIEW_TEXT ||
1894                     (msgview->type == MVIEW_MIME &&
1895                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
1896                         gtk_widget_grab_focus(summaryview->ctree);
1897                 GTK_EVENTS_FLUSH();
1898                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
1899         }
1900
1901         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
1902                 header_window_show(summaryview->headerwin, msginfo);
1903
1904         lock = FALSE;
1905 }
1906
1907 void summary_redisplay_msg(SummaryView *summaryview)
1908 {
1909         GtkCTreeNode *node;
1910
1911         if (summaryview->displayed) {
1912                 node = summaryview->displayed;
1913                 summaryview->displayed = NULL;
1914                 summary_display_msg(summaryview, node, FALSE);
1915         }
1916 }
1917
1918 void summary_open_msg(SummaryView *summaryview)
1919 {
1920         if (!summaryview->selected) return;
1921
1922         summary_display_msg(summaryview, summaryview->selected, TRUE);
1923 }
1924
1925 void summary_view_source(SummaryView * summaryview)
1926 {
1927         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1928         MsgInfo *msginfo;
1929         SourceWindow *srcwin;
1930
1931         if (!summaryview->selected) return;
1932
1933         srcwin = source_window_create();
1934         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1935         source_window_show_msg(srcwin, msginfo);
1936         source_window_show(srcwin);
1937 }
1938
1939 void summary_reedit(SummaryView *summaryview)
1940 {
1941         MsgInfo *msginfo;
1942
1943         if (!summaryview->selected) return;
1944         if (!summaryview->folder_item ||
1945             (summaryview->folder_item->stype != F_DRAFT &&
1946              summaryview->folder_item->stype != F_OUTBOX &&
1947              summaryview->folder_item->stype != F_QUEUE)) return;
1948
1949         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
1950                                               summaryview->selected);
1951         if (!msginfo) return;
1952
1953         compose_reedit(msginfo);
1954 }
1955
1956 void summary_step(SummaryView *summaryview, GtkScrollType type)
1957 {
1958         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1959
1960         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
1961                                 type, 0.0);
1962
1963         if (summaryview->msg_is_toggled_on)
1964                 summary_display_msg(summaryview, summaryview->selected, FALSE);
1965 }
1966
1967 static void summary_toggle_view(SummaryView *summaryview)
1968 {
1969         MainWindow *mainwin = summaryview->mainwin;
1970         union CompositeWin *cwin = &mainwin->win;
1971         GtkWidget *vpaned = NULL;
1972         GtkWidget *container = NULL;
1973
1974         switch (mainwin->type) {
1975         case SEPARATE_NONE:
1976                 vpaned = cwin->sep_none.vpaned;
1977                 container = cwin->sep_none.hpaned;
1978                 break;
1979         case SEPARATE_FOLDER:
1980                 vpaned = cwin->sep_folder.vpaned;
1981                 container = mainwin->vbox_body;
1982                 break;
1983         case SEPARATE_MESSAGE:
1984         case SEPARATE_BOTH:
1985                 return;
1986         }
1987
1988         if (vpaned->parent != NULL) {
1989                 summaryview->msg_is_toggled_on = FALSE;
1990                 summaryview->displayed = NULL;
1991                 gtk_widget_ref(vpaned);
1992                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
1993                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1994                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1995                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1996         } else {
1997                 summaryview->msg_is_toggled_on = TRUE;
1998                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1999                 gtk_container_add(GTK_CONTAINER(container), vpaned);
2000                 gtk_widget_unref(vpaned);
2001                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2002                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2003         }
2004
2005         gtk_widget_grab_focus(summaryview->ctree);
2006 }
2007
2008 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2009 {
2010         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2011         MsgInfo *msginfo;
2012         MsgFlags flags;
2013
2014         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2015         if (!msginfo) return;
2016
2017         flags = msginfo->flags;
2018
2019         gtk_ctree_node_set_foreground(ctree, row, &summaryview->color_normal);
2020
2021         /* set new/unread column */
2022         if (MSG_IS_NEW(flags)) {
2023                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2024                                           newxpm, newxpmmask);
2025         } else if (MSG_IS_UNREAD(flags)) {
2026                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2027                                           unreadxpm, unreadxpmmask);
2028         } else if (MSG_IS_REPLIED(flags)) {
2029                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2030                                           repliedxpm, repliedxpmmask);
2031         } else if (MSG_IS_FORWARDED(flags)) {
2032                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2033                                           forwardedxpm, forwardedxpmmask);
2034         } else {
2035                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
2036         }
2037
2038         /* set mark column */
2039         if (MSG_IS_DELETED(flags)) {
2040                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2041                                           deletedxpm, deletedxpmmask);
2042                 gtk_ctree_node_set_foreground(ctree, row,
2043                                               &summaryview->color_dim);
2044         } else if (MSG_IS_MARKED(flags)) {
2045                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2046                                           markxpm, markxpmmask);
2047         } else if (MSG_IS_MOVE(flags)) {
2048                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "o");
2049                 gtk_ctree_node_set_foreground(ctree, row,
2050                                               &summaryview->color_marked);
2051         } else if (MSG_IS_COPY(flags)) {
2052                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
2053                 gtk_ctree_node_set_foreground(ctree, row,
2054                                               &summaryview->color_marked);
2055         }
2056         else if ((global_scoring ||
2057                   summaryview->folder_item->prefs->scoring) &&
2058                  (msginfo->score >= summaryview->important_score) &&
2059                  ((msginfo->flags &
2060                    (MSG_MARKED | MSG_MOVE | MSG_COPY)) == 0)) {
2061                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2062                 gtk_ctree_node_set_foreground(ctree, row,
2063                                               &summaryview->color_important);
2064         } else {
2065                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
2066         }
2067
2068         if (MSG_IS_MIME(flags)) {
2069                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
2070                                           clipxpm, clipxpmmask);
2071         } else {
2072                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
2073         }
2074 }
2075
2076 void summary_set_marks_selected(SummaryView *summaryview)
2077 {
2078         summary_set_row_marks(summaryview, summaryview->selected);
2079 }
2080
2081 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2082 {
2083         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2084         MsgInfo *msginfo;
2085
2086         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2087         msginfo->to_folder = NULL;
2088         if (MSG_IS_DELETED(msginfo->flags))
2089                 summaryview->deleted--;
2090         if (MSG_IS_MOVE(msginfo->flags))
2091                 summaryview->moved--;
2092         if (MSG_IS_COPY(msginfo->flags))
2093                 summaryview->copied--;
2094         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
2095         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
2096
2097         CHANGE_FLAGS(msginfo);
2098
2099         summary_set_row_marks(summaryview, row);
2100         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
2101 }
2102
2103 void summary_mark(SummaryView *summaryview)
2104 {
2105         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2106         GList *cur;
2107
2108         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2109                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2110
2111         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2112         summary_status_show(summaryview);
2113 }
2114
2115 static void summary_mark_row_as_read(SummaryView *summaryview,
2116                                      GtkCTreeNode *row)
2117 {
2118         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2119         MsgInfo *msginfo;
2120
2121         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2122         if (MSG_IS_NEW(msginfo->flags))
2123                 summaryview->newmsgs--;
2124         if (MSG_IS_UNREAD(msginfo->flags))
2125                 summaryview->unread--;
2126         if (MSG_IS_NEW(msginfo->flags) ||
2127             MSG_IS_UNREAD(msginfo->flags)) {
2128                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2129
2130                 CHANGE_FLAGS(msginfo);
2131                 
2132                 summary_set_row_marks(summaryview, row);
2133                 debug_print(_("Message %d is marked as read\n"),
2134                             msginfo->msgnum);
2135         }
2136 }
2137
2138 void summary_mark_as_read(SummaryView *summaryview)
2139 {
2140         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2141         GList *cur;
2142
2143         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2144                 summary_mark_row_as_read(summaryview,
2145                                          GTK_CTREE_NODE(cur->data));
2146
2147         summary_status_show(summaryview);
2148 }
2149
2150 static void summary_mark_row_as_unread(SummaryView *summaryview,
2151                                        GtkCTreeNode *row)
2152 {
2153         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2154         MsgInfo *msginfo;
2155
2156         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2157         if (MSG_IS_DELETED(msginfo->flags)) {
2158                 msginfo->to_folder = NULL;
2159                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2160                 summaryview->deleted--;
2161         }
2162         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2163         if (!MSG_IS_UNREAD(msginfo->flags)) {
2164                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
2165
2166                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2167                                           unreadxpm, unreadxpmmask);
2168                 summaryview->unread++;
2169                 debug_print(_("Message %d is marked as unread\n"),
2170                             msginfo->msgnum);
2171         }
2172
2173         CHANGE_FLAGS(msginfo);
2174
2175         summary_set_row_marks(summaryview, row);
2176 }
2177
2178 void summary_mark_as_unread(SummaryView *summaryview)
2179 {
2180         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2181         GList *cur;
2182
2183         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2184                 summary_mark_row_as_unread(summaryview,
2185                                            GTK_CTREE_NODE(cur->data));
2186
2187         summary_status_show(summaryview);
2188 }
2189
2190 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2191 {
2192         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2193         MsgInfo *msginfo;
2194
2195         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2196
2197         if (MSG_IS_DELETED(msginfo->flags)) return;
2198
2199         msginfo->to_folder = NULL;
2200         if (MSG_IS_MOVE(msginfo->flags))
2201                 summaryview->moved--;
2202         if (MSG_IS_COPY(msginfo->flags))
2203                 summaryview->copied--;
2204         MSG_UNSET_FLAGS(msginfo->flags,
2205                         MSG_MARKED |
2206                         MSG_MOVE |
2207                         MSG_COPY);
2208         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
2209
2210         CHANGE_FLAGS(msginfo);
2211
2212         summaryview->deleted++;
2213
2214         if (!prefs_common.immediate_exec)
2215                 summary_set_row_marks(summaryview, row);
2216
2217         debug_print(_("Message %s/%d is set to delete\n"),
2218                     msginfo->folder->path, msginfo->msgnum);
2219 }
2220
2221 void summary_delete(SummaryView *summaryview)
2222 {
2223         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2224         GList *cur;
2225
2226         if (!summaryview->folder_item ||
2227             summaryview->folder_item->folder->type == F_NEWS) return;
2228
2229         /* if current folder is trash, don't delete */
2230         if (summaryview->folder_item->stype == F_TRASH) {
2231                 alertpanel_notice(_("Current folder is Trash."));
2232                 return;
2233         }
2234
2235         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2236                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
2237
2238         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2239
2240         if (prefs_common.immediate_exec)
2241                 summary_execute(summaryview);
2242         else
2243                 summary_status_show(summaryview);
2244 }
2245
2246 void summary_delete_duplicated(SummaryView *summaryview)
2247 {
2248         if (!summaryview->folder_item ||
2249             summaryview->folder_item->folder->type == F_NEWS) return;
2250         if (summaryview->folder_item->stype == F_TRASH) return;
2251
2252         main_window_cursor_wait(summaryview->mainwin);
2253         debug_print(_("Deleting duplicated messages..."));
2254         STATUSBAR_PUSH(summaryview->mainwin,
2255                        _("Deleting duplicated messages..."));
2256
2257         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2258                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2259                                 summaryview);
2260
2261         if (prefs_common.immediate_exec)
2262                 summary_execute(summaryview);
2263         else
2264                 summary_status_show(summaryview);
2265
2266         debug_print(_("done.\n"));
2267         STATUSBAR_POP(summaryview->mainwin);
2268         main_window_cursor_normal(summaryview->mainwin);
2269 }
2270
2271 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2272                                            SummaryView *summaryview)
2273 {
2274         GtkCTreeNode *found;
2275         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2276
2277         if (!msginfo->msgid || !*msginfo->msgid) return;
2278
2279         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2280
2281         if (found && found != node)
2282                 summary_delete_row(summaryview, node);
2283 }
2284
2285 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2286 {
2287         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2288         MsgInfo *msginfo;
2289
2290         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2291         msginfo->to_folder = NULL;
2292         if (MSG_IS_DELETED(msginfo->flags))
2293                 summaryview->deleted--;
2294         if (MSG_IS_MOVE(msginfo->flags))
2295                 summaryview->moved--;
2296         if (MSG_IS_COPY(msginfo->flags))
2297                 summaryview->copied--;
2298         MSG_UNSET_FLAGS(msginfo->flags,
2299                         MSG_MARKED |
2300                         MSG_DELETED |
2301                         MSG_MOVE |
2302                         MSG_COPY);
2303
2304         CHANGE_FLAGS(msginfo);
2305
2306         summary_set_row_marks(summaryview, row);
2307
2308         debug_print(_("Message %s/%d is unmarked\n"),
2309                     msginfo->folder->path, msginfo->msgnum);
2310 }
2311
2312 void summary_unmark(SummaryView *summaryview)
2313 {
2314         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2315         GList *cur;
2316
2317         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2318              cur = cur->next)
2319                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2320
2321         summary_status_show(summaryview);
2322 }
2323
2324 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2325                                 FolderItem *to_folder)
2326 {
2327         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2328         MsgInfo *msginfo;
2329
2330         g_return_if_fail(to_folder != NULL);
2331
2332         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2333         msginfo->to_folder = to_folder;
2334         if (MSG_IS_DELETED(msginfo->flags))
2335                 summaryview->deleted--;
2336         MSG_UNSET_FLAGS(msginfo->flags,
2337                         MSG_MARKED | MSG_DELETED | MSG_COPY);
2338         if (!MSG_IS_MOVE(msginfo->flags)) {
2339                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
2340                 summaryview->moved++;
2341         }
2342
2343         if (!prefs_common.immediate_exec)
2344                 summary_set_row_marks(summaryview, row);
2345
2346         debug_print(_("Message %d is set to move to %s\n"),
2347                     msginfo->msgnum, to_folder->path);
2348 }
2349
2350 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2351 {
2352         GList *cur;
2353
2354         if (!to_folder) return;
2355         if (!summaryview->folder_item ||
2356             summaryview->folder_item->folder->type == F_NEWS) return;
2357         if (summaryview->folder_item == to_folder) {
2358                 alertpanel_notice(_("Destination is same as current folder."));
2359                 return;
2360         }
2361
2362         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2363              cur != NULL; cur = cur->next)
2364                 summary_move_row_to
2365                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2366
2367         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2368
2369         if (prefs_common.immediate_exec)
2370                 summary_execute(summaryview);
2371         else
2372                 summary_status_show(summaryview);
2373 }
2374
2375 void summary_move_to(SummaryView *summaryview)
2376 {
2377         FolderItem *to_folder;
2378
2379         if (!summaryview->folder_item ||
2380             summaryview->folder_item->folder->type == F_NEWS) return;
2381
2382         to_folder = foldersel_folder_sel(NULL);
2383         summary_move_selected_to(summaryview, to_folder);
2384 }
2385
2386 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2387                                 FolderItem *to_folder)
2388 {
2389         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2390         MsgInfo *msginfo;
2391
2392         g_return_if_fail(to_folder != NULL);
2393
2394         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2395         msginfo->to_folder = to_folder;
2396         if (MSG_IS_DELETED(msginfo->flags))
2397                 summaryview->deleted--;
2398         MSG_UNSET_FLAGS(msginfo->flags,
2399                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
2400         if (!MSG_IS_COPY(msginfo->flags)) {
2401                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
2402                 summaryview->copied++;
2403         }
2404
2405         if (!prefs_common.immediate_exec)
2406                 summary_set_row_marks(summaryview, row);
2407
2408         debug_print(_("Message %d is set to copy to %s\n"),
2409                     msginfo->msgnum, to_folder->path);
2410 }
2411
2412 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2413 {
2414         GList *cur;
2415
2416         if (!to_folder) return;
2417         if (!summaryview->folder_item ||
2418             summaryview->folder_item->folder->type == F_NEWS) return;
2419         if (summaryview->folder_item == to_folder) {
2420                 alertpanel_notice
2421                         (_("Destination to copy is same as current folder."));
2422                 return;
2423         }
2424
2425         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2426              cur != NULL; cur = cur->next)
2427                 summary_copy_row_to
2428                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2429
2430         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2431
2432         if (prefs_common.immediate_exec)
2433                 summary_execute(summaryview);
2434         else
2435                 summary_status_show(summaryview);
2436 }
2437
2438 void summary_copy_to(SummaryView *summaryview)
2439 {
2440         FolderItem *to_folder;
2441
2442         if (!summaryview->folder_item ||
2443             summaryview->folder_item->folder->type == F_NEWS) return;
2444
2445         to_folder = foldersel_folder_sel(NULL);
2446         summary_copy_selected_to(summaryview, to_folder);
2447 }
2448
2449 void summary_select_all(SummaryView *summaryview)
2450 {
2451         if (summaryview->messages >= 500) {
2452                 STATUSBAR_PUSH(summaryview->mainwin,
2453                                _("Selecting all messages..."));
2454                 main_window_cursor_wait(summaryview->mainwin);
2455         }
2456
2457         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2458
2459         if (summaryview->messages >= 500) {
2460                 STATUSBAR_POP(summaryview->mainwin);
2461                 main_window_cursor_normal(summaryview->mainwin);
2462         }
2463 }
2464
2465 void summary_unselect_all(SummaryView *summaryview)
2466 {
2467         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2468 }
2469
2470 void summary_save_as(SummaryView *summaryview)
2471 {
2472         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2473         MsgInfo *msginfo;
2474         gchar *src, *dest;
2475
2476         if (!summaryview->selected) return;
2477         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2478         if (!msginfo) return;
2479
2480         dest = filesel_select_file(_("Save as"), NULL);
2481         if (!dest) return;
2482         if (is_file_exist(dest)) {
2483                 AlertValue aval;
2484
2485                 aval = alertpanel(_("Overwrite"),
2486                                   _("Overwrite existing file?"),
2487                                   _("OK"), _("Cancel"), NULL);
2488                 if (G_ALERTDEFAULT != aval) return;
2489         }
2490
2491         src = procmsg_get_message_file(msginfo);
2492         copy_file(src, dest);
2493         g_free(src);
2494 }
2495
2496 void summary_print(SummaryView *summaryview)
2497 {
2498         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2499         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2500         MsgInfo *msginfo;
2501         GList *cur;
2502         gchar *cmdline;
2503         gchar *p;
2504
2505         if (clist->selection == NULL) return;
2506
2507         cmdline = input_dialog(_("Print"),
2508                                _("Enter the print command line:\n"
2509                                  "(`%s' will be replaced with file name)"),
2510                                prefs_common.print_cmd);
2511         if (!cmdline) return;
2512         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2513             strchr(p + 2, '%')) {
2514                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2515                                  cmdline);
2516                 g_free(cmdline);
2517                 return;
2518         }
2519
2520         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2521                 msginfo = gtk_ctree_node_get_row_data
2522                         (ctree, GTK_CTREE_NODE(cur->data));
2523                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2524         }
2525         
2526         g_free(cmdline);
2527 }
2528
2529 void summary_execute(SummaryView *summaryview)
2530 {
2531         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2532         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2533         GtkCTreeNode *node, *next;
2534
2535         if (!summaryview->folder_item ||
2536             summaryview->folder_item->folder->type == F_NEWS) return;
2537
2538         gtk_clist_freeze(clist);
2539
2540         if (summaryview->folder_item->prefs->enable_thread)
2541                 /*      if (prefs_common.enable_thread) */
2542                 summary_unthread_for_exec(summaryview);
2543
2544         summary_execute_move(summaryview);
2545         summary_execute_copy(summaryview);
2546         summary_execute_delete(summaryview);
2547
2548         node = GTK_CTREE_NODE(clist->row_list);
2549         while (node != NULL) {
2550                 next = GTK_CTREE_NODE_NEXT(node);
2551                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2552                         if (node == summaryview->displayed) {
2553                                 messageview_clear(summaryview->messageview);
2554                                 summaryview->displayed = NULL;
2555                         }
2556                         if (GTK_CTREE_ROW(node)->children != NULL)
2557                                 g_warning("summary_execute(): children != NULL\n");
2558                         else
2559                                 gtk_ctree_remove_node(ctree, node);
2560                 }
2561                 node = next;
2562         }
2563
2564         if (summaryview->folder_item->prefs->enable_thread)
2565                 /*      if (prefs_common.enable_thread) */
2566                 summary_thread_build(summaryview);
2567
2568         summaryview->selected = clist->selection ?
2569                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2570
2571         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2572                 menu_set_insensitive_all
2573                         (GTK_MENU_SHELL(summaryview->popupmenu));
2574                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2575         } else
2576                 gtk_widget_grab_focus(summaryview->ctree);
2577
2578         summary_update_status(summaryview);
2579         summary_status_show(summaryview);
2580
2581         summary_write_cache(summaryview);
2582
2583         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2584
2585         gtk_clist_thaw(clist);
2586 }
2587
2588 static void summary_execute_move(SummaryView *summaryview)
2589 {
2590         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2591         GSList *cur;
2592
2593         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2594
2595         /* search moving messages and execute */
2596         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2597                                 summaryview);
2598
2599         if (summaryview->mlist) {
2600                 procmsg_move_messages(summaryview->mlist);
2601
2602                 folder_item_scan_foreach(summaryview->folder_table);
2603                 folderview_update_item_foreach(summaryview->folder_table);
2604
2605                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2606                         procmsg_msginfo_free((MsgInfo *)cur->data);
2607                 g_slist_free(summaryview->mlist);
2608                 summaryview->mlist = NULL;
2609         }
2610
2611         g_hash_table_destroy(summaryview->folder_table);
2612         summaryview->folder_table = NULL;
2613 }
2614
2615 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2616                                       gpointer data)
2617 {
2618         SummaryView *summaryview = data;
2619         MsgInfo *msginfo;
2620
2621         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2622
2623         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2624                 g_hash_table_insert(summaryview->folder_table,
2625                                     msginfo->to_folder, GINT_TO_POINTER(1));
2626
2627                 summaryview->mlist =
2628                         g_slist_append(summaryview->mlist, msginfo);
2629                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2630
2631                 if (msginfo->msgid && *msginfo->msgid &&
2632                     node == g_hash_table_lookup(summaryview->msgid_table,
2633                                                 msginfo->msgid))
2634                         g_hash_table_remove(summaryview->msgid_table,
2635                                             msginfo->msgid);
2636         }
2637 }
2638
2639 static void summary_execute_copy(SummaryView *summaryview)
2640 {
2641         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2642
2643         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2644
2645         /* search copying messages and execute */
2646         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2647                                 summaryview);
2648
2649         if (summaryview->mlist) {
2650                 procmsg_copy_messages(summaryview->mlist);
2651
2652                 folder_item_scan_foreach(summaryview->folder_table);
2653                 folderview_update_item_foreach(summaryview->folder_table);
2654
2655                 g_slist_free(summaryview->mlist);
2656                 summaryview->mlist = NULL;
2657         }
2658
2659         g_hash_table_destroy(summaryview->folder_table);
2660         summaryview->folder_table = NULL;
2661 }
2662
2663 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
2664                                       gpointer data)
2665 {
2666         SummaryView *summaryview = data;
2667         MsgInfo *msginfo;
2668
2669         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2670
2671         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
2672                 g_hash_table_insert(summaryview->folder_table,
2673                                     msginfo->to_folder, GINT_TO_POINTER(1));
2674
2675                 summaryview->mlist =
2676                         g_slist_append(summaryview->mlist, msginfo);
2677
2678                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
2679                 
2680                 summary_set_row_marks(summaryview, node);
2681         }
2682 }
2683
2684 static void summary_execute_delete(SummaryView *summaryview)
2685 {
2686         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2687         FolderItem *trash;
2688         GSList *cur;
2689
2690         trash = summaryview->folder_item->folder->trash;
2691         if (summaryview->folder_item->folder->type == F_MH) {
2692                 g_return_if_fail(trash != NULL);
2693         }
2694         if (summaryview->folder_item == trash) return;
2695
2696         /* search deleting messages and execute */
2697         gtk_ctree_pre_recursive
2698                 (ctree, NULL, summary_execute_delete_func, summaryview);
2699
2700         if (!summaryview->mlist) return;
2701
2702         for(cur = summaryview->mlist ; cur != NULL ; cur = cur->next) {
2703                 MsgInfo * msginfo = cur->data;
2704                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2705         }
2706
2707         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
2708
2709         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2710                 procmsg_msginfo_free((MsgInfo *)cur->data);
2711
2712         g_slist_free(summaryview->mlist);
2713         summaryview->mlist = NULL;
2714
2715         folder_item_scan(trash);
2716         folderview_update_item(trash, FALSE);
2717 }
2718
2719 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
2720                                         gpointer data)
2721 {
2722         SummaryView *summaryview = data;
2723         MsgInfo *msginfo;
2724
2725         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2726
2727         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
2728                 summaryview->mlist =
2729                         g_slist_append(summaryview->mlist, msginfo);
2730                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2731
2732                 if (msginfo->msgid && *msginfo->msgid &&
2733                     node == g_hash_table_lookup(summaryview->msgid_table,
2734                                                 msginfo->msgid))
2735                         g_hash_table_remove(summaryview->msgid_table,
2736                                             msginfo->msgid);
2737         }
2738 }
2739
2740 /* thread functions */
2741
2742 void summary_thread_build(SummaryView *summaryview)
2743 {
2744         debug_print(_("Building threads..."));
2745         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
2746         main_window_cursor_wait(summaryview->mainwin);
2747
2748         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2749
2750         gtk_ctree_pre_recursive_to_depth
2751                 (GTK_CTREE(summaryview->ctree), NULL, 1,
2752                  GTK_CTREE_FUNC(summary_thread_func),
2753                  summaryview);
2754
2755         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2756
2757         debug_print(_("done.\n"));
2758         STATUSBAR_POP(summaryview->mainwin);
2759         main_window_cursor_normal(summaryview->mainwin);
2760 }
2761
2762 void summary_unthread(SummaryView *summaryview)
2763 {
2764         GtkCTreeNode *node;
2765         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2766
2767         debug_print(_("Unthreading..."));
2768         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
2769         main_window_cursor_wait(summaryview->mainwin);
2770
2771         gtk_clist_freeze(GTK_CLIST(ctree));
2772
2773         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2774              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2775                 summary_unthread_func(ctree, node, NULL);
2776         }
2777
2778         gtk_clist_thaw(GTK_CLIST(ctree));
2779
2780         debug_print(_("done.\n"));
2781         STATUSBAR_POP(summaryview->mainwin);
2782         main_window_cursor_normal(summaryview->mainwin);
2783 }
2784
2785 static void summary_unthread_for_exec(SummaryView *summaryview)
2786 {
2787         GtkCTreeNode *node;
2788         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2789
2790         debug_print(_("Unthreading for execution..."));
2791
2792         gtk_clist_freeze(GTK_CLIST(ctree));
2793
2794         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2795              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2796                 summary_unthread_for_exec_func(ctree, node, NULL);
2797         }
2798
2799         gtk_clist_thaw(GTK_CLIST(ctree));
2800
2801         debug_print(_("done.\n"));
2802 }
2803
2804 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
2805                                 gpointer data)
2806 {
2807         MsgInfo *msginfo;
2808         GtkCTreeNode *parent = NULL;
2809         
2810         SummaryView * summaryview = (SummaryView *) data;
2811         GHashTable *msgid_table = summaryview->msgid_table;
2812         GHashTable *subject_table = summaryview->subject_table;
2813
2814         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2815
2816         if (!msginfo) return;
2817
2818         if(msginfo->inreplyto) {
2819             parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
2820         }
2821         if (parent == NULL && msginfo->subject) {
2822                 parent = subject_table_lookup(subject_table, msginfo->subject);
2823         }
2824
2825         if (parent && parent != node) {
2826                 gtk_ctree_move(ctree, node, parent, NULL);
2827                 gtk_ctree_expand(ctree, parent);
2828         }
2829 }
2830
2831 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
2832                                   gpointer data)
2833 {
2834         GtkCTreeNode *child;
2835         GtkCTreeNode *sibling;
2836
2837         child = GTK_CTREE_ROW(node)->children;
2838         sibling = GTK_CTREE_ROW(node)->sibling;
2839
2840         while (child != NULL) {
2841                 GtkCTreeNode *next_child;
2842
2843                 next_child = GTK_CTREE_ROW(child)->sibling;
2844                 gtk_ctree_move(ctree, child, NULL, sibling);
2845                 child = next_child;
2846         }
2847 }
2848
2849 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
2850                                            gpointer data)
2851 {
2852         MsgInfo *msginfo;
2853         GtkCTreeNode *top_parent;
2854         GtkCTreeNode *child;
2855         GtkCTreeNode *sibling;
2856
2857         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2858
2859         if (!msginfo ||
2860             (!MSG_IS_MOVE(msginfo->flags) &&
2861              !MSG_IS_DELETED(msginfo->flags)))
2862                 return;
2863         child = GTK_CTREE_ROW(node)->children;
2864         if (!child) return;
2865
2866         for (top_parent = node;
2867              GTK_CTREE_ROW(top_parent)->parent != NULL;
2868              top_parent = GTK_CTREE_ROW(top_parent)->parent)
2869                 ;
2870         sibling = GTK_CTREE_ROW(top_parent)->sibling;
2871
2872         while (child != NULL) {
2873                 GtkCTreeNode *next_child;
2874
2875                 next_child = GTK_CTREE_ROW(child)->sibling;
2876                 gtk_ctree_move(ctree, child, NULL, sibling);
2877                 child = next_child;
2878         }
2879 }
2880
2881 void summary_filter(SummaryView *summaryview)
2882 {
2883         if (!prefs_common.fltlist) return;
2884
2885         debug_print(_("filtering..."));
2886         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
2887         main_window_cursor_wait(summaryview->mainwin);
2888
2889         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2890
2891         if (prefs_filtering == NULL) {
2892                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2893                                         GTK_CTREE_FUNC(summary_filter_func),
2894                                         summaryview);
2895                 
2896                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2897
2898                 if (prefs_common.immediate_exec)
2899                         summary_execute(summaryview);
2900                 else
2901                         summary_status_show(summaryview);
2902         }
2903         else {
2904                 summaryview->folder_table = g_hash_table_new(NULL, NULL);
2905
2906                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2907                                         GTK_CTREE_FUNC(summary_filter_func),
2908                                         summaryview);
2909
2910                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2911
2912                 folder_item_scan_foreach(summaryview->folder_table);
2913                 folderview_update_item_foreach(summaryview->folder_table);
2914
2915                 g_hash_table_destroy(summaryview->folder_table);
2916                 summaryview->folder_table = NULL;
2917
2918                 summary_show(summaryview, summaryview->folder_item, FALSE);
2919         }
2920
2921         debug_print(_("done.\n"));
2922         STATUSBAR_POP(summaryview->mainwin);
2923         main_window_cursor_normal(summaryview->mainwin);
2924 }
2925
2926 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
2927                                 gpointer data)
2928 {
2929         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2930         SummaryView *summaryview = data;
2931         gchar *file;
2932         FolderItem *dest;
2933
2934         if (prefs_filtering == NULL) {
2935                 /* old filtering */
2936                 file = procmsg_get_message_file_path(msginfo);
2937                 dest = filter_get_dest_folder(prefs_common.fltlist, file);
2938                 g_free(file);
2939
2940                 if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
2941                     summaryview->folder_item != dest)
2942                         summary_move_row_to(summaryview, node, dest);
2943         }
2944         else
2945                 filter_msginfo_move_or_delete(prefs_filtering, msginfo,
2946                                               summaryview->folder_table);
2947 }
2948
2949 /* callback functions */
2950
2951 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
2952                                    SummaryView *summaryview)
2953 {
2954         if (!event)
2955                 return;
2956
2957         if (!summaryview->msg_is_toggled_on && summaryview->selected)
2958                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2959         else
2960                 summary_toggle_view(summaryview);
2961 }
2962
2963 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2964                                    SummaryView *summaryview)
2965 {
2966         if (!event) return;
2967
2968         if (event->button == 3) {
2969                 /* right clicked */
2970                 summary_add_sender_to_cb(summaryview, 0, 0);    
2971                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
2972                                NULL, NULL, event->button, event->time);
2973         } else if (event->button == 2) {
2974                 summaryview->display_msg = TRUE;
2975         } else if (event->button == 1) {
2976                 if (!prefs_common.emulate_emacs &&
2977                     summaryview->msg_is_toggled_on)
2978                         summaryview->display_msg = TRUE;
2979         }
2980 }
2981
2982 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
2983                                     SummaryView *summaryview)
2984 {
2985 }
2986
2987 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
2988 {
2989         summary_key_pressed(summaryview->ctree, event, summaryview);
2990 }
2991
2992 #define BREAK_ON_MODIFIER_KEY() \
2993         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2994
2995 #define KEY_PRESS_EVENT_STOP() \
2996         if (gtk_signal_n_emissions_by_name \
2997                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
2998                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
2999                                              "key_press_event"); \
3000         }
3001
3002 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
3003                                 SummaryView *summaryview)
3004 {
3005         GtkCTree *ctree = GTK_CTREE(widget);
3006         GtkCTreeNode *node;
3007         FolderItem *to_folder;
3008
3009         if (!event) return;
3010
3011         switch (event->keyval) {
3012         case GDK_g:             /* Go */
3013         case GDK_G:
3014                 BREAK_ON_MODIFIER_KEY();
3015                 KEY_PRESS_EVENT_STOP();
3016                 to_folder = foldersel_folder_sel(NULL);
3017                 if (to_folder) {
3018                         debug_print(_("Go to %s\n"), to_folder->path);
3019                         folderview_select(summaryview->folderview, to_folder);
3020                 }
3021                 return;
3022         case GDK_w:             /* Write new message */
3023                 BREAK_ON_MODIFIER_KEY();
3024                 if (summaryview->folder_item) {
3025                         PrefsAccount *ac;
3026                         ac = summaryview->folder_item->folder->account;
3027                         if (ac && ac->protocol == A_NNTP)
3028                                 compose_new_with_recipient
3029                                         (ac, summaryview->folder_item->path);
3030                         else
3031                                 compose_new(ac);
3032                 } else
3033                         compose_new(NULL);
3034                 return;
3035         case GDK_D:             /* Empty trash */
3036                 BREAK_ON_MODIFIER_KEY();
3037                 KEY_PRESS_EVENT_STOP();
3038                 main_window_empty_trash(summaryview->mainwin, TRUE);
3039                 return;
3040         case GDK_Q:             /* Quit */
3041                 BREAK_ON_MODIFIER_KEY();
3042
3043                 if (prefs_common.confirm_on_exit) {
3044                         if (alertpanel(_("Exit"), _("Exit this program?"),
3045                                        _("OK"), _("Cancel"), NULL)
3046                                        == G_ALERTDEFAULT) {
3047                                 manage_window_focus_in
3048                                         (summaryview->mainwin->window,
3049                                          NULL, NULL);
3050                                 app_will_exit(NULL, summaryview->mainwin);
3051                         }
3052                 }
3053                 return;
3054         case GDK_Left:          /* Move focus */
3055         case GDK_Escape:
3056                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3057                 return;
3058         default:
3059         }
3060
3061         if (!summaryview->selected) {
3062                 node = gtk_ctree_node_nth(ctree, 0);
3063                 if (node)
3064                         gtk_ctree_select(ctree, node);
3065                 else
3066                         return;
3067         }
3068
3069         switch (event->keyval) {
3070         case GDK_space:         /* Page down or go to the next */
3071                 if (summaryview->displayed != summaryview->selected) {
3072                         summary_display_msg(summaryview,
3073                                             summaryview->selected, FALSE);
3074                         break;
3075                 }
3076                 if (!textview_scroll_page(summaryview->messageview->textview,
3077                                           FALSE))
3078                         summary_select_next_unread(summaryview);
3079                 break;
3080         case GDK_n:             /* Next */
3081         case GDK_N:
3082                 BREAK_ON_MODIFIER_KEY();
3083                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3084                 break;
3085         case GDK_BackSpace:     /* Page up */
3086         case GDK_Delete:
3087                 textview_scroll_page(summaryview->messageview->textview, TRUE);
3088                 break;
3089         case GDK_p:             /* Prev */
3090         case GDK_P:
3091                 BREAK_ON_MODIFIER_KEY();
3092                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
3093                 break;
3094         case GDK_v:             /* Toggle summary mode / message mode */
3095         case GDK_V:
3096                 BREAK_ON_MODIFIER_KEY();
3097
3098                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
3099                         summary_display_msg(summaryview,
3100                                             summaryview->selected, FALSE);
3101                 else
3102                         summary_toggle_view(summaryview);
3103                 break;
3104         case GDK_Return:        /* Scroll up/down one line */
3105                 if (summaryview->displayed != summaryview->selected) {
3106                         summary_display_msg(summaryview,
3107                                             summaryview->selected, FALSE);
3108                         break;
3109                 }
3110                 textview_scroll_one_line(summaryview->messageview->textview,
3111                                          (event->state & GDK_MOD1_MASK) != 0);
3112                 break;
3113         case GDK_asterisk:      /* Mark */
3114                 summary_mark(summaryview);
3115                 break;
3116         case GDK_exclam:        /* Mark as unread */
3117                 summary_mark_as_unread(summaryview);
3118                 break;
3119         case GDK_d:             /* Delete */
3120                 BREAK_ON_MODIFIER_KEY();
3121                 summary_delete(summaryview);
3122                 break;
3123         case GDK_u:             /* Unmark */
3124         case GDK_U:
3125                 BREAK_ON_MODIFIER_KEY();
3126                 summary_unmark(summaryview);
3127                 break;
3128         case GDK_o:             /* Move */
3129                 BREAK_ON_MODIFIER_KEY();
3130                 summary_move_to(summaryview);
3131                 break;
3132         case GDK_O:             /* Copy */
3133                 BREAK_ON_MODIFIER_KEY();
3134                 summary_copy_to(summaryview);
3135                 break;
3136         case GDK_x:             /* Execute */
3137         case GDK_X:
3138                 BREAK_ON_MODIFIER_KEY();
3139                 KEY_PRESS_EVENT_STOP();
3140                 summary_execute(summaryview);
3141                 break;
3142         case GDK_a:             /* Reply to the message */
3143                 BREAK_ON_MODIFIER_KEY();
3144                 summary_reply_cb(summaryview,
3145                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
3146                 break;
3147         case GDK_A:             /* Reply to the message with quotation */
3148                 BREAK_ON_MODIFIER_KEY();
3149                 summary_reply_cb(summaryview,
3150                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
3151                 break;
3152         case GDK_f:             /* Forward the message */
3153                 BREAK_ON_MODIFIER_KEY();
3154                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
3155                 break;
3156         case GDK_F:
3157                 BREAK_ON_MODIFIER_KEY();
3158                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
3159                 break;
3160         case GDK_y:             /* Save the message */
3161                 BREAK_ON_MODIFIER_KEY();
3162                 summary_save_as(summaryview);
3163                 break;
3164         default:
3165         }
3166 }
3167
3168 #undef BREAK_ON_MODIFIER_KEY
3169 #undef KEY_PRESS_EVENT_STOP
3170
3171 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
3172 {
3173         if (summaryview->folder_item->stype == F_DRAFT)
3174                 summary_reedit(summaryview);
3175         else
3176                 summary_open_msg(summaryview);
3177
3178         summaryview->display_msg = FALSE;
3179 }
3180
3181 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
3182                              gint column, SummaryView *summaryview)
3183 {
3184         MsgInfo *msginfo;
3185
3186         summary_status_show(summaryview);
3187         summary_set_menu_sensitive(summaryview);
3188
3189         if (GTK_CLIST(ctree)->selection &&
3190              GTK_CLIST(ctree)->selection->next) {
3191                 summaryview->display_msg = FALSE;
3192                 return;
3193         }
3194
3195         summaryview->selected = row;
3196
3197         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3198
3199         switch (column) {
3200         case S_COL_MARK:
3201                 if (MSG_IS_MARKED(msginfo->flags)) {
3202                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
3203
3204                         CHANGE_FLAGS(msginfo);
3205                         
3206                         summary_set_row_marks(summaryview, row);
3207                 } else
3208                         summary_mark_row(summaryview, row);
3209                 break;
3210         case S_COL_UNREAD:
3211                 if (MSG_IS_UNREAD(msginfo->flags)) {
3212                         summary_mark_row_as_read(summaryview, row);
3213                         summary_status_show(summaryview);
3214                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
3215                          !MSG_IS_FORWARDED(msginfo->flags)) {
3216                         summary_mark_row_as_unread(summaryview, row);
3217                         summary_status_show(summaryview);
3218                 }
3219                 break;
3220         default:
3221         }
3222
3223         if (summaryview->display_msg)
3224                 summary_display_msg(summaryview, row, FALSE);
3225
3226         summaryview->display_msg = FALSE;
3227 }
3228
3229 static void summary_col_resized(GtkCList *clist, gint column, gint width,
3230                                 SummaryView *summaryview)
3231 {
3232         switch (column) {
3233         case S_COL_MARK:
3234                 prefs_common.summary_col_mark = width;
3235                 break;
3236         case S_COL_UNREAD:
3237                 prefs_common.summary_col_unread = width;
3238                 break;
3239         case S_COL_MIME:
3240                 prefs_common.summary_col_mime = width;
3241                 break;
3242         case S_COL_NUMBER:
3243                 prefs_common.summary_col_number = width;
3244                 break;
3245         case S_COL_SCORE:
3246                 prefs_common.summary_col_score = width;
3247                 break;
3248         case S_COL_SIZE:
3249                 prefs_common.summary_col_size = width;
3250                 break;
3251         case S_COL_DATE:
3252                 prefs_common.summary_col_date = width;
3253                 break;
3254         case S_COL_FROM:
3255                 prefs_common.summary_col_from = width;
3256                 break;
3257         case S_COL_SUBJECT:
3258                 prefs_common.summary_col_subject = width;
3259                 break;
3260         default:
3261         }
3262 }
3263
3264 static void summary_reply_cb(SummaryView *summaryview, guint action,
3265                              GtkWidget *widget)
3266 {
3267         MsgInfo *msginfo;
3268
3269         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3270                                               summaryview->selected);
3271         if (!msginfo) return;
3272
3273         switch ((ComposeReplyMode)action) {
3274         case COMPOSE_REPLY:
3275                 compose_reply(msginfo, prefs_common.reply_with_quote,
3276                               FALSE, FALSE);
3277                 break;
3278         case COMPOSE_REPLY_WITH_QUOTE:
3279                 compose_reply(msginfo, TRUE, FALSE, FALSE);
3280                 break;
3281         case COMPOSE_REPLY_WITHOUT_QUOTE:
3282                 compose_reply(msginfo, FALSE, FALSE, FALSE);
3283                 break;
3284         case COMPOSE_REPLY_TO_SENDER:
3285                 compose_reply(msginfo, prefs_common.reply_with_quote,
3286                               FALSE, TRUE);
3287                 break;
3288         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
3289                 compose_followup_and_reply_to(msginfo,
3290                                               prefs_common.reply_with_quote,
3291                                               FALSE, TRUE);
3292                 break;
3293         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
3294                 compose_reply(msginfo, TRUE, FALSE, TRUE);
3295                 break;
3296         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
3297                 compose_reply(msginfo, FALSE, FALSE, TRUE);
3298                 break;
3299         case COMPOSE_REPLY_TO_ALL:
3300                 compose_reply(msginfo, prefs_common.reply_with_quote,
3301                               TRUE, FALSE);
3302                 break;
3303         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
3304                 compose_reply(msginfo, TRUE, TRUE, FALSE);
3305                 break;
3306         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
3307                 compose_reply(msginfo, FALSE, TRUE, FALSE);
3308                 break;
3309         case COMPOSE_FORWARD:
3310                 compose_forward(NULL, msginfo, FALSE);
3311                 break;
3312         case COMPOSE_FORWARD_AS_ATTACH:
3313                 compose_forward(NULL, msginfo, TRUE);
3314                 break;
3315         default:
3316                 g_warning("summary_reply_cb(): invalid action: %d\n", action);
3317         }
3318
3319         summary_set_marks_selected(summaryview);
3320 }
3321
3322 static void summary_show_all_header_cb(SummaryView *summaryview,
3323                                        guint action, GtkWidget *widget)
3324 {
3325         header_window_show_cb(summaryview->mainwin, action, widget);
3326 }
3327
3328 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
3329                                          guint                   action,
3330                                          GtkWidget              *widget_)
3331 {
3332         GtkWidget               *submenu;                                               
3333         GList                   *groups, *tmp;
3334         GtkMenuShell    *menushell;
3335         GtkWidget               *menu;
3336         GtkWidget               *menuitem;
3337         GList                   *child;
3338         gboolean                found = FALSE;
3339         MsgInfo                 *msginfo;
3340         gchar                   *from_address;
3341
3342         menushell = GTK_MENU_SHELL(summaryview->popupmenu);
3343         g_return_if_fail(menushell != NULL);
3344         child = menushell->children;
3345         g_return_if_fail(child);
3346
3347         /* we're iterating each menu item searching for the one with 
3348          * a "contacts" object data. if not found add the menu,
3349          * else update it */
3350         for (child = g_list_first(menushell->children); child; child = g_list_next(child)) {
3351                 if (gtk_object_get_data(GTK_OBJECT(child->data), "contacts")) {
3352                         found = TRUE;
3353                         break;
3354                 }
3355         }
3356
3357         /* add item to default context menu if not present */
3358         if (!found) {
3359                 submenu = gtk_menu_item_new_with_label(_("Add sender to address book"));
3360                 gtk_object_set_data(GTK_OBJECT(submenu), "contacts", (gpointer)1);
3361                 gtk_menu_insert(GTK_MENU(summaryview->popupmenu), submenu, 12);
3362                 gtk_widget_show(submenu);
3363         }
3364         else {
3365                 submenu = (GtkWidget *) child->data;
3366         }
3367
3368         /* get the address info from the summary view */
3369         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3370                                                                                                    summaryview->selected);
3371
3372         if (msginfo != NULL && msginfo->fromname != NULL && msginfo->from != NULL) {
3373                 gtk_widget_set_sensitive(GTK_WIDGET(submenu), TRUE);
3374                 from_address = g_strdup(msginfo->from);
3375                 eliminate_address_comment(from_address);
3376                 extract_address(from_address);