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