2005-02-23 [colin] 1.0.1cvs15.9
[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 <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkpixmap.h>
28 #include <gtk/gtkctree.h>
29 #include <gtk/gtkcontainer.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkitemfactory.h>
35 #include <gtk/gtkvbox.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkwindow.h>
38 #include <gtk/gtkstyle.h>
39 #include <gtk/gtkarrow.h>
40 #include <gtk/gtkeventbox.h>
41 #include <gtk/gtkstatusbar.h>
42 #include <gtk/gtkmenuitem.h>
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48
49 #include "main.h"
50 #include "menu.h"
51 #include "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
958         g_slist_free(mlist);
959
960         gtk_clist_thaw(GTK_CLIST(ctree));
961
962         if (is_refresh) {
963                 summaryview->displayed =
964                         summary_find_msg_by_msgnum(summaryview,
965                                                    displayed_msgnum);
966                 if (!summaryview->displayed)
967                         messageview_clear(summaryview->messageview);
968                 summary_select_by_msgnum(summaryview, selected_msgnum);
969                 if (!summaryview->selected) {
970                         /* no selected message - select first unread
971                            message, but do not display it */
972                         node = summary_find_next_flagged_msg(summaryview, NULL,
973                                                              MSG_UNREAD, FALSE);
974                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
975                                 node = gtk_ctree_node_nth
976                                         (ctree,
977                                          item->sort_type == SORT_DESCENDING
978                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
979                         summary_select_node(summaryview, node, FALSE, TRUE);
980                 }
981         } else {
982                 /* select first unread message */
983 #if 0
984                 if (summaryview->sort_key == SORT_BY_SCORE)
985                         node = summary_find_next_important_score(summaryview,
986                                                                  NULL);
987                 else
988 #endif
989                 node = summary_find_next_flagged_msg(summaryview, NULL,
990                                                      MSG_UNREAD, FALSE);
991                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
992                         node = gtk_ctree_node_nth
993                                 (ctree,
994                                  item->sort_type == SORT_DESCENDING
995                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
996                 }
997                 if (prefs_common.open_unread_on_enter ||
998                     prefs_common.always_show_msg) {
999                         summary_unlock(summaryview);
1000                         summary_select_node(summaryview, node, 
1001                                             messageview_is_visible(summaryview->messageview), 
1002                                             TRUE);
1003                         summary_lock(summaryview);
1004                 } else
1005                         summary_select_node(summaryview, node, FALSE, TRUE);
1006         }
1007
1008         summary_set_column_titles(summaryview);
1009         summary_status_show(summaryview);
1010         summary_set_menu_sensitive(summaryview);
1011         toolbar_main_set_sensitive(summaryview->mainwin);
1012
1013         debug_print("\n");
1014         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1015         STATUSBAR_POP(summaryview->mainwin);
1016         main_window_cursor_normal(summaryview->mainwin);
1017         summary_unlock(summaryview);
1018         inc_unlock();
1019
1020         return TRUE;
1021 }
1022
1023 #undef CURRENTLY_DISPLAYED
1024
1025 void summary_clear_list(SummaryView *summaryview)
1026 {
1027         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1028         gint optimal_width;
1029
1030         gtk_clist_freeze(clist);
1031
1032         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1033                                 NULL, summary_free_msginfo_func, NULL);
1034
1035         if (summaryview->folder_item) {
1036                 summaryview->folder_item->opened = FALSE;
1037                 summaryview->folder_item = NULL;
1038         }
1039
1040         summaryview->display_msg = FALSE;
1041
1042         summaryview->selected = NULL;
1043         summaryview->displayed = NULL;
1044         summaryview->total_size = 0;
1045         summaryview->deleted = summaryview->moved = 0;
1046         summaryview->copied = 0;
1047         if (summaryview->msgid_table) {
1048                 g_hash_table_destroy(summaryview->msgid_table);
1049                 summaryview->msgid_table = NULL;
1050         }
1051         if (summaryview->subject_table) {
1052                 g_hash_table_destroy(summaryview->subject_table);
1053                 summaryview->subject_table = NULL;
1054         }
1055         summaryview->mlist = NULL;
1056
1057         gtk_clist_clear(clist);
1058         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1059                 optimal_width = gtk_clist_optimal_column_width
1060                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1061                 gtk_clist_set_column_width
1062                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1063                          optimal_width);
1064         }
1065
1066         gtk_clist_thaw(clist);
1067 }
1068
1069 void summary_clear_all(SummaryView *summaryview)
1070 {
1071         messageview_clear(summaryview->messageview);
1072         summary_clear_list(summaryview);
1073         summary_set_menu_sensitive(summaryview);
1074         toolbar_main_set_sensitive(summaryview->mainwin);
1075         summary_status_show(summaryview);
1076 }
1077
1078 void summary_lock(SummaryView *summaryview)
1079 {
1080         summaryview->lock_count++;
1081 }
1082
1083 void summary_unlock(SummaryView *summaryview)
1084 {
1085         if (summaryview->lock_count)
1086                 summaryview->lock_count--;
1087 }
1088
1089 gboolean summary_is_locked(SummaryView *summaryview)
1090 {
1091         return summaryview->lock_count > 0;
1092 }
1093
1094 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1095 {
1096         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1097         SummarySelection selection;
1098
1099         if (!clist->row_list)
1100                 selection = SUMMARY_NONE;
1101         else if (!clist->selection)
1102                 selection = SUMMARY_SELECTED_NONE;
1103         else if (!clist->selection->next)
1104                 selection = SUMMARY_SELECTED_SINGLE;
1105         else
1106                 selection = SUMMARY_SELECTED_MULTIPLE;
1107
1108         return selection;
1109 }
1110
1111 /*!
1112  *\return       MsgInfo * Selected message if there's one selected;
1113  *              if multiple selected, or none, return NULL.
1114  */ 
1115 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1116 {
1117         /* summaryview->selected may be valid when multiple 
1118          * messages were selected */
1119         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1120
1121         if (sellist == NULL || sellist->next) 
1122                 return NULL;
1123         
1124         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1125 }
1126
1127 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1128 {
1129         GSList *mlist = NULL;
1130         GList *cur;
1131         MsgInfo *msginfo;
1132
1133         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
1134              cur = cur->next) {
1135                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1136                 mlist = g_slist_prepend(mlist, msginfo);
1137         }
1138
1139         mlist = g_slist_reverse(mlist);
1140
1141         return mlist;
1142 }
1143
1144 GSList *summary_get_msg_list(SummaryView *summaryview)
1145 {
1146         GSList *mlist = NULL;
1147         GtkCTree *ctree;
1148         GtkCTreeNode *node;
1149         MsgInfo *msginfo;
1150
1151         ctree = GTK_CTREE(summaryview->ctree);
1152
1153         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1154              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1155                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1156                 mlist = g_slist_prepend(mlist, msginfo);
1157         }
1158
1159         mlist = g_slist_reverse(mlist);
1160
1161         return mlist;
1162 }
1163
1164 static void summary_set_menu_sensitive(SummaryView *summaryview)
1165 {
1166         GtkItemFactory *ifactory = summaryview->popupfactory;
1167         SensitiveCond state;
1168         gboolean sensitive;
1169         GtkWidget *menuitem;
1170         gint i;
1171
1172         static const struct {
1173                 gchar *const entry;
1174                 SensitiveCond cond;
1175         } entry[] = {
1176                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1177                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1178                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1179                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1180                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1181                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1182
1183                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1184                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1185
1186                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1187
1188                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1189                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1190                 {"/Delete"                      , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1191                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1192
1193                 {"/Mark"                        , M_TARGET_EXIST},
1194                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1195                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1196                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1197                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1198                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1199                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1200                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1201                 {"/Color label"                 , M_TARGET_EXIST},
1202
1203                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1204                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1205                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1206
1207                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1208                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1209                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1210                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1211 #if 0
1212                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1213                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1214
1215                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1216                 {"/Select all"                  , M_TARGET_EXIST},
1217 #endif
1218                 {NULL, 0}
1219         };
1220
1221         main_window_set_menu_sensitive(summaryview->mainwin);
1222
1223         state = main_window_get_current_state(summaryview->mainwin);
1224
1225         for (i = 0; entry[i].entry != NULL; i++) {
1226                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1227                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1228         }
1229
1230
1231         summary_lock(summaryview);
1232         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1233         gtk_check_menu_item_set_active
1234                 (GTK_CHECK_MENU_ITEM(menuitem),
1235                  summaryview->messageview->mimeview->textview->show_all_headers);
1236         summary_unlock(summaryview);
1237 }
1238
1239 void summary_select_prev_unread(SummaryView *summaryview)
1240 {
1241         GtkCTreeNode *node;
1242         gboolean skip_cur = FALSE;
1243
1244         if (summaryview->displayed 
1245         &&  summaryview->selected == summaryview->displayed) {
1246                 debug_print("skipping current\n");
1247                 skip_cur = TRUE;
1248         }
1249
1250         node = summary_find_prev_flagged_msg
1251                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1252
1253         if (!node) {
1254                 AlertValue val = 0;
1255
1256                 switch (prefs_common.next_unread_msg_dialog) {
1257                         case NEXTUNREADMSGDIALOG_ALWAYS:
1258                                 val = alertpanel(_("No more unread messages"),
1259                                                  _("No unread message found. "
1260                                                    "Search from the end?"),
1261                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1262                                 break;
1263                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1264                                 val = G_ALERTDEFAULT;
1265                                 break;
1266                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1267                                 val = !G_ALERTDEFAULT;
1268                                 break;
1269                         default:
1270                                 debug_print(
1271                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1272                 }
1273                 if (val != G_ALERTDEFAULT) return;
1274                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1275                                                      MSG_UNREAD, FALSE);
1276         }
1277
1278         if (!node)
1279                 alertpanel_notice(_("No unread messages."));
1280         else
1281                 summary_select_node(summaryview, node, TRUE, FALSE);
1282 }
1283
1284 void summary_select_next_unread(SummaryView *summaryview)
1285 {
1286         GtkCTreeNode *node = summaryview->selected;
1287         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1288         gboolean skip_cur = FALSE;
1289         
1290         if (summaryview->displayed 
1291         &&  summaryview->selected == summaryview->displayed) {
1292                 debug_print("skipping cur\n");
1293                 skip_cur = TRUE;
1294         }
1295
1296
1297         node = summary_find_next_flagged_msg
1298                 (summaryview, node, MSG_UNREAD, skip_cur);
1299         
1300         if (node)
1301                 summary_select_node(summaryview, node, TRUE, FALSE);
1302         else {
1303                 node = summary_find_next_flagged_msg
1304                         (summaryview, NULL, MSG_UNREAD, FALSE);
1305                 if (node == NULL) {
1306                         AlertValue val = 0;
1307
1308                         switch (prefs_common.next_unread_msg_dialog) {
1309                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1310                                         val = alertpanel(_("No more unread messages"),
1311                                                          _("No unread message found. "
1312                                                            "Go to next folder?"),
1313                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1314                                         break;
1315                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1316                                         val = G_ALERTDEFAULT;
1317                                         break;
1318                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1319                                         val = G_ALERTOTHER;
1320                                         break;
1321                                 default:
1322                                         debug_print(
1323                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1324                         }
1325
1326                         if (val == G_ALERTDEFAULT) {
1327                                 g_signal_stop_emission_by_name(G_OBJECT(ctree),
1328                                                          "key_press_event");
1329                                 folderview_select_next_unread(summaryview->folderview);
1330                                 return;
1331                         } 
1332                         else
1333                                 return;
1334                 } else
1335                         summary_select_node(summaryview, node, TRUE, FALSE);
1336
1337         }
1338 }
1339
1340 void summary_select_prev_new(SummaryView *summaryview)
1341 {
1342         GtkCTreeNode *node;
1343
1344         node = summary_find_prev_flagged_msg
1345                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1346
1347         if (!node) {
1348                 AlertValue val;
1349
1350                 val = alertpanel(_("No more new messages"),
1351                                  _("No new message found. "
1352                                    "Search from the end?"),
1353                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1354                 if (val != G_ALERTDEFAULT) return;
1355                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1356                                                      MSG_NEW, FALSE);
1357         }
1358
1359         if (!node)
1360                 alertpanel_notice(_("No new messages."));
1361         else
1362                 summary_select_node(summaryview, node, TRUE, FALSE);
1363 }
1364
1365 void summary_select_next_new(SummaryView *summaryview)
1366 {
1367         GtkCTreeNode *node = summaryview->selected;
1368         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1369
1370         while ((node = summary_find_next_flagged_msg
1371                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1372                 AlertValue val;
1373
1374                 val = alertpanel(_("No more new messages"),
1375                                  _("No new message found. "
1376                                    "Go to next folder?"),
1377                                  GTK_STOCK_YES, _("Search again"),
1378                                  GTK_STOCK_NO);
1379                 if (val == G_ALERTDEFAULT) {
1380                         g_signal_stop_emission_by_name(G_OBJECT(ctree),"key_press_event");
1381                         folderview_select_next_unread(summaryview->folderview);
1382                         return;
1383                 } else if (val == G_ALERTALTERNATE)
1384                         node = NULL;
1385                 else
1386                         return;
1387         }
1388
1389         if (node)
1390                 summary_select_node(summaryview, node, TRUE, FALSE);
1391 }
1392
1393 void summary_select_prev_marked(SummaryView *summaryview)
1394 {
1395         GtkCTreeNode *node;
1396
1397         node = summary_find_prev_flagged_msg
1398                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1399
1400         if (!node) {
1401                 AlertValue val;
1402
1403                 val = alertpanel(_("No more marked messages"),
1404                                  _("No marked message found. "
1405                                    "Search from the end?"),
1406                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1407                 if (val != G_ALERTDEFAULT) return;
1408                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1409                                                      MSG_MARKED, TRUE);
1410         }
1411
1412         if (!node)
1413                 alertpanel_notice(_("No marked messages."));
1414         else
1415                 summary_select_node(summaryview, node, TRUE, FALSE);
1416 }
1417
1418 void summary_select_next_marked(SummaryView *summaryview)
1419 {
1420         GtkCTreeNode *node;
1421
1422         node = summary_find_next_flagged_msg
1423                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1424
1425         if (!node) {
1426                 AlertValue val;
1427
1428                 val = alertpanel(_("No more marked messages"),
1429                                  _("No marked message found. "
1430                                    "Search from the beginning?"),
1431                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1432                 if (val != G_ALERTDEFAULT) return;
1433                 node = summary_find_next_flagged_msg(summaryview, NULL,
1434                                                      MSG_MARKED, TRUE);
1435         }
1436
1437         if (!node)
1438                 alertpanel_notice(_("No marked messages."));
1439         else
1440                 summary_select_node(summaryview, node, TRUE, FALSE);
1441 }
1442
1443 void summary_select_prev_labeled(SummaryView *summaryview)
1444 {
1445         GtkCTreeNode *node;
1446
1447         node = summary_find_prev_flagged_msg
1448                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1449
1450         if (!node) {
1451                 AlertValue val;
1452
1453                 val = alertpanel(_("No more labeled messages"),
1454                                  _("No labeled message found. "
1455                                    "Search from the end?"),
1456                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1457                 if (val != G_ALERTDEFAULT) return;
1458                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1459                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1460         }
1461
1462         if (!node)
1463                 alertpanel_notice(_("No labeled messages."));
1464         else
1465                 summary_select_node(summaryview, node, TRUE, FALSE);
1466 }
1467
1468 void summary_select_next_labeled(SummaryView *summaryview)
1469 {
1470         GtkCTreeNode *node;
1471
1472         node = summary_find_next_flagged_msg
1473                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1474
1475         if (!node) {
1476                 AlertValue val;
1477
1478                 val = alertpanel(_("No more labeled messages"),
1479                                  _("No labeled message found. "
1480                                    "Search from the beginning?"),
1481                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1482                 if (val != G_ALERTDEFAULT) return;
1483                 node = summary_find_next_flagged_msg(summaryview, NULL,
1484                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1485         }
1486
1487         if (!node)
1488                 alertpanel_notice(_("No labeled messages."));
1489         else
1490                 summary_select_node(summaryview, node, TRUE, FALSE);
1491 }
1492
1493 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1494 {
1495         GtkCTreeNode *node;
1496
1497         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1498         summary_select_node(summaryview, node, FALSE, TRUE);
1499 }
1500
1501 /**
1502  * summary_select_node:
1503  * @summaryview: Summary view.
1504  * @node: Summary tree node.
1505  * @display_msg: TRUE to display the selected message.
1506  * @do_refresh: TRUE to refresh the widget.
1507  *
1508  * Select @node (bringing it into view by scrolling and expanding its
1509  * thread, if necessary) and unselect all others.  If @display_msg is
1510  * TRUE, display the corresponding message in the message view.
1511  * If @do_refresh is TRUE, the widget is refreshed.
1512  **/
1513 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1514                          gboolean display_msg, gboolean do_refresh)
1515 {
1516         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1517
1518         if (node) {
1519                 gtkut_ctree_expand_parent_all(ctree, node);
1520                 if (do_refresh) {
1521                         GTK_EVENTS_FLUSH();
1522                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1523                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1524                 }
1525                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1526                 if (display_msg && summaryview->displayed == node)
1527                         summaryview->displayed = NULL;
1528                 summaryview->display_msg = display_msg;
1529                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1530         }
1531 }
1532
1533 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1534 {
1535         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1536         MsgInfo *msginfo;
1537
1538         if (!node)
1539                 return 0;
1540         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1541         return msginfo->msgnum;
1542 }
1543
1544 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1545                                            GtkCTreeNode *current_node)
1546 {
1547         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1548         GtkCTreeNode *node;
1549         MsgInfo *msginfo;
1550
1551         if (current_node)
1552                 node = current_node;
1553         else
1554                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1555
1556         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1557                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1558                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1559         }
1560
1561         return node;
1562 }
1563
1564 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1565                                            GtkCTreeNode *current_node)
1566 {
1567         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1568         GtkCTreeNode *node;
1569         MsgInfo *msginfo;
1570
1571         if (current_node)
1572                 node = current_node;
1573         else
1574                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1575
1576         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1577                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1578                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1579         }
1580
1581         return node;
1582 }
1583
1584 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1585                                                    GtkCTreeNode *current_node,
1586                                                    MsgPermFlags flags,
1587                                                    gboolean start_from_prev)
1588 {
1589         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1590         GtkCTreeNode *node;
1591         MsgInfo *msginfo;
1592
1593         if (current_node) {
1594                 if (start_from_prev)
1595                         node = GTK_CTREE_NODE_PREV(current_node);
1596                 else
1597                         node = current_node;
1598         } else
1599                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1600
1601         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1602                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1603                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1604         }
1605
1606         return node;
1607 }
1608
1609 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1610                                                    GtkCTreeNode *current_node,
1611                                                    MsgPermFlags flags,
1612                                                    gboolean start_from_next)
1613 {
1614         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1615         GtkCTreeNode *node;
1616         MsgInfo *msginfo;
1617
1618         if (current_node) {
1619                 if (start_from_next)
1620                         node = gtkut_ctree_node_next(ctree, current_node);
1621                 else
1622                         node = current_node;
1623         } else
1624                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1625
1626         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1627                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1628                 /* Find msg with matching flags but ignore messages with
1629                    ignore flags, if searching for new or unread messages */
1630                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1631                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1632                         break;
1633         }
1634
1635         return node;
1636 }
1637
1638 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1639                                                 guint msgnum)
1640 {
1641         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1642         GtkCTreeNode *node;
1643         MsgInfo *msginfo;
1644
1645         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1646
1647         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1648                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1649                 if (msginfo && msginfo->msgnum == msgnum) break;
1650         }
1651
1652         return node;
1653 }
1654
1655 static guint attract_hash_func(gconstpointer key)
1656 {
1657         gchar *str;
1658         gchar *p;
1659         guint h;
1660
1661         Xstrdup_a(str, (const gchar *)key, return 0);
1662         trim_subject(str);
1663
1664         p = str;
1665         h = *p;
1666
1667         if (h) {
1668                 for (p += 1; *p != '\0'; p++)
1669                         h = (h << 5) - h + *p;
1670         }
1671
1672         return h;
1673 }
1674
1675 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1676 {
1677         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1678 }
1679
1680 void summary_attract_by_subject(SummaryView *summaryview)
1681 {
1682         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1683         GtkCList *clist = GTK_CLIST(ctree);
1684         GtkCTreeNode *src_node;
1685         GtkCTreeNode *dst_node, *sibling;
1686         GtkCTreeNode *tmp;
1687         MsgInfo *src_msginfo, *dst_msginfo;
1688         GHashTable *subject_table;
1689
1690         debug_print("Attracting messages by subject...");
1691         STATUSBAR_PUSH(summaryview->mainwin,
1692                        _("Attracting messages by subject..."));
1693
1694         main_window_cursor_wait(summaryview->mainwin);
1695         gtk_clist_freeze(clist);
1696
1697         subject_table = g_hash_table_new(attract_hash_func,
1698                                          attract_compare_func);
1699
1700         for (src_node = GTK_CTREE_NODE(clist->row_list);
1701              src_node != NULL;
1702              src_node = tmp) {
1703                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1704                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1705                 if (!src_msginfo) continue;
1706                 if (!src_msginfo->subject) continue;
1707
1708                 /* find attracting node */
1709                 dst_node = g_hash_table_lookup(subject_table,
1710                                                src_msginfo->subject);
1711
1712                 if (dst_node) {
1713                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1714
1715                         /* if the time difference is more than 20 days,
1716                            don't attract */
1717                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1718                             > 60 * 60 * 24 * 20)
1719                                 continue;
1720
1721                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1722                         if (src_node != sibling)
1723                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1724                 }
1725
1726                 g_hash_table_insert(subject_table,
1727                                     src_msginfo->subject, src_node);
1728         }
1729
1730         g_hash_table_destroy(subject_table);
1731
1732         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1733
1734         gtk_clist_thaw(clist);
1735
1736         debug_print("done.\n");
1737         STATUSBAR_POP(summaryview->mainwin);
1738
1739         main_window_cursor_normal(summaryview->mainwin);
1740 }
1741
1742 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1743                                       gpointer data)
1744 {
1745         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1746
1747         if (msginfo)
1748                 procmsg_msginfo_free(msginfo);
1749 }
1750
1751 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1752                                    gpointer data)
1753 {
1754         SummaryView *summaryview = data;
1755         MsgInfo *msginfo;
1756
1757         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1758
1759         if (msginfo->folder && msginfo->folder->folder &&
1760             msginfo->folder->folder->klass->type == F_NEWS)
1761                 news_flag_crosspost(msginfo);
1762
1763         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1764         && procmsg_msg_has_marked_parent(msginfo))
1765                 summaryview->unreadmarked++;
1766
1767         if (MSG_IS_DELETED(msginfo->flags))
1768                 summaryview->deleted++;
1769
1770         summaryview->total_size += msginfo->size;
1771
1772         summary_set_row_marks(summaryview, node);
1773 }
1774
1775 static void summary_update_status(SummaryView *summaryview)
1776 {
1777         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1778         GtkCTreeNode *node;
1779         MsgInfo *msginfo;
1780
1781         summaryview->total_size =
1782         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1783
1784         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1785              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1786                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1787
1788                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1789                 && procmsg_msg_has_marked_parent(msginfo))
1790                         summaryview->unreadmarked++;
1791                 if (MSG_IS_DELETED(msginfo->flags))
1792                         summaryview->deleted++;
1793                 if (MSG_IS_MOVE(msginfo->flags))
1794                         summaryview->moved++;
1795                 if (MSG_IS_COPY(msginfo->flags))
1796                         summaryview->copied++;
1797                 summaryview->total_size += msginfo->size;
1798         }
1799 }
1800
1801 static void summary_status_show(SummaryView *summaryview)
1802 {
1803         gchar *str;
1804         gchar *del, *mv, *cp;
1805         gchar *sel;
1806         gchar *spc;
1807         gchar *itstr;
1808         GList *rowlist, *cur;
1809         guint n_selected = 0;
1810         off_t sel_size = 0;
1811         MsgInfo *msginfo;
1812         gchar *name;
1813
1814         if (!summaryview->folder_item) {
1815                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1816                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1817                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1818                 return;
1819         }
1820
1821         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1822         for (cur = rowlist; cur != NULL; cur = cur->next) {
1823                 msginfo = gtk_ctree_node_get_row_data
1824                         (GTK_CTREE(summaryview->ctree),
1825                          GTK_CTREE_NODE(cur->data));
1826                 if (!msginfo)
1827                         g_warning("summary_status_show(): msginfo == NULL\n");
1828                 else {
1829                         sel_size += msginfo->size;
1830                         n_selected++;
1831                 }
1832         }
1833
1834         name = folder_item_get_name(summaryview->folder_item);
1835         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1836         g_free(name);
1837
1838         if (summaryview->deleted)
1839                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1840         else
1841                 del = g_strdup("");
1842         if (summaryview->moved)
1843                 mv = g_strdup_printf(_("%s%d moved"),
1844                                      summaryview->deleted ? _(", ") : "",
1845                                      summaryview->moved);
1846         else
1847                 mv = g_strdup("");
1848         if (summaryview->copied)
1849                 cp = g_strdup_printf(_("%s%d copied"),
1850                                      summaryview->deleted ||
1851                                      summaryview->moved ? _(", ") : "",
1852                                      summaryview->copied);
1853         else
1854                 cp = g_strdup("");
1855
1856         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1857                 spc = "    ";
1858         else
1859                 spc = "";
1860
1861         if (n_selected) {
1862                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1863                 if (n_selected == 1)
1864                         itstr = g_strdup(_(" item selected"));
1865                 else
1866                         itstr = g_strdup(_(" items selected"));
1867         } else {
1868                 sel = g_strdup("");
1869                 itstr = g_strdup("");
1870         }
1871                 
1872         str = g_strconcat(n_selected ? itos(n_selected) : "",
1873                                         itstr, sel, spc, del, mv, cp, NULL);
1874         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1875         g_free(str);
1876         g_free(sel);
1877         g_free(del);
1878         g_free(mv);
1879         g_free(cp);
1880         g_free(itstr);
1881
1882         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1883
1884                                       summaryview->folder_item->new_msgs,
1885                                       summaryview->folder_item->unread_msgs,
1886                                       summaryview->folder_item->total_msgs,
1887                                       to_human_readable(summaryview->total_size));
1888         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1889         g_free(str);
1890 }
1891
1892 static void summary_set_column_titles(SummaryView *summaryview)
1893 {
1894         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1895         GtkWidget *hbox;
1896         GtkWidget *label;
1897         GtkWidget *arrow;
1898         gint pos;
1899         const gchar *title;
1900         SummaryColumnType type;
1901         gboolean single_char;
1902         GtkJustification justify;
1903
1904         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1905                 SORT_BY_MARK,
1906                 SORT_BY_STATUS,
1907                 SORT_BY_MIME,
1908                 SORT_BY_SUBJECT,
1909                 SORT_BY_FROM,
1910                 SORT_BY_DATE,
1911                 SORT_BY_SIZE,
1912                 SORT_BY_NUMBER,
1913                 SORT_BY_SCORE,
1914                 SORT_BY_LOCKED
1915         };
1916
1917         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1918                 type = summaryview->col_state[pos].type;
1919
1920                 /* CLAWS: mime and unread are single char headers */
1921                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1922                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1923                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1924
1925                 switch (type) {
1926                 case S_COL_SUBJECT:
1927                 case S_COL_FROM:
1928                 case S_COL_DATE:
1929                 case S_COL_NUMBER:
1930                         if (prefs_common.trans_hdr)
1931                                 title = gettext(col_label[type]);
1932                         else
1933                                 title = col_label[type];
1934                         break;
1935                 /* CLAWS: dummies for mark and locked headers */        
1936                 case S_COL_MARK:        
1937                 case S_COL_LOCKED:
1938                         title = "";
1939                         break;
1940                 default:
1941                         title = gettext(col_label[type]);
1942                 }
1943
1944                 if (type == S_COL_MIME) {
1945                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
1946                         gtk_widget_show(label);
1947                         gtk_clist_set_column_widget(clist, pos, label);
1948                         continue;
1949                 }
1950                 if (single_char) {
1951                         gtk_clist_set_column_title(clist, pos, title);
1952                         continue;
1953                 }
1954
1955                 /* CLAWS: changed so that locked and mark headers
1956                  * show a pixmap instead of single character */
1957                 hbox  = gtk_hbox_new(FALSE, 4);
1958                 
1959                 if (type == S_COL_LOCKED)
1960                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1961                 else if (type == S_COL_MARK) 
1962                         label = gtk_pixmap_new(markxpm, markxpmmask);
1963                 else 
1964                         label = gtk_label_new(title);
1965                 
1966                 if (justify == GTK_JUSTIFY_RIGHT)
1967                         gtk_box_pack_end(GTK_BOX(hbox), label,
1968                                          FALSE, FALSE, 0);
1969                 else
1970                         gtk_box_pack_start(GTK_BOX(hbox), label,
1971                                            FALSE, FALSE, 0);
1972
1973                 if (summaryview->sort_key == sort_by[type]) {
1974                         arrow = gtk_arrow_new
1975                                 (summaryview->sort_type == SORT_ASCENDING
1976                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1977                                  GTK_SHADOW_IN);
1978                         if (justify == GTK_JUSTIFY_RIGHT)
1979                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1980                                                    FALSE, FALSE, 0);
1981                         else
1982                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1983                                                  FALSE, FALSE, 0);
1984                 }
1985
1986                 gtk_widget_show_all(hbox);
1987                 gtk_clist_set_column_widget(clist, pos, hbox);
1988         }
1989 }
1990
1991 void summary_sort(SummaryView *summaryview,
1992                   FolderSortKey sort_key, FolderSortType sort_type)
1993 {
1994         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1995         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1996         GtkCListCompareFunc cmp_func = NULL;
1997
1998         switch (sort_key) {
1999         case SORT_BY_MARK:
2000                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2001                 break;
2002         case SORT_BY_STATUS:
2003                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2004                 break;
2005         case SORT_BY_MIME:
2006                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2007                 break;
2008         case SORT_BY_NUMBER:
2009                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2010                 break;
2011         case SORT_BY_SIZE:
2012                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2013                 break;
2014         case SORT_BY_DATE:
2015                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2016                 break;
2017         case SORT_BY_FROM:
2018                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2019                 break;
2020         case SORT_BY_SUBJECT:
2021                 if (summaryview->simplify_subject_preg)
2022                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2023                 else
2024                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2025                 break;
2026         case SORT_BY_SCORE:
2027                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2028                 break;
2029         case SORT_BY_LABEL:
2030                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2031                 break;
2032         case SORT_BY_TO:
2033                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2034                 break;
2035         case SORT_BY_LOCKED:
2036                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2037                 break;
2038         case SORT_BY_NONE:
2039                 break;
2040         default:
2041                 return;
2042         }
2043
2044         summaryview->sort_key = sort_key;
2045         summaryview->sort_type = sort_type;
2046
2047         summary_set_column_titles(summaryview);
2048         summary_set_menu_sensitive(summaryview);
2049
2050         /* allow fallback to don't sort */
2051         if (summaryview->sort_key == SORT_BY_NONE)
2052                 return;
2053
2054         if(cmp_func != NULL) {
2055                 debug_print("Sorting summary...");
2056                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2057
2058                 main_window_cursor_wait(summaryview->mainwin);
2059
2060                 gtk_clist_set_compare_func(clist, cmp_func);
2061
2062                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2063
2064                 gtk_sctree_sort_recursive(ctree, NULL);
2065
2066                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2067
2068                 main_window_cursor_normal(summaryview->mainwin);
2069
2070                 if (summaryview->threaded)
2071                         summary_thread_init(summaryview);
2072
2073                 debug_print("done.\n");
2074                 STATUSBAR_POP(summaryview->mainwin);
2075         }
2076 }
2077
2078 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2079                                    GtkCTreeNode *cnode, gpointer data)
2080 {
2081         SummaryView *summaryview = (SummaryView *)data;
2082         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2083         gchar *text[N_SUMMARY_COLS];
2084         gint *col_pos = summaryview->col_pos;
2085         const gchar *msgid = msginfo->msgid;
2086         GHashTable *msgid_table = summaryview->msgid_table;
2087
2088         summary_set_header(summaryview, text, msginfo);
2089
2090         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2091                                 NULL, NULL, NULL, NULL, FALSE,
2092                                 gnode->parent->parent ? TRUE : FALSE);
2093 #define SET_TEXT(col) \
2094         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2095                                 text[col_pos[col]])
2096
2097         SET_TEXT(S_COL_NUMBER);
2098         SET_TEXT(S_COL_SCORE);
2099         SET_TEXT(S_COL_SIZE);
2100         SET_TEXT(S_COL_DATE);
2101         SET_TEXT(S_COL_FROM);
2102         SET_TEXT(S_COL_SUBJECT);
2103
2104 #undef SET_TEXT
2105
2106         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2107         summary_set_marks_func(ctree, cnode, summaryview);
2108
2109         if (msgid && msgid[0] != '\0')
2110                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2111
2112         return TRUE;
2113 }
2114
2115 static void summary_set_ctree_from_list(SummaryView *summaryview,
2116                                         GSList *mlist)
2117 {
2118         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2119         MsgInfo *msginfo;
2120                 GtkCTreeNode *node = NULL;
2121         GHashTable *msgid_table;
2122         GHashTable *subject_table;
2123         GSList * cur;
2124         
2125         if (!mlist) return;
2126
2127         debug_print("\tSetting summary from message data...");
2128         STATUSBAR_PUSH(summaryview->mainwin,
2129                        _("Setting summary from message data..."));
2130         gdk_flush();
2131
2132         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2133         summaryview->msgid_table = msgid_table;
2134         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2135         summaryview->subject_table = subject_table;
2136
2137         if (prefs_common.use_addr_book)
2138                 start_address_completion();
2139         
2140         for (cur = mlist ; cur != NULL; cur = cur->next) {
2141                 msginfo = (MsgInfo *)cur->data;
2142                 msginfo->threadscore = msginfo->score;
2143         }
2144
2145         if (summaryview->threaded) {
2146                 GNode *root, *gnode;
2147
2148                 root = procmsg_get_thread_tree(mlist);
2149
2150                 for (gnode = root->children; gnode != NULL;
2151                      gnode = gnode->next) {
2152                         node = gtk_ctree_insert_gnode
2153                                 (ctree, NULL, node, gnode,
2154                                  summary_insert_gnode_func, summaryview);
2155                 }
2156
2157                 g_node_destroy(root);
2158         } else {
2159                 gchar *text[N_SUMMARY_COLS];
2160                 cur = mlist;
2161                 for (; mlist != NULL; mlist = mlist->next) {
2162                         msginfo = (MsgInfo *)mlist->data;
2163
2164                         summary_set_header(summaryview, text, msginfo);
2165
2166                         node = gtk_ctree_insert_node
2167                                 (ctree, NULL, node, text, 2,
2168                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2169                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2170                         summary_set_marks_func(ctree, node, summaryview);
2171
2172                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2173                                 g_hash_table_insert(msgid_table,
2174                                                     msginfo->msgid, node);
2175
2176                         subject_table_insert(subject_table,
2177                                              msginfo->subject,
2178                                              node);
2179                 }
2180                 mlist = cur;
2181         }
2182
2183         if (prefs_common.enable_hscrollbar &&
2184             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2185                 gint optimal_width;
2186
2187                 optimal_width = gtk_clist_optimal_column_width
2188                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2189                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2190                                            summaryview->col_pos[S_COL_SUBJECT],
2191                                            optimal_width);
2192         }
2193
2194         if (prefs_common.use_addr_book)
2195                 end_address_completion();
2196
2197         debug_print("done.\n");
2198         STATUSBAR_POP(summaryview->mainwin);
2199         if (debug_get_mode()) {
2200                 debug_print("\tmsgid hash table size = %d\n",
2201                             g_hash_table_size(msgid_table));
2202                 debug_print("\tsubject hash table size = %d\n",
2203                             g_hash_table_size(subject_table));
2204         }
2205
2206         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2207 }
2208
2209 static gchar *summary_complete_address(const gchar *addr)
2210 {
2211         gint count;
2212         gchar *res, *tmp, *email_addr;
2213
2214         Xstrdup_a(email_addr, addr, return NULL);
2215         extract_address(email_addr);
2216         g_return_val_if_fail(*email_addr, NULL);
2217
2218         /*
2219          * completion stuff must be already initialized
2220          */
2221         res = NULL;
2222         if (1 < (count = complete_address(email_addr))) {
2223                 tmp = get_complete_address(1);
2224 /*      tmp = addressbook_lookup_name( email_addr );
2225         if( tmp ) { */
2226                 res = procheader_get_fromname(tmp);
2227                 g_free(tmp);
2228         }
2229
2230         return res;
2231 }
2232
2233 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2234                                MsgInfo *msginfo)
2235 {
2236         static gchar date_modified[80];
2237         static gchar *to = NULL;
2238         static gchar col_score[11];
2239         static gchar buf[BUFFSIZE];
2240         gint *col_pos = summaryview->col_pos;
2241         FolderType ftype = F_UNKNOWN;
2242
2243         text[col_pos[S_COL_MARK]]   = NULL;
2244         text[col_pos[S_COL_STATUS]] = NULL;
2245         text[col_pos[S_COL_MIME]]   = NULL;
2246         text[col_pos[S_COL_LOCKED]] = NULL;
2247         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2248         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2249         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2250
2251         if (msginfo->date_t) {
2252                 procheader_date_get_localtime(date_modified,
2253                                               sizeof(date_modified),
2254                                               msginfo->date_t);
2255                 text[col_pos[S_COL_DATE]] = date_modified;
2256         } else if (msginfo->date)
2257                 text[col_pos[S_COL_DATE]] = msginfo->date;
2258         else
2259                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2260
2261         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2262                 _("(No From)");
2263         
2264         if (msginfo->folder && msginfo->folder->folder)
2265                 ftype = msginfo->folder->folder->klass->type; 
2266                 
2267         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2268                 gchar *addr = NULL;
2269
2270                 Xstrdup_a(addr, msginfo->from, return);
2271                 extract_address(addr);
2272                 if (prefs_common.use_addr_book) {
2273                         if (account_find_from_address(addr)) {
2274                                 addr = summary_complete_address(msginfo->to);
2275                                 g_free(to);
2276                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2277                                 text[col_pos[S_COL_FROM]] = to;
2278                                 g_free(addr);
2279                         }
2280                 } else {
2281                         if (account_find_from_address(addr)) {
2282                                 g_free(to);
2283                                 to = g_strconcat("-->", msginfo->to, NULL);
2284                                 text[col_pos[S_COL_FROM]] = to;
2285                         }
2286                 }
2287         }
2288
2289         /*
2290          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2291          * checking whether the above block (which handles the special case of
2292          * the --> in sent boxes) was executed.
2293          */
2294         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2295                 gchar *from = summary_complete_address(msginfo->from);
2296                 if (from) {
2297                         g_free(to);
2298                         to = from;
2299                         text[col_pos[S_COL_FROM]] = to;
2300                 }                       
2301         }
2302
2303         if (summaryview->simplify_subject_preg != NULL)
2304                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2305                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2306                                         summaryview->simplify_subject_preg) : 
2307                         _("(No Subject)");
2308         else 
2309                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2310                         _("(No Subject)");
2311 }
2312
2313 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2314 {
2315         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2316 }
2317
2318 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2319                                       GtkCTreeNode *row)
2320 {
2321         g_return_if_fail(summaryview != NULL);
2322         g_return_if_fail(msginfo != NULL);
2323         g_return_if_fail(row != NULL);
2324
2325         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2326         && procmsg_msg_has_marked_parent(msginfo))
2327                 summaryview->unreadmarked--;
2328         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2329                 procmsg_msginfo_unset_flags
2330                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2331                 summary_set_row_marks(summaryview, row);
2332                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2333                 summary_status_show(summaryview);
2334         }
2335 }
2336
2337 typedef struct  {
2338         MsgInfo *msginfo;
2339         SummaryView *summaryview;
2340         GtkCTreeNode *row;
2341 } MarkAsReadData;
2342
2343 static int msginfo_mark_as_read_timeout(void *data)
2344 {
2345         MarkAsReadData *mdata = (MarkAsReadData *)data;
2346         if (!mdata)
2347                 return FALSE;
2348         
2349         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2350                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2351                                      mdata->row); 
2352
2353         g_free(mdata);
2354
2355         return FALSE;   
2356 }
2357
2358 static void summary_display_msg_full(SummaryView *summaryview,
2359                                      GtkCTreeNode *row,
2360                                      gboolean new_window, gboolean all_headers)
2361 {
2362         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2363         MsgInfo *msginfo;
2364         gint val;
2365
2366         if (!new_window) {
2367                 if (summaryview->displayed == row)
2368                         return;
2369                 else
2370                         summaryview->messageview->filtered = FALSE;
2371         }                       
2372         
2373         g_return_if_fail(row != NULL);
2374
2375         if (summary_is_locked(summaryview)) return;
2376         summary_lock(summaryview);
2377
2378         STATUSBAR_POP(summaryview->mainwin);
2379         GTK_EVENTS_FLUSH();
2380
2381         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2382
2383         if (new_window) {
2384                 MessageView *msgview;
2385
2386                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2387                 val = messageview_show(msgview, msginfo, all_headers);
2388         } else {
2389                 MessageView *msgview;
2390
2391                 msgview = summaryview->messageview;
2392
2393                 summaryview->displayed = row;
2394                 if (!messageview_is_visible(msgview))
2395                         main_window_toggle_message_view(summaryview->mainwin);
2396                 val = messageview_show(msgview, msginfo, all_headers);
2397                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2398                         gtk_widget_grab_focus(summaryview->ctree);
2399                 GTK_EVENTS_FLUSH();
2400                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2401         }
2402
2403         if (val == 0) {
2404                 if (prefs_common.mark_as_read_delay) {
2405                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2406                         data->summaryview = summaryview;
2407                         data->msginfo = msginfo;
2408                         data->row = row;
2409                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2410                                 msginfo_mark_as_read_timeout, data);
2411                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2412                         msginfo_mark_as_read(summaryview, msginfo, row);
2413                 }
2414         }
2415
2416         summary_set_menu_sensitive(summaryview);
2417         toolbar_main_set_sensitive(summaryview->mainwin);
2418         messageview_set_menu_sensitive(summaryview->messageview);
2419
2420         summary_unlock(summaryview);
2421 }
2422
2423 void summary_display_msg_selected(SummaryView *summaryview,
2424                                   gboolean all_headers)
2425 {
2426         if (summary_is_locked(summaryview)) return;
2427         summaryview->displayed = NULL;
2428         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2429                                  all_headers);
2430 }
2431
2432 void summary_redisplay_msg(SummaryView *summaryview)
2433 {
2434         GtkCTreeNode *node;
2435
2436         if (summaryview->displayed) {
2437                 node = summaryview->displayed;
2438                 summaryview->displayed = NULL;
2439                 summary_display_msg(summaryview, node);
2440         }
2441 }
2442
2443 void summary_open_msg(SummaryView *summaryview)
2444 {
2445         if (!summaryview->selected) return;
2446         
2447         /* CLAWS: if separate message view, don't open a new window
2448          * but rather use the current separated message view */
2449         summary_display_msg_full(summaryview, summaryview->selected,
2450                                  prefs_common.sep_msg ? FALSE : TRUE, 
2451                                  FALSE);
2452 }
2453
2454 void summary_view_source(SummaryView * summaryview)
2455 {
2456         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2457         MsgInfo *msginfo;
2458         SourceWindow *srcwin;
2459
2460         if (!summaryview->selected) return;
2461
2462         srcwin = source_window_create();
2463         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2464         source_window_show_msg(srcwin, msginfo);
2465         source_window_show(srcwin);
2466 }
2467
2468 void summary_reedit(SummaryView *summaryview)
2469 {
2470         MsgInfo *msginfo;
2471
2472         if (!summaryview->selected) return;
2473         if (!summaryview->folder_item) return;
2474         if (summaryview->folder_item->stype != F_OUTBOX &&
2475             summaryview->folder_item->stype != F_DRAFT  &&
2476             summaryview->folder_item->stype != F_QUEUE) return;
2477
2478         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2479                                               summaryview->selected);
2480         if (!msginfo) return;
2481
2482         compose_reedit(msginfo);
2483 }
2484
2485 void summary_step(SummaryView *summaryview, GtkScrollType type)
2486 {
2487         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2488         GtkCTreeNode *node;
2489
2490         if (summary_is_locked(summaryview)) return;
2491
2492         if (type == GTK_SCROLL_STEP_FORWARD) {
2493                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2494                 if (node)
2495                         gtkut_ctree_expand_parent_all(ctree, node);
2496                 else
2497                         return;
2498         } else {
2499                 if (summaryview->selected) {
2500                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2501                         if (!node) return;
2502                 }
2503         }
2504
2505         if (messageview_is_visible(summaryview->messageview))
2506                 summaryview->display_msg = TRUE;
2507
2508         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
2509                               type, 0.0);
2510
2511         if (GTK_CLIST(ctree)->selection)
2512                 gtk_sctree_set_anchor_row
2513                         (GTK_SCTREE(ctree),
2514                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2515
2516 }
2517
2518 void summary_toggle_view(SummaryView *summaryview)
2519 {
2520         if (!messageview_is_visible(summaryview->messageview) &&
2521             summaryview->selected)
2522                 summary_display_msg(summaryview,
2523                                     summaryview->selected);
2524         else
2525                 main_window_toggle_message_view(summaryview->mainwin);
2526 }
2527
2528 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2529                                                 GtkCTreeNode *node)
2530 {
2531         MsgInfo *msginfo;
2532
2533         if (node) {
2534                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2535                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2536                         return TRUE;
2537                 node = GTK_CTREE_ROW(node)->children;
2538         } else
2539                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2540
2541         while (node) {
2542                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2543                         return TRUE;
2544                 node = GTK_CTREE_ROW(node)->sibling;
2545         }
2546
2547         return FALSE;
2548 }
2549
2550 static gboolean summary_have_unread_children(SummaryView *summaryview,
2551                                              GtkCTreeNode *node)
2552 {
2553         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2554
2555         if (!node) return FALSE;
2556
2557         node = GTK_CTREE_ROW(node)->children;
2558
2559         while (node) {
2560                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2561                         return TRUE;
2562                 node = GTK_CTREE_ROW(node)->sibling;
2563         }
2564
2565         return FALSE;
2566 }
2567
2568 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2569 {
2570         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2571         GtkStyle *style = NULL;
2572         MsgInfo *msginfo;
2573         MsgFlags flags;
2574         gint *col_pos = summaryview->col_pos;
2575
2576         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2577         if (!msginfo) return;
2578
2579         flags = msginfo->flags;
2580
2581         gtk_ctree_node_set_foreground(ctree, row, NULL);
2582
2583         /* set new/unread column */
2584         if (MSG_IS_IGNORE_THREAD(flags)) {
2585                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2586                                           ignorethreadxpm, ignorethreadxpmmask);
2587         } else if (MSG_IS_NEW(flags)) {
2588                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2589                                           newxpm, newxpmmask);
2590         } else if (MSG_IS_UNREAD(flags)) {
2591                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2592                                           unreadxpm, unreadxpmmask);
2593         } else if (MSG_IS_REPLIED(flags)) {
2594                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2595                                           repliedxpm, repliedxpmmask);
2596         } else if (MSG_IS_FORWARDED(flags)) {
2597                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2598                                           forwardedxpm, forwardedxpmmask);
2599         } else {
2600                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2601                                         NULL);
2602         }
2603
2604         if (prefs_common.bold_unread &&
2605             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2606              (!GTK_CTREE_ROW(row)->expanded &&
2607               GTK_CTREE_ROW(row)->children &&
2608               summary_have_unread_children(summaryview, row))))
2609                 style = bold_style;
2610
2611         /* set mark column */
2612         if (MSG_IS_DELETED(flags)) {
2613                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2614                                           deletedxpm, deletedxpmmask);
2615                 if (style)
2616                         style = bold_deleted_style;
2617                 else {
2618                         style = small_deleted_style;
2619                 }
2620                         gtk_ctree_node_set_foreground
2621                                 (ctree, row, &summaryview->color_dim);
2622         } else if (MSG_IS_MARKED(flags)) {
2623                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2624                                           markxpm, markxpmmask);
2625         } else if (MSG_IS_MOVE(flags)) {
2626                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2627                 if (style)
2628                         style = bold_marked_style;
2629                 else {
2630                         style = small_marked_style;
2631                 }
2632                         gtk_ctree_node_set_foreground
2633                                 (ctree, row, &summaryview->color_marked);
2634         } else if (MSG_IS_COPY(flags)) {
2635                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2636                 if (style)
2637                         style = bold_marked_style;
2638                 else {
2639                         style = small_marked_style;
2640                 }
2641                         gtk_ctree_node_set_foreground
2642                                 (ctree, row, &summaryview->color_marked);
2643 #if 0
2644         } else if ((global_scoring ||
2645                   summaryview->folder_item->prefs->scoring) &&
2646                  (msginfo->score >= summaryview->important_score) &&
2647                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2648                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2649                 gtk_ctree_node_set_foreground(ctree, row,
2650                                               &summaryview->color_important);
2651 #endif
2652         } else {
2653                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2654         }
2655
2656         if (MSG_IS_LOCKED(flags)) {
2657                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2658                                           lockedxpm, lockedxpmmask);
2659         }
2660         else {
2661                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2662         }
2663
2664         if (MSG_IS_SIGNED(flags)) {
2665                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2666                                           gpgsignedxpm, gpgsignedxpmmask);
2667         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2668                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2669                                           clipkeyxpm, clipkeyxpmmask);
2670         } else if (MSG_IS_ENCRYPTED(flags)) {
2671                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2672                                           keyxpm, keyxpmmask);
2673         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2674                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2675                                           clipxpm, clipxpmmask);
2676         } else {
2677                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2678         }
2679         if (!style)
2680                 style = small_style;
2681
2682         gtk_ctree_node_set_row_style(ctree, row, style);
2683
2684         if (MSG_GET_COLORLABEL(flags))
2685                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2686 }
2687
2688 void summary_set_marks_selected(SummaryView *summaryview)
2689 {
2690         GList *cur;
2691
2692         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2693              cur = cur->next)
2694                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2695 }
2696
2697 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2698 {
2699         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2700         MsgInfo *msginfo;
2701
2702         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2703         if (MSG_IS_DELETED(msginfo->flags))
2704                 summaryview->deleted--;
2705         if (MSG_IS_MOVE(msginfo->flags))
2706                 summaryview->moved--;
2707         if (MSG_IS_COPY(msginfo->flags))
2708                 summaryview->copied--;
2709         procmsg_update_unread_children (msginfo, TRUE);
2710
2711         procmsg_msginfo_set_to_folder(msginfo, NULL);
2712         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2713         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2714         summary_set_row_marks(summaryview, row);
2715         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2716 }
2717
2718 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2719 {
2720         gboolean changed = FALSE;
2721         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2722         MsgInfo *msginfo;
2723
2724         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2725         if (MSG_IS_DELETED(msginfo->flags))
2726                 summaryview->deleted--;
2727         if (MSG_IS_MOVE(msginfo->flags)) {
2728                 summaryview->moved--;
2729                 changed = TRUE;
2730         }
2731         if (MSG_IS_COPY(msginfo->flags)) {
2732                 summaryview->copied--;
2733                 changed = TRUE;
2734         }
2735         procmsg_msginfo_set_to_folder(msginfo, NULL);
2736         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2737         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2738         summary_set_row_marks(summaryview, row);
2739         debug_print("Message %d is locked\n", msginfo->msgnum);
2740 }
2741
2742 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2743 {
2744         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2745         MsgInfo *msginfo;
2746
2747         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2748         if (!MSG_IS_LOCKED(msginfo->flags))
2749                 return;
2750         procmsg_msginfo_set_to_folder(msginfo, NULL);
2751         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2752         summary_set_row_marks(summaryview, row);
2753         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2754 }
2755
2756 void summary_mark(SummaryView *summaryview)
2757 {
2758         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2759         GList *cur;
2760
2761         folder_item_update_freeze();
2762         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2763         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2764                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2765         folder_item_update_thaw();
2766         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2767
2768         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2769         summary_status_show(summaryview);
2770 }
2771
2772 static void summary_mark_row_as_read(SummaryView *summaryview,
2773                                      GtkCTreeNode *row)
2774 {
2775         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2776         MsgInfo *msginfo;
2777
2778         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2779
2780         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2781                 return;
2782
2783         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2784         && procmsg_msg_has_marked_parent(msginfo))
2785                 summaryview->unreadmarked--;
2786
2787         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2788         summary_set_row_marks(summaryview, row);
2789         debug_print("Message %d is marked as read\n",
2790                 msginfo->msgnum);
2791 }
2792
2793 void summary_mark_as_read(SummaryView *summaryview)
2794 {
2795         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2796         GList *cur;
2797
2798         folder_item_update_freeze();
2799         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2800         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2801                 summary_mark_row_as_read(summaryview,
2802                                          GTK_CTREE_NODE(cur->data));
2803         folder_item_update_thaw();
2804         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2805
2806         summary_status_show(summaryview);
2807 }
2808
2809 void summary_msgs_lock(SummaryView *summaryview)
2810 {
2811         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2812         GList *cur;
2813
2814         folder_item_update_freeze();
2815         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2816         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2817                 summary_lock_row(summaryview,
2818                                          GTK_CTREE_NODE(cur->data));
2819         folder_item_update_thaw();
2820         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2821         
2822         summary_status_show(summaryview);
2823 }
2824
2825 void summary_msgs_unlock(SummaryView *summaryview)
2826 {
2827         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2828         GList *cur;
2829
2830         folder_item_update_freeze();
2831         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2832         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2833                 summary_unlock_row(summaryview,
2834                                    GTK_CTREE_NODE(cur->data));
2835         folder_item_update_thaw();
2836         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2837         
2838         summary_status_show(summaryview);
2839 }
2840
2841 void summary_mark_all_read(SummaryView *summaryview)
2842 {
2843         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2844         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2845         GtkCTreeNode *node;
2846
2847         gtk_clist_freeze(clist);
2848         folder_item_update_freeze();
2849         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2850              node = gtkut_ctree_node_next(ctree, node))
2851                 summary_mark_row_as_read(summaryview, node);
2852         folder_item_update_thaw();
2853         gtk_clist_thaw(clist);
2854
2855         summary_status_show(summaryview);
2856 }
2857
2858 static void summary_mark_row_as_unread(SummaryView *summaryview,
2859                                        GtkCTreeNode *row)
2860 {
2861         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2862         MsgInfo *msginfo;
2863
2864         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2865         if (MSG_IS_DELETED(msginfo->flags)) {
2866                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2867                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2868                 summaryview->deleted--;
2869         }
2870
2871         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2872         && procmsg_msg_has_marked_parent(msginfo))
2873                 summaryview->unreadmarked++;
2874
2875         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2876         debug_print("Message %d is marked as unread\n",
2877                 msginfo->msgnum);
2878
2879         summary_set_row_marks(summaryview, row);
2880 }
2881
2882 void summary_mark_as_unread(SummaryView *summaryview)
2883 {
2884         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2885         GList *cur;
2886
2887         folder_item_update_freeze();
2888         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2889         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2890                 summary_mark_row_as_unread(summaryview,
2891                                            GTK_CTREE_NODE(cur->data));
2892         folder_item_update_thaw();
2893         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2894         
2895         summary_status_show(summaryview);
2896 }
2897
2898 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2899 {
2900         GList * cur;
2901         gboolean found;
2902
2903         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2904
2905         case F_NEWS:
2906
2907                 /*
2908                   security : checks if one the accounts correspond to
2909                   the author of the post
2910                 */
2911
2912                 found = FALSE;
2913                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2914                         PrefsAccount * account;
2915                         gchar * from_name;
2916                         
2917                         account = cur->data;
2918                         if (account->name && *account->name)
2919                                 from_name =
2920                                         g_strdup_printf("%s <%s>",
2921                                                         account->name,
2922                                                         account->address);
2923                         else
2924                                 from_name =
2925                                         g_strdup_printf("%s",
2926                                                         account->address);
2927                         
2928                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
2929                                 g_free(from_name);
2930                                 found = TRUE;
2931                                 break;
2932                         }
2933                         g_free(from_name);
2934                 }
2935
2936                 if (!found) {
2937                         alertpanel_error(_("You're not the author of the article\n"));
2938                 }
2939                 
2940                 return found;
2941
2942         default:
2943                 return TRUE;
2944         }
2945 }
2946
2947 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2948 {
2949         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2950         MsgInfo *msginfo;
2951
2952         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2953
2954         if (!check_permission(summaryview, msginfo))
2955                 return;
2956
2957         if (MSG_IS_LOCKED(msginfo->flags)) return;
2958
2959         if (MSG_IS_DELETED(msginfo->flags)) return;
2960
2961         if (MSG_IS_MOVE(msginfo->flags))
2962                 summaryview->moved--;
2963         if (MSG_IS_COPY(msginfo->flags))
2964                 summaryview->copied--;
2965         procmsg_update_unread_children (msginfo, FALSE);
2966
2967         procmsg_msginfo_set_to_folder(msginfo, NULL);
2968         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2969         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2970         summaryview->deleted++;
2971
2972         if (!prefs_common.immediate_exec && 
2973             summaryview->folder_item->stype != F_TRASH)
2974                 summary_set_row_marks(summaryview, row);
2975
2976         debug_print("Message %s/%d is set to delete\n",
2977                     msginfo->folder->path, msginfo->msgnum);
2978 }
2979
2980 void summary_cancel(SummaryView *summaryview)
2981 {
2982         MsgInfo * msginfo;
2983         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2984
2985         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2986                                               summaryview->selected);
2987         if (!msginfo) return;
2988
2989         if (!check_permission(summaryview, msginfo))
2990                 return;
2991
2992         news_cancel_article(summaryview->folder_item->folder, msginfo);
2993         
2994         if (summary_is_locked(summaryview)) return;
2995
2996         summary_lock(summaryview);
2997
2998         gtk_clist_freeze(clist);
2999
3000         summary_update_status(summaryview);
3001         summary_status_show(summaryview);
3002
3003         gtk_clist_thaw(clist);
3004
3005         summary_unlock(summaryview);
3006 }
3007
3008 void summary_delete(SummaryView *summaryview)
3009 {
3010         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3011         FolderItem *item = summaryview->folder_item;
3012         GList *cur;
3013         GtkCTreeNode *sel_last = NULL;
3014         GtkCTreeNode *node;
3015
3016         if (!item) return;
3017
3018         if (summary_is_locked(summaryview)) return;
3019
3020         /* if current folder is trash, ask for confirmation */
3021         if (item->stype == F_TRASH) {
3022                 AlertValue aval;
3023                 MsgInfo *msginfo;
3024
3025                 aval = alertpanel(_("Delete message(s)"),
3026                                   _("Do you really want to delete message(s) from the trash?"),
3027                                   GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3028                 if (aval != G_ALERTDEFAULT) return;
3029
3030                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3031                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3032                         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3033                         partial_mark_for_delete(msginfo);
3034                 }
3035         
3036         }
3037
3038         main_window_cursor_wait(summaryview->mainwin);
3039
3040         /* next code sets current row focus right. We need to find a row
3041          * that is not deleted. */
3042         folder_item_update_freeze();
3043         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3044         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3045                 sel_last = GTK_CTREE_NODE(cur->data);
3046                 summary_delete_row(summaryview, sel_last);
3047         }
3048         folder_item_update_thaw();
3049         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3050
3051         node = summary_find_next_msg(summaryview, sel_last);
3052         if (!node)
3053                 node = summary_find_prev_msg(summaryview, sel_last);
3054
3055         summary_select_node(summaryview, node, FALSE, FALSE);
3056         
3057         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3058                 summary_execute(summaryview);
3059                 /* after deleting, the anchor may be at an invalid row
3060                  * so reset it to the node we found earlier */
3061                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3062         } else
3063                 summary_status_show(summaryview);
3064                 
3065         main_window_cursor_normal(summaryview->mainwin);
3066 }
3067
3068 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3069 {
3070         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3071         MsgInfo *msginfo;
3072
3073         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3074         if (MSG_IS_DELETED(msginfo->flags))
3075                 summaryview->deleted--;
3076         if (MSG_IS_MOVE(msginfo->flags))
3077                 summaryview->moved--;
3078         if (MSG_IS_COPY(msginfo->flags))
3079                 summaryview->copied--;
3080         procmsg_update_unread_children (msginfo, FALSE);
3081
3082         procmsg_msginfo_set_to_folder(msginfo, NULL);
3083         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3084         summary_set_row_marks(summaryview, row);
3085
3086         debug_print("Message %s/%d is unmarked\n",
3087                     msginfo->folder->path, msginfo->msgnum);
3088 }
3089
3090 void summary_unmark(SummaryView *summaryview)
3091 {
3092         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3093         GList *cur;
3094
3095         folder_item_update_freeze();
3096         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3097
3098         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3099                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3100
3101         folder_item_update_thaw();
3102         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3103
3104         summary_status_show(summaryview);
3105 }
3106
3107 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3108                                 FolderItem *to_folder)
3109 {
3110         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3111         MsgInfo *msginfo;
3112
3113         g_return_if_fail(to_folder != NULL);
3114
3115         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3116         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3117         if (MSG_IS_DELETED(msginfo->flags))
3118                 summaryview->deleted--;
3119         if (MSG_IS_COPY(msginfo->flags)) {
3120                 summaryview->copied--;
3121         }
3122         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3123         if (!MSG_IS_MOVE(msginfo->flags)) {
3124                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3125                 summaryview->moved++;
3126         }
3127         if (!prefs_common.immediate_exec) {
3128                 summary_set_row_marks(summaryview, row);
3129         }
3130
3131         debug_print("Message %d is set to move to %s\n",
3132                     msginfo->msgnum, to_folder->path);
3133 }
3134
3135 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3136 {
3137         GList *cur;
3138
3139         if (!to_folder) return;
3140         if (!summaryview->folder_item ||
3141             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3142
3143         if (summary_is_locked(summaryview)) return;
3144
3145         if (summaryview->folder_item == to_folder) {
3146                 alertpanel_error(_("Destination is same as current folder."));
3147                 return;
3148         }
3149
3150         folder_item_update_freeze();
3151         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3152
3153         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3154              cur != NULL; cur = cur->next)
3155                 summary_move_row_to
3156                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3157
3158         folder_item_update_thaw();
3159         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3160
3161         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3162
3163         if (prefs_common.immediate_exec)
3164                 summary_execute(summaryview);
3165         else {
3166                 summary_status_show(summaryview);
3167         }
3168         
3169         if (!summaryview->selected) { /* this was the last message */
3170                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3171                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3172                 if (node)
3173                         summary_select_node(summaryview, node, TRUE, TRUE);
3174         }
3175
3176 }
3177
3178 void summary_move_to(SummaryView *summaryview)
3179 {
3180         FolderItem *to_folder;
3181
3182         if (!summaryview->folder_item ||
3183             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3184
3185         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3186                                          FOLDER_SEL_MOVE, NULL);
3187         summary_move_selected_to(summaryview, to_folder);
3188 }
3189
3190 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3191                                 FolderItem *to_folder)
3192 {
3193         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3194         MsgInfo *msginfo;
3195
3196         g_return_if_fail(to_folder != NULL);
3197
3198         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3199         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3200         if (MSG_IS_DELETED(msginfo->flags))
3201                 summaryview->deleted--;
3202         if (MSG_IS_MOVE(msginfo->flags)) {
3203                 summaryview->moved--;
3204         }
3205         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3206         if (!MSG_IS_COPY(msginfo->flags)) {
3207                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3208                 summaryview->copied++;
3209         }
3210         if (!prefs_common.immediate_exec) {
3211                 summary_set_row_marks(summaryview, row);
3212         }
3213
3214         debug_print("Message %d is set to copy to %s\n",
3215                     msginfo->msgnum, to_folder->path);
3216 }
3217
3218 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3219 {
3220         GList *cur;
3221
3222         if (!to_folder) return;
3223         if (!summaryview->folder_item) return;
3224
3225         if (summary_is_locked(summaryview)) return;
3226
3227         if (summaryview->folder_item == to_folder) {
3228                 alertpanel_error
3229                         (_("Destination to copy is same as current folder."));
3230                 return;
3231         }
3232
3233         folder_item_update_freeze();
3234         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3235
3236         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3237              cur != NULL; cur = cur->next)
3238                 summary_copy_row_to
3239                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3240
3241         folder_item_update_thaw();
3242         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3243
3244         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3245
3246         if (prefs_common.immediate_exec)
3247                 summary_execute(summaryview);
3248         else {
3249                 summary_status_show(summaryview);
3250         }
3251 }
3252
3253 void summary_copy_to(SummaryView *summaryview)
3254 {
3255         FolderItem *to_folder;
3256
3257         if (!summaryview->folder_item) return;
3258
3259         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3260                                          FOLDER_SEL_COPY, NULL);
3261         summary_copy_selected_to(summaryview, to_folder);
3262 }
3263
3264 void summary_add_address(SummaryView *summaryview)
3265 {
3266         MsgInfo *msginfo;
3267         gchar *from;
3268
3269         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3270                                               summaryview->selected);
3271         if (!msginfo) return;
3272
3273         Xstrdup_a(from, msginfo->from, return);
3274         eliminate_address_comment(from);
3275         extract_address(from);
3276         addressbook_add_contact(msginfo->fromname, from, NULL);
3277 }
3278
3279 void summary_select_all(SummaryView *summaryview)
3280 {
3281         if (!summaryview->folder_item) return;
3282
3283         if (summaryview->folder_item->total_msgs >= 500) {
3284                 STATUSBAR_PUSH(summaryview->mainwin,
3285                                _("Selecting all messages..."));
3286                 main_window_cursor_wait(summaryview->mainwin);
3287         }
3288
3289         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3290
3291         if (summaryview->folder_item->total_msgs >= 500) {
3292                 STATUSBAR_POP(summaryview->mainwin);
3293                 main_window_cursor_normal(summaryview->mainwin);
3294         }
3295 }
3296
3297 void summary_unselect_all(SummaryView *summaryview)
3298 {
3299         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3300 }
3301
3302 void summary_select_thread(SummaryView *summaryview)
3303 {
3304         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3305         GtkCTreeNode *node = summaryview->selected;
3306
3307         if (!node) return;
3308
3309         while (GTK_CTREE_ROW(node)->parent != NULL)
3310                 node = GTK_CTREE_ROW(node)->parent;
3311
3312         if (node != summaryview->selected)
3313                 summary_select_node
3314                         (summaryview, node,
3315                          messageview_is_visible(summaryview->messageview),
3316                          FALSE);
3317
3318         gtk_ctree_select_recursive(ctree, node);
3319
3320         summary_status_show(summaryview);
3321 }
3322
3323 void summary_save_as(SummaryV