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