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