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