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