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