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