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