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