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