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