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