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