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