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