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