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