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