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