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