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