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