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