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