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