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