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