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