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