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