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