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