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