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