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