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