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