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