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