13bc81db132a0fd40470a313fddbd001ae7df2f8
[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
1540         text[S_COL_MARK]   = NULL;
1541         text[S_COL_UNREAD] = NULL;
1542         text[S_COL_MIME] = NULL;
1543         text[S_COL_NUMBER] = itos(msginfo->msgnum);
1544         text[S_COL_SIZE]   = to_human_readable(msginfo->size);
1545         text[S_COL_SCORE]  = itos(msginfo->score);
1546
1547         if (msginfo->date_t) {
1548                 procheader_date_get_localtime(date_modified,
1549                                               sizeof(date_modified),
1550                                               msginfo->date_t);
1551                 text[S_COL_DATE] = date_modified;
1552         } else if (msginfo->date)
1553                 text[S_COL_DATE] = msginfo->date;
1554         else
1555                 text[S_COL_DATE] = _("(No Date)");
1556
1557         text[S_COL_FROM] = msginfo->fromname ? msginfo->fromname :
1558                 _("(No From)");
1559         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
1560             cur_account && cur_account->address) {
1561                 gchar *from;
1562
1563                 Xalloca(from, strlen(msginfo->from) + 1, return);
1564                 strcpy(from, msginfo->from);
1565                 extract_address(from);
1566                 if (!strcmp(from, cur_account->address)) {
1567                         g_free(to);
1568                         to = g_strconcat("-->", msginfo->to, NULL);
1569                         text[S_COL_FROM] = to;
1570                 }
1571         }
1572
1573         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
1574                 _("(No Subject)");
1575 }
1576
1577 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
1578                                 gboolean new_window)
1579 {
1580         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1581         MsgInfo *msginfo;
1582         gchar *filename;
1583         static gboolean lock = FALSE;
1584
1585         if (!new_window && summaryview->displayed == row) return;
1586         g_return_if_fail(row != NULL);
1587
1588         if (lock) return;
1589         lock = TRUE;
1590
1591         STATUSBAR_POP(summaryview->mainwin);
1592
1593         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1594
1595         filename = procmsg_get_message_file(msginfo);
1596         if (!filename) {
1597                 lock = FALSE;
1598                 return;
1599         }
1600         g_free(filename);
1601
1602         if (new_window) {
1603                 MessageView *msgview;
1604
1605                 msgview = messageview_create_with_new_window();
1606                 messageview_show(msgview, msginfo);
1607         } else {
1608                 MessageView *msgview;
1609
1610                 msgview = summaryview->messageview;
1611
1612                 summaryview->displayed = row;
1613                 if (!summaryview->msg_is_toggled_on)
1614                         summary_toggle_view(summaryview);
1615                 messageview_show(msgview, msginfo);
1616                 if (msgview->type == MVIEW_TEXT ||
1617                     (msgview->type == MVIEW_MIME &&
1618                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
1619                         gtk_widget_grab_focus(summaryview->ctree);
1620                 GTK_EVENTS_FLUSH();
1621                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
1622         }
1623
1624         if (MSG_IS_NEW(msginfo->flags))
1625                 summaryview->newmsgs--;
1626         if (MSG_IS_UNREAD(msginfo->flags))
1627                 summaryview->unread--;
1628         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
1629                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1630                 summary_set_row_marks(summaryview, row);
1631                 gtk_clist_thaw(GTK_CLIST(ctree));
1632                 summary_status_show(summaryview);
1633         }
1634
1635         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
1636                 header_window_show(summaryview->headerwin, msginfo);
1637
1638         lock = FALSE;
1639 }
1640
1641 void summary_redisplay_msg(SummaryView *summaryview)
1642 {
1643         GtkCTreeNode *node;
1644
1645         if (summaryview->displayed) {
1646                 node = summaryview->displayed;
1647                 summaryview->displayed = NULL;
1648                 summary_display_msg(summaryview, node, FALSE);
1649         }
1650 }
1651
1652 void summary_open_msg(SummaryView *summaryview)
1653 {
1654         if (!summaryview->selected) return;
1655
1656         summary_display_msg(summaryview, summaryview->selected, TRUE);
1657 }
1658
1659 void summary_view_source(SummaryView * summaryview)
1660 {
1661         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1662         MsgInfo *msginfo;
1663         SourceWindow *srcwin;
1664
1665         if (!summaryview->selected) return;
1666
1667         srcwin = source_window_create();
1668         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1669         source_window_show_msg(srcwin, msginfo);
1670         source_window_show(srcwin);
1671 }
1672
1673 void summary_reedit(SummaryView *summaryview)
1674 {
1675         MsgInfo *msginfo;
1676
1677         if (!summaryview->selected) return;
1678         if (!summaryview->folder_item ||
1679             summaryview->folder_item->stype != F_DRAFT) return;
1680
1681         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
1682                                               summaryview->selected);
1683         if (!msginfo) return;
1684
1685         compose_reedit(msginfo);
1686 }
1687
1688 void summary_step(SummaryView *summaryview, GtkScrollType type)
1689 {
1690         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1691
1692         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
1693                                 type, 0.0);
1694
1695         if (summaryview->msg_is_toggled_on)
1696                 summary_display_msg(summaryview, summaryview->selected, FALSE);
1697 }
1698
1699 static void summary_toggle_view(SummaryView *summaryview)
1700 {
1701         MainWindow *mainwin = summaryview->mainwin;
1702         union CompositeWin *cwin = &mainwin->win;
1703         GtkWidget *vpaned = NULL;
1704         GtkWidget *container = NULL;
1705
1706         switch (mainwin->type) {
1707         case SEPARATE_NONE:
1708                 vpaned = cwin->sep_none.vpaned;
1709                 container = cwin->sep_none.hpaned;
1710                 break;
1711         case SEPARATE_FOLDER:
1712                 vpaned = cwin->sep_folder.vpaned;
1713                 container = mainwin->vbox_body;
1714                 break;
1715         case SEPARATE_MESSAGE:
1716         case SEPARATE_BOTH:
1717                 return;
1718         }
1719
1720         if (vpaned->parent != NULL) {
1721                 summaryview->msg_is_toggled_on = FALSE;
1722                 summaryview->displayed = NULL;
1723                 gtk_widget_ref(vpaned);
1724                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
1725                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1726                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1727                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1728         } else {
1729                 summaryview->msg_is_toggled_on = TRUE;
1730                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1731                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1732                 gtk_widget_unref(vpaned);
1733                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1734                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1735         }
1736
1737         gtk_widget_grab_focus(summaryview->ctree);
1738 }
1739
1740 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
1741 {
1742         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1743         MsgInfo *msginfo;
1744         MsgFlags flags;
1745
1746         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1747         if (!msginfo) return;
1748
1749         flags = msginfo->flags;
1750
1751         gtk_ctree_node_set_foreground(ctree, row, &summaryview->color_normal);
1752
1753         /* set new/unread column */
1754         if (MSG_IS_NEW(flags)) {
1755                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1756                                           newxpm, newxpmmask);
1757         } else if (MSG_IS_UNREAD(flags)) {
1758                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1759                                           unreadxpm, unreadxpmmask);
1760         } else if (MSG_IS_REPLIED(flags)) {
1761                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1762                                           repliedxpm, repliedxpmmask);
1763         } else if (MSG_IS_FORWARDED(flags)) {
1764                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1765                                           forwardedxpm, forwardedxpmmask);
1766         } else {
1767                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
1768         }
1769
1770         /* set mark column */
1771         if (MSG_IS_DELETED(flags)) {
1772                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
1773                                           deletedxpm, deletedxpmmask);
1774                 gtk_ctree_node_set_foreground(ctree, row,
1775                                               &summaryview->color_dim);
1776         } else if (MSG_IS_MARKED(flags)) {
1777                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
1778                                           markxpm, markxpmmask);
1779         } else if (MSG_IS_MOVE(flags)) {
1780                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "o");
1781                 gtk_ctree_node_set_foreground(ctree, row,
1782                                               &summaryview->color_marked);
1783         } else if (MSG_IS_COPY(flags)) {
1784                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
1785                 gtk_ctree_node_set_foreground(ctree, row,
1786                                               &summaryview->color_marked);
1787         } else {
1788                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
1789         }
1790
1791         if (MSG_IS_MIME(flags)) {
1792                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
1793                                           clipxpm, clipxpmmask);
1794         } else {
1795                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
1796         }
1797 }
1798
1799 void summary_set_marks_selected(SummaryView *summaryview)
1800 {
1801         summary_set_row_marks(summaryview, summaryview->selected);
1802 }
1803
1804 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
1805 {
1806         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1807         MsgInfo *msginfo;
1808
1809         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1810         msginfo->to_folder = NULL;
1811         if (MSG_IS_DELETED(msginfo->flags))
1812                 summaryview->deleted--;
1813         if (MSG_IS_MOVE(msginfo->flags))
1814                 summaryview->moved--;
1815         if (MSG_IS_COPY(msginfo->flags))
1816                 summaryview->copied--;
1817         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
1818         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
1819         summary_set_row_marks(summaryview, row);
1820         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
1821 }
1822
1823 void summary_mark(SummaryView *summaryview)
1824 {
1825         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1826         GList *cur;
1827
1828         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1829                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
1830
1831         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
1832 }
1833
1834 static void summary_mark_row_as_read(SummaryView *summaryview,
1835                                      GtkCTreeNode *row)
1836 {
1837         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1838         MsgInfo *msginfo;
1839
1840         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1841         if (MSG_IS_NEW(msginfo->flags))
1842                 summaryview->newmsgs--;
1843         if (MSG_IS_UNREAD(msginfo->flags))
1844                 summaryview->unread--;
1845         if (MSG_IS_NEW(msginfo->flags) ||
1846             MSG_IS_UNREAD(msginfo->flags)) {
1847                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1848                 summary_set_row_marks(summaryview, row);
1849                 debug_print(_("Message %d is marked as being read\n"),
1850                             msginfo->msgnum);
1851         }
1852 }
1853
1854 void summary_mark_as_read(SummaryView *summaryview)
1855 {
1856         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1857         GList *cur;
1858
1859         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1860                 summary_mark_row_as_read(summaryview,
1861                                          GTK_CTREE_NODE(cur->data));
1862
1863         summary_status_show(summaryview);
1864 }
1865
1866 static void summary_mark_row_as_unread(SummaryView *summaryview,
1867                                        GtkCTreeNode *row)
1868 {
1869         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1870         MsgInfo *msginfo;
1871
1872         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1873         if (MSG_IS_DELETED(msginfo->flags)) {
1874                 msginfo->to_folder = NULL;
1875                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
1876                 summaryview->deleted--;
1877         }
1878         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
1879         if (!MSG_IS_UNREAD(msginfo->flags)) {
1880                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
1881                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1882                                           unreadxpm, unreadxpmmask);
1883                 summaryview->unread++;
1884                 debug_print(_("Message %d is marked as unread\n"),
1885                             msginfo->msgnum);
1886         }
1887         summary_set_row_marks(summaryview, row);
1888 }
1889
1890 void summary_mark_as_unread(SummaryView *summaryview)
1891 {
1892         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1893         GList *cur;
1894
1895         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1896                 summary_mark_row_as_unread(summaryview,
1897                                            GTK_CTREE_NODE(cur->data));
1898
1899         summary_status_show(summaryview);
1900 }
1901
1902 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
1903 {
1904         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1905         MsgInfo *msginfo;
1906
1907         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1908
1909         if (MSG_IS_DELETED(msginfo->flags)) return;
1910
1911         msginfo->to_folder = NULL;
1912         if (MSG_IS_MOVE(msginfo->flags))
1913                 summaryview->moved--;
1914         if (MSG_IS_COPY(msginfo->flags))
1915                 summaryview->copied--;
1916         MSG_UNSET_FLAGS(msginfo->flags,
1917                         MSG_MARKED |
1918                         MSG_MOVE |
1919                         MSG_COPY);
1920         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
1921         summaryview->deleted++;
1922
1923         if (!prefs_common.immediate_exec)
1924                 summary_set_row_marks(summaryview, row);
1925
1926         debug_print(_("Message %s/%d is set to delete\n"),
1927                     msginfo->folder->path, msginfo->msgnum);
1928 }
1929
1930 void summary_delete(SummaryView *summaryview)
1931 {
1932         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1933         GList *cur;
1934
1935         if (!summaryview->folder_item ||
1936             summaryview->folder_item->folder->type == F_NEWS) return;
1937
1938         /* if current folder is trash, don't delete */
1939         if (summaryview->folder_item->stype == F_TRASH) {
1940                 alertpanel_notice(_("Current folder is Trash."));
1941                 return;
1942         }
1943
1944         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1945                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
1946
1947         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
1948
1949         if (prefs_common.immediate_exec)
1950                 summary_execute(summaryview);
1951         else
1952                 summary_status_show(summaryview);
1953 }
1954
1955 void summary_delete_duplicated(SummaryView *summaryview)
1956 {
1957         if (!summaryview->folder_item ||
1958             summaryview->folder_item->folder->type == F_NEWS) return;
1959         if (summaryview->folder_item->stype == F_TRASH) return;
1960
1961         main_window_cursor_wait(summaryview->mainwin);
1962         debug_print(_("Deleting duplicated messages..."));
1963         STATUSBAR_PUSH(summaryview->mainwin,
1964                        _("Deleting duplicated messages..."));
1965
1966         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
1967                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
1968                                 summaryview);
1969
1970         if (prefs_common.immediate_exec)
1971                 summary_execute(summaryview);
1972         else
1973                 summary_status_show(summaryview);
1974
1975         debug_print(_("done.\n"));
1976         STATUSBAR_POP(summaryview->mainwin);
1977         main_window_cursor_normal(summaryview->mainwin);
1978 }
1979
1980 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
1981                                            SummaryView *summaryview)
1982 {
1983         GtkCTreeNode *found;
1984         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
1985
1986         if (!msginfo->msgid || !*msginfo->msgid) return;
1987
1988         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
1989
1990         if (found && found != node)
1991                 summary_delete_row(summaryview, node);
1992 }
1993
1994 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
1995 {
1996         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1997         MsgInfo *msginfo;
1998
1999         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2000         msginfo->to_folder = NULL;
2001         if (MSG_IS_DELETED(msginfo->flags))
2002                 summaryview->deleted--;
2003         if (MSG_IS_MOVE(msginfo->flags))
2004                 summaryview->moved--;
2005         if (MSG_IS_COPY(msginfo->flags))
2006                 summaryview->copied--;
2007         MSG_UNSET_FLAGS(msginfo->flags,
2008                         MSG_MARKED |
2009                         MSG_DELETED |
2010                         MSG_MOVE |
2011                         MSG_COPY);
2012         summary_set_row_marks(summaryview, row);
2013
2014         debug_print(_("Message %s/%d is unmarked\n"),
2015                     msginfo->folder->path, msginfo->msgnum);
2016 }
2017
2018 void summary_unmark(SummaryView *summaryview)
2019 {
2020         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2021         GList *cur;
2022
2023         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2024              cur = cur->next)
2025                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2026
2027         summary_status_show(summaryview);
2028 }
2029
2030 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2031                                 FolderItem *to_folder)
2032 {
2033         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2034         MsgInfo *msginfo;
2035
2036         g_return_if_fail(to_folder != NULL);
2037
2038         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2039         msginfo->to_folder = to_folder;
2040         if (MSG_IS_DELETED(msginfo->flags))
2041                 summaryview->deleted--;
2042         MSG_UNSET_FLAGS(msginfo->flags,
2043                         MSG_MARKED | MSG_DELETED | MSG_COPY);
2044         if (!MSG_IS_MOVE(msginfo->flags)) {
2045                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
2046                 summaryview->moved++;
2047         }
2048         if (!prefs_common.immediate_exec)
2049                 summary_set_row_marks(summaryview, row);
2050
2051         debug_print(_("Message %d is set to move to %s\n"),
2052                     msginfo->msgnum, to_folder->path);
2053 }
2054
2055 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2056 {
2057         GList *cur;
2058
2059         if (!to_folder) return;
2060         if (!summaryview->folder_item ||
2061             summaryview->folder_item->folder->type == F_NEWS) return;
2062         if (summaryview->folder_item == to_folder) {
2063                 alertpanel_notice(_("Destination is same as current folder."));
2064                 return;
2065         }
2066
2067         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2068              cur != NULL; cur = cur->next)
2069                 summary_move_row_to
2070                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2071
2072         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2073
2074         if (prefs_common.immediate_exec)
2075                 summary_execute(summaryview);
2076         else
2077                 summary_status_show(summaryview);
2078 }
2079
2080 void summary_move_to(SummaryView *summaryview)
2081 {
2082         FolderItem *to_folder;
2083
2084         if (!summaryview->folder_item ||
2085             summaryview->folder_item->folder->type == F_NEWS) return;
2086
2087         to_folder = foldersel_folder_sel(NULL);
2088         summary_move_selected_to(summaryview, to_folder);
2089 }
2090
2091 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2092                                 FolderItem *to_folder)
2093 {
2094         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2095         MsgInfo *msginfo;
2096
2097         g_return_if_fail(to_folder != NULL);
2098
2099         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2100         msginfo->to_folder = to_folder;
2101         if (MSG_IS_DELETED(msginfo->flags))
2102                 summaryview->deleted--;
2103         MSG_UNSET_FLAGS(msginfo->flags,
2104                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
2105         if (!MSG_IS_COPY(msginfo->flags)) {
2106                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
2107                 summaryview->copied++;
2108         }
2109         if (!prefs_common.immediate_exec)
2110                 summary_set_row_marks(summaryview, row);
2111
2112         debug_print(_("Message %d is set to copy to %s\n"),
2113                     msginfo->msgnum, to_folder->path);
2114 }
2115
2116 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2117 {
2118         GList *cur;
2119
2120         if (!to_folder) return;
2121         if (!summaryview->folder_item ||
2122             summaryview->folder_item->folder->type == F_NEWS) return;
2123         if (summaryview->folder_item == to_folder) {
2124                 alertpanel_notice
2125                         (_("Destination to copy is same as current folder."));
2126                 return;
2127         }
2128
2129         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2130              cur != NULL; cur = cur->next)
2131                 summary_copy_row_to
2132                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2133
2134         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2135
2136         if (prefs_common.immediate_exec)
2137                 summary_execute(summaryview);
2138         else
2139                 summary_status_show(summaryview);
2140 }
2141
2142 void summary_copy_to(SummaryView *summaryview)
2143 {
2144         FolderItem *to_folder;
2145
2146         if (!summaryview->folder_item ||
2147             summaryview->folder_item->folder->type == F_NEWS) return;
2148
2149         to_folder = foldersel_folder_sel(NULL);
2150         summary_copy_selected_to(summaryview, to_folder);
2151 }
2152
2153 void summary_select_all(SummaryView *summaryview)
2154 {
2155         if (summaryview->messages >= 500) {
2156                 STATUSBAR_PUSH(summaryview->mainwin,
2157                                _("Selecting all messages..."));
2158                 main_window_cursor_wait(summaryview->mainwin);
2159         }
2160
2161         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2162
2163         if (summaryview->messages >= 500) {
2164                 STATUSBAR_POP(summaryview->mainwin);
2165                 main_window_cursor_normal(summaryview->mainwin);
2166         }
2167 }
2168
2169 void summary_unselect_all(SummaryView *summaryview)
2170 {
2171         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2172 }
2173
2174 void summary_save_as(SummaryView *summaryview)
2175 {
2176         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2177         MsgInfo *msginfo;
2178         gchar *src, *dest;
2179
2180         if (!summaryview->selected) return;
2181         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2182         if (!msginfo) return;
2183
2184         dest = filesel_select_file(_("Save as"), NULL);
2185         if (!dest) return;
2186         if (is_file_exist(dest)) {
2187                 AlertValue aval;
2188
2189                 aval = alertpanel(_("Overwrite"),
2190                                   _("Overwrite existing file?"),
2191                                   _("OK"), _("Cancel"), NULL);
2192                 if (G_ALERTDEFAULT != aval) return;
2193         }
2194
2195         src = procmsg_get_message_file(msginfo);
2196         copy_file(src, dest);
2197         g_free(src);
2198 }
2199
2200 void summary_print(SummaryView *summaryview)
2201 {
2202         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2203         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2204         MsgInfo *msginfo;
2205         GList *cur;
2206         gchar *cmdline;
2207         gchar *p;
2208
2209         if (clist->selection == NULL) return;
2210
2211         cmdline = input_dialog(_("Print"),
2212                                _("Enter the print command line:\n"
2213                                  "(`%s' will be replaced with file name)"),
2214                                prefs_common.print_cmd);
2215         if (!cmdline) return;
2216         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2217             strchr(p + 2, '%')) {
2218                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2219                                  cmdline);
2220                 return;
2221         }
2222
2223         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2224                 msginfo = gtk_ctree_node_get_row_data
2225                         (ctree, GTK_CTREE_NODE(cur->data));
2226                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2227         }
2228 }
2229
2230 void summary_execute(SummaryView *summaryview)
2231 {
2232         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2233         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2234         GtkCTreeNode *node, *next;
2235
2236         if (!summaryview->folder_item ||
2237             summaryview->folder_item->folder->type == F_NEWS) return;
2238
2239         gtk_clist_freeze(clist);
2240
2241         if (prefs_common.enable_thread)
2242                 summary_unthread_for_exec(summaryview);
2243
2244         summary_execute_move(summaryview);
2245         summary_execute_copy(summaryview);
2246         summary_execute_delete(summaryview);
2247
2248         node = GTK_CTREE_NODE(clist->row_list);
2249         while (node != NULL) {
2250                 next = GTK_CTREE_NODE_NEXT(node);
2251                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2252                         if (node == summaryview->displayed) {
2253                                 messageview_clear(summaryview->messageview);
2254                                 summaryview->displayed = NULL;
2255                         }
2256                         if (GTK_CTREE_ROW(node)->children != NULL)
2257                                 g_warning("summary_execute(): children != NULL\n");
2258                         else
2259                                 gtk_ctree_remove_node(ctree, node);
2260                 }
2261                 node = next;
2262         }
2263
2264         if (prefs_common.enable_thread)
2265                 summary_thread_build(summaryview);
2266
2267         summaryview->selected = clist->selection ?
2268                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2269
2270         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2271                 menu_set_insensitive_all
2272                         (GTK_MENU_SHELL(summaryview->popupmenu));
2273                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2274         } else
2275                 gtk_widget_grab_focus(summaryview->ctree);
2276
2277         summary_update_status(summaryview);
2278         summary_status_show(summaryview);
2279
2280         summary_write_cache(summaryview);
2281
2282         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2283
2284         gtk_clist_thaw(clist);
2285 }
2286
2287 static void summary_execute_move(SummaryView *summaryview)
2288 {
2289         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2290         GSList *cur;
2291
2292         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2293
2294         /* search moving messages and execute */
2295         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2296                                 summaryview);
2297
2298         if (summaryview->mlist) {
2299                 procmsg_move_messages(summaryview->mlist);
2300
2301                 folder_item_scan_foreach(summaryview->folder_table);
2302                 folderview_update_item_foreach(summaryview->folder_table);
2303
2304                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2305                         procmsg_msginfo_free((MsgInfo *)cur->data);
2306                 g_slist_free(summaryview->mlist);
2307                 summaryview->mlist = NULL;
2308         }
2309
2310         g_hash_table_destroy(summaryview->folder_table);
2311         summaryview->folder_table = NULL;
2312 }
2313
2314 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2315                                       gpointer data)
2316 {
2317         SummaryView *summaryview = data;
2318         MsgInfo *msginfo;
2319
2320         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2321
2322         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2323                 g_hash_table_insert(summaryview->folder_table,
2324                                     msginfo->to_folder, GINT_TO_POINTER(1));
2325
2326                 summaryview->mlist =
2327                         g_slist_append(summaryview->mlist, msginfo);
2328                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2329
2330                 if (msginfo->msgid && *msginfo->msgid &&
2331                     node == g_hash_table_lookup(summaryview->msgid_table,
2332                                                 msginfo->msgid))
2333                         g_hash_table_remove(summaryview->msgid_table,
2334                                             msginfo->msgid);
2335         }
2336 }
2337
2338 static void summary_execute_copy(SummaryView *summaryview)
2339 {
2340         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2341
2342         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2343
2344         /* search copying messages and execute */
2345         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2346                                 summaryview);
2347
2348         if (summaryview->mlist) {
2349                 procmsg_copy_messages(summaryview->mlist);
2350
2351                 folder_item_scan_foreach(summaryview->folder_table);
2352                 folderview_update_item_foreach(summaryview->folder_table);
2353
2354                 g_slist_free(summaryview->mlist);
2355                 summaryview->mlist = NULL;
2356         }
2357
2358         g_hash_table_destroy(summaryview->folder_table);
2359         summaryview->folder_table = NULL;
2360 }
2361
2362 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
2363                                       gpointer data)
2364 {
2365         SummaryView *summaryview = data;
2366         MsgInfo *msginfo;
2367
2368         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2369
2370         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
2371                 g_hash_table_insert(summaryview->folder_table,
2372                                     msginfo->to_folder, GINT_TO_POINTER(1));
2373
2374                 summaryview->mlist =
2375                         g_slist_append(summaryview->mlist, msginfo);
2376
2377                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
2378                 summary_set_row_marks(summaryview, node);
2379         }
2380 }
2381
2382 static void summary_execute_delete(SummaryView *summaryview)
2383 {
2384         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2385         FolderItem *trash;
2386         GSList *cur;
2387
2388         trash = summaryview->folder_item->folder->trash;
2389         if (summaryview->folder_item->folder->type == F_MH) {
2390                 g_return_if_fail(trash != NULL);
2391         }
2392         if (summaryview->folder_item == trash) return;
2393
2394         /* search deleting messages and execute */
2395         gtk_ctree_pre_recursive
2396                 (ctree, NULL, summary_execute_delete_func, summaryview);
2397
2398         if (!summaryview->mlist) return;
2399
2400         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
2401
2402         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2403                 procmsg_msginfo_free((MsgInfo *)cur->data);
2404
2405         g_slist_free(summaryview->mlist);
2406         summaryview->mlist = NULL;
2407
2408         folder_item_scan(trash);
2409         folderview_update_item(trash, FALSE);
2410 }
2411
2412 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
2413                                         gpointer data)
2414 {
2415         SummaryView *summaryview = data;
2416         MsgInfo *msginfo;
2417
2418         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2419
2420         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
2421                 summaryview->mlist =
2422                         g_slist_append(summaryview->mlist, msginfo);
2423                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2424
2425                 if (msginfo->msgid && *msginfo->msgid &&
2426                     node == g_hash_table_lookup(summaryview->msgid_table,
2427                                                 msginfo->msgid))
2428                         g_hash_table_remove(summaryview->msgid_table,
2429                                             msginfo->msgid);
2430         }
2431 }
2432
2433 /* thread functions */
2434
2435 void summary_thread_build(SummaryView *summaryview)
2436 {
2437         debug_print(_("Building threads..."));
2438         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
2439         main_window_cursor_wait(summaryview->mainwin);
2440
2441         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2442
2443         gtk_ctree_pre_recursive_to_depth
2444                 (GTK_CTREE(summaryview->ctree), NULL, 1,
2445                  GTK_CTREE_FUNC(summary_thread_func),
2446                  summaryview->msgid_table);
2447
2448         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2449
2450         debug_print(_("done.\n"));
2451         STATUSBAR_POP(summaryview->mainwin);
2452         main_window_cursor_normal(summaryview->mainwin);
2453 }
2454
2455 void summary_unthread(SummaryView *summaryview)
2456 {
2457         GtkCTreeNode *node;
2458         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2459
2460         debug_print(_("Unthreading..."));
2461         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
2462         main_window_cursor_wait(summaryview->mainwin);
2463
2464         gtk_clist_freeze(GTK_CLIST(ctree));
2465
2466         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2467              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2468                 summary_unthread_func(ctree, node, NULL);
2469         }
2470
2471         gtk_clist_thaw(GTK_CLIST(ctree));
2472
2473         debug_print(_("done.\n"));
2474         STATUSBAR_POP(summaryview->mainwin);
2475         main_window_cursor_normal(summaryview->mainwin);
2476 }
2477
2478 static void summary_unthread_for_exec(SummaryView *summaryview)
2479 {
2480         GtkCTreeNode *node;
2481         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2482
2483         debug_print(_("Unthreading for execution..."));
2484
2485         gtk_clist_freeze(GTK_CLIST(ctree));
2486
2487         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2488              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2489                 summary_unthread_for_exec_func(ctree, node, NULL);
2490         }
2491
2492         gtk_clist_thaw(GTK_CLIST(ctree));
2493
2494         debug_print(_("done.\n"));
2495 }
2496
2497 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
2498                                 gpointer data)
2499 {
2500         MsgInfo *msginfo;
2501         GtkCTreeNode *parent;
2502         GHashTable *msgid_table = (GHashTable *)data;
2503
2504         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2505
2506         if (!msginfo || !msginfo->inreplyto) return;
2507
2508         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
2509
2510         if (parent && parent != node) {
2511                 gtk_ctree_move(ctree, node, parent, NULL);
2512                 gtk_ctree_expand(ctree, parent);
2513         }
2514 }
2515
2516 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
2517                                   gpointer data)
2518 {
2519         GtkCTreeNode *child;
2520         GtkCTreeNode *sibling;
2521
2522         child = GTK_CTREE_ROW(node)->children;
2523         sibling = GTK_CTREE_ROW(node)->sibling;
2524
2525         while (child != NULL) {
2526                 GtkCTreeNode *next_child;
2527
2528                 next_child = GTK_CTREE_ROW(child)->sibling;
2529                 gtk_ctree_move(ctree, child, NULL, sibling);
2530                 child = next_child;
2531         }
2532 }
2533
2534 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
2535                                            gpointer data)
2536 {
2537         MsgInfo *msginfo;
2538         GtkCTreeNode *top_parent;
2539         GtkCTreeNode *child;
2540         GtkCTreeNode *sibling;
2541
2542         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2543
2544         if (!msginfo ||
2545             (!MSG_IS_MOVE(msginfo->flags) &&
2546              !MSG_IS_DELETED(msginfo->flags)))
2547                 return;
2548         child = GTK_CTREE_ROW(node)->children;
2549         if (!child) return;
2550
2551         for (top_parent = node;
2552              GTK_CTREE_ROW(top_parent)->parent != NULL;
2553              top_parent = GTK_CTREE_ROW(top_parent)->parent)
2554                 ;
2555         sibling = GTK_CTREE_ROW(top_parent)->sibling;
2556
2557         while (child != NULL) {
2558                 GtkCTreeNode *next_child;
2559
2560                 next_child = GTK_CTREE_ROW(child)->sibling;
2561                 gtk_ctree_move(ctree, child, NULL, sibling);
2562                 child = next_child;
2563         }
2564 }
2565
2566 void summary_filter(SummaryView *summaryview)
2567 {
2568         if (!prefs_common.fltlist) return;
2569
2570         debug_print(_("filtering..."));
2571         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
2572         main_window_cursor_wait(summaryview->mainwin);
2573
2574         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2575
2576         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2577                                 GTK_CTREE_FUNC(summary_filter_func),
2578                                 summaryview);
2579
2580         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2581
2582         if (prefs_common.immediate_exec)
2583                 summary_execute(summaryview);
2584         else
2585                 summary_status_show(summaryview);
2586
2587         debug_print(_("done.\n"));
2588         STATUSBAR_POP(summaryview->mainwin);
2589         main_window_cursor_normal(summaryview->mainwin);
2590 }
2591
2592 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
2593                                 gpointer data)
2594 {
2595         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2596         SummaryView *summaryview = data;
2597         gchar *file;
2598         FolderItem *dest;
2599
2600         file = procmsg_get_message_file_path(msginfo);
2601         dest = filter_get_dest_folder(prefs_common.fltlist, file);
2602         g_free(file);
2603
2604         if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
2605             summaryview->folder_item != dest)
2606                 summary_move_row_to(summaryview, node, dest);
2607 }
2608
2609 /* callback functions */
2610
2611 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
2612                                    SummaryView *summaryview)
2613 {
2614         if (!event)
2615                 return;
2616
2617         if (!summaryview->msg_is_toggled_on && summaryview->selected)
2618                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2619         else
2620                 summary_toggle_view(summaryview);
2621 }
2622
2623 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2624                                    SummaryView *summaryview)
2625 {
2626         if (!event) return;
2627
2628         if (event->button == 3) {
2629                 /* right clicked */
2630                 summary_add_sender_to_cb(summaryview, 0, 0);    
2631                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
2632                                NULL, NULL, event->button, event->time);
2633         } else if (event->button == 2) {
2634                 summaryview->display_msg = TRUE;
2635         } else if (event->button == 1) {
2636                 if (!prefs_common.emulate_emacs &&
2637                     summaryview->msg_is_toggled_on)
2638                         summaryview->display_msg = TRUE;
2639         }
2640 }
2641
2642 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
2643                                     SummaryView *summaryview)
2644 {
2645 }
2646
2647 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
2648 {
2649         summary_key_pressed(summaryview->ctree, event, summaryview);
2650 }
2651
2652 #define BREAK_ON_MODIFIER_KEY() \
2653         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2654
2655 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
2656                                 SummaryView *summaryview)
2657 {
2658         GtkCTree *ctree = GTK_CTREE(widget);
2659         GtkCTreeNode *node;
2660         FolderItem *to_folder;
2661
2662         if (!event) return;
2663
2664         switch (event->keyval) {
2665         case GDK_g:             /* Go */
2666         case GDK_G:
2667                 BREAK_ON_MODIFIER_KEY();
2668
2669                 if (gtk_signal_n_emissions_by_name
2670                         (GTK_OBJECT(ctree), "key_press_event") > 0)
2671                         gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree),
2672                                                      "key_press_event");
2673
2674                 to_folder = foldersel_folder_sel(NULL);
2675                 if (to_folder) {
2676                         debug_print(_("Go to %s\n"), to_folder->path);
2677                         folderview_select(summaryview->folderview, to_folder);
2678                 }
2679                 return;
2680         case GDK_w:             /* Write new message */
2681                 BREAK_ON_MODIFIER_KEY();
2682                 if (summaryview->folder_item)
2683                         compose_new(summaryview->folder_item->folder->account);
2684                 else
2685                         compose_new(NULL);
2686                 return;
2687         case GDK_D:             /* Empty trash */
2688                 BREAK_ON_MODIFIER_KEY();
2689                 if (gtk_signal_n_emissions_by_name
2690                         (GTK_OBJECT(ctree), "key_press_event") > 0)
2691                         gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree),
2692                                                      "key_press_event");
2693                 main_window_empty_trash(summaryview->mainwin, TRUE);
2694                 return;
2695         case GDK_Q:             /* Quit */
2696                 BREAK_ON_MODIFIER_KEY();
2697
2698                 if (prefs_common.confirm_on_exit) {
2699                         if (alertpanel(_("Exit"), _("Exit this program?"),
2700                                        _("OK"), _("Cancel"), NULL)
2701                                        == G_ALERTDEFAULT) {
2702                                 manage_window_focus_in
2703                                         (summaryview->mainwin->window,
2704                                          NULL, NULL);
2705                                 app_will_exit(NULL, summaryview->mainwin);
2706                         }
2707                 }
2708                 return;
2709         case GDK_Left:          /* Move focus */
2710         case GDK_Escape:
2711                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2712                 return;
2713         default:
2714         }
2715
2716         if (!summaryview->selected) {
2717                 node = gtk_ctree_node_nth(ctree, 0);
2718                 if (node)
2719                         gtk_ctree_select(ctree, node);
2720                 else
2721                         return;
2722         }
2723
2724         switch (event->keyval) {
2725         case GDK_space:         /* Page down or go to the next */
2726                 if (summaryview->displayed != summaryview->selected) {
2727                         summary_display_msg(summaryview,
2728                                             summaryview->selected, FALSE);
2729                         break;
2730                 }
2731                 if (!textview_scroll_page(summaryview->messageview->textview,
2732                                           FALSE))
2733                         summary_select_next_unread(summaryview);
2734                 break;
2735         case GDK_n:             /* Next */
2736         case GDK_N:
2737                 BREAK_ON_MODIFIER_KEY();
2738                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2739                 break;
2740         case GDK_BackSpace:     /* Page up */
2741         case GDK_Delete:
2742                 textview_scroll_page(summaryview->messageview->textview, TRUE);
2743                 break;
2744         case GDK_p:             /* Prev */
2745         case GDK_P:
2746                 BREAK_ON_MODIFIER_KEY();
2747                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2748                 break;
2749         case GDK_v:             /* Toggle summary mode / message mode */
2750         case GDK_V:
2751                 BREAK_ON_MODIFIER_KEY();
2752
2753                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
2754                         summary_display_msg(summaryview,
2755                                             summaryview->selected, FALSE);
2756                 else
2757                         summary_toggle_view(summaryview);
2758                 break;
2759         case GDK_Return:        /* Scroll up/down one line */
2760                 if (summaryview->displayed != summaryview->selected) {
2761                         summary_display_msg(summaryview,
2762                                             summaryview->selected, FALSE);
2763                         break;
2764                 }
2765                 textview_scroll_one_line(summaryview->messageview->textview,
2766                                          (event->state & GDK_MOD1_MASK) != 0);
2767                 break;
2768         case GDK_asterisk:      /* Mark */
2769                 summary_mark(summaryview);
2770                 break;
2771         case GDK_exclam:        /* Mark as unread */
2772                 summary_mark_as_unread(summaryview);
2773                 break;
2774         case GDK_d:             /* Delete */
2775                 BREAK_ON_MODIFIER_KEY();
2776                 summary_delete(summaryview);
2777                 break;
2778         case GDK_u:             /* Unmark */
2779         case GDK_U:
2780                 BREAK_ON_MODIFIER_KEY();
2781                 summary_unmark(summaryview);
2782                 break;
2783         case GDK_o:             /* Move */
2784                 BREAK_ON_MODIFIER_KEY();
2785                 summary_move_to(summaryview);
2786                 break;
2787         case GDK_O:             /* Copy */
2788                 BREAK_ON_MODIFIER_KEY();
2789                 summary_copy_to(summaryview);
2790                 break;
2791         case GDK_x:             /* Execute */
2792         case GDK_X:
2793                 BREAK_ON_MODIFIER_KEY();
2794                 summary_execute(summaryview);
2795                 break;
2796         case GDK_a:             /* Reply to the message */
2797                 BREAK_ON_MODIFIER_KEY();
2798                 summary_reply_cb(summaryview,
2799                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
2800                 break;
2801         case GDK_A:             /* Reply to the message with quotation */
2802                 BREAK_ON_MODIFIER_KEY();
2803                 summary_reply_cb(summaryview,
2804                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
2805                 break;
2806         case GDK_f:             /* Forward the message */
2807                 BREAK_ON_MODIFIER_KEY();
2808                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
2809                 break;
2810         case GDK_F:
2811                 BREAK_ON_MODIFIER_KEY();
2812                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
2813                 break;
2814         case GDK_y:             /* Save the message */
2815                 BREAK_ON_MODIFIER_KEY();
2816                 summary_save_as(summaryview);
2817                 break;
2818         default:
2819         }
2820 }
2821
2822 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
2823 {
2824         if (summaryview->folder_item->stype == F_DRAFT)
2825                 summary_reedit(summaryview);
2826         else
2827                 summary_open_msg(summaryview);
2828
2829         summaryview->display_msg = FALSE;
2830 }
2831
2832 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
2833                              gint column, SummaryView *summaryview)
2834 {
2835         MsgInfo *msginfo;
2836
2837         summary_status_show(summaryview);
2838         summary_set_menu_sensitive(summaryview);
2839
2840         if (GTK_CLIST(ctree)->selection &&
2841              GTK_CLIST(ctree)->selection->next) {
2842                 summaryview->display_msg = FALSE;
2843                 return;
2844         }
2845
2846         summaryview->selected = row;
2847
2848         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2849
2850         switch (column) {
2851         case S_COL_MARK:
2852                 if (MSG_IS_MARKED(msginfo->flags)) {
2853                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
2854                         summary_set_row_marks(summaryview, row);
2855                 } else
2856                         summary_mark_row(summaryview, row);
2857                 break;
2858         case S_COL_UNREAD:
2859                 if (MSG_IS_UNREAD(msginfo->flags)) {
2860                         summary_mark_row_as_read(summaryview, row);
2861                         summary_status_show(summaryview);
2862                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
2863                          !MSG_IS_FORWARDED(msginfo->flags)) {
2864                         summary_mark_row_as_unread(summaryview, row);
2865                         summary_status_show(summaryview);
2866                 }
2867                 break;
2868         default:
2869         }
2870
2871         if (summaryview->display_msg)
2872                 summary_display_msg(summaryview, row, FALSE);
2873
2874         summaryview->display_msg = FALSE;
2875 }
2876
2877 static void summary_col_resized(GtkCList *clist, gint column, gint width,
2878                                 SummaryView *summaryview)
2879 {
2880         switch (column) {
2881         case S_COL_MARK:
2882                 prefs_common.summary_col_mark = width;
2883                 break;
2884         case S_COL_UNREAD:
2885                 prefs_common.summary_col_unread = width;
2886                 break;
2887         case S_COL_MIME:
2888                 prefs_common.summary_col_mime = width;
2889                 break;
2890         case S_COL_NUMBER:
2891                 prefs_common.summary_col_number = width;
2892                 break;
2893         case S_COL_SCORE:
2894                 prefs_common.summary_col_score = width;
2895                 break;
2896         case S_COL_SIZE:
2897                 prefs_common.summary_col_size = width;
2898                 break;
2899         case S_COL_DATE:
2900                 prefs_common.summary_col_date = width;
2901                 break;
2902         case S_COL_FROM:
2903                 prefs_common.summary_col_from = width;
2904                 break;
2905         case S_COL_SUBJECT:
2906                 prefs_common.summary_col_subject = width;
2907                 break;
2908         default:
2909         }
2910 }
2911
2912 static void summary_reply_cb(SummaryView *summaryview, guint action,
2913                              GtkWidget *widget)
2914 {
2915         MsgInfo *msginfo;
2916
2917         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2918                                               summaryview->selected);
2919         if (!msginfo) return;
2920
2921         switch ((ComposeReplyMode)action) {
2922         case COMPOSE_REPLY:
2923                 compose_reply(msginfo, prefs_common.reply_with_quote, FALSE);
2924                 break;
2925         case COMPOSE_REPLY_WITH_QUOTE:
2926                 compose_reply(msginfo, TRUE, FALSE);
2927                 break;
2928         case COMPOSE_REPLY_WITHOUT_QUOTE:
2929                 compose_reply(msginfo, FALSE, FALSE);
2930                 break;
2931         case COMPOSE_REPLY_TO_ALL:
2932                 compose_reply(msginfo, prefs_common.reply_with_quote, TRUE);
2933                 break;
2934         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
2935                 compose_reply(msginfo, TRUE, TRUE);
2936                 break;
2937         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
2938                 compose_reply(msginfo, FALSE, TRUE);
2939                 break;
2940         case COMPOSE_FORWARD:
2941                 compose_forward(msginfo, FALSE);
2942                 break;
2943         case COMPOSE_FORWARD_AS_ATTACH:
2944                 compose_forward(msginfo, TRUE);
2945                 break;
2946         default:
2947                 compose_reply(msginfo, prefs_common.reply_with_quote, FALSE);
2948         }
2949
2950         summary_set_marks_selected(summaryview);
2951 }
2952
2953 static void summary_show_all_header_cb(SummaryView *summaryview,
2954                                        guint action, GtkWidget *widget)
2955 {
2956         header_window_show_cb(summaryview->mainwin, action, widget);
2957 }
2958
2959 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
2960                                          guint                   action,
2961                                          GtkWidget              *widget_)
2962 {
2963         GtkWidget               *submenu;                                               
2964         GList                   *groups, *tmp;
2965         GtkMenuShell    *menushell;
2966         GtkWidget               *menu;
2967         GtkWidget               *menuitem;
2968         GList                   *child = menushell->children;
2969         gboolean                found = FALSE;
2970         MsgInfo                 *msginfo;
2971         gchar                   *from_address;
2972
2973         menushell = GTK_MENU_SHELL(summaryview->popupmenu);
2974         g_return_if_fail(GTK_MENU_SHELL(summaryview->popupmenu));
2975
2976         /* we're iterating each menu item searching for the one with 
2977          * a "contacts" object data. if not found add the menu,
2978          * else update it */
2979         for (child = g_list_first(menushell->children); child; child = g_list_next(child)) {
2980                 if (gtk_object_get_data(GTK_OBJECT(child->data), "contacts")) {
2981                         found = TRUE;
2982                         break;
2983                 }
2984         }
2985
2986         /* add item to default context menu if not present */
2987         if (!found) {
2988                 submenu = gtk_menu_item_new_with_label(_("Add sender to address book"));
2989                 gtk_object_set_data(GTK_OBJECT(submenu), "contacts", (gpointer)1);
2990                 gtk_menu_insert(GTK_MENU(summaryview->popupmenu), submenu, 12);
2991                 gtk_widget_show(submenu);
2992         }
2993         else {
2994                 submenu = (GtkWidget *) child->data;
2995         }
2996
2997         /* get the address info from the summary view */
2998         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2999                                                                                                    summaryview->selected);
3000         g_return_if_fail(msginfo != NULL);
3001
3002         from_address = g_strdup(msginfo->from);
3003         eliminate_address_comment(from_address);
3004         extract_address(from_address);
3005         log_message("adding %s %s\n", msginfo->fromname, from_address);
3006         addressbook_add_contact_by_menu(submenu, msginfo->fromname, from_address, NULL);
3007         g_free(from_address);
3008         
3009 }
3010
3011 static void summary_num_clicked(GtkWidget *button, SummaryView *summaryview)
3012 {
3013         summary_sort(summaryview, SORT_BY_NUMBER);
3014 }
3015
3016 static void summary_score_clicked(GtkWidget *button,
3017                                   SummaryView *summaryview)
3018 {
3019         summary_sort(summaryview, SORT_BY_SCORE);
3020 }
3021
3022 static void summary_size_clicked(GtkWidget *button, SummaryView *summaryview)
3023 {
3024         summary_sort(summaryview, SORT_BY_SIZE);
3025 }
3026
3027 static void summary_date_clicked(GtkWidget *button, SummaryView *summaryview)
3028 {
3029         summary_sort(summaryview, SORT_BY_DATE);
3030 }
3031
3032 static void summary_from_clicked(GtkWidget *button, SummaryView *summaryview)
3033 {
3034         summary_sort(summaryview, SORT_BY_FROM);
3035 }
3036
3037 static void summary_subject_clicked(GtkWidget *button,
3038                                     SummaryView *summaryview)
3039 {
3040         summary_sort(summaryview, SORT_BY_SUBJECT);
3041 }
3042
3043 void summary_change_display_item(SummaryView *summaryview)
3044 {
3045         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3046
3047         gtk_clist_set_column_visibility(clist, S_COL_MARK, prefs_common.show_mark);
3048         gtk_clist_set_column_visibility(clist, S_COL_UNREAD, prefs_common.show_unread);
3049         gtk_clist_set_column_visibility(clist, S_COL_MIME, prefs_common.show_mime);
3050         gtk_clist_set_column_visibility(clist, S_COL_NUMBER, prefs_common.show_number);
3051         gtk_clist_set_column_visibility(clist, S_COL_SCORE, prefs_common.show_score);
3052         gtk_clist_set_column_visibility(clist, S_COL_SIZE, prefs_common.show_size);
3053         gtk_clist_set_column_visibility(clist, S_COL_DATE, prefs_common.show_date);
3054         gtk_clist_set_column_visibility(clist, S_COL_FROM, prefs_common.show_from);
3055         gtk_clist_set_column_visibility(clist, S_COL_SUBJECT, prefs_common.show_subject);
3056 }
3057
3058 static void summary_start_drag (GtkWidget   *widget,
3059                                 gint         button,
3060                                 GdkEvent    *event,
3061                                 SummaryView *summaryview)
3062 {
3063         GtkTargetList *list;
3064         GdkDragContext *context;
3065
3066         g_return_if_fail(summaryview != NULL);
3067         g_return_if_fail(summaryview->folder_item != NULL);
3068         g_return_if_fail(summaryview->folder_item->folder != NULL);
3069         if (summaryview->folder_item->folder->type == F_NEWS ||
3070             summaryview->selected == NULL)
3071                 return;
3072
3073         list = gtk_target_list_new(summary_drag_types, 1);
3074
3075         context = gtk_drag_begin(widget, list,
3076                                  GDK_ACTION_MOVE, button, event);
3077         gtk_drag_set_icon_default(context);
3078 }
3079
3080 static void summary_drag_data_get(GtkWidget        *widget,
3081                                   GdkDragContext   *drag_context,
3082                                   GtkSelectionData *selection_data,
3083                                   guint             info,
3084                                   guint             time,
3085                                   SummaryView      *summaryview)
3086 {
3087         if (info == TARGET_MAIL_URI_LIST) {
3088                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3089                 GList *cur;
3090                 MsgInfo *msginfo;
3091                 gchar *mail_list = NULL, *tmp1, *tmp2;
3092
3093                 for (cur = GTK_CLIST(ctree)->selection;
3094                      cur != NULL; cur = cur->next) {
3095                         msginfo = gtk_ctree_node_get_row_data
3096                                 (ctree, GTK_CTREE_NODE(cur->data));
3097                         tmp2 = procmsg_get_message_file_path(msginfo);
3098                         if (!tmp2) continue;
3099                         tmp1 = g_strconcat("file:/", tmp2, NULL);
3100                         g_free(tmp2);
3101
3102                         if (!mail_list) {
3103                                 mail_list = tmp1;
3104                         } else {
3105                                 tmp2 = g_strconcat(mail_list, tmp1, NULL);
3106                                 g_free(mail_list);
3107                                 g_free(tmp1);
3108                                 mail_list = tmp2;
3109                         }
3110                 }
3111
3112                 if (mail_list != NULL) {
3113                         gtk_selection_data_set(selection_data,
3114                                                selection_data->target, 8,
3115                                                mail_list, strlen(mail_list));
3116                         g_free(mail_list);
3117                 } 
3118         } else if (info == TARGET_DUMMY) {
3119                 if (GTK_CLIST(summaryview->ctree)->selection)
3120                         gtk_selection_data_set(selection_data,
3121                                                selection_data->target, 8,
3122                                                "Dummy", 6);
3123         }
3124 }
3125
3126
3127 /* custom compare functions for sorting */
3128
3129 static gint summary_cmp_by_num(GtkCList *clist,
3130                                gconstpointer ptr1, gconstpointer ptr2)
3131 {
3132         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3133         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3134
3135         return msginfo1->msgnum - msginfo2->msgnum;
3136 }
3137
3138 static gint summary_cmp_by_size(GtkCList *clist,
3139                                 gconstpointer ptr1, gconstpointer ptr2)
3140 {
3141         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3142         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3143
3144         return msginfo1->size - msginfo2->size;
3145 }
3146
3147 static gint summary_cmp_by_date(GtkCList *clist,
3148                                gconstpointer ptr1, gconstpointer ptr2)
3149 {
3150         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3151         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3152
3153         return msginfo1->date_t - msginfo2->date_t;
3154 }
3155
3156 static gint summary_cmp_by_from(GtkCList *clist,
3157                                gconstpointer ptr1, gconstpointer ptr2)
3158 {
3159         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3160         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3161
3162         if (!msginfo1->fromname)
3163                 return (msginfo2->fromname != NULL);
3164         if (!msginfo2->fromname)
3165                 return -1;
3166
3167         return strcasecmp(msginfo1->fromname, msginfo2->fromname);
3168 }
3169
3170 static gint summary_cmp_by_subject(GtkCList *clist,
3171                                gconstpointer ptr1, gconstpointer ptr2)
3172 {
3173         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3174         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3175
3176         if (!msginfo1->subject)
3177                 return (msginfo2->subject != NULL);
3178         if (!msginfo2->subject)
3179                 return -1;
3180
3181         return strcasecmp(msginfo1->subject, msginfo2->subject);
3182 }
3183
3184 static gint summary_cmp_by_score(GtkCList *clist,
3185                                  gconstpointer ptr1, gconstpointer ptr2)
3186 {
3187         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3188         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3189         int diff;
3190
3191         /* if score are equal, sort by date */
3192
3193         diff = msginfo1->score - msginfo2->score;
3194         if (diff != 0)
3195                 return diff;
3196         else
3197                 return summary_cmp_by_date(clist, ptr1, ptr2);
3198 }