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