ac2fbda191d68b3e3e483cd5e04f8c9a3d36a83c
[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, TRUE, TRUE);
1148                         summary_lock(summaryview);
1149                 } else
1150                         summary_select_node(summaryview, node, FALSE, TRUE);
1151         }
1152
1153         summary_set_column_titles(summaryview);
1154         summary_status_show(summaryview);
1155         summary_set_menu_sensitive(summaryview);
1156         toolbar_main_set_sensitive(summaryview->mainwin);
1157
1158         debug_print("\n");
1159         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1160         STATUSBAR_POP(summaryview->mainwin);
1161         main_window_cursor_normal(summaryview->mainwin);
1162         summary_unlock(summaryview);
1163         inc_unlock();
1164
1165         return TRUE;
1166 }
1167
1168 #undef CURRENTLY_DISPLAYED
1169
1170 void summary_clear_list(SummaryView *summaryview)
1171 {
1172         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1173         gint optimal_width;
1174
1175         gtk_clist_freeze(clist);
1176
1177         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1178                                 NULL, summary_free_msginfo_func, NULL);
1179
1180         if (summaryview->folder_item) {
1181                 summaryview->folder_item->opened = FALSE;
1182                 summaryview->folder_item = NULL;
1183         }
1184
1185         summaryview->display_msg = FALSE;
1186
1187         summaryview->selected = NULL;
1188         summaryview->displayed = NULL;
1189         summaryview->total_size = 0;
1190         summaryview->deleted = summaryview->moved = 0;
1191         summaryview->copied = 0;
1192         if (summaryview->msgid_table) {
1193                 g_hash_table_destroy(summaryview->msgid_table);
1194                 summaryview->msgid_table = NULL;
1195         }
1196         if (summaryview->subject_table) {
1197                 g_hash_table_destroy(summaryview->subject_table);
1198                 summaryview->subject_table = NULL;
1199         }
1200         summaryview->mlist = NULL;
1201
1202         gtk_clist_clear(clist);
1203         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1204                 optimal_width = gtk_clist_optimal_column_width
1205                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1206                 gtk_clist_set_column_width
1207                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1208                          optimal_width);
1209         }
1210
1211         gtk_clist_thaw(clist);
1212 }
1213
1214 void summary_clear_all(SummaryView *summaryview)
1215 {
1216         messageview_clear(summaryview->messageview);
1217         summary_clear_list(summaryview);
1218         summary_set_menu_sensitive(summaryview);
1219         toolbar_main_set_sensitive(summaryview->mainwin);
1220         summary_status_show(summaryview);
1221 }
1222
1223 void summary_lock(SummaryView *summaryview)
1224 {
1225         summaryview->lock_count++;
1226 }
1227
1228 void summary_unlock(SummaryView *summaryview)
1229 {
1230         if (summaryview->lock_count)
1231                 summaryview->lock_count--;
1232 }
1233
1234 gboolean summary_is_locked(SummaryView *summaryview)
1235 {
1236         return summaryview->lock_count > 0;
1237 }
1238
1239 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1240 {
1241         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1242         SummarySelection selection;
1243
1244         if (!clist->row_list)
1245                 selection = SUMMARY_NONE;
1246         else if (!clist->selection)
1247                 selection = SUMMARY_SELECTED_NONE;
1248         else if (!clist->selection->next)
1249                 selection = SUMMARY_SELECTED_SINGLE;
1250         else
1251                 selection = SUMMARY_SELECTED_MULTIPLE;
1252
1253         return selection;
1254 }
1255
1256 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1257 {
1258         GSList *mlist = NULL;
1259         GList *cur;
1260         MsgInfo *msginfo;
1261
1262         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
1263              cur = cur->next) {
1264                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1265                 mlist = g_slist_prepend(mlist, msginfo);
1266         }
1267
1268         mlist = g_slist_reverse(mlist);
1269
1270         return mlist;
1271 }
1272
1273 GSList *summary_get_msg_list(SummaryView *summaryview)
1274 {
1275         GSList *mlist = NULL;
1276         GtkCTree *ctree;
1277         GtkCTreeNode *node;
1278         MsgInfo *msginfo;
1279
1280         ctree = GTK_CTREE(summaryview->ctree);
1281
1282         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1283              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1284                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1285                 mlist = g_slist_prepend(mlist, msginfo);
1286         }
1287
1288         mlist = g_slist_reverse(mlist);
1289
1290         return mlist;
1291 }
1292
1293 static void summary_set_menu_sensitive(SummaryView *summaryview)
1294 {
1295         GtkItemFactory *ifactory = summaryview->popupfactory;
1296         SensitiveCond state;
1297         gboolean sensitive;
1298         GtkWidget *menuitem;
1299         gint i;
1300
1301         static const struct {
1302                 gchar *const entry;
1303                 SensitiveCond cond;
1304         } entry[] = {
1305                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1306                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1307                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1308                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1309                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1310                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1311
1312                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1313                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1314
1315                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1316
1317                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1318                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1319                 {"/Delete"                      , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1320                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1321                 {"/Execute"                     , M_DELAY_EXEC},
1322
1323                 {"/Mark"                        , M_TARGET_EXIST},
1324                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1325                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1326                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1327                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1328                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1329                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1330                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1331                 {"/Color label"                 , M_TARGET_EXIST},
1332
1333                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1334                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1335
1336                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1337                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1338                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1339                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1340
1341                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1342                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1343
1344                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1345                 {"/Select all"                  , M_TARGET_EXIST},
1346
1347                 {NULL, 0}
1348         };
1349
1350         main_window_set_menu_sensitive(summaryview->mainwin);
1351
1352         state = main_window_get_current_state(summaryview->mainwin);
1353
1354         for (i = 0; entry[i].entry != NULL; i++) {
1355                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1356                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1357         }
1358
1359
1360         summary_lock(summaryview);
1361         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1362         gtk_check_menu_item_set_active
1363                 (GTK_CHECK_MENU_ITEM(menuitem),
1364                  summaryview->messageview->textview->show_all_headers);
1365         summary_unlock(summaryview);
1366 }
1367
1368 void summary_select_prev_unread(SummaryView *summaryview)
1369 {
1370         GtkCTreeNode *node;
1371
1372         node = summary_find_prev_flagged_msg
1373                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1374
1375         if (!node) {
1376                 AlertValue val = 0;
1377
1378                 switch (prefs_common.next_unread_msg_dialog) {
1379                         case NEXTUNREADMSGDIALOG_ALWAYS:
1380                                 val = alertpanel(_("No more unread messages"),
1381                                                  _("No unread message found. "
1382                                                    "Search from the end?"),
1383                                                  _("Yes"), _("No"), NULL);
1384                                 break;
1385                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1386                                 val = G_ALERTDEFAULT;
1387                                 break;
1388                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1389                                 val = !G_ALERTDEFAULT;
1390                                 break;
1391                         default:
1392                                 debug_print(
1393                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1394                 }
1395                 if (val != G_ALERTDEFAULT) return;
1396                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1397                                                      MSG_UNREAD, FALSE);
1398         }
1399
1400         if (!node)
1401                 alertpanel_notice(_("No unread messages."));
1402         else
1403                 summary_select_node(summaryview, node, TRUE, FALSE);
1404 }
1405
1406 void summary_select_next_unread(SummaryView *summaryview)
1407 {
1408         GtkCTreeNode *node = summaryview->selected;
1409         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1410
1411         node = summary_find_next_flagged_msg
1412                 (summaryview, node, MSG_UNREAD, FALSE);
1413         
1414         if (node)
1415                 summary_select_node(summaryview, node, TRUE, FALSE);
1416         else {
1417                 node = summary_find_next_flagged_msg
1418                         (summaryview, NULL, MSG_UNREAD, FALSE);
1419                 if (node == NULL) {
1420                         AlertValue val = 0;
1421
1422                         switch (prefs_common.next_unread_msg_dialog) {
1423                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1424                                         val = alertpanel(_("No more unread messages"),
1425                                                          _("No unread message found. "
1426                                                            "Go to next folder?"),
1427                                                          _("Yes"), _("No"), NULL);
1428                                         break;
1429                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1430                                         val = G_ALERTDEFAULT;
1431                                         break;
1432                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1433                                         val = G_ALERTOTHER;
1434                                         break;
1435                                 default:
1436                                         debug_print(
1437                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1438                         }
1439
1440                         if (val == G_ALERTDEFAULT) {
1441                                 if (gtk_signal_n_emissions_by_name
1442                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1443                                                 gtk_signal_emit_stop_by_name
1444                                                         (GTK_OBJECT(ctree),
1445                                                          "key_press_event");
1446                                 folderview_select_next_unread(summaryview->folderview);
1447                                 return;
1448                         } 
1449                         else
1450                                 return;
1451                 } else
1452                         summary_select_node(summaryview, node, TRUE, FALSE);
1453
1454         }
1455 }
1456
1457 void summary_select_prev_new(SummaryView *summaryview)
1458 {
1459         GtkCTreeNode *node;
1460
1461         node = summary_find_prev_flagged_msg
1462                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1463
1464         if (!node) {
1465                 AlertValue val;
1466
1467                 val = alertpanel(_("No more new messages"),
1468                                  _("No new message found. "
1469                                    "Search from the end?"),
1470                                  _("Yes"), _("No"), NULL);
1471                 if (val != G_ALERTDEFAULT) return;
1472                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1473                                                      MSG_NEW, FALSE);
1474         }
1475
1476         if (!node)
1477                 alertpanel_notice(_("No new messages."));
1478         else
1479                 summary_select_node(summaryview, node, TRUE, FALSE);
1480 }
1481
1482 void summary_select_next_new(SummaryView *summaryview)
1483 {
1484         GtkCTreeNode *node = summaryview->selected;
1485         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1486
1487         while ((node = summary_find_next_flagged_msg
1488                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1489                 AlertValue val;
1490
1491                 val = alertpanel(_("No more new messages"),
1492                                  _("No new message found. "
1493                                    "Go to next folder?"),
1494                                  _("Yes"), _("Search again"), _("No"));
1495                 if (val == G_ALERTDEFAULT) {
1496                         if (gtk_signal_n_emissions_by_name
1497                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1498                                         gtk_signal_emit_stop_by_name
1499                                                 (GTK_OBJECT(ctree),
1500                                                  "key_press_event");
1501                         folderview_select_next_unread(summaryview->folderview);
1502                         return;
1503                 } else if (val == G_ALERTALTERNATE)
1504                         node = NULL;
1505                 else
1506                         return;
1507         }
1508
1509         if (node)
1510                 summary_select_node(summaryview, node, TRUE, FALSE);
1511 }
1512
1513 void summary_select_prev_marked(SummaryView *summaryview)
1514 {
1515         GtkCTreeNode *node;
1516
1517         node = summary_find_prev_flagged_msg
1518                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1519
1520         if (!node) {
1521                 AlertValue val;
1522
1523                 val = alertpanel(_("No more marked messages"),
1524                                  _("No marked message found. "
1525                                    "Search from the end?"),
1526                                  _("Yes"), _("No"), NULL);
1527                 if (val != G_ALERTDEFAULT) return;
1528                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1529                                                      MSG_MARKED, TRUE);
1530         }
1531
1532         if (!node)
1533                 alertpanel_notice(_("No marked messages."));
1534         else
1535                 summary_select_node(summaryview, node, TRUE, FALSE);
1536 }
1537
1538 void summary_select_next_marked(SummaryView *summaryview)
1539 {
1540         GtkCTreeNode *node;
1541
1542         node = summary_find_next_flagged_msg
1543                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1544
1545         if (!node) {
1546                 AlertValue val;
1547
1548                 val = alertpanel(_("No more marked messages"),
1549                                  _("No marked message found. "
1550                                    "Search from the beginning?"),
1551                                  _("Yes"), _("No"), NULL);
1552                 if (val != G_ALERTDEFAULT) return;
1553                 node = summary_find_next_flagged_msg(summaryview, NULL,
1554                                                      MSG_MARKED, TRUE);
1555         }
1556
1557         if (!node)
1558                 alertpanel_notice(_("No marked messages."));
1559         else
1560                 summary_select_node(summaryview, node, TRUE, FALSE);
1561 }
1562
1563 void summary_select_prev_labeled(SummaryView *summaryview)
1564 {
1565         GtkCTreeNode *node;
1566
1567         node = summary_find_prev_flagged_msg
1568                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1569
1570         if (!node) {
1571                 AlertValue val;
1572
1573                 val = alertpanel(_("No more labeled messages"),
1574                                  _("No labeled message found. "
1575                                    "Search from the end?"),
1576                                  _("Yes"), _("No"), NULL);
1577                 if (val != G_ALERTDEFAULT) return;
1578                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1579                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1580         }
1581
1582         if (!node)
1583                 alertpanel_notice(_("No labeled messages."));
1584         else
1585                 summary_select_node(summaryview, node, TRUE, FALSE);
1586 }
1587
1588 void summary_select_next_labeled(SummaryView *summaryview)
1589 {
1590         GtkCTreeNode *node;
1591
1592         node = summary_find_next_flagged_msg
1593                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1594
1595         if (!node) {
1596                 AlertValue val;
1597
1598                 val = alertpanel(_("No more labeled messages"),
1599                                  _("No labeled message found. "
1600                                    "Search from the beginning?"),
1601                                  _("Yes"), _("No"), NULL);
1602                 if (val != G_ALERTDEFAULT) return;
1603                 node = summary_find_next_flagged_msg(summaryview, NULL,
1604                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1605         }
1606
1607         if (!node)
1608                 alertpanel_notice(_("No labeled messages."));
1609         else
1610                 summary_select_node(summaryview, node, TRUE, FALSE);
1611 }
1612
1613 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1614 {
1615         GtkCTreeNode *node;
1616
1617         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1618         summary_select_node(summaryview, node, FALSE, TRUE);
1619 }
1620
1621 /**
1622  * summary_select_node:
1623  * @summaryview: Summary view.
1624  * @node: Summary tree node.
1625  * @display_msg: TRUE to display the selected message.
1626  * @do_refresh: TRUE to refresh the widget.
1627  *
1628  * Select @node (bringing it into view by scrolling and expanding its
1629  * thread, if necessary) and unselect all others.  If @display_msg is
1630  * TRUE, display the corresponding message in the message view.
1631  * If @do_refresh is TRUE, the widget is refreshed.
1632  **/
1633 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1634                          gboolean display_msg, gboolean do_refresh)
1635 {
1636         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1637
1638         if (node) {
1639                 gtkut_ctree_expand_parent_all(ctree, node);
1640                 if (do_refresh) {
1641                         GTK_EVENTS_FLUSH();
1642                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1643                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1644                 }
1645                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1646                 if (display_msg && summaryview->displayed == node)
1647                         summaryview->displayed = NULL;
1648                 summaryview->display_msg = display_msg;
1649                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1650         }
1651 }
1652
1653 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1654 {
1655         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1656         MsgInfo *msginfo;
1657
1658         if (!node)
1659                 return 0;
1660         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1661         return msginfo->msgnum;
1662 }
1663
1664 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1665                                            GtkCTreeNode *current_node)
1666 {
1667         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1668         GtkCTreeNode *node;
1669         MsgInfo *msginfo;
1670
1671         if (current_node)
1672                 node = current_node;
1673         else
1674                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1675
1676         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1677                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1678                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1679         }
1680
1681         return node;
1682 }
1683
1684 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1685                                            GtkCTreeNode *current_node)
1686 {
1687         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1688         GtkCTreeNode *node;
1689         MsgInfo *msginfo;
1690
1691         if (current_node)
1692                 node = current_node;
1693         else
1694                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1695
1696         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1697                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1698                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1699         }
1700
1701         return node;
1702 }
1703
1704 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1705                                                    GtkCTreeNode *current_node,
1706                                                    MsgPermFlags flags,
1707                                                    gboolean start_from_prev)
1708 {
1709         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1710         GtkCTreeNode *node;
1711         MsgInfo *msginfo;
1712
1713         if (current_node) {
1714                 if (start_from_prev)
1715                         node = GTK_CTREE_NODE_PREV(current_node);
1716                 else
1717                         node = current_node;
1718         } else
1719                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1720
1721         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1722                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1723                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1724         }
1725
1726         return node;
1727 }
1728
1729 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1730                                                    GtkCTreeNode *current_node,
1731                                                    MsgPermFlags flags,
1732                                                    gboolean start_from_next)
1733 {
1734         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1735         GtkCTreeNode *node;
1736         MsgInfo *msginfo;
1737
1738         if (current_node) {
1739                 if (start_from_next)
1740                         node = gtkut_ctree_node_next(ctree, current_node);
1741                 else
1742                         node = current_node;
1743         } else
1744                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1745
1746         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1747                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1748                 /* Find msg with matching flags but ignore messages with
1749                    ignore flags, if searching for new or unread messages */
1750                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1751                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1752                         break;
1753         }
1754
1755         return node;
1756 }
1757
1758 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1759                                                 guint msgnum)
1760 {
1761         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1762         GtkCTreeNode *node;
1763         MsgInfo *msginfo;
1764
1765         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1766
1767         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1768                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1769                 if (msginfo && msginfo->msgnum == msgnum) break;
1770         }
1771
1772         return node;
1773 }
1774
1775 static guint attract_hash_func(gconstpointer key)
1776 {
1777         gchar *str;
1778         gchar *p;
1779         guint h;
1780
1781         Xstrdup_a(str, (const gchar *)key, return 0);
1782         trim_subject(str);
1783
1784         p = str;
1785         h = *p;
1786
1787         if (h) {
1788                 for (p += 1; *p != '\0'; p++)
1789                         h = (h << 5) - h + *p;
1790         }
1791
1792         return h;
1793 }
1794
1795 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1796 {
1797         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1798 }
1799
1800 void summary_attract_by_subject(SummaryView *summaryview)
1801 {
1802         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1803         GtkCList *clist = GTK_CLIST(ctree);
1804         GtkCTreeNode *src_node;
1805         GtkCTreeNode *dst_node, *sibling;
1806         GtkCTreeNode *tmp;
1807         MsgInfo *src_msginfo, *dst_msginfo;
1808         GHashTable *subject_table;
1809
1810         debug_print("Attracting messages by subject...");
1811         STATUSBAR_PUSH(summaryview->mainwin,
1812                        _("Attracting messages by subject..."));
1813
1814         main_window_cursor_wait(summaryview->mainwin);
1815         gtk_clist_freeze(clist);
1816
1817         subject_table = g_hash_table_new(attract_hash_func,
1818                                          attract_compare_func);
1819
1820         for (src_node = GTK_CTREE_NODE(clist->row_list);
1821              src_node != NULL;
1822              src_node = tmp) {
1823                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1824                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1825                 if (!src_msginfo) continue;
1826                 if (!src_msginfo->subject) continue;
1827
1828                 /* find attracting node */
1829                 dst_node = g_hash_table_lookup(subject_table,
1830                                                src_msginfo->subject);
1831
1832                 if (dst_node) {
1833                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1834
1835                         /* if the time difference is more than 20 days,
1836                            don't attract */
1837                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1838                             > 60 * 60 * 24 * 20)
1839                                 continue;
1840
1841                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1842                         if (src_node != sibling)
1843                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1844                 }
1845
1846                 g_hash_table_insert(subject_table,
1847                                     src_msginfo->subject, src_node);
1848         }
1849
1850         g_hash_table_destroy(subject_table);
1851
1852         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1853
1854         gtk_clist_thaw(clist);
1855
1856         debug_print("done.\n");
1857         STATUSBAR_POP(summaryview->mainwin);
1858
1859         main_window_cursor_normal(summaryview->mainwin);
1860 }
1861
1862 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1863                                       gpointer data)
1864 {
1865         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1866
1867         if (msginfo)
1868                 procmsg_msginfo_free(msginfo);
1869 }
1870
1871 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1872                                    gpointer data)
1873 {
1874         SummaryView *summaryview = data;
1875         MsgInfo *msginfo;
1876
1877         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1878
1879         if (MSG_IS_NEWS(msginfo->flags))
1880                 news_flag_crosspost(msginfo);
1881
1882         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1883         && procmsg_msg_has_marked_parent(msginfo))
1884                 summaryview->unreadmarked++;
1885
1886         if (MSG_IS_DELETED(msginfo->flags))
1887                 summaryview->deleted++;
1888
1889         summaryview->total_size += msginfo->size;
1890
1891         summary_set_row_marks(summaryview, node);
1892 }
1893
1894 static void summary_update_status(SummaryView *summaryview)
1895 {
1896         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1897         GtkCTreeNode *node;
1898         MsgInfo *msginfo;
1899
1900         summaryview->total_size =
1901         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1902
1903         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1904              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1905                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1906
1907                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1908                 && procmsg_msg_has_marked_parent(msginfo))
1909                         summaryview->unreadmarked++;
1910                 if (MSG_IS_DELETED(msginfo->flags))
1911                         summaryview->deleted++;
1912                 if (MSG_IS_MOVE(msginfo->flags))
1913                         summaryview->moved++;
1914                 if (MSG_IS_COPY(msginfo->flags))
1915                         summaryview->copied++;
1916                 summaryview->total_size += msginfo->size;
1917         }
1918 }
1919
1920 static void summary_status_show(SummaryView *summaryview)
1921 {
1922         gchar *str;
1923         gchar *del, *mv, *cp;
1924         gchar *sel;
1925         gchar *spc;
1926         gchar *itstr;
1927         GList *rowlist, *cur;
1928         guint n_selected = 0;
1929         off_t sel_size = 0;
1930         MsgInfo *msginfo;
1931         gchar *name;
1932
1933         if (!summaryview->folder_item) {
1934                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1935                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1936                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1937                 return;
1938         }
1939
1940         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1941         for (cur = rowlist; cur != NULL; cur = cur->next) {
1942                 msginfo = gtk_ctree_node_get_row_data
1943                         (GTK_CTREE(summaryview->ctree),
1944                          GTK_CTREE_NODE(cur->data));
1945                 if (!msginfo)
1946                         g_warning("summary_status_show(): msginfo == NULL\n");
1947                 else {
1948                         sel_size += msginfo->size;
1949                         n_selected++;
1950                 }
1951         }
1952
1953         name = folder_item_get_name(summaryview->folder_item);
1954         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), name);
1955         g_free(name);
1956
1957         if (summaryview->deleted)
1958                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1959         else
1960                 del = g_strdup("");
1961         if (summaryview->moved)
1962                 mv = g_strdup_printf(_("%s%d moved"),
1963                                      summaryview->deleted ? _(", ") : "",
1964                                      summaryview->moved);
1965         else
1966                 mv = g_strdup("");
1967         if (summaryview->copied)
1968                 cp = g_strdup_printf(_("%s%d copied"),
1969                                      summaryview->deleted ||
1970                                      summaryview->moved ? _(", ") : "",
1971                                      summaryview->copied);
1972         else
1973                 cp = g_strdup("");
1974
1975         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1976                 spc = "    ";
1977         else
1978                 spc = "";
1979
1980         if (n_selected) {
1981                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1982                 if (n_selected == 1)
1983                         itstr = g_strdup(_(" item selected"));
1984                 else
1985                         itstr = g_strdup(_(" items selected"));
1986         } else {
1987                 sel = g_strdup("");
1988                 itstr = g_strdup("");
1989         }
1990                 
1991         str = g_strconcat(n_selected ? itos(n_selected) : "",
1992                                         itstr, sel, spc, del, mv, cp, NULL);
1993         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1994         g_free(str);
1995         g_free(sel);
1996         g_free(del);
1997         g_free(mv);
1998         g_free(cp);
1999         g_free(itstr);
2000
2001         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2002
2003                                       summaryview->folder_item->new_msgs,
2004                                       summaryview->folder_item->unread_msgs,
2005                                       summaryview->folder_item->total_msgs,
2006                                       to_human_readable(summaryview->total_size));
2007         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
2008         g_free(str);
2009 }
2010
2011 static void summary_set_column_titles(SummaryView *summaryview)
2012 {
2013         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2014         GtkWidget *hbox;
2015         GtkWidget *label;
2016         GtkWidget *arrow;
2017         gint pos;
2018         const gchar *title;
2019         SummaryColumnType type;
2020         gboolean single_char;
2021         GtkJustification justify;
2022
2023         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2024                 SORT_BY_MARK,
2025                 SORT_BY_STATUS,
2026                 SORT_BY_MIME,
2027                 SORT_BY_SUBJECT,
2028                 SORT_BY_FROM,
2029                 SORT_BY_DATE,
2030                 SORT_BY_SIZE,
2031                 SORT_BY_NUMBER,
2032                 SORT_BY_SCORE,
2033                 SORT_BY_LOCKED
2034         };
2035
2036         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2037                 type = summaryview->col_state[pos].type;
2038
2039                 /* CLAWS: mime and unread are single char headers */
2040                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
2041                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2042                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2043
2044                 switch (type) {
2045                 case S_COL_SUBJECT:
2046                 case S_COL_FROM:
2047                 case S_COL_DATE:
2048                 case S_COL_NUMBER:
2049                         if (prefs_common.trans_hdr)
2050                                 title = gettext(col_label[type]);
2051                         else
2052                                 title = col_label[type];
2053                         break;
2054                 /* CLAWS: dummies for mark and locked headers */        
2055                 case S_COL_MARK:        
2056                 case S_COL_LOCKED:
2057                         title = "";
2058                         break;
2059                 default:
2060                         title = gettext(col_label[type]);
2061                 }
2062
2063                 if (type == S_COL_MIME) {
2064                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
2065                         gtk_widget_show(label);
2066                         gtk_clist_set_column_widget(clist, pos, label);
2067                         continue;
2068                 }
2069                 if (single_char) {
2070                         gtk_clist_set_column_title(clist, pos, title);
2071                         continue;
2072                 }
2073
2074                 /* CLAWS: changed so that locked and mark headers
2075                  * show a pixmap instead of single character */
2076                 hbox  = gtk_hbox_new(FALSE, 4);
2077                 
2078                 if (type == S_COL_LOCKED)
2079                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
2080                 else if (type == S_COL_MARK) 
2081                         label = gtk_pixmap_new(markxpm, markxpmmask);
2082                 else 
2083                         label = gtk_label_new(title);
2084                 
2085                 if (justify == GTK_JUSTIFY_RIGHT)
2086                         gtk_box_pack_end(GTK_BOX(hbox), label,
2087                                          FALSE, FALSE, 0);
2088                 else
2089                         gtk_box_pack_start(GTK_BOX(hbox), label,
2090                                            FALSE, FALSE, 0);
2091
2092                 if (summaryview->sort_key == sort_by[type]) {
2093                         arrow = gtk_arrow_new
2094                                 (summaryview->sort_type == SORT_ASCENDING
2095                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2096                                  GTK_SHADOW_IN);
2097                         if (justify == GTK_JUSTIFY_RIGHT)
2098                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2099                                                    FALSE, FALSE, 0);
2100                         else
2101                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2102                                                  FALSE, FALSE, 0);
2103                 }
2104
2105                 gtk_widget_show_all(hbox);
2106                 gtk_clist_set_column_widget(clist, pos, hbox);
2107         }
2108 }
2109
2110 void summary_sort(SummaryView *summaryview,
2111                   FolderSortKey sort_key, FolderSortType sort_type)
2112 {
2113         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2114         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2115         GtkCListCompareFunc cmp_func = NULL;
2116
2117         switch (sort_key) {
2118         case SORT_BY_MARK:
2119                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2120                 break;
2121         case SORT_BY_STATUS:
2122                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2123                 break;
2124         case SORT_BY_MIME:
2125                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2126                 break;
2127         case SORT_BY_NUMBER:
2128                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2129                 break;
2130         case SORT_BY_SIZE:
2131                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2132                 break;
2133         case SORT_BY_DATE:
2134                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2135                 break;
2136         case SORT_BY_FROM:
2137                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2138                 break;
2139         case SORT_BY_SUBJECT:
2140                 if (summaryview->simplify_subject_preg)
2141                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2142                 else
2143                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2144                 break;
2145         case SORT_BY_SCORE:
2146                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2147                 break;
2148         case SORT_BY_LABEL:
2149                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2150                 break;
2151         case SORT_BY_TO:
2152                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2153                 break;
2154         case SORT_BY_LOCKED:
2155                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2156                 break;
2157         case SORT_BY_NONE:
2158                 cmp_func = NULL;
2159                 return;
2160         default:
2161                 return;
2162         }
2163
2164         summaryview->sort_key = sort_key;
2165         summaryview->sort_type = sort_type;
2166
2167         summary_set_column_titles(summaryview);
2168         summary_set_menu_sensitive(summaryview);
2169         if(cmp_func != NULL) {
2170                 debug_print("Sorting summary...");
2171                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2172
2173                 main_window_cursor_wait(summaryview->mainwin);
2174
2175                 gtk_clist_set_compare_func(clist, cmp_func);
2176
2177                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2178
2179                 gtk_sctree_sort_recursive(ctree, NULL);
2180
2181                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2182
2183                 main_window_cursor_normal(summaryview->mainwin);
2184
2185                 debug_print("done.\n");
2186                 STATUSBAR_POP(summaryview->mainwin);
2187         }
2188 }
2189
2190 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2191                                    GtkCTreeNode *cnode, gpointer data)
2192 {
2193         SummaryView *summaryview = (SummaryView *)data;
2194         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2195         gchar *text[N_SUMMARY_COLS];
2196         gint *col_pos = summaryview->col_pos;
2197         const gchar *msgid = msginfo->msgid;
2198         GHashTable *msgid_table = summaryview->msgid_table;
2199
2200         summary_set_header(summaryview, text, msginfo);
2201
2202         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2203                                 NULL, NULL, NULL, NULL, FALSE,
2204                                 gnode->parent->parent ? TRUE : FALSE);
2205 #define SET_TEXT(col) \
2206         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2207                                 text[col_pos[col]])
2208
2209         SET_TEXT(S_COL_NUMBER);
2210         SET_TEXT(S_COL_SCORE);
2211         SET_TEXT(S_COL_SIZE);
2212         SET_TEXT(S_COL_DATE);
2213         SET_TEXT(S_COL_FROM);
2214         SET_TEXT(S_COL_SUBJECT);
2215
2216 #undef SET_TEXT
2217
2218         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2219         summary_set_marks_func(ctree, cnode, summaryview);
2220
2221         if (msgid && msgid[0] != '\0')
2222                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2223
2224         return TRUE;
2225 }
2226
2227 static void summary_set_ctree_from_list(SummaryView *summaryview,
2228                                         GSList *mlist)
2229 {
2230         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2231         MsgInfo *msginfo;
2232                 GtkCTreeNode *node = NULL;
2233         GHashTable *msgid_table;
2234         GHashTable *subject_table;
2235         GSList * cur;
2236         
2237         if (!mlist) return;
2238
2239         debug_print("\tSetting summary from message data...");
2240         STATUSBAR_PUSH(summaryview->mainwin,
2241                        _("Setting summary from message data..."));
2242         gdk_flush();
2243
2244         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2245         summaryview->msgid_table = msgid_table;
2246         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2247         summaryview->subject_table = subject_table;
2248
2249         if (prefs_common.use_addr_book)
2250                 start_address_completion();
2251         
2252         for (cur = mlist ; cur != NULL; cur = cur->next) {
2253                 msginfo = (MsgInfo *)cur->data;
2254                 msginfo->threadscore = msginfo->score;
2255         }
2256
2257         if (summaryview->threaded) {
2258                 GNode *root, *gnode;
2259
2260                 root = procmsg_get_thread_tree(mlist);
2261
2262                 for (gnode = root->children; gnode != NULL;
2263                      gnode = gnode->next) {
2264                         node = gtk_ctree_insert_gnode
2265                                 (ctree, NULL, node, gnode,
2266                                  summary_insert_gnode_func, summaryview);
2267                 }
2268
2269                 g_node_destroy(root);
2270
2271                 summary_thread_init(summaryview);
2272         } else {
2273                 gchar *text[N_SUMMARY_COLS];
2274                 cur = mlist;
2275                 for (; mlist != NULL; mlist = mlist->next) {
2276                         msginfo = (MsgInfo *)mlist->data;
2277
2278                         summary_set_header(summaryview, text, msginfo);
2279
2280                         node = gtk_ctree_insert_node
2281                                 (ctree, NULL, node, text, 2,
2282                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2283                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2284                         summary_set_marks_func(ctree, node, summaryview);
2285
2286                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2287                                 g_hash_table_insert(msgid_table,
2288                                                     msginfo->msgid, node);
2289
2290                         subject_table_insert(subject_table,
2291                                              msginfo->subject,
2292                                              node);
2293                 }
2294                 mlist = cur;
2295         }
2296
2297         if (prefs_common.enable_hscrollbar &&
2298             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2299                 gint optimal_width;
2300
2301                 optimal_width = gtk_clist_optimal_column_width
2302                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2303                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2304                                            summaryview->col_pos[S_COL_SUBJECT],
2305                                            optimal_width);
2306         }
2307
2308         if (prefs_common.use_addr_book)
2309                 end_address_completion();
2310
2311         debug_print("done.\n");
2312         STATUSBAR_POP(summaryview->mainwin);
2313         if (debug_get_mode()) {
2314                 debug_print("\tmsgid hash table size = %d\n",
2315                             g_hash_table_size(msgid_table));
2316                 debug_print("\tsubject hash table size = %d\n",
2317                             g_hash_table_size(subject_table));
2318         }
2319 }
2320
2321 static gchar *summary_complete_address(const gchar *addr)
2322 {
2323         gint count;
2324         gchar *res, *tmp, *email_addr;
2325
2326         Xstrdup_a(email_addr, addr, return NULL);
2327         extract_address(email_addr);
2328         g_return_val_if_fail(*email_addr, NULL);
2329
2330         /*
2331          * completion stuff must be already initialized
2332          */
2333         res = NULL;
2334         if (1 < (count = complete_address(email_addr))) {
2335                 tmp = get_complete_address(1);
2336 /*      tmp = addressbook_lookup_name( email_addr );
2337         if( tmp ) { */
2338                 res = procheader_get_fromname(tmp);
2339                 g_free(tmp);
2340         }
2341
2342         return res;
2343 }
2344
2345 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2346                                MsgInfo *msginfo)
2347 {
2348         static gchar date_modified[80];
2349         static gchar *to = NULL;
2350         static gchar col_score[11];
2351         static gchar buf[BUFFSIZE];
2352         gint *col_pos = summaryview->col_pos;
2353
2354         text[col_pos[S_COL_MARK]]   = NULL;
2355         text[col_pos[S_COL_STATUS]] = NULL;
2356         text[col_pos[S_COL_MIME]]   = NULL;
2357         text[col_pos[S_COL_LOCKED]] = NULL;
2358         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2359         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2360         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2361
2362         if (msginfo->date_t) {
2363                 procheader_date_get_localtime(date_modified,
2364                                               sizeof(date_modified),
2365                                               msginfo->date_t);
2366                 text[col_pos[S_COL_DATE]] = date_modified;
2367         } else if (msginfo->date)
2368                 text[col_pos[S_COL_DATE]] = msginfo->date;
2369         else
2370                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2371
2372         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2373                 _("(No From)");
2374         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2375             !MSG_IS_NEWS(msginfo->flags)) {
2376                 gchar *addr = NULL;
2377
2378                 Xstrdup_a(addr, msginfo->from, return);
2379                 extract_address(addr);
2380                 if (prefs_common.use_addr_book) {
2381                         if (account_find_from_address(addr)) {
2382                                 addr = summary_complete_address(msginfo->to);
2383                                 g_free(to);
2384                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2385                                 text[col_pos[S_COL_FROM]] = to;
2386                                 g_free(addr);
2387                         }
2388                 } else {
2389                         if (account_find_from_address(addr)) {
2390                                 g_free(to);
2391                                 to = g_strconcat("-->", msginfo->to, NULL);
2392                                 text[col_pos[S_COL_FROM]] = to;
2393                         }
2394                 }
2395         }
2396
2397         /*
2398          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2399          * checking whether the above block (which handles the special case of
2400          * the --> in sent boxes) was executed.
2401          */
2402         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2403                 gchar *from = summary_complete_address(msginfo->from);
2404                 if (from) {
2405                         g_free(to);
2406                         to = from;
2407                         text[col_pos[S_COL_FROM]] = to;
2408                 }                       
2409         }
2410
2411         if (summaryview->simplify_subject_preg != NULL)
2412                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2413                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2414                                         summaryview->simplify_subject_preg) : 
2415                         _("(No Subject)");
2416         else 
2417                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2418                         _("(No Subject)");
2419 }
2420
2421 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2422 {
2423         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2424 }
2425
2426 static void summary_display_msg_full(SummaryView *summaryview,
2427                                      GtkCTreeNode *row,
2428                                      gboolean new_window, gboolean all_headers)
2429 {
2430         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2431         MsgInfo *msginfo;
2432         MsgFlags flags;
2433
2434         if (!new_window) {
2435                 if (summaryview->displayed == row)
2436                         return;
2437                 else
2438                         summaryview->messageview->filtered = FALSE;
2439         }                       
2440         
2441         g_return_if_fail(row != NULL);
2442
2443         if (summary_is_locked(summaryview)) return;
2444         summary_lock(summaryview);
2445
2446         STATUSBAR_POP(summaryview->mainwin);
2447         GTK_EVENTS_FLUSH();
2448
2449         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2450
2451         if (new_window || !prefs_common.mark_as_read_on_new_window) {
2452                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2453                 && procmsg_msg_has_marked_parent(msginfo))
2454                         summaryview->unreadmarked--;
2455                 if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2456                         procmsg_msginfo_unset_flags
2457                                 (msginfo, MSG_NEW | MSG_UNREAD, 0);
2458                         summary_set_row_marks(summaryview, row);
2459                         gtk_clist_thaw(GTK_CLIST(ctree));
2460                         summary_status_show(summaryview);
2461                         
2462                         flags = msginfo->flags;
2463                 }
2464         }
2465
2466         if (new_window) {
2467                 MessageView *msgview;
2468
2469                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2470                 messageview_show(msgview, msginfo, all_headers);
2471         } else {
2472                 MessageView *msgview;
2473
2474                 msgview = summaryview->messageview;
2475
2476                 summaryview->displayed = row;
2477                 if (!messageview_is_visible(msgview))
2478                         main_window_toggle_message_view(summaryview->mainwin);
2479                 messageview_show(msgview, msginfo, all_headers);
2480                 if (msgview->type == MVIEW_TEXT ||
2481                     (msgview->type == MVIEW_MIME &&
2482                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2483                       gtk_notebook_get_current_page
2484                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2485                         gtk_widget_grab_focus(summaryview->ctree);
2486                 GTK_EVENTS_FLUSH();
2487                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2488         }
2489
2490         summary_set_menu_sensitive(summaryview);
2491         toolbar_main_set_sensitive(summaryview->mainwin);
2492
2493         summary_unlock(summaryview);
2494 }
2495
2496 void summary_display_msg_selected(SummaryView *summaryview,
2497                                   gboolean all_headers)
2498 {
2499         if (summary_is_locked(summaryview)) return;
2500         summaryview->displayed = NULL;
2501         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2502                                  all_headers);
2503 }
2504
2505 void summary_redisplay_msg(SummaryView *summaryview)
2506 {
2507         GtkCTreeNode *node;
2508
2509         if (summaryview->displayed) {
2510                 node = summaryview->displayed;
2511                 summaryview->displayed = NULL;
2512                 summary_display_msg(summaryview, node);
2513         }
2514 }
2515
2516 void summary_open_msg(SummaryView *summaryview)
2517 {
2518         if (!summaryview->selected) return;
2519         
2520         /* CLAWS: if separate message view, don't open a new window
2521          * but rather use the current separated message view */
2522         summary_display_msg_full(summaryview, summaryview->selected,
2523                                  prefs_common.sep_msg ? FALSE : TRUE, 
2524                                  FALSE);
2525 }
2526
2527 void summary_view_source(SummaryView * summaryview)
2528 {
2529         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2530         MsgInfo *msginfo;
2531         SourceWindow *srcwin;
2532
2533         if (!summaryview->selected) return;
2534
2535         srcwin = source_window_create();
2536         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2537         source_window_show_msg(srcwin, msginfo);
2538         source_window_show(srcwin);
2539 }
2540
2541 void summary_reedit(SummaryView *summaryview)
2542 {
2543         MsgInfo *msginfo;
2544
2545         if (!summaryview->selected) return;
2546         if (!summaryview->folder_item) return;
2547         if (summaryview->folder_item->stype != F_OUTBOX &&
2548             summaryview->folder_item->stype != F_DRAFT  &&
2549             summaryview->folder_item->stype != F_QUEUE) return;
2550
2551         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2552                                               summaryview->selected);
2553         if (!msginfo) return;
2554
2555         compose_reedit(msginfo);
2556 }
2557
2558 void summary_step(SummaryView *summaryview, GtkScrollType type)
2559 {
2560         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2561         GtkCTreeNode *node;
2562
2563         if (summary_is_locked(summaryview)) return;
2564
2565         if (type == GTK_SCROLL_STEP_FORWARD) {
2566                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2567                 if (node)
2568                         gtkut_ctree_expand_parent_all(ctree, node);
2569                 else
2570                         return;
2571         } else {
2572                 if (summaryview->selected) {
2573                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2574                         if (!node) return;
2575                 }
2576         }
2577
2578         if (messageview_is_visible(summaryview->messageview))
2579                 summaryview->display_msg = TRUE;
2580
2581         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2582                                 type, 0.0);
2583
2584         if (GTK_CLIST(ctree)->selection)
2585                 gtk_sctree_set_anchor_row
2586                         (GTK_SCTREE(ctree),
2587                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2588
2589 }
2590
2591 void summary_toggle_view(SummaryView *summaryview)
2592 {
2593         if (!messageview_is_visible(summaryview->messageview) &&
2594             summaryview->selected)
2595                 summary_display_msg(summaryview,
2596                                     summaryview->selected);
2597         else
2598                 main_window_toggle_message_view(summaryview->mainwin);
2599 }
2600
2601 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2602                                                 GtkCTreeNode *node)
2603 {
2604         MsgInfo *msginfo;
2605
2606         if (node) {
2607                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2608                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2609                         return TRUE;
2610                 node = GTK_CTREE_ROW(node)->children;
2611         } else
2612                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2613
2614         while (node) {
2615                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2616                         return TRUE;
2617                 node = GTK_CTREE_ROW(node)->sibling;
2618         }
2619
2620         return FALSE;
2621 }
2622
2623 static gboolean summary_have_unread_children(SummaryView *summaryview,
2624                                              GtkCTreeNode *node)
2625 {
2626         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2627
2628         if (!node) return FALSE;
2629
2630         node = GTK_CTREE_ROW(node)->children;
2631
2632         while (node) {
2633                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2634                         return TRUE;
2635                 node = GTK_CTREE_ROW(node)->sibling;
2636         }
2637
2638         return FALSE;
2639 }
2640
2641 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2642 {
2643         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2644         GtkStyle *style = NULL;
2645         MsgInfo *msginfo;
2646         MsgFlags flags;
2647         gint *col_pos = summaryview->col_pos;
2648
2649         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2650         if (!msginfo) return;
2651
2652         flags = msginfo->flags;
2653
2654         gtk_ctree_node_set_foreground(ctree, row, NULL);
2655
2656         /* set new/unread column */
2657         if (MSG_IS_IGNORE_THREAD(flags)) {
2658                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2659                                           ignorethreadxpm, ignorethreadxpmmask);
2660         } else if (MSG_IS_NEW(flags)) {
2661                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2662                                           newxpm, newxpmmask);
2663         } else if (MSG_IS_UNREAD(flags)) {
2664                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2665                                           unreadxpm, unreadxpmmask);
2666         } else if (MSG_IS_REPLIED(flags)) {
2667                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2668                                           repliedxpm, repliedxpmmask);
2669         } else if (MSG_IS_FORWARDED(flags)) {
2670                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2671                                           forwardedxpm, forwardedxpmmask);
2672         } else {
2673                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2674                                         NULL);
2675         }
2676
2677         if (prefs_common.bold_unread &&
2678             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2679              (!GTK_CTREE_ROW(row)->expanded &&
2680               GTK_CTREE_ROW(row)->children &&
2681               summary_have_unread_children(summaryview, row))))
2682                 style = bold_style;
2683
2684         /* set mark column */
2685         if (MSG_IS_DELETED(flags)) {
2686                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2687                                           deletedxpm, deletedxpmmask);
2688                 if (style)
2689                         style = bold_deleted_style;
2690                 else {
2691                         style = small_deleted_style;
2692                 }
2693                         gtk_ctree_node_set_foreground
2694                                 (ctree, row, &summaryview->color_dim);
2695         } else if (MSG_IS_MARKED(flags)) {
2696                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2697                                           markxpm, markxpmmask);
2698         } else if (MSG_IS_MOVE(flags)) {
2699                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2700                 if (style)
2701                         style = bold_marked_style;
2702                 else {
2703                         style = small_marked_style;
2704                 }
2705                         gtk_ctree_node_set_foreground
2706                                 (ctree, row, &summaryview->color_marked);
2707         } else if (MSG_IS_COPY(flags)) {
2708                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2709                 if (style)
2710                         style = bold_marked_style;
2711                 else {
2712                         style = small_marked_style;
2713                 }
2714                         gtk_ctree_node_set_foreground
2715                                 (ctree, row, &summaryview->color_marked);
2716         } else if ((global_scoring ||
2717                   summaryview->folder_item->prefs->scoring) &&
2718                  (msginfo->score >= summaryview->important_score) &&
2719                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2720                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2721                 gtk_ctree_node_set_foreground(ctree, row,
2722                                               &summaryview->color_important);
2723         } else {
2724                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2725         }
2726
2727         if (MSG_IS_LOCKED(flags)) {
2728                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2729                                           lockedxpm, lockedxpmmask);
2730         }
2731         else {
2732                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2733         }
2734
2735         if (MSG_IS_SIGNED(flags)) {
2736                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2737                                           gpgsignedxpm, gpgsignedxpmmask);
2738         } else if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2739                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2740                                           clipkeyxpm, clipkeyxpmmask);
2741         } else if (MSG_IS_ENCRYPTED(flags)) {
2742                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2743                                           keyxpm, keyxpmmask);
2744         } else if (MSG_IS_MIME(flags)) {
2745                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2746                                           clipxpm, clipxpmmask);
2747         } else {
2748                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2749         }
2750         if (!style)
2751                 style = small_style;
2752
2753         gtk_ctree_node_set_row_style(ctree, row, style);
2754
2755         if (MSG_GET_COLORLABEL(flags))
2756                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2757 }
2758
2759 void summary_set_marks_selected(SummaryView *summaryview)
2760 {
2761         GList *cur;
2762
2763         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2764              cur = cur->next)
2765                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2766 }
2767
2768 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2769 {
2770         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2771         MsgInfo *msginfo;
2772
2773         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2774         if (MSG_IS_DELETED(msginfo->flags))
2775                 summaryview->deleted--;
2776         if (MSG_IS_MOVE(msginfo->flags))
2777                 summaryview->moved--;
2778         if (MSG_IS_COPY(msginfo->flags))
2779                 summaryview->copied--;
2780         procmsg_update_unread_children (msginfo, TRUE);
2781
2782         procmsg_msginfo_set_to_folder(msginfo, NULL);
2783         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2784         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2785         summary_set_row_marks(summaryview, row);
2786         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2787 }
2788
2789 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2790 {
2791         gboolean changed = FALSE;
2792         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2793         MsgInfo *msginfo;
2794
2795         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2796         if (MSG_IS_DELETED(msginfo->flags))
2797                 summaryview->deleted--;
2798         if (MSG_IS_MOVE(msginfo->flags)) {
2799                 summaryview->moved--;
2800                 changed = TRUE;
2801         }
2802         if (MSG_IS_COPY(msginfo->flags)) {
2803                 summaryview->copied--;
2804                 changed = TRUE;
2805         }
2806         procmsg_msginfo_set_to_folder(msginfo, NULL);
2807         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2808         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2809         summary_set_row_marks(summaryview, row);
2810         debug_print("Message %d is locked\n", msginfo->msgnum);
2811 }
2812
2813 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2814 {
2815         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2816         MsgInfo *msginfo;
2817
2818         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2819         if (!MSG_IS_LOCKED(msginfo->flags))
2820                 return;
2821         procmsg_msginfo_set_to_folder(msginfo, NULL);
2822         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2823         summary_set_row_marks(summaryview, row);
2824         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2825 }
2826
2827 void summary_mark(SummaryView *summaryview)
2828 {
2829         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2830         GList *cur;
2831
2832         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2833                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2834
2835         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2836         summary_status_show(summaryview);
2837 }
2838
2839 static void summary_mark_row_as_read(SummaryView *summaryview,
2840                                      GtkCTreeNode *row)
2841 {
2842         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2843         MsgInfo *msginfo;
2844
2845         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2846
2847         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2848                 return;
2849
2850         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2851         && procmsg_msg_has_marked_parent(msginfo))
2852                 summaryview->unreadmarked--;
2853
2854         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2855         summary_set_row_marks(summaryview, row);
2856         debug_print("Message %d is marked as read\n",
2857                 msginfo->msgnum);
2858 }
2859
2860 void summary_mark_as_read(SummaryView *summaryview)
2861 {
2862         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2863         GList *cur;
2864
2865         folder_item_update_freeze();
2866         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2867                 summary_mark_row_as_read(summaryview,
2868                                          GTK_CTREE_NODE(cur->data));
2869         folder_item_update_thaw();
2870         
2871         summary_status_show(summaryview);
2872 }
2873
2874 void summary_msgs_lock(SummaryView *summaryview)
2875 {
2876         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2877         GList *cur;
2878
2879         folder_item_update_freeze();
2880         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2881                 summary_lock_row(summaryview,
2882                                          GTK_CTREE_NODE(cur->data));
2883         folder_item_update_thaw();
2884         
2885         summary_status_show(summaryview);
2886 }
2887
2888 void summary_msgs_unlock(SummaryView *summaryview)
2889 {
2890         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2891         GList *cur;
2892
2893         folder_item_update_freeze();
2894         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2895                 summary_unlock_row(summaryview,
2896                                    GTK_CTREE_NODE(cur->data));
2897         folder_item_update_thaw();
2898         
2899         summary_status_show(summaryview);
2900 }
2901
2902 void summary_mark_all_read(SummaryView *summaryview)
2903 {
2904         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2905         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2906         GtkCTreeNode *node;
2907
2908         gtk_clist_freeze(clist);
2909         folder_item_update_freeze();
2910         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2911              node = gtkut_ctree_node_next(ctree, node))
2912                 summary_mark_row_as_read(summaryview, node);
2913         gtk_clist_thaw(clist);
2914         folder_item_update_thaw();
2915
2916         summary_status_show(summaryview);
2917 }
2918
2919 static void summary_mark_row_as_unread(SummaryView *summaryview,
2920                                        GtkCTreeNode *row)
2921 {
2922         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2923         MsgInfo *msginfo;
2924
2925         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2926         if (MSG_IS_DELETED(msginfo->flags)) {
2927                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2928                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2929                 summaryview->deleted--;
2930         }
2931
2932         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2933         && procmsg_msg_has_marked_parent(msginfo))
2934                 summaryview->unreadmarked++;
2935
2936         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2937         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2938         debug_print("Message %d is marked as unread\n",
2939                 msginfo->msgnum);
2940
2941         summary_set_row_marks(summaryview, row);
2942 }
2943
2944 void summary_mark_as_unread(SummaryView *summaryview)
2945 {
2946         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2947         GList *cur;
2948
2949         folder_item_update_freeze();
2950         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2951                 summary_mark_row_as_unread(summaryview,
2952                                            GTK_CTREE_NODE(cur->data));
2953         folder_item_update_thaw();
2954         
2955         summary_status_show(summaryview);
2956 }
2957
2958 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2959 {
2960         GList * cur;
2961         gboolean found;
2962
2963         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2964
2965         case F_NEWS:
2966
2967                 /*
2968                   security : checks if one the accounts correspond to
2969                   the author of the post
2970                 */
2971
2972                 found = FALSE;
2973                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2974                         PrefsAccount * account;
2975                         gchar * from_name;
2976                         
2977                         account = cur->data;
2978                         if (account->name && *account->name)
2979                                 from_name =
2980                                         g_strdup_printf("%s <%s>",
2981                                                         account->name,
2982                                                         account->address);
2983                         else
2984                                 from_name =
2985                                         g_strdup_printf("%s",
2986                                                         account->address);
2987                         
2988                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2989                                 g_free(from_name);
2990                                 found = TRUE;
2991                                 break;
2992                         }
2993                         g_free(from_name);
2994                 }
2995
2996                 if (!found) {
2997                         alertpanel_error(_("You're not the author of the article\n"));
2998                 }
2999                 
3000                 return found;
3001
3002         default:
3003                 return TRUE;
3004         }
3005 }
3006
3007 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3008 {
3009         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3010         MsgInfo *msginfo;
3011
3012         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3013
3014         if (!check_permission(summaryview, msginfo))
3015                 return;
3016
3017         if (MSG_IS_LOCKED(msginfo->flags)) return;
3018
3019         if (MSG_IS_DELETED(msginfo->flags)) return;
3020
3021         if (MSG_IS_MOVE(msginfo->flags))
3022                 summaryview->moved--;
3023         if (MSG_IS_COPY(msginfo->flags))
3024                 summaryview->copied--;
3025         procmsg_update_unread_children (msginfo, FALSE);
3026
3027         procmsg_msginfo_set_to_folder(msginfo, NULL);
3028         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
3029         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
3030         summaryview->deleted++;
3031
3032         if (!prefs_common.immediate_exec && 
3033             summaryview->folder_item->stype != F_TRASH)
3034                 summary_set_row_marks(summaryview, row);
3035
3036         debug_print("Message %s/%d is set to delete\n",
3037                     msginfo->folder->path, msginfo->msgnum);
3038 }
3039
3040 void summary_cancel(SummaryView *summaryview)
3041 {
3042         MsgInfo * msginfo;
3043         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3044
3045         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3046                                               summaryview->selected);
3047         if (!msginfo) return;
3048
3049         if (!check_permission(summaryview, msginfo))
3050                 return;
3051
3052         news_cancel_article(summaryview->folder_item->folder, msginfo);
3053         
3054         if (summary_is_locked(summaryview)) return;
3055
3056         summary_lock(summaryview);
3057
3058         gtk_clist_freeze(clist);
3059
3060         summary_update_status(summaryview);
3061         summary_status_show(summaryview);
3062
3063         gtk_clist_thaw(clist);
3064
3065         summary_unlock(summaryview);
3066 }
3067
3068 void summary_delete(SummaryView *summaryview)
3069 {
3070         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3071         FolderItem *item = summaryview->folder_item;
3072         GList *cur;
3073         GtkCTreeNode *sel_last = NULL;
3074         GtkCTreeNode *node;
3075
3076         if (!item) return;
3077 #if 0
3078         if (!item || item->folder->type == F_NEWS) return;
3079 #endif
3080
3081         if (summary_is_locked(summaryview)) return;
3082
3083         /* if current folder is trash, ask for confirmation */
3084         if (item->stype == F_TRASH) {
3085                 AlertValue aval;
3086
3087                 aval = alertpanel(_("Delete message(s)"),
3088                                   _("Do you really want to delete message(s) from the trash?"),
3089                                   _("Yes"), _("No"), NULL);
3090                 if (aval != G_ALERTDEFAULT) return;
3091         }
3092
3093         main_window_cursor_wait(summaryview->mainwin);
3094
3095         /* next code sets current row focus right. We need to find a row
3096          * that is not deleted. */
3097         folder_item_update_freeze();     
3098         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3099                 sel_last = GTK_CTREE_NODE(cur->data);
3100                 summary_delete_row(summaryview, sel_last);
3101         }
3102         folder_item_update_thaw();
3103
3104         node = summary_find_next_msg(summaryview, sel_last);
3105         if (!node)
3106                 node = summary_find_prev_msg(summaryview, sel_last);
3107
3108         summary_select_node(summaryview, node, FALSE, FALSE);
3109         
3110         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3111                 summary_execute(summaryview);
3112                 /* after deleting, the anchor may be at an invalid row
3113                  * so reset it to the node we found earlier */
3114                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3115         } else
3116                 summary_status_show(summaryview);
3117                 
3118         main_window_cursor_normal(summaryview->mainwin);
3119 }
3120
3121 void summary_delete_duplicated(SummaryView *summaryview)
3122 {
3123         if (!summaryview->folder_item ||
3124             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3125         if (summaryview->folder_item->stype == F_TRASH) return;
3126
3127         main_window_cursor_wait(summaryview->mainwin);
3128         debug_print("Deleting duplicated messages...");
3129         STATUSBAR_PUSH(summaryview->mainwin,
3130                        _("Deleting duplicated messages..."));
3131
3132         folder_item_update_freeze();
3133         
3134         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3135                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
3136                                 summaryview);
3137
3138         if (prefs_common.immediate_exec)
3139                 summary_execute(summaryview);
3140         else
3141                 summary_status_show(summaryview);
3142
3143         folder_item_update_thaw();
3144
3145         debug_print("done.\n");
3146         STATUSBAR_POP(summaryview->mainwin);
3147         main_window_cursor_normal(summaryview->mainwin);
3148 }
3149
3150 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
3151                                            SummaryView *summaryview)
3152 {
3153         GtkCTreeNode *found;
3154         MsgInfo *msginfo;
3155         MsgInfo *dup_msginfo;
3156
3157         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3158         
3159         if (!msginfo || !msginfo->msgid || !*msginfo->msgid) return;
3160
3161         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
3162         
3163         if (found && found != node) {
3164                 dup_msginfo = gtk_ctree_node_get_row_data(ctree, found);
3165                 /* prefer to delete the unread one */
3166                 if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(dup_msginfo->flags))
3167                 ||  (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(dup_msginfo->flags)))
3168                         summary_delete_row(summaryview, node);
3169         }
3170 }
3171
3172 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3173 {
3174         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3175         MsgInfo *msginfo;
3176
3177         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3178         if (MSG_IS_DELETED(msginfo->flags))
3179                 summaryview->deleted--;
3180         if (MSG_IS_MOVE(msginfo->flags))
3181                 summaryview->moved--;
3182         if (MSG_IS_COPY(msginfo->flags))
3183                 summaryview->copied--;
3184         procmsg_update_unread_children (msginfo, FALSE);
3185
3186         procmsg_msginfo_set_to_folder(msginfo, NULL);
3187         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3188         summary_set_row_marks(summaryview, row);
3189
3190         debug_print("Message %s/%d is unmarked\n",
3191                     msginfo->folder->path, msginfo->msgnum);
3192 }
3193
3194 void summary_unmark(SummaryView *summaryview)
3195 {
3196         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3197         GList *cur;
3198
3199         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3200                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3201
3202         summary_status_show(summaryview);
3203 }
3204
3205 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3206                                 FolderItem *to_folder)
3207 {
3208         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3209         MsgInfo *msginfo;
3210
3211         g_return_if_fail(to_folder != NULL);
3212
3213         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3214         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3215         if (MSG_IS_DELETED(msginfo->flags))
3216                 summaryview->deleted--;
3217         if (MSG_IS_COPY(msginfo->flags)) {
3218                 summaryview->copied--;
3219         }
3220         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3221         if (!MSG_IS_MOVE(msginfo->flags)) {
3222                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3223                 summaryview->moved++;
3224         }
3225         if (!prefs_common.immediate_exec) {
3226                 summary_set_row_marks(summaryview, row);
3227         }
3228
3229         debug_print("Message %d is set to move to %s\n",
3230                     msginfo->msgnum, to_folder->path);
3231 }
3232
3233 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3234 {
3235         GList *cur;
3236
3237         if (!to_folder) return;
3238         if (!summaryview->folder_item ||
3239             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3240
3241         if (summary_is_locked(summaryview)) return;
3242
3243         if (summaryview->folder_item == to_folder) {
3244                 alertpanel_notice(_("Destination is same as current folder."));
3245                 return;
3246         }
3247
3248         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3249              cur != NULL; cur = cur->next)
3250                 summary_move_row_to
3251                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3252
3253         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3254
3255         if (prefs_common.immediate_exec)
3256                 summary_execute(summaryview);
3257         else {
3258                 summary_status_show(summaryview);
3259         }
3260         
3261         if (!summaryview->selected) { /* this was the last message */
3262                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3263                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3264                 if (node)
3265                         summary_select_node(summaryview, node, TRUE, TRUE);
3266         }
3267
3268 }
3269
3270 void summary_move_to(SummaryView *summaryview)
3271 {
3272         FolderItem *to_folder;
3273
3274         if (!summaryview->folder_item ||
3275             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3276
3277         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3278                                          FOLDER_SEL_MOVE, NULL);
3279         summary_move_selected_to(summaryview, to_folder);
3280 }
3281
3282 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3283                                 FolderItem *to_folder)
3284 {
3285         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3286         MsgInfo *msginfo;
3287
3288         g_return_if_fail(to_folder != NULL);
3289
3290         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3291         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3292         if (MSG_IS_DELETED(msginfo->flags))
3293                 summaryview->deleted--;
3294         if (MSG_IS_MOVE(msginfo->flags)) {
3295                 summaryview->moved--;
3296         }
3297         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3298         if (!MSG_IS_COPY(msginfo->flags)) {
3299                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3300                 summaryview->copied++;
3301         }
3302         if (!prefs_common.immediate_exec) {
3303                 summary_set_row_marks(summaryview, row);
3304         }
3305
3306         debug_print("Message %d is set to copy to %s\n",
3307                     msginfo->msgnum, to_folder->path);
3308 }
3309
3310 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3311 {
3312         GList *cur;
3313
3314         if (!to_folder) return;
3315         if (!summaryview->folder_item) return;
3316
3317         if (summary_is_locked(summaryview)) return;
3318
3319         if (summaryview->folder_item == to_folder) {
3320                 alertpanel_notice
3321                         (_("Destination to copy is same as current folder."));
3322                 return;
3323         }
3324
3325         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3326              cur != NULL; cur = cur->next)
3327                 summary_copy_row_to
3328                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3329
3330         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3331
3332         if (prefs_common.immediate_exec)
3333                 summary_execute(summaryview);
3334         else {
3335                 summary_status_show(summaryview);
3336         }
3337 }
3338
3339 void summary_copy_to(SummaryView *summaryview)
3340 {
3341         FolderItem *to_folder;
3342
3343         if (!summaryview->folder_item) return;
3344
3345         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3346                                          FOLDER_SEL_COPY, NULL);
3347         summary_copy_selected_to(summaryview, to_folder);
3348 }
3349
3350 void summary_add_address(SummaryView *summaryview)
3351 {
3352         MsgInfo *msginfo;
3353         gchar *from;
3354
3355         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3356                                               summaryview->selected);
3357         if (!msginfo) return;
3358
3359         Xstrdup_a(from, msginfo->from, return);
3360         eliminate_address_comment(from);
3361         extract_address(from);
3362         addressbook_add_contact(msginfo->fromname, from, NULL);
3363 }
3364
3365 void summary_select_all(SummaryView *summaryview)
3366 {
3367         if (!summaryview->folder_item) return;
3368
3369         if (summaryview->folder_item->total_msgs >= 500) {
3370                 STATUSBAR_PUSH(summaryview->mainwin,
3371                                _("Selecting all messages..."));
3372                 main_window_cursor_wait(summaryview->mainwin);
3373         }
3374
3375         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3376
3377         if (summaryview->folder_item->total_msgs >= 500) {
3378                 STATUSBAR_POP(summaryview->mainwin);
3379                 main_window_cursor_normal(summaryview->mainwin);
3380         }
3381 }
3382
3383 void summary_unselect_all(SummaryView *summaryview)
3384 {
3385         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3386 }
3387
3388 void summary_select_thread(SummaryView *summaryview)
3389 {
3390         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3391         GtkCTreeNode *node = summaryview->selected;
3392
3393         if (!node) return;
3394
3395         while (GTK_CTREE_ROW(node)->parent != NULL)
3396                 node = GTK_CTREE_ROW(node)->parent;
3397
3398         if (node != summaryview->selected)
3399                 summary_select_node
3400                         (summaryview, node,
3401                          messageview_is_visible(summaryview->messageview),
3402                          FALSE);
3403
3404         gtk_ctree_select_recursive(ctree, node);
3405
3406         summary_status_show(summaryview);
3407 }
3408
3409 void summary_save_as(SummaryView *summaryview)
3410 {
3411         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3412         MsgInfo *msginfo;
3413         gchar *filename = NULL;
3414         gchar *src, *dest;
3415
3416         AlertValue aval = 0;
3417
3418         if (!summaryview->selected) return;
3419         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3420         if (!msginfo) return;
3421
3422         if (msginfo->subject) {
3423                 Xstrdup_a(filename, msginfo->subject, return);
3424                 subst_for_filename(filename);
3425         }
3426         dest = filesel_select_file(_("Save as"), filename);
3427         if (!dest) return;
3428         if (is_file_exist(dest)) {
3429                 aval = alertpanel(_("Append or Overwrite"),
3430                                   _("Append or overwrite existing file?"),
3431                                   _("Append"), _("Overwrite"), _("Cancel"));
3432                 if (aval != 0 && aval != 1)
3433                         return;
3434         }
3435
3436         src = procmsg_get_message_file(msginfo);
3437         if ( aval==0 ) { /* append */
3438                 if (append_file(src, dest, TRUE) < 0) 
3439                         alertpanel_error(_("Can't save the file `%s'."),
3440                                          g_basename(dest));
3441         } else { /* overwrite */
3442                 if (copy_file(src, dest, TRUE) < 0)
3443                         alertpanel_error(_("Can't save the file `%s'."),
3444                                          g_basename(dest));
3445         }
3446         g_free(src);
3447
3448         
3449         /*
3450          * If two or more msgs are selected,
3451          * append them to the output file.
3452          */
3453         if (GTK_CLIST(ctree)->selection->next) {
3454                 GList *item;
3455                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3456                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3457                         if (!msginfo) break;
3458                         src = procmsg_get_message_file(msginfo);
3459                         if (append_file(src, dest, TRUE) < 0)
3460                                 alertpanel_error(_("Can't save the file `%s'."),
3461                                                  g_basename(dest));
3462                 }
3463                 g_free(src);
3464         }
3465 }
3466
3467 void summary_print(SummaryView *summaryview)
3468 {
3469         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3470         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3471         MsgInfo *msginfo;
3472         GList *cur;
3473         gchar *cmdline;
3474         gchar *p;
3475
3476         if (clist->selection == NULL) return;
3477
3478         cmdline = input_dialog(_("Print"),
3479                                _("Enter the print command line:\n"
3480                                  "(`%s' will be replaced with file name)"),
3481                                prefs_common.print_cmd);
3482         if (!cmdline) return;
3483         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3484             strchr(p + 2, '%')) {
3485                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3486                                  cmdline);
3487                 g_free(cmdline);
3488                 return;
3489         }
3490
3491         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3492                 msginfo = gtk_ctree_node_get_row_data
3493                         (ctree, GTK_CTREE_NODE(cur->data));
3494                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3495         }
3496
3497         g_free(cmdline);
3498 }
3499
3500 gboolean summary_execute(SummaryView *summaryview)
3501 {
3502         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3503         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3504         GtkCTreeNode *node, *next;
3505         GtkCTreeNode *new_selected = NULL;
3506
3507         if (!summaryview->folder_item) return FALSE;
3508
3509         if (summary_is_locked(summaryview)) return FALSE;
3510         summary_lock(summaryview);
3511
3512         gtk_clist_freeze(clist);
3513
3514         if (summaryview->threaded)
3515                 summary_unthread_for_exec(summaryview);
3516
3517         folder_item_update_freeze();
3518         summary_execute_move(summaryview);
3519         summary_execute_copy(summaryview);
3520         summary_execute_delete(summaryview);
3521         folder_item_update_thaw();
3522
3523         node = GTK_CTREE_NODE(clist->row_list);
3524         for (; node != NULL; node = next) {
3525                 next = gtkut_ctree_node_next(ctree, node);
3526                 if (gtk_ctree_node_get_row_data(ctree, node) != NULL) continue;
3527
3528                 if (node == summaryview->displayed) {
3529                         messageview_clear(summaryview->messageview);
3530                         summaryview->displayed = NULL;
3531                 }
3532                 if (GTK_CTREE_ROW(node)->children != NULL) {
3533                         g_warning("summary_execute(): children != NULL\n");
3534                         continue;
3535                 }
3536
3537                 if (!new_selected &&
3538                     gtkut_ctree_node_is_selected(ctree, node)) {
3539                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
3540                         new_selected = summary_find_next_msg(summaryview, node);
3541                         if (!new_selected)
3542                                 new_selected = summary_find_prev_msg
3543                                         (summaryview, node);
3544                 }
3545
3546                 gtk_ctree_remove_node(ctree, node);
3547         }
3548
3549         if (new_selected) {
3550                 gtk_sctree_select
3551                         (GTK_SCTREE(ctree),
3552                          summaryview->displayed ? summaryview->displayed
3553                          : new_selected);
3554         }
3555
3556         if (summaryview->threaded)
3557                 summary_thread_build(summaryview);
3558
3559         summaryview->selected = clist->selection ?
3560                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3561
3562         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3563                 menu_set_insensitive_all
3564                         (GTK_MENU_SHELL(summaryview->popupmenu));
3565                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3566         } else
3567                 gtk_widget_grab_focus(summaryview->ctree);
3568
3569         summary_update_status(summaryview);
3570         summary_status_show(summaryview);
3571
3572         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3573
3574         gtk_clist_thaw(clist);
3575
3576         summary_unlock(summaryview);
3577         return TRUE;
3578 }
3579
3580 static void summary_execute_move(SummaryView *summaryview)
3581 {
3582         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3583         GSList *cur;
3584
3585         /* search moving messages and execute */
3586         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3587                                 summaryview);
3588
3589         if (summaryview->mlist) {
3590                 procmsg_move_messages(summaryview->mlist);
3591
3592                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3593                         procmsg_msginfo_free((MsgInfo *)cur->data);
3594                 g_slist_free(summaryview->mlist);
3595                 summaryview->mlist = NULL;
3596         }
3597 }
3598
3599 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3600                                       gpointer data)
3601 {
3602         SummaryView *summaryview = data;
3603         MsgInfo *msginfo;
3604
3605         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3606
3607         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3608                 summaryview->mlist =
3609                         g_slist_prepend(summaryview->mlist, msginfo);
3610                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3611
3612                 if (msginfo->msgid && *msginfo->msgid &&
3613                     node == g_hash_table_lookup(summaryview->msgid_table,
3614                                                 msginfo->msgid))
3615                         g_hash_table_remove(summaryview->msgid_table,
3616                                             msginfo->msgid);
3617         }
3618 }
3619
3620 static void summary_execute_copy(SummaryView *summaryview)
3621 {
3622         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3623
3624         /* search copying messages and execute */
3625         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3626                                 summaryview);
3627
3628         if (summaryview->mlist) {
3629                 summaryview->mlist = g_slist_reverse(summaryview->mlist);
3630                 procmsg_copy_messages(summaryview->mlist);
3631
3632                 g_slist_free(summaryview->mlist);
3633                 summaryview->mlist = NULL;
3634         }
3635 }
3636
3637 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3638                                       gpointer data)
3639 {
3640         SummaryView *summaryview = data;
3641         MsgInfo *msginfo;
3642
3643         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3644
3645         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3646                 summaryview->mlist =
3647                         g_slist_prepend(summaryview->mlist, msginfo);
3648
3649                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3650                 summary_set_row_marks(summaryview, node);
3651         }
3652 }
3653
3654 static void summary_execute_delete(SummaryView *summaryview)
3655 {
3656         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3657         FolderItem *trash;
3658         GSList *cur;
3659
3660         trash = summaryview->folder_item->folder->trash;
3661
3662         /* search deleting messages and execute */
3663         gtk_ctree_pre_recursive
3664                 (ctree, NULL, summary_execute_delete_func, summaryview);
3665
3666         if (!summaryview->mlist) return;
3667
3668         if (trash == NULL || summaryview->folder_item == trash)
3669                 folder_item_remove_msgs(summaryview->folder_item,
3670                                         summaryview->mlist);
3671         else
3672                 folder_item_move_msgs(trash, summaryview->mlist);
3673
3674         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3675                 procmsg_msginfo_free((MsgInfo *)cur->data);
3676
3677         g_slist_free(summaryview->mlist);
3678         summaryview->mlist = NULL;
3679 }
3680
3681 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3682                                         gpointer data)
3683 {
3684         SummaryView *summaryview = data;
3685         MsgInfo *msginfo;
3686
3687         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3688
3689         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3690                 summaryview->mlist =
3691                         g_slist_append(summaryview->mlist, msginfo);
3692                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3693
3694                 if (msginfo->msgid && *msginfo->msgid &&
3695                     node == g_hash_table_lookup(summaryview->msgid_table,
3696                                                 msginfo->msgid)) {
3697                         g_hash_table_remove(summaryview->msgid_table,
3698                                             msginfo->msgid);
3699                 }       
3700                 if (msginfo->subject && *msginfo->subject && 
3701                     node == subject_table_lookup(summaryview->subject_table,
3702                                                  msginfo->subject)) {
3703                         subject_table_remove(summaryview->subject_table,
3704                                              msginfo->subject);
3705                 }                                           
3706         }
3707 }
3708
3709 /* thread functions */
3710
3711 void summary_thread_build(SummaryView *summaryview)
3712 {
3713         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3714         GtkCTreeNode *node;
3715         GtkCTreeNode *next;
3716         GtkCTreeNode *parent;
3717         MsgInfo *msginfo;
3718
3719         summary_lock(summaryview);
3720
3721         debug_print("Building threads...");
3722         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3723         main_window_cursor_wait(summaryview->mainwin);
3724
3725         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
3726                                          summary_tree_expanded, summaryview);
3727         gtk_clist_freeze(GTK_CLIST(ctree));
3728
3729         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3730         while (node) {
3731                 next = GTK_CTREE_ROW(node)->sibling;
3732
3733                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3734
3735                 parent = NULL;
3736
3737                 /* alfons - claws seems to prefer subject threading before
3738                  * inreplyto threading. we should look more deeply in this,
3739                  * because inreplyto should have precedence... */
3740                 if (msginfo && msginfo->inreplyto) {
3741                         parent = g_hash_table_lookup(summaryview->msgid_table,
3742                                                      msginfo->inreplyto);
3743                 }
3744
3745                 if (prefs_common.thread_by_subject && parent == NULL) {
3746                         parent = subject_table_lookup
3747                                 (summaryview->subject_table,
3748                                  msginfo->subject);
3749                 }
3750
3751                 if (parent && parent != node) {
3752                         gtk_ctree_move(ctree, node, parent, NULL);
3753                         gtk_ctree_expand(ctree, node);
3754                 }
3755
3756                 node = next;
3757         }
3758
3759         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3760
3761         while (node) {
3762                 next = GTK_CTREE_NODE_NEXT(node);
3763                 if (!summaryview->thread_collapsed)
3764                         gtk_ctree_expand(ctree, node);
3765                 if (prefs_common.bold_unread &&
3766                     GTK_CTREE_ROW(node)->children)
3767                         summary_set_row_marks(summaryview, node);
3768                 node = next;
3769         }
3770
3771         gtkut_ctree_set_focus_row(ctree, summaryview->selected);
3772
3773         gtk_clist_thaw(GTK_CLIST(ctree));
3774         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
3775                                            summary_tree_expanded, summaryview);
3776
3777         debug_print("done.\n");
3778         STATUSBAR_POP(summaryview->mainwin);
3779         main_window_cursor_normal(summaryview->mainwin);
3780
3781         summaryview->threaded = TRUE;
3782
3783         summary_unlock(summaryview);
3784 }
3785
3786 static void summary_thread_init(SummaryView *summaryview)
3787 {
3788         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3789         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3790         GtkCTreeNode *next;
3791
3792         if (!summaryview->thread_collapsed) {
3793                 while (node) {
3794                         next = GTK_CTREE_ROW(node)->sibling;
3795                         if (GTK_CTREE_ROW(node)->children)
3796                                 gtk_ctree_expand(ctree, node);
3797                         node = next;
3798                 }
3799         } else if (prefs_common.bold_unread) {
3800                 while (node) {
3801                         next = GTK_CTREE_ROW(node)->sibling;
3802                         if (GTK_CTREE_ROW(node)->children)
3803                                 summary_set_row_marks(summaryview, node);
3804                         node = next;
3805                 }
3806         }
3807 }
3808
3809 void summary_unthread(SummaryView *summaryview)
3810 {
3811         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3812         GtkCTreeNode *node;
3813         GtkCTreeNode *child;
3814         GtkCTreeNode *sibling;
3815         GtkCTreeNode *next_child;
3816
3817         summary_lock(summaryview);
3818
3819         debug_print("Unthreading...");
3820         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
3821         main_window_cursor_wait(summaryview->mainwin);
3822         
3823         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
3824                                          summary_tree_collapsed, summaryview);
3825         gtk_clist_freeze(GTK_CLIST(ctree));
3826
3827         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3828              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3829                 child = GTK_CTREE_ROW(node)->children;
3830                 sibling = GTK_CTREE_ROW(node)->sibling;
3831
3832                 while (child != NULL) {
3833                         next_child = GTK_CTREE_ROW(child)->sibling;
3834                         gtk_ctree_move(ctree, child, NULL, sibling);
3835                         child = next_child;
3836                 }
3837         }
3838
3839         /* CLAWS: and sort it */
3840         gtk_sctree_sort_recursive(ctree, NULL); 
3841
3842         gtk_clist_thaw(GTK_CLIST(ctree));
3843         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
3844                                            summary_tree_collapsed, summaryview);
3845
3846         debug_print("done.\n");
3847         STATUSBAR_POP(summaryview->mainwin);
3848         main_window_cursor_normal(summaryview->mainwin);
3849
3850         summaryview->threaded = FALSE;