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