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