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