a2a327653044e8ca165caff38295f4384ebdd3cc
[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 void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2439                                       GtkCTreeNode *row)
2440 {
2441         g_return_if_fail(summaryview != NULL);
2442         g_return_if_fail(msginfo != NULL);
2443         g_return_if_fail(row != NULL);
2444
2445         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2446                 procmsg_msginfo_unset_flags
2447                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2448                 summary_set_row_marks(summaryview, row);
2449                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2450                 summary_status_show(summaryview);
2451         }
2452 }
2453
2454 typedef struct  {
2455         MsgInfo *msginfo;
2456         SummaryView *summaryview;
2457         GtkCTreeNode *row;
2458 } MarkAsReadData;
2459
2460 static int msginfo_mark_as_read_timeout(void *data)
2461 {
2462         MarkAsReadData *mdata = (MarkAsReadData *)data;
2463         if (!mdata)
2464                 return FALSE;
2465         
2466         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2467                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2468                                      mdata->row); 
2469
2470         g_free(mdata);
2471
2472         return FALSE;   
2473 }
2474
2475 static void summary_display_msg_full(SummaryView *summaryview,
2476                                      GtkCTreeNode *row,
2477                                      gboolean new_window, gboolean all_headers)
2478 {
2479         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2480         MsgInfo *msginfo;
2481         gint val;
2482
2483         if (!new_window) {
2484                 if (summaryview->displayed == row)
2485                         return;
2486                 else
2487                         summaryview->messageview->filtered = FALSE;
2488         }                       
2489         
2490         g_return_if_fail(row != NULL);
2491
2492         if (summary_is_locked(summaryview)) return;
2493         summary_lock(summaryview);
2494
2495         STATUSBAR_POP(summaryview->mainwin);
2496         GTK_EVENTS_FLUSH();
2497
2498         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2499
2500         if (new_window) {
2501                 MessageView *msgview;
2502
2503                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2504                 val = messageview_show(msgview, msginfo, all_headers);
2505         } else {
2506                 MessageView *msgview;
2507
2508                 msgview = summaryview->messageview;
2509
2510                 summaryview->displayed = row;
2511                 if (!messageview_is_visible(msgview)) {
2512                         main_window_toggle_message_view(summaryview->mainwin);
2513                         GTK_EVENTS_FLUSH();
2514                 }
2515                 val = messageview_show(msgview, msginfo, all_headers);
2516                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2517                         gtk_widget_grab_focus(summaryview->ctree);
2518                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2519         }
2520
2521         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2522                 if (prefs_common.mark_as_read_delay) {
2523                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2524                         data->summaryview = summaryview;
2525                         data->msginfo = msginfo;
2526                         data->row = row;
2527                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2528                                 msginfo_mark_as_read_timeout, data);
2529                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2530                         msginfo_mark_as_read(summaryview, msginfo, row);
2531                 }
2532         }
2533
2534         summary_set_menu_sensitive(summaryview);
2535         toolbar_main_set_sensitive(summaryview->mainwin);
2536         messageview_set_menu_sensitive(summaryview->messageview);
2537
2538         summary_unlock(summaryview);
2539 }
2540
2541 void summary_display_msg_selected(SummaryView *summaryview,
2542                                   gboolean all_headers)
2543 {
2544         if (summary_is_locked(summaryview)) return;
2545         summaryview->displayed = NULL;
2546         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2547                                  all_headers);
2548 }
2549
2550 void summary_redisplay_msg(SummaryView *summaryview)
2551 {
2552         GtkCTreeNode *node;
2553
2554         if (summaryview->displayed) {
2555                 node = summaryview->displayed;
2556                 summaryview->displayed = NULL;
2557                 summary_display_msg(summaryview, node);
2558         }
2559 }
2560
2561 void summary_open_msg(SummaryView *summaryview)
2562 {
2563         if (!summaryview->selected) return;
2564         
2565         /* CLAWS: if separate message view, don't open a new window
2566          * but rather use the current separated message view */
2567         summary_display_msg_full(summaryview, summaryview->selected,
2568                                  prefs_common.sep_msg ? FALSE : TRUE, 
2569                                  FALSE);
2570 }
2571
2572 void summary_view_source(SummaryView * summaryview)
2573 {
2574         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2575         MsgInfo *msginfo;
2576         SourceWindow *srcwin;
2577
2578         if (!summaryview->selected) return;
2579
2580         srcwin = source_window_create();
2581         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2582         source_window_show_msg(srcwin, msginfo);
2583         source_window_show(srcwin);
2584 }
2585
2586 void summary_reedit(SummaryView *summaryview)
2587 {
2588         MsgInfo *msginfo;
2589
2590         if (!summaryview->selected) return;
2591         if (!summaryview->folder_item) return;
2592         if (!folder_has_parent_of_type(summaryview->folder_item, F_OUTBOX)
2593         &&  !folder_has_parent_of_type(summaryview->folder_item, F_DRAFT)
2594         &&  !folder_has_parent_of_type(summaryview->folder_item, F_QUEUE))
2595                 return;
2596
2597         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2598                                               summaryview->selected);
2599         if (!msginfo) return;
2600
2601         compose_reedit(msginfo);
2602 }
2603
2604 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2605 {
2606         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2607         GtkCTreeNode *node;
2608
2609         if (summary_is_locked(summaryview)) return FALSE;
2610         if (type == GTK_SCROLL_STEP_FORWARD) {
2611                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2612                 if (node)
2613                         gtkut_ctree_expand_parent_all(ctree, node);
2614                 else
2615                         return FALSE;
2616         } else {
2617                 if (summaryview->selected) {
2618                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2619                         if (!node) return FALSE;
2620                 }
2621         }
2622
2623         if (messageview_is_visible(summaryview->messageview))
2624                 summaryview->display_msg = TRUE;
2625
2626         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2627
2628         if (GTK_CLIST(ctree)->selection)
2629                 gtk_sctree_set_anchor_row
2630                         (GTK_SCTREE(ctree),
2631                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2632
2633         return TRUE;
2634 }
2635
2636 void summary_toggle_view(SummaryView *summaryview)
2637 {
2638         if (!messageview_is_visible(summaryview->messageview) &&
2639             summaryview->selected)
2640                 summary_display_msg(summaryview,
2641                                     summaryview->selected);
2642         else
2643                 main_window_toggle_message_view(summaryview->mainwin);
2644 }
2645
2646 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2647                                                 GtkCTreeNode *node)
2648 {
2649         MsgInfo *msginfo;
2650
2651         if (node) {
2652                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2653                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2654                         return TRUE;
2655                 node = GTK_CTREE_ROW(node)->children;
2656         } else
2657                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2658
2659         while (node) {
2660                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2661                         return TRUE;
2662                 node = GTK_CTREE_ROW(node)->sibling;
2663         }
2664
2665         return FALSE;
2666 }
2667
2668 static gboolean summary_have_unread_children(SummaryView *summaryview,
2669                                              GtkCTreeNode *node)
2670 {
2671         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2672
2673         if (!node) return FALSE;
2674
2675         node = GTK_CTREE_ROW(node)->children;
2676
2677         while (node) {
2678                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2679                         return TRUE;
2680                 node = GTK_CTREE_ROW(node)->sibling;
2681         }
2682         return FALSE;
2683 }
2684
2685 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2686 {
2687         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2688         GtkStyle *style = NULL;
2689         MsgInfo *msginfo;
2690         MsgFlags flags;
2691         gint *col_pos = summaryview->col_pos;
2692
2693         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2694         if (!msginfo) return;
2695
2696         flags = msginfo->flags;
2697
2698         gtk_ctree_node_set_foreground(ctree, row, NULL);
2699
2700         /* set new/unread column */
2701         if (MSG_IS_IGNORE_THREAD(flags)) {
2702                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2703                                           ignorethreadxpm, ignorethreadxpmmask);
2704         } else if (MSG_IS_NEW(flags)) {
2705                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2706                                           newxpm, newxpmmask);
2707         } else if (MSG_IS_UNREAD(flags)) {
2708                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2709                                           unreadxpm, unreadxpmmask);
2710         } else if (MSG_IS_REPLIED(flags)) {
2711                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2712                                           repliedxpm, repliedxpmmask);
2713         } else if (MSG_IS_FORWARDED(flags)) {
2714                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2715                                           forwardedxpm, forwardedxpmmask);
2716         } else {
2717                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2718                                         "");
2719         }
2720
2721         if (prefs_common.bold_unread &&
2722             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2723              (!GTK_CTREE_ROW(row)->expanded &&
2724               GTK_CTREE_ROW(row)->children &&
2725               summary_have_unread_children(summaryview, row))))
2726                 style = bold_style;
2727
2728         /* set mark column */
2729         if (MSG_IS_DELETED(flags)) {
2730                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2731                                           deletedxpm, deletedxpmmask);
2732                 if (style)
2733                         style = bold_deleted_style;
2734                 else {
2735                         style = small_deleted_style;
2736                 }
2737                         gtk_ctree_node_set_foreground
2738                                 (ctree, row, &summaryview->color_dim);
2739         } else if (MSG_IS_MARKED(flags)) {
2740                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2741                                           markxpm, markxpmmask);
2742         } else if (MSG_IS_MOVE(flags)) {
2743                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2744                 if (style)
2745                         style = bold_marked_style;
2746                 else {
2747                         style = small_marked_style;
2748                 }
2749                         gtk_ctree_node_set_foreground
2750                                 (ctree, row, &summaryview->color_marked);
2751         } else if (MSG_IS_COPY(flags)) {
2752                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2753                 if (style)
2754                         style = bold_marked_style;
2755                 else {
2756                         style = small_marked_style;
2757                 }
2758                         gtk_ctree_node_set_foreground
2759                                 (ctree, row, &summaryview->color_marked);
2760 #if 0
2761         } else if ((global_scoring ||
2762                   summaryview->folder_item->prefs->scoring) &&
2763                  (msginfo->score >= summaryview->important_score) &&
2764                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2765                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2766                 gtk_ctree_node_set_foreground(ctree, row,
2767                                               &summaryview->color_important);
2768 #endif
2769         } else {
2770                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "");
2771         }
2772
2773         if (MSG_IS_LOCKED(flags)) {
2774                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2775                                           lockedxpm, lockedxpmmask);
2776         }
2777         else {
2778                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], "");
2779         }
2780
2781         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2782                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2783                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2784         } else if (MSG_IS_SIGNED(flags)) {
2785                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2786                                           gpgsignedxpm, gpgsignedxpmmask);
2787         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2788                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2789                                           clipkeyxpm, clipkeyxpmmask);
2790         } else if (MSG_IS_ENCRYPTED(flags)) {
2791                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2792                                           keyxpm, keyxpmmask);
2793         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2794                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2795                                           clipxpm, clipxpmmask);
2796         } else {
2797                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], "");
2798         }
2799         if (!style)
2800                 style = small_style;
2801
2802         gtk_ctree_node_set_row_style(ctree, row, style);
2803
2804         if (MSG_GET_COLORLABEL(flags))
2805                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2806 }
2807
2808 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2809 {
2810         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2811         MsgInfo *msginfo;
2812
2813         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2814         if (MSG_IS_DELETED(msginfo->flags))
2815                 summaryview->deleted--;
2816         if (MSG_IS_MOVE(msginfo->flags))
2817                 summaryview->moved--;
2818         if (MSG_IS_COPY(msginfo->flags))
2819                 summaryview->copied--;
2820
2821         procmsg_msginfo_set_to_folder(msginfo, NULL);
2822         procmsg_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2823         summary_set_row_marks(summaryview, row);
2824         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2825 }
2826
2827 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2828 {
2829         gboolean changed = FALSE;
2830         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2831         MsgInfo *msginfo;
2832
2833         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2834         if (MSG_IS_DELETED(msginfo->flags))
2835                 summaryview->deleted--;
2836         if (MSG_IS_MOVE(msginfo->flags)) {
2837                 summaryview->moved--;
2838                 changed = TRUE;
2839         }
2840         if (MSG_IS_COPY(msginfo->flags)) {
2841                 summaryview->copied--;
2842                 changed = TRUE;
2843         }
2844         procmsg_msginfo_set_to_folder(msginfo, NULL);
2845         procmsg_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2846         
2847         summary_set_row_marks(summaryview, row);
2848         debug_print("Message %d is locked\n", msginfo->msgnum);
2849 }
2850
2851 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2852 {
2853         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2854         MsgInfo *msginfo;
2855
2856         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2857         if (!MSG_IS_LOCKED(msginfo->flags))
2858                 return;
2859         procmsg_msginfo_set_to_folder(msginfo, NULL);
2860         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2861         summary_set_row_marks(summaryview, row);
2862         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2863 }
2864
2865 void summary_mark(SummaryView *summaryview)
2866 {
2867         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2868         GList *cur;
2869
2870         START_LONG_OPERATION(summaryview);
2871         folder_item_set_batch(summaryview->folder_item, TRUE);
2872         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2873                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2874         folder_item_set_batch(summaryview->folder_item, FALSE);
2875         END_LONG_OPERATION(summaryview);
2876
2877         summary_status_show(summaryview);
2878 }
2879
2880 static void summary_mark_row_as_read(SummaryView *summaryview,
2881                                      GtkCTreeNode *row)
2882 {
2883         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2884         MsgInfo *msginfo;
2885
2886         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2887
2888         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2889                 return;
2890
2891         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2892         summary_set_row_marks(summaryview, row);
2893         debug_print("Message %d is marked as read\n",
2894                 msginfo->msgnum);
2895 }
2896
2897 void summary_mark_as_read(SummaryView *summaryview)
2898 {
2899         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2900         GList *cur;
2901
2902         START_LONG_OPERATION(summaryview);
2903         folder_item_set_batch(summaryview->folder_item, TRUE);
2904         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2905                 summary_mark_row_as_read(summaryview,
2906                                          GTK_CTREE_NODE(cur->data));
2907         folder_item_set_batch(summaryview->folder_item, FALSE);
2908         END_LONG_OPERATION(summaryview);
2909         
2910         summary_status_show(summaryview);
2911 }
2912
2913 void summary_msgs_lock(SummaryView *summaryview)
2914 {
2915         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2916         GList *cur;
2917
2918         START_LONG_OPERATION(summaryview);
2919         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2920                 summary_lock_row(summaryview,
2921                                          GTK_CTREE_NODE(cur->data));
2922         END_LONG_OPERATION(summaryview);
2923         
2924         summary_status_show(summaryview);
2925 }
2926
2927 void summary_msgs_unlock(SummaryView *summaryview)
2928 {
2929         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2930         GList *cur;
2931
2932         START_LONG_OPERATION(summaryview);
2933         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2934                 summary_unlock_row(summaryview,
2935                                    GTK_CTREE_NODE(cur->data));
2936         END_LONG_OPERATION(summaryview);
2937         
2938         summary_status_show(summaryview);
2939 }
2940
2941 void summary_mark_all_read(SummaryView *summaryview)
2942 {
2943         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2944         GtkCTreeNode *node;
2945
2946         START_LONG_OPERATION(summaryview);
2947         folder_item_set_batch(summaryview->folder_item, TRUE);
2948         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2949              node = gtkut_ctree_node_next(ctree, node))
2950                 summary_mark_row_as_read(summaryview, node);
2951         folder_item_set_batch(summaryview->folder_item, FALSE);
2952         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2953              node = gtkut_ctree_node_next(ctree, node)) {
2954                 if (!GTK_CTREE_ROW(node)->expanded)
2955                         summary_set_row_marks(summaryview, node);
2956         }
2957         END_LONG_OPERATION(summaryview);
2958         
2959         summary_status_show(summaryview);
2960 }
2961
2962 static void summary_mark_row_as_unread(SummaryView *summaryview,
2963                                        GtkCTreeNode *row)
2964 {
2965         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2966         MsgInfo *msginfo;
2967
2968         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2969         if (MSG_IS_DELETED(msginfo->flags)) {
2970                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2971                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2972                 summaryview->deleted--;
2973         }
2974
2975         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2976         debug_print("Message %d is marked as unread\n",
2977                 msginfo->msgnum);
2978
2979         summary_set_row_marks(summaryview, row);
2980 }
2981
2982 void summary_mark_as_unread(SummaryView *summaryview)
2983 {
2984         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2985         GList *cur;
2986
2987         START_LONG_OPERATION(summaryview);
2988         folder_item_set_batch(summaryview->folder_item, TRUE);
2989         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
2990                 cur = cur->next)
2991                 summary_mark_row_as_unread(summaryview,
2992                                            GTK_CTREE_NODE(cur->data));
2993         folder_item_set_batch(summaryview->folder_item, FALSE);
2994         END_LONG_OPERATION(summaryview);
2995         
2996         summary_status_show(summaryview);
2997 }
2998
2999 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
3000 {
3001         GList * cur;
3002         gboolean found;
3003
3004         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
3005
3006         case F_NEWS:
3007
3008                 /*
3009                   security : checks if one the accounts correspond to
3010                   the author of the post
3011                 */
3012
3013                 found = FALSE;
3014                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3015                         PrefsAccount * account;
3016                         gchar * from_name;
3017                         
3018                         account = cur->data;
3019                         if (account->name && *account->name)
3020                                 from_name =
3021                                         g_strdup_printf("%s <%s>",
3022                                                         account->name,
3023                                                         account->address);
3024                         else
3025                                 from_name =
3026                                         g_strdup_printf("%s",
3027                                                         account->address);
3028                         
3029                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3030                                 g_free(from_name);
3031                                 found = TRUE;
3032                                 break;
3033                         }
3034                         g_free(from_name);
3035                 }
3036
3037                 if (!found) {
3038                         alertpanel_error(_("You're not the author of the article\n"));
3039                 }
3040                 
3041                 return found;
3042
3043         default:
3044                 return TRUE;
3045         }
3046 }
3047
3048 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3049 {
3050         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3051         MsgInfo *msginfo;
3052
3053         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3054
3055         if (!check_permission(summaryview, msginfo))
3056                 return;
3057
3058         if (MSG_IS_LOCKED(msginfo->flags)) return;
3059
3060         if (MSG_IS_DELETED(msginfo->flags)) return;
3061
3062         if (MSG_IS_MOVE(msginfo->flags))
3063                 summaryview->moved--;
3064         if (MSG_IS_COPY(msginfo->flags))
3065                 summaryview->copied--;
3066
3067         procmsg_msginfo_set_to_folder(msginfo, NULL);
3068         procmsg_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, MSG_MOVE | MSG_COPY);
3069         summaryview->deleted++;
3070
3071         if (!prefs_common.immediate_exec && 
3072             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3073                 summary_set_row_marks(summaryview, row);
3074
3075         debug_print("Message %s/%d is set to delete\n",
3076                     msginfo->folder->path, msginfo->msgnum);
3077 }
3078
3079 void summary_cancel(SummaryView *summaryview)
3080 {
3081         MsgInfo * msginfo;
3082         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3083
3084         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3085                                               summaryview->selected);
3086         if (!msginfo) return;
3087
3088         if (!check_permission(summaryview, msginfo))
3089                 return;
3090
3091         news_cancel_article(summaryview->folder_item->folder, msginfo);
3092         
3093         if (summary_is_locked(summaryview)) return;
3094
3095         summary_lock(summaryview);
3096
3097         gtk_clist_freeze(clist);
3098
3099         summary_update_status(summaryview);
3100         summary_status_show(summaryview);
3101
3102         gtk_clist_thaw(clist);
3103
3104         summary_unlock(summaryview);
3105 }
3106
3107 void summary_delete(SummaryView *summaryview)
3108 {
3109         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3110         FolderItem *item = summaryview->folder_item;
3111         GList *cur;
3112         GtkCTreeNode *sel_last = NULL;
3113         GtkCTreeNode *node;
3114         AlertValue aval;
3115         MsgInfo *msginfo;
3116
3117         if (!item) return;
3118
3119         if (summary_is_locked(summaryview)) return;
3120
3121         if (!summaryview->folder_item ||
3122             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3123
3124         aval = alertpanel(_("Delete message(s)"),
3125                           _("Do you really want to delete selected message(s)?"),
3126                           GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3127         if (aval != G_ALERTDEFAULT) return;
3128
3129         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3130              cur = cur->next) {
3131                 GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3132                 msginfo = gtk_ctree_node_get_row_data(ctree, row);
3133                 if (msginfo->total_size != 0 && 
3134                     msginfo->size != (off_t)msginfo->total_size)
3135                         partial_mark_for_delete(msginfo);
3136         }
3137
3138         main_window_cursor_wait(summaryview->mainwin);
3139
3140         /* next code sets current row focus right. We need to find a row
3141          * that is not deleted. */
3142         START_LONG_OPERATION(summaryview);
3143         folder_item_set_batch(summaryview->folder_item, TRUE);
3144         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3145                 sel_last = GTK_CTREE_NODE(cur->data);
3146                 summary_delete_row(summaryview, sel_last);
3147         }
3148         folder_item_set_batch(summaryview->folder_item, FALSE);
3149         END_LONG_OPERATION(summaryview);
3150
3151         node = summary_find_next_msg(summaryview, sel_last);
3152         if (!node)
3153                 node = summary_find_prev_msg(summaryview, sel_last);
3154
3155         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3156         
3157         if (prefs_common.immediate_exec || folder_has_parent_of_type(item, F_TRASH)) {
3158                 summary_execute(summaryview);
3159                 /* after deleting, the anchor may be at an invalid row
3160                  * so reset it to the node we found earlier */
3161                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3162         } else
3163                 summary_status_show(summaryview);
3164
3165                 
3166         main_window_cursor_normal(summaryview->mainwin);
3167 }
3168
3169 void summary_delete_trash(SummaryView *summaryview)
3170 {
3171         FolderItem *to_folder = NULL;
3172         PrefsAccount *ac;
3173         if (!summaryview->folder_item ||
3174             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3175         
3176         if (NULL != (ac = account_find_from_item(summaryview->folder_item)))
3177                 to_folder = account_get_special_folder(ac, F_TRASH);
3178
3179         if (to_folder == NULL)
3180                 to_folder = summaryview->folder_item->folder->trash;
3181         
3182         if (to_folder == NULL || to_folder == summaryview->folder_item
3183             || folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3184                 summary_delete(summaryview);
3185         else
3186                 summary_move_selected_to(summaryview, to_folder);
3187 }
3188
3189
3190 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3191 {
3192         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3193         MsgInfo *msginfo;
3194
3195         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3196         if (MSG_IS_DELETED(msginfo->flags))
3197                 summaryview->deleted--;
3198         if (MSG_IS_MOVE(msginfo->flags))
3199                 summaryview->moved--;
3200         if (MSG_IS_COPY(msginfo->flags))
3201                 summaryview->copied--;
3202
3203         procmsg_msginfo_set_to_folder(msginfo, NULL);
3204         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3205         summary_set_row_marks(summaryview, row);
3206
3207         debug_print("Message %s/%d is unmarked\n",
3208                     msginfo->folder->path, msginfo->msgnum);
3209 }
3210
3211 void summary_unmark(SummaryView *summaryview)
3212 {
3213         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3214         GList *cur;
3215
3216         START_LONG_OPERATION(summaryview);
3217         folder_item_set_batch(summaryview->folder_item, TRUE);
3218         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3219                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3220         folder_item_set_batch(summaryview->folder_item, FALSE);
3221         END_LONG_OPERATION(summaryview);
3222         
3223         summary_status_show(summaryview);
3224 }
3225
3226 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3227                                 FolderItem *to_folder)
3228 {
3229         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3230         MsgInfo *msginfo;
3231
3232         g_return_if_fail(to_folder != NULL);
3233
3234         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3235         if (MSG_IS_LOCKED(msginfo->flags))
3236                 return;
3237
3238         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3239         if (MSG_IS_DELETED(msginfo->flags))
3240                 summaryview->deleted--;
3241         if (MSG_IS_COPY(msginfo->flags)) {
3242                 summaryview->copied--;
3243         }
3244         if (!MSG_IS_MOVE(msginfo->flags)) {
3245                 procmsg_msginfo_change_flags(msginfo, 0, MSG_MOVE, MSG_DELETED, MSG_COPY);
3246                 summaryview->moved++;
3247         } else {
3248                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_COPY);
3249         }
3250         
3251         if (!prefs_common.immediate_exec) {
3252                 summary_set_row_marks(summaryview, row);
3253         }
3254
3255         debug_print("Message %d is set to move to %s\n",
3256                     msginfo->msgnum, to_folder->path);
3257 }
3258
3259 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3260 {
3261         GList *cur;
3262
3263         if (!to_folder) return;
3264         if (!summaryview->folder_item ||
3265             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3266
3267         if (summary_is_locked(summaryview)) return;
3268
3269         if (summaryview->folder_item == to_folder) {
3270                 alertpanel_error(_("Destination is same as current folder."));
3271                 return;
3272         }
3273
3274         START_LONG_OPERATION(summaryview);
3275
3276         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3277              cur != NULL && cur->data != NULL; cur = cur->next)
3278                 summary_move_row_to
3279                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3280
3281         END_LONG_OPERATION(summaryview);
3282
3283         if (prefs_common.immediate_exec) {
3284                 summary_execute(summaryview);
3285         } else {
3286                 summary_status_show(summaryview);
3287         }
3288         
3289         if (!summaryview->selected) { /* this was the last message */
3290                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3291                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3292                 if (node)
3293                         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3294         }
3295
3296 }
3297
3298 void summary_move_to(SummaryView *summaryview)
3299 {
3300         FolderItem *to_folder;
3301
3302         if (!summaryview->folder_item ||
3303             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3304
3305         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3306                                          FOLDER_SEL_MOVE, NULL);
3307         summary_move_selected_to(summaryview, to_folder);
3308 }
3309
3310 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3311                                 FolderItem *to_folder)
3312 {
3313         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3314         MsgInfo *msginfo;
3315
3316         g_return_if_fail(to_folder != NULL);
3317
3318         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3319         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3320         if (MSG_IS_DELETED(msginfo->flags))
3321                 summaryview->deleted--;
3322         if (MSG_IS_MOVE(msginfo->flags)) {
3323                 summaryview->moved--;
3324         }
3325         
3326         if (!MSG_IS_COPY(msginfo->flags)) {
3327                 procmsg_msginfo_change_flags(msginfo, 0, MSG_COPY, MSG_DELETED, MSG_MOVE);
3328                 summaryview->copied++;
3329         } else {
3330                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE);
3331         }
3332         if (!prefs_common.immediate_exec) {
3333                 summary_set_row_marks(summaryview, row);
3334         }
3335
3336         debug_print("Message %d is set to copy to %s\n",
3337                     msginfo->msgnum, to_folder->path);
3338 }
3339
3340 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3341 {
3342         GList *cur;
3343
3344         if (!to_folder) return;
3345         if (!summaryview->folder_item) return;
3346
3347         if (summary_is_locked(summaryview)) return;
3348
3349         if (summaryview->folder_item == to_folder) {
3350                 alertpanel_error
3351                         (_("Destination to copy is same as current folder."));
3352                 return;
3353         }
3354
3355         START_LONG_OPERATION(summaryview);
3356
3357         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3358              cur != NULL && cur->data != NULL; cur = cur->next)
3359                 summary_copy_row_to
3360                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3361
3362         END_LONG_OPERATION(summaryview);
3363
3364         if (prefs_common.immediate_exec)
3365                 summary_execute(summaryview);
3366         else {
3367                 summary_status_show(summaryview);
3368         }
3369 }
3370
3371 void summary_copy_to(SummaryView *summaryview)
3372 {
3373         FolderItem *to_folder;
3374
3375         if (!summaryview->folder_item) return;
3376
3377         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3378                                          FOLDER_SEL_COPY, NULL);
3379         summary_copy_selected_to(summaryview, to_folder);
3380 }
3381
3382 void summary_add_address(SummaryView *summaryview)
3383 {
3384         MsgInfo *msginfo;
3385         gchar *from;
3386
3387         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3388                                               summaryview->selected);
3389         if (!msginfo) return;
3390
3391         Xstrdup_a(from, msginfo->from, return);
3392         eliminate_address_comment(from);
3393         extract_address(from);
3394         addressbook_add_contact(msginfo->fromname, from, NULL);
3395 }
3396
3397 void summary_select_all(SummaryView *summaryview)
3398 {
3399         if (!summaryview->folder_item) return;
3400
3401         summary_lock(summaryview);
3402         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3403         summary_unlock(summaryview);
3404         summary_status_show(summaryview);
3405 }
3406
3407 void summary_unselect_all(SummaryView *summaryview)
3408 {
3409         summary_lock(summaryview);
3410         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3411         summary_unlock(summaryview);
3412         summary_status_show(summaryview);
3413 }
3414
3415 void summary_select_thread(SummaryView *summaryview)
3416 {
3417         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3418         GtkCTreeNode *node = summaryview->selected;
3419
3420         if (!node) return;
3421
3422         while (GTK_CTREE_ROW(node)->parent != NULL)
3423                 node = GTK_CTREE_ROW(node)->parent;
3424
3425         if (node != summaryview->selected)
3426                 summary_select_node
3427                         (summaryview, node,
3428                          messageview_is_visible(summaryview->messageview),
3429                          FALSE);
3430
3431         gtk_ctree_select_recursive(ctree, node);
3432
3433         summary_status_show(summaryview);
3434 }
3435
3436 void summary_save_as(SummaryView *summaryview)
3437 {
3438         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3439         MsgInfo *msginfo;
3440         gchar *filename = NULL;
3441         gchar *src, *dest;
3442         gchar *tmp;
3443
3444         AlertValue aval = 0;
3445
3446         if (!summaryview->selected) return;
3447         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3448         if (!msginfo) return;
3449
3450         if (msginfo->subject) {
3451                 Xstrdup_a(filename, msginfo->subject, return);
3452                 subst_for_filename(filename);
3453         }
3454         if (g_getenv ("G_BROKEN_FILENAMES") &&
3455             filename && !g_utf8_validate(filename, -1, NULL)) {
3456                 gchar *oldstr = filename;
3457                 filename = conv_codeset_strdup(filename,
3458                                                conv_get_locale_charset_str(),
3459                                                CS_UTF_8);
3460                 if (!filename) {
3461                         g_warning("summary_save_as(): faild to convert character set.");
3462                         filename = g_strdup(oldstr);
3463                 }
3464                 dest = filesel_select_file_save(_("Save as"), filename);
3465                 g_free(filename);
3466         } else
3467                 dest = filesel_select_file_save(_("Save as"), filename);
3468         filename = NULL;
3469         if (!dest) return;
3470         if (is_file_exist(dest)) {
3471                 aval = alertpanel(_("Append or Overwrite"),
3472                                   _("Append or overwrite existing file?"),
3473                                   _("_Append"), _("_Overwrite"),
3474                                   GTK_STOCK_CANCEL);
3475                 if (aval != 0 && aval != 1)
3476                         return;
3477         }
3478
3479         src = procmsg_get_message_file(msginfo);
3480         tmp = g_path_get_basename(dest);
3481
3482         if ( aval==0 ) { /* append */
3483                 if (append_file(src, dest, TRUE) < 0) 
3484                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3485         } else { /* overwrite */
3486                 if (copy_file(src, dest, TRUE) < 0)
3487                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3488         }
3489         g_free(src);
3490         
3491         /*
3492          * If two or more msgs are selected,
3493          * append them to the output file.
3494          */
3495         if (GTK_CLIST(ctree)->selection->next) {
3496                 GList *item;
3497                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3498                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3499                         if (!msginfo) break;
3500                         src = procmsg_get_message_file(msginfo);
3501                         if (append_file(src, dest, TRUE) < 0)
3502                                 alertpanel_error(_("Can't save the file '%s'."), tmp);
3503                 }
3504                 g_free(src);
3505         }
3506         g_free(dest);
3507         g_free(tmp);
3508 }
3509
3510 #ifdef USE_GNOMEPRINT
3511 static void print_mimeview(MimeView *mimeview) 
3512 {
3513         if (!mimeview 
3514         ||  !mimeview->textview
3515         ||  !mimeview->textview->text)
3516                 alertpanel_warning(_("Cannot print: the message doesn't "
3517                                      "contain text."));
3518         else
3519                 gedit_print(GTK_TEXT_VIEW(mimeview->textview->text));
3520 }
3521 #endif
3522
3523 void summary_print(SummaryView *summaryview)
3524 {
3525         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3526 #ifndef USE_GNOMEPRINT
3527         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3528         MsgInfo *msginfo;
3529         gchar *cmdline = NULL;
3530         gchar *p;
3531 #endif
3532         GList *cur;
3533
3534         if (clist->selection == NULL) return;
3535 #ifndef USE_GNOMEPRINT
3536         cmdline = input_dialog(_("Print"),
3537                                _("Enter the print command line:\n"
3538                                  "('%s' will be replaced with file name)"),
3539                                prefs_common.print_cmd);
3540         if (!cmdline) return;
3541         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3542             strchr(p + 2, '%')) {
3543                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
3544                                  cmdline);
3545                 g_free(cmdline);
3546                 return;
3547         }
3548         for (cur = clist->selection; 
3549              cur != NULL && cur->data != NULL; 
3550              cur = cur->next) {
3551                 msginfo = gtk_ctree_node_get_row_data
3552                         (ctree, GTK_CTREE_NODE(cur->data));
3553                 if (msginfo) 
3554                         procmsg_print_message(msginfo, cmdline);
3555         }
3556
3557         g_free(cmdline);
3558 #else
3559         for (cur = clist->selection; 
3560              cur != NULL && cur->data != NULL; 
3561              cur = cur->next) {
3562                 GtkCTreeNode *node = GTK_CTREE_NODE(cur->data);
3563                 if (node != summaryview->displayed) {
3564                         MessageView *tmpview = messageview_create(
3565                                                 summaryview->mainwin);
3566                         MsgInfo *msginfo = gtk_ctree_node_get_row_data(
3567                                                 GTK_CTREE(summaryview->ctree),
3568                                                 node);
3569
3570                         messageview_init(tmpview);
3571                         tmpview->all_headers = summaryview->messageview->all_headers;
3572                         if (msginfo && messageview_show(tmpview, msginfo, 
3573                                 tmpview->all_headers) >= 0) {
3574                                         print_mimeview(tmpview->mimeview);
3575                         }
3576                         messageview_destroy(tmpview);
3577                 } else {
3578                         print_mimeview(summaryview->messageview->mimeview);
3579                 }
3580         }
3581 #endif
3582 }
3583
3584 gboolean summary_execute(SummaryView *summaryview)
3585 {
3586         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3587         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3588         GtkCTreeNode *node, *next;
3589         GtkCTreeNode *new_selected = NULL;
3590         gint move_val = -1;
3591
3592         if (!summaryview->folder_item) return FALSE;
3593
3594         if (summary_is_locked(summaryview)) return FALSE;
3595         summary_lock(summaryview);
3596
3597         gtk_clist_freeze(clist);
3598
3599         if (summaryview->threaded)
3600                 summary_unthread_for_exec(summaryview);
3601
3602         folder_item_update_freeze();
3603         move_val = summary_execute_move(summaryview);
3604         summary_execute_copy(summaryview);
3605         summary_execute_delete(summaryview);
3606         
3607         node = GTK_CTREE_NODE(clist->row_list);
3608         for (; node != NULL; node = next) {
3609                 next = gtkut_ctree_node_next(ctree, node);
3610                 if (gtk_ctree_node_get_row_data(ctree, node) != NULL) continue;
3611
3612                 if (node == summaryview->displayed) {
3613                         messageview_clear(summaryview->messageview);
3614                         summaryview->displayed = NULL;
3615                 }
3616                 if (GTK_CTREE_ROW(node)->children != NULL) {
3617                         g_warning("summary_execute(): children != NULL\n");
3618                         continue;
3619                 }
3620
3621                 if (!new_selected &&
3622                     gtkut_ctree_node_is_selected(ctree, node)) {
3623                         summary_unselect_all(summaryview);
3624                         new_selected = summary_find_next_msg(summaryview, node);
3625                         if (!new_selected)
3626                                 new_selected = summary_find_prev_msg
3627                                         (summaryview, node);
3628                 }
3629
3630                 gtk_ctree_remove_node(ctree, node);
3631         }
3632
3633         folder_item_update_thaw();
3634         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3635
3636         if (new_selected) {
3637                 summary_unlock(summaryview);
3638                 gtk_sctree_select
3639                         (GTK_SCTREE(ctree),
3640                          new_selected);
3641                 summary_lock(summaryview);
3642         }
3643
3644         if (summaryview->threaded) {
3645                 gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3646                 summary_thread_build(summaryview);
3647                 summary_thread_init(summaryview);
3648                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3649         }
3650
3651         summaryview->selected = clist->selection ?
3652                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3653
3654         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3655                 menu_set_insensitive_all
3656                         (GTK_MENU_SHELL(summaryview->popupmenu));
3657                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3658         } else
3659                 gtk_widget_grab_focus(summaryview->ctree);
3660
3661         summary_update_status(summaryview);
3662         summary_status_show(summaryview);
3663
3664         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3665
3666         summary_unlock(summaryview);
3667         
3668         if (move_val < 0) 
3669                 summary_show(summaryview, summaryview->folder_item);
3670         return TRUE;
3671 }
3672
3673 static gint summary_execute_move(SummaryView *summaryview)
3674 {
3675         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3676         GSList *cur;
3677         gint val = -1;
3678         /* search moving messages and execute */
3679         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3680                                 summaryview);
3681
3682         if (summaryview->mlist) {
3683                 val = procmsg_move_messages(summaryview->mlist);
3684
3685                 for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3686                         procmsg_msginfo_free((MsgInfo *)cur->data);
3687                 g_slist_free(summaryview->mlist);
3688                 summaryview->mlist = NULL;
3689         }
3690         return val;
3691 }
3692
3693 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3694                                       gpointer data)
3695 {
3696         SummaryView *summaryview = data;
3697         MsgInfo *msginfo;
3698
3699         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3700
3701         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3702                 summaryview->mlist =
3703                         g_slist_prepend(summaryview->mlist, msginfo);
3704                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3705
3706                 if (msginfo->msgid && *msginfo->msgid &&
3707                     node == g_hash_table_lookup(summaryview->msgid_table,
3708                                                 msginfo->msgid))
3709                         g_hash_table_remove(summaryview->msgid_table,
3710                                             msginfo->msgid);
3711         }
3712 }
3713
3714 static void summary_execute_copy(SummaryView *summaryview)
3715 {
3716         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3717
3718         /* search copying messages and execute */
3719         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3720                                 summaryview);
3721
3722         if (summaryview->mlist) {
3723                 summaryview->mlist = g_slist_reverse(summaryview->mlist);
3724                 procmsg_copy_messages(summaryview->mlist);
3725
3726                 g_slist_free(summaryview->mlist);
3727                 summaryview->mlist = NULL;
3728         }
3729 }
3730
3731 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3732                                       gpointer data)
3733 {
3734         SummaryView *summaryview = data;
3735         MsgInfo *msginfo;
3736
3737         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3738
3739         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3740                 summaryview->mlist =
3741                         g_slist_prepend(summaryview->mlist, msginfo);
3742
3743                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3744                 summary_set_row_marks(summaryview, node);
3745         }
3746 }
3747
3748 static void summary_execute_delete(SummaryView *summaryview)
3749 {
3750         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3751         GSList *cur;
3752
3753         /* search deleting messages and execute */
3754         gtk_ctree_pre_recursive
3755                 (ctree, NULL, summary_execute_delete_func, summaryview);
3756
3757         if (!summaryview->mlist) return;
3758
3759         folder_item_remove_msgs(summaryview->folder_item,
3760                                 summaryview->mlist);
3761
3762         for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3763                 procmsg_msginfo_free((MsgInfo *)cur->data);
3764
3765         g_slist_free(summaryview->mlist);
3766         summaryview->mlist = NULL;
3767 }
3768
3769 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3770                                         gpointer data)
3771 {
3772         SummaryView *summaryview = data;
3773         MsgInfo *msginfo;
3774
3775         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3776
3777         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3778                 summaryview->mlist =
3779                         g_slist_append(summaryview->mlist, msginfo);
3780                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3781
3782                 if (msginfo->msgid && *msginfo->msgid &&
3783                     node == g_hash_table_lookup(summaryview->msgid_table,
3784                                                 msginfo->msgid)) {
3785                         g_hash_table_remove(summaryview->msgid_table,
3786                                             msginfo->msgid);
3787                 }       
3788                 if (msginfo->subject && *msginfo->subject && 
3789                     node == subject_table_lookup(summaryview->subject_table,
3790                                                  msginfo->subject)) {
3791                         subject_table_remove(summaryview->subject_table,
3792                                              msginfo->subject);
3793                 }                                           
3794         }
3795 }
3796
3797 /* thread functions */
3798
3799 void summary_thread_build(SummaryView *summaryview)
3800 {
3801         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3802         GtkCTreeNode *node;
3803         GtkCTreeNode *next;
3804         GtkCTreeNode *parent;
3805         MsgInfo *msginfo;
3806         GSList *reflist;
3807
3808         summary_lock(summaryview);
3809
3810         debug_print("Building threads...");
3811         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3812         main_window_cursor_wait(summaryview->mainwin);
3813
3814         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3815                                        G_CALLBACK(summary_tree_expanded), summaryview);
3816         gtk_clist_freeze(GTK_CLIST(ctree));
3817
3818         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3819         while (node) {
3820                 next = GTK_CTREE_ROW(node)->sibling;
3821
3822                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3823
3824                 parent = NULL;
3825
3826                 if (msginfo && msginfo->inreplyto) {
3827                         parent = g_hash_table_lookup(summaryview->msgid_table,
3828                                                      msginfo->inreplyto);
3829                                                      
3830                         if (!parent && msginfo->references) {
3831                                 for (reflist = msginfo->references;
3832                                      reflist != NULL; reflist = reflist->next)
3833                                         if ((parent = g_hash_table_lookup
3834                                                 (summaryview->msgid_table,
3835                                                  reflist->data)))
3836                                                 break;
3837                         }
3838                 }