show recipient on 'from' for all accounts
[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_EVENTS_FLUSH();
1071                 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1072                 gtk_widget_grab_focus(GTK_WIDGET(ctree));
1073                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1074                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1075                 if (summaryview->msg_is_toggled_on) {
1076                         if (summaryview->displayed == node)
1077                                 summaryview->displayed = NULL;
1078                         summary_display_msg(summaryview, node, FALSE);
1079                 }
1080         }
1081 }
1082
1083 guint summary_get_current_msgnum(SummaryView *summaryview)
1084 {
1085         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1086         MsgInfo *msginfo;
1087
1088         if (!summaryview->selected)
1089                 return 0;
1090         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1091         return msginfo->msgnum;
1092 }
1093
1094 static GtkCTreeNode *summary_find_next_unread_msg(SummaryView *summaryview,
1095                                                   GtkCTreeNode *current_node)
1096 {
1097         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1098         GtkCTreeNode *node;
1099         MsgInfo *msginfo;
1100
1101         if (current_node)
1102                 node = current_node;
1103                 //node = GTK_CTREE_NODE_NEXT(current_node);
1104         else
1105                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1106
1107         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1108                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1109                 if (MSG_IS_UNREAD(msginfo->flags)) break;
1110         }
1111
1112         return node;
1113 }
1114
1115 static GtkCTreeNode *summary_find_next_marked_msg(SummaryView *summaryview,
1116                                                   GtkCTreeNode *current_node)
1117 {
1118         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1119         GtkCTreeNode *node;
1120         MsgInfo *msginfo;
1121
1122         if (current_node)
1123                 node = GTK_CTREE_NODE_NEXT(current_node);
1124         else
1125                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1126
1127         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1128                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1129                 if (MSG_IS_MARKED(msginfo->flags)) break;
1130         }
1131
1132         return node;
1133 }
1134
1135 static GtkCTreeNode *summary_find_prev_marked_msg(SummaryView *summaryview,
1136                                                   GtkCTreeNode *current_node)
1137 {
1138         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1139         GtkCTreeNode *node;
1140         MsgInfo *msginfo;
1141
1142         if (current_node)
1143                 node = GTK_CTREE_NODE_PREV(current_node);
1144         else
1145                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1146
1147         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1148                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1149                 if (MSG_IS_MARKED(msginfo->flags)) break;
1150         }
1151
1152         return node;
1153 }
1154
1155 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1156                                                 guint msgnum)
1157 {
1158         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1159         GtkCTreeNode *node;
1160         MsgInfo *msginfo;
1161
1162         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1163
1164         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1165                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1166                 if (msginfo->msgnum == msgnum) break;
1167         }
1168
1169         return node;
1170 }
1171
1172 #if 0
1173 static GtkCTreeNode *summary_find_prev_unread_msg(SummaryView *summaryview,
1174                                                   GtkCTreeNode *current_node)
1175 {
1176         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1177         GtkCTreeNode *node;
1178         MsgInfo *msginfo;
1179
1180         if (current_node)
1181                 node = current_node;
1182                 //node = GTK_CTREE_NODE_PREV(current_node);
1183         else
1184                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1185
1186         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1187                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1188                 if (MSG_IS_UNREAD(msginfo->flags)) break;
1189         }
1190
1191         return node;
1192 }
1193 #endif
1194
1195 static guint attract_hash_func(gconstpointer key)
1196 {
1197         gchar *str;
1198         gchar *p;
1199         guint h;
1200
1201         Xstrdup_a(str, (const gchar *)key, return 0);
1202         trim_subject(str);
1203
1204         p = str;
1205         h = *p;
1206
1207         if (h) {
1208                 for (p += 1; *p != '\0'; p++)
1209                         h = (h << 5) - h + *p;
1210         }
1211
1212         return h;
1213 }
1214
1215 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1216 {
1217         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1218 }
1219
1220 void summary_attract_by_subject(SummaryView *summaryview)
1221 {
1222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1223         GtkCList *clist = GTK_CLIST(ctree);
1224         GtkCTreeNode *src_node;
1225         GtkCTreeNode *dst_node, *sibling;
1226         GtkCTreeNode *tmp;
1227         MsgInfo *src_msginfo, *dst_msginfo;
1228         GHashTable *subject_table;
1229
1230         debug_print(_("Attracting messages by subject..."));
1231         STATUSBAR_PUSH(summaryview->mainwin,
1232                        _("Attracting messages by subject..."));
1233
1234         main_window_cursor_wait(summaryview->mainwin);
1235         gtk_clist_freeze(clist);
1236
1237         subject_table = g_hash_table_new(attract_hash_func,
1238                                          attract_compare_func);
1239
1240         for (src_node = GTK_CTREE_NODE(clist->row_list);
1241              src_node != NULL;
1242              src_node = tmp) {
1243                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1244                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1245                 if (!src_msginfo) continue;
1246                 if (!src_msginfo->subject) continue;
1247
1248                 /* find attracting node */
1249                 dst_node = g_hash_table_lookup(subject_table,
1250                                                src_msginfo->subject);
1251
1252                 if (dst_node) {
1253                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1254
1255                         /* if the time difference is more than 20 days,
1256                            don't attract */
1257                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1258                             > 60 * 60 * 24 * 20)
1259                                 continue;
1260
1261                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1262                         if (src_node != sibling)
1263                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1264                 }
1265
1266                 g_hash_table_insert(subject_table,
1267                                     src_msginfo->subject, src_node);
1268         }
1269
1270         g_hash_table_destroy(subject_table);
1271
1272         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1273
1274         gtk_clist_thaw(clist);
1275
1276         debug_print(_("done.\n"));
1277         STATUSBAR_POP(summaryview->mainwin);
1278
1279         main_window_cursor_normal(summaryview->mainwin);
1280 }
1281
1282 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1283                                       gpointer data)
1284 {
1285         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1286
1287         if (msginfo)
1288                 procmsg_msginfo_free(msginfo);
1289 }
1290
1291 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1292                                    gpointer data)
1293 {
1294         SummaryView *summaryview = data;
1295         MsgInfo *msginfo;
1296
1297         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1298
1299         if (MSG_IS_NEW(msginfo->flags))
1300                 summaryview->newmsgs++;
1301         if (MSG_IS_UNREAD(msginfo->flags))
1302                 summaryview->unread++;
1303         if (MSG_IS_DELETED(msginfo->flags))
1304                 summaryview->deleted++;
1305
1306         summaryview->messages++;
1307         summaryview->total_size += msginfo->size;
1308
1309         summary_set_row_marks(summaryview, node);
1310 }
1311
1312 static void summary_update_status(SummaryView *summaryview)
1313 {
1314         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1315         GtkCTreeNode *node;
1316         MsgInfo *msginfo;
1317
1318         summaryview->newmsgs = summaryview->unread =
1319         summaryview->messages = summaryview->total_size =
1320         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1321
1322         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1323              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1324                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1325
1326                 if (MSG_IS_NEW(msginfo->flags))
1327                         summaryview->newmsgs++;
1328                 if (MSG_IS_UNREAD(msginfo->flags))
1329                         summaryview->unread++;
1330                 if (MSG_IS_DELETED(msginfo->flags))
1331                         summaryview->deleted++;
1332                 if (MSG_IS_MOVE(msginfo->flags))
1333                         summaryview->moved++;
1334                 if (MSG_IS_COPY(msginfo->flags))
1335                         summaryview->copied++;
1336                 summaryview->messages++;
1337                 summaryview->total_size += msginfo->size;
1338         }
1339 }
1340
1341 static void summary_status_show(SummaryView *summaryview)
1342 {
1343         gchar *str;
1344         gchar *del, *mv, *cp;
1345         gchar *sel;
1346         gchar *spc;
1347         GList *rowlist, *cur;
1348         guint n_selected = 0;
1349         off_t sel_size = 0;
1350         MsgInfo *msginfo;
1351
1352         if (!summaryview->folder_item) {
1353                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1354                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1355                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1356                 return;
1357         }
1358
1359         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1360         for (cur = rowlist; cur != NULL; cur = cur->next) {
1361                 msginfo = gtk_ctree_node_get_row_data
1362                         (GTK_CTREE(summaryview->ctree),
1363                          GTK_CTREE_NODE(cur->data));
1364                 sel_size += msginfo->size;
1365                 n_selected++;
1366         }
1367
1368         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1369                       summaryview->folder_item &&
1370                       summaryview->folder_item->folder->type == F_NEWS
1371                       ? g_basename(summaryview->folder_item->path)
1372                       : summaryview->folder_item->path);
1373
1374         if (summaryview->deleted)
1375                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1376         else
1377                 del = g_strdup("");
1378         if (summaryview->moved)
1379                 mv = g_strdup_printf(_("%s%d moved"),
1380                                      summaryview->deleted ? _(", ") : "",
1381                                      summaryview->moved);
1382         else
1383                 mv = g_strdup("");
1384         if (summaryview->copied)
1385                 cp = g_strdup_printf(_("%s%d copied"),
1386                                      summaryview->deleted ||
1387                                      summaryview->moved ? _(", ") : "",
1388                                      summaryview->copied);
1389         else
1390                 cp = g_strdup("");
1391
1392         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1393                 spc = "    ";
1394         else
1395                 spc = "";
1396
1397         if (n_selected)
1398                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1399         else
1400                 sel = g_strdup("");
1401         str = g_strconcat(n_selected ? itos(n_selected) : "",
1402                           n_selected ? _(" item(s) selected") : "",
1403                           sel, spc, del, mv, cp, NULL);
1404         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1405         g_free(str);
1406         g_free(sel);
1407         g_free(del);
1408         g_free(mv);
1409         g_free(cp);
1410
1411         if (summaryview->folder_item &&
1412             summaryview->folder_item->folder->type == F_MH) {
1413                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1414                                       summaryview->newmsgs,
1415                                       summaryview->unread,
1416                                       summaryview->messages,
1417                                       to_human_readable(summaryview->total_size));
1418         } else {
1419                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1420                                       summaryview->newmsgs,
1421                                       summaryview->unread,
1422                                       summaryview->messages);
1423         }
1424         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1425         g_free(str);
1426
1427         folderview_update_msg_num(summaryview->folderview,
1428                                   summaryview->folderview->opened,
1429                                   summaryview->newmsgs,
1430                                   summaryview->unread,
1431                                   summaryview->messages);
1432 }
1433
1434 void summary_sort(SummaryView *summaryview, SummarySortType type)
1435 {
1436         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1437         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1438         GtkCListCompareFunc cmp_func;
1439
1440         if (!summaryview->folder_item)
1441                 return;
1442
1443         switch (type) {
1444         case SORT_BY_NUMBER:
1445                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1446                 break;
1447         case SORT_BY_SIZE:
1448                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1449                 break;
1450         case SORT_BY_DATE:
1451                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1452                 break;
1453         case SORT_BY_FROM:
1454                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1455                 break;
1456         case SORT_BY_SUBJECT:
1457                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1458                 break;
1459         case SORT_BY_SCORE:
1460                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1461                 break;
1462         default:
1463                 return;
1464         }
1465
1466         debug_print(_("Sorting summary..."));
1467         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1468
1469         main_window_cursor_wait(summaryview->mainwin);
1470
1471         gtk_clist_set_compare_func(clist, cmp_func);
1472
1473         /* toggle sort type if the same column is selected */
1474         if (summaryview->sort_mode == type)
1475                 summaryview->sort_type =
1476                         summaryview->sort_type == GTK_SORT_ASCENDING
1477                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1478         else
1479                 summaryview->sort_type = GTK_SORT_ASCENDING;
1480         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1481         summaryview->sort_mode = type;
1482
1483         gtk_ctree_sort_node(ctree, NULL);
1484
1485         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1486         //gtkut_ctree_set_focus_row(ctree, summaryview->selected);
1487
1488         prefs_folder_item_set_config(summaryview->folder_item,
1489                                      summaryview->sort_type,
1490                                      summaryview->sort_mode);
1491         prefs_folder_item_save_config(summaryview->folder_item);
1492
1493         debug_print(_("done.\n"));
1494         STATUSBAR_POP(summaryview->mainwin);
1495
1496         main_window_cursor_normal(summaryview->mainwin);
1497 }
1498
1499 static GtkCTreeNode * subject_table_lookup(GHashTable *subject_table,
1500                                            gchar * subject)
1501 {
1502         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1503                 return g_hash_table_lookup(subject_table, subject + 4);
1504         else
1505                 return g_hash_table_lookup(subject_table, subject);
1506 }
1507
1508 static void subject_table_insert(GHashTable *subject_table, gchar * subject,
1509                                  GtkCTreeNode * node)
1510 {
1511         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1512                 g_hash_table_insert(subject_table, subject + 4, node);
1513         else
1514                 g_hash_table_insert(subject_table, subject, node);
1515 }
1516
1517 static void summary_set_ctree_from_list(SummaryView *summaryview,
1518                                         GSList *mlist)
1519 {
1520         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1521         MsgInfo *msginfo;
1522         MsgInfo *parentinfo;
1523         MsgInfo *cur_msginfo;
1524         GtkCTreeNode *node, *parent;
1525         gchar *text[N_SUMMARY_COLS];
1526         GHashTable *msgid_table;
1527         GHashTable *subject_table;
1528         GSList * cur;
1529         GtkCTreeNode *cur_parent;
1530
1531         if (!mlist) return;
1532
1533         debug_print(_("\tSetting summary from message data..."));
1534         STATUSBAR_PUSH(summaryview->mainwin,
1535                        _("Setting summary from message data..."));
1536
1537         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1538         summaryview->msgid_table = msgid_table;
1539         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
1540         summaryview->subject_table = subject_table;
1541
1542         if (prefs_common.use_addr_book)
1543                 start_address_completion();
1544         
1545         main_window_set_thread_option(summaryview->mainwin);
1546
1547         for (cur = mlist ; cur != NULL; cur = cur->next) {
1548                 msginfo = (MsgInfo *)cur->data;
1549                 msginfo->threadscore = msginfo->score;
1550         }
1551
1552         if (global_scoring || summaryview->folder_item->prefs->scoring) {
1553                 summaryview->important_score = prefs_common.important_score;
1554                 if (summaryview->folder_item->prefs->important_score >
1555                     summaryview->important_score)
1556                         summaryview->important_score =
1557                                 summaryview->folder_item->prefs->important_score;
1558         }
1559         
1560         /*      if (prefs_common.enable_thread) { */
1561         if (summaryview->folder_item->prefs->enable_thread) {
1562                 for (; mlist != NULL; mlist = mlist->next) {
1563                         msginfo = (MsgInfo *)mlist->data;
1564                         parent = NULL;
1565
1566                         summary_set_header(text, msginfo);
1567
1568                         /* search parent node for threading */
1569                         if (msginfo->inreplyto && *msginfo->inreplyto) {
1570                                 parent = g_hash_table_lookup
1571                                         (msgid_table, msginfo->inreplyto);
1572                         }
1573                         if (parent == NULL && msginfo->subject) {
1574                                 parent = subject_table_lookup
1575                                         (subject_table, msginfo->subject);
1576                         }
1577
1578                         node = gtk_ctree_insert_node
1579                                 (ctree, parent, NULL, text, 2,
1580                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1581                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1582
1583                         summary_set_marks_func(ctree, node, summaryview);
1584
1585                         /* preserve previous node if the message is
1586                            duplicated */
1587                         if (msginfo->msgid && *msginfo->msgid &&
1588                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1589                             == NULL)
1590                                 g_hash_table_insert(msgid_table,
1591                                                     msginfo->msgid, node);
1592                         if (msginfo->subject &&
1593                             subject_table_lookup(subject_table,
1594                                                  msginfo->subject) == NULL) {
1595                                 subject_table_insert(subject_table,
1596                                                      msginfo->subject, node);
1597                         }
1598
1599                         cur_parent = parent;
1600                         cur_msginfo = msginfo;
1601                         while (cur_parent != NULL) {
1602                                 parentinfo = gtk_ctree_node_get_row_data(ctree, cur_parent);
1603
1604                                 if (!parentinfo)
1605                                         break;
1606                                 
1607                                 if (parentinfo->threadscore <
1608                                     cur_msginfo->threadscore) {
1609                                         gchar * s;
1610                                         parentinfo->threadscore =
1611                                                 cur_msginfo->threadscore;
1612 #if 0
1613                                         s = itos(parentinfo->threadscore);
1614                                         gtk_ctree_node_set_text(ctree, cur_parent, S_COL_SCORE, s);
1615 #endif
1616                                 }
1617                                 else break;
1618                                 
1619                                 cur_msginfo = parentinfo;
1620                                 if (cur_msginfo->inreplyto &&
1621                                     *cur_msginfo->inreplyto) {
1622                                         cur_parent = g_hash_table_lookup(msgid_table, cur_msginfo->inreplyto);
1623                                 }
1624                                 if (cur_parent == NULL && cur_msginfo->subject) {
1625                                         cur_parent = subject_table_lookup(subject_table, cur_msginfo->subject);
1626                                 }
1627                         }
1628                 }
1629
1630                 /* complete the thread */
1631                 summary_thread_build(summaryview);
1632         } else {
1633                 for (; mlist != NULL; mlist = mlist->next) {
1634                         msginfo = (MsgInfo *)mlist->data;
1635
1636                         summary_set_header(text, msginfo);
1637
1638                         node = gtk_ctree_insert_node
1639                                 (ctree, NULL, NULL, text, 2,
1640                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1641                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1642                         summary_set_marks_func(ctree, node, summaryview);
1643
1644                         if (msginfo->msgid && *msginfo->msgid &&
1645                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1646                             == NULL)
1647                                 g_hash_table_insert(msgid_table,
1648                                                     msginfo->msgid, node);
1649
1650                         if (msginfo->subject &&
1651                             subject_table_lookup(subject_table,
1652                                                  msginfo->subject) == NULL)
1653                                 subject_table_insert(subject_table,
1654                                                      msginfo->subject, node);
1655                 }
1656         }
1657
1658         if (prefs_common.enable_hscrollbar) {
1659                 gint optimal_width;
1660
1661                 optimal_width = gtk_clist_optimal_column_width
1662                         (GTK_CLIST(ctree), S_COL_SUBJECT);
1663                 gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
1664                                            optimal_width);
1665         }
1666
1667         if (prefs_common.use_addr_book)
1668                 end_address_completion();
1669
1670         debug_print(_("done.\n"));
1671         STATUSBAR_POP(summaryview->mainwin);
1672         if (debug_mode) {
1673                 debug_print("\tmsgid hash table size = %d\n",
1674                             g_hash_table_size(msgid_table));
1675                 debug_print("\tsubject hash table size = %d\n",
1676                             g_hash_table_size(subject_table));
1677         }
1678 }
1679
1680 struct wcachefp
1681 {
1682         FILE *cache_fp;
1683         FILE *mark_fp;
1684 };
1685
1686 gint summary_write_cache(SummaryView *summaryview)
1687 {
1688         struct wcachefp fps;
1689         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1690         gint ver = CACHE_VERSION;
1691         gchar *buf;
1692         gchar *cachefile, *markfile;
1693         GSList * cur;
1694
1695         if (!summaryview->folder_item || !summaryview->folder_item->path)
1696                 return -1;
1697
1698         if (summaryview->folder_item->folder->update_mark != NULL)
1699                 summaryview->folder_item->folder->update_mark(summaryview->folder_item->folder, summaryview->folder_item);
1700
1701         cachefile = folder_item_get_cache_file(summaryview->folder_item);
1702         g_return_val_if_fail(cachefile != NULL, -1);
1703         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
1704                 FILE_OP_ERROR(cachefile, "fopen");
1705                 g_free(cachefile);
1706                 return -1;
1707         }
1708         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
1709                 FILE_OP_ERROR(cachefile, "chmod");
1710         g_free(cachefile);
1711
1712         markfile = folder_item_get_mark_file(summaryview->folder_item);
1713         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
1714                 FILE_OP_ERROR(markfile, "fopen");
1715                 fclose(fps.cache_fp);
1716                 g_free(markfile);
1717                 return -1;
1718         }
1719         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
1720                 FILE_OP_ERROR(markfile, "chmod");
1721         g_free(markfile);
1722
1723         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
1724                               summaryview->folder_item->path);
1725         debug_print(buf);
1726         STATUSBAR_PUSH(summaryview->mainwin, buf);
1727         g_free(buf);
1728
1729         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
1730         ver = MARK_VERSION;
1731         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
1732
1733         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
1734
1735         for(cur = summaryview->killed_messages ; cur != NULL ;
1736             cur = g_slist_next(cur)) {
1737                 MsgInfo *msginfo = (MsgInfo *) cur->data;
1738
1739                 procmsg_write_cache(msginfo, fps.cache_fp);
1740                 procmsg_write_flags(msginfo, fps.mark_fp);
1741         }
1742
1743         fclose(fps.cache_fp);
1744         fclose(fps.mark_fp);
1745
1746         debug_print(_("done.\n"));
1747         STATUSBAR_POP(summaryview->mainwin);
1748
1749         return 0;
1750 }
1751
1752 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
1753                                      gpointer data)
1754 {
1755         struct wcachefp *fps = data;
1756         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1757
1758         if (msginfo == NULL) return;
1759
1760         procmsg_write_cache(msginfo, fps->cache_fp);
1761         procmsg_write_flags(msginfo, fps->mark_fp);
1762 }
1763
1764 static void summary_set_header(gchar *text[], MsgInfo *msginfo)
1765 {
1766         static gchar date_modified[80];
1767         static gchar *to = NULL;
1768         static gchar *from_name = NULL;
1769         static gchar col_number[11];
1770         static gchar col_score[11];
1771
1772         text[S_COL_MARK]   = NULL;
1773         text[S_COL_UNREAD] = NULL;
1774         text[S_COL_MIME]   = NULL;
1775         text[S_COL_NUMBER] = itos_buf(col_number, msginfo->msgnum);
1776         text[S_COL_SIZE]   = to_human_readable(msginfo->size);
1777 #if 0
1778         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->threadscore);
1779 #else
1780         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->score);
1781 #endif
1782
1783         if (msginfo->date_t) {
1784                 procheader_date_get_localtime(date_modified,
1785                                               sizeof(date_modified),
1786                                               msginfo->date_t);
1787                 text[S_COL_DATE] = date_modified;
1788         } else if (msginfo->date)
1789                 text[S_COL_DATE] = msginfo->date;
1790         else
1791                 text[S_COL_DATE] = _("(No Date)");
1792
1793         text[S_COL_FROM] = msginfo->fromname ? msginfo->fromname :
1794                 _("(No From)");
1795         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
1796             !MSG_IS_NEWS(msginfo->flags)) {
1797                 gchar *from;
1798
1799                 Xalloca(from, strlen(msginfo->from) + 1, return);
1800                 strcpy(from, msginfo->from);
1801                 extract_address(from);
1802                 if (account_find_mail_from_address(from)) {
1803                         g_free(to);
1804                         to = g_strconcat("-->", msginfo->to, NULL);
1805                         text[S_COL_FROM] = to;
1806                 }
1807         }
1808
1809         if ((text[S_COL_FROM] != to) && prefs_common.use_addr_book &&
1810             msginfo->from) {
1811                 gint count;
1812                 gchar *from;
1813   
1814                 Xalloca(from, strlen(msginfo->from) + 1, return);
1815                 strcpy(from, msginfo->from);
1816                 extract_address(from);
1817                 count = complete_address(from);
1818                 if (count > 1) {
1819                         g_free(from_name);
1820                         from = get_complete_address(1);
1821                         from_name = procheader_get_fromname(from);
1822                         g_free(from);
1823                         text[S_COL_FROM] = from_name;
1824                 }
1825         }
1826
1827         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
1828                 _("(No Subject)");
1829 }
1830
1831 #define CHANGE_FLAGS(msginfo) \
1832 { \
1833 if (msginfo->folder->folder->change_flags != NULL) \
1834 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
1835                                       msginfo->folder, \
1836                                       msginfo); \
1837 }
1838
1839 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
1840                                 gboolean new_window)
1841 {
1842         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1843         MsgInfo *msginfo;
1844         gchar *filename;
1845         static gboolean lock = FALSE;
1846
1847         if (!new_window && summaryview->displayed == row) return;
1848         g_return_if_fail(row != NULL);
1849
1850         if (lock) return;
1851         lock = TRUE;
1852
1853         STATUSBAR_POP(summaryview->mainwin);
1854
1855         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1856
1857         filename = procmsg_get_message_file(msginfo);
1858         if (!filename) {
1859                 lock = FALSE;
1860                 return;
1861         }
1862         g_free(filename);
1863
1864         if (MSG_IS_NEW(msginfo->flags))
1865                 summaryview->newmsgs--;
1866         if (MSG_IS_UNREAD(msginfo->flags))
1867                 summaryview->unread--;
1868         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
1869                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1870
1871                 CHANGE_FLAGS(msginfo);
1872
1873                 summary_set_row_marks(summaryview, row);
1874                 gtk_clist_thaw(GTK_CLIST(ctree));
1875                 summary_status_show(summaryview);
1876         }
1877
1878         if (new_window) {
1879                 MessageView *msgview;
1880
1881                 msgview = messageview_create_with_new_window();
1882                 messageview_show(msgview, msginfo);
1883         } else {
1884                 MessageView *msgview;
1885
1886                 msgview = summaryview->messageview;
1887
1888                 summaryview->displayed = row;
1889                 if (!summaryview->msg_is_toggled_on)
1890                         summary_toggle_view(summaryview);
1891                 messageview_show(msgview, msginfo);
1892                 if (msgview->type == MVIEW_TEXT ||
1893                     (msgview->type == MVIEW_MIME &&
1894                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
1895                         gtk_widget_grab_focus(summaryview->ctree);
1896                 GTK_EVENTS_FLUSH();
1897                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
1898         }
1899
1900         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
1901                 header_window_show(summaryview->headerwin, msginfo);
1902
1903         lock = FALSE;
1904 }
1905
1906 void summary_redisplay_msg(SummaryView *summaryview)
1907 {
1908         GtkCTreeNode *node;
1909
1910         if (summaryview->displayed) {
1911                 node = summaryview->displayed;
1912                 summaryview->displayed = NULL;
1913                 summary_display_msg(summaryview, node, FALSE);
1914         }
1915 }
1916
1917 void summary_open_msg(SummaryView *summaryview)
1918 {
1919         if (!summaryview->selected) return;
1920
1921         summary_display_msg(summaryview, summaryview->selected, TRUE);
1922 }
1923
1924 void summary_view_source(SummaryView * summaryview)
1925 {
1926         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1927         MsgInfo *msginfo;
1928         SourceWindow *srcwin;
1929
1930         if (!summaryview->selected) return;
1931
1932         srcwin = source_window_create();
1933         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1934         source_window_show_msg(srcwin, msginfo);
1935         source_window_show(srcwin);
1936 }
1937
1938 void summary_reedit(SummaryView *summaryview)
1939 {
1940         MsgInfo *msginfo;
1941
1942         if (!summaryview->selected) return;
1943         if (!summaryview->folder_item ||
1944             (summaryview->folder_item->stype != F_DRAFT &&
1945              summaryview->folder_item->stype != F_OUTBOX &&
1946              summaryview->folder_item->stype != F_QUEUE)) return;
1947
1948         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
1949                                               summaryview->selected);
1950         if (!msginfo) return;
1951
1952         compose_reedit(msginfo);
1953 }
1954
1955 void summary_step(SummaryView *summaryview, GtkScrollType type)
1956 {
1957         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1958
1959         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
1960                                 type, 0.0);
1961
1962         if (summaryview->msg_is_toggled_on)
1963                 summary_display_msg(summaryview, summaryview->selected, FALSE);
1964 }
1965
1966 static void summary_toggle_view(SummaryView *summaryview)
1967 {
1968         MainWindow *mainwin = summaryview->mainwin;
1969         union CompositeWin *cwin = &mainwin->win;
1970         GtkWidget *vpaned = NULL;
1971         GtkWidget *container = NULL;
1972
1973         switch (mainwin->type) {
1974         case SEPARATE_NONE:
1975                 vpaned = cwin->sep_none.vpaned;
1976                 container = cwin->sep_none.hpaned;
1977                 break;
1978         case SEPARATE_FOLDER:
1979                 vpaned = cwin->sep_folder.vpaned;
1980                 container = mainwin->vbox_body;
1981                 break;
1982         case SEPARATE_MESSAGE:
1983         case SEPARATE_BOTH:
1984                 return;
1985         }
1986
1987         if (vpaned->parent != NULL) {
1988                 summaryview->msg_is_toggled_on = FALSE;
1989                 summaryview->displayed = NULL;
1990                 gtk_widget_ref(vpaned);
1991                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
1992                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1993                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1994                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1995         } else {
1996                 summaryview->msg_is_toggled_on = TRUE;
1997                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1998                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1999                 gtk_widget_unref(vpaned);
2000                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2001                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2002         }
2003
2004         gtk_widget_grab_focus(summaryview->ctree);
2005 }
2006
2007 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2008 {
2009         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2010         MsgInfo *msginfo;
2011         MsgFlags flags;
2012
2013         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2014         if (!msginfo) return;
2015
2016         flags = msginfo->flags;
2017
2018         gtk_ctree_node_set_foreground(ctree, row, &summaryview->color_normal);
2019
2020         /* set new/unread column */
2021         if (MSG_IS_NEW(flags)) {
2022                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2023                                           newxpm, newxpmmask);
2024         } else if (MSG_IS_UNREAD(flags)) {
2025                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2026                                           unreadxpm, unreadxpmmask);
2027         } else if (MSG_IS_REPLIED(flags)) {
2028                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2029                                           repliedxpm, repliedxpmmask);
2030         } else if (MSG_IS_FORWARDED(flags)) {
2031                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2032                                           forwardedxpm, forwardedxpmmask);
2033         } else {
2034                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
2035         }
2036
2037         /* set mark column */
2038         if (MSG_IS_DELETED(flags)) {
2039                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2040                                           deletedxpm, deletedxpmmask);
2041                 gtk_ctree_node_set_foreground(ctree, row,
2042                                               &summaryview->color_dim);
2043         } else if (MSG_IS_MARKED(flags)) {
2044                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2045                                           markxpm, markxpmmask);
2046         } else if (MSG_IS_MOVE(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         } else if (MSG_IS_COPY(flags)) {
2051                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
2052                 gtk_ctree_node_set_foreground(ctree, row,
2053                                               &summaryview->color_marked);
2054         }
2055         else if ((global_scoring ||
2056                   summaryview->folder_item->prefs->scoring) &&
2057                  (msginfo->score >= summaryview->important_score) &&
2058                  ((msginfo->flags &
2059                    (MSG_MARKED | MSG_MOVE | MSG_COPY)) == 0)) {
2060                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2061                 gtk_ctree_node_set_foreground(ctree, row,
2062                                               &summaryview->color_important);
2063         } else {
2064                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
2065         }
2066
2067         if (MSG_IS_MIME(flags)) {
2068                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
2069                                           clipxpm, clipxpmmask);
2070         } else {
2071                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
2072         }
2073 }
2074
2075 void summary_set_marks_selected(SummaryView *summaryview)
2076 {
2077         summary_set_row_marks(summaryview, summaryview->selected);
2078 }
2079
2080 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2081 {
2082         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2083         MsgInfo *msginfo;
2084
2085         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2086         msginfo->to_folder = NULL;
2087         if (MSG_IS_DELETED(msginfo->flags))
2088                 summaryview->deleted--;
2089         if (MSG_IS_MOVE(msginfo->flags))
2090                 summaryview->moved--;
2091         if (MSG_IS_COPY(msginfo->flags))
2092                 summaryview->copied--;
2093         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
2094         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
2095
2096         CHANGE_FLAGS(msginfo);
2097
2098         summary_set_row_marks(summaryview, row);
2099         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
2100 }
2101
2102 void summary_mark(SummaryView *summaryview)
2103 {
2104         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2105         GList *cur;
2106
2107         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2108                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2109
2110         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2111         summary_status_show(summaryview);
2112 }
2113
2114 static void summary_mark_row_as_read(SummaryView *summaryview,
2115                                      GtkCTreeNode *row)
2116 {
2117         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2118         MsgInfo *msginfo;
2119
2120         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2121         if (MSG_IS_NEW(msginfo->flags))
2122                 summaryview->newmsgs--;
2123         if (MSG_IS_UNREAD(msginfo->flags))
2124                 summaryview->unread--;
2125         if (MSG_IS_NEW(msginfo->flags) ||
2126             MSG_IS_UNREAD(msginfo->flags)) {
2127                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2128
2129                 CHANGE_FLAGS(msginfo);
2130                 
2131                 summary_set_row_marks(summaryview, row);
2132                 debug_print(_("Message %d is marked as read\n"),
2133                             msginfo->msgnum);
2134         }
2135 }
2136
2137 void summary_mark_as_read(SummaryView *summaryview)
2138 {
2139         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2140         GList *cur;
2141
2142         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2143                 summary_mark_row_as_read(summaryview,
2144                                          GTK_CTREE_NODE(cur->data));
2145
2146         summary_status_show(summaryview);
2147 }
2148
2149 static void summary_mark_row_as_unread(SummaryView *summaryview,
2150                                        GtkCTreeNode *row)
2151 {
2152         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2153         MsgInfo *msginfo;
2154
2155         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2156         if (MSG_IS_DELETED(msginfo->flags)) {
2157                 msginfo->to_folder = NULL;
2158                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2159                 summaryview->deleted--;
2160         }
2161         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2162         if (!MSG_IS_UNREAD(msginfo->flags)) {
2163                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
2164
2165                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2166                                           unreadxpm, unreadxpmmask);
2167                 summaryview->unread++;
2168                 debug_print(_("Message %d is marked as unread\n"),
2169                             msginfo->msgnum);
2170         }
2171
2172         CHANGE_FLAGS(msginfo);
2173
2174         summary_set_row_marks(summaryview, row);
2175 }
2176
2177 void summary_mark_as_unread(SummaryView *summaryview)
2178 {
2179         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2180         GList *cur;
2181
2182         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2183                 summary_mark_row_as_unread(summaryview,
2184                                            GTK_CTREE_NODE(cur->data));
2185
2186         summary_status_show(summaryview);
2187 }
2188
2189 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2190 {
2191         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2192         MsgInfo *msginfo;
2193
2194         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2195
2196         if (MSG_IS_DELETED(msginfo->flags)) return;
2197
2198         msginfo->to_folder = NULL;
2199         if (MSG_IS_MOVE(msginfo->flags))
2200                 summaryview->moved--;
2201         if (MSG_IS_COPY(msginfo->flags))
2202                 summaryview->copied--;
2203         MSG_UNSET_FLAGS(msginfo->flags,
2204                         MSG_MARKED |
2205                         MSG_MOVE |
2206                         MSG_COPY);
2207         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
2208
2209         CHANGE_FLAGS(msginfo);
2210
2211         summaryview->deleted++;
2212
2213         if (!prefs_common.immediate_exec)
2214                 summary_set_row_marks(summaryview, row);
2215
2216         debug_print(_("Message %s/%d is set to delete\n"),
2217                     msginfo->folder->path, msginfo->msgnum);
2218 }
2219
2220 void summary_delete(SummaryView *summaryview)
2221 {
2222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2223         GList *cur;
2224
2225         if (!summaryview->folder_item ||
2226             summaryview->folder_item->folder->type == F_NEWS) return;
2227
2228         /* if current folder is trash, don't delete */
2229         if (summaryview->folder_item->stype == F_TRASH) {
2230                 alertpanel_notice(_("Current folder is Trash."));
2231                 return;
2232         }
2233
2234         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2235                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
2236
2237         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2238
2239         if (prefs_common.immediate_exec)
2240                 summary_execute(summaryview);
2241         else
2242                 summary_status_show(summaryview);
2243 }
2244
2245 void summary_delete_duplicated(SummaryView *summaryview)
2246 {
2247         if (!summaryview->folder_item ||
2248             summaryview->folder_item->folder->type == F_NEWS) return;
2249         if (summaryview->folder_item->stype == F_TRASH) return;
2250
2251         main_window_cursor_wait(summaryview->mainwin);
2252         debug_print(_("Deleting duplicated messages..."));
2253         STATUSBAR_PUSH(summaryview->mainwin,
2254                        _("Deleting duplicated messages..."));
2255
2256         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2257                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2258                                 summaryview);
2259
2260         if (prefs_common.immediate_exec)
2261                 summary_execute(summaryview);
2262         else
2263                 summary_status_show(summaryview);
2264
2265         debug_print(_("done.\n"));
2266         STATUSBAR_POP(summaryview->mainwin);
2267         main_window_cursor_normal(summaryview->mainwin);
2268 }
2269
2270 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2271                                            SummaryView *summaryview)
2272 {
2273         GtkCTreeNode *found;
2274         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2275
2276         if (!msginfo->msgid || !*msginfo->msgid) return;
2277
2278         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2279
2280         if (found && found != node)
2281                 summary_delete_row(summaryview, node);
2282 }
2283
2284 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2285 {
2286         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2287         MsgInfo *msginfo;
2288
2289         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2290         msginfo->to_folder = NULL;
2291         if (MSG_IS_DELETED(msginfo->flags))
2292                 summaryview->deleted--;
2293         if (MSG_IS_MOVE(msginfo->flags))
2294                 summaryview->moved--;
2295         if (MSG_IS_COPY(msginfo->flags))
2296                 summaryview->copied--;
2297         MSG_UNSET_FLAGS(msginfo->flags,
2298                         MSG_MARKED |
2299                         MSG_DELETED |
2300                         MSG_MOVE |
2301                         MSG_COPY);
2302
2303         CHANGE_FLAGS(msginfo);
2304
2305         summary_set_row_marks(summaryview, row);
2306
2307         debug_print(_("Message %s/%d is unmarked\n"),
2308                     msginfo->folder->path, msginfo->msgnum);
2309 }
2310
2311 void summary_unmark(SummaryView *summaryview)
2312 {
2313         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2314         GList *cur;
2315
2316         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2317              cur = cur->next)
2318                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2319
2320         summary_status_show(summaryview);
2321 }
2322
2323 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2324                                 FolderItem *to_folder)
2325 {
2326         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2327         MsgInfo *msginfo;
2328
2329         g_return_if_fail(to_folder != NULL);
2330
2331         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2332         msginfo->to_folder = to_folder;
2333         if (MSG_IS_DELETED(msginfo->flags))
2334                 summaryview->deleted--;
2335         MSG_UNSET_FLAGS(msginfo->flags,
2336                         MSG_MARKED | MSG_DELETED | MSG_COPY);
2337         if (!MSG_IS_MOVE(msginfo->flags)) {
2338                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
2339                 summaryview->moved++;
2340         }
2341
2342         if (!prefs_common.immediate_exec)
2343                 summary_set_row_marks(summaryview, row);
2344
2345         debug_print(_("Message %d is set to move to %s\n"),
2346                     msginfo->msgnum, to_folder->path);
2347 }
2348
2349 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2350 {
2351         GList *cur;
2352
2353         if (!to_folder) return;
2354         if (!summaryview->folder_item ||
2355             summaryview->folder_item->folder->type == F_NEWS) return;
2356         if (summaryview->folder_item == to_folder) {
2357                 alertpanel_notice(_("Destination is same as current folder."));
2358                 return;
2359         }
2360
2361         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2362              cur != NULL; cur = cur->next)
2363                 summary_move_row_to
2364                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2365
2366         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2367
2368         if (prefs_common.immediate_exec)
2369                 summary_execute(summaryview);
2370         else
2371                 summary_status_show(summaryview);
2372 }
2373
2374 void summary_move_to(SummaryView *summaryview)
2375 {
2376         FolderItem *to_folder;
2377
2378         if (!summaryview->folder_item ||
2379             summaryview->folder_item->folder->type == F_NEWS) return;
2380
2381         to_folder = foldersel_folder_sel(NULL);
2382         summary_move_selected_to(summaryview, to_folder);
2383 }
2384
2385 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2386                                 FolderItem *to_folder)
2387 {
2388         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2389         MsgInfo *msginfo;
2390
2391         g_return_if_fail(to_folder != NULL);
2392
2393         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2394         msginfo->to_folder = to_folder;
2395         if (MSG_IS_DELETED(msginfo->flags))
2396                 summaryview->deleted--;
2397         MSG_UNSET_FLAGS(msginfo->flags,
2398                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
2399         if (!MSG_IS_COPY(msginfo->flags)) {
2400                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
2401                 summaryview->copied++;
2402         }
2403
2404         if (!prefs_common.immediate_exec)
2405                 summary_set_row_marks(summaryview, row);
2406
2407         debug_print(_("Message %d is set to copy to %s\n"),
2408                     msginfo->msgnum, to_folder->path);
2409 }
2410
2411 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2412 {
2413         GList *cur;
2414
2415         if (!to_folder) return;
2416         if (!summaryview->folder_item ||
2417             summaryview->folder_item->folder->type == F_NEWS) return;
2418         if (summaryview->folder_item == to_folder) {
2419                 alertpanel_notice
2420                         (_("Destination to copy is same as current folder."));
2421                 return;
2422         }
2423
2424         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2425              cur != NULL; cur = cur->next)
2426                 summary_copy_row_to
2427                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2428
2429         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2430
2431         if (prefs_common.immediate_exec)
2432                 summary_execute(summaryview);
2433         else
2434                 summary_status_show(summaryview);
2435 }
2436
2437 void summary_copy_to(SummaryView *summaryview)
2438 {
2439         FolderItem *to_folder;
2440
2441         if (!summaryview->folder_item ||
2442             summaryview->folder_item->folder->type == F_NEWS) return;
2443
2444         to_folder = foldersel_folder_sel(NULL);
2445         summary_copy_selected_to(summaryview, to_folder);
2446 }
2447
2448 void summary_select_all(SummaryView *summaryview)
2449 {
2450         if (summaryview->messages >= 500) {
2451                 STATUSBAR_PUSH(summaryview->mainwin,
2452                                _("Selecting all messages..."));
2453                 main_window_cursor_wait(summaryview->mainwin);
2454         }
2455
2456         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2457
2458         if (summaryview->messages >= 500) {
2459                 STATUSBAR_POP(summaryview->mainwin);
2460                 main_window_cursor_normal(summaryview->mainwin);
2461         }
2462 }
2463
2464 void summary_unselect_all(SummaryView *summaryview)
2465 {
2466         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2467 }
2468
2469 void summary_save_as(SummaryView *summaryview)
2470 {
2471         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2472         MsgInfo *msginfo;
2473         gchar *src, *dest;
2474
2475         if (!summaryview->selected) return;
2476         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2477         if (!msginfo) return;
2478
2479         dest = filesel_select_file(_("Save as"), NULL);
2480         if (!dest) return;
2481         if (is_file_exist(dest)) {
2482                 AlertValue aval;
2483
2484                 aval = alertpanel(_("Overwrite"),
2485                                   _("Overwrite existing file?"),
2486                                   _("OK"), _("Cancel"), NULL);
2487                 if (G_ALERTDEFAULT != aval) return;
2488         }
2489
2490         src = procmsg_get_message_file(msginfo);
2491         copy_file(src, dest);
2492         g_free(src);
2493 }
2494
2495 void summary_print(SummaryView *summaryview)
2496 {
2497         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2498         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2499         MsgInfo *msginfo;
2500         GList *cur;
2501         gchar *cmdline;
2502         gchar *p;
2503
2504         if (clist->selection == NULL) return;
2505
2506         cmdline = input_dialog(_("Print"),
2507                                _("Enter the print command line:\n"
2508                                  "(`%s' will be replaced with file name)"),
2509                                prefs_common.print_cmd);
2510         if (!cmdline) return;
2511         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2512             strchr(p + 2, '%')) {
2513                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2514                                  cmdline);
2515                 g_free(cmdline);
2516                 return;
2517         }
2518
2519         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2520                 msginfo = gtk_ctree_node_get_row_data
2521                         (ctree, GTK_CTREE_NODE(cur->data));
2522                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2523         }
2524         
2525         g_free(cmdline);
2526 }
2527
2528 void summary_execute(SummaryView *summaryview)
2529 {
2530         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2531         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2532         GtkCTreeNode *node, *next;
2533
2534         if (!summaryview->folder_item ||
2535             summaryview->folder_item->folder->type == F_NEWS) return;
2536
2537         gtk_clist_freeze(clist);
2538
2539         if (summaryview->folder_item->prefs->enable_thread)
2540                 /*      if (prefs_common.enable_thread) */
2541                 summary_unthread_for_exec(summaryview);
2542
2543         summary_execute_move(summaryview);
2544         summary_execute_copy(summaryview);
2545         summary_execute_delete(summaryview);
2546
2547         node = GTK_CTREE_NODE(clist->row_list);
2548         while (node != NULL) {
2549                 next = GTK_CTREE_NODE_NEXT(node);
2550                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2551                         if (node == summaryview->displayed) {
2552                                 messageview_clear(summaryview->messageview);
2553                                 summaryview->displayed = NULL;
2554                         }
2555                         if (GTK_CTREE_ROW(node)->children != NULL)
2556                                 g_warning("summary_execute(): children != NULL\n");
2557                         else
2558                                 gtk_ctree_remove_node(ctree, node);
2559                 }
2560                 node = next;
2561         }
2562
2563         if (summaryview->folder_item->prefs->enable_thread)
2564                 /*      if (prefs_common.enable_thread) */
2565                 summary_thread_build(summaryview);
2566
2567         summaryview->selected = clist->selection ?
2568                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2569
2570         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2571                 menu_set_insensitive_all
2572                         (GTK_MENU_SHELL(summaryview->popupmenu));
2573                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2574         } else
2575                 gtk_widget_grab_focus(summaryview->ctree);
2576
2577         summary_update_status(summaryview);
2578         summary_status_show(summaryview);
2579
2580         summary_write_cache(summaryview);
2581
2582         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2583
2584         gtk_clist_thaw(clist);
2585 }
2586
2587 static void summary_execute_move(SummaryView *summaryview)
2588 {
2589         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2590         GSList *cur;
2591
2592         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2593
2594         /* search moving messages and execute */
2595         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2596                                 summaryview);
2597
2598         if (summaryview->mlist) {
2599                 procmsg_move_messages(summaryview->mlist);
2600
2601                 folder_item_scan_foreach(summaryview->folder_table);
2602                 folderview_update_item_foreach(summaryview->folder_table);
2603
2604                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2605                         procmsg_msginfo_free((MsgInfo *)cur->data);
2606                 g_slist_free(summaryview->mlist);
2607                 summaryview->mlist = NULL;
2608         }
2609
2610         g_hash_table_destroy(summaryview->folder_table);
2611         summaryview->folder_table = NULL;
2612 }
2613
2614 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2615                                       gpointer data)
2616 {
2617         SummaryView *summaryview = data;
2618         MsgInfo *msginfo;
2619
2620         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2621
2622         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2623                 g_hash_table_insert(summaryview->folder_table,
2624                                     msginfo->to_folder, GINT_TO_POINTER(1));
2625
2626                 summaryview->mlist =
2627                         g_slist_append(summaryview->mlist, msginfo);
2628                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2629
2630                 if (msginfo->msgid && *msginfo->msgid &&
2631                     node == g_hash_table_lookup(summaryview->msgid_table,
2632                                                 msginfo->msgid))
2633                         g_hash_table_remove(summaryview->msgid_table,
2634                                             msginfo->msgid);
2635         }
2636 }
2637
2638 static void summary_execute_copy(SummaryView *summaryview)
2639 {
2640         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2641
2642         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2643
2644         /* search copying messages and execute */
2645         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2646                                 summaryview);
2647
2648         if (summaryview->mlist) {
2649                 procmsg_copy_messages(summaryview->mlist);
2650
2651                 folder_item_scan_foreach(summaryview->folder_table);
2652                 folderview_update_item_foreach(summaryview->folder_table);
2653
2654                 g_slist_free(summaryview->mlist);
2655                 summaryview->mlist = NULL;
2656         }
2657
2658         g_hash_table_destroy(summaryview->folder_table);
2659         summaryview->folder_table = NULL;
2660 }
2661
2662 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
2663                                       gpointer data)
2664 {
2665         SummaryView *summaryview = data;
2666         MsgInfo *msginfo;
2667
2668         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2669
2670         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
2671                 g_hash_table_insert(summaryview->folder_table,
2672                                     msginfo->to_folder, GINT_TO_POINTER(1));
2673
2674                 summaryview->mlist =
2675                         g_slist_append(summaryview->mlist, msginfo);
2676
2677                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
2678                 
2679                 summary_set_row_marks(summaryview, node);
2680         }
2681 }
2682
2683 static void summary_execute_delete(SummaryView *summaryview)
2684 {
2685         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2686         FolderItem *trash;
2687         GSList *cur;
2688
2689         trash = summaryview->folder_item->folder->trash;
2690         if (summaryview->folder_item->folder->type == F_MH) {
2691                 g_return_if_fail(trash != NULL);
2692         }
2693         if (summaryview->folder_item == trash) return;
2694
2695         /* search deleting messages and execute */
2696         gtk_ctree_pre_recursive
2697                 (ctree, NULL, summary_execute_delete_func, summaryview);
2698
2699         if (!summaryview->mlist) return;
2700
2701         for(cur = summaryview->mlist ; cur != NULL ; cur = cur->next) {
2702                 MsgInfo * msginfo = cur->data;
2703                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2704         }
2705
2706         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
2707
2708         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2709                 procmsg_msginfo_free((MsgInfo *)cur->data);
2710
2711         g_slist_free(summaryview->mlist);
2712         summaryview->mlist = NULL;
2713
2714         folder_item_scan(trash);
2715         folderview_update_item(trash, FALSE);
2716 }
2717
2718 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
2719                                         gpointer data)
2720 {
2721         SummaryView *summaryview = data;
2722         MsgInfo *msginfo;
2723
2724         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2725
2726         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
2727                 summaryview->mlist =
2728                         g_slist_append(summaryview->mlist, msginfo);
2729                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2730
2731                 if (msginfo->msgid && *msginfo->msgid &&
2732                     node == g_hash_table_lookup(summaryview->msgid_table,
2733                                                 msginfo->msgid))
2734                         g_hash_table_remove(summaryview->msgid_table,
2735                                             msginfo->msgid);
2736         }
2737 }
2738
2739 /* thread functions */
2740
2741 void summary_thread_build(SummaryView *summaryview)
2742 {
2743         debug_print(_("Building threads..."));
2744         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
2745         main_window_cursor_wait(summaryview->mainwin);
2746
2747         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2748
2749         gtk_ctree_pre_recursive_to_depth
2750                 (GTK_CTREE(summaryview->ctree), NULL, 1,
2751                  GTK_CTREE_FUNC(summary_thread_func),
2752                  summaryview);
2753
2754         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2755
2756         debug_print(_("done.\n"));
2757         STATUSBAR_POP(summaryview->mainwin);
2758         main_window_cursor_normal(summaryview->mainwin);
2759 }
2760
2761 void summary_unthread(SummaryView *summaryview)
2762 {
2763         GtkCTreeNode *node;
2764         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2765
2766         debug_print(_("Unthreading..."));
2767         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
2768         main_window_cursor_wait(summaryview->mainwin);
2769
2770         gtk_clist_freeze(GTK_CLIST(ctree));
2771
2772         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2773              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2774                 summary_unthread_func(ctree, node, NULL);
2775         }
2776
2777         gtk_clist_thaw(GTK_CLIST(ctree));
2778
2779         debug_print(_("done.\n"));
2780         STATUSBAR_POP(summaryview->mainwin);
2781         main_window_cursor_normal(summaryview->mainwin);
2782 }
2783
2784 static void summary_unthread_for_exec(SummaryView *summaryview)
2785 {
2786         GtkCTreeNode *node;
2787         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2788
2789         debug_print(_("Unthreading for execution..."));
2790
2791         gtk_clist_freeze(GTK_CLIST(ctree));
2792
2793         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2794              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2795                 summary_unthread_for_exec_func(ctree, node, NULL);
2796         }
2797
2798         gtk_clist_thaw(GTK_CLIST(ctree));
2799
2800         debug_print(_("done.\n"));
2801 }
2802
2803 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
2804                                 gpointer data)
2805 {
2806         MsgInfo *msginfo;
2807         GtkCTreeNode *parent = NULL;
2808         
2809         SummaryView * summaryview = (SummaryView *) data;
2810         GHashTable *msgid_table = summaryview->msgid_table;
2811         GHashTable *subject_table = summaryview->subject_table;
2812
2813         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2814
2815         if (!msginfo) return;
2816
2817         if(msginfo->inreplyto) {
2818             parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
2819         }
2820         if (parent == NULL && msginfo->subject) {
2821                 parent = subject_table_lookup(subject_table, msginfo->subject);
2822         }
2823
2824         if (parent && parent != node) {
2825                 gtk_ctree_move(ctree, node, parent, NULL);
2826                 gtk_ctree_expand(ctree, parent);
2827         }
2828 }
2829
2830 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
2831                                   gpointer data)
2832 {
2833         GtkCTreeNode *child;
2834         GtkCTreeNode *sibling;
2835
2836         child = GTK_CTREE_ROW(node)->children;
2837         sibling = GTK_CTREE_ROW(node)->sibling;
2838
2839         while (child != NULL) {
2840                 GtkCTreeNode *next_child;
2841
2842                 next_child = GTK_CTREE_ROW(child)->sibling;
2843                 gtk_ctree_move(ctree, child, NULL, sibling);
2844                 child = next_child;
2845         }
2846 }
2847
2848 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
2849                                            gpointer data)
2850 {
2851         MsgInfo *msginfo;
2852         GtkCTreeNode *top_parent;
2853         GtkCTreeNode *child;
2854         GtkCTreeNode *sibling;
2855
2856         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2857
2858         if (!msginfo ||
2859             (!MSG_IS_MOVE(msginfo->flags) &&
2860              !MSG_IS_DELETED(msginfo->flags)))
2861                 return;
2862         child = GTK_CTREE_ROW(node)->children;
2863         if (!child) return;
2864
2865         for (top_parent = node;
2866              GTK_CTREE_ROW(top_parent)->parent != NULL;
2867              top_parent = GTK_CTREE_ROW(top_parent)->parent)
2868                 ;
2869         sibling = GTK_CTREE_ROW(top_parent)->sibling;
2870
2871         while (child != NULL) {
2872                 GtkCTreeNode *next_child;
2873
2874                 next_child = GTK_CTREE_ROW(child)->sibling;
2875                 gtk_ctree_move(ctree, child, NULL, sibling);
2876                 child = next_child;
2877         }
2878 }
2879
2880 void summary_filter(SummaryView *summaryview)
2881 {
2882         if (!prefs_common.fltlist) return;
2883
2884         debug_print(_("filtering..."));
2885         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
2886         main_window_cursor_wait(summaryview->mainwin);
2887
2888         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2889
2890         if (prefs_filtering == NULL) {
2891                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2892                                         GTK_CTREE_FUNC(summary_filter_func),
2893                                         summaryview);
2894                 
2895                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2896
2897                 if (prefs_common.immediate_exec)
2898                         summary_execute(summaryview);
2899                 else
2900                         summary_status_show(summaryview);
2901         }
2902         else {
2903                 summaryview->folder_table = g_hash_table_new(NULL, NULL);
2904
2905                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2906                                         GTK_CTREE_FUNC(summary_filter_func),
2907                                         summaryview);
2908
2909                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2910
2911                 folder_item_scan_foreach(summaryview->folder_table);
2912                 folderview_update_item_foreach(summaryview->folder_table);
2913
2914                 g_hash_table_destroy(summaryview->folder_table);
2915                 summaryview->folder_table = NULL;
2916
2917                 summary_show(summaryview, summaryview->folder_item, FALSE);
2918         }
2919
2920         debug_print(_("done.\n"));
2921         STATUSBAR_POP(summaryview->mainwin);
2922         main_window_cursor_normal(summaryview->mainwin);
2923 }
2924
2925 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
2926                                 gpointer data)
2927 {
2928         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2929         SummaryView *summaryview = data;
2930         gchar *file;
2931         FolderItem *dest;
2932
2933         if (prefs_filtering == NULL) {
2934                 /* old filtering */
2935                 file = procmsg_get_message_file_path(msginfo);
2936                 dest = filter_get_dest_folder(prefs_common.fltlist, file);
2937                 g_free(file);
2938
2939                 if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
2940                     summaryview->folder_item != dest)
2941                         summary_move_row_to(summaryview, node, dest);
2942         }
2943         else
2944                 filter_msginfo_move_or_delete(prefs_filtering, msginfo,
2945                                               summaryview->folder_table);
2946 }
2947
2948 /* callback functions */
2949
2950 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
2951                                    SummaryView *summaryview)
2952 {
2953         if (!event)
2954                 return;
2955
2956         if (!summaryview->msg_is_toggled_on && summaryview->selected)
2957                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2958         else
2959                 summary_toggle_view(summaryview);
2960 }
2961
2962 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2963                                    SummaryView *summaryview)
2964 {
2965         if (!event) return;
2966
2967         if (event->button == 3) {
2968                 /* right clicked */
2969                 summary_add_sender_to_cb(summaryview, 0, 0);    
2970                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
2971                                NULL, NULL, event->button, event->time);
2972         } else if (event->button == 2) {
2973                 summaryview->display_msg = TRUE;
2974         } else if (event->button == 1) {
2975                 if (!prefs_common.emulate_emacs &&
2976                     summaryview->msg_is_toggled_on)
2977                         summaryview->display_msg = TRUE;
2978         }
2979 }
2980
2981 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
2982                                     SummaryView *summaryview)
2983 {
2984 }
2985
2986 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
2987 {
2988         summary_key_pressed(summaryview->ctree, event, summaryview);
2989 }
2990
2991 #define BREAK_ON_MODIFIER_KEY() \
2992         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2993
2994 #define KEY_PRESS_EVENT_STOP() \
2995         if (gtk_signal_n_emissions_by_name \
2996                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
2997                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
2998                                              "key_press_event"); \
2999         }
3000
3001 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
3002                                 SummaryView *summaryview)
3003 {
3004         GtkCTree *ctree = GTK_CTREE(widget);
3005         GtkCTreeNode *node;
3006         FolderItem *to_folder;
3007
3008         if (!event) return;
3009
3010         switch (event->keyval) {
3011         case GDK_g:             /* Go */
3012         case GDK_G:
3013                 BREAK_ON_MODIFIER_KEY();
3014                 KEY_PRESS_EVENT_STOP();
3015                 to_folder = foldersel_folder_sel(NULL);
3016                 if (to_folder) {
3017                         debug_print(_("Go to %s\n"), to_folder->path);
3018                         folderview_select(summaryview->folderview, to_folder);
3019                 }
3020                 return;
3021         case GDK_w:             /* Write new message */
3022                 BREAK_ON_MODIFIER_KEY();
3023                 if (summaryview->folder_item) {
3024                         PrefsAccount *ac;
3025                         ac = summaryview->folder_item->folder->account;
3026                         if (ac && ac->protocol == A_NNTP)
3027                                 compose_new_with_recipient
3028                                         (ac, summaryview->folder_item->path);
3029                         else
3030                                 compose_new(ac);
3031                 } else
3032                         compose_new(NULL);
3033                 return;
3034         case GDK_D:             /* Empty trash */
3035                 BREAK_ON_MODIFIER_KEY();
3036                 KEY_PRESS_EVENT_STOP();
3037                 main_window_empty_trash(summaryview->mainwin, TRUE);
3038                 return;
3039         case GDK_Q:             /* Quit */
3040                 BREAK_ON_MODIFIER_KEY();
3041
3042                 if (prefs_common.confirm_on_exit) {
3043                         if (alertpanel(_("Exit"), _("Exit this program?"),
3044                                        _("OK"), _("Cancel"), NULL)
3045                                        == G_ALERTDEFAULT) {
3046                                 manage_window_focus_in
3047                                         (summaryview->mainwin->window,
3048                                          NULL, NULL);
3049                                 app_will_exit(NULL, summaryview->mainwin);
3050                         }
3051                 }
3052                 return;
3053         case GDK_Left:          /* Move focus */
3054         case GDK_Escape:
3055                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3056                 return;
3057         default:
3058         }
3059
3060         if (!summaryview->selected) {
3061                 node = gtk_ctree_node_nth(ctree, 0);
3062                 if (node)
3063                         gtk_ctree_select(ctree, node);
3064                 else
3065                         return;
3066         }
3067
3068         switch (event->keyval) {
3069         case GDK_space:         /* Page down or go to the next */
3070                 if (summaryview->displayed != summaryview->selected) {
3071                         summary_display_msg(summaryview,
3072                                             summaryview->selected, FALSE);
3073                         break;
3074                 }
3075                 if (!textview_scroll_page(summaryview->messageview->textview,
3076                                           FALSE))
3077                         summary_select_next_unread(summaryview);
3078                 break;
3079         case GDK_n:             /* Next */
3080         case GDK_N:
3081                 BREAK_ON_MODIFIER_KEY();
3082                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3083                 break;
3084         case GDK_BackSpace:     /* Page up */
3085         case GDK_Delete:
3086                 textview_scroll_page(summaryview->messageview->textview, TRUE);
3087                 break;
3088         case GDK_p:             /* Prev */
3089         case GDK_P:
3090                 BREAK_ON_MODIFIER_KEY();
3091                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
3092                 break;
3093         case GDK_v:             /* Toggle summary mode / message mode */
3094         case GDK_V:
3095                 BREAK_ON_MODIFIER_KEY();
3096
3097                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
3098                         summary_display_msg(summaryview,
3099                                             summaryview->selected, FALSE);
3100                 else
3101                         summary_toggle_view(summaryview);
3102                 break;
3103         case GDK_Return:        /* Scroll up/down one line */
3104                 if (summaryview->displayed != summaryview->selected) {
3105                         summary_display_msg(summaryview,
3106                                             summaryview->selected, FALSE);
3107                         break;
3108                 }
3109                 textview_scroll_one_line(summaryview->messageview->textview,
3110                                          (event->state & GDK_MOD1_MASK) != 0);
3111                 break;
3112         case GDK_asterisk:      /* Mark */
3113                 summary_mark(summaryview);
3114                 break;
3115         case GDK_exclam:        /* Mark as unread */
3116                 summary_mark_as_unread(summaryview);
3117                 break;
3118         case GDK_d:             /* Delete */
3119                 BREAK_ON_MODIFIER_KEY();
3120                 summary_delete(summaryview);
3121                 break;
3122         case GDK_u:             /* Unmark */
3123         case GDK_U:
3124                 BREAK_ON_MODIFIER_KEY();
3125                 summary_unmark(summaryview);
3126                 break;
3127         case GDK_o:             /* Move */
3128                 BREAK_ON_MODIFIER_KEY();
3129                 summary_move_to(summaryview);
3130                 break;
3131         case GDK_O:             /* Copy */
3132                 BREAK_ON_MODIFIER_KEY();
3133                 summary_copy_to(summaryview);
3134                 break;
3135         case GDK_x:             /* Execute */
3136         case GDK_X:
3137                 BREAK_ON_MODIFIER_KEY();
3138                 KEY_PRESS_EVENT_STOP();
3139                 summary_execute(summaryview);
3140                 break;
3141         case GDK_a:             /* Reply to the message */
3142                 BREAK_ON_MODIFIER_KEY();
3143                 summary_reply_cb(summaryview,
3144                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
3145                 break;
3146         case GDK_A:             /* Reply to the message with quotation */
3147                 BREAK_ON_MODIFIER_KEY();
3148                 summary_reply_cb(summaryview,
3149                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
3150                 break;
3151         case GDK_f:             /* Forward the message */
3152                 BREAK_ON_MODIFIER_KEY();
3153                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
3154                 break;
3155         case GDK_F:
3156                 BREAK_ON_MODIFIER_KEY();
3157                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
3158                 break;
3159         case GDK_y:             /* Save the message */
3160                 BREAK_ON_MODIFIER_KEY();
3161                 summary_save_as(summaryview);
3162                 break;
3163         default:
3164         }
3165 }
3166
3167 #undef BREAK_ON_MODIFIER_KEY
3168 #undef KEY_PRESS_EVENT_STOP
3169
3170 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
3171 {
3172         if (summaryview->folder_item->stype == F_DRAFT)
3173                 summary_reedit(summaryview);
3174         else
3175                 summary_open_msg(summaryview);
3176
3177         summaryview->display_msg = FALSE;
3178 }
3179
3180 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
3181                              gint column, SummaryView *summaryview)
3182 {
3183         MsgInfo *msginfo;
3184
3185         summary_status_show(summaryview);
3186         summary_set_menu_sensitive(summaryview);
3187
3188         if (GTK_CLIST(ctree)->selection &&
3189              GTK_CLIST(ctree)->selection->next) {
3190                 summaryview->display_msg = FALSE;
3191                 return;
3192         }
3193
3194         summaryview->selected = row;
3195
3196         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3197
3198         switch (column) {
3199         case S_COL_MARK:
3200                 if (MSG_IS_MARKED(msginfo->flags)) {
3201                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
3202
3203                         CHANGE_FLAGS(msginfo);
3204                         
3205                         summary_set_row_marks(summaryview, row);
3206                 } else
3207                         summary_mark_row(summaryview, row);
3208                 break;
3209         case S_COL_UNREAD:
3210                 if (MSG_IS_UNREAD(msginfo->flags)) {
3211                         summary_mark_row_as_read(summaryview, row);
3212                         summary_status_show(summaryview);
3213                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
3214                          !MSG_IS_FORWARDED(msginfo->flags)) {
3215                         summary_mark_row_as_unread(summaryview, row);
3216                         summary_status_show(summaryview);
3217                 }
3218                 break;
3219         default:
3220         }
3221
3222         if (summaryview->display_msg)
3223                 summary_display_msg(summaryview, row, FALSE);
3224
3225         summaryview->display_msg = FALSE;
3226 }
3227
3228 static void summary_col_resized(GtkCList *clist, gint column, gint width,
3229                                 SummaryView *summaryview)
3230 {
3231         switch (column) {
3232         case S_COL_MARK:
3233                 prefs_common.summary_col_mark = width;
3234                 break;
3235         case S_COL_UNREAD:
3236                 prefs_common.summary_col_unread = width;
3237                 break;
3238         case S_COL_MIME:
3239                 prefs_common.summary_col_mime = width;
3240                 break;
3241         case S_COL_NUMBER:
3242                 prefs_common.summary_col_number = width;
3243                 break;
3244         case S_COL_SCORE:
3245                 prefs_common.summary_col_score = width;
3246                 break;
3247         case S_COL_SIZE:
3248                 prefs_common.summary_col_size = width;
3249                 break;
3250         case S_COL_DATE:
3251                 prefs_common.summary_col_date = width;
3252                 break;
3253         case S_COL_FROM:
3254                 prefs_common.summary_col_from = width;
3255                 break;
3256         case S_COL_SUBJECT:
3257                 prefs_common.summary_col_subject = width;
3258                 break;
3259         default:
3260         }
3261 }
3262
3263 static void summary_reply_cb(SummaryView *summaryview, guint action,
3264                              GtkWidget *widget)
3265 {
3266         MsgInfo *msginfo;
3267
3268         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3269                                               summaryview->selected);
3270         if (!msginfo) return;
3271
3272         switch ((ComposeReplyMode)action) {
3273         case COMPOSE_REPLY:
3274                 compose_reply(msginfo, prefs_common.reply_with_quote,
3275                               FALSE, FALSE);
3276                 break;
3277         case COMPOSE_REPLY_WITH_QUOTE:
3278                 compose_reply(msginfo, TRUE, FALSE, FALSE);
3279                 break;
3280         case COMPOSE_REPLY_WITHOUT_QUOTE:
3281                 compose_reply(msginfo, FALSE, FALSE, FALSE);
3282                 break;
3283         case COMPOSE_REPLY_TO_SENDER:
3284                 compose_reply(msginfo, prefs_common.reply_with_quote,
3285                               FALSE, TRUE);
3286                 break;
3287         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
3288                 compose_followup_and_reply_to(msginfo,
3289                                               prefs_common.reply_with_quote,
3290                                               FALSE, TRUE);
3291                 break;
3292         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
3293                 compose_reply(msginfo, TRUE, FALSE, TRUE);
3294                 break;
3295         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
3296                 compose_reply(msginfo, FALSE, FALSE, TRUE);
3297                 break;
3298         case COMPOSE_REPLY_TO_ALL:
3299                 compose_reply(msginfo, prefs_common.reply_with_quote,
3300                               TRUE, FALSE);
3301                 break;
3302         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
3303                 compose_reply(msginfo, TRUE, TRUE, FALSE);
3304                 break;
3305         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
3306                 compose_reply(msginfo, FALSE, TRUE, FALSE);
3307                 break;
3308         case COMPOSE_FORWARD:
3309                 compose_forward(NULL, msginfo, FALSE);
3310                 break;
3311         case COMPOSE_FORWARD_AS_ATTACH:
3312                 compose_forward(NULL, msginfo, TRUE);
3313                 break;
3314         default:
3315                 g_warning("summary_reply_cb(): invalid action: %d\n", action);
3316         }
3317
3318         summary_set_marks_selected(summaryview);
3319 }
3320
3321 static void summary_show_all_header_cb(SummaryView *summaryview,
3322                                        guint action, GtkWidget *widget)
3323 {
3324         header_window_show_cb(summaryview->mainwin, action, widget);
3325 }
3326
3327 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
3328                                          guint                   action,
3329                                          GtkWidget              *widget_)
3330 {
3331         GtkWidget               *submenu;                                               
3332         GList                   *groups, *tmp;
3333         GtkMenuShell    *menushell;
3334         GtkWidget               *menu;
3335         GtkWidget               *menuitem;
3336         GList                   *child;
3337         gboolean                found = FALSE;
3338         MsgInfo                 *msginfo;
3339         gchar                   *from_address;
3340
3341         menushell = GTK_MENU_SHELL(summaryview->popupmenu);
3342         g_return_if_fail(menushell != NULL);
3343         child = menushell->children;
3344         g_return_if_fail(child);
3345
3346         /* we're iterating each menu item searching for the one with 
3347          * a "contacts" object data. if not found add the menu,
3348          * else update it */
3349         for (child = g_list_first(menushell->children); child; child = g_list_next(child)) {
3350                 if (gtk_object_get_data(GTK_OBJECT(child->data), "contacts")) {
3351                         found = TRUE;
3352                         break;
3353                 }
3354         }
3355
3356         /* add item to default context menu if not present */
3357         if (!found) {
3358                 submenu = gtk_menu_item_new_with_label(_("Add sender to address book"));
3359                 gtk_object_set_data(GTK_OBJECT(submenu), "contacts", (gpointer)1);
3360                 gtk_menu_insert(GTK_MENU(summaryview->popupmenu), submenu, 12);
3361                 gtk_widget_show(submenu);
3362         }
3363         else {
3364                 submenu = (GtkWidget *) child->data;
3365         }
3366
3367         /* get the address info from the summary view */
3368         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3369                                                                                                    summaryview->selected);
3370
3371         if (msginfo != NULL && msginfo->fromname != NULL && msginfo->from != NULL) {
3372                 gtk_widget_set_sensitive(GTK_WIDGET(submenu), TRUE);
3373                 from_address = g_strdup(msginfo->from);
3374                 eliminate_address_comment(from_address);
3375                 extract_address(from_address);
3376                 log_message("adding %s %s\n", msginfo->fromname, from_address);
3377                 addressbook_add_contact_by_menu(submenu, msginfo->fromname, from_address, NULL);
3378                 g_free(from_address);
3379         }               
3380         else {
3381                 gtk_widget_set_sensitive(GTK_WIDGET(submenu), FALSE);
3382         }
3383 }
3384
3385 static void summary_num_clicked(GtkWidget *button, SummaryView *summaryview)
3386 {
3387         summary_sort(summaryview, SORT_BY_NUMBER);
3388 }
3389
3390 static void summary_score_clicked(GtkWidget *button,
3391                                   SummaryView *summaryview)
3392 {
3393         summary_sort(summaryview, SORT_BY_SCORE);
3394 }
3395
3396 static void summary_size_clicked(GtkWidget *button, SummaryView *summaryview)
3397 {
3398         summary_sort(summaryview, SORT_BY_SIZE);
3399 }
3400
3401 static void summary_date_clicked(GtkWidget *button, SummaryView *summaryview)
3402 {
3403         summary_sort(summaryview, SORT_BY_DATE);
3404 }
3405
3406 static void summary_from_clicked(GtkWidget *button, SummaryView *summaryview)
3407 {
3408         summary_sort(summaryview, SORT_BY_FROM);
3409 }
3410
3411 static void summary_subject_clicked(GtkWidget *button,
3412                                     SummaryView *summaryview)
3413 {
3414         summary_sort(summaryview, SORT_BY_SUBJECT);
3415 }
3416
3417 void summary_change_display_item(SummaryView *summaryview)
3418 {
3419         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3420
3421         gtk_clist_set_column_visibility(clist, S_COL_MARK, prefs_common.show_mark);
3422         gtk_clist_set_column_visibility(clist, S_COL_UNREAD, prefs_common.show_unread);
3423         gtk_clist_set_column_visibility(clist, S_COL_MIME, prefs_common.show_mime);
3424         gtk_clist_set_column_visibility(clist, S_COL_NUMBER, prefs_common.show_number);
3425         gtk_clist_set_column_visibility(clist, S_COL_SCORE, prefs_common.show_score);
3426         gtk_clist_set_column_visibility(clist, S_COL_SIZE, prefs_common.show_size);
3427         gtk_clist_set_column_visibility(clist, S_COL_DATE, prefs_common.show_date);
3428         gtk_clist_set_column_visibility(clist, S_COL_FROM, prefs_common.show_from);
3429         gtk_clist_set_column_visibility(clist, S_COL_SUBJECT, prefs_common.show_subject);
3430 }
3431
3432 static void summary_start_drag (GtkWidget   *widget,
3433                                 gint         button,
3434                                 GdkEvent    *event,
3435                                 SummaryView *summaryview)
3436 {
3437         GtkTargetList *list;
3438         GdkDragContext *context;
3439
3440         g_return_if_fail(summaryview != NULL);
3441         g_return_if_fail(summaryview->folder_item != NULL);
3442         g_return_if_fail(summaryview->folder_item->folder != NULL);
3443         if (summaryview->folder_item->folder->type == F_NEWS ||
3444             summaryview->selected == NULL)
3445                 return;
3446
3447         list = gtk_target_list_new(summary_drag_types, 1);
3448
3449         context = gtk_drag_begin(widget, list,
3450                                  GDK_ACTION_MOVE, button, event);
3451         gtk_drag_set_icon_default(context);
3452 }
3453
3454 static void summary_drag_data_get(GtkWidget        *widget,
3455                                   GdkDragContext   *drag_context,
3456                                   GtkSelectionData *selection_data,
3457                                   guint             info,
3458                                   guint             time,
3459                                   SummaryView      *summaryview)
3460 {
3461         if (info == TARGET_MAIL_URI_LIST) {
3462                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3463                 GList *cur;
3464                 MsgInfo *msginfo;
3465                 gchar *mail_list = NULL, *tmp1, *tmp2;
3466
3467                 for (cur = GTK_CLIST(ctree)->selection;
3468                      cur != NULL; cur = cur->next) {
3469                         msginfo = gtk_ctree_node_get_row_data
3470                                 (ctree, GTK_CTREE_NODE(cur->data));
3471                         tmp2 = procmsg_get_message_file_path(msginfo);
3472                         if (!tmp2) continue;
3473                         tmp1 = g_strconcat("file:/", tmp2, NULL);
3474                         g_free(tmp2);
3475
3476                         if (!mail_list) {
3477                                 mail_list = tmp1;
3478                         } else {
3479                                 tmp2 = g_strconcat(mail_list, tmp1, NULL);
3480                                 g_free(mail_list);
3481                                 g_free(tmp1);
3482                                 mail_list = tmp2;
3483                         }
3484                 }
3485
3486                 if (mail_list != NULL) {
3487                         gtk_selection_data_set(selection_data,
3488                                                selection_data->target, 8,
3489                                                mail_list, strlen(mail_list));
3490                         g_free(mail_list);
3491                 } 
3492         } else if (info == TARGET_DUMMY) {
3493                 if (GTK_CLIST(summaryview->ctree)->selection)
3494                         gtk_selection_data_set(selection_data,
3495                                                selection_data->target, 8,
3496                                                "Dummy", 6);
3497         }
3498 }
3499
3500
3501 /* custom compare functions for sorting */
3502
3503 static gint summary_cmp_by_num(GtkCList *clist,
3504                                gconstpointer ptr1, gconstpointer ptr2)
3505 {
3506         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3507         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3508
3509         return msginfo1->msgnum - msginfo2->msgnum;
3510 }
3511
3512 static gint summary_cmp_by_size(GtkCList *clist,
3513                                 gconstpointer ptr1, gconstpointer ptr2)
3514 {
3515         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3516         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3517
3518         return msginfo1->size - msginfo2->size;
3519 }
3520
3521 static gint summary_cmp_by_date(GtkCList *clist,
3522                                gconstpointer ptr1, gconstpointer ptr2)
3523 {
3524         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3525         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3526
3527         return msginfo1->date_t - msginfo2->date_t;
3528 }
3529
3530 static gint summary_cmp_by_from(GtkCList *clist,
3531                                gconstpointer ptr1, gconstpointer ptr2)
3532 {
3533         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3534         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3535
3536         if (!msginfo1->fromname)
3537                 return (msginfo2->fromname != NULL);
3538         if (!msginfo2->fromname)
3539                 return -1;
3540
3541         return strcasecmp(msginfo1->fromname, msginfo2->fromname);
3542 }
3543
3544 static gint summary_cmp_by_subject(GtkCList *clist,
3545                                gconstpointer ptr1, gconstpointer ptr2)
3546 {
3547         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3548         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3549
3550         if (!msginfo1->subject)
3551                 return (msginfo2->subject != NULL);
3552         if (!msginfo2->subject)
3553                 return -1;
3554
3555         return strcasecmp(msginfo1->subject, msginfo2->subject);
3556 }
3557
3558 static gint summary_cmp_by_score(GtkCList *clist,
3559                                  gconstpointer ptr1, gconstpointer ptr2)
3560 {
3561         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3562         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3563         int diff;
3564
3565         /* if score are equal, sort by date */
3566
3567         diff = msginfo1->threadscore - msginfo2->threadscore;
3568         if (diff != 0)
3569                 return diff;
3570         else
3571                 return summary_cmp_by_date(clist, ptr1, ptr2);
3572 }