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