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