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