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