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