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