scoring dialog box finished
[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                 summary_write_cache(summaryview);
632
633         gtk_clist_freeze(GTK_CLIST(ctree));
634
635         summary_clear_list(summaryview);
636         summary_set_menu_sensitive(summaryview);
637         messageview_clear(summaryview->messageview);
638
639         buf = NULL;
640         if (!item || !item->path || !item->parent ||
641             (item->folder->type == F_MH &&
642              ((buf = folder_item_get_path(item)) == NULL ||
643               change_dir(buf) < 0))) {
644                 g_free(buf);
645                 debug_print(_("empty folder\n\n"));
646                 summary_clear_all(summaryview);
647                 summaryview->folder_item = item;
648                 gtk_clist_thaw(GTK_CLIST(ctree));
649                 return TRUE;
650         }
651         g_free(buf);
652
653         summaryview->folder_item = item;
654
655         gtk_signal_disconnect_by_data(GTK_OBJECT(ctree), summaryview);
656
657         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
658         debug_print("%s\n", buf);
659         STATUSBAR_PUSH(summaryview->mainwin, buf);
660         g_free(buf);
661
662         main_window_cursor_wait(summaryview->mainwin);
663
664         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
665
666         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
667                 MsgInfo * msginfo = (MsgInfo *) cur->data;
668
669                 msginfo->score = score_message(prefs_scoring, msginfo);
670         }
671
672         STATUSBAR_POP(summaryview->mainwin);
673
674         /* set ctree and hash table from the msginfo list
675            creating thread, and count the number of messages */
676         summary_set_ctree_from_list(summaryview, mlist);
677
678         g_slist_free(mlist);
679
680         summary_write_cache(summaryview);
681
682         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
683                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
684         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
685                            GTK_SIGNAL_FUNC(summary_button_pressed),
686                            summaryview);
687         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
688                            GTK_SIGNAL_FUNC(summary_button_released),
689                            summaryview);
690         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
691                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
692         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
693                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
694         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
695                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
696
697         /*connect drag and drop signal*/
698         gtk_signal_connect (GTK_OBJECT (ctree),"start_drag",
699                             GTK_SIGNAL_FUNC (summary_start_drag),
700                             summaryview);
701         gtk_signal_connect (GTK_OBJECT (ctree),"drag_data_get",
702                             GTK_SIGNAL_FUNC (summary_drag_data_get),
703                             summaryview);
704         
705         gtk_clist_thaw(GTK_CLIST(ctree));
706
707         if (is_refresh) {
708                 summary_select_by_msgnum(summaryview, prev_msgnum);
709         } else {
710                 /* select first unread message */
711                 node = summary_find_next_unread_msg(summaryview, NULL);
712                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
713                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
714                 if (node) {
715                         GTK_EVENTS_FLUSH();
716                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
717                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
718                         if (prefs_common.open_unread_on_enter)
719                                 summaryview->display_msg = TRUE;
720                         gtk_sctree_select(GTK_SCTREE(ctree), node);
721                 }
722         }
723
724         summary_status_show(summaryview);
725
726         summary_set_menu_sensitive(summaryview);
727
728         main_window_set_toolbar_sensitive
729                 (summaryview->mainwin, summaryview->selected ? TRUE : FALSE);
730
731         debug_print("\n");
732         STATUSBAR_PUSH(summaryview->mainwin, _("done."));
733
734         main_window_cursor_normal(summaryview->mainwin);
735
736         /* sort before */
737         sort_mode = prefs_folder_item_get_sort_mode(item);
738         sort_type = prefs_folder_item_get_sort_type(item);
739
740         if (sort_mode != SORT_BY_NONE) {
741                 summaryview->sort_mode = sort_mode;
742                 if (sort_type == GTK_SORT_DESCENDING)
743                         summaryview->sort_type = GTK_SORT_ASCENDING;
744                 else
745                         summaryview->sort_type = GTK_SORT_DESCENDING;
746
747                 summary_sort(summaryview, sort_mode);
748         }
749
750         return TRUE;
751 }
752
753 void summary_clear_list(SummaryView *summaryview)
754 {
755         GtkCList *clist = GTK_CLIST(summaryview->ctree);
756         gint optimal_width;
757
758         gtk_clist_freeze(clist);
759
760         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
761                                 NULL, summary_free_msginfo_func, NULL);
762
763         summaryview->folder_item = NULL;
764
765         summaryview->display_msg = FALSE;
766
767         summaryview->selected  = NULL;
768         summaryview->displayed = NULL;
769         summaryview->newmsgs   = summaryview->unread     = 0;
770         summaryview->messages  = summaryview->total_size = 0;
771         summaryview->deleted   = summaryview->moved      = 0;
772         summaryview->copied    = 0;
773         if (summaryview->msgid_table) {
774                 g_hash_table_destroy(summaryview->msgid_table);
775                 summaryview->msgid_table = NULL;
776         }
777         summaryview->mlist = NULL;
778         if (summaryview->folder_table) {
779                 g_hash_table_destroy(summaryview->folder_table);
780                 summaryview->folder_table = NULL;
781         }
782         summaryview->sort_mode = SORT_BY_NONE;
783         summaryview->sort_type = GTK_SORT_ASCENDING;
784
785         gtk_clist_clear(clist);
786         optimal_width = gtk_clist_optimal_column_width(clist, S_COL_SUBJECT);
787         gtk_clist_set_column_width(clist, S_COL_SUBJECT, optimal_width);
788
789         gtk_clist_thaw(clist);
790 }
791
792 void summary_clear_all(SummaryView *summaryview)
793 {
794         summary_clear_list(summaryview);
795         summary_set_menu_sensitive(summaryview);
796         main_window_set_toolbar_sensitive(summaryview->mainwin, FALSE);
797         summary_status_show(summaryview);
798 }
799
800 static void summary_set_menu_sensitive(SummaryView *summaryview)
801 {
802         GtkItemFactory *ifactory = summaryview->popupfactory;
803         GtkCList *clist = GTK_CLIST(summaryview->ctree);
804         gboolean sens;
805         SummarySelection selection;
806
807         if (!clist->row_list)
808                 selection = SUMMARY_NONE;
809         else if (!clist->selection)
810                 selection = SUMMARY_SELECTED_NONE;
811         else if (!clist->selection->next)
812                 selection = SUMMARY_SELECTED_SINGLE;
813         else
814                 selection = SUMMARY_SELECTED_MULTIPLE;
815
816         main_window_set_menu_sensitive(summaryview->mainwin, selection);
817
818         if (selection == SUMMARY_NONE) {
819                 GtkWidget *submenu;
820
821                 submenu = gtk_item_factory_get_widget
822                         (summaryview->popupfactory, "/Mark");
823                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
824                 menu_set_insensitive_all
825                         (GTK_MENU_SHELL(summaryview->popupmenu));
826                 return;
827         }
828
829         if (summaryview->folder_item->folder->type != F_NEWS) {
830                 if (summaryview->folder_item->stype != F_TRASH)
831                         menu_set_sensitive(ifactory, "/Delete", TRUE);
832                 menu_set_sensitive(ifactory, "/Move...", TRUE);
833                 menu_set_sensitive(ifactory, "/Copy...", TRUE);
834         }
835         menu_set_sensitive(ifactory, "/Execute", TRUE);
836
837         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
838         menu_set_sensitive(ifactory, "/Reply",                    sens);
839         menu_set_sensitive(ifactory, "/Reply to all",             sens);
840         menu_set_sensitive(ifactory, "/Forward",                  sens);
841         menu_set_sensitive(ifactory, "/Forward as an attachment", sens);
842
843         menu_set_sensitive(ifactory, "/Open in new window", sens);
844         menu_set_sensitive(ifactory, "/View source", sens);
845         menu_set_sensitive(ifactory, "/Show all header", sens);
846         if (summaryview->folder_item->stype == F_DRAFT)
847                 menu_set_sensitive(ifactory, "/Reedit", sens);
848
849         menu_set_sensitive(ifactory, "/Save as...", sens);
850         menu_set_sensitive(ifactory, "/Print...",   TRUE);
851
852         menu_set_sensitive(ifactory, "/Mark", TRUE);
853
854         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
855         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
856
857         menu_set_sensitive(ifactory, "/Mark/Mark as unread",        TRUE);
858         menu_set_sensitive(ifactory, "/Mark/Make it as being read", TRUE);
859
860         menu_set_sensitive(ifactory, "/Select all", TRUE);
861 }
862
863 void summary_select_next_unread(SummaryView *summaryview)
864 {
865         GtkCTreeNode *node;
866         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
867
868         node = summary_find_next_unread_msg(summaryview,
869                                             summaryview->selected);
870
871         if (node) {
872                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
873                 gtk_sctree_select(GTK_SCTREE(ctree), node);
874                 if (summaryview->displayed == node)
875                         summaryview->displayed = NULL;
876                 summary_display_msg(summaryview, node, FALSE);
877         } else {
878                 AlertValue val;
879
880                 val = alertpanel(_("No unread message"),
881                                  _("No unread message found. Go to next folder?"),
882                                  _("Yes"), _("No"), NULL);
883                 if (val == G_ALERTDEFAULT) {
884                         if (gtk_signal_n_emissions_by_name
885                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
886                                         gtk_signal_emit_stop_by_name
887                                                 (GTK_OBJECT(ctree),
888                                                  "key_press_event");
889                         folderview_select_next_unread(summaryview->folderview);
890                 }
891         }
892 }
893
894 void summary_select_next_marked(SummaryView *summaryview)
895 {
896         GtkCTreeNode *node;
897         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
898
899         node = summary_find_next_marked_msg(summaryview,
900                                             summaryview->selected);
901
902         if (!node) {
903                 AlertValue val;
904
905                 val = alertpanel(_("No more marked messages"),
906                                  _("No marked message found. "
907                                    "Search from the beginning?"),
908                                  _("Yes"), _("No"), NULL);
909                 if (val != G_ALERTDEFAULT) return;
910                 node = summary_find_next_marked_msg(summaryview,
911                                                     NULL);
912         }
913         if (!node) {
914                 alertpanel_notice(_("No marked messages."));
915         } else {
916                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
917                 gtk_sctree_select(GTK_SCTREE(ctree), node);
918                 if (summaryview->displayed == node)
919                         summaryview->displayed = NULL;
920                 summary_display_msg(summaryview, node, FALSE);
921         }
922 }
923
924 void summary_select_prev_marked(SummaryView *summaryview)
925 {
926         GtkCTreeNode *node;
927         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
928
929         node = summary_find_prev_marked_msg(summaryview,
930                                             summaryview->selected);
931
932         if (!node) {
933                 AlertValue val;
934
935                 val = alertpanel(_("No more marked messages"),
936                                  _("No marked message found. "
937                                    "Search from the end?"),
938                                  _("Yes"), _("No"), NULL);
939                 if (val != G_ALERTDEFAULT) return;
940                 node = summary_find_prev_marked_msg(summaryview,
941                                                     NULL);
942         }
943         if (!node) {
944                 alertpanel_notice(_("No marked messages."));
945         } else {
946                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
947                 gtk_sctree_select(GTK_SCTREE(ctree), node);
948                 if (summaryview->displayed == node)
949                         summaryview->displayed = NULL;
950                 summary_display_msg(summaryview, node, FALSE);
951         }
952 }
953
954 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
955 {
956         GtkCTreeNode *node;
957         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
958
959         node = summary_find_msg_by_msgnum(summaryview, msgnum);
960
961         if (node) {
962                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
963                 gtk_sctree_select(GTK_SCTREE(ctree), node);
964                 if (summaryview->displayed == node)
965                         summaryview->displayed = NULL;
966                 summary_display_msg(summaryview, node, FALSE);
967         }
968 }
969
970 guint summary_get_current_msgnum(SummaryView *summaryview)
971 {
972         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
973         MsgInfo *msginfo;
974
975         if (!summaryview->selected)
976                 return 0;
977         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
978         return msginfo->msgnum;
979 }
980
981 static GtkCTreeNode *summary_find_next_unread_msg(SummaryView *summaryview,
982                                                   GtkCTreeNode *current_node)
983 {
984         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
985         GtkCTreeNode *node;
986         MsgInfo *msginfo;
987
988         if (current_node)
989                 node = current_node;
990                 //node = GTK_CTREE_NODE_NEXT(current_node);
991         else
992                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
993
994         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
995                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
996                 if (MSG_IS_UNREAD(msginfo->flags)) break;
997         }
998
999         return node;
1000 }
1001
1002 static GtkCTreeNode *summary_find_next_marked_msg(SummaryView *summaryview,
1003                                                   GtkCTreeNode *current_node)
1004 {
1005         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1006         GtkCTreeNode *node;
1007         MsgInfo *msginfo;
1008
1009         if (current_node)
1010                 node = GTK_CTREE_NODE_NEXT(current_node);
1011         else
1012                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1013
1014         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1015                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1016                 if (MSG_IS_MARKED(msginfo->flags)) break;
1017         }
1018
1019         return node;
1020 }
1021
1022 static GtkCTreeNode *summary_find_prev_marked_msg(SummaryView *summaryview,
1023                                                   GtkCTreeNode *current_node)
1024 {
1025         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1026         GtkCTreeNode *node;
1027         MsgInfo *msginfo;
1028
1029         if (current_node)
1030                 node = GTK_CTREE_NODE_PREV(current_node);
1031         else
1032                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1033
1034         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1035                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1036                 if (MSG_IS_MARKED(msginfo->flags)) break;
1037         }
1038
1039         return node;
1040 }
1041
1042 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1043                                                 guint msgnum)
1044 {
1045         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1046         GtkCTreeNode *node;
1047         MsgInfo *msginfo;
1048
1049         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1050
1051         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1052                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1053                 if (msginfo->msgnum == msgnum) break;
1054         }
1055
1056         return node;
1057 }
1058
1059 #if 0
1060 static GtkCTreeNode *summary_find_prev_unread_msg(SummaryView *summaryview,
1061                                                   GtkCTreeNode *current_node)
1062 {
1063         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1064         GtkCTreeNode *node;
1065         MsgInfo *msginfo;
1066
1067         if (current_node)
1068                 node = current_node;
1069                 //node = GTK_CTREE_NODE_PREV(current_node);
1070         else
1071                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1072
1073         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1074                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1075                 if (MSG_IS_UNREAD(msginfo->flags)) break;
1076         }
1077
1078         return node;
1079 }
1080 #endif
1081
1082 static guint attract_hash_func(gconstpointer key)
1083 {
1084         gchar *str;
1085         gchar *p;
1086         guint h;
1087
1088         Xstrdup_a(str, (const gchar *)key, return 0);
1089         trim_subject(str);
1090
1091         p = str;
1092         h = *p;
1093
1094         if (h) {
1095                 for (p += 1; *p != '\0'; p++)
1096                         h = (h << 5) - h + *p;
1097         }
1098
1099         return h;
1100 }
1101
1102 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1103 {
1104         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1105 }
1106
1107 void summary_attract_by_subject(SummaryView *summaryview)
1108 {
1109         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1110         GtkCList *clist = GTK_CLIST(ctree);
1111         GtkCTreeNode *src_node;
1112         GtkCTreeNode *dst_node, *sibling;
1113         GtkCTreeNode *tmp;
1114         MsgInfo *src_msginfo, *dst_msginfo;
1115         GHashTable *subject_table;
1116
1117         debug_print(_("Attracting messages by subject..."));
1118         STATUSBAR_PUSH(summaryview->mainwin,
1119                        _("Attracting messages by subject..."));
1120
1121         main_window_cursor_wait(summaryview->mainwin);
1122         gtk_clist_freeze(clist);
1123
1124         subject_table = g_hash_table_new(attract_hash_func,
1125                                          attract_compare_func);
1126
1127         for (src_node = GTK_CTREE_NODE(clist->row_list);
1128              src_node != NULL;
1129              src_node = tmp) {
1130                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1131                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1132                 if (!src_msginfo) continue;
1133                 if (!src_msginfo->subject) continue;
1134
1135                 /* find attracting node */
1136                 dst_node = g_hash_table_lookup(subject_table,
1137                                                src_msginfo->subject);
1138
1139                 if (dst_node) {
1140                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1141
1142                         /* if the time difference is more than 20 days,
1143                            don't attract */
1144                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1145                             > 60 * 60 * 24 * 20)
1146                                 continue;
1147
1148                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1149                         if (src_node != sibling)
1150                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1151                 }
1152
1153                 g_hash_table_insert(subject_table,
1154                                     src_msginfo->subject, src_node);
1155         }
1156
1157         g_hash_table_destroy(subject_table);
1158
1159         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1160
1161         gtk_clist_thaw(clist);
1162
1163         debug_print(_("done.\n"));
1164         STATUSBAR_POP(summaryview->mainwin);
1165
1166         main_window_cursor_normal(summaryview->mainwin);
1167 }
1168
1169 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1170                                       gpointer data)
1171 {
1172         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1173
1174         if (msginfo)
1175                 procmsg_msginfo_free(msginfo);
1176 }
1177
1178 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1179                                    gpointer data)
1180 {
1181         SummaryView *summaryview = data;
1182         MsgInfo *msginfo;
1183
1184         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1185
1186         if (MSG_IS_NEW(msginfo->flags))
1187                 summaryview->newmsgs++;
1188         if (MSG_IS_UNREAD(msginfo->flags))
1189                 summaryview->unread++;
1190         if (MSG_IS_DELETED(msginfo->flags))
1191                 summaryview->deleted++;
1192
1193         summaryview->messages++;
1194         summaryview->total_size += msginfo->size;
1195
1196         summary_set_row_marks(summaryview, node);
1197 }
1198
1199 static void summary_update_status(SummaryView *summaryview)
1200 {
1201         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1202         GtkCTreeNode *node;
1203         MsgInfo *msginfo;
1204
1205         summaryview->newmsgs = summaryview->unread =
1206         summaryview->messages = summaryview->total_size =
1207         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1208
1209         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1210              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1211                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1212
1213                 if (MSG_IS_NEW(msginfo->flags))
1214                         summaryview->newmsgs++;
1215                 if (MSG_IS_UNREAD(msginfo->flags))
1216                         summaryview->unread++;
1217                 if (MSG_IS_DELETED(msginfo->flags))
1218                         summaryview->deleted++;
1219                 if (MSG_IS_MOVE(msginfo->flags))
1220                         summaryview->moved++;
1221                 if (MSG_IS_COPY(msginfo->flags))
1222                         summaryview->copied++;
1223                 summaryview->messages++;
1224                 summaryview->total_size += msginfo->size;
1225         }
1226 }
1227
1228 static void summary_status_show(SummaryView *summaryview)
1229 {
1230         gchar *str;
1231         gchar *del, *mv, *cp;
1232         gchar *sel;
1233         gchar *spc;
1234         GList *rowlist, *cur;
1235         guint n_selected = 0;
1236         off_t sel_size = 0;
1237         MsgInfo *msginfo;
1238
1239         if (!summaryview->folder_item) {
1240                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1241                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1242                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1243                 return;
1244         }
1245
1246         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1247         for (cur = rowlist; cur != NULL; cur = cur->next) {
1248                 msginfo = gtk_ctree_node_get_row_data
1249                         (GTK_CTREE(summaryview->ctree),
1250                          GTK_CTREE_NODE(cur->data));
1251                 sel_size += msginfo->size;
1252                 n_selected++;
1253         }
1254
1255         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1256                       summaryview->folder_item &&
1257                       summaryview->folder_item->folder->type == F_NEWS
1258                       ? g_basename(summaryview->folder_item->path)
1259                       : summaryview->folder_item->path);
1260
1261         if (summaryview->deleted)
1262                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1263         else
1264                 del = g_strdup("");
1265         if (summaryview->moved)
1266                 mv = g_strdup_printf(_("%s%d moved"),
1267                                      summaryview->deleted ? _(", ") : "",
1268                                      summaryview->moved);
1269         else
1270                 mv = g_strdup("");
1271         if (summaryview->copied)
1272                 cp = g_strdup_printf(_("%s%d copied"),
1273                                      summaryview->deleted ||
1274                                      summaryview->moved ? _(", ") : "",
1275                                      summaryview->copied);
1276         else
1277                 cp = g_strdup("");
1278
1279         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1280                 spc = "    ";
1281         else
1282                 spc = "";
1283
1284         if (n_selected)
1285                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1286         else
1287                 sel = g_strdup("");
1288         str = g_strconcat(n_selected ? itos(n_selected) : "",
1289                           n_selected ? _(" item(s) selected") : "",
1290                           sel, spc, del, mv, cp, NULL);
1291         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1292         g_free(str);
1293         g_free(sel);
1294         g_free(del);
1295         g_free(mv);
1296         g_free(cp);
1297
1298         if (summaryview->folder_item &&
1299             summaryview->folder_item->folder->type == F_MH) {
1300                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1301                                       summaryview->newmsgs,
1302                                       summaryview->unread,
1303                                       summaryview->messages,
1304                                       to_human_readable(summaryview->total_size));
1305         } else {
1306                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1307                                       summaryview->newmsgs,
1308                                       summaryview->unread,
1309                                       summaryview->messages);
1310         }
1311         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1312         g_free(str);
1313
1314         folderview_update_msg_num(summaryview->folderview,
1315                                   summaryview->folderview->opened,
1316                                   summaryview->newmsgs,
1317                                   summaryview->unread,
1318                                   summaryview->messages);
1319 }
1320
1321 void summary_sort(SummaryView *summaryview, SummarySortType type)
1322 {
1323         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1324         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1325         GtkCListCompareFunc cmp_func;
1326
1327         switch (type) {
1328         case SORT_BY_NUMBER:
1329                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1330                 break;
1331         case SORT_BY_SIZE:
1332                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1333                 break;
1334         case SORT_BY_DATE:
1335                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1336                 break;
1337         case SORT_BY_FROM:
1338                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1339                 break;
1340         case SORT_BY_SUBJECT:
1341                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1342                 break;
1343         case SORT_BY_SCORE:
1344                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1345                 break;
1346         default:
1347                 return;
1348         }
1349
1350         debug_print(_("Sorting summary..."));
1351         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1352
1353         main_window_cursor_wait(summaryview->mainwin);
1354
1355         gtk_clist_set_compare_func(clist, cmp_func);
1356
1357         /* toggle sort type if the same column is selected */
1358         if (summaryview->sort_mode == type)
1359                 summaryview->sort_type =
1360                         summaryview->sort_type == GTK_SORT_ASCENDING
1361                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1362         else
1363                 summaryview->sort_type = GTK_SORT_ASCENDING;
1364         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1365         summaryview->sort_mode = type;
1366
1367         gtk_ctree_sort_node(ctree, NULL);
1368
1369         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1370         //gtkut_ctree_set_focus_row(ctree, summaryview->selected);
1371
1372         prefs_folder_item_set_config(summaryview->folder_item,
1373                                      summaryview->sort_type,
1374                                      summaryview->sort_mode);
1375         prefs_folder_item_save_config(summaryview->folder_item);
1376
1377         debug_print(_("done.\n"));
1378         STATUSBAR_POP(summaryview->mainwin);
1379
1380         main_window_cursor_normal(summaryview->mainwin);
1381 }
1382
1383 static void summary_set_ctree_from_list(SummaryView *summaryview,
1384                                         GSList *mlist)
1385 {
1386         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1387         MsgInfo *msginfo;
1388         GtkCTreeNode *node, *parent;
1389         gchar *text[N_SUMMARY_COLS];
1390         GHashTable *msgid_table;
1391
1392         if (!mlist) return;
1393
1394         debug_print(_("\tSetting summary from message data..."));
1395         STATUSBAR_PUSH(summaryview->mainwin,
1396                        _("Setting summary from message data..."));
1397
1398         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1399         summaryview->msgid_table = msgid_table;
1400
1401         if (prefs_common.use_addr_book)
1402                 start_address_completion();
1403         
1404         main_window_set_thread_option(summaryview->mainwin);
1405
1406         /*      if (prefs_common.enable_thread) { */
1407         if (summaryview->folder_item->prefs->enable_thread) {
1408                 for (; mlist != NULL; mlist = mlist->next) {
1409                         msginfo = (MsgInfo *)mlist->data;
1410                         parent = NULL;
1411
1412                         summary_set_header(text, msginfo);
1413
1414                         /* search parent node for threading */
1415                         if (msginfo->inreplyto && *msginfo->inreplyto)
1416                                 parent = g_hash_table_lookup
1417                                         (msgid_table, msginfo->inreplyto);
1418
1419                         node = gtk_ctree_insert_node
1420                                 (ctree, parent, NULL, text, 2,
1421                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1422                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1423                         summary_set_marks_func(ctree, node, summaryview);
1424
1425                         /* preserve previous node if the message is
1426                            duplicated */
1427                         if (msginfo->msgid && *msginfo->msgid &&
1428                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1429                             == NULL)
1430                                 g_hash_table_insert(msgid_table,
1431                                                     msginfo->msgid, node);
1432                 }
1433
1434                 /* complete the thread */
1435                 summary_thread_build(summaryview);
1436         } else {
1437                 for (; mlist != NULL; mlist = mlist->next) {
1438                         msginfo = (MsgInfo *)mlist->data;
1439
1440                         summary_set_header(text, msginfo);
1441
1442                         node = gtk_ctree_insert_node
1443                                 (ctree, NULL, NULL, text, 2,
1444                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1445                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1446                         summary_set_marks_func(ctree, node, summaryview);
1447
1448                         if (msginfo->msgid && *msginfo->msgid &&
1449                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1450                             == NULL)
1451                                 g_hash_table_insert(msgid_table,
1452                                                     msginfo->msgid, node);
1453                 }
1454         }
1455
1456         if (prefs_common.enable_hscrollbar) {
1457                 gint optimal_width;
1458
1459                 optimal_width = gtk_clist_optimal_column_width
1460                         (GTK_CLIST(ctree), S_COL_SUBJECT);
1461                 gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
1462                                            optimal_width);
1463         }
1464
1465         if (prefs_common.use_addr_book)
1466                 end_address_completion();
1467
1468         debug_print(_("done.\n"));
1469         STATUSBAR_POP(summaryview->mainwin);
1470         if (debug_mode)
1471                 debug_print("\tmsgid hash table size = %d\n",
1472                             g_hash_table_size(msgid_table));
1473 }
1474
1475 struct wcachefp
1476 {
1477         FILE *cache_fp;
1478         FILE *mark_fp;
1479 };
1480
1481 gint summary_write_cache(SummaryView *summaryview)
1482 {
1483         struct wcachefp fps;
1484         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1485         gint ver = CACHE_VERSION;
1486         gchar *buf;
1487         gchar *cachefile, *markfile;
1488
1489         if (!summaryview->folder_item || !summaryview->folder_item->path)
1490                 return -1;
1491
1492         cachefile = folder_item_get_cache_file(summaryview->folder_item);
1493         g_return_val_if_fail(cachefile != NULL, -1);
1494         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
1495                 FILE_OP_ERROR(cachefile, "fopen");
1496                 g_free(cachefile);
1497                 return -1;
1498         }
1499         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
1500                 FILE_OP_ERROR(cachefile, "chmod");
1501         g_free(cachefile);
1502
1503         markfile = folder_item_get_mark_file(summaryview->folder_item);
1504         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
1505                 FILE_OP_ERROR(markfile, "fopen");
1506                 fclose(fps.cache_fp);
1507                 g_free(markfile);
1508                 return -1;
1509         }
1510         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
1511                 FILE_OP_ERROR(markfile, "chmod");
1512         g_free(markfile);
1513
1514         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
1515                               summaryview->folder_item->path);
1516         debug_print(buf);
1517         STATUSBAR_PUSH(summaryview->mainwin, buf);
1518         g_free(buf);
1519
1520         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
1521         ver = MARK_VERSION;
1522         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
1523
1524         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
1525
1526         fclose(fps.cache_fp);
1527         fclose(fps.mark_fp);
1528
1529         debug_print(_("done.\n"));
1530         STATUSBAR_POP(summaryview->mainwin);
1531
1532         return 0;
1533 }
1534
1535 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
1536                                      gpointer data)
1537 {
1538         struct wcachefp *fps = data;
1539         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1540
1541         if (msginfo == NULL) return;
1542
1543         procmsg_write_cache(msginfo, fps->cache_fp);
1544         procmsg_write_flags(msginfo, fps->mark_fp);
1545 }
1546
1547 static void summary_set_header(gchar *text[], MsgInfo *msginfo)
1548 {
1549         static gchar date_modified[80];
1550         static gchar *to = NULL;
1551         static gchar *from_name = NULL;
1552         static gchar col_number[11];
1553         static gchar col_score[11];
1554
1555         text[S_COL_MARK]   = NULL;
1556         text[S_COL_UNREAD] = NULL;
1557         text[S_COL_MIME] = NULL;
1558         text[S_COL_NUMBER] = itos_buf(col_number, msginfo->msgnum);
1559         text[S_COL_SIZE]   = to_human_readable(msginfo->size);
1560         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->score);
1561
1562         if (msginfo->date_t) {
1563                 procheader_date_get_localtime(date_modified,
1564                                               sizeof(date_modified),
1565                                               msginfo->date_t);
1566                 text[S_COL_DATE] = date_modified;
1567         } else if (msginfo->date)
1568                 text[S_COL_DATE] = msginfo->date;
1569         else
1570                 text[S_COL_DATE] = _("(No Date)");
1571
1572         text[S_COL_FROM] = msginfo->fromname ? msginfo->fromname :
1573                 _("(No From)");
1574         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
1575             cur_account && cur_account->address &&
1576             !MSG_IS_NEWS(msginfo->flags)) {
1577                 gchar *from;
1578
1579                 Xalloca(from, strlen(msginfo->from) + 1, return);
1580                 strcpy(from, msginfo->from);
1581                 extract_address(from);
1582                 if (!strcmp(from, cur_account->address)) {
1583                         g_free(to);
1584                         to = g_strconcat("-->", msginfo->to, NULL);
1585                         text[S_COL_FROM] = to;
1586                 }
1587         }
1588
1589         if ((text[S_COL_FROM] != to) && prefs_common.use_addr_book &&
1590             msginfo->from) {
1591                 gint count;
1592                 gchar *from;
1593   
1594                 Xalloca(from, strlen(msginfo->from) + 1, return);
1595                 strcpy(from, msginfo->from);
1596                 extract_address(from);
1597                 count = complete_address(from);
1598                 if (count > 1) {
1599                         g_free(from_name);
1600                         from = get_complete_address(1);
1601                         from_name = procheader_get_fromname(from);
1602                         g_free(from);
1603                         text[S_COL_FROM] = from_name;
1604                 }
1605         }
1606
1607         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
1608                 _("(No Subject)");
1609 }
1610
1611 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
1612                                 gboolean new_window)
1613 {
1614         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1615         MsgInfo *msginfo;
1616         gchar *filename;
1617         static gboolean lock = FALSE;
1618
1619         if (!new_window && summaryview->displayed == row) return;
1620         g_return_if_fail(row != NULL);
1621
1622         if (lock) return;
1623         lock = TRUE;
1624
1625         STATUSBAR_POP(summaryview->mainwin);
1626
1627         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1628
1629         filename = procmsg_get_message_file(msginfo);
1630         if (!filename) {
1631                 lock = FALSE;
1632                 return;
1633         }
1634         g_free(filename);
1635
1636         if (MSG_IS_NEW(msginfo->flags))
1637                 summaryview->newmsgs--;
1638         if (MSG_IS_UNREAD(msginfo->flags))
1639                 summaryview->unread--;
1640         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
1641                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1642                 summary_set_row_marks(summaryview, row);
1643                 gtk_clist_thaw(GTK_CLIST(ctree));
1644                 summary_status_show(summaryview);
1645         }
1646
1647         if (new_window) {
1648                 MessageView *msgview;
1649
1650                 msgview = messageview_create_with_new_window();
1651                 messageview_show(msgview, msginfo);
1652         } else {
1653                 MessageView *msgview;
1654
1655                 msgview = summaryview->messageview;
1656
1657                 summaryview->displayed = row;
1658                 if (!summaryview->msg_is_toggled_on)
1659                         summary_toggle_view(summaryview);
1660                 messageview_show(msgview, msginfo);
1661                 if (msgview->type == MVIEW_TEXT ||
1662                     (msgview->type == MVIEW_MIME &&
1663                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
1664                         gtk_widget_grab_focus(summaryview->ctree);
1665                 GTK_EVENTS_FLUSH();
1666                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
1667         }
1668
1669         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
1670                 header_window_show(summaryview->headerwin, msginfo);
1671
1672         lock = FALSE;
1673 }
1674
1675 void summary_redisplay_msg(SummaryView *summaryview)
1676 {
1677         GtkCTreeNode *node;
1678
1679         if (summaryview->displayed) {
1680                 node = summaryview->displayed;
1681                 summaryview->displayed = NULL;
1682                 summary_display_msg(summaryview, node, FALSE);
1683         }
1684 }
1685
1686 void summary_open_msg(SummaryView *summaryview)
1687 {
1688         if (!summaryview->selected) return;
1689
1690         summary_display_msg(summaryview, summaryview->selected, TRUE);
1691 }
1692
1693 void summary_view_source(SummaryView * summaryview)
1694 {
1695         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1696         MsgInfo *msginfo;
1697         SourceWindow *srcwin;
1698
1699         if (!summaryview->selected) return;
1700
1701         srcwin = source_window_create();
1702         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1703         source_window_show_msg(srcwin, msginfo);
1704         source_window_show(srcwin);
1705 }
1706
1707 void summary_reedit(SummaryView *summaryview)
1708 {
1709         MsgInfo *msginfo;
1710
1711         if (!summaryview->selected) return;
1712         if (!summaryview->folder_item ||
1713             summaryview->folder_item->stype != F_DRAFT) return;
1714
1715         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
1716                                               summaryview->selected);
1717         if (!msginfo) return;
1718
1719         compose_reedit(msginfo);
1720 }
1721
1722 void summary_step(SummaryView *summaryview, GtkScrollType type)
1723 {
1724         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1725
1726         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
1727                                 type, 0.0);
1728
1729         if (summaryview->msg_is_toggled_on)
1730                 summary_display_msg(summaryview, summaryview->selected, FALSE);
1731 }
1732
1733 static void summary_toggle_view(SummaryView *summaryview)
1734 {
1735         MainWindow *mainwin = summaryview->mainwin;
1736         union CompositeWin *cwin = &mainwin->win;
1737         GtkWidget *vpaned = NULL;
1738         GtkWidget *container = NULL;
1739
1740         switch (mainwin->type) {
1741         case SEPARATE_NONE:
1742                 vpaned = cwin->sep_none.vpaned;
1743                 container = cwin->sep_none.hpaned;
1744                 break;
1745         case SEPARATE_FOLDER:
1746                 vpaned = cwin->sep_folder.vpaned;
1747                 container = mainwin->vbox_body;
1748                 break;
1749         case SEPARATE_MESSAGE:
1750         case SEPARATE_BOTH:
1751                 return;
1752         }
1753
1754         if (vpaned->parent != NULL) {
1755                 summaryview->msg_is_toggled_on = FALSE;
1756                 summaryview->displayed = NULL;
1757                 gtk_widget_ref(vpaned);
1758                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
1759                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1760                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1761                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1762         } else {
1763                 summaryview->msg_is_toggled_on = TRUE;
1764                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1765                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1766                 gtk_widget_unref(vpaned);
1767                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1768                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1769         }
1770
1771         gtk_widget_grab_focus(summaryview->ctree);
1772 }
1773
1774 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
1775 {
1776         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1777         MsgInfo *msginfo;
1778         MsgFlags flags;
1779
1780         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1781         if (!msginfo) return;
1782
1783         flags = msginfo->flags;
1784
1785         gtk_ctree_node_set_foreground(ctree, row, &summaryview->color_normal);
1786
1787         /* set new/unread column */
1788         if (MSG_IS_NEW(flags)) {
1789                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1790                                           newxpm, newxpmmask);
1791         } else if (MSG_IS_UNREAD(flags)) {
1792                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1793                                           unreadxpm, unreadxpmmask);
1794         } else if (MSG_IS_REPLIED(flags)) {
1795                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1796                                           repliedxpm, repliedxpmmask);
1797         } else if (MSG_IS_FORWARDED(flags)) {
1798                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1799                                           forwardedxpm, forwardedxpmmask);
1800         } else {
1801                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
1802         }
1803
1804         /* set mark column */
1805         if (MSG_IS_DELETED(flags)) {
1806                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
1807                                           deletedxpm, deletedxpmmask);
1808                 gtk_ctree_node_set_foreground(ctree, row,
1809                                               &summaryview->color_dim);
1810         } else if (MSG_IS_MARKED(flags)) {
1811                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
1812                                           markxpm, markxpmmask);
1813         } else if (MSG_IS_MOVE(flags)) {
1814                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "o");
1815                 gtk_ctree_node_set_foreground(ctree, row,
1816                                               &summaryview->color_marked);
1817         } else if (MSG_IS_COPY(flags)) {
1818                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
1819                 gtk_ctree_node_set_foreground(ctree, row,
1820                                               &summaryview->color_marked);
1821         } else {
1822                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
1823         }
1824
1825         if (MSG_IS_MIME(flags)) {
1826                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
1827                                           clipxpm, clipxpmmask);
1828         } else {
1829                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
1830         }
1831 }
1832
1833 void summary_set_marks_selected(SummaryView *summaryview)
1834 {
1835         summary_set_row_marks(summaryview, summaryview->selected);
1836 }
1837
1838 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
1839 {
1840         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1841         MsgInfo *msginfo;
1842
1843         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1844         msginfo->to_folder = NULL;
1845         if (MSG_IS_DELETED(msginfo->flags))
1846                 summaryview->deleted--;
1847         if (MSG_IS_MOVE(msginfo->flags))
1848                 summaryview->moved--;
1849         if (MSG_IS_COPY(msginfo->flags))
1850                 summaryview->copied--;
1851         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
1852         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
1853         summary_set_row_marks(summaryview, row);
1854         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
1855 }
1856
1857 void summary_mark(SummaryView *summaryview)
1858 {
1859         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1860         GList *cur;
1861
1862         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1863                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
1864
1865         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
1866 }
1867
1868 static void summary_mark_row_as_read(SummaryView *summaryview,
1869                                      GtkCTreeNode *row)
1870 {
1871         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1872         MsgInfo *msginfo;
1873
1874         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1875         if (MSG_IS_NEW(msginfo->flags))
1876                 summaryview->newmsgs--;
1877         if (MSG_IS_UNREAD(msginfo->flags))
1878                 summaryview->unread--;
1879         if (MSG_IS_NEW(msginfo->flags) ||
1880             MSG_IS_UNREAD(msginfo->flags)) {
1881                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1882                 summary_set_row_marks(summaryview, row);
1883                 debug_print(_("Message %d is marked as being read\n"),
1884                             msginfo->msgnum);
1885         }
1886 }
1887
1888 void summary_mark_as_read(SummaryView *summaryview)
1889 {
1890         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1891         GList *cur;
1892
1893         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1894                 summary_mark_row_as_read(summaryview,
1895                                          GTK_CTREE_NODE(cur->data));
1896
1897         summary_status_show(summaryview);
1898 }
1899
1900 static void summary_mark_row_as_unread(SummaryView *summaryview,
1901                                        GtkCTreeNode *row)
1902 {
1903         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1904         MsgInfo *msginfo;
1905
1906         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1907         if (MSG_IS_DELETED(msginfo->flags)) {
1908                 msginfo->to_folder = NULL;
1909                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
1910                 summaryview->deleted--;
1911         }
1912         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
1913         if (!MSG_IS_UNREAD(msginfo->flags)) {
1914                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
1915                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1916                                           unreadxpm, unreadxpmmask);
1917                 summaryview->unread++;
1918                 debug_print(_("Message %d is marked as unread\n"),
1919                             msginfo->msgnum);
1920         }
1921         summary_set_row_marks(summaryview, row);
1922 }
1923
1924 void summary_mark_as_unread(SummaryView *summaryview)
1925 {
1926         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1927         GList *cur;
1928
1929         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1930                 summary_mark_row_as_unread(summaryview,
1931                                            GTK_CTREE_NODE(cur->data));
1932
1933         summary_status_show(summaryview);
1934 }
1935
1936 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
1937 {
1938         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1939         MsgInfo *msginfo;
1940
1941         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1942
1943         if (MSG_IS_DELETED(msginfo->flags)) return;
1944
1945         msginfo->to_folder = NULL;
1946         if (MSG_IS_MOVE(msginfo->flags))
1947                 summaryview->moved--;
1948         if (MSG_IS_COPY(msginfo->flags))
1949                 summaryview->copied--;
1950         MSG_UNSET_FLAGS(msginfo->flags,
1951                         MSG_MARKED |
1952                         MSG_MOVE |
1953                         MSG_COPY);
1954         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
1955         summaryview->deleted++;
1956
1957         if (!prefs_common.immediate_exec)
1958                 summary_set_row_marks(summaryview, row);
1959
1960         debug_print(_("Message %s/%d is set to delete\n"),
1961                     msginfo->folder->path, msginfo->msgnum);
1962 }
1963
1964 void summary_delete(SummaryView *summaryview)
1965 {
1966         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1967         GList *cur;
1968
1969         if (!summaryview->folder_item ||
1970             summaryview->folder_item->folder->type == F_NEWS) return;
1971
1972         /* if current folder is trash, don't delete */
1973         if (summaryview->folder_item->stype == F_TRASH) {
1974                 alertpanel_notice(_("Current folder is Trash."));
1975                 return;
1976         }
1977
1978         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1979                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
1980
1981         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
1982
1983         if (prefs_common.immediate_exec)
1984                 summary_execute(summaryview);
1985         else
1986                 summary_status_show(summaryview);
1987 }
1988
1989 void summary_delete_duplicated(SummaryView *summaryview)
1990 {
1991         if (!summaryview->folder_item ||
1992             summaryview->folder_item->folder->type == F_NEWS) return;
1993         if (summaryview->folder_item->stype == F_TRASH) return;
1994
1995         main_window_cursor_wait(summaryview->mainwin);
1996         debug_print(_("Deleting duplicated messages..."));
1997         STATUSBAR_PUSH(summaryview->mainwin,
1998                        _("Deleting duplicated messages..."));
1999
2000         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2001                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2002                                 summaryview);
2003
2004         if (prefs_common.immediate_exec)
2005                 summary_execute(summaryview);
2006         else
2007                 summary_status_show(summaryview);
2008
2009         debug_print(_("done.\n"));
2010         STATUSBAR_POP(summaryview->mainwin);
2011         main_window_cursor_normal(summaryview->mainwin);
2012 }
2013
2014 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2015                                            SummaryView *summaryview)
2016 {
2017         GtkCTreeNode *found;
2018         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2019
2020         if (!msginfo->msgid || !*msginfo->msgid) return;
2021
2022         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2023
2024         if (found && found != node)
2025                 summary_delete_row(summaryview, node);
2026 }
2027
2028 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2029 {
2030         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2031         MsgInfo *msginfo;
2032
2033         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2034         msginfo->to_folder = NULL;
2035         if (MSG_IS_DELETED(msginfo->flags))
2036                 summaryview->deleted--;
2037         if (MSG_IS_MOVE(msginfo->flags))
2038                 summaryview->moved--;
2039         if (MSG_IS_COPY(msginfo->flags))
2040                 summaryview->copied--;
2041         MSG_UNSET_FLAGS(msginfo->flags,
2042                         MSG_MARKED |
2043                         MSG_DELETED |
2044                         MSG_MOVE |
2045                         MSG_COPY);
2046         summary_set_row_marks(summaryview, row);
2047
2048         debug_print(_("Message %s/%d is unmarked\n"),
2049                     msginfo->folder->path, msginfo->msgnum);
2050 }
2051
2052 void summary_unmark(SummaryView *summaryview)
2053 {
2054         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2055         GList *cur;
2056
2057         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2058              cur = cur->next)
2059                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2060
2061         summary_status_show(summaryview);
2062 }
2063
2064 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2065                                 FolderItem *to_folder)
2066 {
2067         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2068         MsgInfo *msginfo;
2069
2070         g_return_if_fail(to_folder != NULL);
2071
2072         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2073         msginfo->to_folder = to_folder;
2074         if (MSG_IS_DELETED(msginfo->flags))
2075                 summaryview->deleted--;
2076         MSG_UNSET_FLAGS(msginfo->flags,
2077                         MSG_MARKED | MSG_DELETED | MSG_COPY);
2078         if (!MSG_IS_MOVE(msginfo->flags)) {
2079                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
2080                 summaryview->moved++;
2081         }
2082         if (!prefs_common.immediate_exec)
2083                 summary_set_row_marks(summaryview, row);
2084
2085         debug_print(_("Message %d is set to move to %s\n"),
2086                     msginfo->msgnum, to_folder->path);
2087 }
2088
2089 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2090 {
2091         GList *cur;
2092
2093         if (!to_folder) return;
2094         if (!summaryview->folder_item ||
2095             summaryview->folder_item->folder->type == F_NEWS) return;
2096         if (summaryview->folder_item == to_folder) {
2097                 alertpanel_notice(_("Destination is same as current folder."));
2098                 return;
2099         }
2100
2101         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2102              cur != NULL; cur = cur->next)
2103                 summary_move_row_to
2104                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2105
2106         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2107
2108         if (prefs_common.immediate_exec)
2109                 summary_execute(summaryview);
2110         else
2111                 summary_status_show(summaryview);
2112 }
2113
2114 void summary_move_to(SummaryView *summaryview)
2115 {
2116         FolderItem *to_folder;
2117
2118         if (!summaryview->folder_item ||
2119             summaryview->folder_item->folder->type == F_NEWS) return;
2120
2121         to_folder = foldersel_folder_sel(NULL);
2122         summary_move_selected_to(summaryview, to_folder);
2123 }
2124
2125 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2126                                 FolderItem *to_folder)
2127 {
2128         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2129         MsgInfo *msginfo;
2130
2131         g_return_if_fail(to_folder != NULL);
2132
2133         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2134         msginfo->to_folder = to_folder;
2135         if (MSG_IS_DELETED(msginfo->flags))
2136                 summaryview->deleted--;
2137         MSG_UNSET_FLAGS(msginfo->flags,
2138                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
2139         if (!MSG_IS_COPY(msginfo->flags)) {
2140                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
2141                 summaryview->copied++;
2142         }
2143         if (!prefs_common.immediate_exec)
2144                 summary_set_row_marks(summaryview, row);
2145
2146         debug_print(_("Message %d is set to copy to %s\n"),
2147                     msginfo->msgnum, to_folder->path);
2148 }
2149
2150 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2151 {
2152         GList *cur;
2153
2154         if (!to_folder) return;
2155         if (!summaryview->folder_item ||
2156             summaryview->folder_item->folder->type == F_NEWS) return;
2157         if (summaryview->folder_item == to_folder) {
2158                 alertpanel_notice
2159                         (_("Destination to copy is same as current folder."));
2160                 return;
2161         }
2162
2163         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2164              cur != NULL; cur = cur->next)
2165                 summary_copy_row_to
2166                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2167
2168         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2169
2170         if (prefs_common.immediate_exec)
2171                 summary_execute(summaryview);
2172         else
2173                 summary_status_show(summaryview);
2174 }
2175
2176 void summary_copy_to(SummaryView *summaryview)
2177 {
2178         FolderItem *to_folder;
2179
2180         if (!summaryview->folder_item ||
2181             summaryview->folder_item->folder->type == F_NEWS) return;
2182
2183         to_folder = foldersel_folder_sel(NULL);
2184         summary_copy_selected_to(summaryview, to_folder);
2185 }
2186
2187 void summary_select_all(SummaryView *summaryview)
2188 {
2189         if (summaryview->messages >= 500) {
2190                 STATUSBAR_PUSH(summaryview->mainwin,
2191                                _("Selecting all messages..."));
2192                 main_window_cursor_wait(summaryview->mainwin);
2193         }
2194
2195         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2196
2197         if (summaryview->messages >= 500) {
2198                 STATUSBAR_POP(summaryview->mainwin);
2199                 main_window_cursor_normal(summaryview->mainwin);
2200         }
2201 }
2202
2203 void summary_unselect_all(SummaryView *summaryview)
2204 {
2205         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2206 }
2207
2208 void summary_save_as(SummaryView *summaryview)
2209 {
2210         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2211         MsgInfo *msginfo;
2212         gchar *src, *dest;
2213
2214         if (!summaryview->selected) return;
2215         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2216         if (!msginfo) return;
2217
2218         dest = filesel_select_file(_("Save as"), NULL);
2219         if (!dest) return;
2220         if (is_file_exist(dest)) {
2221                 AlertValue aval;
2222
2223                 aval = alertpanel(_("Overwrite"),
2224                                   _("Overwrite existing file?"),
2225                                   _("OK"), _("Cancel"), NULL);
2226                 if (G_ALERTDEFAULT != aval) return;
2227         }
2228
2229         src = procmsg_get_message_file(msginfo);
2230         copy_file(src, dest);
2231         g_free(src);
2232 }
2233
2234 void summary_print(SummaryView *summaryview)
2235 {
2236         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2237         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2238         MsgInfo *msginfo;
2239         GList *cur;
2240         gchar *cmdline;
2241         gchar *p;
2242
2243         if (clist->selection == NULL) return;
2244
2245         cmdline = input_dialog(_("Print"),
2246                                _("Enter the print command line:\n"
2247                                  "(`%s' will be replaced with file name)"),
2248                                prefs_common.print_cmd);
2249         if (!cmdline) return;
2250         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2251             strchr(p + 2, '%')) {
2252                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2253                                  cmdline);
2254                 g_free(cmdline);
2255                 return;
2256         }
2257
2258         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2259                 msginfo = gtk_ctree_node_get_row_data
2260                         (ctree, GTK_CTREE_NODE(cur->data));
2261                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2262         }
2263         
2264         g_free(cmdline);
2265 }
2266
2267 void summary_execute(SummaryView *summaryview)
2268 {
2269         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2270         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2271         GtkCTreeNode *node, *next;
2272
2273         if (!summaryview->folder_item ||
2274             summaryview->folder_item->folder->type == F_NEWS) return;
2275
2276         gtk_clist_freeze(clist);
2277
2278         if (prefs_common.enable_thread)
2279                 summary_unthread_for_exec(summaryview);
2280
2281         summary_execute_move(summaryview);
2282         summary_execute_copy(summaryview);
2283         summary_execute_delete(summaryview);
2284
2285         node = GTK_CTREE_NODE(clist->row_list);
2286         while (node != NULL) {
2287                 next = GTK_CTREE_NODE_NEXT(node);
2288                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2289                         if (node == summaryview->displayed) {
2290                                 messageview_clear(summaryview->messageview);
2291                                 summaryview->displayed = NULL;
2292                         }
2293                         if (GTK_CTREE_ROW(node)->children != NULL)
2294                                 g_warning("summary_execute(): children != NULL\n");
2295                         else
2296                                 gtk_ctree_remove_node(ctree, node);
2297                 }
2298                 node = next;
2299         }
2300
2301         if (prefs_common.enable_thread)
2302                 summary_thread_build(summaryview);
2303
2304         summaryview->selected = clist->selection ?
2305                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2306
2307         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2308                 menu_set_insensitive_all
2309                         (GTK_MENU_SHELL(summaryview->popupmenu));
2310                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2311         } else
2312                 gtk_widget_grab_focus(summaryview->ctree);
2313
2314         summary_update_status(summaryview);
2315         summary_status_show(summaryview);
2316
2317         summary_write_cache(summaryview);
2318
2319         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2320
2321         gtk_clist_thaw(clist);
2322 }
2323
2324 static void summary_execute_move(SummaryView *summaryview)
2325 {
2326         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2327         GSList *cur;
2328
2329         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2330
2331         /* search moving messages and execute */
2332         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2333                                 summaryview);
2334
2335         if (summaryview->mlist) {
2336                 procmsg_move_messages(summaryview->mlist);
2337
2338                 folder_item_scan_foreach(summaryview->folder_table);
2339                 folderview_update_item_foreach(summaryview->folder_table);
2340
2341                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2342                         procmsg_msginfo_free((MsgInfo *)cur->data);
2343                 g_slist_free(summaryview->mlist);
2344                 summaryview->mlist = NULL;
2345         }
2346
2347         g_hash_table_destroy(summaryview->folder_table);
2348         summaryview->folder_table = NULL;
2349 }
2350
2351 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2352                                       gpointer data)
2353 {
2354         SummaryView *summaryview = data;
2355         MsgInfo *msginfo;
2356
2357         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2358
2359         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2360                 g_hash_table_insert(summaryview->folder_table,
2361                                     msginfo->to_folder, GINT_TO_POINTER(1));
2362
2363                 summaryview->mlist =
2364                         g_slist_append(summaryview->mlist, msginfo);
2365                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2366
2367                 if (msginfo->msgid && *msginfo->msgid &&
2368                     node == g_hash_table_lookup(summaryview->msgid_table,
2369                                                 msginfo->msgid))
2370                         g_hash_table_remove(summaryview->msgid_table,
2371                                             msginfo->msgid);
2372         }
2373 }
2374
2375 static void summary_execute_copy(SummaryView *summaryview)
2376 {
2377         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2378
2379         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2380
2381         /* search copying messages and execute */
2382         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2383                                 summaryview);
2384
2385         if (summaryview->mlist) {
2386                 procmsg_copy_messages(summaryview->mlist);
2387
2388                 folder_item_scan_foreach(summaryview->folder_table);
2389                 folderview_update_item_foreach(summaryview->folder_table);
2390
2391                 g_slist_free(summaryview->mlist);
2392                 summaryview->mlist = NULL;
2393         }
2394
2395         g_hash_table_destroy(summaryview->folder_table);
2396         summaryview->folder_table = NULL;
2397 }
2398
2399 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
2400                                       gpointer data)
2401 {
2402         SummaryView *summaryview = data;
2403         MsgInfo *msginfo;
2404
2405         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2406
2407         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
2408                 g_hash_table_insert(summaryview->folder_table,
2409                                     msginfo->to_folder, GINT_TO_POINTER(1));
2410
2411                 summaryview->mlist =
2412                         g_slist_append(summaryview->mlist, msginfo);
2413
2414                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
2415                 summary_set_row_marks(summaryview, node);
2416         }
2417 }
2418
2419 static void summary_execute_delete(SummaryView *summaryview)
2420 {
2421         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2422         FolderItem *trash;
2423         GSList *cur;
2424
2425         trash = summaryview->folder_item->folder->trash;
2426         if (summaryview->folder_item->folder->type == F_MH) {
2427                 g_return_if_fail(trash != NULL);
2428         }
2429         if (summaryview->folder_item == trash) return;
2430
2431         /* search deleting messages and execute */
2432         gtk_ctree_pre_recursive
2433                 (ctree, NULL, summary_execute_delete_func, summaryview);
2434
2435         if (!summaryview->mlist) return;
2436
2437         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
2438
2439         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2440                 procmsg_msginfo_free((MsgInfo *)cur->data);
2441
2442         g_slist_free(summaryview->mlist);
2443         summaryview->mlist = NULL;
2444
2445         folder_item_scan(trash);
2446         folderview_update_item(trash, FALSE);
2447 }
2448
2449 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
2450                                         gpointer data)
2451 {
2452         SummaryView *summaryview = data;
2453         MsgInfo *msginfo;
2454
2455         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2456
2457         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
2458                 summaryview->mlist =
2459                         g_slist_append(summaryview->mlist, msginfo);
2460                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2461
2462                 if (msginfo->msgid && *msginfo->msgid &&
2463                     node == g_hash_table_lookup(summaryview->msgid_table,
2464                                                 msginfo->msgid))
2465                         g_hash_table_remove(summaryview->msgid_table,
2466                                             msginfo->msgid);
2467         }
2468 }
2469
2470 /* thread functions */
2471
2472 void summary_thread_build(SummaryView *summaryview)
2473 {
2474         debug_print(_("Building threads..."));
2475         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
2476         main_window_cursor_wait(summaryview->mainwin);
2477
2478         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2479
2480         gtk_ctree_pre_recursive_to_depth
2481                 (GTK_CTREE(summaryview->ctree), NULL, 1,
2482                  GTK_CTREE_FUNC(summary_thread_func),
2483                  summaryview->msgid_table);
2484
2485         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2486
2487         debug_print(_("done.\n"));
2488         STATUSBAR_POP(summaryview->mainwin);
2489         main_window_cursor_normal(summaryview->mainwin);
2490 }
2491
2492 void summary_unthread(SummaryView *summaryview)
2493 {
2494         GtkCTreeNode *node;
2495         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2496
2497         debug_print(_("Unthreading..."));
2498         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
2499         main_window_cursor_wait(summaryview->mainwin);
2500
2501         gtk_clist_freeze(GTK_CLIST(ctree));
2502
2503         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2504              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2505                 summary_unthread_func(ctree, node, NULL);
2506         }
2507
2508         gtk_clist_thaw(GTK_CLIST(ctree));
2509
2510         debug_print(_("done.\n"));
2511         STATUSBAR_POP(summaryview->mainwin);
2512         main_window_cursor_normal(summaryview->mainwin);
2513 }
2514
2515 static void summary_unthread_for_exec(SummaryView *summaryview)
2516 {
2517         GtkCTreeNode *node;
2518         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2519
2520         debug_print(_("Unthreading for execution..."));
2521
2522         gtk_clist_freeze(GTK_CLIST(ctree));
2523
2524         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2525              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2526                 summary_unthread_for_exec_func(ctree, node, NULL);
2527         }
2528
2529         gtk_clist_thaw(GTK_CLIST(ctree));
2530
2531         debug_print(_("done.\n"));
2532 }
2533
2534 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
2535                                 gpointer data)
2536 {
2537         MsgInfo *msginfo;
2538         GtkCTreeNode *parent;
2539         GHashTable *msgid_table = (GHashTable *)data;
2540
2541         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2542
2543         if (!msginfo || !msginfo->inreplyto) return;
2544
2545         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
2546
2547         if (parent && parent != node) {
2548                 gtk_ctree_move(ctree, node, parent, NULL);
2549                 gtk_ctree_expand(ctree, parent);
2550         }
2551 }
2552
2553 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
2554                                   gpointer data)
2555 {
2556         GtkCTreeNode *child;
2557         GtkCTreeNode *sibling;
2558
2559         child = GTK_CTREE_ROW(node)->children;
2560         sibling = GTK_CTREE_ROW(node)->sibling;
2561
2562         while (child != NULL) {
2563                 GtkCTreeNode *next_child;
2564
2565                 next_child = GTK_CTREE_ROW(child)->sibling;
2566                 gtk_ctree_move(ctree, child, NULL, sibling);
2567                 child = next_child;
2568         }
2569 }
2570
2571 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
2572                                            gpointer data)
2573 {
2574         MsgInfo *msginfo;
2575         GtkCTreeNode *top_parent;
2576         GtkCTreeNode *child;
2577         GtkCTreeNode *sibling;
2578
2579         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2580
2581         if (!msginfo ||
2582             (!MSG_IS_MOVE(msginfo->flags) &&
2583              !MSG_IS_DELETED(msginfo->flags)))
2584                 return;
2585         child = GTK_CTREE_ROW(node)->children;
2586         if (!child) return;
2587
2588         for (top_parent = node;
2589              GTK_CTREE_ROW(top_parent)->parent != NULL;
2590              top_parent = GTK_CTREE_ROW(top_parent)->parent)
2591                 ;
2592         sibling = GTK_CTREE_ROW(top_parent)->sibling;
2593
2594         while (child != NULL) {
2595                 GtkCTreeNode *next_child;
2596
2597                 next_child = GTK_CTREE_ROW(child)->sibling;
2598                 gtk_ctree_move(ctree, child, NULL, sibling);
2599                 child = next_child;
2600         }
2601 }
2602
2603 void summary_filter(SummaryView *summaryview)
2604 {
2605         if (!prefs_common.fltlist) return;
2606
2607         debug_print(_("filtering..."));
2608         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
2609         main_window_cursor_wait(summaryview->mainwin);
2610
2611         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2612
2613         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2614                                 GTK_CTREE_FUNC(summary_filter_func),
2615                                 summaryview);
2616
2617         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2618
2619         if (prefs_common.immediate_exec)
2620                 summary_execute(summaryview);
2621         else
2622                 summary_status_show(summaryview);
2623
2624         debug_print(_("done.\n"));
2625         STATUSBAR_POP(summaryview->mainwin);
2626         main_window_cursor_normal(summaryview->mainwin);
2627 }
2628
2629 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
2630                                 gpointer data)
2631 {
2632         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2633         SummaryView *summaryview = data;
2634         gchar *file;
2635         FolderItem *dest;
2636
2637         file = procmsg_get_message_file_path(msginfo);
2638         dest = filter_get_dest_folder(prefs_common.fltlist, file);
2639         g_free(file);
2640
2641         if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
2642             summaryview->folder_item != dest)
2643                 summary_move_row_to(summaryview, node, dest);
2644 }
2645
2646 /* callback functions */
2647
2648 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
2649                                    SummaryView *summaryview)
2650 {
2651         if (!event)
2652                 return;
2653
2654         if (!summaryview->msg_is_toggled_on && summaryview->selected)
2655                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2656         else
2657                 summary_toggle_view(summaryview);
2658 }
2659
2660 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2661                                    SummaryView *summaryview)
2662 {
2663         if (!event) return;
2664
2665         if (event->button == 3) {
2666                 /* right clicked */
2667                 summary_add_sender_to_cb(summaryview, 0, 0);    
2668                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
2669                                NULL, NULL, event->button, event->time);
2670         } else if (event->button == 2) {
2671                 summaryview->display_msg = TRUE;
2672         } else if (event->button == 1) {
2673                 if (!prefs_common.emulate_emacs &&
2674                     summaryview->msg_is_toggled_on)
2675                         summaryview->display_msg = TRUE;
2676         }
2677 }
2678
2679 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
2680                                     SummaryView *summaryview)
2681 {
2682 }
2683
2684 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
2685 {
2686         summary_key_pressed(summaryview->ctree, event, summaryview);
2687 }
2688
2689 #define BREAK_ON_MODIFIER_KEY() \
2690         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2691
2692 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
2693                                 SummaryView *summaryview)
2694 {
2695         GtkCTree *ctree = GTK_CTREE(widget);
2696         GtkCTreeNode *node;
2697         FolderItem *to_folder;
2698
2699         if (!event) return;
2700
2701         switch (event->keyval) {
2702         case GDK_g:             /* Go */
2703         case GDK_G:
2704                 BREAK_ON_MODIFIER_KEY();
2705
2706                 if (gtk_signal_n_emissions_by_name
2707                         (GTK_OBJECT(ctree), "key_press_event") > 0)
2708                         gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree),
2709                                                      "key_press_event");
2710
2711                 to_folder = foldersel_folder_sel(NULL);
2712                 if (to_folder) {
2713                         debug_print(_("Go to %s\n"), to_folder->path);
2714                         folderview_select(summaryview->folderview, to_folder);
2715                 }
2716                 return;
2717         case GDK_w:             /* Write new message */
2718                 BREAK_ON_MODIFIER_KEY();
2719                 if (summaryview->folder_item)
2720                         compose_new(summaryview->folder_item->folder->account);
2721                 else
2722                         compose_new(NULL);
2723                 return;
2724         case GDK_D:             /* Empty trash */
2725                 BREAK_ON_MODIFIER_KEY();
2726                 if (gtk_signal_n_emissions_by_name
2727                         (GTK_OBJECT(ctree), "key_press_event") > 0)
2728                         gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree),
2729                                                      "key_press_event");
2730                 main_window_empty_trash(summaryview->mainwin, TRUE);
2731                 return;
2732         case GDK_Q:             /* Quit */
2733                 BREAK_ON_MODIFIER_KEY();
2734
2735                 if (prefs_common.confirm_on_exit) {
2736                         if (alertpanel(_("Exit"), _("Exit this program?"),
2737                                        _("OK"), _("Cancel"), NULL)
2738                                        == G_ALERTDEFAULT) {
2739                                 manage_window_focus_in
2740                                         (summaryview->mainwin->window,
2741                                          NULL, NULL);
2742                                 app_will_exit(NULL, summaryview->mainwin);
2743                         }
2744                 }
2745                 return;
2746         case GDK_Left:          /* Move focus */
2747         case GDK_Escape:
2748                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2749                 return;
2750         default:
2751         }
2752
2753         if (!summaryview->selected) {
2754                 node = gtk_ctree_node_nth(ctree, 0);
2755                 if (node)
2756                         gtk_ctree_select(ctree, node);
2757                 else
2758                         return;
2759         }
2760
2761         switch (event->keyval) {
2762         case GDK_space:         /* Page down or go to the next */
2763                 if (summaryview->displayed != summaryview->selected) {
2764                         summary_display_msg(summaryview,
2765                                             summaryview->selected, FALSE);
2766                         break;
2767                 }
2768                 if (!textview_scroll_page(summaryview->messageview->textview,
2769                                           FALSE))
2770                         summary_select_next_unread(summaryview);
2771                 break;
2772         case GDK_n:             /* Next */
2773         case GDK_N:
2774                 BREAK_ON_MODIFIER_KEY();
2775                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2776                 break;
2777         case GDK_BackSpace:     /* Page up */
2778         case GDK_Delete:
2779                 textview_scroll_page(summaryview->messageview->textview, TRUE);
2780                 break;
2781         case GDK_p:             /* Prev */
2782         case GDK_P:
2783                 BREAK_ON_MODIFIER_KEY();
2784                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2785                 break;
2786         case GDK_v:             /* Toggle summary mode / message mode */
2787         case GDK_V:
2788                 BREAK_ON_MODIFIER_KEY();
2789
2790                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
2791                         summary_display_msg(summaryview,
2792                                             summaryview->selected, FALSE);
2793                 else
2794                         summary_toggle_view(summaryview);
2795                 break;
2796         case GDK_Return:        /* Scroll up/down one line */
2797                 if (summaryview->displayed != summaryview->selected) {
2798                         summary_display_msg(summaryview,
2799                                             summaryview->selected, FALSE);
2800                         break;
2801                 }
2802                 textview_scroll_one_line(summaryview->messageview->textview,
2803                                          (event->state & GDK_MOD1_MASK) != 0);
2804                 break;
2805         case GDK_asterisk:      /* Mark */
2806                 summary_mark(summaryview);
2807                 break;
2808         case GDK_exclam:        /* Mark as unread */
2809                 summary_mark_as_unread(summaryview);
2810                 break;
2811         case GDK_d:             /* Delete */
2812                 BREAK_ON_MODIFIER_KEY();
2813                 summary_delete(summaryview);
2814                 break;
2815         case GDK_u:             /* Unmark */
2816         case GDK_U:
2817                 BREAK_ON_MODIFIER_KEY();
2818                 summary_unmark(summaryview);
2819                 break;
2820         case GDK_o:             /* Move */
2821                 BREAK_ON_MODIFIER_KEY();
2822                 summary_move_to(summaryview);
2823                 break;
2824         case GDK_O:             /* Copy */
2825                 BREAK_ON_MODIFIER_KEY();
2826                 summary_copy_to(summaryview);
2827                 break;
2828         case GDK_x:             /* Execute */
2829         case GDK_X:
2830                 BREAK_ON_MODIFIER_KEY();
2831                 summary_execute(summaryview);
2832                 break;
2833         case GDK_a:             /* Reply to the message */
2834                 BREAK_ON_MODIFIER_KEY();
2835                 summary_reply_cb(summaryview,
2836                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
2837                 break;
2838         case GDK_A:             /* Reply to the message with quotation */
2839                 BREAK_ON_MODIFIER_KEY();
2840                 summary_reply_cb(summaryview,
2841                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
2842                 break;
2843         case GDK_f:             /* Forward the message */
2844                 BREAK_ON_MODIFIER_KEY();
2845                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
2846                 break;
2847         case GDK_F:
2848                 BREAK_ON_MODIFIER_KEY();
2849                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
2850                 break;
2851         case GDK_y:             /* Save the message */
2852                 BREAK_ON_MODIFIER_KEY();
2853                 summary_save_as(summaryview);
2854                 break;
2855         default:
2856         }
2857 }
2858
2859 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
2860 {
2861         if (summaryview->folder_item->stype == F_DRAFT)
2862                 summary_reedit(summaryview);
2863         else
2864                 summary_open_msg(summaryview);
2865
2866         summaryview->display_msg = FALSE;
2867 }
2868
2869 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
2870                              gint column, SummaryView *summaryview)
2871 {
2872         MsgInfo *msginfo;
2873
2874         summary_status_show(summaryview);
2875         summary_set_menu_sensitive(summaryview);
2876
2877         if (GTK_CLIST(ctree)->selection &&
2878              GTK_CLIST(ctree)->selection->next) {
2879                 summaryview->display_msg = FALSE;
2880                 return;
2881         }
2882
2883         summaryview->selected = row;
2884
2885         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2886
2887         switch (column) {
2888         case S_COL_MARK:
2889                 if (MSG_IS_MARKED(msginfo->flags)) {
2890                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
2891                         summary_set_row_marks(summaryview, row);
2892                 } else
2893                         summary_mark_row(summaryview, row);
2894                 break;
2895         case S_COL_UNREAD:
2896                 if (MSG_IS_UNREAD(msginfo->flags)) {
2897                         summary_mark_row_as_read(summaryview, row);
2898                         summary_status_show(summaryview);
2899                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
2900                          !MSG_IS_FORWARDED(msginfo->flags)) {
2901                         summary_mark_row_as_unread(summaryview, row);
2902                         summary_status_show(summaryview);
2903                 }
2904                 break;
2905         default:
2906         }
2907
2908         if (summaryview->display_msg)
2909                 summary_display_msg(summaryview, row, FALSE);
2910
2911         summaryview->display_msg = FALSE;
2912 }
2913
2914 static void summary_col_resized(GtkCList *clist, gint column, gint width,
2915                                 SummaryView *summaryview)
2916 {
2917         switch (column) {
2918         case S_COL_MARK:
2919                 prefs_common.summary_col_mark = width;
2920                 break;
2921         case S_COL_UNREAD:
2922                 prefs_common.summary_col_unread = width;
2923                 break;
2924         case S_COL_MIME:
2925                 prefs_common.summary_col_mime = width;
2926                 break;
2927         case S_COL_NUMBER:
2928                 prefs_common.summary_col_number = width;
2929                 break;
2930         case S_COL_SCORE:
2931                 prefs_common.summary_col_score = width;
2932                 break;
2933         case S_COL_SIZE:
2934                 prefs_common.summary_col_size = width;
2935                 break;
2936         case S_COL_DATE:
2937                 prefs_common.summary_col_date = width;
2938                 break;
2939         case S_COL_FROM:
2940                 prefs_common.summary_col_from = width;
2941                 break;
2942         case S_COL_SUBJECT:
2943                 prefs_common.summary_col_subject = width;
2944                 break;
2945         default:
2946         }
2947 }
2948
2949 static void summary_reply_cb(SummaryView *summaryview, guint action,
2950                              GtkWidget *widget)
2951 {
2952         MsgInfo *msginfo;
2953
2954         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2955                                               summaryview->selected);
2956         if (!msginfo) return;
2957
2958         switch ((ComposeReplyMode)action) {
2959         case COMPOSE_REPLY:
2960                 compose_reply(msginfo, prefs_common.reply_with_quote, FALSE);
2961                 break;
2962         case COMPOSE_REPLY_WITH_QUOTE:
2963                 compose_reply(msginfo, TRUE, FALSE);
2964                 break;
2965         case COMPOSE_REPLY_WITHOUT_QUOTE:
2966                 compose_reply(msginfo, FALSE, FALSE);
2967                 break;
2968         case COMPOSE_REPLY_TO_ALL:
2969                 compose_reply(msginfo, prefs_common.reply_with_quote, TRUE);
2970                 break;
2971         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
2972                 compose_reply(msginfo, TRUE, TRUE);
2973                 break;
2974         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
2975                 compose_reply(msginfo, FALSE, TRUE);
2976                 break;
2977         case COMPOSE_FORWARD:
2978                 compose_forward(msginfo, FALSE);
2979                 break;
2980         case COMPOSE_FORWARD_AS_ATTACH:
2981                 compose_forward(msginfo, TRUE);
2982                 break;
2983         default:
2984                 compose_reply(msginfo, prefs_common.reply_with_quote, FALSE);
2985         }
2986
2987         summary_set_marks_selected(summaryview);
2988 }
2989
2990 static void summary_show_all_header_cb(SummaryView *summaryview,
2991                                        guint action, GtkWidget *widget)
2992 {
2993         header_window_show_cb(summaryview->mainwin, action, widget);
2994 }
2995
2996 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
2997                                          guint                   action,
2998                                          GtkWidget              *widget_)
2999 {
3000         GtkWidget               *submenu;                                               
3001         GList                   *groups, *tmp;
3002         GtkMenuShell    *menushell;
3003         GtkWidget               *menu;
3004         GtkWidget               *menuitem;
3005         GList                   *child = menushell->children;
3006         gboolean                found = FALSE;
3007         MsgInfo                 *msginfo;
3008         gchar                   *from_address;
3009
3010         menushell = GTK_MENU_SHELL(summaryview->popupmenu);
3011         g_return_if_fail(GTK_MENU_SHELL(summaryview->popupmenu));
3012
3013         /* we're iterating each menu item searching for the one with 
3014          * a "contacts" object data. if not found add the menu,
3015          * else update it */
3016         for (child = g_list_first(menushell->children); child; child = g_list_next(child)) {
3017                 if (gtk_object_get_data(GTK_OBJECT(child->data), "contacts")) {
3018                         found = TRUE;
3019                         break;
3020                 }
3021         }
3022
3023         /* add item to default context menu if not present */
3024         if (!found) {
3025                 submenu = gtk_menu_item_new_with_label(_("Add sender to address book"));
3026                 gtk_object_set_data(GTK_OBJECT(submenu), "contacts", (gpointer)1);
3027                 gtk_menu_insert(GTK_MENU(summaryview->popupmenu), submenu, 12);
3028                 gtk_widget_show(submenu);
3029         }
3030         else {
3031                 submenu = (GtkWidget *) child->data;
3032         }
3033
3034         /* get the address info from the summary view */
3035         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3036                                                                                                    summaryview->selected);
3037         g_return_if_fail(msginfo != NULL);
3038
3039         from_address = g_strdup(msginfo->from);
3040         eliminate_address_comment(from_address);
3041         extract_address(from_address);
3042         log_message("adding %s %s\n", msginfo->fromname, from_address);
3043         addressbook_add_contact_by_menu(submenu, msginfo->fromname, from_address, NULL);
3044         g_free(from_address);
3045         
3046 }
3047
3048 static void summary_num_clicked(GtkWidget *button, SummaryView *summaryview)
3049 {
3050         summary_sort(summaryview, SORT_BY_NUMBER);
3051 }
3052
3053 static void summary_score_clicked(GtkWidget *button,
3054                                   SummaryView *summaryview)
3055 {
3056         summary_sort(summaryview, SORT_BY_SCORE);
3057 }
3058
3059 static void summary_size_clicked(GtkWidget *button, SummaryView *summaryview)
3060 {
3061         summary_sort(summaryview, SORT_BY_SIZE);
3062 }
3063
3064 static void summary_date_clicked(GtkWidget *button, SummaryView *summaryview)
3065 {
3066         summary_sort(summaryview, SORT_BY_DATE);
3067 }
3068
3069 static void summary_from_clicked(GtkWidget *button, SummaryView *summaryview)
3070 {
3071         summary_sort(summaryview, SORT_BY_FROM);
3072 }
3073
3074 static void summary_subject_clicked(GtkWidget *button,
3075                                     SummaryView *summaryview)
3076 {
3077         summary_sort(summaryview, SORT_BY_SUBJECT);
3078 }
3079
3080 void summary_change_display_item(SummaryView *summaryview)
3081 {
3082         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3083
3084         gtk_clist_set_column_visibility(clist, S_COL_MARK, prefs_common.show_mark);
3085         gtk_clist_set_column_visibility(clist, S_COL_UNREAD, prefs_common.show_unread);
3086         gtk_clist_set_column_visibility(clist, S_COL_MIME, prefs_common.show_mime);
3087         gtk_clist_set_column_visibility(clist, S_COL_NUMBER, prefs_common.show_number);
3088         gtk_clist_set_column_visibility(clist, S_COL_SCORE, prefs_common.show_score);
3089         gtk_clist_set_column_visibility(clist, S_COL_SIZE, prefs_common.show_size);
3090         gtk_clist_set_column_visibility(clist, S_COL_DATE, prefs_common.show_date);
3091         gtk_clist_set_column_visibility(clist, S_COL_FROM, prefs_common.show_from);
3092         gtk_clist_set_column_visibility(clist, S_COL_SUBJECT, prefs_common.show_subject);
3093 }
3094
3095 static void summary_start_drag (GtkWidget   *widget,
3096                                 gint         button,
3097                                 GdkEvent    *event,
3098                                 SummaryView *summaryview)
3099 {
3100         GtkTargetList *list;
3101         GdkDragContext *context;
3102
3103         g_return_if_fail(summaryview != NULL);
3104         g_return_if_fail(summaryview->folder_item != NULL);
3105         g_return_if_fail(summaryview->folder_item->folder != NULL);
3106         if (summaryview->folder_item->folder->type == F_NEWS ||
3107             summaryview->selected == NULL)
3108                 return;
3109
3110         list = gtk_target_list_new(summary_drag_types, 1);
3111
3112         context = gtk_drag_begin(widget, list,
3113                                  GDK_ACTION_MOVE, button, event);
3114         gtk_drag_set_icon_default(context);
3115 }
3116
3117 static void summary_drag_data_get(GtkWidget        *widget,
3118                                   GdkDragContext   *drag_context,
3119                                   GtkSelectionData *selection_data,
3120                                   guint             info,
3121                                   guint             time,
3122                                   SummaryView      *summaryview)
3123 {
3124         if (info == TARGET_MAIL_URI_LIST) {
3125                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3126                 GList *cur;
3127                 MsgInfo *msginfo;
3128                 gchar *mail_list = NULL, *tmp1, *tmp2;
3129
3130                 for (cur = GTK_CLIST(ctree)->selection;
3131                      cur != NULL; cur = cur->next) {
3132                         msginfo = gtk_ctree_node_get_row_data
3133                                 (ctree, GTK_CTREE_NODE(cur->data));
3134                         tmp2 = procmsg_get_message_file_path(msginfo);
3135                         if (!tmp2) continue;
3136                         tmp1 = g_strconcat("file:/", tmp2, NULL);
3137                         g_free(tmp2);
3138
3139                         if (!mail_list) {
3140                                 mail_list = tmp1;
3141                         } else {
3142                                 tmp2 = g_strconcat(mail_list, tmp1, NULL);
3143                                 g_free(mail_list);
3144                                 g_free(tmp1);
3145                                 mail_list = tmp2;
3146                         }
3147                 }
3148
3149                 if (mail_list != NULL) {
3150                         gtk_selection_data_set(selection_data,
3151                                                selection_data->target, 8,
3152                                                mail_list, strlen(mail_list));
3153                         g_free(mail_list);
3154                 } 
3155         } else if (info == TARGET_DUMMY) {
3156                 if (GTK_CLIST(summaryview->ctree)->selection)
3157                         gtk_selection_data_set(selection_data,
3158                                                selection_data->target, 8,
3159                                                "Dummy", 6);
3160         }
3161 }
3162
3163
3164 /* custom compare functions for sorting */
3165
3166 static gint summary_cmp_by_num(GtkCList *clist,
3167                                gconstpointer ptr1, gconstpointer ptr2)
3168 {
3169         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3170         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3171
3172         return msginfo1->msgnum - msginfo2->msgnum;
3173 }
3174
3175 static gint summary_cmp_by_size(GtkCList *clist,
3176                                 gconstpointer ptr1, gconstpointer ptr2)
3177 {
3178         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3179         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3180
3181         return msginfo1->size - msginfo2->size;
3182 }
3183
3184 static gint summary_cmp_by_date(GtkCList *clist,
3185                                gconstpointer ptr1, gconstpointer ptr2)
3186 {
3187         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3188         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3189
3190         return msginfo1->date_t - msginfo2->date_t;
3191 }
3192
3193 static gint summary_cmp_by_from(GtkCList *clist,
3194                                gconstpointer ptr1, gconstpointer ptr2)
3195 {
3196         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3197         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3198
3199         if (!msginfo1->fromname)
3200                 return (msginfo2->fromname != NULL);
3201         if (!msginfo2->fromname)
3202                 return -1;
3203
3204         return strcasecmp(msginfo1->fromname, msginfo2->fromname);
3205 }
3206
3207 static gint summary_cmp_by_subject(GtkCList *clist,
3208                                gconstpointer ptr1, gconstpointer ptr2)
3209 {
3210         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3211         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3212
3213         if (!msginfo1->subject)
3214                 return (msginfo2->subject != NULL);
3215         if (!msginfo2->subject)
3216                 return -1;
3217
3218         return strcasecmp(msginfo1->subject, msginfo2->subject);
3219 }
3220
3221 static gint summary_cmp_by_score(GtkCList *clist,
3222                                  gconstpointer ptr1, gconstpointer ptr2)
3223 {
3224         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3225         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3226         int diff;
3227
3228         /* if score are equal, sort by date */
3229
3230         diff = msginfo1->score - msginfo2->score;
3231         if (diff != 0)
3232                 return diff;
3233         else
3234                 return summary_cmp_by_date(clist, ptr1, ptr2);
3235 }