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