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