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