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