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