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