ea9e54577397179631d51c53a9f24963782372c8
[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         summaryview->quick_search_pixmap = pixmap;
691         
692         /* Init summaryview prefs */
693         summaryview->sort_key = SORT_BY_NONE;
694         summaryview->sort_type = SORT_ASCENDING;
695
696         /* Init summaryview extra data */
697         summaryview->simplify_subject_preg = NULL;
698
699         summary_clear_list(summaryview);
700         summary_set_column_titles(summaryview);
701         summary_colorlabel_menu_create(summaryview);
702         summary_set_menu_sensitive(summaryview);
703
704 }
705
706 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
707                                                  GtkCTreeNode *current_node)
708 {
709         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
710         GtkCTreeNode *node;
711         MsgInfo *msginfo;
712         gint best_score = MIN_SCORE;
713         GtkCTreeNode *best_node = NULL;
714
715         if (current_node)
716                 /*node = current_node;*/
717                 node = GTK_CTREE_NODE_NEXT(current_node);
718         else
719                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
720
721         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
722                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
723                 if (msginfo->score >= summaryview->important_score)
724                         break;
725                 if (msginfo->score > best_score) {
726                         best_score = msginfo->score;
727                         best_node = node;
728                 }
729         }
730
731         if (node != NULL)
732                 return node;
733         else
734                 return best_node;
735 }
736
737 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
738                                                  GtkCTreeNode *current_node)
739 {
740         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
741         GtkCTreeNode *node;
742         MsgInfo *msginfo;
743         gint best_score = MIN_SCORE;
744         GtkCTreeNode *best_node = NULL;
745
746         if (current_node)
747                 /*node = current_node;*/
748                 node = GTK_CTREE_NODE_PREV(current_node);
749         else
750                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
751
752         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
753                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
754                 if (msginfo->score >= summaryview->important_score)
755                         break;
756                 if (msginfo->score > best_score) {
757                         best_score = msginfo->score;
758                         best_node = node;
759                 }
760         }
761
762         if (node != NULL)
763                 return node;
764         else
765                 return best_node;
766 }
767
768 #define CURRENTLY_DISPLAYED(m) \
769 ( (m->msgnum == displayed_msgnum) \
770   && (!g_strcasecmp(m->folder->name,item->name)) )
771
772 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
773 {
774         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
775         GtkCTreeNode *node;
776         GSList *mlist = NULL;
777         gchar *buf;
778         gboolean is_refresh;
779         guint selected_msgnum = 0;
780         guint displayed_msgnum = 0;
781         GSList *cur;
782
783         if (summary_is_locked(summaryview)) return FALSE;
784
785         inc_lock();
786         summary_lock(summaryview);
787
788         if (item != summaryview->folder_item) {
789                 /* changing folder, reset search */
790                 gtk_entry_set_text(GTK_ENTRY(summaryview->search_string), "");
791         }
792         
793         STATUSBAR_POP(summaryview->mainwin);
794
795         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
796         if (is_refresh) {
797                 selected_msgnum = summary_get_msgnum(summaryview,
798                                                      summaryview->selected);
799                 displayed_msgnum = summary_get_msgnum(summaryview,
800                                                       summaryview->displayed);
801         }
802
803         /* process the marks if any */
804         if (summaryview->mainwin->lock_count == 0 &&
805             (summaryview->moved > 0 || summaryview->copied > 0)) {
806                 AlertValue val;
807
808                 val = alertpanel(_("Process mark"),
809                                  _("Some marks are left. Process it?"),
810                                  _("Yes"), _("No"), _("Cancel"));
811                 if (G_ALERTDEFAULT == val) {
812                         summary_unlock(summaryview);
813                         summary_execute(summaryview);
814                         summary_lock(summaryview);
815                 } else if (G_ALERTALTERNATE == val) {
816                         /* DO NOTHING */
817                 } else {
818                         summary_unlock(summaryview);
819                         inc_unlock();
820                         return FALSE;
821                 }
822                 folder_update_op_count();
823         }
824         
825         gtk_clist_freeze(GTK_CLIST(ctree));
826
827         summary_clear_list(summaryview);
828         summary_set_column_titles(summaryview);
829         if (!is_refresh)
830                 messageview_clear(summaryview->messageview);
831
832         buf = NULL;
833         if (!item || !item->path || !item->parent || item->no_select ||
834             (item->folder->type == F_MH &&
835              ((buf = folder_item_get_path(item)) == NULL ||
836               change_dir(buf) < 0))) {
837                 g_free(buf);
838                 debug_print("empty folder\n\n");
839                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
840                 if (is_refresh)
841                         messageview_clear(summaryview->messageview);
842                 summary_clear_all(summaryview);
843                 summaryview->folder_item = item;
844                 gtk_clist_thaw(GTK_CLIST(ctree));
845                 summary_unlock(summaryview);
846                 inc_unlock();
847                 return TRUE;
848         }
849         g_free(buf);
850
851         summaryview->folder_item = item;
852         item->opened = TRUE;
853
854         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
855
856         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
857         debug_print("%s\n", buf);
858         STATUSBAR_PUSH(summaryview->mainwin, buf);
859         g_free(buf);
860
861         main_window_cursor_wait(summaryview->mainwin);
862
863 /*
864         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
865
866         !!! NEW !!!
867         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
868         BY OTHER PROGRAMS TO THE FOLDER
869 */
870         mlist = folder_item_get_msg_list(item);
871 #if 0
872         summary_processing(summaryview, mlist);
873 #endif
874         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
875                 MsgInfo * msginfo = (MsgInfo *) cur->data;
876
877                 msginfo->score = score_message(global_scoring, msginfo);
878                 if (msginfo->score != MAX_SCORE &&
879                     msginfo->score != MIN_SCORE) {
880                         msginfo->score += score_message(item->prefs->scoring,
881                                                         msginfo);
882                 }
883         }
884
885         if (summaryview->folder_item->hide_read_msgs) {
886                 GSList *not_killed;
887                 
888                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
889                 not_killed = NULL;
890                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
891                         MsgInfo * msginfo = (MsgInfo *) cur->data;
892                         
893                         if ((MSG_IS_UNREAD(msginfo->flags)
894                              || MSG_IS_MARKED(msginfo->flags)
895                              || MSG_IS_LOCKED(msginfo->flags)
896                              || CURRENTLY_DISPLAYED(msginfo))
897                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
898                             not_killed = g_slist_append(not_killed, msginfo);
899                         else
900                                 procmsg_msginfo_free(msginfo);
901                 }
902                 g_slist_free(mlist);
903                 mlist = not_killed;
904         } else {
905                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
906         }
907
908         if (strlen(gtk_entry_get_text(GTK_ENTRY(summaryview->search_string))) > 0) {
909                 GSList *not_killed;
910                 gint search_type = GPOINTER_TO_INT(gtk_object_get_user_data(
911                                    GTK_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
912                                    GTK_MENU(summaryview->search_type))))));
913                 gchar *search_string = gtk_entry_get_text(GTK_ENTRY(summaryview->search_string));
914                 gchar *searched_header = NULL;
915                 
916                 not_killed = NULL;
917                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
918                         MsgInfo * msginfo = (MsgInfo *) cur->data;
919
920                         switch (search_type) {
921                         case S_SEARCH_SUBJECT:
922                                 searched_header = msginfo->subject;
923                                 break;
924                         case S_SEARCH_FROM:
925                                 searched_header = msginfo->from;
926                                 break;
927                         case S_SEARCH_TO:
928                                 searched_header = msginfo->to;
929                                 break;
930                         default:
931                                 debug_print("unknown search type (%d)\n", search_type);
932                                 break;
933                         }
934                         if (searched_header && strcasestr(searched_header, search_string) != NULL)
935                                 not_killed = g_slist_append(not_killed, msginfo);
936                         else
937                                 procmsg_msginfo_free(msginfo);
938                 }
939                 g_slist_free(mlist);
940                 mlist = not_killed;
941         }
942         
943         if ((global_scoring || item->prefs->scoring)) {
944                 GSList *not_killed;
945                 gint kill_score;
946
947                 not_killed = NULL;
948                 kill_score = prefs_common.kill_score;
949                 if (item->prefs->kill_score > kill_score)
950                         kill_score = item->prefs->kill_score;
951                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
952                         MsgInfo * msginfo = (MsgInfo *) cur->data;
953
954                         if (msginfo->score > kill_score)
955                                 not_killed = g_slist_append(not_killed, msginfo);
956                         else
957                                 procmsg_msginfo_free(msginfo);
958                 }
959                 g_slist_free(mlist);
960                 mlist = not_killed;
961         }
962
963         STATUSBAR_POP(summaryview->mainwin);
964
965         /* set ctree and hash table from the msginfo list
966            creating thread, and count the number of messages */
967         summary_set_ctree_from_list(summaryview, mlist);
968
969         g_slist_free(mlist);
970
971         if (summaryview->sort_key != SORT_BY_NONE)
972                 summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
973
974         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
975
976         gtk_clist_thaw(GTK_CLIST(ctree));
977
978         if (is_refresh) {
979                 summaryview->displayed =
980                         summary_find_msg_by_msgnum(summaryview,
981                                                    displayed_msgnum);
982                 if (!summaryview->displayed)
983                         messageview_clear(summaryview->messageview);
984                 summary_select_by_msgnum(summaryview, selected_msgnum);
985                 if (!summaryview->selected) {
986                         /* no selected message - select first unread
987                            message, but do not display it */
988                         node = summary_find_next_flagged_msg(summaryview, NULL,
989                                                              MSG_UNREAD, FALSE);
990                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
991                                 node = gtk_ctree_node_nth
992                                         (ctree,
993                                          item->sort_type == SORT_DESCENDING
994                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
995                         summary_select_node(summaryview, node, FALSE, TRUE);
996                 }
997         } else {
998                 /* select first unread message */
999                 if (summaryview->sort_key == SORT_BY_SCORE)
1000                         node = summary_find_next_important_score(summaryview,
1001                                                                  NULL);
1002                 else
1003                 node = summary_find_next_flagged_msg(summaryview, NULL,
1004                                                      MSG_UNREAD, FALSE);
1005                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1006                         node = gtk_ctree_node_nth
1007                                 (ctree,
1008                                  item->sort_type == SORT_DESCENDING
1009                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1010                 }
1011                 if (prefs_common.open_unread_on_enter) {
1012                         summary_unlock(summaryview);
1013                         summary_select_node(summaryview, node, TRUE, TRUE);
1014                         summary_lock(summaryview);
1015                 } else
1016                         summary_select_node(summaryview, node, FALSE, TRUE);
1017         }
1018
1019         summary_status_show(summaryview);
1020         summary_set_menu_sensitive(summaryview);
1021         toolbar_set_sensitive(summaryview->mainwin);
1022
1023         debug_print("\n");
1024         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1025
1026         main_window_cursor_normal(summaryview->mainwin);
1027         summary_unlock(summaryview);
1028         inc_unlock();
1029
1030         return TRUE;
1031 }
1032
1033 #undef CURRENTLY_DISPLAYED
1034
1035 void summary_clear_list(SummaryView *summaryview)
1036 {
1037         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1038         gint optimal_width;
1039
1040         gtk_clist_freeze(clist);
1041
1042         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1043                                 NULL, summary_free_msginfo_func, NULL);
1044
1045         if (summaryview->folder_item) {
1046                 summaryview->folder_item->opened = FALSE;
1047                 summaryview->folder_item = NULL;
1048         }
1049
1050         summaryview->display_msg = FALSE;
1051
1052         summaryview->selected  = NULL;
1053         summaryview->displayed = NULL;
1054         summaryview->newmsgs   = summaryview->unread     = 0;
1055         summaryview->messages  = summaryview->total_size = 0;
1056         summaryview->deleted   = summaryview->moved      = 0;
1057         summaryview->copied    = 0;
1058         if (summaryview->msgid_table) {
1059                 g_hash_table_destroy(summaryview->msgid_table);
1060                 summaryview->msgid_table = NULL;
1061         }
1062         if (summaryview->subject_table) {
1063                 g_hash_table_destroy(summaryview->subject_table);
1064                 summaryview->subject_table = NULL;
1065         }
1066         summaryview->mlist = NULL;
1067
1068         gtk_clist_clear(clist);
1069         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1070                 optimal_width = gtk_clist_optimal_column_width
1071                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1072                 gtk_clist_set_column_width
1073                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1074                          optimal_width);
1075         }
1076
1077         gtk_clist_thaw(clist);
1078 }
1079
1080 void summary_clear_all(SummaryView *summaryview)
1081 {
1082         summary_clear_list(summaryview);
1083         summary_set_menu_sensitive(summaryview);
1084         toolbar_set_sensitive(summaryview->mainwin);
1085         summary_status_show(summaryview);
1086 }
1087
1088 void summary_lock(SummaryView *summaryview)
1089 {
1090         summaryview->lock_count++;
1091 }
1092
1093 void summary_unlock(SummaryView *summaryview)
1094 {
1095         if (summaryview->lock_count)
1096                 summaryview->lock_count--;
1097 }
1098
1099 gboolean summary_is_locked(SummaryView *summaryview)
1100 {
1101         return summaryview->lock_count > 0;
1102 }
1103
1104 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1105 {
1106         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1107         SummarySelection selection;
1108
1109         if (!clist->row_list)
1110                 selection = SUMMARY_NONE;
1111         else if (!clist->selection)
1112                 selection = SUMMARY_SELECTED_NONE;
1113         else if (!clist->selection->next)
1114                 selection = SUMMARY_SELECTED_SINGLE;
1115         else
1116                 selection = SUMMARY_SELECTED_MULTIPLE;
1117
1118         return selection;
1119 }
1120
1121 static void summary_set_menu_sensitive(SummaryView *summaryview)
1122 {
1123         GtkItemFactory *ifactory = summaryview->popupfactory;
1124         SummarySelection selection;
1125         GtkWidget *menuitem;
1126         gboolean sens;
1127
1128         selection = summary_get_selection_type(summaryview);
1129         main_window_set_menu_sensitive(summaryview->mainwin);
1130
1131         if (selection == SUMMARY_NONE) {
1132                 GtkWidget *submenu;
1133
1134                 submenu = gtk_item_factory_get_widget
1135                         (summaryview->popupfactory, "/Mark");
1136                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1137                 menu_set_insensitive_all
1138                         (GTK_MENU_SHELL(summaryview->popupmenu));
1139                 return;
1140         }
1141
1142         if (summaryview->folder_item->folder->type != F_NEWS)
1143                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1144         else
1145                 menu_set_sensitive(ifactory, "/Move...", FALSE);
1146
1147 #if 0
1148         menu_set_sensitive(ifactory, "/Delete", TRUE);
1149 #endif
1150         menu_set_sensitive(ifactory, "/Copy...", TRUE);
1151         menu_set_sensitive(ifactory, "/Execute", TRUE);
1152
1153         menu_set_sensitive(ifactory, "/Mark", TRUE);
1154         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1155         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1156
1157         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1158         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1159         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1160         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1161         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1162
1163         menu_set_sensitive(ifactory, "/Color label", TRUE);
1164
1165         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1166         menu_set_sensitive(ifactory, "/Reply",                    sens);
1167         menu_set_sensitive(ifactory, "/Reply to",                 sens);
1168         menu_set_sensitive(ifactory, "/Reply to/all",             sens);
1169         menu_set_sensitive(ifactory, "/Reply to/sender",          sens);
1170         menu_set_sensitive(ifactory, "/Reply to/mailing list",    sens);
1171         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1172         menu_set_sensitive(ifactory, "/Redirect",                 sens);
1173
1174         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1175         menu_set_sensitive(ifactory, "/Create filter rule",         sens);
1176
1177         menu_set_sensitive(ifactory, "/View", sens);
1178         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1179         menu_set_sensitive(ifactory, "/View/Source", sens);
1180         menu_set_sensitive(ifactory, "/View/All header", sens);
1181         if (summaryview->folder_item->stype == F_OUTBOX ||
1182             summaryview->folder_item->stype == F_DRAFT  ||
1183             summaryview->folder_item->stype == F_QUEUE)
1184                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1185         else
1186                 menu_set_sensitive(ifactory, "/Re-edit", FALSE);
1187
1188         menu_set_sensitive(ifactory, "/Save as...", TRUE);
1189         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1190
1191         menu_set_sensitive(ifactory, "/Select all", TRUE);
1192         menu_set_sensitive(ifactory, "/Select thread", sens);
1193         if (summaryview->folder_item->folder->account)
1194                 sens = summaryview->folder_item->folder->account->protocol
1195                         == A_NNTP;
1196         else
1197                 sens = FALSE;
1198         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1199         menu_set_sensitive(ifactory, "/Cancel a news message", sens);
1200         menu_set_sensitive(ifactory, "/Delete", !sens);
1201
1202         summary_lock(summaryview);
1203         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1204         gtk_check_menu_item_set_active
1205                 (GTK_CHECK_MENU_ITEM(menuitem),
1206                  summaryview->messageview->textview->show_all_headers);
1207         summary_unlock(summaryview);
1208 }
1209
1210 void summary_select_prev_unread(SummaryView *summaryview)
1211 {
1212         GtkCTreeNode *node;
1213
1214         node = summary_find_prev_flagged_msg
1215                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1216
1217         if (!node) {
1218                 AlertValue val = 0;
1219
1220                 switch (prefs_common.next_unread_msg_dialog) {
1221                         case NEXTUNREADMSGDIALOG_ALWAYS:
1222                                 val = alertpanel(_("No more unread messages"),
1223                                                  _("No unread message found. "
1224                                                    "Search from the end?"),
1225                                                  _("Yes"), _("No"), NULL);
1226                                 break;
1227                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1228                                 val = G_ALERTDEFAULT;
1229                                 break;
1230                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1231                                 val = !G_ALERTDEFAULT;
1232                                 break;
1233                         default:
1234                                 debug_print(
1235                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1236                 }
1237                 if (val != G_ALERTDEFAULT) return;
1238                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1239                                                      MSG_UNREAD, FALSE);
1240         }
1241
1242         if (!node)
1243                 alertpanel_notice(_("No unread messages."));
1244         else
1245                 summary_select_node(summaryview, node, TRUE, FALSE);
1246 }
1247
1248 void summary_select_next_unread(SummaryView *summaryview)
1249 {
1250         GtkCTreeNode *node = summaryview->selected;
1251         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1252
1253         node = summary_find_next_flagged_msg
1254                 (summaryview, node, MSG_UNREAD, FALSE);
1255         
1256         if (node)
1257                 summary_select_node(summaryview, node, TRUE, FALSE);
1258         else {
1259                 node = summary_find_next_flagged_msg
1260                         (summaryview, NULL, MSG_UNREAD, FALSE);
1261                 if (node == NULL) {
1262                         AlertValue val = 0;
1263
1264                         switch (prefs_common.next_unread_msg_dialog) {
1265                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1266                                         val = alertpanel(_("No more unread messages"),
1267                                                          _("No unread message found. "
1268                                                            "Go to next folder?"),
1269                                                          _("Yes"), _("No"), NULL);
1270                                         break;
1271                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1272                                         val = G_ALERTDEFAULT;
1273                                         break;
1274                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1275                                         val = G_ALERTOTHER;
1276                                         break;
1277                                 default:
1278                                         debug_print(
1279                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1280                         }
1281
1282                         if (val == G_ALERTDEFAULT) {
1283                                 if (gtk_signal_n_emissions_by_name
1284                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1285                                                 gtk_signal_emit_stop_by_name
1286                                                         (GTK_OBJECT(ctree),
1287                                                          "key_press_event");
1288                                 folderview_select_next_unread(summaryview->folderview);
1289                                 return;
1290                         } 
1291                         else
1292                                 return;
1293                 } else
1294                         summary_select_node(summaryview, node, TRUE, FALSE);
1295
1296         }
1297 }
1298
1299 void summary_select_prev_new(SummaryView *summaryview)
1300 {
1301         GtkCTreeNode *node;
1302
1303         node = summary_find_prev_flagged_msg
1304                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1305
1306         if (!node) {
1307                 AlertValue val;
1308
1309                 val = alertpanel(_("No more new messages"),
1310                                  _("No new message found. "
1311                                    "Search from the end?"),
1312                                  _("Yes"), _("No"), NULL);
1313                 if (val != G_ALERTDEFAULT) return;
1314                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1315                                                      MSG_NEW, FALSE);
1316         }
1317
1318         if (!node)
1319                 alertpanel_notice(_("No new messages."));
1320         else
1321                 summary_select_node(summaryview, node, TRUE, FALSE);
1322 }
1323
1324 void summary_select_next_new(SummaryView *summaryview)
1325 {
1326         GtkCTreeNode *node = summaryview->selected;
1327         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1328
1329         while ((node = summary_find_next_flagged_msg
1330                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1331                 AlertValue val;
1332
1333                 val = alertpanel(_("No more new messages"),
1334                                  _("No new message found. "
1335                                    "Go to next folder?"),
1336                                  _("Yes"), _("Search again"), _("No"));
1337                 if (val == G_ALERTDEFAULT) {
1338                         if (gtk_signal_n_emissions_by_name
1339                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1340                                         gtk_signal_emit_stop_by_name
1341                                                 (GTK_OBJECT(ctree),
1342                                                  "key_press_event");
1343                         folderview_select_next_unread(summaryview->folderview);
1344                         return;
1345                 } else if (val == G_ALERTALTERNATE)
1346                         node = NULL;
1347                 else
1348                         return;
1349         }
1350
1351         if (node)
1352                 summary_select_node(summaryview, node, TRUE, FALSE);
1353 }
1354
1355 void summary_select_prev_marked(SummaryView *summaryview)
1356 {
1357         GtkCTreeNode *node;
1358
1359         node = summary_find_prev_flagged_msg
1360                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1361
1362         if (!node) {
1363                 AlertValue val;
1364
1365                 val = alertpanel(_("No more marked messages"),
1366                                  _("No marked message found. "
1367                                    "Search from the end?"),
1368                                  _("Yes"), _("No"), NULL);
1369                 if (val != G_ALERTDEFAULT) return;
1370                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1371                                                      MSG_MARKED, TRUE);
1372         }
1373
1374         if (!node)
1375                 alertpanel_notice(_("No marked messages."));
1376         else
1377                 summary_select_node(summaryview, node, TRUE, FALSE);
1378 }
1379
1380 void summary_select_next_marked(SummaryView *summaryview)
1381 {
1382         GtkCTreeNode *node;
1383
1384         node = summary_find_next_flagged_msg
1385                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1386
1387         if (!node) {
1388                 AlertValue val;
1389
1390                 val = alertpanel(_("No more marked messages"),
1391                                  _("No marked message found. "
1392                                    "Search from the beginning?"),
1393                                  _("Yes"), _("No"), NULL);
1394                 if (val != G_ALERTDEFAULT) return;
1395                 node = summary_find_next_flagged_msg(summaryview, NULL,
1396                                                      MSG_MARKED, TRUE);
1397         }
1398
1399         if (!node)
1400                 alertpanel_notice(_("No marked messages."));
1401         else
1402                 summary_select_node(summaryview, node, TRUE, FALSE);
1403 }
1404
1405 void summary_select_prev_labeled(SummaryView *summaryview)
1406 {
1407         GtkCTreeNode *node;
1408
1409         node = summary_find_prev_flagged_msg
1410                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1411
1412         if (!node) {
1413                 AlertValue val;
1414
1415                 val = alertpanel(_("No more labeled messages"),
1416                                  _("No labeled message found. "
1417                                    "Search from the end?"),
1418                                  _("Yes"), _("No"), NULL);
1419                 if (val != G_ALERTDEFAULT) return;
1420                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1421                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1422         }
1423
1424         if (!node)
1425                 alertpanel_notice(_("No labeled messages."));
1426         else
1427                 summary_select_node(summaryview, node, TRUE, FALSE);
1428 }
1429
1430 void summary_select_next_labeled(SummaryView *summaryview)
1431 {
1432         GtkCTreeNode *node;
1433
1434         node = summary_find_next_flagged_msg
1435                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1436
1437         if (!node) {
1438                 AlertValue val;
1439
1440                 val = alertpanel(_("No more labeled messages"),
1441                                  _("No labeled message found. "
1442                                    "Search from the beginning?"),
1443                                  _("Yes"), _("No"), NULL);
1444                 if (val != G_ALERTDEFAULT) return;
1445                 node = summary_find_next_flagged_msg(summaryview, NULL,
1446                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1447         }
1448
1449         if (!node)
1450                 alertpanel_notice(_("No labeled messages."));
1451         else
1452                 summary_select_node(summaryview, node, TRUE, FALSE);
1453 }
1454
1455 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1456 {
1457         GtkCTreeNode *node;
1458
1459         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1460         summary_select_node(summaryview, node, FALSE, TRUE);
1461 }
1462
1463 /**
1464  * summary_select_node:
1465  * @summaryview: Summary view.
1466  * @node: Summary tree node.
1467  * @display_msg: TRUE to display the selected message.
1468  * @do_refresh: TRUE to refresh the widget.
1469  *
1470  * Select @node (bringing it into view by scrolling and expanding its
1471  * thread, if necessary) and unselect all others.  If @display_msg is
1472  * TRUE, display the corresponding message in the message view.
1473  * If @do_refresh is TRUE, the widget is refreshed.
1474  **/
1475 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1476                          gboolean display_msg, gboolean do_refresh)
1477 {
1478         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1479
1480         if (node) {
1481                 gtkut_ctree_expand_parent_all(ctree, node);
1482                 if (do_refresh) {
1483                         GTK_EVENTS_FLUSH();
1484                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1485                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1486                 }
1487                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1488                 if (display_msg && summaryview->displayed == node)
1489                         summaryview->displayed = NULL;
1490                 summaryview->display_msg = display_msg;
1491                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1492         }
1493 }
1494
1495 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1496 {
1497         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1498         MsgInfo *msginfo;
1499
1500         if (!node)
1501                 return 0;
1502         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1503         return msginfo->msgnum;
1504 }
1505
1506 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1507                                            GtkCTreeNode *current_node)
1508 {
1509         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1510         GtkCTreeNode *node;
1511         MsgInfo *msginfo;
1512
1513         if (current_node)
1514                 node = current_node;
1515         else
1516                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1517
1518         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1519                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1520                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1521         }
1522
1523         return node;
1524 }
1525
1526 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1527                                            GtkCTreeNode *current_node)
1528 {
1529         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1530         GtkCTreeNode *node;
1531         MsgInfo *msginfo;
1532
1533         if (current_node)
1534                 node = current_node;
1535         else
1536                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1537
1538         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1539                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1540                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1541         }
1542
1543         return node;
1544 }
1545
1546 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1547                                                    GtkCTreeNode *current_node,
1548                                                    MsgPermFlags flags,
1549                                                    gboolean start_from_prev)
1550 {
1551         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1552         GtkCTreeNode *node;
1553         MsgInfo *msginfo;
1554
1555         if (current_node) {
1556                 if (start_from_prev)
1557                         node = GTK_CTREE_NODE_PREV(current_node);
1558                 else
1559                         node = current_node;
1560         } else
1561                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1562
1563         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1564                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1565                 if ((msginfo->flags.perm_flags & flags) != 0) break;
1566         }
1567
1568         return node;
1569 }
1570
1571 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1572                                                    GtkCTreeNode *current_node,
1573                                                    MsgPermFlags flags,
1574                                                    gboolean start_from_next)
1575 {
1576         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1577         GtkCTreeNode *node;
1578         MsgInfo *msginfo;
1579
1580         if (current_node) {
1581                 if (start_from_next)
1582                         node = gtkut_ctree_node_next(ctree, current_node);
1583                 else
1584                         node = current_node;
1585         } else
1586                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1587
1588         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1589                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1590                 /* Find msg with matching flags but ignore messages with
1591                    ignore flags, if searching for new or unread messages */
1592                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1593                         ((msginfo->flags.perm_flags & flags) != 0))
1594                         break;
1595         }
1596
1597         return node;
1598 }
1599
1600 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1601                                                 guint msgnum)
1602 {
1603         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1604         GtkCTreeNode *node;
1605         MsgInfo *msginfo;
1606
1607         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1608
1609         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1610                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1611                 if (msginfo->msgnum == msgnum) break;
1612         }
1613
1614         return node;
1615 }
1616
1617 static guint attract_hash_func(gconstpointer key)
1618 {
1619         gchar *str;
1620         gchar *p;
1621         guint h;
1622
1623         Xstrdup_a(str, (const gchar *)key, return 0);
1624         trim_subject(str);
1625
1626         p = str;
1627         h = *p;
1628
1629         if (h) {
1630                 for (p += 1; *p != '\0'; p++)
1631                         h = (h << 5) - h + *p;
1632         }
1633
1634         return h;
1635 }
1636
1637 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1638 {
1639         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1640 }
1641
1642 void summary_attract_by_subject(SummaryView *summaryview)
1643 {
1644         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1645         GtkCList *clist = GTK_CLIST(ctree);
1646         GtkCTreeNode *src_node;
1647         GtkCTreeNode *dst_node, *sibling;
1648         GtkCTreeNode *tmp;
1649         MsgInfo *src_msginfo, *dst_msginfo;
1650         GHashTable *subject_table;
1651
1652         debug_print("Attracting messages by subject...");
1653         STATUSBAR_PUSH(summaryview->mainwin,
1654                        _("Attracting messages by subject..."));
1655
1656         main_window_cursor_wait(summaryview->mainwin);
1657         gtk_clist_freeze(clist);
1658
1659         subject_table = g_hash_table_new(attract_hash_func,
1660                                          attract_compare_func);
1661
1662         for (src_node = GTK_CTREE_NODE(clist->row_list);
1663              src_node != NULL;
1664              src_node = tmp) {
1665                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1666                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1667                 if (!src_msginfo) continue;
1668                 if (!src_msginfo->subject) continue;
1669
1670                 /* find attracting node */
1671                 dst_node = g_hash_table_lookup(subject_table,
1672                                                src_msginfo->subject);
1673
1674                 if (dst_node) {
1675                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1676
1677                         /* if the time difference is more than 20 days,
1678                            don't attract */
1679                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1680                             > 60 * 60 * 24 * 20)
1681                                 continue;
1682
1683                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1684                         if (src_node != sibling)
1685                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1686                 }
1687
1688                 g_hash_table_insert(subject_table,
1689                                     src_msginfo->subject, src_node);
1690         }
1691
1692         g_hash_table_destroy(subject_table);
1693
1694         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1695
1696         gtk_clist_thaw(clist);
1697
1698         debug_print("done.\n");
1699         STATUSBAR_POP(summaryview->mainwin);
1700
1701         main_window_cursor_normal(summaryview->mainwin);
1702 }
1703
1704 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1705                                       gpointer data)
1706 {
1707         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1708
1709         if (msginfo)
1710                 procmsg_msginfo_free(msginfo);
1711 }
1712
1713 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1714                                    gpointer data)
1715 {
1716         SummaryView *summaryview = data;
1717         MsgInfo *msginfo;
1718
1719         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1720
1721         if (MSG_IS_NEWS(msginfo->flags))
1722                 news_flag_crosspost(msginfo);
1723
1724         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1725                 summaryview->newmsgs++;
1726         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1727                 summaryview->unread++;
1728         if (MSG_IS_DELETED(msginfo->flags))
1729                 summaryview->deleted++;
1730
1731         summaryview->messages++;
1732         summaryview->total_size += msginfo->size;
1733
1734         summary_set_row_marks(summaryview, node);
1735 }
1736
1737 static void summary_update_status(SummaryView *summaryview)
1738 {
1739         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1740         GtkCTreeNode *node;
1741         MsgInfo *msginfo;
1742
1743         summaryview->newmsgs = summaryview->unread =
1744         summaryview->messages = summaryview->total_size =
1745         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1746
1747         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1748              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1749                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1750
1751                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1752                         summaryview->newmsgs++;
1753                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1754                         summaryview->unread++;
1755                 if (MSG_IS_DELETED(msginfo->flags))
1756                         summaryview->deleted++;
1757                 if (MSG_IS_MOVE(msginfo->flags))
1758                         summaryview->moved++;
1759                 if (MSG_IS_COPY(msginfo->flags))
1760                         summaryview->copied++;
1761                 summaryview->messages++;
1762                 summaryview->total_size += msginfo->size;
1763         }
1764 }
1765
1766 static void summary_status_show(SummaryView *summaryview)
1767 {
1768         gchar *str;
1769         gchar *del, *mv, *cp;
1770         gchar *sel;
1771         gchar *spc;
1772         gchar *itstr;
1773         GList *rowlist, *cur;
1774         guint n_selected = 0;
1775         off_t sel_size = 0;
1776         MsgInfo *msginfo;
1777
1778         if (!summaryview->folder_item) {
1779                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1780                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1781                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1782                 return;
1783         }
1784
1785         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1786         for (cur = rowlist; cur != NULL; cur = cur->next) {
1787                 msginfo = gtk_ctree_node_get_row_data
1788                         (GTK_CTREE(summaryview->ctree),
1789                          GTK_CTREE_NODE(cur->data));
1790                 sel_size += msginfo->size;
1791                 n_selected++;
1792         }
1793
1794         if (summaryview->folder_item->folder->type == F_NEWS &&
1795             prefs_common.ng_abbrev_len < strlen(summaryview->folder_item->path)) {
1796                 gchar *group;
1797                 group = get_abbrev_newsgroup_name
1798                         (g_basename(summaryview->folder_item->path), prefs_common.ng_abbrev_len);
1799                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), group);
1800                 g_free(group);
1801         } else {
1802                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1803                               summaryview->folder_item->path);
1804         }
1805
1806         if (summaryview->deleted)
1807                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1808         else
1809                 del = g_strdup("");
1810         if (summaryview->moved)
1811                 mv = g_strdup_printf(_("%s%d moved"),
1812                                      summaryview->deleted ? _(", ") : "",
1813                                      summaryview->moved);
1814         else
1815                 mv = g_strdup("");
1816         if (summaryview->copied)
1817                 cp = g_strdup_printf(_("%s%d copied"),
1818                                      summaryview->deleted ||
1819                                      summaryview->moved ? _(", ") : "",
1820                                      summaryview->copied);
1821         else
1822                 cp = g_strdup("");
1823
1824         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1825                 spc = "    ";
1826         else
1827                 spc = "";
1828
1829         if (n_selected) {
1830                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1831                 if (n_selected == 1)
1832                         itstr = g_strdup(_(" item selected"));
1833                 else
1834                         itstr = g_strdup(_(" items selected"));
1835         } else {
1836                 sel = g_strdup("");
1837                 itstr = g_strdup("");
1838         }
1839                 
1840         str = g_strconcat(n_selected ? itos(n_selected) : "",
1841                                         itstr, sel, spc, del, mv, cp, NULL);
1842         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1843         g_free(str);
1844         g_free(sel);
1845         g_free(del);
1846         g_free(mv);
1847         g_free(cp);
1848         g_free(itstr);
1849
1850         if (FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1851                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1852                                       summaryview->newmsgs,
1853                                       summaryview->unread,
1854                                       summaryview->messages,
1855                                       to_human_readable(summaryview->total_size));
1856         } else {
1857                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1858                                       summaryview->newmsgs,
1859                                       summaryview->unread,
1860                                       summaryview->messages);
1861         }
1862         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1863         g_free(str);
1864 }
1865
1866 static void summary_set_column_titles(SummaryView *summaryview)
1867 {
1868         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1869         GtkWidget *hbox;
1870         GtkWidget *label;
1871         GtkWidget *arrow;
1872         gint pos;
1873         const gchar *title;
1874         SummaryColumnType type;
1875         gboolean single_char;
1876         GtkJustification justify;
1877
1878         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1879                 SORT_BY_MARK,
1880                 SORT_BY_UNREAD,
1881                 SORT_BY_MIME,
1882                 SORT_BY_SUBJECT,
1883                 SORT_BY_FROM,
1884                 SORT_BY_DATE,
1885                 SORT_BY_SIZE,
1886                 SORT_BY_NUMBER,
1887                 SORT_BY_SCORE,
1888                 SORT_BY_LOCKED
1889         };
1890
1891         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1892                 type = summaryview->col_state[pos].type;
1893
1894                 /* CLAWS: mime and unread are single char headers */
1895                 single_char = (type == S_COL_MIME || type == S_COL_UNREAD);
1896                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1897                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1898
1899                 switch (type) {
1900                 case S_COL_SUBJECT:
1901                 case S_COL_FROM:
1902                 case S_COL_DATE:
1903                 case S_COL_NUMBER:
1904                         if (prefs_common.trans_hdr)
1905                                 title = gettext(col_label[type]);
1906                         else
1907                                 title = col_label[type];
1908                         break;
1909                 /* CLAWS: dummies for mark and locked headers */        
1910                 case S_COL_MARK:        
1911                 case S_COL_LOCKED:
1912                         title = "";
1913                         break;
1914                 default:
1915                         title = gettext(col_label[type]);
1916                 }
1917
1918                 if (type == S_COL_MIME) {
1919                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1920                         gtk_widget_show(label);
1921                         gtk_clist_set_column_widget(clist, pos, label);
1922                         continue;
1923                 }
1924                 if (single_char) {
1925                         gtk_clist_set_column_title(clist, pos, title);
1926                         continue;
1927                 }
1928
1929                 /* CLAWS: changed so that locked and mark headers
1930                  * show a pixmap instead of single character */
1931                 hbox  = gtk_hbox_new(FALSE, 4);
1932                 
1933                 if (type == S_COL_LOCKED)
1934                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1935                 else if (type == S_COL_MARK) 
1936                         label = gtk_pixmap_new(markxpm, markxpmmask);
1937                 else 
1938                         label = gtk_label_new(title);
1939                 
1940                 if (justify == GTK_JUSTIFY_RIGHT)
1941                         gtk_box_pack_end(GTK_BOX(hbox), label,
1942                                          FALSE, FALSE, 0);
1943                 else
1944                         gtk_box_pack_start(GTK_BOX(hbox), label,
1945                                            FALSE, FALSE, 0);
1946
1947                 if (summaryview->sort_key == sort_by[type]) {
1948                         arrow = gtk_arrow_new
1949                                 (summaryview->sort_type == SORT_ASCENDING
1950                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1951                                  GTK_SHADOW_IN);
1952                         if (justify == GTK_JUSTIFY_RIGHT)
1953                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1954                                                    FALSE, FALSE, 0);
1955                         else
1956                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1957                                                  FALSE, FALSE, 0);
1958                 }
1959
1960                 gtk_widget_show_all(hbox);
1961                 gtk_clist_set_column_widget(clist, pos, hbox);
1962         }
1963 }
1964
1965 void summary_sort(SummaryView *summaryview,
1966                   FolderSortKey sort_key, FolderSortType sort_type)
1967 {
1968         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1969         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1970         GtkCListCompareFunc cmp_func = NULL;
1971
1972         switch (sort_key) {
1973         case SORT_BY_MARK:
1974                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
1975                 break;
1976         case SORT_BY_UNREAD:
1977                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_unread;
1978                 break;
1979         case SORT_BY_MIME:
1980                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
1981                 break;
1982         case SORT_BY_NUMBER:
1983                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1984                 break;
1985         case SORT_BY_SIZE:
1986                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1987                 break;
1988         case SORT_BY_DATE:
1989                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1990                 break;
1991         case SORT_BY_FROM:
1992                 if (summaryview->folder_item && summaryview->folder_item->stype != F_OUTBOX)
1993                         cmp_func = (GtkCListCompareFunc) summary_cmp_by_from;
1994                 else                    
1995                         cmp_func = (GtkCListCompareFunc) summary_cmp_by_to;
1996                 break;
1997         case SORT_BY_SUBJECT:
1998                 if (summaryview->simplify_subject_preg)
1999                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2000                 else
2001                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2002                 break;
2003         case SORT_BY_SCORE:
2004                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2005                 break;
2006         case SORT_BY_LOCKED:
2007                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2008                 break;
2009         case SORT_BY_LABEL:
2010                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2011                 break;
2012         case SORT_BY_NONE:
2013                 cmp_func = NULL;
2014                 return;
2015         default:
2016                 return;
2017         }
2018
2019         summaryview->sort_key = sort_key;
2020         summaryview->sort_type = sort_type;
2021
2022         summary_set_column_titles(summaryview);
2023         summary_set_menu_sensitive(summaryview);
2024         if(cmp_func != NULL) {
2025                 debug_print("Sorting summary...");
2026                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2027
2028                 main_window_cursor_wait(summaryview->mainwin);
2029
2030                 gtk_clist_set_compare_func(clist, cmp_func);
2031
2032                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2033
2034                 gtk_sctree_sort_recursive(ctree, NULL);
2035
2036                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2037
2038                 main_window_cursor_normal(summaryview->mainwin);
2039
2040                 debug_print("done.\n");
2041                 STATUSBAR_POP(summaryview->mainwin);
2042         }
2043 }
2044
2045 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2046                                    GtkCTreeNode *cnode, gpointer data)
2047 {
2048         SummaryView *summaryview = (SummaryView *)data;
2049         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2050         gchar *text[N_SUMMARY_COLS];
2051         gint *col_pos = summaryview->col_pos;
2052         const gchar *msgid = msginfo->msgid;
2053         GHashTable *msgid_table = summaryview->msgid_table;
2054
2055         summary_set_header(summaryview, text, msginfo);
2056
2057         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2058                                 NULL, NULL, NULL, NULL, FALSE,
2059                                 gnode->parent->parent ? TRUE : FALSE);
2060 #define SET_TEXT(col) \
2061         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2062                                 text[col_pos[col]])
2063
2064         SET_TEXT(S_COL_NUMBER);
2065         SET_TEXT(S_COL_SCORE);
2066         SET_TEXT(S_COL_SIZE);
2067         SET_TEXT(S_COL_DATE);
2068         SET_TEXT(S_COL_FROM);
2069         SET_TEXT(S_COL_SUBJECT);
2070
2071 #undef SET_TEXT
2072
2073         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2074         summary_set_marks_func(ctree, cnode, summaryview);
2075
2076         if (msgid && msgid[0] != '\0')
2077                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2078
2079         return TRUE;
2080 }
2081
2082 static void summary_set_ctree_from_list(SummaryView *summaryview,
2083                                         GSList *mlist)
2084 {
2085         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2086         MsgInfo *msginfo;
2087                 GtkCTreeNode *node = NULL;
2088         GHashTable *msgid_table;
2089         GHashTable *subject_table;
2090         GSList * cur;
2091         
2092         if (!mlist) return;
2093
2094         debug_print("\tSetting summary from message data...");
2095         STATUSBAR_PUSH(summaryview->mainwin,
2096                        _("Setting summary from message data..."));
2097         gdk_flush();
2098
2099         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2100         summaryview->msgid_table = msgid_table;
2101         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2102         summaryview->subject_table = subject_table;
2103
2104         if (prefs_common.use_addr_book)
2105                 start_address_completion();
2106         
2107         for (cur = mlist ; cur != NULL; cur = cur->next) {
2108                 msginfo = (MsgInfo *)cur->data;
2109                 msginfo->threadscore = msginfo->score;
2110         }
2111
2112         if (global_scoring || summaryview->folder_item->prefs->scoring) {
2113                 summaryview->important_score = prefs_common.important_score;
2114                 if (summaryview->folder_item->prefs->important_score >
2115                     summaryview->important_score)
2116                         summaryview->important_score =
2117                                 summaryview->folder_item->prefs->important_score;
2118         }
2119
2120         if (summaryview->threaded) {
2121                 GNode *root, *gnode;
2122
2123                 root = procmsg_get_thread_tree(mlist);
2124
2125                 for (gnode = root->children; gnode != NULL;
2126                      gnode = gnode->next) {
2127                         node = gtk_ctree_insert_gnode
2128                                 (ctree, NULL, node, gnode,
2129                                  summary_insert_gnode_func, summaryview);
2130                 }
2131
2132                 g_node_destroy(root);
2133
2134                 folderview_update_items_when_required(FALSE);
2135
2136                 summary_thread_init(summaryview);
2137         } else {
2138                 gchar *text[N_SUMMARY_COLS];
2139
2140                 mlist = g_slist_reverse(mlist);
2141                 for (; mlist != NULL; mlist = mlist->next) {
2142                         msginfo = (MsgInfo *)mlist->data;
2143
2144                         summary_set_header(summaryview, text, msginfo);
2145
2146                         node = gtk_ctree_insert_node
2147                                 (ctree, NULL, node, text, 2,
2148                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2149                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2150                         summary_set_marks_func(ctree, node, summaryview);
2151
2152                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2153                                 g_hash_table_insert(msgid_table,
2154                                                     msginfo->msgid, node);
2155
2156                         subject_table_insert(subject_table,
2157                                              msginfo->subject,
2158                                              node);
2159                 }
2160                 mlist = g_slist_reverse(mlist);
2161         }
2162
2163         if (prefs_common.enable_hscrollbar &&
2164             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2165                 gint optimal_width;
2166
2167                 optimal_width = gtk_clist_optimal_column_width
2168                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2169                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2170                                            summaryview->col_pos[S_COL_SUBJECT],
2171                                            optimal_width);
2172         }
2173
2174         if (prefs_common.use_addr_book)
2175                 end_address_completion();
2176
2177         debug_print("done.\n");
2178         STATUSBAR_POP(summaryview->mainwin);
2179         if (debug_mode) {
2180                 debug_print("\tmsgid hash table size = %d\n",
2181                             g_hash_table_size(msgid_table));
2182                 debug_print("\tsubject hash table size = %d\n",
2183                             g_hash_table_size(subject_table));
2184         }
2185 }
2186
2187 static gchar *summary_complete_address(const gchar *addr)
2188 {
2189         gint count;
2190         gchar *res, *tmp, *email_addr;
2191
2192         Xstrdup_a(email_addr, addr, return NULL);
2193         extract_address(email_addr);
2194         g_return_val_if_fail(*email_addr, NULL);
2195
2196         /*
2197          * completion stuff must be already initialized
2198          */
2199         res = NULL;
2200         if (1 < (count = complete_address(email_addr))) {
2201                 tmp = get_complete_address(1);
2202                 res = procheader_get_fromname(tmp);
2203                 g_free(tmp);    
2204         }
2205         
2206         return res;
2207 }
2208
2209 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2210                                MsgInfo *msginfo)
2211 {
2212         static gchar date_modified[80];
2213         static gchar *to = NULL;
2214         static gchar col_score[11];
2215         static gchar buf[BUFFSIZE];
2216         gint *col_pos = summaryview->col_pos;
2217
2218         text[col_pos[S_COL_MARK]]   = NULL;
2219         text[col_pos[S_COL_UNREAD]] = NULL;
2220         text[col_pos[S_COL_MIME]]   = NULL;
2221         text[col_pos[S_COL_LOCKED]] = NULL;
2222         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2223         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2224         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2225
2226         if (msginfo->date_t) {
2227                 procheader_date_get_localtime(date_modified,
2228                                               sizeof(date_modified),
2229                                               msginfo->date_t);
2230                 text[col_pos[S_COL_DATE]] = date_modified;
2231         } else if (msginfo->date)
2232                 text[col_pos[S_COL_DATE]] = msginfo->date;
2233         else
2234                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2235
2236         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2237                 _("(No From)");
2238         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2239             !MSG_IS_NEWS(msginfo->flags)) {
2240                 gchar *addr = NULL;
2241
2242                 Xstrdup_a(addr, msginfo->from, return);
2243                 extract_address(addr);
2244
2245                 if (prefs_common.use_addr_book) {
2246                         if (account_find_from_address(addr)) {
2247                                 addr = summary_complete_address(msginfo->to);
2248                                 g_free(to);
2249                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2250                                 text[col_pos[S_COL_FROM]] = to;
2251                                 g_free(addr);
2252                         }
2253                 } else {
2254                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2255                                 g_free(to);
2256                                 to = g_strconcat("-->", msginfo->to, NULL);
2257                                 text[col_pos[S_COL_FROM]] = to;
2258                         }
2259                 }
2260         }
2261
2262         /*
2263          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2264          * checking whether the above block (which handles the special case of
2265          * the --> in sent boxes) was executed.
2266          */
2267         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2268                 gchar *from = summary_complete_address(msginfo->from);
2269                 if (from) {
2270                         g_free(to);
2271                         to = from;
2272                         text[col_pos[S_COL_FROM]] = to;
2273                 }                       
2274         }
2275
2276         if (summaryview->simplify_subject_preg != NULL)
2277                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2278                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2279                                         summaryview->simplify_subject_preg) : 
2280                         _("(No Subject)");
2281         else 
2282                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2283                         _("(No Subject)");
2284 }
2285
2286 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2287 {
2288         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2289 }
2290
2291 static void summary_display_msg_full(SummaryView *summaryview,
2292                                      GtkCTreeNode *row,
2293                                      gboolean new_window, gboolean all_headers)
2294 {
2295         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2296         MsgInfo *msginfo;
2297         MsgFlags flags;
2298         gchar *filename;
2299
2300         if (!new_window && summaryview->displayed == row) return;
2301         g_return_if_fail(row != NULL);
2302
2303         if (summary_is_locked(summaryview)) return;
2304         summary_lock(summaryview);
2305
2306         STATUSBAR_POP(summaryview->mainwin);
2307         GTK_EVENTS_FLUSH();
2308
2309         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2310
2311         filename = procmsg_get_message_file(msginfo);
2312         if (!filename) {
2313                 summary_unlock(summaryview);
2314                 return;
2315         }
2316         g_free(filename);
2317
2318         if (new_window || !prefs_common.mark_as_read_on_new_window) {
2319                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2320                         summaryview->newmsgs--;
2321                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2322                         summaryview->unread--;
2323                 if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2324                         procmsg_msginfo_unset_flags
2325                                 (msginfo, MSG_NEW | MSG_UNREAD, 0);
2326                         folderview_update_item(msginfo->folder, FALSE);
2327                         summary_set_row_marks(summaryview, row);
2328                         gtk_clist_thaw(GTK_CLIST(ctree));
2329                         summary_status_show(summaryview);
2330                         
2331                         flags = msginfo->flags;
2332                 }
2333         }
2334
2335         if (new_window) {
2336                 MessageView *msgview;
2337
2338                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2339                 messageview_show(msgview, msginfo, all_headers);
2340         } else {
2341                 MessageView *msgview;
2342
2343                 msgview = summaryview->messageview;
2344
2345                 summaryview->displayed = row;
2346                 if (!messageview_is_visible(msgview))
2347                         main_window_toggle_message_view(summaryview->mainwin);
2348                 messageview_show(msgview, msginfo, all_headers);
2349                 if (msgview->type == MVIEW_TEXT ||
2350                     (msgview->type == MVIEW_MIME &&
2351                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2352                       gtk_notebook_get_current_page
2353                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2354                         gtk_widget_grab_focus(summaryview->ctree);
2355                 GTK_EVENTS_FLUSH();
2356                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2357         }
2358
2359         summary_set_menu_sensitive(summaryview);
2360         toolbar_set_sensitive(summaryview->mainwin);
2361
2362         summary_unlock(summaryview);
2363 }
2364
2365 void summary_display_msg_selected(SummaryView *summaryview,
2366                                   gboolean all_headers)
2367 {
2368         if (summary_is_locked(summaryview)) return;
2369         summaryview->displayed = NULL;
2370         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2371                                  all_headers);
2372 }
2373
2374 void summary_redisplay_msg(SummaryView *summaryview)
2375 {
2376         GtkCTreeNode *node;
2377
2378         if (summaryview->displayed) {
2379                 node = summaryview->displayed;
2380                 summaryview->displayed = NULL;
2381                 summary_display_msg(summaryview, node);
2382         }
2383 }
2384
2385 void summary_open_msg(SummaryView *summaryview)
2386 {
2387         if (!summaryview->selected) return;
2388
2389         summary_display_msg_full(summaryview, summaryview->selected,
2390                                  TRUE, FALSE);
2391 }
2392
2393 void summary_view_source(SummaryView * summaryview)
2394 {
2395         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2396         MsgInfo *msginfo;
2397         SourceWindow *srcwin;
2398
2399         if (!summaryview->selected) return;
2400
2401         srcwin = source_window_create();
2402         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2403         source_window_show_msg(srcwin, msginfo);
2404         source_window_show(srcwin);
2405 }
2406
2407 void summary_reedit(SummaryView *summaryview)
2408 {
2409         MsgInfo *msginfo;
2410
2411         if (!summaryview->selected) return;
2412         if (!summaryview->folder_item) return;
2413         if (summaryview->folder_item->stype != F_OUTBOX &&
2414             summaryview->folder_item->stype != F_DRAFT  &&
2415             summaryview->folder_item->stype != F_QUEUE) return;
2416
2417         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2418                                               summaryview->selected);
2419         if (!msginfo) return;
2420
2421         compose_reedit(msginfo);
2422 }
2423
2424 void summary_step(SummaryView *summaryview, GtkScrollType type)
2425 {
2426         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2427         GtkCTreeNode *node;
2428
2429         if (summary_is_locked(summaryview)) return;
2430
2431         if (type == GTK_SCROLL_STEP_FORWARD) {
2432                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2433                 if (node)
2434                         gtkut_ctree_expand_parent_all(ctree, node);
2435                 else
2436                         return;
2437         } else {
2438                 if (summaryview->selected) {
2439                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2440                         if (!node) return;
2441                 }
2442         }
2443
2444         if (messageview_is_visible(summaryview->messageview))
2445                 summaryview->display_msg = TRUE;
2446
2447         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2448                                 type, 0.0);
2449
2450         if (GTK_CLIST(ctree)->selection)
2451                 gtk_sctree_set_anchor_row
2452                         (GTK_SCTREE(ctree),
2453                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2454
2455 }
2456
2457 void summary_toggle_view(SummaryView *summaryview)
2458 {
2459         if (!messageview_is_visible(summaryview->messageview) &&
2460             summaryview->selected)
2461                 summary_display_msg(summaryview,
2462                                     summaryview->selected);
2463         else
2464                 main_window_toggle_message_view(summaryview->mainwin);
2465 }
2466
2467 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2468                                                 GtkCTreeNode *node)
2469 {
2470         MsgInfo *msginfo;
2471
2472         if (node) {
2473                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2474                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2475                         return TRUE;
2476                 node = GTK_CTREE_ROW(node)->children;
2477         } else
2478                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2479
2480         while (node) {
2481                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2482                         return TRUE;
2483                 node = GTK_CTREE_ROW(node)->sibling;
2484         }
2485
2486         return FALSE;
2487 }
2488
2489 static gboolean summary_have_unread_children(SummaryView *summaryview,
2490                                              GtkCTreeNode *node)
2491 {
2492         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2493
2494         if (!node) return FALSE;
2495
2496         node = GTK_CTREE_ROW(node)->children;
2497
2498         while (node) {
2499                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2500                         return TRUE;
2501                 node = GTK_CTREE_ROW(node)->sibling;
2502         }
2503
2504         return FALSE;
2505 }
2506
2507 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2508 {
2509         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2510         GtkStyle *style = NULL;
2511         MsgInfo *msginfo;
2512         MsgFlags flags;
2513         gint *col_pos = summaryview->col_pos;
2514
2515         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2516         if (!msginfo) return;
2517
2518         flags = msginfo->flags;
2519
2520         gtk_ctree_node_set_foreground(ctree, row, NULL);
2521
2522         /* set new/unread column */
2523         if (MSG_IS_IGNORE_THREAD(flags)) {
2524                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2525                                           ignorethreadxpm, ignorethreadxpmmask);
2526         } else if (MSG_IS_NEW(flags)) {
2527                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2528                                           newxpm, newxpmmask);
2529         } else if (MSG_IS_UNREAD(flags)) {
2530                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2531                                           unreadxpm, unreadxpmmask);
2532         } else if (MSG_IS_REPLIED(flags)) {
2533                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2534                                           repliedxpm, repliedxpmmask);
2535         } else if (MSG_IS_FORWARDED(flags)) {
2536                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2537                                           forwardedxpm, forwardedxpmmask);
2538         } else {
2539                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2540                                         NULL);
2541         }
2542
2543         if (prefs_common.bold_unread &&
2544             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2545              (!GTK_CTREE_ROW(row)->expanded &&
2546               GTK_CTREE_ROW(row)->children &&
2547               summary_have_unread_children(summaryview, row))))
2548                 style = bold_style;
2549
2550         /* set mark column */
2551         if (MSG_IS_DELETED(flags)) {
2552                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2553                                           deletedxpm, deletedxpmmask);
2554                 if (style)
2555                         style = bold_deleted_style;
2556                 else {
2557                         style = small_deleted_style;
2558                 }
2559                         gtk_ctree_node_set_foreground
2560                                 (ctree, row, &summaryview->color_dim);
2561         } else if (MSG_IS_MARKED(flags)) {
2562                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2563                                           markxpm, markxpmmask);
2564         } else if (MSG_IS_MOVE(flags)) {
2565                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2566                 if (style)
2567                         style = bold_marked_style;
2568                 else {
2569                         style = small_marked_style;
2570                 }
2571                         gtk_ctree_node_set_foreground
2572                                 (ctree, row, &summaryview->color_marked);
2573         } else if (MSG_IS_COPY(flags)) {
2574                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2575                 if (style)
2576                         style = bold_marked_style;
2577                 else {
2578                         style = small_marked_style;
2579                 }
2580                         gtk_ctree_node_set_foreground
2581                                 (ctree, row, &summaryview->color_marked);
2582         }
2583         else if ((global_scoring ||
2584                   summaryview->folder_item->prefs->scoring) &&
2585                  (msginfo->score >= summaryview->important_score) &&
2586                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2587                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2588                 gtk_ctree_node_set_foreground(ctree, row,
2589                                               &summaryview->color_important);
2590         } else {
2591                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2592         }
2593
2594         if (MSG_IS_LOCKED(flags)) {
2595                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2596                                           lockedxpm, lockedxpmmask);
2597         }
2598         else {
2599                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2600         }
2601
2602         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2603                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2604                                           clipkeyxpm, clipkeyxpmmask);
2605         } else if (MSG_IS_ENCRYPTED(flags)) {
2606                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2607                                           keyxpm, keyxpmmask);
2608         } else if (MSG_IS_MIME(flags)) {
2609                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2610                                           clipxpm, clipxpmmask);
2611         } else {
2612                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2613         }
2614         if (!style)
2615                 style = small_style;
2616
2617         gtk_ctree_node_set_row_style(ctree, row, style);
2618
2619         if (MSG_GET_COLORLABEL(flags))
2620                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2621 }
2622
2623 void summary_set_marks_selected(SummaryView *summaryview)
2624 {
2625         GList *cur;
2626
2627         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2628              cur = cur->next)
2629                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2630 }
2631
2632 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2633 {
2634         gboolean changed = FALSE;
2635         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2636         MsgInfo *msginfo;
2637
2638         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2639         if (MSG_IS_DELETED(msginfo->flags))
2640                 summaryview->deleted--;
2641         if (MSG_IS_MOVE(msginfo->flags)) {
2642                 summaryview->moved--;
2643                 changed = TRUE;
2644         }
2645         if (MSG_IS_COPY(msginfo->flags)) {
2646                 summaryview->copied--;
2647                 changed = TRUE;
2648         }
2649         if (changed && !prefs_common.immediate_exec) {
2650                 msginfo->to_folder->op_count--;
2651                 if (msginfo->to_folder->op_count == 0)
2652                         folderview_update_item(msginfo->to_folder, 0);
2653         }
2654         msginfo->to_folder = NULL;
2655         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2656         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2657         summary_set_row_marks(summaryview, row);
2658         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2659 }
2660
2661 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2662 {
2663         /* almost verbatim summary_mark_row(); may want a menu action? */
2664         gboolean changed = FALSE;
2665         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2666         MsgInfo *msginfo;
2667
2668         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2669         if (MSG_IS_DELETED(msginfo->flags))
2670                 summaryview->deleted--;
2671         if (MSG_IS_MOVE(msginfo->flags)) {
2672                 summaryview->moved--;
2673                 changed = TRUE;
2674         }
2675         if (MSG_IS_COPY(msginfo->flags)) {
2676                 summaryview->copied--;
2677                 changed = TRUE;
2678         }
2679         if (changed && !prefs_common.immediate_exec) {
2680                 msginfo->to_folder->op_count--;
2681                 if (msginfo->to_folder->op_count == 0)
2682                         folderview_update_item(msginfo->to_folder, 0);
2683         }
2684         msginfo->to_folder = NULL;
2685         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2686         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2687         summary_set_row_marks(summaryview, row);
2688         debug_print("Message %d is locked\n", msginfo->msgnum);
2689 }
2690
2691 void summary_mark(SummaryView *summaryview)
2692 {
2693         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2694         GList *cur;
2695
2696         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2697                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2698
2699         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2700         summary_status_show(summaryview);
2701 }
2702
2703 static void summary_mark_row_as_read(SummaryView *summaryview,
2704                                      GtkCTreeNode *row)
2705 {
2706         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2707         MsgInfo *msginfo;
2708
2709         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2710
2711         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2712                 return;
2713
2714         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2715                 summaryview->newmsgs--;
2716         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2717                 summaryview->unread--;
2718
2719         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2720         summary_set_row_marks(summaryview, row);
2721         debug_print("Message %d is marked as read\n",
2722                 msginfo->msgnum);
2723 }
2724
2725 void summary_mark_as_read(SummaryView *summaryview)
2726 {
2727         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2728         GList *cur;
2729
2730         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2731                 summary_mark_row_as_read(summaryview,
2732                                          GTK_CTREE_NODE(cur->data));
2733         folderview_update_items_when_required(FALSE);
2734
2735         summary_status_show(summaryview);
2736 }
2737
2738 void summary_mark_all_read(SummaryView *summaryview)
2739 {
2740         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2741         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2742         GtkCTreeNode *node;
2743
2744         gtk_clist_freeze(clist);
2745         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2746              node = gtkut_ctree_node_next(ctree, node))
2747                 summary_mark_row_as_read(summaryview, node);
2748         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2749              node = gtkut_ctree_node_next(ctree, node)) {
2750                 if (!GTK_CTREE_ROW(node)->expanded)
2751                         summary_set_row_marks(summaryview, node);
2752         }
2753         gtk_clist_thaw(clist);
2754         folderview_update_items_when_required(FALSE);
2755
2756         summary_status_show(summaryview);
2757 }
2758
2759 static void summary_mark_row_as_unread(SummaryView *summaryview,
2760                                        GtkCTreeNode *row)
2761 {
2762         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2763         MsgInfo *msginfo;
2764
2765         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2766         if (MSG_IS_DELETED(msginfo->flags)) {
2767                 msginfo->to_folder = NULL;
2768                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2769                 summaryview->deleted--;
2770         }
2771
2772         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2773                 summaryview->unread++;
2774
2775         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2776         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2777         debug_print("Message %d is marked as unread\n",
2778                 msginfo->msgnum);
2779
2780         summary_set_row_marks(summaryview, row);
2781 }
2782
2783 void summary_mark_as_unread(SummaryView *summaryview)
2784 {
2785         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2786         GList *cur;
2787
2788         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2789                 summary_mark_row_as_unread(summaryview,
2790                                            GTK_CTREE_NODE(cur->data));
2791         folderview_update_items_when_required(FALSE);
2792
2793         summary_status_show(summaryview);
2794 }
2795
2796 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2797 {
2798         GList * cur;
2799         gboolean found;
2800
2801         switch (summaryview->folder_item->folder->type) {
2802
2803         case F_NEWS:
2804
2805                 /*
2806                   security : checks if one the accounts correspond to
2807                   the author of the post
2808                 */
2809
2810                 found = FALSE;
2811                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2812                         PrefsAccount * account;
2813                         gchar * from_name;
2814                         
2815                         account = cur->data;
2816                         if (account->name && *account->name)
2817                                 from_name =
2818                                         g_strdup_printf("%s <%s>",
2819                                                         account->name,
2820                                                         account->address);
2821                         else
2822                                 from_name =
2823                                         g_strdup_printf("%s",
2824                                                         account->address);
2825                         
2826                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2827                                 g_free(from_name);
2828                                 found = TRUE;
2829                                 break;
2830                         }
2831                         g_free(from_name);
2832                 }
2833
2834                 if (!found) {
2835                         alertpanel_error(_("You're not the author of the article\n"));
2836                 }
2837                 
2838                 return found;
2839
2840         default:
2841                 return TRUE;
2842         }
2843 }
2844
2845 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2846 {
2847         gboolean changed = FALSE;
2848         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2849         MsgInfo *msginfo;
2850
2851         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2852
2853         if (!check_permission(summaryview, msginfo))
2854                 return;
2855
2856         if (MSG_IS_LOCKED(msginfo->flags)) return;
2857
2858         if (MSG_IS_DELETED(msginfo->flags)) return;
2859
2860         if (MSG_IS_MOVE(msginfo->flags)) {
2861                 summaryview->moved--;
2862                 changed = TRUE;
2863         }
2864         if (MSG_IS_COPY(msginfo->flags)) {
2865                 summaryview->copied--;
2866                 changed = TRUE;
2867         }
2868         if (changed && !prefs_common.immediate_exec) {
2869                 msginfo->to_folder->op_count--;
2870                 if (msginfo->to_folder->op_count == 0)
2871                         folderview_update_item(msginfo->to_folder, 0);
2872         }
2873         msginfo->to_folder = NULL;
2874         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2875         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2876         summaryview->deleted++;
2877
2878         if (!prefs_common.immediate_exec && 
2879             summaryview->folder_item->stype != F_TRASH)
2880                 summary_set_row_marks(summaryview, row);
2881
2882         debug_print("Message %s/%d is set to delete\n",
2883                     msginfo->folder->path, msginfo->msgnum);
2884 }
2885
2886 void summary_cancel(SummaryView *summaryview)
2887 {
2888         MsgInfo * msginfo;
2889         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2890
2891         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2892                                               summaryview->selected);
2893         if (!msginfo) return;
2894
2895         if (!check_permission(summaryview, msginfo))
2896                 return;
2897
2898         news_cancel_article(summaryview->folder_item->folder, msginfo);
2899         
2900         if (summary_is_locked(summaryview)) return;
2901
2902         summary_lock(summaryview);
2903
2904         gtk_clist_freeze(clist);
2905
2906         summary_update_status(summaryview);
2907         summary_status_show(summaryview);
2908
2909         gtk_clist_thaw(clist);
2910
2911         summary_unlock(summaryview);
2912 }
2913
2914 void summary_delete(SummaryView *summaryview)
2915 {
2916         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2917         FolderItem *item = summaryview->folder_item;
2918         GList *cur;
2919         GtkCTreeNode *sel_last = NULL;
2920         GtkCTreeNode *node;
2921
2922         if (!item) return;
2923 #if 0
2924         if (!item || item->folder->type == F_NEWS) return;
2925 #endif
2926
2927         if (summary_is_locked(summaryview)) return;
2928
2929         /* if current folder is trash, ask for confirmation */
2930         if (item->stype == F_TRASH) {
2931                 AlertValue aval;
2932
2933                 aval = alertpanel(_("Delete message(s)"),
2934                                   _("Do you really want to delete message(s) from the trash?"),
2935                                   _("Yes"), _("No"), NULL);
2936                 if (aval != G_ALERTDEFAULT) return;
2937         }
2938
2939         /* next code sets current row focus right. We need to find a row
2940          * that is not deleted. */
2941         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2942                 sel_last = GTK_CTREE_NODE(cur->data);
2943                 summary_delete_row(summaryview, sel_last);
2944         }
2945
2946         node = summary_find_next_msg(summaryview, sel_last);
2947         if (!node)
2948                 node = summary_find_prev_msg(summaryview, sel_last);
2949
2950         if (node) {
2951                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2952                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2953                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2954                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2955                 else
2956                         summary_select_node
2957                                 (summaryview, node,
2958                                  messageview_is_visible(summaryview->messageview),
2959                                  FALSE);
2960         }
2961
2962         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2963                 summary_execute(summaryview);
2964         else
2965                 summary_status_show(summaryview);
2966 }
2967
2968 void summary_delete_duplicated(SummaryView *summaryview)
2969 {
2970         if (!summaryview->folder_item ||
2971             summaryview->folder_item->folder->type == F_NEWS) return;
2972         if (summaryview->folder_item->stype == F_TRASH) return;
2973
2974         main_window_cursor_wait(summaryview->mainwin);
2975         debug_print("Deleting duplicated messages...");
2976         STATUSBAR_PUSH(summaryview->mainwin,
2977                        _("Deleting duplicated messages..."));
2978
2979         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2980                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2981                                 summaryview);
2982
2983         if (prefs_common.immediate_exec)
2984                 summary_execute(summaryview);
2985         else
2986                 summary_status_show(summaryview);
2987
2988         debug_print("done.\n");
2989         STATUSBAR_POP(summaryview->mainwin);
2990         main_window_cursor_normal(summaryview->mainwin);
2991 }
2992
2993 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2994                                            SummaryView *summaryview)
2995 {
2996         GtkCTreeNode *found;
2997         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2998
2999         if (!msginfo->msgid || !*msginfo->msgid) return;
3000
3001         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
3002
3003         if (found && found != node)
3004                 summary_delete_row(summaryview, node);
3005 }
3006
3007 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3008 {
3009         gboolean changed = FALSE;
3010         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3011         MsgInfo *msginfo;
3012
3013         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3014         if (MSG_IS_DELETED(msginfo->flags))
3015                 summaryview->deleted--;
3016         if (MSG_IS_MOVE(msginfo->flags)) {
3017                 summaryview->moved--;
3018                 changed = TRUE;
3019         }
3020         if (MSG_IS_COPY(msginfo->flags)) {
3021                 summaryview->copied--;
3022                 changed = TRUE;
3023         }
3024         if (changed && !prefs_common.immediate_exec) {
3025                 msginfo->to_folder->op_count--;
3026                 if (msginfo->to_folder->op_count == 0)
3027                         folderview_update_item(msginfo->to_folder, 0);
3028         }
3029         msginfo->to_folder = NULL;
3030         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3031         summary_set_row_marks(summaryview, row);
3032
3033         debug_print("Message %s/%d is unmarked\n",
3034                     msginfo->folder->path, msginfo->msgnum);
3035 }
3036
3037 void summary_unmark(SummaryView *summaryview)
3038 {
3039         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3040         GList *cur;
3041
3042         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3043                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3044
3045         summary_status_show(summaryview);
3046 }
3047
3048 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3049                                 FolderItem *to_folder)
3050 {
3051         gboolean changed = FALSE;
3052         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3053         MsgInfo *msginfo;
3054
3055         g_return_if_fail(to_folder != NULL);
3056
3057         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3058         if (MSG_IS_MOVE(msginfo->flags)) {
3059                 if (!prefs_common.immediate_exec) {
3060                         msginfo->to_folder->op_count--;
3061                         if (msginfo->to_folder->op_count == 0) {
3062                                 folderview_update_item(msginfo->to_folder, 0);
3063                                 changed = TRUE;
3064                         }
3065                 }
3066         }
3067         msginfo->to_folder = to_folder;
3068         if (MSG_IS_DELETED(msginfo->flags))
3069                 summaryview->deleted--;
3070         if (MSG_IS_COPY(msginfo->flags)) {
3071                 summaryview->copied--;
3072                 if (!prefs_common.immediate_exec)
3073                         msginfo->to_folder->op_count--;
3074         }
3075         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3076         if (!MSG_IS_MOVE(msginfo->flags)) {
3077                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3078                 summaryview->moved++;
3079                 changed = TRUE;
3080         }
3081         if (!prefs_common.immediate_exec) {
3082                 summary_set_row_marks(summaryview, row);
3083                 if (changed) {
3084                         msginfo->to_folder->op_count++;
3085                         if (msginfo->to_folder->op_count == 1)
3086                                 folderview_update_item(msginfo->to_folder, 0);
3087                 }
3088         }
3089
3090         debug_print("Message %d is set to move to %s\n",
3091                     msginfo->msgnum, to_folder->path);
3092 }
3093
3094 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3095 {
3096         GList *cur;
3097
3098         if (!to_folder) return;
3099         if (!summaryview->folder_item ||
3100             summaryview->folder_item->folder->type == F_NEWS) return;
3101
3102         if (summary_is_locked(summaryview)) return;
3103
3104         if (summaryview->folder_item == to_folder) {
3105                 alertpanel_notice(_("Destination is same as current folder."));
3106                 return;
3107         }
3108
3109         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3110              cur != NULL; cur = cur->next)
3111                 summary_move_row_to
3112                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3113
3114         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3115
3116         if (prefs_common.immediate_exec)
3117                 summary_execute(summaryview);
3118         else {
3119                 summary_status_show(summaryview);
3120
3121                 folderview_update_item(to_folder, 0);
3122         }
3123         
3124         if (!summaryview->selected) { /* this was the last message */
3125                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3126                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3127                 if (node)
3128                         summary_select_node(summaryview, node, TRUE, TRUE);
3129         }
3130
3131 }
3132
3133 void summary_move_to(SummaryView *summaryview)
3134 {
3135         FolderItem *to_folder;
3136
3137         if (!summaryview->folder_item ||
3138             summaryview->folder_item->folder->type == F_NEWS) return;
3139
3140         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3141                                          FOLDER_SEL_MOVE, NULL);
3142         summary_move_selected_to(summaryview, to_folder);
3143 }
3144
3145 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3146                                 FolderItem *to_folder)
3147 {
3148         gboolean changed = FALSE;
3149         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3150         MsgInfo *msginfo;
3151
3152         g_return_if_fail(to_folder != NULL);
3153
3154         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3155         if (MSG_IS_COPY(msginfo->flags)) {
3156                 if (!prefs_common.immediate_exec) {
3157                         msginfo->to_folder->op_count--;
3158                         if (msginfo->to_folder->op_count == 0) {
3159                                 folderview_update_item(msginfo->to_folder, 0);
3160                                 changed = TRUE;
3161                         }
3162                 }
3163         }
3164         msginfo->to_folder = to_folder;
3165         if (MSG_IS_DELETED(msginfo->flags))
3166                 summaryview->deleted--;
3167         if (MSG_IS_MOVE(msginfo->flags)) {
3168                 summaryview->moved--;
3169                 if (!prefs_common.immediate_exec)
3170                         msginfo->to_folder->op_count--;
3171         }
3172         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3173         if (!MSG_IS_COPY(msginfo->flags)) {
3174                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3175                 summaryview->copied++;
3176                 changed = TRUE;
3177         }
3178         if (!prefs_common.immediate_exec) {
3179                 summary_set_row_marks(summaryview, row);
3180                 if (changed) {
3181                         msginfo->to_folder->op_count++;
3182                         if (msginfo->to_folder->op_count == 1)
3183                                 folderview_update_item(msginfo->to_folder, 0);
3184                 }
3185         }
3186
3187         debug_print("Message %d is set to copy to %s\n",
3188                     msginfo->msgnum, to_folder->path);
3189 }
3190
3191 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3192 {
3193         GList *cur;
3194
3195         if (!to_folder) return;
3196         if (!summaryview->folder_item) return;
3197
3198         if (summary_is_locked(summaryview)) return;
3199
3200         if (summaryview->folder_item == to_folder) {
3201                 alertpanel_notice
3202                         (_("Destination to copy is same as current folder."));
3203                 return;
3204         }
3205
3206         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3207              cur != NULL; cur = cur->next)
3208                 summary_copy_row_to
3209                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3210
3211         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3212
3213         if (prefs_common.immediate_exec)
3214                 summary_execute(summaryview);
3215         else {
3216                 summary_status_show(summaryview);
3217
3218                 folderview_update_item(to_folder, 0);
3219         }
3220 }
3221
3222 void summary_copy_to(SummaryView *summaryview)
3223 {
3224         FolderItem *to_folder;
3225
3226         if (!summaryview->folder_item) return;
3227
3228         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3229                                          FOLDER_SEL_COPY, NULL);
3230         summary_copy_selected_to(summaryview, to_folder);
3231 }
3232
3233 void summary_add_address(SummaryView *summaryview)
3234 {
3235         MsgInfo *msginfo;
3236         gchar *from;
3237
3238         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3239                                               summaryview->selected);
3240         if (!msginfo) return;
3241
3242         Xstrdup_a(from, msginfo->from, return);
3243         eliminate_address_comment(from);
3244         extract_address(from);
3245         addressbook_add_contact(msginfo->fromname, from, NULL);
3246 }
3247
3248 void summary_select_all(SummaryView *summaryview)
3249 {
3250         if (summaryview->messages >= 500) {
3251                 STATUSBAR_PUSH(summaryview->mainwin,
3252                                _("Selecting all messages..."));
3253                 main_window_cursor_wait(summaryview->mainwin);
3254         }
3255
3256         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3257
3258         if (summaryview->messages >= 500) {
3259                 STATUSBAR_POP(summaryview->mainwin);
3260                 main_window_cursor_normal(summaryview->mainwin);
3261         }
3262 }
3263
3264 void summary_unselect_all(SummaryView *summaryview)
3265 {
3266         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3267 }
3268
3269 void summary_select_thread(SummaryView *summaryview)
3270 {
3271         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3272         GtkCTreeNode *node = summaryview->selected;
3273
3274         if (!node) return;
3275
3276         while (GTK_CTREE_ROW(node)->parent != NULL)
3277                 node = GTK_CTREE_ROW(node)->parent;
3278
3279         if (node != summaryview->selected)
3280                 summary_select_node
3281                         (summaryview, node,
3282                          messageview_is_visible(summaryview->messageview),
3283                          FALSE);
3284
3285         gtk_ctree_select_recursive(ctree, node);
3286
3287         summary_status_show(summaryview);
3288 }
3289
3290 void summary_save_as(SummaryView *summaryview)
3291 {
3292         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3293         MsgInfo *msginfo;
3294         gchar *filename = NULL;
3295         gchar *src, *dest;
3296
3297         AlertValue aval;
3298
3299         if (!summaryview->selected) return;
3300         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3301         if (!msginfo) return;
3302
3303         if (msginfo->subject) {
3304                 Xstrdup_a(filename, msginfo->subject, return);
3305                 subst_for_filename(filename);
3306         }
3307         dest = filesel_select_file(_("Save as"), filename);
3308         if (!dest) return;
3309         if (is_file_exist(dest)) {
3310                 aval = alertpanel(_("Append or Overwrite"),
3311                                   _("Append or overwrite existing file?"),
3312                                   _("Append"), _("Overwrite"), _("Cancel"));
3313                 if (aval!=0 && aval!=1) return;
3314         }
3315
3316         src = procmsg_get_message_file(msginfo);
3317         if ( aval==0 ) { /* append */
3318                 if (append_file(src, dest, TRUE) < 0) 
3319                         alertpanel_error(_("Can't save the file `%s'."),
3320                                          g_basename(dest));
3321         } else { /* overwrite */
3322                 if (copy_file(src, dest, TRUE) < 0)
3323                         alertpanel_error(_("Can't save the file `%s'."),
3324                                          g_basename(dest));
3325         }
3326         g_free(src);
3327
3328         
3329         /*
3330          * If two or more msgs are selected,
3331          * append them to the output file.
3332          */
3333         if (GTK_CLIST(ctree)->selection->next) {
3334                 GList *item;
3335                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3336                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3337                         if (!msginfo) break;
3338                         src = procmsg_get_message_file(msginfo);
3339                         if (append_file(src, dest, TRUE) < 0)
3340                                 alertpanel_error(_("Can't save the file `%s'."),
3341                                                  g_basename(dest));
3342                 }
3343                 g_free(src);
3344         }
3345 }
3346
3347 void summary_print(SummaryView *summaryview)
3348 {
3349         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3350         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3351         MsgInfo *msginfo;
3352         GList *cur;
3353         gchar *cmdline;
3354         gchar *p;
3355
3356         if (clist->selection == NULL) return;
3357
3358         cmdline = input_dialog(_("Print"),
3359                                _("Enter the print command line:\n"
3360                                  "(`%s' will be replaced with file name)"),
3361                                prefs_common.print_cmd);
3362         if (!cmdline) return;
3363         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3364             strchr(p + 2, '%')) {
3365                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3366                                  cmdline);
3367                 g_free(cmdline);
3368                 return;
3369         }
3370
3371         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3372                 msginfo = gtk_ctree_node_get_row_data
3373                         (ctree, GTK_CTREE_NODE(cur->data));
3374                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3375         }
3376
3377         g_free(cmdline);
3378 }
3379
3380 gboolean summary_execute(SummaryView *summaryview)
3381 {
3382         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3383         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3384         GtkCTreeNode *node, *next;
3385
3386         if (!summaryview->folder_item) return FALSE;
3387
3388         if (summary_is_locked(summaryview)) return FALSE;
3389         summary_lock(summaryview);
3390
3391         gtk_clist_freeze(clist);
3392
3393         if (summaryview->threaded)
3394                 summary_unthread_for_exec(summaryview);
3395
3396         summary_execute_move(summaryview);
3397         summary_execute_copy(summaryview);
3398         summary_execute_delete(summaryview);
3399
3400         node = GTK_CTREE_NODE(clist->row_list);
3401         while (node != NULL) {
3402                 next = gtkut_ctree_node_next(ctree, node);
3403                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
3404                         if (node == summaryview->displayed) {
3405                                 messageview_clear(summaryview->messageview);
3406                                 summaryview->displayed = NULL;
3407                         }
3408                         if (GTK_CTREE_ROW(node)->children != NULL)
3409                                 g_warning("summary_execute(): children != NULL\n");
3410                         else
3411                                 gtk_ctree_remove_node(ctree, node);
3412                 }
3413                 node = next;
3414         }
3415
3416         if (summaryview->threaded)
3417                 summary_thread_build(summaryview);
3418
3419         summaryview->selected = clist->selection ?
3420                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3421
3422         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3423                 menu_set_insensitive_all
3424                         (GTK_MENU_SHELL(summaryview->popupmenu));
3425                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3426         } else
3427                 gtk_widget_grab_focus(summaryview->ctree);
3428
3429         summary_update_status(summaryview);
3430         summary_status_show(summaryview);
3431
3432         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3433
3434         gtk_clist_thaw(clist);
3435
3436         summary_unlock(summaryview);
3437         return TRUE;
3438 }
3439
3440 static void summary_execute_move(SummaryView *summaryview)
3441 {
3442         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3443         GSList *cur;
3444
3445         /* search moving messages and execute */
3446         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3447                                 summaryview);
3448
3449         if (summaryview->mlist) {
3450                 procmsg_move_messages(summaryview->mlist);
3451
3452                 folderview_update_items_when_required(FALSE);
3453
3454                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3455                         procmsg_msginfo_free((MsgInfo *)cur->data);
3456                 g_slist_free(summaryview->mlist);
3457                 summaryview->mlist = NULL;
3458         }
3459
3460         folderview_update_item(summaryview->folder_item, FALSE);
3461 }
3462
3463 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3464                                       gpointer data)
3465 {
3466         SummaryView *summaryview = data;
3467         MsgInfo *msginfo;
3468
3469         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3470
3471         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3472                 if (!prefs_common.immediate_exec &&
3473                     msginfo->to_folder->op_count > 0)
3474                         msginfo->to_folder->op_count--;
3475
3476                 summaryview->mlist =
3477                         g_slist_append(summaryview->mlist, msginfo);
3478                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3479
3480                 if (msginfo->msgid && *msginfo->msgid &&
3481                     node == g_hash_table_lookup(summaryview->msgid_table,
3482                                                 msginfo->msgid))
3483                         g_hash_table_remove(summaryview->msgid_table,
3484                                             msginfo->msgid);
3485         }
3486 }
3487
3488 static void summary_execute_copy(SummaryView *summaryview)
3489 {
3490         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3491
3492         /* search copying messages and execute */
3493         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3494                                 summaryview);
3495
3496         if (summaryview->mlist) {
3497                 procmsg_copy_messages(summaryview->mlist);
3498
3499                 folderview_update_items_when_required(FALSE);
3500
3501                 g_slist_free(summaryview->mlist);
3502                 summaryview->mlist = NULL;
3503         }
3504 }
3505
3506 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3507                                       gpointer data)
3508 {
3509         SummaryView *summaryview = data;
3510         MsgInfo *msginfo;
3511
3512         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3513
3514         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3515                 if (!prefs_common.immediate_exec &&
3516                     msginfo->to_folder->op_count > 0)
3517                         msginfo->to_folder->op_count--;
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                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3849                                         GTK_CTREE_FUNC(summary_filter_func),
3850                                         summaryview);
3851
3852                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3853
3854                 folderview_update_items_when_required(FALSE);
3855         }
3856
3857         debug_print("done.\n");
3858         STATUSBAR_POP(summaryview->mainwin);
3859         main_window_cursor_normal(summaryview->mainwin);
3860
3861         summary_unlock(summaryview);
3862
3863         /* 
3864          * CLAWS: summary_show() only valid after having a lock. ideally
3865          * we want the lock to be context aware...  
3866          */
3867         if (global_processing) {
3868                 summary_show(summaryview, summaryview->folder_item);
3869         }               
3870 }
3871
3872 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
3873                                 gpointer data)
3874 {
3875         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3876         SummaryView *summaryview = data;
3877         gchar *file;
3878         FolderItem *dest;
3879
3880         if (global_processing == NULL) {