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