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