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