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