speed improvements
[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         summary_set_ctree_from_list(summaryview, mlist);
1093
1094         g_slist_free(mlist);
1095
1096         if (summaryview->sort_key != SORT_BY_NONE)
1097                 summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
1098
1099         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
1100
1101         gtk_clist_thaw(GTK_CLIST(ctree));
1102
1103         if (is_refresh) {
1104                 summaryview->displayed =
1105                         summary_find_msg_by_msgnum(summaryview,
1106                                                    displayed_msgnum);
1107                 if (!summaryview->displayed)
1108                         messageview_clear(summaryview->messageview);
1109                 summary_select_by_msgnum(summaryview, selected_msgnum);
1110                 if (!summaryview->selected) {
1111                         /* no selected message - select first unread
1112                            message, but do not display it */
1113                         node = summary_find_next_flagged_msg(summaryview, NULL,
1114                                                              MSG_UNREAD, FALSE);
1115                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
1116                                 node = gtk_ctree_node_nth
1117                                         (ctree,
1118                                          item->sort_type == SORT_DESCENDING
1119                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
1120                         summary_select_node(summaryview, node, FALSE, TRUE);
1121                 }
1122         } else {
1123                 /* select first unread message */
1124                 if (summaryview->sort_key == SORT_BY_SCORE)
1125                         node = summary_find_next_important_score(summaryview,
1126                                                                  NULL);
1127                 else
1128                 node = summary_find_next_flagged_msg(summaryview, NULL,
1129                                                      MSG_UNREAD, FALSE);
1130                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1131                         node = gtk_ctree_node_nth
1132                                 (ctree,
1133                                  item->sort_type == SORT_DESCENDING
1134                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1135                 }
1136                 if (prefs_common.open_unread_on_enter) {
1137                         summary_unlock(summaryview);
1138                         summary_select_node(summaryview, node, TRUE, TRUE);
1139                         summary_lock(summaryview);
1140                 } else
1141                         summary_select_node(summaryview, node, FALSE, TRUE);
1142         }
1143
1144         summary_set_column_titles(summaryview);
1145         summary_status_show(summaryview);
1146         summary_set_menu_sensitive(summaryview);
1147         toolbar_main_set_sensitive(summaryview->mainwin);
1148
1149         debug_print("\n");
1150         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1151         STATUSBAR_POP(summaryview->mainwin);
1152         main_window_cursor_normal(summaryview->mainwin);
1153         summary_unlock(summaryview);
1154         inc_unlock();
1155
1156         return TRUE;
1157 }
1158
1159 #undef CURRENTLY_DISPLAYED
1160
1161 void summary_clear_list(SummaryView *summaryview)
1162 {
1163         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1164         gint optimal_width;
1165
1166         gtk_clist_freeze(clist);
1167
1168         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1169                                 NULL, summary_free_msginfo_func, NULL);
1170
1171         if (summaryview->folder_item) {
1172                 summaryview->folder_item->opened = FALSE;
1173                 summaryview->folder_item = NULL;
1174         }
1175
1176         summaryview->display_msg = FALSE;
1177
1178         summaryview->selected  = NULL;
1179         summaryview->displayed = NULL;
1180         summaryview->newmsgs   = summaryview->unread     = 0;
1181         summaryview->messages  = summaryview->total_size = 0;
1182         summaryview->deleted   = summaryview->moved      = 0;
1183         summaryview->copied    = 0;
1184         if (summaryview->msgid_table) {
1185                 g_hash_table_destroy(summaryview->msgid_table);
1186                 summaryview->msgid_table = NULL;
1187         }
1188         if (summaryview->subject_table) {
1189                 g_hash_table_destroy(summaryview->subject_table);
1190                 summaryview->subject_table = NULL;
1191         }
1192         summaryview->mlist = NULL;
1193
1194         gtk_clist_clear(clist);
1195         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1196                 optimal_width = gtk_clist_optimal_column_width
1197                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1198                 gtk_clist_set_column_width
1199                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1200                          optimal_width);
1201         }
1202
1203         gtk_clist_thaw(clist);
1204 }
1205
1206 void summary_clear_all(SummaryView *summaryview)
1207 {
1208         summary_clear_list(summaryview);
1209         summary_set_menu_sensitive(summaryview);
1210         toolbar_main_set_sensitive(summaryview->mainwin);
1211         summary_status_show(summaryview);
1212 }
1213
1214 void summary_lock(SummaryView *summaryview)
1215 {
1216         summaryview->lock_count++;
1217 }
1218
1219 void summary_unlock(SummaryView *summaryview)
1220 {
1221         if (summaryview->lock_count)
1222                 summaryview->lock_count--;
1223 }
1224
1225 gboolean summary_is_locked(SummaryView *summaryview)
1226 {
1227         return summaryview->lock_count > 0;
1228 }
1229
1230 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1231 {
1232         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1233         SummarySelection selection;
1234
1235         if (!clist->row_list)
1236                 selection = SUMMARY_NONE;
1237         else if (!clist->selection)
1238                 selection = SUMMARY_SELECTED_NONE;
1239         else if (!clist->selection->next)
1240                 selection = SUMMARY_SELECTED_SINGLE;
1241         else
1242                 selection = SUMMARY_SELECTED_MULTIPLE;
1243
1244         return selection;
1245 }
1246
1247 static void summary_set_menu_sensitive(SummaryView *summaryview)
1248 {
1249         GtkItemFactory *ifactory = summaryview->popupfactory;
1250         SummarySelection selection;
1251         GtkWidget *menuitem;
1252         gboolean sens;
1253
1254         selection = summary_get_selection_type(summaryview);
1255         main_window_set_menu_sensitive(summaryview->mainwin);
1256
1257         if (selection == SUMMARY_NONE) {
1258                 GtkWidget *submenu;
1259
1260                 submenu = gtk_item_factory_get_widget
1261                         (summaryview->popupfactory, "/Mark");
1262                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1263                 menu_set_insensitive_all
1264                         (GTK_MENU_SHELL(summaryview->popupmenu));
1265                 return;
1266         }
1267
1268         if (summaryview->folder_item->folder->type != F_NEWS)
1269                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1270         else
1271                 menu_set_sensitive(ifactory, "/Move...", FALSE);
1272
1273 #if 0
1274         menu_set_sensitive(ifactory, "/Delete", TRUE);
1275 #endif
1276         menu_set_sensitive(ifactory, "/Copy...", TRUE);
1277         menu_set_sensitive(ifactory, "/Execute", TRUE);
1278
1279         menu_set_sensitive(ifactory, "/Mark", TRUE);
1280         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1281         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1282
1283         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1284         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1285         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1286         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1287         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1288
1289         menu_set_sensitive(ifactory, "/Color label", TRUE);
1290
1291         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1292         menu_set_sensitive(ifactory, "/Reply",                    sens);
1293         menu_set_sensitive(ifactory, "/Reply to",                 sens);
1294         menu_set_sensitive(ifactory, "/Reply to/all",             sens);
1295         menu_set_sensitive(ifactory, "/Reply to/sender",          sens);
1296         menu_set_sensitive(ifactory, "/Reply to/mailing list",    sens);
1297         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1298         menu_set_sensitive(ifactory, "/Redirect",                 sens);
1299
1300         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1301         menu_set_sensitive(ifactory, "/Create filter rule",         sens);
1302
1303         menu_set_sensitive(ifactory, "/View", sens);
1304         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1305         menu_set_sensitive(ifactory, "/View/Source", sens);
1306         menu_set_sensitive(ifactory, "/View/All header", sens);
1307         if (summaryview->folder_item->stype == F_OUTBOX ||
1308             summaryview->folder_item->stype == F_DRAFT  ||
1309             summaryview->folder_item->stype == F_QUEUE)
1310                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1311         else
1312                 menu_set_sensitive(ifactory, "/Re-edit", FALSE);
1313
1314         menu_set_sensitive(ifactory, "/Save as...", TRUE);
1315         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1316
1317         menu_set_sensitive(ifactory, "/Select all", TRUE);
1318         menu_set_sensitive(ifactory, "/Select thread", sens);
1319         if (summaryview->folder_item->folder->account)
1320                 sens = summaryview->folder_item->folder->account->protocol
1321                         == A_NNTP;
1322         else
1323                 sens = FALSE;
1324         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1325         menu_set_sensitive(ifactory, "/Cancel a news message", sens);
1326         menu_set_sensitive(ifactory, "/Delete", !sens);
1327
1328         summary_lock(summaryview);
1329         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1330         gtk_check_menu_item_set_active
1331                 (GTK_CHECK_MENU_ITEM(menuitem),
1332                  summaryview->messageview->textview->show_all_headers);
1333         summary_unlock(summaryview);
1334 }
1335
1336 void summary_select_prev_unread(SummaryView *summaryview)
1337 {
1338         GtkCTreeNode *node;
1339
1340         node = summary_find_prev_flagged_msg
1341                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1342
1343         if (!node) {
1344                 AlertValue val = 0;
1345
1346                 switch (prefs_common.next_unread_msg_dialog) {
1347                         case NEXTUNREADMSGDIALOG_ALWAYS:
1348                                 val = alertpanel(_("No more unread messages"),
1349                                                  _("No unread message found. "
1350                                                    "Search from the end?"),
1351                                                  _("Yes"), _("No"), NULL);
1352                                 break;
1353                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1354                                 val = G_ALERTDEFAULT;
1355                                 break;
1356                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1357                                 val = !G_ALERTDEFAULT;
1358                                 break;
1359                         default:
1360                                 debug_print(
1361                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1362                 }
1363                 if (val != G_ALERTDEFAULT) return;
1364                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1365                                                      MSG_UNREAD, FALSE);
1366         }
1367
1368         if (!node)
1369                 alertpanel_notice(_("No unread messages."));
1370         else
1371                 summary_select_node(summaryview, node, TRUE, FALSE);
1372 }
1373
1374 void summary_select_next_unread(SummaryView *summaryview)
1375 {
1376         GtkCTreeNode *node = summaryview->selected;
1377         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1378
1379         node = summary_find_next_flagged_msg
1380                 (summaryview, node, MSG_UNREAD, FALSE);
1381         
1382         if (node)
1383                 summary_select_node(summaryview, node, TRUE, FALSE);
1384         else {
1385                 node = summary_find_next_flagged_msg
1386                         (summaryview, NULL, MSG_UNREAD, FALSE);
1387                 if (node == NULL) {
1388                         AlertValue val = 0;
1389
1390                         switch (prefs_common.next_unread_msg_dialog) {
1391                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1392                                         val = alertpanel(_("No more unread messages"),
1393                                                          _("No unread message found. "
1394                                                            "Go to next folder?"),
1395                                                          _("Yes"), _("No"), NULL);
1396                                         break;
1397                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1398                                         val = G_ALERTDEFAULT;
1399                                         break;
1400                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1401                                         val = G_ALERTOTHER;
1402                                         break;
1403                                 default:
1404                                         debug_print(
1405                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1406                         }
1407
1408                         if (val == G_ALERTDEFAULT) {
1409                                 if (gtk_signal_n_emissions_by_name
1410                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1411                                                 gtk_signal_emit_stop_by_name
1412                                                         (GTK_OBJECT(ctree),
1413                                                          "key_press_event");
1414                                 folderview_select_next_unread(summaryview->folderview);
1415                                 return;
1416                         } 
1417                         else
1418                                 return;
1419                 } else
1420                         summary_select_node(summaryview, node, TRUE, FALSE);
1421
1422         }
1423 }
1424
1425 void summary_select_prev_new(SummaryView *summaryview)
1426 {
1427         GtkCTreeNode *node;
1428
1429         node = summary_find_prev_flagged_msg
1430                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1431
1432         if (!node) {
1433                 AlertValue val;
1434
1435                 val = alertpanel(_("No more new messages"),
1436                                  _("No new message found. "
1437                                    "Search from the end?"),
1438                                  _("Yes"), _("No"), NULL);
1439                 if (val != G_ALERTDEFAULT) return;
1440                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1441                                                      MSG_NEW, FALSE);
1442         }
1443
1444         if (!node)
1445                 alertpanel_notice(_("No new messages."));
1446         else
1447                 summary_select_node(summaryview, node, TRUE, FALSE);
1448 }
1449
1450 void summary_select_next_new(SummaryView *summaryview)
1451 {
1452         GtkCTreeNode *node = summaryview->selected;
1453         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1454
1455         while ((node = summary_find_next_flagged_msg
1456                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1457                 AlertValue val;
1458
1459                 val = alertpanel(_("No more new messages"),
1460                                  _("No new message found. "
1461                                    "Go to next folder?"),
1462                                  _("Yes"), _("Search again"), _("No"));
1463                 if (val == G_ALERTDEFAULT) {
1464                         if (gtk_signal_n_emissions_by_name
1465                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1466                                         gtk_signal_emit_stop_by_name
1467                                                 (GTK_OBJECT(ctree),
1468                                                  "key_press_event");
1469                         folderview_select_next_unread(summaryview->folderview);
1470                         return;
1471                 } else if (val == G_ALERTALTERNATE)
1472                         node = NULL;
1473                 else
1474                         return;
1475         }
1476
1477         if (node)
1478                 summary_select_node(summaryview, node, TRUE, FALSE);
1479 }
1480
1481 void summary_select_prev_marked(SummaryView *summaryview)
1482 {
1483         GtkCTreeNode *node;
1484
1485         node = summary_find_prev_flagged_msg
1486                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1487
1488         if (!node) {
1489                 AlertValue val;
1490
1491                 val = alertpanel(_("No more marked messages"),
1492                                  _("No marked message found. "
1493                                    "Search from the end?"),
1494                                  _("Yes"), _("No"), NULL);
1495                 if (val != G_ALERTDEFAULT) return;
1496                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1497                                                      MSG_MARKED, TRUE);
1498         }
1499
1500         if (!node)
1501                 alertpanel_notice(_("No marked messages."));
1502         else
1503                 summary_select_node(summaryview, node, TRUE, FALSE);
1504 }
1505
1506 void summary_select_next_marked(SummaryView *summaryview)
1507 {
1508         GtkCTreeNode *node;
1509
1510         node = summary_find_next_flagged_msg
1511                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1512
1513         if (!node) {
1514                 AlertValue val;
1515
1516                 val = alertpanel(_("No more marked messages"),
1517                                  _("No marked message found. "
1518                                    "Search from the beginning?"),
1519                                  _("Yes"), _("No"), NULL);
1520                 if (val != G_ALERTDEFAULT) return;
1521                 node = summary_find_next_flagged_msg(summaryview, NULL,
1522                                                      MSG_MARKED, TRUE);
1523         }
1524
1525         if (!node)
1526                 alertpanel_notice(_("No marked messages."));
1527         else
1528                 summary_select_node(summaryview, node, TRUE, FALSE);
1529 }
1530
1531 void summary_select_prev_labeled(SummaryView *summaryview)
1532 {
1533         GtkCTreeNode *node;
1534
1535         node = summary_find_prev_flagged_msg
1536                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1537
1538         if (!node) {
1539                 AlertValue val;
1540
1541                 val = alertpanel(_("No more labeled messages"),
1542                                  _("No labeled message found. "
1543                                    "Search from the end?"),
1544                                  _("Yes"), _("No"), NULL);
1545                 if (val != G_ALERTDEFAULT) return;
1546                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1547                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1548         }
1549
1550         if (!node)
1551                 alertpanel_notice(_("No labeled messages."));
1552         else
1553                 summary_select_node(summaryview, node, TRUE, FALSE);
1554 }
1555
1556 void summary_select_next_labeled(SummaryView *summaryview)
1557 {
1558         GtkCTreeNode *node;
1559
1560         node = summary_find_next_flagged_msg
1561                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1562
1563         if (!node) {
1564                 AlertValue val;
1565
1566                 val = alertpanel(_("No more labeled messages"),
1567                                  _("No labeled message found. "
1568                                    "Search from the beginning?"),
1569                                  _("Yes"), _("No"), NULL);
1570                 if (val != G_ALERTDEFAULT) return;
1571                 node = summary_find_next_flagged_msg(summaryview, NULL,
1572                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1573         }
1574
1575         if (!node)
1576                 alertpanel_notice(_("No labeled messages."));
1577         else
1578                 summary_select_node(summaryview, node, TRUE, FALSE);
1579 }
1580
1581 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1582 {
1583         GtkCTreeNode *node;
1584
1585         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1586         summary_select_node(summaryview, node, FALSE, TRUE);
1587 }
1588
1589 /**
1590  * summary_select_node:
1591  * @summaryview: Summary view.
1592  * @node: Summary tree node.
1593  * @display_msg: TRUE to display the selected message.
1594  * @do_refresh: TRUE to refresh the widget.
1595  *
1596  * Select @node (bringing it into view by scrolling and expanding its
1597  * thread, if necessary) and unselect all others.  If @display_msg is
1598  * TRUE, display the corresponding message in the message view.
1599  * If @do_refresh is TRUE, the widget is refreshed.
1600  **/
1601 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1602                          gboolean display_msg, gboolean do_refresh)
1603 {
1604         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1605
1606         if (node) {
1607                 gtkut_ctree_expand_parent_all(ctree, node);
1608                 if (do_refresh) {
1609                         GTK_EVENTS_FLUSH();
1610                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1611                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1612                 }
1613                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1614                 if (display_msg && summaryview->displayed == node)
1615                         summaryview->displayed = NULL;
1616                 summaryview->display_msg = display_msg;
1617                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1618         }
1619 }
1620
1621 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1622 {
1623         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1624         MsgInfo *msginfo;
1625
1626         if (!node)
1627                 return 0;
1628         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1629         return msginfo->msgnum;
1630 }
1631
1632 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1633                                            GtkCTreeNode *current_node)
1634 {
1635         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1636         GtkCTreeNode *node;
1637         MsgInfo *msginfo;
1638
1639         if (current_node)
1640                 node = current_node;
1641         else
1642                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1643
1644         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1645                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1646                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1647         }
1648
1649         return node;
1650 }
1651
1652 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1653                                            GtkCTreeNode *current_node)
1654 {
1655         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1656         GtkCTreeNode *node;
1657         MsgInfo *msginfo;
1658
1659         if (current_node)
1660                 node = current_node;
1661         else
1662                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1663
1664         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1665                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1666                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1667         }
1668
1669         return node;
1670 }
1671
1672 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1673                                                    GtkCTreeNode *current_node,
1674                                                    MsgPermFlags flags,
1675                                                    gboolean start_from_prev)
1676 {
1677         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1678         GtkCTreeNode *node;
1679         MsgInfo *msginfo;
1680
1681         if (current_node) {
1682                 if (start_from_prev)
1683                         node = GTK_CTREE_NODE_PREV(current_node);
1684                 else
1685                         node = current_node;
1686         } else
1687                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1688
1689         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1690                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1691                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1692         }
1693
1694         return node;
1695 }
1696
1697 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1698                                                    GtkCTreeNode *current_node,
1699                                                    MsgPermFlags flags,
1700                                                    gboolean start_from_next)
1701 {
1702         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1703         GtkCTreeNode *node;
1704         MsgInfo *msginfo;
1705
1706         if (current_node) {
1707                 if (start_from_next)
1708                         node = gtkut_ctree_node_next(ctree, current_node);
1709                 else
1710                         node = current_node;
1711         } else
1712                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1713
1714         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1715                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1716                 /* Find msg with matching flags but ignore messages with
1717                    ignore flags, if searching for new or unread messages */
1718                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1719                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1720                         break;
1721         }
1722
1723         return node;
1724 }
1725
1726 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1727                                                 guint msgnum)
1728 {
1729         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1730         GtkCTreeNode *node;
1731         MsgInfo *msginfo;
1732
1733         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1734
1735         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1736                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1737                 if (msginfo && msginfo->msgnum == msgnum) break;
1738         }
1739
1740         return node;
1741 }
1742
1743 static guint attract_hash_func(gconstpointer key)
1744 {
1745         gchar *str;
1746         gchar *p;
1747         guint h;
1748
1749         Xstrdup_a(str, (const gchar *)key, return 0);
1750         trim_subject(str);
1751
1752         p = str;
1753         h = *p;
1754
1755         if (h) {
1756                 for (p += 1; *p != '\0'; p++)
1757                         h = (h << 5) - h + *p;
1758         }
1759
1760         return h;
1761 }
1762
1763 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1764 {
1765         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1766 }
1767
1768 void summary_attract_by_subject(SummaryView *summaryview)
1769 {
1770         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1771         GtkCList *clist = GTK_CLIST(ctree);
1772         GtkCTreeNode *src_node;
1773         GtkCTreeNode *dst_node, *sibling;
1774         GtkCTreeNode *tmp;
1775         MsgInfo *src_msginfo, *dst_msginfo;
1776         GHashTable *subject_table;
1777
1778         debug_print("Attracting messages by subject...");
1779         STATUSBAR_PUSH(summaryview->mainwin,
1780                        _("Attracting messages by subject..."));
1781
1782         main_window_cursor_wait(summaryview->mainwin);
1783         gtk_clist_freeze(clist);
1784
1785         subject_table = g_hash_table_new(attract_hash_func,
1786                                          attract_compare_func);
1787
1788         for (src_node = GTK_CTREE_NODE(clist->row_list);
1789              src_node != NULL;
1790              src_node = tmp) {
1791                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1792                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1793                 if (!src_msginfo) continue;
1794                 if (!src_msginfo->subject) continue;
1795
1796                 /* find attracting node */
1797                 dst_node = g_hash_table_lookup(subject_table,
1798                                                src_msginfo->subject);
1799
1800                 if (dst_node) {
1801                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1802
1803                         /* if the time difference is more than 20 days,
1804                            don't attract */
1805                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1806                             > 60 * 60 * 24 * 20)
1807                                 continue;
1808
1809                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1810                         if (src_node != sibling)
1811                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1812                 }
1813
1814                 g_hash_table_insert(subject_table,
1815                                     src_msginfo->subject, src_node);
1816         }
1817
1818         g_hash_table_destroy(subject_table);
1819
1820         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1821
1822         gtk_clist_thaw(clist);
1823
1824         debug_print("done.\n");
1825         STATUSBAR_POP(summaryview->mainwin);
1826
1827         main_window_cursor_normal(summaryview->mainwin);
1828 }
1829
1830 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1831                                       gpointer data)
1832 {
1833         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1834
1835         if (msginfo)
1836                 procmsg_msginfo_free(msginfo);
1837 }
1838
1839 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1840                                    gpointer data)
1841 {
1842         SummaryView *summaryview = data;
1843         MsgInfo *msginfo;
1844
1845         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1846
1847         if (MSG_IS_NEWS(msginfo->flags))
1848                 news_flag_crosspost(msginfo);
1849
1850         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1851                 summaryview->newmsgs++;
1852         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1853                 summaryview->unread++;
1854         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1855         && procmsg_msg_has_marked_parent(msginfo))
1856                 summaryview->unreadmarked++;
1857
1858         if (MSG_IS_DELETED(msginfo->flags))
1859                 summaryview->deleted++;
1860
1861         summaryview->messages++;
1862         summaryview->total_size += msginfo->size;
1863
1864         summary_set_row_marks(summaryview, node);
1865 }
1866
1867 static void summary_update_status(SummaryView *summaryview)
1868 {
1869         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1870         GtkCTreeNode *node;
1871         MsgInfo *msginfo;
1872
1873         summaryview->newmsgs = summaryview->unread =
1874         summaryview->messages = summaryview->total_size =
1875         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1876
1877         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1878              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1879                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1880
1881                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1882                         summaryview->newmsgs++;
1883                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1884                         summaryview->unread++;
1885                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1886                 && procmsg_msg_has_marked_parent(msginfo))
1887                         summaryview->unreadmarked++;
1888                 if (MSG_IS_DELETED(msginfo->flags))
1889                         summaryview->deleted++;
1890                 if (MSG_IS_MOVE(msginfo->flags))
1891                         summaryview->moved++;
1892                 if (MSG_IS_COPY(msginfo->flags))
1893                         summaryview->copied++;
1894                 summaryview->messages++;
1895                 summaryview->total_size += msginfo->size;
1896         }
1897 }
1898
1899 static void summary_status_show(SummaryView *summaryview)
1900 {
1901         gchar *str;
1902         gchar *del, *mv, *cp;
1903         gchar *sel;
1904         gchar *spc;
1905         gchar *itstr;
1906         GList *rowlist, *cur;
1907         guint n_selected = 0;
1908         off_t sel_size = 0;
1909         MsgInfo *msginfo;
1910
1911         if (!summaryview->folder_item) {
1912                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1913                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1914                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1915                 return;
1916         }
1917
1918         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1919         for (cur = rowlist; cur != NULL; cur = cur->next) {
1920                 msginfo = gtk_ctree_node_get_row_data
1921                         (GTK_CTREE(summaryview->ctree),
1922                          GTK_CTREE_NODE(cur->data));
1923                 if (!msginfo)
1924                         g_warning("summary_status_show(): msginfo == NULL\n");
1925                 else {
1926                         sel_size += msginfo->size;
1927                         n_selected++;
1928                 }
1929         }
1930
1931         if (summaryview->folder_item->folder->type == F_NEWS) {
1932                 gchar *group;
1933                 group = get_abbrev_newsgroup_name
1934                         (g_basename(summaryview->folder_item->path),
1935                          prefs_common.ng_abbrev_len);
1936                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), group);
1937                 g_free(group);
1938         } else {
1939                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1940                               summaryview->folder_item->path);
1941         }
1942
1943         if (summaryview->deleted)
1944                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1945         else
1946                 del = g_strdup("");
1947         if (summaryview->moved)
1948                 mv = g_strdup_printf(_("%s%d moved"),
1949                                      summaryview->deleted ? _(", ") : "",
1950                                      summaryview->moved);
1951         else
1952                 mv = g_strdup("");
1953         if (summaryview->copied)
1954                 cp = g_strdup_printf(_("%s%d copied"),
1955                                      summaryview->deleted ||
1956                                      summaryview->moved ? _(", ") : "",
1957                                      summaryview->copied);
1958         else
1959                 cp = g_strdup("");
1960
1961         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1962                 spc = "    ";
1963         else
1964                 spc = "";
1965
1966         if (n_selected) {
1967                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1968                 if (n_selected == 1)
1969                         itstr = g_strdup(_(" item selected"));
1970                 else
1971                         itstr = g_strdup(_(" items selected"));
1972         } else {
1973                 sel = g_strdup("");
1974                 itstr = g_strdup("");
1975         }
1976                 
1977         str = g_strconcat(n_selected ? itos(n_selected) : "",
1978                                         itstr, sel, spc, del, mv, cp, NULL);
1979         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1980         g_free(str);
1981         g_free(sel);
1982         g_free(del);
1983         g_free(mv);
1984         g_free(cp);
1985         g_free(itstr);
1986
1987         if (FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1988                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1989                                       summaryview->newmsgs,
1990                                       summaryview->unread,
1991                                       summaryview->messages,
1992                                       to_human_readable(summaryview->total_size));
1993         } else {
1994                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1995                                       summaryview->newmsgs,
1996                                       summaryview->unread,
1997                                       summaryview->messages);
1998         }
1999         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
2000         g_free(str);
2001 }
2002
2003 static void summary_set_column_titles(SummaryView *summaryview)
2004 {
2005         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2006         GtkWidget *hbox;
2007         GtkWidget *label;
2008         GtkWidget *arrow;
2009         gint pos;
2010         const gchar *title;
2011         SummaryColumnType type;
2012         gboolean single_char;
2013         GtkJustification justify;
2014
2015         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2016                 SORT_BY_MARK,
2017                 SORT_BY_STATUS,
2018                 SORT_BY_MIME,
2019                 SORT_BY_SUBJECT,
2020                 SORT_BY_FROM,
2021                 SORT_BY_DATE,
2022                 SORT_BY_SIZE,
2023                 SORT_BY_NUMBER,
2024                 SORT_BY_SCORE,
2025                 SORT_BY_LOCKED
2026         };
2027
2028         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2029                 type = summaryview->col_state[pos].type;
2030
2031                 /* CLAWS: mime and unread are single char headers */
2032                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
2033                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2034                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2035
2036                 switch (type) {
2037                 case S_COL_SUBJECT:
2038                 case S_COL_FROM:
2039                 case S_COL_DATE:
2040                 case S_COL_NUMBER:
2041                         if (prefs_common.trans_hdr)
2042                                 title = gettext(col_label[type]);
2043                         else
2044                                 title = col_label[type];
2045                         break;
2046                 /* CLAWS: dummies for mark and locked headers */        
2047                 case S_COL_MARK:        
2048                 case S_COL_LOCKED:
2049                         title = "";
2050                         break;
2051                 default:
2052                         title = gettext(col_label[type]);
2053                 }
2054
2055                 if (type == S_COL_MIME) {
2056                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
2057                         gtk_widget_show(label);
2058                         gtk_clist_set_column_widget(clist, pos, label);
2059                         continue;
2060                 }
2061                 if (single_char) {
2062                         gtk_clist_set_column_title(clist, pos, title);
2063                         continue;
2064                 }
2065
2066                 /* CLAWS: changed so that locked and mark headers
2067                  * show a pixmap instead of single character */
2068                 hbox  = gtk_hbox_new(FALSE, 4);
2069                 
2070                 if (type == S_COL_LOCKED)
2071                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
2072                 else if (type == S_COL_MARK) 
2073                         label = gtk_pixmap_new(markxpm, markxpmmask);
2074                 else 
2075                         label = gtk_label_new(title);
2076                 
2077                 if (justify == GTK_JUSTIFY_RIGHT)
2078                         gtk_box_pack_end(GTK_BOX(hbox), label,
2079                                          FALSE, FALSE, 0);
2080                 else
2081                         gtk_box_pack_start(GTK_BOX(hbox), label,
2082                                            FALSE, FALSE, 0);
2083
2084                 if (summaryview->sort_key == sort_by[type]) {
2085                         arrow = gtk_arrow_new
2086                                 (summaryview->sort_type == SORT_ASCENDING
2087                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2088                                  GTK_SHADOW_IN);
2089                         if (justify == GTK_JUSTIFY_RIGHT)
2090                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2091                                                    FALSE, FALSE, 0);
2092                         else
2093                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2094                                                  FALSE, FALSE, 0);
2095                 }
2096
2097                 gtk_widget_show_all(hbox);
2098                 gtk_clist_set_column_widget(clist, pos, hbox);
2099         }
2100 }
2101
2102 void summary_sort(SummaryView *summaryview,
2103                   FolderSortKey sort_key, FolderSortType sort_type)
2104 {
2105         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2106         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2107         GtkCListCompareFunc cmp_func = NULL;
2108
2109         switch (sort_key) {
2110         case SORT_BY_MARK:
2111                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2112                 break;
2113         case SORT_BY_STATUS:
2114                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2115                 break;
2116         case SORT_BY_MIME:
2117                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2118                 break;
2119         case SORT_BY_NUMBER:
2120                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2121                 break;
2122         case SORT_BY_SIZE:
2123                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2124                 break;
2125         case SORT_BY_DATE:
2126                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2127                 break;
2128         case SORT_BY_FROM:
2129                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2130                 break;
2131         case SORT_BY_SUBJECT:
2132                 if (summaryview->simplify_subject_preg)
2133                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2134                 else
2135                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2136                 break;
2137         case SORT_BY_SCORE:
2138                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2139                 break;
2140         case SORT_BY_LABEL:
2141                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2142                 break;
2143         case SORT_BY_TO:
2144                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2145                 break;
2146         case SORT_BY_LOCKED:
2147                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2148                 break;
2149         case SORT_BY_NONE:
2150                 cmp_func = NULL;
2151                 return;
2152         default:
2153                 return;
2154         }
2155
2156         summaryview->sort_key = sort_key;
2157         summaryview->sort_type = sort_type;
2158
2159         summary_set_column_titles(summaryview);
2160         summary_set_menu_sensitive(summaryview);
2161         if(cmp_func != NULL) {
2162                 debug_print("Sorting summary...");
2163                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2164
2165                 main_window_cursor_wait(summaryview->mainwin);
2166
2167                 gtk_clist_set_compare_func(clist, cmp_func);
2168
2169                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2170
2171                 gtk_sctree_sort_recursive(ctree, NULL);
2172
2173                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2174
2175                 main_window_cursor_normal(summaryview->mainwin);
2176
2177                 debug_print("done.\n");
2178                 STATUSBAR_POP(summaryview->mainwin);
2179         }
2180 }
2181
2182 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2183                                    GtkCTreeNode *cnode, gpointer data)
2184 {
2185         SummaryView *summaryview = (SummaryView *)data;
2186         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2187         gchar *text[N_SUMMARY_COLS];
2188         gint *col_pos = summaryview->col_pos;
2189         const gchar *msgid = msginfo->msgid;
2190         GHashTable *msgid_table = summaryview->msgid_table;
2191
2192         summary_set_header(summaryview, text, msginfo);
2193
2194         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2195                                 NULL, NULL, NULL, NULL, FALSE,
2196                                 gnode->parent->parent ? TRUE : FALSE);
2197 #define SET_TEXT(col) \
2198         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2199                                 text[col_pos[col]])
2200
2201         SET_TEXT(S_COL_NUMBER);
2202         SET_TEXT(S_COL_SCORE);
2203         SET_TEXT(S_COL_SIZE);
2204         SET_TEXT(S_COL_DATE);
2205         SET_TEXT(S_COL_FROM);
2206         SET_TEXT(S_COL_SUBJECT);
2207
2208 #undef SET_TEXT
2209
2210         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2211         summary_set_marks_func(ctree, cnode, summaryview);
2212
2213         if (msgid && msgid[0] != '\0')
2214                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2215
2216         return TRUE;
2217 }
2218
2219 static void summary_set_ctree_from_list(SummaryView *summaryview,
2220                                         GSList *mlist)
2221 {
2222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2223         MsgInfo *msginfo;
2224                 GtkCTreeNode *node = NULL;
2225         GHashTable *msgid_table;
2226         GHashTable *subject_table;
2227         GSList * cur;
2228         
2229         if (!mlist) return;
2230
2231         debug_print("\tSetting summary from message data...");
2232         STATUSBAR_PUSH(summaryview->mainwin,
2233                        _("Setting summary from message data..."));
2234         gdk_flush();
2235
2236         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2237         summaryview->msgid_table = msgid_table;
2238         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2239         summaryview->subject_table = subject_table;
2240
2241         if (prefs_common.use_addr_book)
2242                 start_address_completion();
2243         
2244         for (cur = mlist ; cur != NULL; cur = cur->next) {
2245                 msginfo = (MsgInfo *)cur->data;
2246                 msginfo->threadscore = msginfo->score;
2247         }
2248
2249         if (summaryview->threaded) {
2250                 GNode *root, *gnode;
2251
2252                 root = procmsg_get_thread_tree(mlist);
2253
2254                 for (gnode = root->children; gnode != NULL;
2255                      gnode = gnode->next) {
2256                         node = gtk_ctree_insert_gnode
2257                                 (ctree, NULL, node, gnode,
2258                                  summary_insert_gnode_func, summaryview);
2259                 }
2260
2261                 g_node_destroy(root);
2262
2263                 summary_thread_init(summaryview);
2264         } else {
2265                 gchar *text[N_SUMMARY_COLS];
2266
2267                 for (; mlist != NULL; mlist = mlist->next) {
2268                         msginfo = (MsgInfo *)mlist->data;
2269
2270                         summary_set_header(summaryview, text, msginfo);
2271
2272                         node = gtk_ctree_insert_node
2273                                 (ctree, NULL, node, text, 2,
2274                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2275                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2276                         summary_set_marks_func(ctree, node, summaryview);
2277
2278                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2279                                 g_hash_table_insert(msgid_table,
2280                                                     msginfo->msgid, node);
2281
2282                         subject_table_insert(subject_table,
2283                                              msginfo->subject,
2284                                              node);
2285                 }
2286         }
2287
2288         if (prefs_common.enable_hscrollbar &&
2289             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2290                 gint optimal_width;
2291
2292                 optimal_width = gtk_clist_optimal_column_width
2293                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2294                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2295                                            summaryview->col_pos[S_COL_SUBJECT],
2296                                            optimal_width);
2297         }
2298
2299         if (prefs_common.use_addr_book)
2300                 end_address_completion();
2301
2302         debug_print("done.\n");
2303         STATUSBAR_POP(summaryview->mainwin);
2304         if (debug_get_mode()) {
2305                 debug_print("\tmsgid hash table size = %d\n",
2306                             g_hash_table_size(msgid_table));
2307                 debug_print("\tsubject hash table size = %d\n",
2308                             g_hash_table_size(subject_table));
2309         }
2310 }
2311
2312 static gchar *summary_complete_address(const gchar *addr)
2313 {
2314         gint count;
2315         gchar *res, *tmp, *email_addr;
2316
2317         Xstrdup_a(email_addr, addr, return NULL);
2318         extract_address(email_addr);
2319         g_return_val_if_fail(*email_addr, NULL);
2320
2321         /*
2322          * completion stuff must be already initialized
2323          */
2324         res = NULL;
2325         if (1 < (count = complete_address(email_addr))) {
2326                 tmp = get_complete_address(1);
2327                 res = procheader_get_fromname(tmp);
2328                 g_free(tmp);    
2329         }
2330         
2331         return res;
2332 }
2333
2334 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2335                                MsgInfo *msginfo)
2336 {
2337         static gchar date_modified[80];
2338         static gchar *to = NULL;
2339         static gchar col_score[11];
2340         static gchar buf[BUFFSIZE];
2341         gint *col_pos = summaryview->col_pos;
2342
2343         text[col_pos[S_COL_MARK]]   = NULL;
2344         text[col_pos[S_COL_STATUS]] = NULL;
2345         text[col_pos[S_COL_MIME]]   = NULL;
2346         text[col_pos[S_COL_LOCKED]] = NULL;
2347         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2348         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2349         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2350
2351         if (msginfo->date_t) {
2352                 procheader_date_get_localtime(date_modified,
2353                                               sizeof(date_modified),
2354                                               msginfo->date_t);
2355                 text[col_pos[S_COL_DATE]] = date_modified;
2356         } else if (msginfo->date)
2357                 text[col_pos[S_COL_DATE]] = msginfo->date;
2358         else
2359                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2360
2361         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2362                 _("(No From)");
2363         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2364             !MSG_IS_NEWS(msginfo->flags)) {
2365                 gchar *addr = NULL;
2366
2367                 Xstrdup_a(addr, msginfo->from, return);
2368                 extract_address(addr);
2369
2370                 if (prefs_common.use_addr_book) {
2371                         if (account_find_from_address(addr)) {
2372                                 addr = summary_complete_address(msginfo->to);
2373                                 g_free(to);
2374                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2375                                 text[col_pos[S_COL_FROM]] = to;
2376                                 g_free(addr);
2377                         }
2378                 } else {
2379                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2380                                 g_free(to);
2381                                 to = g_strconcat("-->", msginfo->to, NULL);
2382                                 text[col_pos[S_COL_FROM]] = to;
2383                         }
2384                 }
2385         }
2386
2387         /*
2388          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2389          * checking whether the above block (which handles the special case of
2390          * the --> in sent boxes) was executed.
2391          */
2392         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2393                 gchar *from = summary_complete_address(msginfo->from);
2394                 if (from) {
2395                         g_free(to);
2396                         to = from;
2397                         text[col_pos[S_COL_FROM]] = to;
2398                 }                       
2399         }
2400
2401         if (summaryview->simplify_subject_preg != NULL)
2402                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2403                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2404                                         summaryview->simplify_subject_preg) : 
2405                         _("(No Subject)");
2406         else 
2407                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2408                         _("(No Subject)");
2409 }
2410
2411 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2412 {
2413         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2414 }
2415
2416 static void summary_display_msg_full(SummaryView *summaryview,
2417                                      GtkCTreeNode *row,
2418                                      gboolean new_window, gboolean all_headers)
2419 {
2420         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2421         MsgInfo *msginfo;
2422         MsgFlags flags;
2423         gchar *filename;
2424
2425         if (!new_window && summaryview->displayed == row) return;
2426         g_return_if_fail(row != NULL);
2427
2428         if (summary_is_locked(summaryview)) return;
2429         summary_lock(summaryview);
2430
2431         STATUSBAR_POP(summaryview->mainwin);
2432         GTK_EVENTS_FLUSH();
2433
2434         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2435
2436         filename = procmsg_get_message_file(msginfo);
2437         if (!filename) {
2438                 summary_unlock(summaryview);
2439                 return;
2440         }
2441         g_free(filename);
2442
2443         if (new_window || !prefs_common.mark_as_read_on_new_window) {
2444                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2445                         summaryview->newmsgs--;
2446                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2447                         summaryview->unread--;
2448                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2449                 && procmsg_msg_has_marked_parent(msginfo))
2450                         summaryview->unreadmarked--;
2451                 if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2452                         procmsg_msginfo_unset_flags
2453                                 (msginfo, MSG_NEW | MSG_UNREAD, 0);
2454                         summary_set_row_marks(summaryview, row);
2455                         gtk_clist_thaw(GTK_CLIST(ctree));
2456                         summary_status_show(summaryview);
2457                         
2458                         flags = msginfo->flags;
2459                 }
2460         }
2461
2462         if (new_window) {
2463                 MessageView *msgview;
2464
2465                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2466                 messageview_show(msgview, msginfo, all_headers);
2467         } else {
2468                 MessageView *msgview;
2469
2470                 msgview = summaryview->messageview;
2471
2472                 summaryview->displayed = row;
2473                 if (!messageview_is_visible(msgview))
2474                         main_window_toggle_message_view(summaryview->mainwin);
2475                 messageview_show(msgview, msginfo, all_headers);
2476                 if (msgview->type == MVIEW_TEXT ||
2477                     (msgview->type == MVIEW_MIME &&
2478                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2479                       gtk_notebook_get_current_page
2480                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2481                         gtk_widget_grab_focus(summaryview->ctree);
2482                 GTK_EVENTS_FLUSH();
2483                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2484         }
2485
2486         summary_set_menu_sensitive(summaryview);
2487         toolbar_main_set_sensitive(summaryview->mainwin);
2488
2489         summary_unlock(summaryview);
2490 }
2491
2492 void summary_display_msg_selected(SummaryView *summaryview,
2493                                   gboolean all_headers)
2494 {
2495         if (summary_is_locked(summaryview)) return;
2496         summaryview->displayed = NULL;
2497         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2498                                  all_headers);
2499 }
2500
2501 void summary_redisplay_msg(SummaryView *summaryview)
2502 {
2503         GtkCTreeNode *node;
2504
2505         if (summaryview->displayed) {
2506                 node = summaryview->displayed;
2507                 summaryview->displayed = NULL;
2508                 summary_display_msg(summaryview, node);
2509         }
2510 }
2511
2512 void summary_open_msg(SummaryView *summaryview)
2513 {
2514         if (!summaryview->selected) return;
2515
2516         summary_display_msg_full(summaryview, summaryview->selected,
2517                                  TRUE, FALSE);
2518 }
2519
2520 void summary_view_source(SummaryView * summaryview)
2521 {
2522         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2523         MsgInfo *msginfo;
2524         SourceWindow *srcwin;
2525
2526         if (!summaryview->selected) return;
2527
2528         srcwin = source_window_create();
2529         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2530         source_window_show_msg(srcwin, msginfo);
2531         source_window_show(srcwin);
2532 }
2533
2534 void summary_reedit(SummaryView *summaryview)
2535 {
2536         MsgInfo *msginfo;
2537
2538         if (!summaryview->selected) return;
2539         if (!summaryview->folder_item) return;
2540         if (summaryview->folder_item->stype != F_OUTBOX &&
2541             summaryview->folder_item->stype != F_DRAFT  &&
2542             summaryview->folder_item->stype != F_QUEUE) return;
2543
2544         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2545                                               summaryview->selected);
2546         if (!msginfo) return;
2547
2548         compose_reedit(msginfo);
2549 }
2550
2551 void summary_step(SummaryView *summaryview, GtkScrollType type)
2552 {
2553         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2554         GtkCTreeNode *node;
2555
2556         if (summary_is_locked(summaryview)) return;
2557
2558         if (type == GTK_SCROLL_STEP_FORWARD) {
2559                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2560                 if (node)
2561                         gtkut_ctree_expand_parent_all(ctree, node);
2562                 else
2563                         return;
2564         } else {
2565                 if (summaryview->selected) {
2566                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2567                         if (!node) return;
2568                 }
2569         }
2570
2571         if (messageview_is_visible(summaryview->messageview))
2572                 summaryview->display_msg = TRUE;
2573
2574         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2575                                 type, 0.0);
2576
2577         if (GTK_CLIST(ctree)->selection)
2578                 gtk_sctree_set_anchor_row
2579                         (GTK_SCTREE(ctree),
2580                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2581
2582 }
2583
2584 void summary_toggle_view(SummaryView *summaryview)
2585 {
2586         if (!messageview_is_visible(summaryview->messageview) &&
2587             summaryview->selected)
2588                 summary_display_msg(summaryview,
2589                                     summaryview->selected);
2590         else
2591                 main_window_toggle_message_view(summaryview->mainwin);
2592 }
2593
2594 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2595                                                 GtkCTreeNode *node)
2596 {
2597         MsgInfo *msginfo;
2598
2599         if (node) {
2600                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2601                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2602                         return TRUE;
2603                 node = GTK_CTREE_ROW(node)->children;
2604         } else
2605                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2606
2607         while (node) {
2608                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2609                         return TRUE;
2610                 node = GTK_CTREE_ROW(node)->sibling;
2611         }
2612
2613         return FALSE;
2614 }
2615
2616 static gboolean summary_have_unread_children(SummaryView *summaryview,
2617                                              GtkCTreeNode *node)
2618 {
2619         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2620
2621         if (!node) return FALSE;
2622
2623         node = GTK_CTREE_ROW(node)->children;
2624
2625         while (node) {
2626                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2627                         return TRUE;
2628                 node = GTK_CTREE_ROW(node)->sibling;
2629         }
2630
2631         return FALSE;
2632 }
2633
2634 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2635 {
2636         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2637         GtkStyle *style = NULL;
2638         MsgInfo *msginfo;
2639         MsgFlags flags;
2640         gint *col_pos = summaryview->col_pos;
2641
2642         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2643         if (!msginfo) return;
2644
2645         flags = msginfo->flags;
2646
2647         gtk_ctree_node_set_foreground(ctree, row, NULL);
2648
2649         /* set new/unread column */
2650         if (MSG_IS_IGNORE_THREAD(flags)) {
2651                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2652                                           ignorethreadxpm, ignorethreadxpmmask);
2653         } else if (MSG_IS_NEW(flags)) {
2654                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2655                                           newxpm, newxpmmask);
2656         } else if (MSG_IS_UNREAD(flags)) {
2657                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2658                                           unreadxpm, unreadxpmmask);
2659         } else if (MSG_IS_REPLIED(flags)) {
2660                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2661                                           repliedxpm, repliedxpmmask);
2662         } else if (MSG_IS_FORWARDED(flags)) {
2663                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2664                                           forwardedxpm, forwardedxpmmask);
2665         } else {
2666                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2667                                         NULL);
2668         }
2669
2670         if (prefs_common.bold_unread &&
2671             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2672              (!GTK_CTREE_ROW(row)->expanded &&
2673               GTK_CTREE_ROW(row)->children &&
2674               summary_have_unread_children(summaryview, row))))
2675                 style = bold_style;
2676
2677         /* set mark column */
2678         if (MSG_IS_DELETED(flags)) {
2679                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2680                                           deletedxpm, deletedxpmmask);
2681                 if (style)
2682                         style = bold_deleted_style;
2683                 else {
2684                         style = small_deleted_style;
2685                 }
2686                         gtk_ctree_node_set_foreground
2687                                 (ctree, row, &summaryview->color_dim);
2688         } else if (MSG_IS_MARKED(flags)) {
2689                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2690                                           markxpm, markxpmmask);
2691         } else if (MSG_IS_MOVE(flags)) {
2692                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2693                 if (style)
2694                         style = bold_marked_style;
2695                 else {
2696                         style = small_marked_style;
2697                 }
2698                         gtk_ctree_node_set_foreground
2699                                 (ctree, row, &summaryview->color_marked);
2700         } else if (MSG_IS_COPY(flags)) {
2701                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2702                 if (style)
2703                         style = bold_marked_style;
2704                 else {
2705                         style = small_marked_style;
2706                 }
2707                         gtk_ctree_node_set_foreground
2708                                 (ctree, row, &summaryview->color_marked);
2709         }
2710         else if ((global_scoring ||
2711                   summaryview->folder_item->prefs->scoring) &&
2712                  (msginfo->score >= summaryview->important_score) &&
2713                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2714                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2715                 gtk_ctree_node_set_foreground(ctree, row,
2716                                               &summaryview->color_important);
2717         } else {
2718                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2719         }
2720
2721         if (MSG_IS_LOCKED(flags)) {
2722                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2723                                           lockedxpm, lockedxpmmask);
2724         }
2725         else {
2726                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2727         }
2728
2729         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2730                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2731                                           clipkeyxpm, clipkeyxpmmask);
2732         } else if (MSG_IS_ENCRYPTED(flags)) {
2733                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2734                                           keyxpm, keyxpmmask);
2735         } else if (MSG_IS_MIME(flags)) {
2736                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2737                                           clipxpm, clipxpmmask);
2738         } else {
2739                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2740         }
2741         if (!style)
2742                 style = small_style;
2743
2744         gtk_ctree_node_set_row_style(ctree, row, style);
2745
2746         if (MSG_GET_COLORLABEL(flags))
2747                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2748 }
2749
2750 void summary_set_marks_selected(SummaryView *summaryview)
2751 {
2752         GList *cur;
2753
2754         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2755              cur = cur->next)
2756                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2757 }
2758
2759 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2760 {
2761         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2762         MsgInfo *msginfo;
2763
2764         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2765         if (MSG_IS_DELETED(msginfo->flags))
2766                 summaryview->deleted--;
2767         if (MSG_IS_MOVE(msginfo->flags))
2768                 summaryview->moved--;
2769         if (MSG_IS_COPY(msginfo->flags))
2770                 summaryview->copied--;
2771         procmsg_update_unread_children (msginfo, TRUE);
2772
2773         procmsg_msginfo_set_to_folder(msginfo, NULL);
2774         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2775         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2776         summary_set_row_marks(summaryview, row);
2777         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2778 }
2779
2780 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2781 {
2782         /* almost verbatim summary_mark_row(); may want a menu action? */
2783         gboolean changed = FALSE;
2784         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2785         MsgInfo *msginfo;
2786
2787         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2788         if (MSG_IS_DELETED(msginfo->flags))
2789                 summaryview->deleted--;
2790         if (MSG_IS_MOVE(msginfo->flags)) {
2791                 summaryview->moved--;
2792                 changed = TRUE;
2793         }
2794         if (MSG_IS_COPY(msginfo->flags)) {
2795                 summaryview->copied--;
2796                 changed = TRUE;
2797         }
2798         procmsg_msginfo_set_to_folder(msginfo, NULL);
2799         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2800         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2801         summary_set_row_marks(summaryview, row);
2802         debug_print("Message %d is locked\n", msginfo->msgnum);
2803 }
2804
2805 void summary_mark(SummaryView *summaryview)
2806 {
2807         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2808         GList *cur;
2809
2810         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2811                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2812
2813         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2814         summary_status_show(summaryview);
2815 }
2816
2817 static void summary_mark_row_as_read(SummaryView *summaryview,
2818                                      GtkCTreeNode *row)
2819 {
2820         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2821         MsgInfo *msginfo;
2822
2823         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2824
2825         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2826                 return;
2827
2828         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2829                 summaryview->newmsgs--;
2830         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2831                 summaryview->unread--;
2832         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2833         && procmsg_msg_has_marked_parent(msginfo))
2834                 summaryview->unreadmarked--;
2835
2836         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2837         summary_set_row_marks(summaryview, row);
2838         debug_print("Message %d is marked as read\n",
2839                 msginfo->msgnum);
2840 }
2841
2842 void summary_mark_as_read(SummaryView *summaryview)
2843 {
2844         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2845         GList *cur;
2846
2847         folder_item_update_freeze();
2848         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2849                 summary_mark_row_as_read(summaryview,
2850                                          GTK_CTREE_NODE(cur->data));
2851         folder_item_update_thaw();
2852         
2853         summary_status_show(summaryview);
2854 }
2855
2856 void summary_mark_all_read(SummaryView *summaryview)
2857 {
2858         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2859         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2860         GtkCTreeNode *node;
2861
2862         gtk_clist_freeze(clist);
2863         folder_item_update_freeze();
2864         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2865              node = gtkut_ctree_node_next(ctree, node))
2866                 summary_mark_row_as_read(summaryview, node);
2867         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2868              node = gtkut_ctree_node_next(ctree, node)) {
2869                 if (!GTK_CTREE_ROW(node)->expanded)
2870                         summary_set_row_marks(summaryview, node);
2871         }
2872         gtk_clist_thaw(clist);
2873         folder_item_update_thaw();
2874
2875         summary_status_show(summaryview);
2876 }
2877
2878 static void summary_mark_row_as_unread(SummaryView *summaryview,
2879                                        GtkCTreeNode *row)
2880 {
2881         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2882         MsgInfo *msginfo;
2883
2884         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2885         if (MSG_IS_DELETED(msginfo->flags)) {
2886                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2887                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2888                 summaryview->deleted--;
2889         }
2890
2891         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2892                 summaryview->unread++;
2893
2894         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2895         && procmsg_msg_has_marked_parent(msginfo))
2896                 summaryview->unreadmarked++;
2897
2898         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2899         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2900         debug_print("Message %d is marked as unread\n",
2901                 msginfo->msgnum);
2902
2903         summary_set_row_marks(summaryview, row);
2904 }
2905
2906 void summary_mark_as_unread(SummaryView *summaryview)
2907 {
2908         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2909         GList *cur;
2910
2911         folder_item_update_freeze();
2912         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2913                 summary_mark_row_as_unread(summaryview,
2914                                            GTK_CTREE_NODE(cur->data));
2915         folder_item_update_thaw();
2916         
2917         summary_status_show(summaryview);
2918 }
2919
2920 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2921 {
2922         GList * cur;
2923         gboolean found;
2924
2925         switch (summaryview->folder_item->folder->type) {
2926
2927         case F_NEWS:
2928
2929                 /*
2930                   security : checks if one the accounts correspond to
2931                   the author of the post
2932                 */
2933
2934                 found = FALSE;
2935                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2936                         PrefsAccount * account;
2937                         gchar * from_name;
2938                         
2939                         account = cur->data;
2940                         if (account->name && *account->name)
2941                                 from_name =
2942                                         g_strdup_printf("%s <%s>",
2943                                                         account->name,
2944                                                         account->address);
2945                         else
2946                                 from_name =
2947                                         g_strdup_printf("%s",
2948                                                         account->address);
2949                         
2950                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2951                                 g_free(from_name);
2952                                 found = TRUE;
2953                                 break;
2954                         }
2955                         g_free(from_name);
2956                 }
2957
2958                 if (!found) {
2959                         alertpanel_error(_("You're not the author of the article\n"));
2960                 }
2961                 
2962                 return found;
2963
2964         default:
2965                 return TRUE;
2966         }
2967 }
2968
2969 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2970 {
2971         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2972         MsgInfo *msginfo;
2973
2974         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2975
2976         if (!check_permission(summaryview, msginfo))
2977                 return;
2978
2979         if (MSG_IS_LOCKED(msginfo->flags)) return;
2980
2981         if (MSG_IS_DELETED(msginfo->flags)) return;
2982
2983         if (MSG_IS_MOVE(msginfo->flags))
2984                 summaryview->moved--;
2985         if (MSG_IS_COPY(msginfo->flags))
2986                 summaryview->copied--;
2987         procmsg_update_unread_children (msginfo, FALSE);
2988
2989         procmsg_msginfo_set_to_folder(msginfo, NULL);
2990         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2991         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2992         summaryview->deleted++;
2993
2994         if (!prefs_common.immediate_exec && 
2995             summaryview->folder_item->stype != F_TRASH)
2996                 summary_set_row_marks(summaryview, row);
2997
2998         debug_print("Message %s/%d is set to delete\n",
2999                     msginfo->folder->path, msginfo->msgnum);
3000 }
3001
3002 void summary_cancel(SummaryView *summaryview)
3003 {
3004         MsgInfo * msginfo;
3005         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3006
3007         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3008                                               summaryview->selected);
3009         if (!msginfo) return;
3010
3011         if (!check_permission(summaryview, msginfo))
3012                 return;
3013
3014         news_cancel_article(summaryview->folder_item->folder, msginfo);
3015         
3016         if (summary_is_locked(summaryview)) return;
3017
3018         summary_lock(summaryview);
3019
3020         gtk_clist_freeze(clist);
3021
3022         summary_update_status(summaryview);
3023         summary_status_show(summaryview);
3024
3025         gtk_clist_thaw(clist);
3026
3027         summary_unlock(summaryview);
3028 }
3029
3030 void summary_delete(SummaryView *summaryview)
3031 {
3032         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3033         FolderItem *item = summaryview->folder_item;
3034         GList *cur;
3035         GtkCTreeNode *sel_last = NULL;
3036         GtkCTreeNode *node;
3037
3038         if (!item) return;
3039 #if 0
3040         if (!item || item->folder->type == F_NEWS) return;
3041 #endif
3042
3043         if (summary_is_locked(summaryview)) return;
3044
3045         /* if current folder is trash, ask for confirmation */
3046         if (item->stype == F_TRASH) {
3047                 AlertValue aval;
3048
3049                 aval = alertpanel(_("Delete message(s)"),
3050                                   _("Do you really want to delete message(s) from the trash?"),
3051                                   _("Yes"), _("No"), NULL);
3052                 if (aval != G_ALERTDEFAULT) return;
3053         }
3054
3055         /* next code sets current row focus right. We need to find a row
3056          * that is not deleted. */
3057         folder_item_update_freeze();     
3058         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3059                 sel_last = GTK_CTREE_NODE(cur->data);
3060                 summary_delete_row(summaryview, sel_last);
3061         }
3062         folder_item_update_thaw();
3063
3064         node = summary_find_next_msg(summaryview, sel_last);
3065         if (!node)
3066                 node = summary_find_prev_msg(summaryview, sel_last);
3067
3068         if (node) {
3069                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
3070                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3071                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
3072                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
3073                 else
3074                         summary_select_node
3075                                 (summaryview, node,
3076                                  messageview_is_visible(summaryview->messageview),
3077                                  FALSE);
3078         }
3079
3080         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3081                 summary_execute(summaryview);
3082                 /* after deleting, the anchor may be at an invalid row
3083                  * so reset it to the node we found earlier */
3084                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3085         } else
3086                 summary_status_show(summaryview);
3087 }
3088
3089 void summary_delete_duplicated(SummaryView *summaryview)
3090 {
3091         if (!summaryview->folder_item ||
3092             summaryview->folder_item->folder->type == F_NEWS) return;
3093         if (summaryview->folder_item->stype == F_TRASH) return;
3094
3095         main_window_cursor_wait(summaryview->mainwin);
3096         debug_print("Deleting duplicated messages...");
3097         STATUSBAR_PUSH(summaryview->mainwin,
3098                        _("Deleting duplicated messages..."));
3099
3100         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3101                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
3102                                 summaryview);
3103
3104         if (prefs_common.immediate_exec)
3105                 summary_execute(summaryview);
3106         else
3107                 summary_status_show(summaryview);
3108
3109         debug_print("done.\n");
3110         STATUSBAR_POP(summaryview->mainwin);
3111         main_window_cursor_normal(summaryview->mainwin);
3112 }
3113
3114 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
3115                                            SummaryView *summaryview)
3116 {
3117         GtkCTreeNode *found;
3118         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
3119         MsgInfo *dup_msginfo;
3120         
3121         if (!msginfo->msgid || !*msginfo->msgid) return;
3122
3123         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
3124         
3125         if (found && found != node) {
3126                 dup_msginfo = gtk_ctree_node_get_row_data(ctree, found);
3127                 /* prefer to delete the unread one */
3128                 if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(dup_msginfo->flags))
3129                 ||  (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(dup_msginfo->flags)))
3130                         summary_delete_row(summaryview, node);
3131         }
3132 }
3133
3134 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3135 {
3136         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3137         MsgInfo *msginfo;
3138
3139         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3140         if (MSG_IS_DELETED(msginfo->flags))
3141                 summaryview->deleted--;
3142         if (MSG_IS_MOVE(msginfo->flags))
3143                 summaryview->moved--;
3144         if (MSG_IS_COPY(msginfo->flags))
3145                 summaryview->copied--;
3146         procmsg_update_unread_children (msginfo, FALSE);
3147
3148         procmsg_msginfo_set_to_folder(msginfo, NULL);
3149         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3150         summary_set_row_marks(summaryview, row);
3151
3152         debug_print("Message %s/%d is unmarked\n",
3153                     msginfo->folder->path, msginfo->msgnum);
3154 }
3155
3156 void summary_unmark(SummaryView *summaryview)
3157 {
3158         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3159         GList *cur;
3160
3161         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3162                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3163
3164         summary_status_show(summaryview);
3165 }
3166
3167 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3168                                 FolderItem *to_folder)
3169 {
3170         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3171         MsgInfo *msginfo;
3172
3173         g_return_if_fail(to_folder != NULL);
3174
3175         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3176         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3177         if (MSG_IS_DELETED(msginfo->flags))
3178                 summaryview->deleted--;
3179         if (MSG_IS_COPY(msginfo->flags)) {
3180                 summaryview->copied--;
3181         }
3182         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3183         if (!MSG_IS_MOVE(msginfo->flags)) {
3184                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3185                 summaryview->moved++;
3186         }
3187         if (!prefs_common.immediate_exec) {
3188                 summary_set_row_marks(summaryview, row);
3189         }
3190
3191         debug_print("Message %d is set to move to %s\n",
3192                     msginfo->msgnum, to_folder->path);
3193 }
3194
3195 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3196 {
3197         GList *cur;
3198
3199         if (!to_folder) return;
3200         if (!summaryview->folder_item ||
3201             summaryview->folder_item->folder->type == F_NEWS) return;
3202
3203         if (summary_is_locked(summaryview)) return;
3204
3205         if (summaryview->folder_item == to_folder) {
3206                 alertpanel_notice(_("Destination is same as current folder."));
3207                 return;
3208         }
3209
3210         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3211              cur != NULL; cur = cur->next)
3212                 summary_move_row_to
3213                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3214
3215         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3216
3217         if (prefs_common.immediate_exec)
3218                 summary_execute(summaryview);
3219         else {
3220                 summary_status_show(summaryview);
3221         }
3222         
3223         if (!summaryview->selected) { /* this was the last message */
3224                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3225                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3226                 if (node)
3227                         summary_select_node(summaryview, node, TRUE, TRUE);
3228         }
3229
3230 }
3231
3232 void summary_move_to(SummaryView *summaryview)
3233 {
3234         FolderItem *to_folder;
3235
3236         if (!summaryview->folder_item ||
3237             summaryview->folder_item->folder->type == F_NEWS) return;
3238
3239         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3240                                          FOLDER_SEL_MOVE, NULL);
3241         summary_move_selected_to(summaryview, to_folder);
3242 }
3243
3244 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3245                                 FolderItem *to_folder)
3246 {
3247         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3248         MsgInfo *msginfo;
3249
3250         g_return_if_fail(to_folder != NULL);
3251
3252         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3253         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3254         if (MSG_IS_DELETED(msginfo->flags))
3255                 summaryview->deleted--;
3256         if (MSG_IS_MOVE(msginfo->flags)) {
3257                 summaryview->moved--;
3258         }
3259         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3260         if (!MSG_IS_COPY(msginfo->flags)) {
3261                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3262                 summaryview->copied++;
3263         }
3264         if (!prefs_common.immediate_exec) {
3265                 summary_set_row_marks(summaryview, row);
3266         }
3267
3268         debug_print("Message %d is set to copy to %s\n",
3269                     msginfo->msgnum, to_folder->path);
3270 }
3271
3272 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3273 {
3274         GList *cur;
3275
3276         if (!to_folder) return;
3277         if (!summ