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