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