4520c77273eae2d66aeac77ab057eb14ee806ced
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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(NORMAL_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         gboolean hidden_removed = FALSE;
813
814         if (summary_is_locked(summaryview)) return FALSE;
815
816         inc_lock();
817         summary_lock(summaryview);
818
819         if (!prefs_common.summary_quicksearch_sticky
820          && !prefs_common.summary_quicksearch_recurse
821          && !quicksearch_is_running(summaryview->quicksearch)) {
822                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
823         }
824
825         /* STATUSBAR_POP(summaryview->mainwin); */
826
827         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
828
829         if (is_refresh) {
830                 selected_msgnum = summary_get_msgnum(summaryview,
831                                                      summaryview->selected);
832                 displayed_msgnum = summary_get_msgnum(summaryview,
833                                                       summaryview->displayed);
834         }
835
836         /* process the marks if any */
837         if (summaryview->mainwin->lock_count == 0 &&
838             (summaryview->moved > 0 || summaryview->copied > 0)) {
839                 AlertValue val;
840                 gboolean changed = FALSE;
841
842                 val = alertpanel(_("Process mark"),
843                                  _("Some marks are left. Process it?"),
844                                  GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL);
845                 if (G_ALERTDEFAULT == val) {
846                         summary_unlock(summaryview);
847                         summary_execute(summaryview);
848                         summary_lock(summaryview);
849                         changed = TRUE;
850                 } else if (G_ALERTALTERNATE == val) {
851                         /* DO NOTHING */
852                 } else {
853                         summary_unlock(summaryview);
854                         inc_unlock();
855                         return FALSE;
856                 }
857                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
858                         folder_update_op_count();
859         }
860         
861         gtk_clist_freeze(GTK_CLIST(ctree));
862
863         summary_clear_list(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 (!msginfo->hidden) {
913                                 if (MSG_IS_UNREAD(msginfo->flags) &&
914                                     !MSG_IS_IGNORE_THREAD(msginfo->flags))
915                                         not_killed = g_slist_prepend(not_killed, msginfo);
916                                 else if (MSG_IS_MARKED(msginfo->flags) ||
917                                          MSG_IS_LOCKED(msginfo->flags))
918                                         not_killed = g_slist_prepend(not_killed, msginfo);
919                                 else if (is_refresh &&
920                                         (msginfo->msgnum == selected_msgnum ||
921                                          msginfo->msgnum == displayed_msgnum))
922                                         not_killed = g_slist_prepend(not_killed, msginfo);
923                                 else
924                                         procmsg_msginfo_free(msginfo);
925                          } else
926                                 procmsg_msginfo_free(msginfo);
927                 }
928                 hidden_removed = TRUE;
929                 g_slist_free(mlist);
930                 mlist = not_killed;
931         } else {
932                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
933         }
934
935         if (quicksearch_is_active(summaryview->quicksearch)) {
936                 GSList *not_killed;
937                 
938                 not_killed = NULL;
939                 for (cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
940                         MsgInfo * msginfo = (MsgInfo *) cur->data;
941
942                         if (!msginfo->hidden && quicksearch_match(summaryview->quicksearch, msginfo))
943                                 not_killed = g_slist_prepend(not_killed, msginfo);
944                         else
945                                 procmsg_msginfo_free(msginfo);
946                 }
947                 hidden_removed = TRUE;
948                 if (quicksearch_is_running(summaryview->quicksearch)) {
949                         /* only scan subfolders when quicksearch changed,
950                          * not when search is the same and folder changed */
951                         main_window_cursor_wait(summaryview->mainwin);
952                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
953                         quicksearch_search_subfolders(summaryview->quicksearch, 
954                                               summaryview->folderview,
955                                               summaryview->folder_item);
956                         main_window_cursor_normal(summaryview->mainwin);
957                 }
958                 
959                 g_slist_free(mlist);
960                 mlist = not_killed;
961         }
962
963         if (!hidden_removed) {
964                 not_killed = NULL;
965                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
966                         MsgInfo * msginfo = (MsgInfo *) cur->data;
967
968                         if (!msginfo->hidden)
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
977         STATUSBAR_POP(summaryview->mainwin);
978
979         /* set ctree and hash table from the msginfo list, and
980            create the thread */
981         summary_set_ctree_from_list(summaryview, mlist);
982
983         g_slist_free(mlist);
984
985         gtk_clist_thaw(GTK_CLIST(ctree));
986
987         if (is_refresh) {
988                 summaryview->displayed =
989                         summary_find_msg_by_msgnum(summaryview,
990                                                    displayed_msgnum);
991                 if (!summaryview->displayed)
992                         messageview_clear(summaryview->messageview);
993                 summary_unlock(summaryview);
994                 summary_select_by_msgnum(summaryview, selected_msgnum);
995                 summary_lock(summaryview);
996                 if (!summaryview->selected) {
997                         /* no selected message - select first unread
998                            message, but do not display it */
999                         node = summary_find_next_flagged_msg(summaryview, NULL,
1000                                                              MSG_UNREAD, FALSE);
1001                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
1002                                 node = gtk_ctree_node_nth
1003                                         (ctree,
1004                                          item->sort_type == SORT_DESCENDING
1005                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
1006                         summary_unlock(summaryview);
1007                         summary_select_node(summaryview, node, FALSE, TRUE);
1008                         summary_lock(summaryview);
1009                 }
1010         } else {
1011                 switch (prefs_common.select_on_entry) {
1012                         case SELECTONENTRY_NEW:
1013                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1014                                                                      MSG_NEW, FALSE);
1015                                 if (node == NULL)
1016                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1017                                                                      MSG_UNREAD, FALSE);
1018                                 break;
1019                         case SELECTONENTRY_UNREAD:
1020                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1021                                                                      MSG_UNREAD, FALSE);
1022                                 if (node == NULL)
1023                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1024                                                                      MSG_NEW, FALSE);
1025                                 break;
1026                         default:
1027                                 node = NULL;
1028                 }
1029
1030                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1031                         node = gtk_ctree_node_nth
1032                                 (ctree,
1033                                  item->sort_type == SORT_DESCENDING
1034                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1035                 }
1036                 summary_unlock(summaryview);
1037                 summary_select_node(summaryview, node,
1038                                     prefs_common.always_show_msg,
1039                                     TRUE);
1040                 summary_lock(summaryview);
1041         }
1042
1043         summary_status_show(summaryview);
1044         summary_set_menu_sensitive(summaryview);
1045         toolbar_main_set_sensitive(summaryview->mainwin);
1046
1047         debug_print("\n");
1048         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1049         STATUSBAR_POP(summaryview->mainwin);
1050         main_window_cursor_normal(summaryview->mainwin);
1051         summary_unlock(summaryview);
1052         inc_unlock();
1053
1054         return TRUE;
1055 }
1056
1057 #undef CURRENTLY_DISPLAYED
1058
1059
1060 void summary_clear_list(SummaryView *summaryview)
1061 {
1062         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1063         gint optimal_width;
1064
1065         gtk_clist_freeze(clist);
1066
1067         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1068                                 NULL, summary_free_msginfo_func, NULL);
1069
1070         if (summaryview->folder_item) {
1071                 summaryview->folder_item->opened = FALSE;
1072                 summaryview->folder_item = NULL;
1073         }
1074
1075         summaryview->display_msg = FALSE;
1076
1077         summaryview->selected = NULL;
1078         summaryview->displayed = NULL;
1079         summaryview->total_size = 0;
1080         summaryview->deleted = summaryview->moved = 0;
1081         summaryview->copied = 0;
1082         if (summaryview->msgid_table) {
1083                 g_hash_table_destroy(summaryview->msgid_table);
1084                 summaryview->msgid_table = NULL;
1085         }
1086         if (summaryview->subject_table) {
1087                 g_hash_table_destroy(summaryview->subject_table);
1088                 summaryview->subject_table = NULL;
1089         }
1090         summaryview->mlist = NULL;
1091
1092         gtk_clist_clear(clist);
1093         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1094                 optimal_width = gtk_clist_optimal_column_width
1095                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1096                 gtk_clist_set_column_width
1097                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1098                          optimal_width);
1099         }
1100
1101         gtk_clist_thaw(clist);
1102 }
1103
1104 void summary_clear_all(SummaryView *summaryview)
1105 {
1106         messageview_clear(summaryview->messageview);
1107         summary_clear_list(summaryview);
1108         summary_set_menu_sensitive(summaryview);
1109         toolbar_main_set_sensitive(summaryview->mainwin);
1110         summary_status_show(summaryview);
1111 }
1112
1113 void summary_lock(SummaryView *summaryview)
1114 {
1115         summaryview->lock_count++;
1116 }
1117
1118 void summary_unlock(SummaryView *summaryview)
1119 {
1120         if (summaryview->lock_count)
1121                 summaryview->lock_count--;
1122 }
1123
1124 gboolean summary_is_locked(SummaryView *summaryview)
1125 {
1126         return summaryview->lock_count > 0;
1127 }
1128
1129 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1130 {
1131         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1132         SummarySelection selection;
1133
1134         if (!clist->row_list)
1135                 selection = SUMMARY_NONE;
1136         else if (!clist->selection)
1137                 selection = SUMMARY_SELECTED_NONE;
1138         else if (!clist->selection->next)
1139                 selection = SUMMARY_SELECTED_SINGLE;
1140         else
1141                 selection = SUMMARY_SELECTED_MULTIPLE;
1142
1143         return selection;
1144 }
1145
1146 /*!
1147  *\return       MsgInfo * Selected message if there's one selected;
1148  *              if multiple selected, or none, return NULL.
1149  */ 
1150 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1151 {
1152         /* summaryview->selected may be valid when multiple 
1153          * messages were selected */
1154         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1155
1156         if (sellist == NULL || sellist->next) 
1157                 return NULL;
1158         
1159         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1160 }
1161
1162 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1163 {
1164         GSList *mlist = NULL;
1165         GList *cur;
1166         MsgInfo *msginfo;
1167
1168         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1169              cur = cur->next) {
1170                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1171                 mlist = g_slist_prepend(mlist, msginfo);
1172         }
1173
1174         mlist = g_slist_reverse(mlist);
1175
1176         return mlist;
1177 }
1178
1179 GSList *summary_get_msg_list(SummaryView *summaryview)
1180 {
1181         GSList *mlist = NULL;
1182         GtkCTree *ctree;
1183         GtkCTreeNode *node;
1184         MsgInfo *msginfo;
1185
1186         ctree = GTK_CTREE(summaryview->ctree);
1187
1188         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1189              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1190                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1191                 mlist = g_slist_prepend(mlist, msginfo);
1192         }
1193
1194         mlist = g_slist_reverse(mlist);
1195
1196         return mlist;
1197 }
1198
1199 static void summary_set_menu_sensitive(SummaryView *summaryview)
1200 {
1201         GtkItemFactory *ifactory = summaryview->popupfactory;
1202         SensitiveCond state;
1203         gboolean sensitive;
1204         GtkWidget *menuitem;
1205         gint i;
1206
1207         static const struct {
1208                 gchar *const entry;
1209                 SensitiveCond cond;
1210         } entry[] = {
1211                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1212                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1213                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1214                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1215                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1216                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1217
1218                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1219                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1220
1221                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1222
1223                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1224                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1225                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1226                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1227                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1228
1229                 {"/Mark"                        , M_TARGET_EXIST},
1230                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1231                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1232                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1233                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1234                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1235                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1236                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1237                 {"/Color label"                 , M_TARGET_EXIST},
1238
1239                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1240                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1241                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1242
1243                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1244                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1245                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1246                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1247 #if 0
1248                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1249                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1250
1251                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1252                 {"/Select all"                  , M_TARGET_EXIST},
1253 #endif
1254                 {NULL, 0}
1255         };
1256
1257         main_window_set_menu_sensitive(summaryview->mainwin);
1258
1259         state = main_window_get_current_state(summaryview->mainwin);
1260
1261         for (i = 0; entry[i].entry != NULL; i++) {
1262                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1263                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1264         }
1265
1266
1267         summary_lock(summaryview);
1268         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1269         gtk_check_menu_item_set_active
1270                 (GTK_CHECK_MENU_ITEM(menuitem),
1271                  summaryview->messageview->mimeview->textview->show_all_headers);
1272         summary_unlock(summaryview);
1273 }
1274
1275 void summary_select_prev_unread(SummaryView *summaryview)
1276 {
1277         GtkCTreeNode *node;
1278         gboolean skip_cur = FALSE;
1279
1280         if (summaryview->displayed 
1281         &&  summaryview->selected == summaryview->displayed) {
1282                 debug_print("skipping current\n");
1283                 skip_cur = TRUE;
1284         }
1285
1286         node = summary_find_prev_flagged_msg
1287                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1288
1289         if (!node || node == summaryview->selected) {
1290                 AlertValue val = 0;
1291
1292                 switch (prefs_common.next_unread_msg_dialog) {
1293                         case NEXTUNREADMSGDIALOG_ALWAYS:
1294                                 val = alertpanel(_("No more unread messages"),
1295                                                  _("No unread message found. "
1296                                                    "Search from the end?"),
1297                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1298                                 break;
1299                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1300                                 val = G_ALERTDEFAULT;
1301                                 break;
1302                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1303                                 val = !G_ALERTDEFAULT;
1304                                 break;
1305                         default:
1306                                 debug_print(
1307                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1308                 }
1309                 if (val != G_ALERTDEFAULT) return;
1310                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1311                                                      MSG_UNREAD, FALSE);
1312         }
1313
1314         if (!node)
1315                 alertpanel_notice(_("No unread messages."));
1316         else
1317                 summary_select_node(summaryview, node, TRUE, FALSE);
1318 }
1319
1320 void summary_select_next_unread(SummaryView *summaryview)
1321 {
1322         GtkCTreeNode *node = summaryview->selected;
1323         gboolean skip_cur = FALSE;
1324         
1325         if (summaryview->displayed 
1326         &&  summaryview->selected == summaryview->displayed) {
1327                 debug_print("skipping cur (%p %p)\n",
1328                         summaryview->displayed, summaryview->selected);
1329                 skip_cur = TRUE;
1330         }
1331
1332
1333         node = summary_find_next_flagged_msg
1334                 (summaryview, node, MSG_UNREAD, skip_cur);
1335         
1336         if (node)
1337                 summary_select_node(summaryview, node, TRUE, FALSE);
1338         else {
1339                 node = summary_find_next_flagged_msg
1340                         (summaryview, NULL, MSG_UNREAD, FALSE);
1341                 if (node == NULL || node == summaryview->selected) {
1342                         AlertValue val = 0;
1343
1344                         switch (prefs_common.next_unread_msg_dialog) {
1345                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1346                                         val = alertpanel(_("No more unread messages"),
1347                                                          _("No unread message found. "
1348                                                            "Go to next folder?"),
1349                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1350                                         break;
1351                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1352                                         val = G_ALERTDEFAULT;
1353                                         break;
1354                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1355                                         val = G_ALERTOTHER;
1356                                         break;
1357                                 default:
1358                                         debug_print(
1359                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1360                         }
1361
1362                         if (val == G_ALERTDEFAULT) {
1363                                 folderview_select_next_unread(summaryview->folderview);
1364                                 return;
1365                         } 
1366                         else
1367                                 return;
1368                 } else
1369                         summary_select_node(summaryview, node, TRUE, FALSE);
1370
1371         }
1372 }
1373
1374 void summary_select_prev_new(SummaryView *summaryview)
1375 {
1376         GtkCTreeNode *node;
1377         gboolean skip_cur = FALSE;
1378
1379         if (summaryview->displayed 
1380         &&  summaryview->selected == summaryview->displayed) {
1381                 debug_print("skipping current\n");
1382                 skip_cur = TRUE;
1383         }
1384
1385         node = summary_find_prev_flagged_msg
1386                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1387
1388         if (!node || node == summaryview->selected) {
1389                 AlertValue val = 0;
1390
1391                 switch (prefs_common.next_unread_msg_dialog) {
1392                         case NEXTUNREADMSGDIALOG_ALWAYS:
1393                                 val = alertpanel(_("No more new messages"),
1394                                                  _("No new message found. "
1395                                                    "Search from the end?"),
1396                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1397                                 break;
1398                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1399                                 val = G_ALERTDEFAULT;
1400                                 break;
1401                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1402                                 val = !G_ALERTDEFAULT;
1403                                 break;
1404                         default:
1405                                 debug_print(
1406                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1407                 }
1408                 if (val != G_ALERTDEFAULT) return;
1409                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1410                                                      MSG_NEW, FALSE);
1411         }
1412
1413         if (!node)
1414                 alertpanel_notice(_("No new messages."));
1415         else
1416                 summary_select_node(summaryview, node, TRUE, FALSE);
1417 }
1418
1419 void summary_select_next_new(SummaryView *summaryview)
1420 {
1421         GtkCTreeNode *node = summaryview->selected;
1422         gboolean skip_cur = FALSE;
1423         
1424         if (summaryview->displayed 
1425         &&  summaryview->selected == summaryview->displayed) {
1426                 debug_print("skipping cur (%p %p)\n",
1427                         summaryview->displayed, summaryview->selected);
1428                 skip_cur = TRUE;
1429         }
1430
1431
1432         node = summary_find_next_flagged_msg
1433                 (summaryview, node, MSG_NEW, skip_cur);
1434         
1435         if (node)
1436                 summary_select_node(summaryview, node, TRUE, FALSE);
1437         else {
1438                 node = summary_find_next_flagged_msg
1439                         (summaryview, NULL, MSG_NEW, FALSE);
1440                 if (node == NULL || node == summaryview->selected) {
1441                         AlertValue val = 0;
1442
1443                         switch (prefs_common.next_unread_msg_dialog) {
1444                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1445                                         val = alertpanel(_("No more new messages"),
1446                                                          _("No new message found. "
1447                                                            "Go to next folder?"),
1448                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1449                                         break;
1450                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1451                                         val = G_ALERTDEFAULT;
1452                                         break;
1453                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1454                                         val = G_ALERTOTHER;
1455                                         break;
1456                                 default:
1457                                         debug_print(
1458                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1459                         }
1460
1461                         if (val == G_ALERTDEFAULT) {
1462                                 folderview_select_next_new(summaryview->folderview);
1463                                 return;
1464                         } 
1465                         else
1466                                 return;
1467                 } else
1468                         summary_select_node(summaryview, node, TRUE, FALSE);
1469
1470         }
1471 }
1472
1473 void summary_select_prev_marked(SummaryView *summaryview)
1474 {
1475         GtkCTreeNode *node;
1476
1477         node = summary_find_prev_flagged_msg
1478                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1479
1480         if (!node) {
1481                 AlertValue val;
1482
1483                 val = alertpanel(_("No more marked messages"),
1484                                  _("No marked message found. "
1485                                    "Search from the end?"),
1486                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1487                 if (val != G_ALERTDEFAULT) return;
1488                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1489                                                      MSG_MARKED, TRUE);
1490         }
1491
1492         if (!node)
1493                 alertpanel_notice(_("No marked messages."));
1494         else
1495                 summary_select_node(summaryview, node, TRUE, FALSE);
1496 }
1497
1498 void summary_select_next_marked(SummaryView *summaryview)
1499 {
1500         GtkCTreeNode *node;
1501
1502         node = summary_find_next_flagged_msg
1503                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1504
1505         if (!node) {
1506                 AlertValue val;
1507
1508                 val = alertpanel(_("No more marked messages"),
1509                                  _("No marked message found. "
1510                                    "Search from the beginning?"),
1511                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1512                 if (val != G_ALERTDEFAULT) return;
1513                 node = summary_find_next_flagged_msg(summaryview, NULL,
1514                                                      MSG_MARKED, TRUE);
1515         }
1516
1517         if (!node)
1518                 alertpanel_notice(_("No marked messages."));
1519         else
1520                 summary_select_node(summaryview, node, TRUE, FALSE);
1521 }
1522
1523 void summary_select_prev_labeled(SummaryView *summaryview)
1524 {
1525         GtkCTreeNode *node;
1526
1527         node = summary_find_prev_flagged_msg
1528                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1529
1530         if (!node) {
1531                 AlertValue val;
1532
1533                 val = alertpanel(_("No more labeled messages"),
1534                                  _("No labeled message found. "
1535                                    "Search from the end?"),
1536                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1537                 if (val != G_ALERTDEFAULT) return;
1538                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1539                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1540         }
1541
1542         if (!node)
1543                 alertpanel_notice(_("No labeled messages."));
1544         else
1545                 summary_select_node(summaryview, node, TRUE, FALSE);
1546 }
1547
1548 void summary_select_next_labeled(SummaryView *summaryview)
1549 {
1550         GtkCTreeNode *node;
1551
1552         node = summary_find_next_flagged_msg
1553                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1554
1555         if (!node) {
1556                 AlertValue val;
1557
1558                 val = alertpanel(_("No more labeled messages"),
1559                                  _("No labeled message found. "
1560                                    "Search from the beginning?"),
1561                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1562                 if (val != G_ALERTDEFAULT) return;
1563                 node = summary_find_next_flagged_msg(summaryview, NULL,
1564                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1565         }
1566
1567         if (!node)
1568                 alertpanel_notice(_("No labeled messages."));
1569         else
1570                 summary_select_node(summaryview, node, TRUE, FALSE);
1571 }
1572
1573 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1574 {
1575         GtkCTreeNode *node;
1576
1577         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1578         summary_select_node(summaryview, node, FALSE, TRUE);
1579 }
1580
1581 /**
1582  * summary_select_node:
1583  * @summaryview: Summary view.
1584  * @node: Summary tree node.
1585  * @display_msg: TRUE to display the selected message.
1586  * @do_refresh: TRUE to refresh the widget.
1587  *
1588  * Select @node (bringing it into view by scrolling and expanding its
1589  * thread, if necessary) and unselect all others.  If @display_msg is
1590  * TRUE, display the corresponding message in the message view.
1591  * If @do_refresh is TRUE, the widget is refreshed.
1592  **/
1593 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1594                          gboolean display_msg, gboolean do_refresh)
1595 {
1596         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1597
1598         if (node) {
1599                 gtkut_ctree_expand_parent_all(ctree, node);
1600                 if (do_refresh) {
1601                         GTK_EVENTS_FLUSH();
1602                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1603                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1604                 }
1605                 summary_unselect_all(summaryview);
1606                 if (display_msg && summaryview->displayed == node)
1607                         summaryview->displayed = NULL;
1608                 summaryview->display_msg = display_msg;
1609                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1610                 if (summaryview->selected == NULL)
1611                         summaryview->selected = node;
1612         }
1613 }
1614
1615 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1616 {
1617         GtkCTree *ctree =NULL;
1618         MsgInfo *msginfo;
1619
1620         if (!summaryview)
1621                 return 0;
1622         ctree = GTK_CTREE(summaryview->ctree);
1623         if (!node)
1624                 return 0;
1625         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1626         return msginfo->msgnum;
1627 }
1628
1629 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1630                                            GtkCTreeNode *current_node)
1631 {
1632         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1633         GtkCTreeNode *node;
1634         MsgInfo *msginfo;
1635
1636         if (current_node)
1637                 node = current_node;
1638         else
1639                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1640
1641         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1642                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1643                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1644         }
1645
1646         return node;
1647 }
1648
1649 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1650                                            GtkCTreeNode *current_node)
1651 {
1652         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1653         GtkCTreeNode *node;
1654         MsgInfo *msginfo;
1655
1656         if (current_node)
1657                 node = current_node;
1658         else
1659                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1660
1661         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1662                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1663                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1664         }
1665
1666         return node;
1667 }
1668
1669 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1670                                                    GtkCTreeNode *current_node,
1671                                                    MsgPermFlags flags,
1672                                                    gboolean start_from_prev)
1673 {
1674         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1675         GtkCTreeNode *node;
1676         MsgInfo *msginfo;
1677
1678         if (current_node) {
1679                 if (start_from_prev)
1680                         node = GTK_CTREE_NODE_PREV(current_node);
1681                 else
1682                         node = current_node;
1683         } else
1684                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1685
1686         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1687                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1688                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1689         }
1690
1691         return node;
1692 }
1693
1694 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1695                                                    GtkCTreeNode *current_node,
1696                                                    MsgPermFlags flags,
1697                                                    gboolean start_from_next)
1698 {
1699         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1700         GtkCTreeNode *node;
1701         MsgInfo *msginfo;
1702
1703         if (current_node) {
1704                 if (start_from_next)
1705                         node = gtkut_ctree_node_next(ctree, current_node);
1706                 else
1707                         node = current_node;
1708         } else
1709                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1710
1711         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1712                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1713                 /* Find msg with matching flags but ignore messages with
1714                    ignore flags, if searching for new or unread messages */
1715                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1716                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1717                         break;
1718         }
1719
1720         return node;
1721 }
1722
1723 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1724                                                 guint msgnum)
1725 {
1726         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1727         GtkCTreeNode *node;
1728         MsgInfo *msginfo;
1729
1730         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1731
1732         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1733                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1734                 if (msginfo && msginfo->msgnum == msgnum) break;
1735         }
1736
1737         return node;
1738 }
1739
1740 static guint attract_hash_func(gconstpointer key)
1741 {
1742         gchar *str;
1743         gchar *p;
1744         guint h;
1745
1746         Xstrdup_a(str, (const gchar *)key, return 0);
1747         trim_subject(str);
1748
1749         p = str;
1750         h = *p;
1751
1752         if (h) {
1753                 for (p += 1; *p != '\0'; p++)
1754                         h = (h << 5) - h + *p;
1755         }
1756
1757         return h;
1758 }
1759
1760 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1761 {
1762         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1763 }
1764
1765 void summary_attract_by_subject(SummaryView *summaryview)
1766 {
1767         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1768         GtkCList *clist = GTK_CLIST(ctree);
1769         GtkCTreeNode *src_node;
1770         GtkCTreeNode *dst_node, *sibling;
1771         GtkCTreeNode *tmp;
1772         MsgInfo *src_msginfo, *dst_msginfo;
1773         GHashTable *subject_table;
1774
1775         debug_print("Attracting messages by subject...");
1776         STATUSBAR_PUSH(summaryview->mainwin,
1777                        _("Attracting messages by subject..."));
1778
1779         main_window_cursor_wait(summaryview->mainwin);
1780         gtk_clist_freeze(clist);
1781
1782         subject_table = g_hash_table_new(attract_hash_func,
1783                                          attract_compare_func);
1784
1785         for (src_node = GTK_CTREE_NODE(clist->row_list);
1786              src_node != NULL;
1787              src_node = tmp) {
1788                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1789                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1790                 if (!src_msginfo) continue;
1791                 if (!src_msginfo->subject) continue;
1792
1793                 /* find attracting node */
1794                 dst_node = g_hash_table_lookup(subject_table,
1795                                                src_msginfo->subject);
1796
1797                 if (dst_node) {
1798                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1799
1800                         /* if the time difference is more than 20 days,
1801                            don't attract */
1802                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1803                             > 60 * 60 * 24 * 20)
1804                                 continue;
1805
1806                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1807                         if (src_node != sibling)
1808                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1809                 }
1810
1811                 g_hash_table_insert(subject_table,
1812                                     src_msginfo->subject, src_node);
1813         }
1814
1815         g_hash_table_destroy(subject_table);
1816
1817         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1818
1819         gtk_clist_thaw(clist);
1820
1821         debug_print("done.\n");
1822         STATUSBAR_POP(summaryview->mainwin);
1823
1824         main_window_cursor_normal(summaryview->mainwin);
1825 }
1826
1827 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1828                                       gpointer data)
1829 {
1830         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1831
1832         if (msginfo)
1833                 procmsg_msginfo_free(msginfo);
1834 }
1835
1836 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1837                                    gpointer data)
1838 {
1839         SummaryView *summaryview = data;
1840         MsgInfo *msginfo;
1841
1842         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1843
1844         if (msginfo->folder && msginfo->folder->folder &&
1845             msginfo->folder->folder->klass->type == F_NEWS)
1846                 news_flag_crosspost(msginfo);
1847
1848         if (MSG_IS_DELETED(msginfo->flags))
1849                 summaryview->deleted++;
1850
1851         summaryview->total_size += msginfo->size;
1852
1853         summary_set_row_marks(summaryview, node);
1854 }
1855
1856 static void summary_update_status(SummaryView *summaryview)
1857 {
1858         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1859         GtkCTreeNode *node;
1860         MsgInfo *msginfo;
1861
1862         summaryview->total_size =
1863         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1864
1865         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1866              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1867                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1868
1869                 if (MSG_IS_DELETED(msginfo->flags))
1870                         summaryview->deleted++;
1871                 if (MSG_IS_MOVE(msginfo->flags))
1872                         summaryview->moved++;
1873                 if (MSG_IS_COPY(msginfo->flags))
1874                         summaryview->copied++;
1875                 summaryview->total_size += msginfo->size;
1876         }
1877 }
1878
1879 static void summary_status_show(SummaryView *summaryview)
1880 {
1881         gchar *str;
1882         gchar *del, *mv, *cp;
1883         gchar *sel;
1884         gchar *spc;
1885         gchar *itstr;
1886         GList *rowlist, *cur;
1887         guint n_selected = 0;
1888         off_t sel_size = 0;
1889         MsgInfo *msginfo;
1890         gchar *name;
1891         
1892         if (!summaryview->folder_item) {
1893                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1894                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1895                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1896                 return;
1897         }
1898
1899         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1900         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1901                 msginfo = gtk_ctree_node_get_row_data
1902                         (GTK_CTREE(summaryview->ctree),
1903                          GTK_CTREE_NODE(cur->data));
1904                 if (!msginfo)
1905                         g_warning("summary_status_show(): msginfo == NULL\n");
1906                 else {
1907                         sel_size += msginfo->size;
1908                         n_selected++;
1909                 }
1910         }
1911
1912         name = folder_item_get_name(summaryview->folder_item);
1913         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1914         g_free(name);
1915
1916         if (summaryview->deleted)
1917                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1918         else
1919                 del = g_strdup("");
1920         if (summaryview->moved)
1921                 mv = g_strdup_printf(_("%s%d moved"),
1922                                      summaryview->deleted ? _(", ") : "",
1923                                      summaryview->moved);
1924         else
1925                 mv = g_strdup("");
1926         if (summaryview->copied)
1927                 cp = g_strdup_printf(_("%s%d copied"),
1928                                      summaryview->deleted ||
1929                                      summaryview->moved ? _(", ") : "",
1930                                      summaryview->copied);
1931         else
1932                 cp = g_strdup("");
1933
1934         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1935                 spc = "    ";
1936         else
1937                 spc = "";
1938
1939         if (n_selected) {
1940                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1941                 if (n_selected == 1)
1942                         itstr = g_strdup(_(" item selected"));
1943                 else
1944                         itstr = g_strdup(_(" items selected"));
1945         } else {
1946                 sel = g_strdup("");
1947                 itstr = g_strdup("");
1948         }
1949                 
1950         str = g_strconcat(n_selected ? itos(n_selected) : "",
1951                                         itstr, sel, spc, del, mv, cp, NULL);
1952         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1953         g_free(str);
1954         g_free(sel);
1955         g_free(del);
1956         g_free(mv);
1957         g_free(cp);
1958         g_free(itstr);
1959
1960         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1961
1962                                       summaryview->folder_item->new_msgs,
1963                                       summaryview->folder_item->unread_msgs,
1964                                       summaryview->folder_item->total_msgs,
1965                                       to_human_readable(summaryview->total_size));
1966         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1967         g_free(str);
1968 }
1969
1970 static void summary_set_column_titles(SummaryView *summaryview)
1971 {
1972         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1973         GtkWidget *hbox;
1974         GtkWidget *label;
1975         GtkWidget *arrow;
1976         gint pos;
1977         const gchar *title;
1978         SummaryColumnType type;
1979         gboolean single_char;
1980         GtkJustification justify;
1981
1982         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1983                 SORT_BY_MARK,
1984                 SORT_BY_STATUS,
1985                 SORT_BY_MIME,
1986                 SORT_BY_SUBJECT,
1987                 SORT_BY_FROM,
1988                 SORT_BY_TO,
1989                 SORT_BY_DATE,
1990                 SORT_BY_SIZE,
1991                 SORT_BY_NUMBER,
1992                 SORT_BY_SCORE,
1993                 SORT_BY_LOCKED
1994         };
1995
1996         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1997                 type = summaryview->col_state[pos].type;
1998
1999                 /* CLAWS: mime and unread are single char headers */
2000                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
2001                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2002                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2003
2004                 switch (type) {
2005                 case S_COL_SUBJECT:
2006                 case S_COL_FROM:
2007                 case S_COL_TO:
2008                 case S_COL_DATE:
2009                 case S_COL_NUMBER:
2010                         if (prefs_common.trans_hdr)
2011                                 title = gettext(col_label[type]);
2012                         else
2013                                 title = col_label[type];
2014                         break;
2015                 /* CLAWS: dummies for mark and locked headers */        
2016                 case S_COL_MARK:        
2017                 case S_COL_LOCKED:
2018                         title = "";
2019                         break;
2020                 default:
2021                         title = gettext(col_label[type]);
2022                 }
2023
2024                 if (type == S_COL_MIME) {
2025                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2026                         gtk_widget_show(label);
2027                         gtk_clist_set_column_widget(clist, pos, label);
2028                         continue;
2029                 } else if (single_char) {
2030                         gtk_clist_set_column_title(clist, pos, title);
2031                         continue;
2032                 }
2033
2034                 /* CLAWS: changed so that locked and mark headers
2035                  * show a pixmap instead of single character */
2036                 hbox  = gtk_hbox_new(FALSE, 4);
2037                 
2038                 if (type == S_COL_LOCKED)
2039                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
2040                 else if (type == S_COL_MARK) 
2041                         label = gtk_pixmap_new(markxpm, markxpmmask);
2042                 else 
2043                         label = gtk_label_new(title);
2044                 
2045                 if (justify == GTK_JUSTIFY_RIGHT)
2046                         gtk_box_pack_end(GTK_BOX(hbox), label,
2047                                          FALSE, FALSE, 0);
2048                 else
2049                         gtk_box_pack_start(GTK_BOX(hbox), label,
2050                                            FALSE, FALSE, 0);
2051
2052                 if (summaryview->sort_key == sort_by[type]) {
2053                         arrow = gtk_arrow_new
2054                                 (summaryview->sort_type == SORT_ASCENDING
2055                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2056                                  GTK_SHADOW_IN);
2057                         if (justify == GTK_JUSTIFY_RIGHT)
2058                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2059                                                    FALSE, FALSE, 0);
2060                         else
2061                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2062                                                  FALSE, FALSE, 0);
2063                 }
2064
2065                 gtk_widget_show_all(hbox);
2066                 gtk_clist_set_column_widget(clist, pos, hbox);
2067         }
2068 }
2069
2070 void summary_reflect_prefs(void)
2071 {
2072         SummaryView *summaryview = NULL;
2073         if (!mainwindow_get_mainwindow())
2074                 return;
2075         summaryview = mainwindow_get_mainwindow()->summaryview;
2076         bold_style = bold_marked_style = bold_deleted_style = 
2077                 small_style = small_marked_style = small_deleted_style = NULL;
2078         summary_set_fonts(summaryview);
2079         summary_set_column_titles(summaryview);
2080         summary_show(summaryview, summaryview->folder_item);
2081 }
2082
2083 void summary_sort(SummaryView *summaryview,
2084                   FolderSortKey sort_key, FolderSortType sort_type)
2085 {
2086         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2087         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2088         GtkCListCompareFunc cmp_func = NULL;
2089         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2090                                        G_CALLBACK(summary_tree_expanded), summaryview);
2091         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2092
2093         switch (sort_key) {
2094         case SORT_BY_MARK:
2095                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2096                 break;
2097         case SORT_BY_STATUS:
2098                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2099                 break;
2100         case SORT_BY_MIME:
2101                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2102                 break;
2103         case SORT_BY_NUMBER:
2104                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2105                 break;
2106         case SORT_BY_SIZE:
2107                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2108                 break;
2109         case SORT_BY_DATE:
2110                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2111                 break;
2112         case SORT_BY_FROM:
2113                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2114                 break;
2115         case SORT_BY_SUBJECT:
2116                 if (summaryview->simplify_subject_preg)
2117                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2118                 else
2119                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2120                 break;
2121         case SORT_BY_SCORE:
2122                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2123                 break;
2124         case SORT_BY_LABEL:
2125                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2126                 break;
2127         case SORT_BY_TO:
2128                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2129                 break;
2130         case SORT_BY_LOCKED:
2131                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2132                 break;
2133         case SORT_BY_NONE:
2134                 break;
2135         default:
2136                 goto unlock;
2137         }
2138
2139         summaryview->sort_key = sort_key;
2140         summaryview->sort_type = sort_type;
2141
2142         summary_set_column_titles(summaryview);
2143         summary_set_menu_sensitive(summaryview);
2144
2145         /* allow fallback to don't sort */
2146         if (summaryview->sort_key == SORT_BY_NONE)
2147                 goto unlock;
2148
2149         if (cmp_func != NULL) {
2150                 debug_print("Sorting summary...");
2151                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2152
2153                 main_window_cursor_wait(summaryview->mainwin);
2154
2155                 gtk_clist_freeze(clist);
2156                 gtk_clist_set_compare_func(clist, cmp_func);
2157
2158                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2159
2160                 gtk_sctree_sort_recursive(ctree, NULL);
2161
2162                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2163
2164                 main_window_cursor_normal(summaryview->mainwin);
2165                 gtk_clist_thaw(clist);
2166
2167                 debug_print("done.\n");
2168                 STATUSBAR_POP(summaryview->mainwin);
2169         }
2170 unlock:
2171         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2172         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2173                                        G_CALLBACK(summary_tree_expanded), summaryview);
2174 }
2175
2176 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2177                                    GtkCTreeNode *cnode, gpointer data)
2178 {
2179         SummaryView *summaryview = (SummaryView *)data;
2180         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2181         gchar *text[N_SUMMARY_COLS];
2182         gint *col_pos = summaryview->col_pos;
2183         const gchar *msgid = msginfo->msgid;
2184         GHashTable *msgid_table = summaryview->msgid_table;
2185
2186         summary_set_header(summaryview, text, msginfo);
2187
2188         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2189                                 NULL, NULL, NULL, NULL, FALSE,
2190                                 gnode->parent->parent ? TRUE : FALSE);
2191 #define SET_TEXT(col) \
2192         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2193                                 text[col_pos[col]])
2194
2195         SET_TEXT(S_COL_NUMBER);
2196         SET_TEXT(S_COL_SCORE);
2197         SET_TEXT(S_COL_SIZE);
2198         SET_TEXT(S_COL_DATE);
2199         SET_TEXT(S_COL_FROM);
2200         SET_TEXT(S_COL_TO);
2201         /* SET_TEXT(S_COL_SUBJECT);  already set by node info */
2202
2203 #undef SET_TEXT
2204
2205         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2206         summary_set_marks_func(ctree, cnode, summaryview);
2207
2208         if (msgid && msgid[0] != '\0')
2209                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2210
2211         return TRUE;
2212 }
2213
2214 static void summary_set_ctree_from_list(SummaryView *summaryview,
2215                                         GSList *mlist)
2216 {
2217         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2218         MsgInfo *msginfo;
2219         GtkCTreeNode *node = NULL;
2220         GHashTable *msgid_table;
2221         GHashTable *subject_table;
2222         GSList * cur;
2223         START_TIMING("summary_set_ctree_from_list");
2224         
2225         if (!mlist) return;
2226
2227         debug_print("\tSetting summary from message data...");
2228         STATUSBAR_PUSH(summaryview->mainwin,
2229                        _("Setting summary from message data..."));
2230         gdk_flush();
2231
2232         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2233                                        G_CALLBACK(summary_tree_expanded), summaryview);
2234
2235         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2236         summaryview->msgid_table = msgid_table;
2237         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2238         summaryview->subject_table = subject_table;
2239
2240         if (prefs_common.use_addr_book)
2241                 start_address_completion();
2242         
2243         if (summaryview->threaded) {
2244                 GNode *root, *gnode;
2245
2246                 root = procmsg_get_thread_tree(mlist);
2247
2248                 for (gnode = root->children; gnode != NULL;
2249                      gnode = gnode->next) {
2250                         node = gtk_ctree_insert_gnode
2251                                 (ctree, NULL, node, gnode,
2252                                  summary_insert_gnode_func, summaryview);
2253                 }
2254
2255                 g_node_destroy(root);
2256                 
2257                 summary_thread_init(summaryview);
2258         } else {
2259                 gchar *text[N_SUMMARY_COLS];
2260                 cur = mlist;
2261                 for (; mlist != NULL; mlist = mlist->next) {
2262                         msginfo = (MsgInfo *)mlist->data;
2263
2264                         summary_set_header(summaryview, text, msginfo);
2265
2266                         node = gtk_ctree_insert_node
2267                                 (ctree, NULL, node, text, 2,
2268                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2269                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2270                         summary_set_marks_func(ctree, node, summaryview);
2271
2272                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2273                                 g_hash_table_insert(msgid_table,
2274                                                     msginfo->msgid, node);
2275
2276                         subject_table_insert(subject_table,
2277                                              msginfo->subject,
2278                                              node);
2279                 }
2280                 mlist = cur;
2281         }
2282
2283         if (prefs_common.enable_hscrollbar &&
2284             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2285                 gint optimal_width;
2286
2287                 optimal_width = gtk_clist_optimal_column_width
2288                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2289                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2290                                            summaryview->col_pos[S_COL_SUBJECT],
2291                                            optimal_width);
2292         }
2293
2294         if (prefs_common.use_addr_book)
2295                 end_address_completion();
2296
2297         debug_print("done.\n");
2298         STATUSBAR_POP(summaryview->mainwin);
2299         if (debug_get_mode()) {
2300                 debug_print("\tmsgid hash table size = %d\n",
2301                             g_hash_table_size(msgid_table));
2302                 debug_print("\tsubject hash table size = %d\n",
2303                             g_hash_table_size(subject_table));
2304         }
2305
2306         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2307
2308         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2309
2310         if (prefs_common.bold_unread) {
2311                 while (node) {
2312                         GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2313                         if (GTK_CTREE_ROW(node)->children)
2314                                 summary_set_row_marks(summaryview, node);
2315                         node = next;
2316                 }
2317         }
2318
2319         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2320                                        G_CALLBACK(summary_tree_expanded), summaryview);
2321         END_TIMING();
2322 }
2323
2324 static gchar *summary_complete_address(const gchar *addr)
2325 {
2326         gint count;
2327         gchar *res, *tmp, *email_addr;
2328         
2329         if (addr == NULL)
2330                 return NULL;
2331
2332         Xstrdup_a(email_addr, addr, return NULL);
2333         extract_address(email_addr);
2334         g_return_val_if_fail(*email_addr, NULL);
2335
2336         /*
2337          * completion stuff must be already initialized
2338          */
2339         res = NULL;
2340         if (1 < (count = complete_address(email_addr))) {
2341                 tmp = get_complete_address(1);
2342                 res = procheader_get_fromname(tmp);
2343                 g_free(tmp);
2344         }
2345
2346         return res;
2347 }
2348
2349 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2350                                MsgInfo *msginfo)
2351 {
2352         static gchar date_modified[80];
2353         static gchar col_score[11];
2354         static gchar buf[BUFFSIZE];
2355         gint *col_pos = summaryview->col_pos;
2356         gchar *from_text = NULL, *to_text = NULL;
2357         gboolean should_swap = FALSE;
2358
2359         text[col_pos[S_COL_FROM]]   = "";
2360         text[col_pos[S_COL_TO]]     = "";
2361         text[col_pos[S_COL_SUBJECT]]= "";
2362         text[col_pos[S_COL_MARK]]   = "";
2363         text[col_pos[S_COL_STATUS]] = "";
2364         text[col_pos[S_COL_MIME]]   = "";
2365         text[col_pos[S_COL_LOCKED]] = "";
2366         text[col_pos[S_COL_DATE]]   = "";
2367         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2368         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2369         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2370
2371         if (msginfo->date_t) {
2372                 procheader_date_get_localtime(date_modified,
2373                                               sizeof(date_modified),
2374                                               msginfo->date_t);
2375                 text[col_pos[S_COL_DATE]] = date_modified;
2376         } else if (msginfo->date)
2377                 text[col_pos[S_COL_DATE]] = msginfo->date;
2378         else
2379                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2380
2381         if (prefs_common.swap_from && msginfo->from && msginfo->to) {
2382                 gchar *addr = NULL;
2383                 
2384                 addr = g_strdup(msginfo->from);
2385
2386                 if (addr) {
2387                         extract_address(addr);
2388                         if (account_find_from_address(addr)) {
2389                                 should_swap = TRUE;
2390                         }
2391                         g_free(addr);
2392                 }
2393         }
2394
2395         if (!prefs_common.use_addr_book) {
2396                 from_text = msginfo->fromname ? 
2397                                 msginfo->fromname :
2398                                 _("(No From)");
2399         } else {
2400                 gchar *tmp = summary_complete_address(msginfo->from);
2401                 from_text = tmp ? tmp : (msginfo->fromname ?
2402                                          msginfo->fromname: 
2403                                                 _("(No From)"));
2404         }
2405         
2406         to_text = msginfo->to ? msginfo->to : 
2407                    (msginfo->cc ? msginfo->cc :
2408                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2409                      )
2410                    );
2411
2412         if (!should_swap) {
2413                 text[col_pos[S_COL_FROM]] = from_text;
2414                 text[col_pos[S_COL_TO]] = to_text;
2415         } else {
2416                 gchar *tmp = NULL;
2417                 tmp = g_strconcat("-->", to_text, NULL);
2418                 text[col_pos[S_COL_FROM]] = tmp;
2419                 tmp = g_strconcat("<--", from_text, NULL);
2420                 text[col_pos[S_COL_TO]] = tmp;
2421         }
2422         
2423         if (summaryview->simplify_subject_preg != NULL)
2424                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2425                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2426                                         summaryview->simplify_subject_preg) : 
2427                         _("(No Subject)");
2428         else 
2429                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2430                         _("(No Subject)");
2431 }
2432
2433 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2434 {
2435         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2436 }
2437
2438 static gboolean defer_change(gpointer data);
2439 typedef struct _ChangeData {
2440         MsgInfo *info;
2441         gint op; /* 0, 1, 2 for unset, set, change */
2442         MsgPermFlags set_flags;
2443         MsgTmpFlags  set_tmp_flags;
2444         MsgPermFlags unset_flags;
2445         MsgTmpFlags  unset_tmp_flags;
2446 } ChangeData;
2447
2448 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2449 {
2450         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2451                 debug_print("flags: doing unset now\n");
2452                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
2453         } else {
2454                 ChangeData *unset_data = g_new0(ChangeData, 1);
2455                 unset_data->info = msginfo;
2456                 unset_data->op = 0;
2457                 unset_data->unset_flags = flags;
2458                 unset_data->unset_tmp_flags = tmp_flags;
2459                 debug_print("flags: deferring unset\n");
2460                 g_timeout_add(100, defer_change, unset_data);
2461         }
2462 }
2463
2464 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2465 {
2466         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2467                 debug_print("flags: doing set now\n");
2468                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
2469         } else {
2470                 ChangeData *set_data = g_new0(ChangeData, 1);
2471                 set_data->info = msginfo;
2472                 set_data->op = 1;
2473                 set_data->set_flags = flags;
2474                 set_data->set_tmp_flags = tmp_flags;
2475                 debug_print("flags: deferring set\n");
2476                 g_timeout_add(100, defer_change, set_data);
2477         }
2478 }
2479
2480 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
2481                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
2482                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
2483 {
2484         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2485                 debug_print("flags: doing change now\n");
2486                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
2487                         rem_flags, rem_tmp_flags);
2488         } else {
2489                 ChangeData *change_data = g_new0(ChangeData, 1);
2490                 change_data->info = msginfo;
2491                 change_data->op = 2;
2492                 change_data->set_flags = add_flags;
2493                 change_data->set_tmp_flags = add_tmp_flags;
2494                 change_data->unset_flags = rem_flags;
2495                 change_data->unset_tmp_flags = rem_tmp_flags;
2496                 debug_print("flags: deferring change\n");
2497                 g_timeout_add(100, defer_change, change_data);
2498         }
2499 }
2500
2501 gboolean defer_change(gpointer data)
2502 {
2503         ChangeData *chg = (ChangeData *)data;
2504         if (chg->info->folder && chg->info->folder->processing_pending) {
2505                 debug_print("flags: trying later\n");
2506                 return TRUE; /* try again */
2507         } else {
2508                 debug_print("flags: finally doing it\n");
2509                 switch(chg->op) {
2510                 case 0:
2511                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
2512                         break;
2513                 case 1:
2514                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
2515                         break;
2516                 case 2:
2517                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
2518                                 chg->unset_flags, chg->unset_tmp_flags);
2519                         break;
2520                 default:
2521                         g_warning("shouldn't happen\n");
2522                 }
2523                 g_free(chg);
2524         }
2525         return FALSE;
2526 }
2527
2528 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2529                                       GtkCTreeNode *row)
2530 {
2531         g_return_if_fail(summaryview != NULL);
2532         g_return_if_fail(msginfo != NULL);
2533         g_return_if_fail(row != NULL);
2534
2535         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2536                 summary_msginfo_unset_flags
2537                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2538                 summary_set_row_marks(summaryview, row);
2539                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2540                 summary_status_show(summaryview);
2541         }
2542 }
2543
2544 typedef struct  {
2545         MsgInfo *msginfo;
2546         SummaryView *summaryview;
2547         GtkCTreeNode *row;
2548 } MarkAsReadData;
2549
2550 static int msginfo_mark_as_read_timeout(void *data)
2551 {
2552         MarkAsReadData *mdata = (MarkAsReadData *)data;
2553         if (!mdata)
2554                 return FALSE;
2555         
2556         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2557                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2558                                      mdata->row); 
2559
2560         g_free(mdata);
2561
2562         return FALSE;   
2563 }
2564
2565 static void summary_display_msg_full(SummaryView *summaryview,
2566                                      GtkCTreeNode *row,
2567                                      gboolean new_window, gboolean all_headers)
2568 {
2569         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2570         MsgInfo *msginfo;
2571         gint val;
2572
2573         if (!new_window) {
2574                 if (summaryview->displayed == row)
2575                         return;
2576                 else
2577                         summaryview->messageview->filtered = FALSE;
2578         }                       
2579         
2580         g_return_if_fail(row != NULL);
2581
2582         if (summary_is_locked(summaryview)) return;
2583         summary_lock(summaryview);
2584
2585         STATUSBAR_POP(summaryview->mainwin);
2586         GTK_EVENTS_FLUSH();
2587
2588         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2589
2590         if (new_window) {
2591                 MessageView *msgview;
2592
2593                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2594                 val = messageview_show(msgview, msginfo, all_headers);
2595         } else {
2596                 MessageView *msgview;
2597
2598                 msgview = summaryview->messageview;
2599
2600                 summaryview->displayed = row;
2601                 if (!messageview_is_visible(msgview)) {
2602                         main_window_toggle_message_view(summaryview->mainwin);
2603                         GTK_EVENTS_FLUSH();
2604                 }
2605                 val = messageview_show(msgview, msginfo, all_headers);
2606                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2607                         gtk_widget_grab_focus(summaryview->ctree);
2608                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2609         }
2610
2611         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2612                 if (prefs_common.mark_as_read_delay) {
2613                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2614                         data->summaryview = summaryview;
2615                         data->msginfo = msginfo;
2616                         data->row = row;
2617                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2618                                 msginfo_mark_as_read_timeout, data);
2619                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2620                         msginfo_mark_as_read(summaryview, msginfo, row);
2621                 }
2622         }
2623
2624         summary_set_menu_sensitive(summaryview);
2625         toolbar_main_set_sensitive(summaryview->mainwin);
2626         messageview_set_menu_sensitive(summaryview->messageview);
2627
2628         summary_unlock(summaryview);
2629 }
2630
2631 void summary_display_msg_selected(SummaryView *summaryview,
2632                                   gboolean all_headers)
2633 {
2634         if (summary_is_locked(summaryview)) return;
2635         summaryview->displayed = NULL;
2636         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2637                                  all_headers);
2638 }
2639
2640 void summary_redisplay_msg(SummaryView *summaryview)
2641 {
2642         GtkCTreeNode *node;
2643
2644         if (summaryview->displayed) {
2645                 node = summaryview->displayed;
2646                 summaryview->displayed = NULL;
2647                 summary_display_msg(summaryview, node);
2648         }
2649 }
2650
2651 void summary_open_msg(SummaryView *summaryview)
2652 {
2653         if (!summaryview->selected) return;
2654         
2655         /* CLAWS: if separate message view, don't open a new window
2656          * but rather use the current separated message view */
2657         summary_display_msg_full(summaryview, summaryview->selected,
2658                                  prefs_common.sep_msg ? FALSE : TRUE, 
2659                                  FALSE);
2660 }
2661
2662 void summary_view_source(SummaryView * summaryview)
2663 {
2664         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2665         MsgInfo *msginfo;
2666         SourceWindow *srcwin;
2667
2668         if (!summaryview->selected) return;
2669
2670         srcwin = source_window_create();
2671         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2672         source_window_show_msg(srcwin, msginfo);
2673         source_window_show(srcwin);
2674 }
2675
2676 void summary_reedit(SummaryView *summaryview)
2677 {
2678         MsgInfo *msginfo;
2679
2680         if (!summaryview->selected) return;
2681         if (!summaryview->folder_item) return;
2682         if (!folder_has_parent_of_type(summaryview->folder_item, F_OUTBOX)
2683         &&  !folder_has_parent_of_type(summaryview->folder_item, F_DRAFT)
2684         &&  !folder_has_parent_of_type(summaryview->folder_item, F_QUEUE))
2685                 return;
2686
2687         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2688                                               summaryview->selected);
2689         if (!msginfo) return;
2690
2691         compose_reedit(msginfo);
2692 }
2693
2694 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2695 {
2696         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2697         GtkCTreeNode *node;
2698
2699         if (summary_is_locked(summaryview)) return FALSE;
2700         if (type == GTK_SCROLL_STEP_FORWARD) {
2701                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2702                 if (node)
2703                         gtkut_ctree_expand_parent_all(ctree, node);
2704                 else
2705                         return FALSE;
2706         } else {
2707                 if (summaryview->selected) {
2708                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2709                         if (!node) return FALSE;
2710                 }
2711         }
2712
2713         if (messageview_is_visible(summaryview->messageview))
2714                 summaryview->display_msg = TRUE;
2715
2716         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2717
2718         if (GTK_CLIST(ctree)->selection)
2719                 gtk_sctree_set_anchor_row
2720                         (GTK_SCTREE(ctree),
2721                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2722
2723         return TRUE;
2724 }
2725
2726 void summary_toggle_view(SummaryView *summaryview)
2727 {
2728         if (!messageview_is_visible(summaryview->messageview) &&
2729             summaryview->selected)
2730                 summary_display_msg(summaryview,
2731                                     summaryview->selected);
2732         else
2733                 main_window_toggle_message_view(summaryview->mainwin);
2734 }
2735
2736 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2737                                                 GtkCTreeNode *node)
2738 {
2739         MsgInfo *msginfo;
2740
2741         if (node) {
2742                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2743                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2744                         return TRUE;
2745                 node = GTK_CTREE_ROW(node)->children;
2746         } else
2747                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2748
2749         while (node) {
2750                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2751                         return TRUE;
2752                 node = GTK_CTREE_ROW(node)->sibling;
2753         }
2754
2755         return FALSE;
2756 }
2757
2758 static gboolean summary_have_unread_children(SummaryView *summaryview,
2759                                              GtkCTreeNode *node)
2760 {
2761         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2762
2763         if (!node) return FALSE;
2764
2765         node = GTK_CTREE_ROW(node)->children;
2766
2767         while (node) {
2768                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2769                         return TRUE;
2770                 node = GTK_CTREE_ROW(node)->sibling;
2771         }
2772         return FALSE;
2773 }
2774
2775 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2776 {
2777         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2778         GtkStyle *style = NULL;
2779         MsgInfo *msginfo;
2780         MsgFlags flags;
2781         gint *col_pos = summaryview->col_pos;
2782
2783         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2784         if (!msginfo) return;
2785
2786         flags = msginfo->flags;
2787
2788         gtk_ctree_node_set_foreground(ctree, row, NULL);
2789
2790         /* set new/unread column */
2791         if (MSG_IS_IGNORE_THREAD(flags)) {
2792                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2793                                           ignorethreadxpm, ignorethreadxpmmask);
2794         } else if (MSG_IS_NEW(flags)) {
2795                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2796                                           newxpm, newxpmmask);
2797         } else if (MSG_IS_UNREAD(flags)) {
2798                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2799                                           unreadxpm, unreadxpmmask);
2800         } else if (MSG_IS_REPLIED(flags)) {
2801                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2802                                           repliedxpm, repliedxpmmask);
2803         } else if (MSG_IS_FORWARDED(flags)) {
2804                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2805                                           forwardedxpm, forwardedxpmmask);
2806         } else {
2807                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2808                                         "");
2809         }
2810
2811         if (prefs_common.bold_unread &&
2812             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2813              (!GTK_CTREE_ROW(row)->expanded &&
2814               GTK_CTREE_ROW(row)->children &&
2815               summary_have_unread_children(summaryview, row))))
2816                 style = bold_style;
2817
2818         /* set mark column */
2819         if (MSG_IS_DELETED(flags)) {
2820                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2821                                           deletedxpm, deletedxpmmask);
2822                 if (style)
2823                         style = bold_deleted_style;
2824                 else {
2825                         style = small_deleted_style;
2826                 }
2827                         gtk_ctree_node_set_foreground
2828                                 (ctree, row, &summaryview->color_dim);
2829         } else if (MSG_IS_MARKED(flags)) {
2830                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2831                                           markxpm, markxpmmask);
2832         } else if (MSG_IS_MOVE(flags)) {
2833                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2834                 if (style)
2835                         style = bold_marked_style;
2836                 else {
2837                         style = small_marked_style;
2838                 }
2839                         gtk_ctree_node_set_foreground
2840                                 (ctree, row, &summaryview->color_marked);
2841         } else if (MSG_IS_COPY(flags)) {
2842                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2843                 if (style)
2844                         style = bold_marked_style;
2845                 else {
2846                         style = small_marked_style;
2847                 }
2848                         gtk_ctree_node_set_foreground
2849                                 (ctree, row, &summaryview->color_marked);
2850 #if 0
2851         } else if ((global_scoring ||
2852                   summaryview->folder_item->prefs->scoring) &&
2853                  (msginfo->score >= summaryview->important_score) &&
2854                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2855                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2856                 gtk_ctree_node_set_foreground(ctree, row,
2857                                               &summaryview->color_important);
2858 #endif
2859         } else {
2860                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "");
2861         }
2862
2863         if (MSG_IS_LOCKED(flags)) {
2864                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2865                                           lockedxpm, lockedxpmmask);
2866         }
2867         else {
2868                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], "");
2869         }
2870
2871         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2872                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2873                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2874         } else if (MSG_IS_SIGNED(flags)) {
2875                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2876                                           gpgsignedxpm, gpgsignedxpmmask);
2877         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2878                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2879                                           clipkeyxpm, clipkeyxpmmask);
2880         } else if (MSG_IS_ENCRYPTED(flags)) {
2881                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2882                                           keyxpm, keyxpmmask);
2883         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2884                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2885                                           clipxpm, clipxpmmask);
2886         } else {
2887                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], "");
2888         }
2889         if (!style)
2890                 style = small_style;
2891
2892         gtk_ctree_node_set_row_style(ctree, row, style);
2893
2894         if (MSG_GET_COLORLABEL(flags))
2895                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2896 }
2897
2898 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2899 {
2900         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2901         MsgInfo *msginfo;
2902
2903         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2904         if (MSG_IS_DELETED(msginfo->flags))
2905                 summaryview->deleted--;
2906         if (MSG_IS_MOVE(msginfo->flags))
2907                 summaryview->moved--;
2908         if (MSG_IS_COPY(msginfo->flags))
2909                 summaryview->copied--;
2910
2911         procmsg_msginfo_set_to_folder(msginfo, NULL);
2912         summary_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2913         summary_set_row_marks(summaryview, row);
2914         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2915 }
2916
2917 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2918 {
2919         gboolean changed = FALSE;
2920         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2921         MsgInfo *msginfo;
2922
2923         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2924         if (MSG_IS_DELETED(msginfo->flags))
2925                 summaryview->deleted--;
2926         if (MSG_IS_MOVE(msginfo->flags)) {
2927                 summaryview->moved--;
2928                 changed = TRUE;
2929         }
2930         if (MSG_IS_COPY(msginfo->flags)) {
2931                 summaryview->copied--;
2932                 changed = TRUE;
2933         }
2934         procmsg_msginfo_set_to_folder(msginfo, NULL);
2935         summary_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2936         
2937         summary_set_row_marks(summaryview, row);
2938         debug_print("Message %d is locked\n", msginfo->msgnum);
2939 }
2940
2941 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2942 {
2943         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2944         MsgInfo *msginfo;
2945
2946         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2947         if (!MSG_IS_LOCKED(msginfo->flags))
2948                 return;
2949         procmsg_msginfo_set_to_folder(msginfo, NULL);
2950         summary_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2951         summary_set_row_marks(summaryview, row);
2952         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2953 }
2954
2955 void summary_mark(SummaryView *summaryview)
2956 {
2957         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2958         GList *cur;
2959
2960         START_LONG_OPERATION(summaryview);
2961         folder_item_set_batch(summaryview->folder_item, TRUE);
2962         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2963                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2964         folder_item_set_batch(summaryview->folder_item, FALSE);
2965         END_LONG_OPERATION(summaryview);
2966
2967         summary_status_show(summaryview);
2968 }
2969
2970 static void summary_mark_row_as_read(SummaryView *summaryview,
2971                                      GtkCTreeNode *row)
2972 {
2973         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2974         MsgInfo *msginfo;
2975
2976         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2977
2978         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2979                 return;
2980
2981         summary_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2982         summary_set_row_marks(summaryview, row);
2983         debug_print("Message %d is marked as read\n",
2984                 msginfo->msgnum);
2985 }
2986
2987 void summary_mark_as_read(SummaryView *summaryview)
2988 {
2989         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2990         GList *cur;
2991
2992         START_LONG_OPERATION(summaryview);
2993         folder_item_set_batch(summaryview->folder_item, TRUE);
2994         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2995                 summary_mark_row_as_read(summaryview,
2996                                          GTK_CTREE_NODE(cur->data));
2997         folder_item_set_batch(summaryview->folder_item, FALSE);
2998         END_LONG_OPERATION(summaryview);
2999         
3000         summary_status_show(summaryview);
3001 }
3002
3003 void summary_msgs_lock(SummaryView *summaryview)
3004 {
3005         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3006         GList *cur;
3007
3008         START_LONG_OPERATION(summaryview);
3009         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3010                 summary_lock_row(summaryview,
3011                                          GTK_CTREE_NODE(cur->data));
3012         END_LONG_OPERATION(summaryview);
3013         
3014         summary_status_show(summaryview);
3015 }
3016
3017 void summary_msgs_unlock(SummaryView *summaryview)
3018 {
3019         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3020         GList *cur;
3021
3022         START_LONG_OPERATION(summaryview);
3023         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3024                 summary_unlock_row(summaryview,
3025                                    GTK_CTREE_NODE(cur->data));
3026         END_LONG_OPERATION(summaryview);
3027         
3028         summary_status_show(summaryview);
3029 }
3030
3031 void summary_mark_all_read(SummaryView *summaryview)
3032 {
3033         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3034         GtkCTreeNode *node;
3035
3036         START_LONG_OPERATION(summaryview);
3037         folder_item_set_batch(summaryview->folder_item, TRUE);
3038         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3039              node = gtkut_ctree_node_next(ctree, node))
3040                 summary_mark_row_as_read(summaryview, node);
3041         folder_item_set_batch(summaryview->folder_item, FALSE);
3042         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3043              node = gtkut_ctree_node_next(ctree, node)) {
3044                 if (!GTK_CTREE_ROW(node)->expanded)
3045                         summary_set_row_marks(summaryview, node);
3046         }
3047         END_LONG_OPERATION(summaryview);
3048         
3049         summary_status_show(summaryview);
3050 }
3051
3052 static void summary_mark_row_as_unread(SummaryView *summaryview,
3053                                        GtkCTreeNode *row)
3054 {
3055         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3056         MsgInfo *msginfo;
3057
3058         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3059         if (MSG_IS_DELETED(msginfo->flags)) {
3060                 procmsg_msginfo_set_to_folder(msginfo, NULL);
3061                 summary_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
3062                 summaryview->deleted--;
3063         }
3064
3065         summary_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
3066         debug_print("Message %d is marked as unread\n",
3067                 msginfo->msgnum);
3068
3069         summary_set_row_marks(summaryview, row);
3070 }
3071
3072 void summary_mark_as_unread(SummaryView *summaryview)
3073 {
3074         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3075         GList *cur;
3076
3077         START_LONG_OPERATION(summaryview);
3078         folder_item_set_batch(summaryview->folder_item, TRUE);
3079         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3080                 cur = cur->next)
3081                 summary_mark_row_as_unread(summaryview,
3082                                            GTK_CTREE_NODE(cur->data));
3083         folder_item_set_batch(summaryview->folder_item, FALSE);
3084         END_LONG_OPERATION(summaryview);
3085         
3086         summary_status_show(summaryview);
3087 }
3088
3089 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
3090 {
3091         GList * cur;
3092         gboolean found;
3093
3094         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
3095
3096         case F_NEWS:
3097
3098                 /*
3099                   security : checks if one the accounts correspond to
3100                   the author of the post
3101                 */
3102
3103                 found = FALSE;
3104                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3105                         PrefsAccount * account;
3106                         gchar * from_name;
3107                         
3108                         account = cur->data;
3109                         if (account->name && *account->name)
3110                                 from_name =
3111                                         g_strdup_printf("%s <%s>",
3112                                                         account->name,
3113                                                         account->address);
3114                         else
3115                                 from_name =
3116                                         g_strdup_printf("%s",
3117                                                         account->address);
3118                         
3119                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3120                                 g_free(from_name);
3121                                 found = TRUE;
3122                                 break;
3123                         }
3124                         g_free(from_name);
3125                 }
3126
3127                 if (!found) {
3128                         alertpanel_error(_("You're not the author of the article\n"));
3129                 }
3130                 
3131                 return found;
3132
3133         default:
3134                 return TRUE;
3135         }
3136 }
3137
3138 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3139 {
3140         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3141         MsgInfo *msginfo;
3142
3143         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3144
3145         if (!check_permission(summaryview, msginfo))
3146                 return;
3147
3148         if (MSG_IS_LOCKED(msginfo->flags)) return;
3149
3150         if (MSG_IS_DELETED(msginfo->flags)) return;
3151
3152         if (MSG_IS_MOVE(msginfo->flags))
3153                 summaryview->moved--;
3154         if (MSG_IS_COPY(msginfo->flags))
3155                 summaryview->copied--;
3156
3157         procmsg_msginfo_set_to_folder(msginfo, NULL);
3158         summary_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, MSG_MOVE | MSG_COPY);
3159         summaryview->deleted++;
3160
3161         if (!prefs_common.immediate_exec && 
3162             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3163                 summary_set_row_marks(summaryview, row);
3164
3165         debug_print("Message %s/%d is set to delete\n",
3166                     msginfo->folder->path, msginfo->msgnum);
3167 }
3168
3169 void summary_cancel(SummaryView *summaryview)
3170 {
3171         MsgInfo * msginfo;
3172         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3173
3174         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3175                                               summaryview->selected);
3176         if (!msginfo) return;
3177
3178         if (!check_permission(summaryview, msginfo))
3179                 return;
3180
3181         news_cancel_article(summaryview->folder_item->folder, msginfo);
3182         
3183         if (summary_is_locked(summaryview)) return;
3184
3185         summary_lock(summaryview);
3186
3187         gtk_clist_freeze(clist);
3188
3189         summary_update_status(summaryview);
3190         summary_status_show(summaryview);
3191
3192         gtk_clist_thaw(clist);
3193
3194         summary_unlock(summaryview);
3195 }
3196
3197 void summary_delete(SummaryView *summaryview)
3198 {
3199         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3200         FolderItem *item = summaryview->folder_item;
3201         GList *cur;
3202         GtkCTreeNode *sel_last = NULL;
3203         GtkCTreeNode *node;
3204         AlertValue aval;
3205         MsgInfo *msginfo;
3206
3207         if (!item) return;
3208
3209         if (summary_is_locked(summaryview)) return;
3210
3211         if (!summaryview->folder_item ||
3212             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3213
3214         aval = alertpanel(_("Delete message(s)"),
3215                           _("Do you really want to delete selected message(s)?"),
3216                           GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3217         if (aval != G_ALERTDEFAULT) return;
3218
3219         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3220              cur = cur->next) {
3221                 GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3222                 msginfo = gtk_ctree_node_get_row_data(ctree, row);
3223                 if (msginfo->total_size != 0 && 
3224                     msginfo->size != (off_t)msginfo->total_size)
3225                         partial_mark_for_delete(msginfo);
3226         }
3227
3228         main_window_cursor_wait(summaryview->mainwin);
3229
3230         /* next code sets current row focus right. We need to find a row
3231          * that is not deleted. */
3232         START_LONG_OPERATION(summaryview);
3233         folder_item_set_batch(summaryview->folder_item, TRUE);
3234         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3235                 sel_last = GTK_CTREE_NODE(cur->data);
3236                 summary_delete_row(summaryview, sel_last);
3237         }
3238         folder_item_set_batch(summaryview->folder_item, FALSE);
3239         END_LONG_OPERATION(summaryview);
3240
3241         node = summary_find_next_msg(summaryview, sel_last);
3242         if (!node)
3243                 node = summary_find_prev_msg(summaryview, sel_last);
3244
3245         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3246         
3247         if (prefs_common.immediate_exec || folder_has_parent_of_type(item, F_TRASH)) {
3248                 summary_execute(summaryview);
3249                 /* after deleting, the anchor may be at an invalid row
3250                  * so reset it to the node we found earlier */
3251                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3252         } else
3253                 summary_status_show(summaryview);
3254
3255                 
3256         main_window_cursor_normal(summaryview->mainwin);
3257 }
3258
3259 void summary_delete_trash(SummaryView *summaryview)
3260 {
3261         FolderItem *to_folder = NULL;
3262         PrefsAccount *ac;
3263         if (!summaryview->folder_item ||
3264             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3265         
3266         if (NULL != (ac = account_find_from_item(summaryview->folder_item)))
3267                 to_folder = account_get_special_folder(ac, F_TRASH);
3268
3269         if (to_folder == NULL)
3270                 to_folder = summaryview->folder_item->folder->trash;
3271         
3272         if (to_folder == NULL || to_folder == summaryview->folder_item
3273             || folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3274                 summary_delete(summaryview);
3275         else
3276                 summary_move_selected_to(summaryview, to_folder);
3277 }
3278
3279
3280 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3281 {
3282         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3283         MsgInfo *msginfo;
3284
3285         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3286         if (MSG_IS_DELETED(msginfo->flags))
3287                 summaryview->deleted--;
3288         if (MSG_IS_MOVE(msginfo->flags))
3289                 summaryview->moved--;
3290         if (MSG_IS_COPY(msginfo->flags))
3291                 summaryview->copied--;
3292
3293         procmsg_msginfo_set_to_folder(msginfo, NULL);
3294         summary_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3295         summary_set_row_marks(summaryview, row);
3296
3297         debug_print("Message %s/%d is unmarked\n",
3298                     msginfo->folder->path, msginfo->msgnum);
3299 }
3300
3301 void summary_unmark(SummaryView *summaryview)
3302 {
3303         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3304         GList *cur;
3305
3306         START_LONG_OPERATION(summaryview);
3307         folder_item_set_batch(summaryview->folder_item, TRUE);
3308         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3309                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3310         folder_item_set_batch(summaryview->folder_item, FALSE);
3311         END_LONG_OPERATION(summaryview);
3312         
3313         summary_status_show(summaryview);
3314 }
3315
3316 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3317                                 FolderItem *to_folder)
3318 {
3319         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3320         MsgInfo *msginfo;
3321
3322         g_return_if_fail(to_folder != NULL);
3323
3324         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3325         if (MSG_IS_LOCKED(msginfo->flags))
3326                 return;
3327
3328         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3329         if (MSG_IS_DELETED(msginfo->flags))
3330                 summaryview->deleted--;
3331         if (MSG_IS_COPY(msginfo->flags)) {
3332                 summaryview->copied--;
3333         }
3334         if (!MSG_IS_MOVE(msginfo->flags)) {
3335                 summary_msginfo_change_flags(msginfo, 0, MSG_MOVE, MSG_DELETED, MSG_COPY);
3336                 summaryview->moved++;
3337         } else {
3338                 summary_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_COPY);
3339         }
3340         
3341         if (!prefs_common.immediate_exec) {
3342                 summary_set_row_marks(summaryview, row);
3343         }
3344
3345         debug_print("Message %d is set to move to %s\n",
3346                     msginfo->msgnum, to_folder->path);
3347 }
3348
3349 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3350 {
3351         GList *cur;
3352
3353         if (!to_folder) return;
3354         if (!summaryview->folder_item ||
3355             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3356
3357         if (summary_is_locked(summaryview)) return;
3358
3359         if (summaryview->folder_item == to_folder) {
3360                 alertpanel_error(_("Destination is same as current folder."));
3361                 return;
3362         }
3363
3364         START_LONG_OPERATION(summaryview);
3365
3366         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3367              cur != NULL && cur->data != NULL; cur = cur->next)
3368                 summary_move_row_to
3369                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3370
3371         END_LONG_OPERATION(summaryview);
3372
3373         if (prefs_common.immediate_exec) {
3374                 summary_execute(summaryview);
3375         } else {
3376                 summary_status_show(summaryview);
3377         }
3378         
3379         if (!summaryview->selected) { /* this was the last message */
3380                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3381                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3382                 if (node)
3383                         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3384         }
3385
3386 }
3387
3388 void summary_move_to(SummaryView *summaryview)
3389 {
3390         FolderItem *to_folder;
3391
3392         if (!summaryview->folder_item ||
3393             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3394
3395         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3396                                          FOLDER_SEL_MOVE, NULL);
3397         summary_move_selected_to(summaryview, to_folder);
3398 }
3399
3400 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3401                                 FolderItem *to_folder)
3402 {
3403         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3404         MsgInfo *msginfo;
3405
3406         g_return_if_fail(to_folder != NULL);
3407
3408         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3409         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3410         if (MSG_IS_DELETED(msginfo->flags))
3411                 summaryview->deleted--;
3412         if (MSG_IS_MOVE(msginfo->flags)) {
3413                 summaryview->moved--;
3414         }
3415         
3416         if (!MSG_IS_COPY(msginfo->flags)) {
3417                 summary_msginfo_change_flags(msginfo, 0, MSG_COPY, MSG_DELETED, MSG_MOVE);
3418                 summaryview->copied++;
3419         } else {
3420                 summary_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE);
3421         }
3422         if (!prefs_common.immediate_exec) {
3423                 summary_set_row_marks(summaryview, row);
3424         }
3425
3426         debug_print("Message %d is set to copy to %s\n",
3427                     msginfo->msgnum, to_folder->path);
3428 }
3429
3430 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3431 {
3432         GList *cur;
3433
3434         if (!to_folder) return;
3435         if (!summaryview->folder_item) return;
3436
3437         if (summary_is_locked(summaryview)) return;
3438
3439         if (summaryview->folder_item == to_folder) {
3440                 alertpanel_error
3441                         (_("Destination to copy is same as current folder."));
3442                 return;
3443         }
3444
3445         START_LONG_OPERATION(summaryview);
3446
3447         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3448              cur != NULL && cur->data != NULL; cur = cur->next)
3449                 summary_copy_row_to
3450                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3451
3452         END_LONG_OPERATION(summaryview);
3453
3454         if (prefs_common.immediate_exec)
3455                 summary_execute(summaryview);
3456         else {
3457                 summary_status_show(summaryview);
3458         }
3459 }
3460
3461 void summary_copy_to(SummaryView *summaryview)
3462 {
3463         FolderItem *to_folder;
3464
3465         if (!summaryview->folder_item) return;
3466
3467         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3468                                          FOLDER_SEL_COPY, NULL);
3469         summary_copy_selected_to(summaryview, to_folder);
3470 }
3471
3472 void summary_add_address(SummaryView *summaryview)
3473 {
3474         MsgInfo *msginfo;
3475         gchar *from;
3476
3477         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3478                                               summaryview->selected);
3479         if (!msginfo) return;
3480
3481         Xstrdup_a(from, msginfo->from, return);
3482         eliminate_address_comment(from);
3483         extract_address(from);
3484         addressbook_add_contact(msginfo->fromname, from, NULL);
3485 }
3486
3487 void summary_select_all(SummaryView *summaryview)
3488 {
3489         if (!summaryview->folder_item) return;
3490
3491         summary_lock(summaryview);
3492         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3493         summary_unlock(summaryview);
3494         summary_status_show(summaryview);
3495 }
3496
3497 void summary_unselect_all(SummaryView *summaryview)
3498 {
3499         summary_lock(summaryview);
3500         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3501         summary_unlock(summaryview);
3502         summary_status_show(summaryview);
3503 }
3504
3505 void summary_select_thread(SummaryView *summaryview)
3506 {
3507         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3508         GtkCTreeNode *node = summaryview->selected;
3509
3510         if (!node) return;
3511
3512         while (GTK_CTREE_ROW(node)->parent != NULL)
3513                 node = GTK_CTREE_ROW(node)->parent;
3514
3515         if (node != summaryview->selected)
3516                 summary_select_node
3517                         (summaryview, node,
3518                          messageview_is_visible(summaryview->messageview),
3519                          FALSE);
3520
3521         gtk_ctree_select_recursive(ctree, node);
3522
3523         summary_status_show(summaryview);
3524 }
3525
3526 void summary_save_as(SummaryView *summaryview)
3527 {
3528         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3529         MsgInfo *msginfo;
3530         gchar *filename = NULL;
3531         gchar *src, *dest;
3532         gchar *tmp;
3533
3534         AlertValue aval = 0;
3535
3536         if (!summaryview->selected) return;
3537         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3538         if (!msginfo) return;
3539
3540         if (msginfo->subject) {
3541                 Xstrdup_a(filename, msginfo->subject, return);
3542                 subst_for_filename(filename);
3543         }
3544         if (g_getenv ("G_BROKEN_FILENAMES") &&
3545             filename && !g_utf8_validate(filename, -1, NULL)) {
3546                 gchar *oldstr = filename;
3547                 filename = conv_codeset_strdup(filename,
3548                                                conv_get_locale_charset_str(),
3549                                                CS_UTF_8);
3550                 if (!filename) {
3551                         g_warning("summary_save_as(): faild to convert character set.");
3552                         filename = g_strdup(oldstr);
3553                 }
3554                 dest = filesel_select_file_save(_("Save as"), filename);
3555                 g_free(filename);
3556         } else
3557                 dest = filesel_select_file_save(_("Save as"), filename);
3558         filename = NULL;
3559         if (!dest) return;
3560         if (is_file_exist(dest)) {
3561                 aval = alertpanel(_("Append or Overwrite"),
3562                                   _("Append or overwrite existing file?"),
3563                                   _("_Append"), _("_Overwrite"),
3564                                   GTK_STOCK_CANCEL);
3565                 if (aval != 0 && aval != 1)
3566                         return;
3567         }
3568
3569         src = procmsg_get_message_file(msginfo);
3570         tmp = g_path_get_basename(dest);
3571
3572         if ( aval==0 ) { /* append */
3573                 if (append_file(src, dest, TRUE) < 0) 
3574                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3575         } else { /* overwrite */
3576                 if (copy_file(src, dest, TRUE) < 0)
3577                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3578         }
3579         g_free(src);
3580         
3581         /*
3582          * If two or more msgs are selected,
3583          * append them to the output file.
3584          */
3585         if (GTK_CLIST(ctree)->selection->next) {
3586                 GList *item;
3587                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3588                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3589                         if (!msginfo) break;
3590                         src = procmsg_get_message_file(msginfo);
3591                         if (append_file(src, dest, TRUE) < 0)
3592                                 alertpanel_error(_("Can't save the file '%s'."), tmp);
3593                 }
3594                 g_free(src);
3595         }
3596         g_free(dest);
3597         g_free(tmp);
3598 }
3599
3600 #ifdef USE_GNOMEPRINT
3601 static void print_mimeview(MimeView *mimeview) 
3602 {
3603         if (!mimeview 
3604         ||  !mimeview->textview
3605         ||  !mimeview->textview->text)
3606                 alertpanel_warning(_("Cannot print: the message doesn't "
3607                                      "contain text."));
3608         else
3609                 gedit_print(GTK_TEXT_VIEW(mimeview->textview->text));
3610 }
3611 #endif
3612
3613 void summary_print(SummaryView *summaryview)
3614 {
3615         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3616 #ifndef USE_GNOMEPRINT
3617         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3618         MsgInfo *msginfo;
3619         gchar *cmdline = NULL;
3620         gchar *p;
3621 #endif
3622         GList *cur;
3623
3624         if (clist->selection == NULL) return;
3625 #ifndef USE_GNOMEPRINT
3626         cmdline = input_dialog(_("Print"),
3627                                _("Enter the print command line:\n"
3628                                  "('%s' will be replaced with file name)"),
3629                                prefs_common.print_cmd);
3630         if (!cmdline) return;
3631         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3632             strchr(p + 2, '%')) {
3633                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
3634                                  cmdline);
3635                 g_free(cmdline);
3636                 return;
3637         }
3638         for (cur = clist->selection; 
3639              cur != NULL && cur->data != NULL; 
3640              cur = cur->next) {
3641                 msginfo = gtk_ctree_node_get_row_data
3642                         (ctree, GTK_CTREE_NODE(cur->data));
3643                 if (msginfo) 
3644                         procmsg_print_message(msginfo, cmdline);
3645         }
3646
3647         g_free(cmdline);
3648 #else
3649         for (cur = clist->selection; 
3650              cur != NULL && cur->data != NULL; 
3651              cur = cur->next) {
3652                 GtkCTreeNode *node = GTK_CTREE_NODE(cur->data);
3653                 if (node != summaryview->displayed) {
3654                         MessageView *tmpview = messageview_create(
3655                                                 summaryview->mainwin);
3656                         MsgInfo *msginfo = gtk_ctree_node_get_row_data(
3657                                                 GTK_CTREE(summaryview->ctree),
3658                                                 node);
3659
3660                         messageview_init(tmpview);
3661                         tmpview->all_headers = summaryview->messageview->all_headers;
3662                         if (msginfo && messageview_show(tmpview, msginfo, 
3663                                 tmpview->all_headers) >= 0) {
3664                                         print_mimeview(tmpview->mimeview);
3665                         }
3666                         messageview_destroy(tmpview);
3667                 } else {
3668                         print_mimeview(summaryview->messageview->mimeview);
3669                 }
3670         }
3671 #endif
3672 }
3673
3674 gboolean summary_execute(SummaryView *summaryview)
3675 {
3676         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3677         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3678         GtkCTreeNode *node, *next;
3679         GtkCTreeNode *new_selected = NULL;
3680         gint move_val = -1;
3681
3682         if (!summaryview->folder_item) return FALSE;
3683
3684         if (summary_is_locked(summaryview)) return FALSE;
3685         summary_lock(summaryview);
3686
3687         gtk_clist_freeze(clist);
3688
3689         if (summaryview->threaded)
3690                 summary_unthread_for_exec(summaryview);
3691
3692         folder_item_update_freeze();
3693         move_val = summary_execute_move(summaryview);
3694         summary_execute_copy(summaryview);
3695         summary_execute_delete(summaryview);
3696         
3697         node = GTK_CTREE_NODE(clist->row_list);
3698         for (; node != NULL; node = next) {
3699                 next = gtkut_ctree_node_next(ctree, node);
3700                 if (gtk_ctree_node_get_row_data(ctree, node) != NULL) continue;
3701
3702                 if (node == summaryview->displayed) {
3703                         messageview_clear(summaryview->messageview);
3704                         summaryview->displayed = NULL;
3705                 }
3706                 if (GTK_CTREE_ROW(node)->children != NULL) {
3707                         g_warning("summary_execute(): children != NULL\n");
3708                         continue;
3709                 }
3710
3711                 if (!new_selected &&
3712                     gtkut_ctree_node_is_selected(ctree, node)) {
3713                         summary_unselect_all(summaryview);
3714                         new_selected = summary_find_next_msg(summaryview, node);
3715                         if (!new_selected)
3716                                 new_selected = summary_find_prev_msg
3717                                         (summaryview, node);
3718                 }
3719
3720                 gtk_ctree_remove_node(ctree, node);
3721         }
3722
3723         folder_item_update_thaw();
3724         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3725
3726         if (new_selected) {
3727                 summary_unlock(summaryview);
3728                 gtk_sctree_select
3729                         (GTK_SCTREE(ctree),
3730                          new_selected);
3731                 summary_lock(summaryview);
3732         }
3733
3734         if (summaryview->threaded) {
3735                 gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3736                 summary_thread_build(summaryview);
3737                 summary_thread_init(summaryview);
3738                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3739         }
3740
3741         summaryview->selected = clist->selection ?
3742                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3743
3744         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3745                 menu_set_insensitive_all
3746                         (GTK_MENU_SHELL(summaryview->popupmenu));
3747                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3748         } else
3749                 gtk_widget_grab_focus(summaryview->ctree);
3750
3751         summary_update_status(summaryview);
3752         summary_status_show(summaryview);
3753
3754         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3755
3756         summary_unlock(summaryview);
3757         
3758         if (move_val < 0) 
3759                 summary_show(summaryview, summaryview->folder_item);
3760         return TRUE;
3761 }
3762
3763 static gint summary_execute_move(SummaryView *summaryview)
3764 {
3765         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3766         GSList *cur;
3767         gint val = -1;
3768         /* search moving messages and execute */
3769         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3770                                 summaryview);
3771
3772         if (summaryview->mlist) {
3773                 val = procmsg_move_messages(summaryview->mlist);
3774
3775                 for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3776                         procmsg_msginfo_free((MsgInfo *)cur->data);
3777                 g_slist_free(summaryview->mlist);
3778                 summaryview->mlist = NULL;
3779         }
3780         return val;
3781 }
3782
3783 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3784                                       gpointer data)
3785 {
3786         SummaryView *summaryview = data;
3787         MsgInfo *msginfo;
3788
3789         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3790
3791         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3792                 summaryview->mlist =
3793                         g_slist_prepend(summaryview->mlist, msginfo);
3794                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3795
3796                 if (msginfo->msgid && *msginfo->msgid &&
3797                     node == g_hash_table_lookup(summaryview->msgid_table,
3798                                                 msginfo->msgid))
3799                         g_hash_table_remove(summaryview->msgid_table,
3800                                             msginfo->msgid);
3801         }
3802 }
3803
3804 static void summary_execute_copy(SummaryView *summaryview)
3805 {
3806         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3807
3808         /* search copying messages and execute */
3809         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3810                                 summaryview);
3811
3812         if (summaryview->mlist) {
3813                 summaryview->mlist = g_slist_reverse(summaryview->mlist);
3814                 procmsg_copy_messages(summaryview->mlist);
3815
3816                 g_slist_free(summaryview->mlist);
3817                 summaryview->mlist = NULL;
3818         }
3819 }
3820
3821 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3822                                       gpointer data)
3823 {
3824         SummaryView *summaryview = data;
3825         MsgInfo *msginfo;
3826
3827         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3828
3829         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3830                 summaryview->mlist =
3831                         g_slist_prepend(summaryview->mlist, msginfo);
3832
3833                 summary_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3834                 summary_set_row_marks(summaryview, node);
3835         }
3836 }
3837
3838 static void summary_execute_delete(SummaryView *summaryview)
3839 {
3840         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3841         GSList *cur;
3842
3843         /* search&nbs