* src/summaryview.c
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkscrolledwindow.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkpixmap.h>
27 #include <gtk/gtkctree.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtktext.h>
31 #include <gtk/gtkmenu.h>
32 #include <gtk/gtkmenuitem.h>
33 #include <gtk/gtkitemfactory.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtkwindow.h>
37 #include <gtk/gtkstyle.h>
38 #include <gtk/gtkarrow.h>
39 #include <gtk/gtkeventbox.h>
40 #include <gtk/gtkstatusbar.h>
41 #include <gtk/gtkmenuitem.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <unistd.h>
48 #include <sys/stat.h>
49 #include <regex.h>
50
51 #include "intl.h"
52 #include "main.h"
53 #include "menu.h"
54 #include "mainwindow.h"
55 #include "folderview.h"
56 #include "summaryview.h"
57 #include "messageview.h"
58 #include "foldersel.h"
59 #include "procmsg.h"
60 #include "procheader.h"
61 #include "sourcewindow.h"
62 #include "prefs_common.h"
63 #include "prefs_summary_column.h"
64 #include "prefs_filter.h"
65 #include "prefs_filtering.h"
66 #include "account.h"
67 #include "compose.h"
68 #include "utils.h"
69 #include "gtkutils.h"
70 #include "stock_pixmap.h"
71 #include "filesel.h"
72 #include "alertpanel.h"
73 #include "inputdialog.h"
74 #include "statusbar.h"
75 #include "filter.h"
76 #include "folder.h"
77 #include "colorlabel.h"
78 #include "inc.h"
79 #include "imap.h"
80 #include "addressbook.h"
81 #include "addr_compl.h"
82 #include "scoring.h"
83 #include "prefs_folder_item.h"
84 #include "filtering.h"
85 #include "string_match.h"
86 #include "toolbar.h"
87 #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));
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 #define CHANGE_FLAGS(msginfo) \
2287 { \
2288 if (msginfo->folder->folder->change_flags != NULL) \
2289 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2290                                       msginfo->folder, \
2291                                       msginfo); \
2292 }
2293
2294 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2295 {
2296         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2297 }
2298
2299 static void summary_display_msg_full(SummaryView *summaryview,
2300                                      GtkCTreeNode *row,
2301                                      gboolean new_window, gboolean all_headers)
2302 {
2303         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2304         MsgInfo *msginfo;
2305         MsgFlags flags;
2306         gchar *filename;
2307
2308         if (!new_window && summaryview->displayed == row) return;
2309         g_return_if_fail(row != NULL);
2310
2311         if (summary_is_locked(summaryview)) return;
2312         summary_lock(summaryview);
2313
2314         STATUSBAR_POP(summaryview->mainwin);
2315         GTK_EVENTS_FLUSH();
2316
2317         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2318
2319         filename = procmsg_get_message_file(msginfo);
2320         if (!filename) {
2321                 summary_unlock(summaryview);
2322                 return;
2323         }
2324         g_free(filename);
2325
2326         if (new_window || !prefs_common.mark_as_read_on_new_window) {
2327                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2328                         summaryview->newmsgs--;
2329                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2330                         summaryview->unread--;
2331                 if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2332                         procmsg_msginfo_unset_flags
2333                                 (msginfo, MSG_NEW | MSG_UNREAD, 0);
2334                         folderview_update_item(msginfo->folder, FALSE);
2335                         summary_set_row_marks(summaryview, row);
2336                         gtk_clist_thaw(GTK_CLIST(ctree));
2337                         summary_status_show(summaryview);
2338                         
2339                         flags = msginfo->flags;
2340                 }
2341         }
2342
2343         if (new_window) {
2344                 MessageView *msgview;
2345
2346                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2347                 messageview_show(msgview, msginfo, all_headers);
2348         } else {
2349                 MessageView *msgview;
2350
2351                 msgview = summaryview->messageview;
2352
2353                 summaryview->displayed = row;
2354                 if (!messageview_is_visible(msgview))
2355                         main_window_toggle_message_view(summaryview->mainwin);
2356                 messageview_show(msgview, msginfo, all_headers);
2357                 if (msgview->type == MVIEW_TEXT ||
2358                     (msgview->type == MVIEW_MIME &&
2359                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2360                       gtk_notebook_get_current_page
2361                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2362                         gtk_widget_grab_focus(summaryview->ctree);
2363                 GTK_EVENTS_FLUSH();
2364                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2365         }
2366
2367         summary_set_menu_sensitive(summaryview);
2368         toolbar_set_sensitive(summaryview->mainwin);
2369
2370         summary_unlock(summaryview);
2371 }
2372
2373 void summary_display_msg_selected(SummaryView *summaryview,
2374                                   gboolean all_headers)
2375 {
2376         if (summary_is_locked(summaryview)) return;
2377         summaryview->displayed = NULL;
2378         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2379                                  all_headers);
2380 }
2381
2382 void summary_redisplay_msg(SummaryView *summaryview)
2383 {
2384         GtkCTreeNode *node;
2385
2386         if (summaryview->displayed) {
2387                 node = summaryview->displayed;
2388                 summaryview->displayed = NULL;
2389                 summary_display_msg(summaryview, node);
2390         }
2391 }
2392
2393 void summary_open_msg(SummaryView *summaryview)
2394 {
2395         if (!summaryview->selected) return;
2396
2397         summary_display_msg_full(summaryview, summaryview->selected,
2398                                  TRUE, FALSE);
2399 }
2400
2401 void summary_view_source(SummaryView * summaryview)
2402 {
2403         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2404         MsgInfo *msginfo;
2405         SourceWindow *srcwin;
2406
2407         if (!summaryview->selected) return;
2408
2409         srcwin = source_window_create();
2410         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2411         source_window_show_msg(srcwin, msginfo);
2412         source_window_show(srcwin);
2413 }
2414
2415 void summary_reedit(SummaryView *summaryview)
2416 {
2417         MsgInfo *msginfo;
2418
2419         if (!summaryview->selected) return;
2420         if (!summaryview->folder_item) return;
2421         if (summaryview->folder_item->stype != F_OUTBOX &&
2422             summaryview->folder_item->stype != F_DRAFT  &&
2423             summaryview->folder_item->stype != F_QUEUE) return;
2424
2425         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2426                                               summaryview->selected);
2427         if (!msginfo) return;
2428
2429         compose_reedit(msginfo);
2430 }
2431
2432 void summary_step(SummaryView *summaryview, GtkScrollType type)
2433 {
2434         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2435         GtkCTreeNode *node;
2436
2437         if (summary_is_locked(summaryview)) return;
2438
2439         if (type == GTK_SCROLL_STEP_FORWARD) {
2440                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2441                 if (node)
2442                         gtkut_ctree_expand_parent_all(ctree, node);
2443                 else
2444                         return;
2445         } else {
2446                 if (summaryview->selected) {
2447                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2448                         if (!node) return;
2449                 }
2450         }
2451
2452         if (messageview_is_visible(summaryview->messageview))
2453                 summaryview->display_msg = TRUE;
2454
2455         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2456                                 type, 0.0);
2457         
2458         if(summaryview->selected) {
2459                 gtk_sctree_reanchor (GTK_SCTREE(ctree), summaryview->selected);
2460         }
2461
2462
2463 }
2464
2465 void summary_toggle_view(SummaryView *summaryview)
2466 {
2467         if (!messageview_is_visible(summaryview->messageview) &&
2468             summaryview->selected)
2469                 summary_display_msg(summaryview,
2470                                     summaryview->selected);
2471         else
2472                 main_window_toggle_message_view(summaryview->mainwin);
2473 }
2474
2475 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2476                                                 GtkCTreeNode *node)
2477 {
2478         MsgInfo *msginfo;
2479
2480         if (node) {
2481                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2482                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2483                         return TRUE;
2484                 node = GTK_CTREE_ROW(node)->children;
2485         } else
2486                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2487
2488         while (node) {
2489                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2490                         return TRUE;
2491                 node = GTK_CTREE_ROW(node)->sibling;
2492         }
2493
2494         return FALSE;
2495 }
2496
2497 static gboolean summary_have_unread_children(SummaryView *summaryview,
2498                                              GtkCTreeNode *node)
2499 {
2500         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2501
2502         if (!node) return FALSE;
2503
2504         node = GTK_CTREE_ROW(node)->children;
2505
2506         while (node) {
2507                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2508                         return TRUE;
2509                 node = GTK_CTREE_ROW(node)->sibling;
2510         }
2511
2512         return FALSE;
2513 }
2514
2515 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2516 {
2517         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2518         GtkStyle *style = NULL;
2519         MsgInfo *msginfo;
2520         MsgFlags flags;
2521         gint *col_pos = summaryview->col_pos;
2522
2523         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2524         if (!msginfo) return;
2525
2526         flags = msginfo->flags;
2527
2528         gtk_ctree_node_set_foreground(ctree, row, NULL);
2529
2530         /* set new/unread column */
2531         if (MSG_IS_IGNORE_THREAD(flags)) {
2532                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2533                                           ignorethreadxpm, ignorethreadxpmmask);
2534         } else if (MSG_IS_NEW(flags)) {
2535                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2536                                           newxpm, newxpmmask);
2537         } else if (MSG_IS_UNREAD(flags)) {
2538                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2539                                           unreadxpm, unreadxpmmask);
2540         } else if (MSG_IS_REPLIED(flags)) {
2541                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2542                                           repliedxpm, repliedxpmmask);
2543         } else if (MSG_IS_FORWARDED(flags)) {
2544                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2545                                           forwardedxpm, forwardedxpmmask);
2546         } else {
2547                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2548                                         NULL);
2549         }
2550
2551         if (prefs_common.bold_unread &&
2552             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2553              (!GTK_CTREE_ROW(row)->expanded &&
2554               GTK_CTREE_ROW(row)->children &&
2555               summary_have_unread_children(summaryview, row))))
2556                 style = bold_style;
2557
2558         /* set mark column */
2559         if (MSG_IS_DELETED(flags)) {
2560                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2561                                           deletedxpm, deletedxpmmask);
2562                 if (style)
2563                         style = bold_deleted_style;
2564                 else {
2565                         style = small_deleted_style;
2566                 }
2567                         gtk_ctree_node_set_foreground
2568                                 (ctree, row, &summaryview->color_dim);
2569         } else if (MSG_IS_MARKED(flags)) {
2570                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2571                                           markxpm, markxpmmask);
2572         } else if (MSG_IS_MOVE(flags)) {
2573                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2574                 if (style)
2575                         style = bold_marked_style;
2576                 else {
2577                         style = small_marked_style;
2578                 }
2579                         gtk_ctree_node_set_foreground
2580                                 (ctree, row, &summaryview->color_marked);
2581         } else if (MSG_IS_COPY(flags)) {
2582                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2583                 if (style)
2584                         style = bold_marked_style;
2585                 else {
2586                         style = small_marked_style;
2587                 }
2588                         gtk_ctree_node_set_foreground
2589                                 (ctree, row, &summaryview->color_marked);
2590         }
2591         else if ((global_scoring ||
2592                   summaryview->folder_item->prefs->scoring) &&
2593                  (msginfo->score >= summaryview->important_score) &&
2594                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2595                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2596                 gtk_ctree_node_set_foreground(ctree, row,
2597                                               &summaryview->color_important);
2598         } else {
2599                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2600         }
2601
2602         if (MSG_IS_LOCKED(flags)) {
2603                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2604                                           lockedxpm, lockedxpmmask);
2605         }
2606         else {
2607                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2608         }
2609
2610         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2611                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2612                                           clipkeyxpm, clipkeyxpmmask);
2613         } else if (MSG_IS_ENCRYPTED(flags)) {
2614                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2615                                           keyxpm, keyxpmmask);
2616         } else if (MSG_IS_MIME(flags)) {
2617                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2618                                           clipxpm, clipxpmmask);
2619         } else {
2620                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2621         }
2622         if (!style)
2623                 style = small_style;
2624
2625         gtk_ctree_node_set_row_style(ctree, row, style);
2626
2627         if (MSG_GET_COLORLABEL(flags))
2628                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2629 }
2630
2631 void summary_set_marks_selected(SummaryView *summaryview)
2632 {
2633         GList *cur;
2634
2635         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2636              cur = cur->next)
2637                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2638 }
2639
2640 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2641 {
2642         gboolean changed = FALSE;
2643         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2644         MsgInfo *msginfo;
2645
2646         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2647         if (MSG_IS_DELETED(msginfo->flags))
2648                 summaryview->deleted--;
2649         if (MSG_IS_MOVE(msginfo->flags)) {
2650                 summaryview->moved--;
2651                 changed = TRUE;
2652         }
2653         if (MSG_IS_COPY(msginfo->flags)) {
2654                 summaryview->copied--;
2655                 changed = TRUE;
2656         }
2657         if (changed && !prefs_common.immediate_exec) {
2658                 msginfo->to_folder->op_count--;
2659                 if (msginfo->to_folder->op_count == 0)
2660                         folderview_update_item(msginfo->to_folder, 0);
2661         }
2662         msginfo->to_folder = NULL;
2663         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2664         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2665         summary_set_row_marks(summaryview, row);
2666         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2667 }
2668
2669 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2670 {
2671         /* almost verbatim summary_mark_row(); may want a menu action? */
2672         gboolean changed = FALSE;
2673         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2674         MsgInfo *msginfo;
2675
2676         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2677         if (MSG_IS_DELETED(msginfo->flags))
2678                 summaryview->deleted--;
2679         if (MSG_IS_MOVE(msginfo->flags)) {
2680                 summaryview->moved--;
2681                 changed = TRUE;
2682         }
2683         if (MSG_IS_COPY(msginfo->flags)) {
2684                 summaryview->copied--;
2685                 changed = TRUE;
2686         }
2687         if (changed && !prefs_common.immediate_exec) {
2688                 msginfo->to_folder->op_count--;
2689                 if (msginfo->to_folder->op_count == 0)
2690                         folderview_update_item(msginfo->to_folder, 0);
2691         }
2692         msginfo->to_folder = NULL;
2693         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2694         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2695         summary_set_row_marks(summaryview, row);
2696         debug_print("Message %d is locked\n", msginfo->msgnum);
2697 }
2698
2699 void summary_mark(SummaryView *summaryview)
2700 {
2701         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2702         GList *cur;
2703
2704         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2705                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2706
2707         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2708         summary_status_show(summaryview);
2709 }
2710
2711 static void summary_mark_row_as_read(SummaryView *summaryview,
2712                                      GtkCTreeNode *row)
2713 {
2714         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2715         MsgInfo *msginfo;
2716
2717         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2718
2719         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2720                 return;
2721
2722         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2723                 summaryview->newmsgs--;
2724         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2725                 summaryview->unread--;
2726
2727         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2728         summary_set_row_marks(summaryview, row);
2729         debug_print("Message %d is marked as read\n",
2730                 msginfo->msgnum);
2731 }
2732
2733 void summary_mark_as_read(SummaryView *summaryview)
2734 {
2735         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2736         GList *cur;
2737
2738         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2739                 summary_mark_row_as_read(summaryview,
2740                                          GTK_CTREE_NODE(cur->data));
2741         folderview_update_items_when_required(FALSE);
2742
2743         summary_status_show(summaryview);
2744 }
2745
2746 void summary_mark_all_read(SummaryView *summaryview)
2747 {
2748         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2749         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2750         GtkCTreeNode *node;
2751
2752         gtk_clist_freeze(clist);
2753         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2754              node = gtkut_ctree_node_next(ctree, node))
2755                 summary_mark_row_as_read(summaryview, node);
2756         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2757              node = gtkut_ctree_node_next(ctree, node)) {
2758                 if (!GTK_CTREE_ROW(node)->expanded)
2759                         summary_set_row_marks(summaryview, node);
2760         }
2761         gtk_clist_thaw(clist);
2762         folderview_update_items_when_required(FALSE);
2763
2764         summary_status_show(summaryview);
2765 }
2766
2767 static void summary_mark_row_as_unread(SummaryView *summaryview,
2768                                        GtkCTreeNode *row)
2769 {
2770         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2771         MsgInfo *msginfo;
2772
2773         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2774         if (MSG_IS_DELETED(msginfo->flags)) {
2775                 msginfo->to_folder = NULL;
2776                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2777                 summaryview->deleted--;
2778         }
2779
2780         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2781                 summaryview->unread++;
2782
2783         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2784         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2785         debug_print("Message %d is marked as unread\n",
2786                 msginfo->msgnum);
2787
2788         summary_set_row_marks(summaryview, row);
2789 }
2790
2791 void summary_mark_as_unread(SummaryView *summaryview)
2792 {
2793         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2794         GList *cur;
2795
2796         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2797                 summary_mark_row_as_unread(summaryview,
2798                                            GTK_CTREE_NODE(cur->data));
2799         folderview_update_items_when_required(FALSE);
2800
2801         summary_status_show(summaryview);
2802 }
2803
2804 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2805 {
2806         GList * cur;
2807         gboolean found;
2808
2809         switch (summaryview->folder_item->folder->type) {
2810
2811         case F_NEWS:
2812
2813                 /*
2814                   security : checks if one the accounts correspond to
2815                   the author of the post
2816                 */
2817
2818                 found = FALSE;
2819                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2820                         PrefsAccount * account;
2821                         gchar * from_name;
2822                         
2823                         account = cur->data;
2824                         if (account->name && *account->name)
2825                                 from_name =
2826                                         g_strdup_printf("%s <%s>",
2827                                                         account->name,
2828                                                         account->address);
2829                         else
2830                                 from_name =
2831                                         g_strdup_printf("%s",
2832                                                         account->address);
2833                         
2834                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2835                                 g_free(from_name);
2836                                 found = TRUE;
2837                                 break;
2838                         }
2839                         g_free(from_name);
2840                 }
2841
2842                 if (!found) {
2843                         alertpanel_error(_("You're not the author of the article\n"));
2844                 }
2845                 
2846                 return found;
2847
2848         default:
2849                 return TRUE;
2850         }
2851 }
2852
2853 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2854 {
2855         gboolean changed = FALSE;
2856         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2857         MsgInfo *msginfo;
2858
2859         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2860
2861         if (!check_permission(summaryview, msginfo))
2862                 return;
2863
2864         if (MSG_IS_LOCKED(msginfo->flags)) return;
2865
2866         if (MSG_IS_DELETED(msginfo->flags)) return;
2867
2868         if (MSG_IS_MOVE(msginfo->flags)) {
2869                 summaryview->moved--;
2870                 changed = TRUE;
2871         }
2872         if (MSG_IS_COPY(msginfo->flags)) {
2873                 summaryview->copied--;
2874                 changed = TRUE;
2875         }
2876         if (changed && !prefs_common.immediate_exec) {
2877                 msginfo->to_folder->op_count--;
2878                 if (msginfo->to_folder->op_count == 0)
2879                         folderview_update_item(msginfo->to_folder, 0);
2880         }
2881         msginfo->to_folder = NULL;
2882         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2883         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2884         summaryview->deleted++;
2885
2886         if (!prefs_common.immediate_exec && 
2887             summaryview->folder_item->stype != F_TRASH)
2888                 summary_set_row_marks(summaryview, row);
2889
2890         debug_print("Message %s/%d is set to delete\n",
2891                     msginfo->folder->path, msginfo->msgnum);
2892 }
2893
2894 void summary_cancel(SummaryView *summaryview)
2895 {
2896         MsgInfo * msginfo;
2897         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2898
2899         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2900                                               summaryview->selected);
2901         if (!msginfo) return;
2902
2903         if (!check_permission(summaryview, msginfo))
2904                 return;
2905
2906         news_cancel_article(summaryview->folder_item->folder, msginfo);
2907         
2908         if (summary_is_locked(summaryview)) return;
2909
2910         summary_lock(summaryview);
2911
2912         gtk_clist_freeze(clist);
2913
2914         summary_update_status(summaryview);
2915         summary_status_show(summaryview);
2916
2917         gtk_clist_thaw(clist);
2918
2919         summary_unlock(summaryview);
2920 }
2921
2922 void summary_delete(SummaryView *summaryview)
2923 {
2924         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2925         FolderItem *item = summaryview->folder_item;
2926         GList *cur;
2927         GtkCTreeNode *sel_last = NULL;
2928         GtkCTreeNode *node;
2929
2930         if (!item) return;
2931 #if 0
2932         if (!item || item->folder->type == F_NEWS) return;
2933 #endif
2934
2935         if (summary_is_locked(summaryview)) return;
2936
2937         /* if current folder is trash, ask for confirmation */
2938         if (item->stype == F_TRASH) {
2939                 AlertValue aval;
2940
2941                 aval = alertpanel(_("Delete message(s)"),
2942                                   _("Do you really want to delete message(s) from the trash?"),
2943                                   _("Yes"), _("No"), NULL);
2944                 if (aval != G_ALERTDEFAULT) return;
2945         }
2946
2947         /* next code sets current row focus right. We need to find a row
2948          * that is not deleted. */
2949         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2950                 sel_last = GTK_CTREE_NODE(cur->data);
2951                 summary_delete_row(summaryview, sel_last);
2952         }
2953
2954         node = summary_find_next_msg(summaryview, sel_last);
2955         if (!node)
2956                 node = summary_find_prev_msg(summaryview, sel_last);
2957
2958         if (node) {
2959                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2960                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2961                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2962                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2963                 else
2964                         summary_select_node
2965                                 (summaryview, node,
2966                                  messageview_is_visible(summaryview->messageview),
2967                                  FALSE);
2968         }
2969
2970         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2971                 summary_execute(summaryview);
2972         else
2973                 summary_status_show(summaryview);
2974 }
2975
2976 void summary_delete_duplicated(SummaryView *summaryview)
2977 {
2978         if (!summaryview->folder_item ||
2979             summaryview->folder_item->folder->type == F_NEWS) return;
2980         if (summaryview->folder_item->stype == F_TRASH) return;
2981
2982         main_window_cursor_wait(summaryview->mainwin);
2983         debug_print("Deleting duplicated messages...");
2984         STATUSBAR_PUSH(summaryview->mainwin,
2985                        _("Deleting duplicated messages..."));
2986
2987         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2988                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2989                                 summaryview);
2990
2991         if (prefs_common.immediate_exec)
2992                 summary_execute(summaryview);
2993         else
2994                 summary_status_show(summaryview);
2995
2996         debug_print("done.\n");
2997         STATUSBAR_POP(summaryview->mainwin);
2998         main_window_cursor_normal(summaryview->mainwin);
2999 }
3000
3001 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
3002                                            SummaryView *summaryview)
3003 {
3004         GtkCTreeNode *found;
3005         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
3006
3007         if (!msginfo->msgid || !*msginfo->msgid) return;
3008
3009         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
3010
3011         if (found && found != node)
3012                 summary_delete_row(summaryview, node);
3013 }
3014
3015 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3016 {
3017         gboolean changed = FALSE;
3018         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3019         MsgInfo *msginfo;
3020
3021         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3022         if (MSG_IS_DELETED(msginfo->flags))
3023                 summaryview->deleted--;
3024         if (MSG_IS_MOVE(msginfo->flags)) {
3025                 summaryview->moved--;
3026                 changed = TRUE;
3027         }
3028         if (MSG_IS_COPY(msginfo->flags)) {
3029                 summaryview->copied--;
3030                 changed = TRUE;
3031         }
3032         if (changed && !prefs_common.immediate_exec) {
3033                 msginfo->to_folder->op_count--;
3034                 if (msginfo->to_folder->op_count == 0)
3035                         folderview_update_item(msginfo->to_folder, 0);
3036         }
3037         msginfo->to_folder = NULL;
3038         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3039         summary_set_row_marks(summaryview, row);
3040
3041         debug_print("Message %s/%d is unmarked\n",
3042                     msginfo->folder->path, msginfo->msgnum);
3043 }
3044
3045 void summary_unmark(SummaryView *summaryview)
3046 {
3047         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3048         GList *cur;
3049
3050         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3051                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3052
3053         summary_status_show(summaryview);
3054 }
3055
3056 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3057                                 FolderItem *to_folder)
3058 {
3059         gboolean changed = FALSE;
3060         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3061         MsgInfo *msginfo;
3062
3063         g_return_if_fail(to_folder != NULL);
3064
3065         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3066         if (MSG_IS_MOVE(msginfo->flags)) {
3067                 if (!prefs_common.immediate_exec) {
3068                         msginfo->to_folder->op_count--;
3069                         if (msginfo->to_folder->op_count == 0) {
3070                                 folderview_update_item(msginfo->to_folder, 0);
3071                                 changed = TRUE;
3072                         }
3073                 }
3074         }
3075         msginfo->to_folder = to_folder;
3076         if (MSG_IS_DELETED(msginfo->flags))
3077                 summaryview->deleted--;
3078         if (MSG_IS_COPY(msginfo->flags)) {
3079                 summaryview->copied--;
3080                 if (!prefs_common.immediate_exec)
3081                         msginfo->to_folder->op_count--;
3082         }
3083         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3084         if (!MSG_IS_MOVE(msginfo->flags)) {
3085                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3086                 summaryview->moved++;
3087                 changed = TRUE;
3088         }
3089         if (!prefs_common.immediate_exec) {
3090                 summary_set_row_marks(summaryview, row);
3091                 if (changed) {
3092                         msginfo->to_folder->op_count++;
3093                         if (msginfo->to_folder->op_count == 1)
3094                                 folderview_update_item(msginfo->to_folder, 0);
3095                 }
3096         }
3097
3098         debug_print("Message %d is set to move to %s\n",
3099                     msginfo->msgnum, to_folder->path);
3100 }
3101
3102 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3103 {
3104         GList *cur;
3105
3106         if (!to_folder) return;
3107         if (!summaryview->folder_item ||
3108             summaryview->folder_item->folder->type == F_NEWS) return;
3109
3110         if (summary_is_locked(summaryview)) return;
3111
3112         if (summaryview->folder_item == to_folder) {
3113                 alertpanel_notice(_("Destination is same as current folder."));
3114                 return;
3115         }
3116
3117         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3118              cur != NULL; cur = cur->next)
3119                 summary_move_row_to
3120                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3121
3122         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3123
3124         if (prefs_common.immediate_exec)
3125                 summary_execute(summaryview);
3126         else {
3127                 summary_status_show(summaryview);
3128
3129                 folderview_update_item(to_folder, 0);
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);