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