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