b5a60c96840f6f8e5b0577a7bcb6250dece5465a
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 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         font_desc = pango_font_description_from_string(NORMAL_FONT);
645         gtk_widget_modify_font(summaryview->ctree, font_desc);
646         pango_font_description_free(font_desc);
647
648         if (!bold_style) {
649                 bold_style = gtk_style_copy
650                         (gtk_widget_get_style(summaryview->ctree));
651                 font_desc = pango_font_description_from_string(BOLD_FONT);
652                 if (font_desc) {
653                         pango_font_description_free(bold_style->font_desc);
654                         bold_style->font_desc = font_desc;
655                 }
656                 
657                 pango_font_description_set_weight
658                                 (bold_style->font_desc, PANGO_WEIGHT_BOLD);
659                 bold_marked_style = gtk_style_copy(bold_style);
660                 bold_marked_style->fg[GTK_STATE_NORMAL] =
661                         summaryview->color_marked;
662                 bold_deleted_style = gtk_style_copy(bold_style);
663                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
664                         summaryview->color_dim;
665         }
666
667         font_desc = pango_font_description_new();
668         size = pango_font_description_get_size
669                 (summaryview->statlabel_folder->style->font_desc);
670         pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
671         gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
672         gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
673         gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
674         pango_font_description_free(font_desc);
675
676         pixmap = stock_pixmap_widget(summaryview->hbox_l, STOCK_PIXMAP_DIR_OPEN);
677         gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), pixmap, FALSE, FALSE, 4);
678         gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), pixmap, 0);
679         gtk_widget_show(pixmap);
680         summaryview->folder_pixmap = pixmap;
681
682         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
683         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
684         gtk_widget_show(pixmap);
685         summaryview->quick_search_pixmap = pixmap;
686
687         /* Init summaryview prefs */
688         summaryview->sort_key = SORT_BY_NONE;
689         summaryview->sort_type = SORT_ASCENDING;
690
691         /* Init summaryview extra data */
692         summaryview->simplify_subject_preg = NULL;
693
694         summary_clear_list(summaryview);
695         summary_set_column_titles(summaryview);
696         summary_colorlabel_menu_create(summaryview);
697         summary_set_menu_sensitive(summaryview);
698
699 }
700
701 #if 0
702 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
703                                                  GtkCTreeNode *current_node)
704 {
705         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
706         GtkCTreeNode *node;
707         MsgInfo *msginfo;
708         gint best_score = MIN_SCORE;
709         GtkCTreeNode *best_node = NULL;
710
711         if (current_node)
712                 /*node = current_node;*/
713                 node = GTK_CTREE_NODE_NEXT(current_node);
714         else
715                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
716
717         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
718                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
719                 if (msginfo->score >= summaryview->important_score)
720                         break;
721                 if (msginfo->score > best_score) {
722                         best_score = msginfo->score;
723                         best_node = node;
724                 }
725         }
726
727         if (node != NULL)
728                 return node;
729         else
730                 return best_node;
731 }
732
733 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
734                                                  GtkCTreeNode *current_node)
735 {
736         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
737         GtkCTreeNode *node;
738         MsgInfo *msginfo;
739         gint best_score = MIN_SCORE;
740         GtkCTreeNode *best_node = NULL;
741
742         if (current_node)
743                 /*node = current_node;*/
744                 node = GTK_CTREE_NODE_PREV(current_node);
745         else
746                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
747
748         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
749                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
750                 if (msginfo->score >= summaryview->important_score)
751                         break;
752                 if (msginfo->score > best_score) {
753                         best_score = msginfo->score;
754                         best_node = node;
755                 }
756         }
757
758         if (node != NULL)
759                 return node;
760         else
761                 return best_node;
762 }
763 #endif
764
765 #define CURRENTLY_DISPLAYED(m) \
766 ( (m->msgnum == displayed_msgnum) \
767   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
768
769 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
770 {
771         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
772         GtkCTreeNode *node;
773         GSList *mlist = NULL;
774         gchar *buf;
775         gboolean is_refresh;
776         guint selected_msgnum = 0;
777         guint displayed_msgnum = 0;
778         GSList *cur;
779         GSList *not_killed;
780
781         if (summary_is_locked(summaryview)) return FALSE;
782
783         inc_lock();
784         summary_lock(summaryview);
785
786         if (!prefs_common.summary_quicksearch_sticky
787          && !prefs_common.summary_quicksearch_recurse
788          && !quicksearch_is_running(summaryview->quicksearch)) {
789                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
790         }
791
792         /* STATUSBAR_POP(summaryview->mainwin); */
793
794         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
795         if (is_refresh) {
796                 selected_msgnum = summary_get_msgnum(summaryview,
797                                                      summaryview->selected);
798                 displayed_msgnum = summary_get_msgnum(summaryview,
799                                                       summaryview->displayed);
800         }
801
802         /* process the marks if any */
803         if (summaryview->mainwin->lock_count == 0 &&
804             (summaryview->moved > 0 || summaryview->copied > 0)) {
805                 AlertValue val;
806                 gboolean changed = FALSE;
807
808                 val = alertpanel(_("Process mark"),
809                                  _("Some marks are left. Process it?"),
810                                  GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL);
811                 if (G_ALERTDEFAULT == val) {
812                         summary_unlock(summaryview);
813                         summary_execute(summaryview);
814                         summary_lock(summaryview);
815                         changed = TRUE;
816                 } else if (G_ALERTALTERNATE == val) {
817                         /* DO NOTHING */
818                 } else {
819                         summary_unlock(summaryview);
820                         inc_unlock();
821                         return FALSE;
822                 }
823                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
824                         folder_update_op_count();
825         }
826         
827         gtk_clist_freeze(GTK_CLIST(ctree));
828
829         summary_clear_list(summaryview);
830         summary_set_column_titles(summaryview);
831
832         buf = NULL;
833         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
834                 g_free(buf);
835                 debug_print("empty folder\n\n");
836                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
837                 summary_clear_all(summaryview);
838                 summaryview->folder_item = item;
839                 gtk_clist_thaw(GTK_CLIST(ctree));
840                 summary_unlock(summaryview);
841                 inc_unlock();
842                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
843                         main_window_cursor_wait(summaryview->mainwin);
844                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
845                         if (quicksearch_is_active(summaryview->quicksearch))
846                                 quicksearch_search_subfolders(summaryview->quicksearch, 
847                                               summaryview->folderview,
848                                               summaryview->folder_item);
849                         main_window_cursor_normal(summaryview->mainwin);
850                 }                       
851                 return TRUE;
852         }
853         g_free(buf);
854
855         if (!is_refresh)
856                 messageview_clear(summaryview->messageview);
857
858         summaryview->folder_item = item;
859         item->opened = TRUE;
860
861         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
862         debug_print("%s\n", buf);
863         STATUSBAR_PUSH(summaryview->mainwin, buf);
864         g_free(buf);
865
866         main_window_cursor_wait(summaryview->mainwin);
867
868 /*
869         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
870
871         !!! NEW !!!
872         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
873         BY OTHER PROGRAMS TO THE FOLDER
874 */
875         mlist = folder_item_get_msg_list(item);
876
877         if (summaryview->folder_item->hide_read_msgs) {
878                 GSList *not_killed;
879                 
880                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
881                 not_killed = NULL;
882                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
883                         MsgInfo * msginfo = (MsgInfo *) cur->data;
884                         
885                         if ((MSG_IS_UNREAD(msginfo->flags)
886                              || MSG_IS_MARKED(msginfo->flags)
887                              || MSG_IS_LOCKED(msginfo->flags)
888                              || CURRENTLY_DISPLAYED(msginfo))
889                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
890                                 not_killed = g_slist_prepend(not_killed, msginfo);
891                         else
892                                 procmsg_msginfo_free(msginfo);
893                 }
894                 g_slist_free(mlist);
895                 mlist = not_killed;
896         } else {
897                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
898         }
899
900         if (quicksearch_is_active(summaryview->quicksearch)) {
901                 GSList *not_killed;
902                 
903                 not_killed = NULL;
904                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
905                         MsgInfo * msginfo = (MsgInfo *) cur->data;
906
907                         if (quicksearch_match(summaryview->quicksearch, msginfo))
908                                 not_killed = g_slist_prepend(not_killed, msginfo);
909                         else
910                                 procmsg_msginfo_free(msginfo);
911                 }
912                 
913                 if (quicksearch_is_running(summaryview->quicksearch)) {
914                         /* only scan subfolders when quicksearch changed,
915                          * not when search is the same and folder changed */
916                         main_window_cursor_wait(summaryview->mainwin);
917                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
918                         quicksearch_search_subfolders(summaryview->quicksearch, 
919                                               summaryview->folderview,
920                                               summaryview->folder_item);
921                         main_window_cursor_normal(summaryview->mainwin);
922                 }
923                 
924                 g_slist_free(mlist);
925                 mlist = not_killed;
926         }
927
928 #if 0   
929         if ((global_scoring || item->prefs->scoring)) {
930                 GSList *not_killed;
931                 gint kill_score;
932
933                 not_killed = NULL;
934                 kill_score = prefs_common.kill_score;
935                 if (item->prefs->kill_score > kill_score)
936                         kill_score = item->prefs->kill_score;
937                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
938                         MsgInfo * msginfo = (MsgInfo *) cur->data;
939
940                         if (msginfo->score > kill_score)
941                                 not_killed = g_slist_prepend(not_killed, msginfo);
942                         else
943                                 procmsg_msginfo_free(msginfo);
944                 }
945                 g_slist_free(mlist);
946                 mlist = not_killed;
947         }
948 #endif
949         not_killed = NULL;
950         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
951                 MsgInfo * msginfo = (MsgInfo *) cur->data;
952                 
953                 if (!msginfo->hidden)
954                         not_killed = g_slist_prepend(not_killed, msginfo);
955                 else
956                         procmsg_msginfo_free(msginfo);
957         }
958         g_slist_free(mlist);
959         mlist = not_killed;
960
961         STATUSBAR_POP(summaryview->mainwin);
962
963         /* set ctree and hash table from the msginfo list, and
964            create the thread */
965         summary_set_ctree_from_list(summaryview, mlist);
966
967         g_slist_free(mlist);
968
969         gtk_clist_thaw(GTK_CLIST(ctree));
970
971         if (is_refresh) {
972                 summaryview->displayed =
973                         summary_find_msg_by_msgnum(summaryview,
974                                                    displayed_msgnum);
975                 if (!summaryview->displayed)
976                         messageview_clear(summaryview->messageview);
977                 summary_select_by_msgnum(summaryview, selected_msgnum);
978                 if (!summaryview->selected) {
979                         /* no selected message - select first unread
980                            message, but do not display it */
981                         node = summary_find_next_flagged_msg(summaryview, NULL,
982                                                              MSG_UNREAD, FALSE);
983                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
984                                 node = gtk_ctree_node_nth
985                                         (ctree,
986                                          item->sort_type == SORT_DESCENDING
987                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
988                         summary_select_node(summaryview, node, FALSE, TRUE);
989                 }
990         } else {
991                 /* select first unread message */
992 #if 0
993                 if (summaryview->sort_key == SORT_BY_SCORE)
994                         node = summary_find_next_important_score(summaryview,
995                                                                  NULL);
996                 else
997 #endif
998                 node = summary_find_next_flagged_msg(summaryview, NULL,
999                                                      MSG_UNREAD, FALSE);
1000                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1001                         node = gtk_ctree_node_nth
1002                                 (ctree,
1003                                  item->sort_type == SORT_DESCENDING
1004                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1005                 }
1006                 if (prefs_common.open_unread_on_enter ||
1007                     prefs_common.always_show_msg) {
1008                         summary_unlock(summaryview);
1009                         summary_select_node(summaryview, node, 
1010                                             messageview_is_visible(summaryview->messageview), 
1011                                             TRUE);
1012                         summary_lock(summaryview);
1013                 } else
1014                         summary_select_node(summaryview, node, FALSE, TRUE);
1015         }
1016
1017         summary_set_column_titles(summaryview);
1018         summary_status_show(summaryview);
1019         summary_set_menu_sensitive(summaryview);
1020         toolbar_main_set_sensitive(summaryview->mainwin);
1021
1022         debug_print("\n");
1023         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1024         STATUSBAR_POP(summaryview->mainwin);
1025         main_window_cursor_normal(summaryview->mainwin);
1026         summary_unlock(summaryview);
1027         inc_unlock();
1028
1029         return TRUE;
1030 }
1031
1032 #undef CURRENTLY_DISPLAYED
1033
1034 void summary_clear_list(SummaryView *summaryview)
1035 {
1036         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1037         gint optimal_width;
1038
1039         gtk_clist_freeze(clist);
1040
1041         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1042                                 NULL, summary_free_msginfo_func, NULL);
1043
1044         if (summaryview->folder_item) {
1045                 summaryview->folder_item->opened = FALSE;
1046                 summaryview->folder_item = NULL;
1047         }
1048
1049         summaryview->display_msg = FALSE;
1050
1051         summaryview->selected = NULL;
1052         summaryview->displayed = NULL;
1053         summaryview->total_size = 0;
1054         summaryview->deleted = summaryview->moved = 0;
1055         summaryview->copied = 0;
1056         if (summaryview->msgid_table) {
1057                 g_hash_table_destroy(summaryview->msgid_table);
1058                 summaryview->msgid_table = NULL;
1059         }
1060         if (summaryview->subject_table) {
1061                 g_hash_table_destroy(summaryview->subject_table);
1062                 summaryview->subject_table = NULL;
1063         }
1064         summaryview->mlist = NULL;
1065
1066         gtk_clist_clear(clist);
1067         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1068                 optimal_width = gtk_clist_optimal_column_width
1069                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1070                 gtk_clist_set_column_width
1071                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1072                          optimal_width);
1073         }
1074
1075         gtk_clist_thaw(clist);
1076 }
1077
1078 void summary_clear_all(SummaryView *summaryview)
1079 {
1080         messageview_clear(summaryview->messageview);
1081         summary_clear_list(summaryview);
1082         summary_set_menu_sensitive(summaryview);
1083         toolbar_main_set_sensitive(summaryview->mainwin);
1084         summary_status_show(summaryview);
1085 }
1086
1087 void summary_lock(SummaryView *summaryview)
1088 {
1089         summaryview->lock_count++;
1090 }
1091
1092 void summary_unlock(SummaryView *summaryview)
1093 {
1094         if (summaryview->lock_count)
1095                 summaryview->lock_count--;
1096 }
1097
1098 gboolean summary_is_locked(SummaryView *summaryview)
1099 {
1100         return summaryview->lock_count > 0;
1101 }
1102
1103 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1104 {
1105         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1106         SummarySelection selection;
1107
1108         if (!clist->row_list)
1109                 selection = SUMMARY_NONE;
1110         else if (!clist->selection)
1111                 selection = SUMMARY_SELECTED_NONE;
1112         else if (!clist->selection->next)
1113                 selection = SUMMARY_SELECTED_SINGLE;
1114         else
1115                 selection = SUMMARY_SELECTED_MULTIPLE;
1116
1117         return selection;
1118 }
1119
1120 /*!
1121  *\return       MsgInfo * Selected message if there's one selected;
1122  *              if multiple selected, or none, return NULL.
1123  */ 
1124 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1125 {
1126         /* summaryview->selected may be valid when multiple 
1127          * messages were selected */
1128         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1129
1130         if (sellist == NULL || sellist->next) 
1131                 return NULL;
1132         
1133         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1134 }
1135
1136 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1137 {
1138         GSList *mlist = NULL;
1139         GList *cur;
1140         MsgInfo *msginfo;
1141
1142         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
1143              cur = cur->next) {
1144                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1145                 mlist = g_slist_prepend(mlist, msginfo);
1146         }
1147
1148         mlist = g_slist_reverse(mlist);
1149
1150         return mlist;
1151 }
1152
1153 GSList *summary_get_msg_list(SummaryView *summaryview)
1154 {
1155         GSList *mlist = NULL;
1156         GtkCTree *ctree;
1157         GtkCTreeNode *node;
1158         MsgInfo *msginfo;
1159
1160         ctree = GTK_CTREE(summaryview->ctree);
1161
1162         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1163              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1164                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1165                 mlist = g_slist_prepend(mlist, msginfo);
1166         }
1167
1168         mlist = g_slist_reverse(mlist);
1169
1170         return mlist;
1171 }
1172
1173 static void summary_set_menu_sensitive(SummaryView *summaryview)
1174 {
1175         GtkItemFactory *ifactory = summaryview->popupfactory;
1176         SensitiveCond state;
1177         gboolean sensitive;
1178         GtkWidget *menuitem;
1179         gint i;
1180
1181         static const struct {
1182                 gchar *const entry;
1183                 SensitiveCond cond;
1184         } entry[] = {
1185                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1186                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1187                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1188                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1189                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1190                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1191
1192                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1193                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1194
1195                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1196
1197                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1198                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1199                 {"/Delete"                      , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1200                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1201
1202                 {"/Mark"                        , M_TARGET_EXIST},
1203                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1204                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1205                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1206                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1207                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1208                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1209                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1210                 {"/Color label"                 , M_TARGET_EXIST},
1211
1212                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1213                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1214                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1215
1216                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1217                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1218                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1219                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1220 #if 0
1221                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1222                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1223
1224                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1225                 {"/Select all"                  , M_TARGET_EXIST},
1226 #endif
1227                 {NULL, 0}
1228         };
1229
1230         main_window_set_menu_sensitive(summaryview->mainwin);
1231
1232         state = main_window_get_current_state(summaryview->mainwin);
1233
1234         for (i = 0; entry[i].entry != NULL; i++) {
1235                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1236                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1237         }
1238
1239
1240         summary_lock(summaryview);
1241         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1242         gtk_check_menu_item_set_active
1243                 (GTK_CHECK_MENU_ITEM(menuitem),
1244                  summaryview->messageview->mimeview->textview->show_all_headers);
1245         summary_unlock(summaryview);
1246 }
1247
1248 void summary_select_prev_unread(SummaryView *summaryview)
1249 {
1250         GtkCTreeNode *node;
1251         gboolean skip_cur = FALSE;
1252
1253         if (summaryview->displayed 
1254         &&  summaryview->selected == summaryview->displayed) {
1255                 debug_print("skipping current\n");
1256                 skip_cur = TRUE;
1257         }
1258
1259         node = summary_find_prev_flagged_msg
1260                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1261
1262         if (!node) {
1263                 AlertValue val = 0;
1264
1265                 switch (prefs_common.next_unread_msg_dialog) {
1266                         case NEXTUNREADMSGDIALOG_ALWAYS:
1267                                 val = alertpanel(_("No more unread messages"),
1268                                                  _("No unread message found. "
1269                                                    "Search from the end?"),
1270                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1271                                 break;
1272                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1273                                 val = G_ALERTDEFAULT;
1274                                 break;
1275                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1276                                 val = !G_ALERTDEFAULT;
1277                                 break;
1278                         default:
1279                                 debug_print(
1280                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1281                 }
1282                 if (val != G_ALERTDEFAULT) return;
1283                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1284                                                      MSG_UNREAD, FALSE);
1285         }
1286
1287         if (!node)
1288                 alertpanel_notice(_("No unread messages."));
1289         else
1290                 summary_select_node(summaryview, node, TRUE, FALSE);
1291 }
1292
1293 void summary_select_next_unread(SummaryView *summaryview)
1294 {
1295         GtkCTreeNode *node = summaryview->selected;
1296         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1297         gboolean skip_cur = FALSE;
1298         
1299         if (summaryview->displayed 
1300         &&  summaryview->selected == summaryview->displayed) {
1301                 debug_print("skipping cur\n");
1302                 skip_cur = TRUE;
1303         }
1304
1305
1306         node = summary_find_next_flagged_msg
1307                 (summaryview, node, MSG_UNREAD, skip_cur);
1308         
1309         if (node)
1310                 summary_select_node(summaryview, node, TRUE, FALSE);
1311         else {
1312                 node = summary_find_next_flagged_msg
1313                         (summaryview, NULL, MSG_UNREAD, FALSE);
1314                 if (node == NULL) {
1315                         AlertValue val = 0;
1316
1317                         switch (prefs_common.next_unread_msg_dialog) {
1318                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1319                                         val = alertpanel(_("No more unread messages"),
1320                                                          _("No unread message found. "
1321                                                            "Go to next folder?"),
1322                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1323                                         break;
1324                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1325                                         val = G_ALERTDEFAULT;
1326                                         break;
1327                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1328                                         val = G_ALERTOTHER;
1329                                         break;
1330                                 default:
1331                                         debug_print(
1332                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1333                         }
1334
1335                         if (val == G_ALERTDEFAULT) {
1336                                 g_signal_stop_emission_by_name(G_OBJECT(ctree),
1337                                                          "key_press_event");
1338                                 folderview_select_next_unread(summaryview->folderview);
1339                                 return;
1340                         } 
1341                         else
1342                                 return;
1343                 } else
1344                         summary_select_node(summaryview, node, TRUE, FALSE);
1345
1346         }
1347 }
1348
1349 void summary_select_prev_new(SummaryView *summaryview)
1350 {
1351         GtkCTreeNode *node;
1352
1353         node = summary_find_prev_flagged_msg
1354                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1355
1356         if (!node) {
1357                 AlertValue val;
1358
1359                 val = alertpanel(_("No more new messages"),
1360                                  _("No new message found. "
1361                                    "Search from the end?"),
1362                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1363                 if (val != G_ALERTDEFAULT) return;
1364                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1365                                                      MSG_NEW, FALSE);
1366         }
1367
1368         if (!node)
1369                 alertpanel_notice(_("No new messages."));
1370         else
1371                 summary_select_node(summaryview, node, TRUE, FALSE);
1372 }
1373
1374 void summary_select_next_new(SummaryView *summaryview)
1375 {
1376         GtkCTreeNode *node = summaryview->selected;
1377         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1378
1379         while ((node = summary_find_next_flagged_msg
1380                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1381                 AlertValue val;
1382
1383                 val = alertpanel(_("No more new messages"),
1384                                  _("No new message found. "
1385                                    "Go to next folder?"),
1386                                  GTK_STOCK_YES, _("Search again"),
1387                                  GTK_STOCK_NO);
1388                 if (val == G_ALERTDEFAULT) {
1389                         g_signal_stop_emission_by_name(G_OBJECT(ctree),"key_press_event");
1390                         folderview_select_next_unread(summaryview->folderview);
1391                         return;
1392                 } else if (val == G_ALERTALTERNATE)
1393                         node = NULL;
1394                 else
1395                         return;
1396         }
1397
1398         if (node)
1399                 summary_select_node(summaryview, node, TRUE, FALSE);
1400 }
1401
1402 void summary_select_prev_marked(SummaryView *summaryview)
1403 {
1404         GtkCTreeNode *node;
1405
1406         node = summary_find_prev_flagged_msg
1407                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1408
1409         if (!node) {
1410                 AlertValue val;
1411
1412                 val = alertpanel(_("No more marked messages"),
1413                                  _("No marked message found. "
1414                                    "Search from the end?"),
1415                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1416                 if (val != G_ALERTDEFAULT) return;
1417                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1418                                                      MSG_MARKED, TRUE);
1419         }
1420
1421         if (!node)
1422                 alertpanel_notice(_("No marked messages."));
1423         else
1424                 summary_select_node(summaryview, node, TRUE, FALSE);
1425 }
1426
1427 void summary_select_next_marked(SummaryView *summaryview)
1428 {
1429         GtkCTreeNode *node;
1430
1431         node = summary_find_next_flagged_msg
1432                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1433
1434         if (!node) {
1435                 AlertValue val;
1436
1437                 val = alertpanel(_("No more marked messages"),
1438                                  _("No marked message found. "
1439                                    "Search from the beginning?"),
1440                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1441                 if (val != G_ALERTDEFAULT) return;
1442                 node = summary_find_next_flagged_msg(summaryview, NULL,
1443                                                      MSG_MARKED, TRUE);
1444         }
1445
1446         if (!node)
1447                 alertpanel_notice(_("No marked messages."));
1448         else
1449                 summary_select_node(summaryview, node, TRUE, FALSE);
1450 }
1451
1452 void summary_select_prev_labeled(SummaryView *summaryview)
1453 {
1454         GtkCTreeNode *node;
1455
1456         node = summary_find_prev_flagged_msg
1457                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1458
1459         if (!node) {
1460                 AlertValue val;
1461
1462                 val = alertpanel(_("No more labeled messages"),
1463                                  _("No labeled message found. "
1464                                    "Search from the end?"),
1465                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1466                 if (val != G_ALERTDEFAULT) return;
1467                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1468                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1469         }
1470
1471         if (!node)
1472                 alertpanel_notice(_("No labeled messages."));
1473         else
1474                 summary_select_node(summaryview, node, TRUE, FALSE);
1475 }
1476
1477 void summary_select_next_labeled(SummaryView *summaryview)
1478 {
1479         GtkCTreeNode *node;
1480
1481         node = summary_find_next_flagged_msg
1482                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1483
1484         if (!node) {
1485                 AlertValue val;
1486
1487                 val = alertpanel(_("No more labeled messages"),
1488                                  _("No labeled message found. "
1489                                    "Search from the beginning?"),
1490                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1491                 if (val != G_ALERTDEFAULT) return;
1492                 node = summary_find_next_flagged_msg(summaryview, NULL,
1493                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1494         }
1495
1496         if (!node)
1497                 alertpanel_notice(_("No labeled messages."));
1498         else
1499                 summary_select_node(summaryview, node, TRUE, FALSE);
1500 }
1501
1502 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1503 {
1504         GtkCTreeNode *node;
1505
1506         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1507         summary_select_node(summaryview, node, FALSE, TRUE);
1508 }
1509
1510 /**
1511  * summary_select_node:
1512  * @summaryview: Summary view.
1513  * @node: Summary tree node.
1514  * @display_msg: TRUE to display the selected message.
1515  * @do_refresh: TRUE to refresh the widget.
1516  *
1517  * Select @node (bringing it into view by scrolling and expanding its
1518  * thread, if necessary) and unselect all others.  If @display_msg is
1519  * TRUE, display the corresponding message in the message view.
1520  * If @do_refresh is TRUE, the widget is refreshed.
1521  **/
1522 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1523                          gboolean display_msg, gboolean do_refresh)
1524 {
1525         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1526
1527         if (node) {
1528                 gtkut_ctree_expand_parent_all(ctree, node);
1529                 if (do_refresh) {
1530                         GTK_EVENTS_FLUSH();
1531                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1532                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1533                 }
1534                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1535                 if (display_msg && summaryview->displayed == node)
1536                         summaryview->displayed = NULL;
1537                 summaryview->display_msg = display_msg;
1538                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1539         }
1540 }
1541
1542 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1543 {
1544         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1545         MsgInfo *msginfo;
1546
1547         if (!node)
1548                 return 0;
1549         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1550         return msginfo->msgnum;
1551 }
1552
1553 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1554                                            GtkCTreeNode *current_node)
1555 {
1556         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1557         GtkCTreeNode *node;
1558         MsgInfo *msginfo;
1559
1560         if (current_node)
1561                 node = current_node;
1562         else
1563                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1564
1565         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1566                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1567                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1568         }
1569
1570         return node;
1571 }
1572
1573 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1574                                            GtkCTreeNode *current_node)
1575 {
1576         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1577         GtkCTreeNode *node;
1578         MsgInfo *msginfo;
1579
1580         if (current_node)
1581                 node = current_node;
1582         else
1583                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1584
1585         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1586                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1587                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1588         }
1589
1590         return node;
1591 }
1592
1593 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1594                                                    GtkCTreeNode *current_node,
1595                                                    MsgPermFlags flags,
1596                                                    gboolean start_from_prev)
1597 {
1598         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1599         GtkCTreeNode *node;
1600         MsgInfo *msginfo;
1601
1602         if (current_node) {
1603                 if (start_from_prev)
1604                         node = GTK_CTREE_NODE_PREV(current_node);
1605                 else
1606                         node = current_node;
1607         } else
1608                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1609
1610         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1611                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1612                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1613         }
1614
1615         return node;
1616 }
1617
1618 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1619                                                    GtkCTreeNode *current_node,
1620                                                    MsgPermFlags flags,
1621                                                    gboolean start_from_next)
1622 {
1623         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1624         GtkCTreeNode *node;
1625         MsgInfo *msginfo;
1626
1627         if (current_node) {
1628                 if (start_from_next)
1629                         node = gtkut_ctree_node_next(ctree, current_node);
1630                 else
1631                         node = current_node;
1632         } else
1633                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1634
1635         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1636                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1637                 /* Find msg with matching flags but ignore messages with
1638                    ignore flags, if searching for new or unread messages */
1639                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1640                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1641                         break;
1642         }
1643
1644         return node;
1645 }
1646
1647 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1648                                                 guint msgnum)
1649 {
1650         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1651         GtkCTreeNode *node;
1652         MsgInfo *msginfo;
1653
1654         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1655
1656         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1657                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1658                 if (msginfo && msginfo->msgnum == msgnum) break;
1659         }
1660
1661         return node;
1662 }
1663
1664 static guint attract_hash_func(gconstpointer key)
1665 {
1666         gchar *str;
1667         gchar *p;
1668         guint h;
1669
1670         Xstrdup_a(str, (const gchar *)key, return 0);
1671         trim_subject(str);
1672
1673         p = str;
1674         h = *p;
1675
1676         if (h) {
1677                 for (p += 1; *p != '\0'; p++)
1678                         h = (h << 5) - h + *p;
1679         }
1680
1681         return h;
1682 }
1683
1684 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1685 {
1686         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1687 }
1688
1689 void summary_attract_by_subject(SummaryView *summaryview)
1690 {
1691         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1692         GtkCList *clist = GTK_CLIST(ctree);
1693         GtkCTreeNode *src_node;
1694         GtkCTreeNode *dst_node, *sibling;
1695         GtkCTreeNode *tmp;
1696         MsgInfo *src_msginfo, *dst_msginfo;
1697         GHashTable *subject_table;
1698
1699         debug_print("Attracting messages by subject...");
1700         STATUSBAR_PUSH(summaryview->mainwin,
1701                        _("Attracting messages by subject..."));
1702
1703         main_window_cursor_wait(summaryview->mainwin);
1704         gtk_clist_freeze(clist);
1705
1706         subject_table = g_hash_table_new(attract_hash_func,
1707                                          attract_compare_func);
1708
1709         for (src_node = GTK_CTREE_NODE(clist->row_list);
1710              src_node != NULL;
1711              src_node = tmp) {
1712                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1713                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1714                 if (!src_msginfo) continue;
1715                 if (!src_msginfo->subject) continue;
1716
1717                 /* find attracting node */
1718                 dst_node = g_hash_table_lookup(subject_table,
1719                                                src_msginfo->subject);
1720
1721                 if (dst_node) {
1722                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1723
1724                         /* if the time difference is more than 20 days,
1725                            don't attract */
1726                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1727                             > 60 * 60 * 24 * 20)
1728                                 continue;
1729
1730                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1731                         if (src_node != sibling)
1732                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1733                 }
1734
1735                 g_hash_table_insert(subject_table,
1736                                     src_msginfo->subject, src_node);
1737         }
1738
1739         g_hash_table_destroy(subject_table);
1740
1741         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1742
1743         gtk_clist_thaw(clist);
1744
1745         debug_print("done.\n");
1746         STATUSBAR_POP(summaryview->mainwin);
1747
1748         main_window_cursor_normal(summaryview->mainwin);
1749 }
1750
1751 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1752                                       gpointer data)
1753 {
1754         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1755
1756         if (msginfo)
1757                 procmsg_msginfo_free(msginfo);
1758 }
1759
1760 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1761                                    gpointer data)
1762 {
1763         SummaryView *summaryview = data;
1764         MsgInfo *msginfo;
1765
1766         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1767
1768         if (msginfo->folder && msginfo->folder->folder &&
1769             msginfo->folder->folder->klass->type == F_NEWS)
1770                 news_flag_crosspost(msginfo);
1771
1772         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1773         && procmsg_msg_has_marked_parent(msginfo))
1774                 summaryview->unreadmarked++;
1775
1776         if (MSG_IS_DELETED(msginfo->flags))
1777                 summaryview->deleted++;
1778
1779         summaryview->total_size += msginfo->size;
1780
1781         summary_set_row_marks(summaryview, node);
1782 }
1783
1784 static void summary_update_status(SummaryView *summaryview)
1785 {
1786         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1787         GtkCTreeNode *node;
1788         MsgInfo *msginfo;
1789
1790         summaryview->total_size =
1791         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1792
1793         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1794              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1795                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1796
1797                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1798                 && procmsg_msg_has_marked_parent(msginfo))
1799                         summaryview->unreadmarked++;
1800                 if (MSG_IS_DELETED(msginfo->flags))
1801                         summaryview->deleted++;
1802                 if (MSG_IS_MOVE(msginfo->flags))
1803                         summaryview->moved++;
1804                 if (MSG_IS_COPY(msginfo->flags))
1805                         summaryview->copied++;
1806                 summaryview->total_size += msginfo->size;
1807         }
1808 }
1809
1810 static void summary_status_show(SummaryView *summaryview)
1811 {
1812         gchar *str;
1813         gchar *del, *mv, *cp;
1814         gchar *sel;
1815         gchar *spc;
1816         gchar *itstr;
1817         GList *rowlist, *cur;
1818         guint n_selected = 0;
1819         off_t sel_size = 0;
1820         MsgInfo *msginfo;
1821         gchar *name;
1822
1823         if (!summaryview->folder_item) {
1824                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1825                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1826                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1827                 return;
1828         }
1829
1830         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1831         for (cur = rowlist; cur != NULL; cur = cur->next) {
1832                 msginfo = gtk_ctree_node_get_row_data
1833                         (GTK_CTREE(summaryview->ctree),
1834                          GTK_CTREE_NODE(cur->data));
1835                 if (!msginfo)
1836                         g_warning("summary_status_show(): msginfo == NULL\n");
1837                 else {
1838                         sel_size += msginfo->size;
1839                         n_selected++;
1840                 }
1841         }
1842
1843         name = folder_item_get_name(summaryview->folder_item);
1844         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1845         g_free(name);
1846
1847         if (summaryview->deleted)
1848                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1849         else
1850                 del = g_strdup("");
1851         if (summaryview->moved)
1852                 mv = g_strdup_printf(_("%s%d moved"),
1853                                      summaryview->deleted ? _(", ") : "",
1854                                      summaryview->moved);
1855         else
1856                 mv = g_strdup("");
1857         if (summaryview->copied)
1858                 cp = g_strdup_printf(_("%s%d copied"),
1859                                      summaryview->deleted ||
1860                                      summaryview->moved ? _(", ") : "",
1861                                      summaryview->copied);
1862         else
1863                 cp = g_strdup("");
1864
1865         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1866                 spc = "    ";
1867         else
1868                 spc = "";
1869
1870         if (n_selected) {
1871                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1872                 if (n_selected == 1)
1873                         itstr = g_strdup(_(" item selected"));
1874                 else
1875                         itstr = g_strdup(_(" items selected"));
1876         } else {
1877                 sel = g_strdup("");
1878                 itstr = g_strdup("");
1879         }
1880                 
1881         str = g_strconcat(n_selected ? itos(n_selected) : "",
1882                                         itstr, sel, spc, del, mv, cp, NULL);
1883         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1884         g_free(str);
1885         g_free(sel);
1886         g_free(del);
1887         g_free(mv);
1888         g_free(cp);
1889         g_free(itstr);
1890
1891         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1892
1893                                       summaryview->folder_item->new_msgs,
1894                                       summaryview->folder_item->unread_msgs,
1895                                       summaryview->folder_item->total_msgs,
1896                                       to_human_readable(summaryview->total_size));
1897         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1898         g_free(str);
1899 }
1900
1901 static void summary_set_column_titles(SummaryView *summaryview)
1902 {
1903         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1904         GtkWidget *hbox;
1905         GtkWidget *label;
1906         GtkWidget *arrow;
1907         gint pos;
1908         const gchar *title;
1909         SummaryColumnType type;
1910         gboolean single_char;
1911         GtkJustification justify;
1912
1913         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1914                 SORT_BY_MARK,
1915                 SORT_BY_STATUS,
1916                 SORT_BY_MIME,
1917                 SORT_BY_SUBJECT,
1918                 SORT_BY_FROM,
1919                 SORT_BY_DATE,
1920                 SORT_BY_SIZE,
1921                 SORT_BY_NUMBER,
1922                 SORT_BY_SCORE,
1923                 SORT_BY_LOCKED
1924         };
1925
1926         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1927                 type = summaryview->col_state[pos].type;
1928
1929                 /* CLAWS: mime and unread are single char headers */
1930                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1931                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1932                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1933
1934                 switch (type) {
1935                 case S_COL_SUBJECT:
1936                 case S_COL_FROM:
1937                 case S_COL_DATE:
1938                 case S_COL_NUMBER:
1939                         if (prefs_common.trans_hdr)
1940                                 title = gettext(col_label[type]);
1941                         else
1942                                 title = col_label[type];
1943                         break;
1944                 /* CLAWS: dummies for mark and locked headers */        
1945                 case S_COL_MARK:        
1946                 case S_COL_LOCKED:
1947                         title = "";
1948                         break;
1949                 default:
1950                         title = gettext(col_label[type]);
1951                 }
1952
1953                 if (type == S_COL_MIME) {
1954                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
1955                         gtk_widget_show(label);
1956                         gtk_clist_set_column_widget(clist, pos, label);
1957                         continue;
1958                 }
1959                 if (single_char) {
1960                         gtk_clist_set_column_title(clist, pos, title);
1961                         continue;
1962                 }
1963
1964                 /* CLAWS: changed so that locked and mark headers
1965                  * show a pixmap instead of single character */
1966                 hbox  = gtk_hbox_new(FALSE, 4);
1967                 
1968                 if (type == S_COL_LOCKED)
1969                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1970                 else if (type == S_COL_MARK) 
1971                         label = gtk_pixmap_new(markxpm, markxpmmask);
1972                 else 
1973                         label = gtk_label_new(title);
1974                 
1975                 if (justify == GTK_JUSTIFY_RIGHT)
1976                         gtk_box_pack_end(GTK_BOX(hbox), label,
1977                                          FALSE, FALSE, 0);
1978                 else
1979                         gtk_box_pack_start(GTK_BOX(hbox), label,
1980                                            FALSE, FALSE, 0);
1981
1982                 if (summaryview->sort_key == sort_by[type]) {
1983                         arrow = gtk_arrow_new
1984                                 (summaryview->sort_type == SORT_ASCENDING
1985                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1986                                  GTK_SHADOW_IN);
1987                         if (justify == GTK_JUSTIFY_RIGHT)
1988                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1989                                                    FALSE, FALSE, 0);
1990                         else
1991                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1992                                                  FALSE, FALSE, 0);
1993                 }
1994
1995                 gtk_widget_show_all(hbox);
1996                 gtk_clist_set_column_widget(clist, pos, hbox);
1997         }
1998 }
1999
2000 void summary_sort(SummaryView *summaryview,
2001                   FolderSortKey sort_key, FolderSortType sort_type)
2002 {
2003         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2004         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2005         GtkCListCompareFunc cmp_func = NULL;
2006
2007         switch (sort_key) {
2008         case SORT_BY_MARK:
2009                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2010                 break;
2011         case SORT_BY_STATUS:
2012                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2013                 break;
2014         case SORT_BY_MIME:
2015                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2016                 break;
2017         case SORT_BY_NUMBER:
2018                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2019                 break;
2020         case SORT_BY_SIZE:
2021                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2022                 break;
2023         case SORT_BY_DATE:
2024                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2025                 break;
2026         case SORT_BY_FROM:
2027                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2028                 break;
2029         case SORT_BY_SUBJECT:
2030                 if (summaryview->simplify_subject_preg)
2031                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2032                 else
2033                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2034                 break;
2035         case SORT_BY_SCORE:
2036                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2037                 break;
2038         case SORT_BY_LABEL:
2039                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2040                 break;
2041         case SORT_BY_TO:
2042                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2043                 break;
2044         case SORT_BY_LOCKED:
2045                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2046                 break;
2047         case SORT_BY_NONE:
2048                 break;
2049         default:
2050                 return;
2051         }
2052
2053         summaryview->sort_key = sort_key;
2054         summaryview->sort_type = sort_type;
2055
2056         summary_set_column_titles(summaryview);
2057         summary_set_menu_sensitive(summaryview);
2058
2059         /* allow fallback to don't sort */
2060         if (summaryview->sort_key == SORT_BY_NONE)
2061                 return;
2062
2063         if (cmp_func != NULL) {
2064                 debug_print("Sorting summary...");
2065                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2066
2067                 main_window_cursor_wait(summaryview->mainwin);
2068
2069                 gtk_clist_freeze(clist);
2070                 gtk_clist_set_compare_func(clist, cmp_func);
2071
2072                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2073
2074                 gtk_sctree_sort_recursive(ctree, NULL);
2075
2076                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2077
2078                 main_window_cursor_normal(summaryview->mainwin);
2079                 gtk_clist_thaw(clist);
2080
2081                 debug_print("done.\n");
2082                 STATUSBAR_POP(summaryview->mainwin);
2083         }
2084 }
2085
2086 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2087                                    GtkCTreeNode *cnode, gpointer data)
2088 {
2089         SummaryView *summaryview = (SummaryView *)data;
2090         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2091         gchar *text[N_SUMMARY_COLS];
2092         gint *col_pos = summaryview->col_pos;
2093         const gchar *msgid = msginfo->msgid;
2094         GHashTable *msgid_table = summaryview->msgid_table;
2095
2096         summary_set_header(summaryview, text, msginfo);
2097
2098         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2099                                 NULL, NULL, NULL, NULL, FALSE,
2100                                 gnode->parent->parent ? TRUE : FALSE);
2101 #define SET_TEXT(col) \
2102         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2103                                 text[col_pos[col]])
2104
2105         SET_TEXT(S_COL_NUMBER);
2106         SET_TEXT(S_COL_SCORE);
2107         SET_TEXT(S_COL_SIZE);
2108         SET_TEXT(S_COL_DATE);
2109         SET_TEXT(S_COL_FROM);
2110         SET_TEXT(S_COL_SUBJECT);
2111
2112 #undef SET_TEXT
2113
2114         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2115         summary_set_marks_func(ctree, cnode, summaryview);
2116
2117         if (msgid && msgid[0] != '\0')
2118                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2119
2120         return TRUE;
2121 }
2122
2123 static void summary_set_ctree_from_list(SummaryView *summaryview,
2124                                         GSList *mlist)
2125 {
2126         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2127         MsgInfo *msginfo;
2128                 GtkCTreeNode *node = NULL;
2129         GHashTable *msgid_table;
2130         GHashTable *subject_table;
2131         GSList * cur;
2132         
2133         if (!mlist) return;
2134
2135         debug_print("\tSetting summary from message data...");
2136         STATUSBAR_PUSH(summaryview->mainwin,
2137                        _("Setting summary from message data..."));
2138         gdk_flush();
2139
2140         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2141                                        G_CALLBACK(summary_tree_expanded), summaryview);
2142
2143         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2144         summaryview->msgid_table = msgid_table;
2145         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2146         summaryview->subject_table = subject_table;
2147
2148         if (prefs_common.use_addr_book)
2149                 start_address_completion();
2150         
2151         for (cur = mlist ; cur != NULL; cur = cur->next) {
2152                 msginfo = (MsgInfo *)cur->data;
2153                 msginfo->threadscore = msginfo->score;
2154         }
2155
2156         if (summaryview->threaded) {
2157                 GNode *root, *gnode;
2158
2159                 root = procmsg_get_thread_tree(mlist);
2160
2161                 for (gnode = root->children; gnode != NULL;
2162                      gnode = gnode->next) {
2163                         node = gtk_ctree_insert_gnode
2164                                 (ctree, NULL, node, gnode,
2165                                  summary_insert_gnode_func, summaryview);
2166                 }
2167
2168                 g_node_destroy(root);
2169                 
2170                 summary_thread_init(summaryview);
2171         } else {
2172                 gchar *text[N_SUMMARY_COLS];
2173                 cur = mlist;
2174                 for (; mlist != NULL; mlist = mlist->next) {
2175                         msginfo = (MsgInfo *)mlist->data;
2176
2177                         summary_set_header(summaryview, text, msginfo);
2178
2179                         node = gtk_ctree_insert_node
2180                                 (ctree, NULL, node, text, 2,
2181                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2182                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2183                         summary_set_marks_func(ctree, node, summaryview);
2184
2185                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2186                                 g_hash_table_insert(msgid_table,
2187                                                     msginfo->msgid, node);
2188
2189                         subject_table_insert(subject_table,
2190                                              msginfo->subject,
2191                                              node);
2192                 }
2193                 mlist = cur;
2194         }
2195
2196         if (prefs_common.enable_hscrollbar &&
2197             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2198                 gint optimal_width;
2199
2200                 optimal_width = gtk_clist_optimal_column_width
2201                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2202                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2203                                            summaryview->col_pos[S_COL_SUBJECT],
2204                                            optimal_width);
2205         }
2206
2207         if (prefs_common.use_addr_book)
2208                 end_address_completion();
2209
2210         debug_print("done.\n");
2211         STATUSBAR_POP(summaryview->mainwin);
2212         if (debug_get_mode()) {
2213                 debug_print("\tmsgid hash table size = %d\n",
2214                             g_hash_table_size(msgid_table));
2215                 debug_print("\tsubject hash table size = %d\n",
2216                             g_hash_table_size(subject_table));
2217         }
2218
2219         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2220
2221         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2222
2223         while (prefs_common.bold_unread && node) {
2224                 GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2225                 if (GTK_CTREE_ROW(node)->children)
2226                         summary_set_row_marks(summaryview, node);
2227                 node = next;
2228         }
2229
2230         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2231                                        G_CALLBACK(summary_tree_expanded), summaryview);
2232
2233 }
2234
2235 static gchar *summary_complete_address(const gchar *addr)
2236 {
2237         gint count;
2238         gchar *res, *tmp, *email_addr;
2239
2240         Xstrdup_a(email_addr, addr, return NULL);
2241         extract_address(email_addr);
2242         g_return_val_if_fail(*email_addr, NULL);
2243
2244         /*
2245          * completion stuff must be already initialized
2246          */
2247         res = NULL;
2248         if (1 < (count = complete_address(email_addr))) {
2249                 tmp = get_complete_address(1);
2250 /*      tmp = addressbook_lookup_name( email_addr );
2251         if( tmp ) { */
2252                 res = procheader_get_fromname(tmp);
2253                 g_free(tmp);
2254         }
2255
2256         return res;
2257 }
2258
2259 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2260                                MsgInfo *msginfo)
2261 {
2262         static gchar date_modified[80];
2263         static gchar *to = NULL;
2264         static gchar col_score[11];
2265         static gchar buf[BUFFSIZE];
2266         gint *col_pos = summaryview->col_pos;
2267         FolderType ftype = F_UNKNOWN;
2268
2269         text[col_pos[S_COL_MARK]]   = NULL;
2270         text[col_pos[S_COL_STATUS]] = NULL;
2271         text[col_pos[S_COL_MIME]]   = NULL;
2272         text[col_pos[S_COL_LOCKED]] = NULL;
2273         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2274         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2275         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2276
2277         if (msginfo->date_t) {
2278                 procheader_date_get_localtime(date_modified,
2279                                               sizeof(date_modified),
2280                                               msginfo->date_t);
2281                 text[col_pos[S_COL_DATE]] = date_modified;
2282         } else if (msginfo->date)
2283                 text[col_pos[S_COL_DATE]] = msginfo->date;
2284         else
2285                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2286
2287         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2288                 _("(No From)");
2289         
2290         if (msginfo->folder && msginfo->folder->folder)
2291                 ftype = msginfo->folder->folder->klass->type; 
2292                 
2293         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2294                 gchar *addr = NULL;
2295
2296                 Xstrdup_a(addr, msginfo->from, return);
2297                 extract_address(addr);
2298                 if (prefs_common.use_addr_book) {
2299                         if (account_find_from_address(addr)) {
2300                                 addr = summary_complete_address(msginfo->to);
2301                                 g_free(to);
2302                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2303                                 text[col_pos[S_COL_FROM]] = to;
2304                                 g_free(addr);
2305                         }
2306                 } else {
2307                         if (account_find_from_address(addr)) {
2308                                 g_free(to);
2309                                 to = g_strconcat("-->", msginfo->to, NULL);
2310                                 text[col_pos[S_COL_FROM]] = to;
2311                         }
2312                 }
2313         }
2314
2315         /*
2316          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2317          * checking whether the above block (which handles the special case of
2318          * the --> in sent boxes) was executed.
2319          */
2320         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2321                 gchar *from = summary_complete_address(msginfo->from);
2322                 if (from) {
2323                         g_free(to);
2324                         to = from;
2325                         text[col_pos[S_COL_FROM]] = to;
2326                 }                       
2327         }
2328
2329         if (summaryview->simplify_subject_preg != NULL)
2330                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2331                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2332                                         summaryview->simplify_subject_preg) : 
2333                         _("(No Subject)");
2334         else 
2335                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2336                         _("(No Subject)");
2337 }
2338
2339 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2340 {
2341         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2342 }
2343
2344 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2345                                       GtkCTreeNode *row)
2346 {
2347         g_return_if_fail(summaryview != NULL);
2348         g_return_if_fail(msginfo != NULL);
2349         g_return_if_fail(row != NULL);
2350
2351         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2352         && procmsg_msg_has_marked_parent(msginfo))
2353                 summaryview->unreadmarked--;
2354         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2355                 procmsg_msginfo_unset_flags
2356                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2357                 summary_set_row_marks(summaryview, row);
2358                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2359                 summary_status_show(summaryview);
2360         }
2361 }
2362
2363 typedef struct  {
2364         MsgInfo *msginfo;
2365         SummaryView *summaryview;
2366         GtkCTreeNode *row;
2367 } MarkAsReadData;
2368
2369 static int msginfo_mark_as_read_timeout(void *data)
2370 {
2371         MarkAsReadData *mdata = (MarkAsReadData *)data;
2372         if (!mdata)
2373                 return FALSE;
2374         
2375         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2376                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2377                                      mdata->row); 
2378
2379         g_free(mdata);
2380
2381         return FALSE;   
2382 }
2383
2384 static void summary_display_msg_full(SummaryView *summaryview,
2385                                      GtkCTreeNode *row,
2386                                      gboolean new_window, gboolean all_headers)
2387 {
2388         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2389         MsgInfo *msginfo;
2390         gint val;
2391
2392         if (!new_window) {
2393                 if (summaryview->displayed == row)
2394                         return;
2395                 else
2396                         summaryview->messageview->filtered = FALSE;
2397         }                       
2398         
2399         g_return_if_fail(row != NULL);
2400
2401         if (summary_is_locked(summaryview)) return;
2402         summary_lock(summaryview);
2403
2404         STATUSBAR_POP(summaryview->mainwin);
2405         GTK_EVENTS_FLUSH();
2406
2407         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2408
2409         if (new_window) {
2410                 MessageView *msgview;
2411
2412                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2413                 val = messageview_show(msgview, msginfo, all_headers);
2414         } else {
2415                 MessageView *msgview;
2416
2417                 msgview = summaryview->messageview;
2418
2419                 summaryview->displayed = row;
2420                 if (!messageview_is_visible(msgview)) {
2421                         main_window_toggle_message_view(summaryview->mainwin);
2422                         GTK_EVENTS_FLUSH();
2423                 }
2424                 val = messageview_show(msgview, msginfo, all_headers);
2425                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2426                         gtk_widget_grab_focus(summaryview->ctree);
2427                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2428         }
2429
2430         if (val == 0) {
2431                 if (prefs_common.mark_as_read_delay) {
2432                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2433                         data->summaryview = summaryview;
2434                         data->msginfo = msginfo;
2435                         data->row = row;
2436                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2437                                 msginfo_mark_as_read_timeout, data);
2438                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2439                         msginfo_mark_as_read(summaryview, msginfo, row);
2440                 }
2441         }
2442
2443         summary_set_menu_sensitive(summaryview);
2444         toolbar_main_set_sensitive(summaryview->mainwin);
2445         messageview_set_menu_sensitive(summaryview->messageview);
2446
2447         summary_unlock(summaryview);
2448 }
2449
2450 void summary_display_msg_selected(SummaryView *summaryview,
2451                                   gboolean all_headers)
2452 {
2453         if (summary_is_locked(summaryview)) return;
2454         summaryview->displayed = NULL;
2455         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2456                                  all_headers);
2457 }
2458
2459 void summary_redisplay_msg(SummaryView *summaryview)
2460 {
2461         GtkCTreeNode *node;
2462
2463         if (summaryview->displayed) {
2464                 node = summaryview->displayed;
2465                 summaryview->displayed = NULL;
2466                 summary_display_msg(summaryview, node);
2467         }
2468 }
2469
2470 void summary_open_msg(SummaryView *summaryview)
2471 {
2472         if (!summaryview->selected) return;
2473         
2474         /* CLAWS: if separate message view, don't open a new window
2475          * but rather use the current separated message view */
2476         summary_display_msg_full(summaryview, summaryview->selected,
2477                                  prefs_common.sep_msg ? FALSE : TRUE, 
2478                                  FALSE);
2479 }
2480
2481 void summary_view_source(SummaryView * summaryview)
2482 {
2483         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2484         MsgInfo *msginfo;
2485         SourceWindow *srcwin;
2486
2487         if (!summaryview->selected) return;
2488
2489         srcwin = source_window_create();
2490         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2491         source_window_show_msg(srcwin, msginfo);
2492         source_window_show(srcwin);
2493 }
2494
2495 void summary_reedit(SummaryView *summaryview)
2496 {
2497         MsgInfo *msginfo;
2498
2499         if (!summaryview->selected) return;
2500         if (!summaryview->folder_item) return;
2501         if (summaryview->folder_item->stype != F_OUTBOX &&
2502             summaryview->folder_item->stype != F_DRAFT  &&
2503             summaryview->folder_item->stype != F_QUEUE) return;
2504
2505         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2506                                               summaryview->selected);
2507         if (!msginfo) return;
2508
2509         compose_reedit(msginfo);
2510 }
2511
2512 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2513 {
2514         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2515         GtkCTreeNode *node;
2516
2517         if (summary_is_locked(summaryview)) return FALSE;
2518
2519         if (type == GTK_SCROLL_STEP_FORWARD) {
2520                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2521                 if (node)
2522                         gtkut_ctree_expand_parent_all(ctree, node);
2523                 else
2524                         return FALSE;
2525         } else {
2526                 if (summaryview->selected) {
2527                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2528                         if (!node) return FALSE;
2529                 }
2530         }
2531
2532         if (messageview_is_visible(summaryview->messageview))
2533                 summaryview->display_msg = TRUE;
2534
2535         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2536
2537         if (GTK_CLIST(ctree)->selection)
2538                 gtk_sctree_set_anchor_row
2539                         (GTK_SCTREE(ctree),
2540                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2541
2542         return TRUE;
2543 }
2544
2545 void summary_toggle_view(SummaryView *summaryview)
2546 {
2547         if (!messageview_is_visible(summaryview->messageview) &&
2548             summaryview->selected)
2549                 summary_display_msg(summaryview,
2550                                     summaryview->selected);
2551         else
2552                 main_window_toggle_message_view(summaryview->mainwin);
2553 }
2554
2555 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2556                                                 GtkCTreeNode *node)
2557 {
2558         MsgInfo *msginfo;
2559
2560         if (node) {
2561                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2562                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2563                         return TRUE;
2564                 node = GTK_CTREE_ROW(node)->children;
2565         } else
2566                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2567
2568         while (node) {
2569                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2570                         return TRUE;
2571                 node = GTK_CTREE_ROW(node)->sibling;
2572         }
2573
2574         return FALSE;
2575 }
2576
2577 static gboolean summary_have_unread_children(SummaryView *summaryview,
2578                                              GtkCTreeNode *node)
2579 {
2580         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2581
2582         if (!node) return FALSE;
2583
2584         node = GTK_CTREE_ROW(node)->children;
2585
2586         while (node) {
2587                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2588                         return TRUE;
2589                 node = GTK_CTREE_ROW(node)->sibling;
2590         }
2591         return FALSE;
2592 }
2593
2594 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2595 {
2596         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2597         GtkStyle *style = NULL;
2598         MsgInfo *msginfo;
2599         MsgFlags flags;
2600         gint *col_pos = summaryview->col_pos;
2601
2602         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2603         if (!msginfo) return;
2604
2605         flags = msginfo->flags;
2606
2607         gtk_ctree_node_set_foreground(ctree, row, NULL);
2608
2609         /* set new/unread column */
2610         if (MSG_IS_IGNORE_THREAD(flags)) {
2611                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2612                                           ignorethreadxpm, ignorethreadxpmmask);
2613         } else if (MSG_IS_NEW(flags)) {
2614                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2615                                           newxpm, newxpmmask);
2616         } else if (MSG_IS_UNREAD(flags)) {
2617                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2618                                           unreadxpm, unreadxpmmask);
2619         } else if (MSG_IS_REPLIED(flags)) {
2620                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2621                                           repliedxpm, repliedxpmmask);
2622         } else if (MSG_IS_FORWARDED(flags)) {
2623                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2624                                           forwardedxpm, forwardedxpmmask);
2625         } else {
2626                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2627                                         NULL);
2628         }
2629
2630         if (prefs_common.bold_unread &&
2631             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2632              (!GTK_CTREE_ROW(row)->expanded &&
2633               GTK_CTREE_ROW(row)->children &&
2634               summary_have_unread_children(summaryview, row))))
2635                 style = bold_style;
2636
2637         /* set mark column */
2638         if (MSG_IS_DELETED(flags)) {
2639                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2640                                           deletedxpm, deletedxpmmask);
2641                 if (style)
2642                         style = bold_deleted_style;
2643                 else {
2644                         style = small_deleted_style;
2645                 }
2646                         gtk_ctree_node_set_foreground
2647                                 (ctree, row, &summaryview->color_dim);
2648         } else if (MSG_IS_MARKED(flags)) {
2649                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2650                                           markxpm, markxpmmask);
2651         } else if (MSG_IS_MOVE(flags)) {
2652                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2653                 if (style)
2654                         style = bold_marked_style;
2655                 else {
2656                         style = small_marked_style;
2657                 }
2658                         gtk_ctree_node_set_foreground
2659                                 (ctree, row, &summaryview->color_marked);
2660         } else if (MSG_IS_COPY(flags)) {
2661                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2662                 if (style)
2663                         style = bold_marked_style;
2664                 else {
2665                         style = small_marked_style;
2666                 }
2667                         gtk_ctree_node_set_foreground
2668                                 (ctree, row, &summaryview->color_marked);
2669 #if 0
2670         } else if ((global_scoring ||
2671                   summaryview->folder_item->prefs->scoring) &&
2672                  (msginfo->score >= summaryview->important_score) &&
2673                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2674                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2675                 gtk_ctree_node_set_foreground(ctree, row,
2676                                               &summaryview->color_important);
2677 #endif
2678         } else {
2679                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2680         }
2681
2682         if (MSG_IS_LOCKED(flags)) {
2683                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2684                                           lockedxpm, lockedxpmmask);
2685         }
2686         else {
2687                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2688         }
2689
2690         if (MSG_IS_SIGNED(flags)) {
2691                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2692                                           gpgsignedxpm, gpgsignedxpmmask);
2693         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2694                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2695                                           clipkeyxpm, clipkeyxpmmask);
2696         } else if (MSG_IS_ENCRYPTED(flags)) {
2697                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2698                                           keyxpm, keyxpmmask);
2699         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2700                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2701                                           clipxpm, clipxpmmask);
2702         } else {
2703                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2704         }
2705         if (!style)
2706                 style = small_style;
2707
2708         gtk_ctree_node_set_row_style(ctree, row, style);
2709
2710         if (MSG_GET_COLORLABEL(flags))
2711                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2712 }
2713
2714 void summary_set_marks_selected(SummaryView *summaryview)
2715 {
2716         GList *cur;
2717
2718         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2719              cur = cur->next)
2720                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2721 }
2722
2723 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2724 {
2725         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2726         MsgInfo *msginfo;
2727
2728         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2729         if (MSG_IS_DELETED(msginfo->flags))
2730                 summaryview->deleted--;
2731         if (MSG_IS_MOVE(msginfo->flags))
2732                 summaryview->moved--;
2733         if (MSG_IS_COPY(msginfo->flags))
2734                 summaryview->copied--;
2735         procmsg_update_unread_children (msginfo, TRUE);
2736
2737         procmsg_msginfo_set_to_folder(msginfo, NULL);
2738         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2739         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2740         summary_set_row_marks(summaryview, row);
2741         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2742 }
2743
2744 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2745 {
2746         gboolean changed = FALSE;
2747         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2748         MsgInfo *msginfo;
2749
2750         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2751         if (MSG_IS_DELETED(msginfo->flags))
2752                 summaryview->deleted--;
2753         if (MSG_IS_MOVE(msginfo->flags)) {
2754                 summaryview->moved--;
2755                 changed = TRUE;
2756         }
2757         if (MSG_IS_COPY(msginfo->flags)) {
2758                 summaryview->copied--;
2759                 changed = TRUE;
2760         }
2761         procmsg_msginfo_set_to_folder(msginfo, NULL);
2762         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2763         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2764         summary_set_row_marks(summaryview, row);
2765         debug_print("Message %d is locked\n", msginfo->msgnum);
2766 }
2767
2768 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2769 {
2770         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2771         MsgInfo *msginfo;
2772
2773         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2774         if (!MSG_IS_LOCKED(msginfo->flags))
2775                 return;
2776         procmsg_msginfo_set_to_folder(msginfo, NULL);
2777         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2778         summary_set_row_marks(summaryview, row);
2779         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2780 }
2781
2782 void summary_mark(SummaryView *summaryview)
2783 {
2784         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2785         GList *cur;
2786
2787         folder_item_update_freeze();
2788         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2789         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2790                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2791         folder_item_update_thaw();
2792         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2793
2794         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2795         summary_status_show(summaryview);
2796 }
2797
2798 static void summary_mark_row_as_read(SummaryView *summaryview,
2799                                      GtkCTreeNode *row)
2800 {
2801         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2802         MsgInfo *msginfo;
2803
2804         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2805
2806         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2807                 return;
2808
2809         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2810         && procmsg_msg_has_marked_parent(msginfo))
2811                 summaryview->unreadmarked--;
2812
2813         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2814         summary_set_row_marks(summaryview, row);
2815         debug_print("Message %d is marked as read\n",
2816                 msginfo->msgnum);
2817 }
2818
2819 void summary_mark_as_read(SummaryView *summaryview)
2820 {
2821         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2822         GList *cur;
2823
2824         folder_item_update_freeze();
2825         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2826         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2827                 summary_mark_row_as_read(summaryview,
2828                                          GTK_CTREE_NODE(cur->data));
2829         folder_item_update_thaw();
2830         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2831
2832         summary_status_show(summaryview);
2833 }
2834
2835 void summary_msgs_lock(SummaryView *summaryview)
2836 {
2837         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2838         GList *cur;
2839
2840         folder_item_update_freeze();
2841         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2842         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2843                 summary_lock_row(summaryview,
2844                                          GTK_CTREE_NODE(cur->data));
2845         folder_item_update_thaw();
2846         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2847         
2848         summary_status_show(summaryview);
2849 }
2850
2851 void summary_msgs_unlock(SummaryView *summaryview)
2852 {
2853         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2854         GList *cur;
2855
2856         folder_item_update_freeze();
2857         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2858         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2859                 summary_unlock_row(summaryview,
2860                                    GTK_CTREE_NODE(cur->data));
2861         folder_item_update_thaw();
2862         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2863         
2864         summary_status_show(summaryview);
2865 }
2866
2867 void summary_mark_all_read(SummaryView *summaryview)
2868 {
2869         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2870         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2871         GtkCTreeNode *node;
2872
2873         gtk_clist_freeze(clist);
2874         folder_item_update_freeze();
2875         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2876              node = gtkut_ctree_node_next(ctree, node))
2877                 summary_mark_row_as_read(summaryview, node);
2878         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2879              node = gtkut_ctree_node_next(ctree, node)) {
2880                 if (!GTK_CTREE_ROW(node)->expanded)
2881                         summary_set_row_marks(summaryview, node);
2882         }
2883         folder_item_update_thaw();
2884         gtk_clist_thaw(clist);
2885
2886         summary_status_show(summaryview);
2887 }
2888
2889 static void summary_mark_row_as_unread(SummaryView *summaryview,
2890                                        GtkCTreeNode *row)
2891 {
2892         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2893         MsgInfo *msginfo;
2894
2895         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2896         if (MSG_IS_DELETED(msginfo->flags)) {
2897                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2898                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2899                 summaryview->deleted--;
2900         }
2901
2902         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2903         && procmsg_msg_has_marked_parent(msginfo))
2904                 summaryview->unreadmarked++;
2905
2906         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2907         debug_print("Message %d is marked as unread\n",
2908                 msginfo->msgnum);
2909
2910         summary_set_row_marks(summaryview, row);
2911 }
2912
2913 void summary_mark_as_unread(SummaryView *summaryview)
2914 {
2915         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2916         GList *cur;
2917
2918         folder_item_update_freeze();
2919         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2920         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2921                 summary_mark_row_as_unread(summaryview,
2922                                            GTK_CTREE_NODE(cur->data));
2923         folder_item_update_thaw();
2924         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2925         
2926         summary_status_show(summaryview);
2927 }
2928
2929 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2930 {
2931         GList * cur;
2932         gboolean found;
2933
2934         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2935
2936         case F_NEWS:
2937
2938                 /*
2939                   security : checks if one the accounts correspond to
2940                   the author of the post
2941                 */
2942
2943                 found = FALSE;
2944                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2945                         PrefsAccount * account;
2946                         gchar * from_name;
2947                         
2948                         account = cur->data;
2949                         if (account->name && *account->name)
2950                                 from_name =
2951                                         g_strdup_printf("%s <%s>",
2952                                                         account->name,
2953                                                         account->address);
2954                         else
2955                                 from_name =
2956                                         g_strdup_printf("%s",
2957                                                         account->address);
2958                         
2959                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
2960                                 g_free(from_name);
2961                                 found = TRUE;
2962                                 break;
2963                         }
2964                         g_free(from_name);
2965                 }
2966
2967                 if (!found) {
2968                         alertpanel_error(_("You're not the author of the article\n"));
2969                 }
2970                 
2971                 return found;
2972
2973         default:
2974                 return TRUE;
2975         }
2976 }
2977
2978 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2979 {
2980         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2981         MsgInfo *msginfo;
2982
2983         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2984
2985         if (!check_permission(summaryview, msginfo))
2986                 return;
2987
2988         if (MSG_IS_LOCKED(msginfo->flags)) return;
2989
2990         if (MSG_IS_DELETED(msginfo->flags)) return;
2991
2992         if (MSG_IS_MOVE(msginfo->flags))
2993                 summaryview->moved--;
2994         if (MSG_IS_COPY(msginfo->flags))
2995                 summaryview->copied--;
2996         procmsg_update_unread_children (msginfo, FALSE);
2997
2998         procmsg_msginfo_set_to_folder(msginfo, NULL);
2999         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
3000         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
3001         summaryview->deleted++;
3002
3003         if (!prefs_common.immediate_exec && 
3004             summaryview->folder_item->stype != F_TRASH)
3005                 summary_set_row_marks(summaryview, row);
3006
3007         debug_print("Message %s/%d is set to delete\n",
3008                     msginfo->folder->path, msginfo->msgnum);
3009 }
3010
3011 void summary_cancel(SummaryView *summaryview)
3012 {
3013         MsgInfo * msginfo;
3014         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3015
3016         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3017                                               summaryview->selected);
3018         if (!msginfo) return;
3019
3020         if (!check_permission(summaryview, msginfo))
3021                 return;
3022
3023         news_cancel_article(summaryview->folder_item->folder, msginfo);
3024         
3025         if (summary_is_locked(summaryview)) return;
3026
3027         summary_lock(summaryview);
3028
3029         gtk_clist_freeze(clist);
3030
3031         summary_update_status(summaryview);
3032         summary_status_show(summaryview);
3033
3034         gtk_clist_thaw(clist);
3035
3036         summary_unlock(summaryview);
3037 }
3038
3039 void summary_delete(SummaryView *summaryview)
3040 {
3041         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3042         FolderItem *item = summaryview->folder_item;
3043         GList *cur;
3044         GtkCTreeNode *sel_last = NULL;
3045         GtkCTreeNode *node;
3046
3047         if (!item) return;
3048
3049         if (summary_is_locked(summaryview)) return;
3050
3051         /* if current folder is trash, ask for confirmation */
3052         if (item->stype == F_TRASH) {
3053                 AlertValue aval;
3054                 MsgInfo *msginfo;
3055
3056                 aval = alertpanel(_("Delete message(s)"),
3057                                   _("Do you really want to delete message(s) from the trash?"),
3058                                   GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3059                 if (aval != G_ALERTDEFAULT) return;
3060
3061                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3062                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3063                         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3064                         partial_mark_for_delete(msginfo);
3065                 }
3066         
3067         }
3068
3069         main_window_cursor_wait(summaryview->mainwin);
3070
3071         /* next code sets current row focus right. We need to find a row
3072          * that is not deleted. */
3073         folder_item_update_freeze();
3074         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3075         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3076                 sel_last = GTK_CTREE_NODE(cur->data);
3077                 summary_delete_row(summaryview, sel_last);
3078         }
3079         folder_item_update_thaw();
3080         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3081
3082         node = summary_find_next_msg(summaryview, sel_last);
3083         if (!node)
3084                 node = summary_find_prev_msg(summaryview, sel_last);
3085
3086         summary_select_node(summaryview, node, FALSE, FALSE);
3087         
3088         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3089                 summary_execute(summaryview);
3090                 /* after deleting, the anchor may be at an invalid row
3091                  * so reset it to the node we found earlier */
3092                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3093         } else
3094                 summary_status_show(summaryview);
3095                 
3096         main_window_cursor_normal(summaryview->mainwin);
3097 }
3098
3099 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3100 {
3101         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3102         MsgInfo *msginfo;
3103
3104         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3105         if (MSG_IS_DELETED(msginfo->flags))
3106                 summaryview->deleted--;
3107         if (MSG_IS_MOVE(msginfo->flags))
3108                 summaryview->moved--;
3109         if (MSG_IS_COPY(msginfo->flags))
3110                 summaryview->copied--;
3111         procmsg_update_unread_children (msginfo, FALSE);
3112
3113         procmsg_msginfo_set_to_folder(msginfo, NULL);
3114         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3115         summary_set_row_marks(summaryview, row);
3116
3117         debug_print("Message %s/%d is unmarked\n",
3118                     msginfo->folder->path, msginfo->msgnum);
3119 }
3120
3121 void summary_unmark(SummaryView *summaryview)
3122 {
3123         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3124         GList *cur;
3125
3126         folder_item_update_freeze();
3127         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3128
3129         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3130                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3131
3132         folder_item_update_thaw();
3133         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3134
3135         summary_status_show(summaryview);
3136 }
3137
3138 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3139                                 FolderItem *to_folder)
3140 {
3141         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3142         MsgInfo *msginfo;
3143
3144         g_return_if_fail(to_folder != NULL);
3145
3146         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3147         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3148         if (MSG_IS_DELETED(msginfo->flags))
3149                 summaryview->deleted--;
3150         if (MSG_IS_COPY(msginfo->flags)) {
3151                 summaryview->copied--;
3152         }
3153         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3154         if (!MSG_IS_MOVE(msginfo->flags)) {
3155                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3156                 summaryview->moved++;
3157         }
3158         if (!prefs_common.immediate_exec) {
3159                 summary_set_row_marks(summaryview, row);
3160         }
3161
3162         debug_print("Message %d is set to move to %s\n",
3163                     msginfo->msgnum, to_folder->path);
3164 }
3165
3166 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3167 {
3168         GList *cur;
3169
3170         if (!to_folder) return;
3171         if (!summaryview->folder_item ||
3172             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3173
3174         if (summary_is_locked(summaryview)) return;
3175
3176         if (summaryview->folder_item == to_folder) {
3177                 alertpanel_error(_("Destination is same as current folder."));
3178                 return;
3179         }
3180
3181         folder_item_update_freeze();
3182         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3183
3184         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3185              cur != NULL; cur = cur->next)
3186                 summary_move_row_to
3187                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3188
3189         folder_item_update_thaw();
3190         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3191
3192         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3193
3194         if (prefs_common.immediate_exec)
3195                 summary_execute(summaryview);
3196         else {
3197                 summary_status_show(summaryview);
3198         }
3199         
3200         if (!summaryview->selected) { /* this was the last message */
3201                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3202                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3203                 if (node)
3204                         summary_select_node(summaryview, node, TRUE, TRUE);
3205         }
3206
3207 }
3208
3209 void summary_move_to(SummaryView *summaryview)
3210 {
3211         FolderItem *to_folder;
3212
3213         if (!summaryview->folder_item ||
3214             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3215
3216         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3217                                          FOLDER_SEL_MOVE, NULL);
3218         summary_move_selected_to(summaryview, to_folder);
3219 }
3220
3221 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3222                                 FolderItem *to_folder)
3223 {
3224         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3225         MsgInfo *msginfo;
3226
3227         g_return_if_fail(to_folder != NULL);
3228
3229         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3230         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3231         if (MSG_IS_DELETED(msginfo->flags))
3232                 summaryview->deleted--;
3233         if (MSG_IS_MOVE(msginfo->flags)) {
3234                 summaryview->moved--;
3235         }
3236         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3237         if (!MSG_IS_COPY(msginfo->flags)) {
3238                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3239                 summaryview->copied++;
3240         }
3241         if (!prefs_common.immediate_exec) {
3242                 summary_set_row_marks(summaryview, row);
3243         }
3244
3245         debug_print("Message %d is set to copy to %s\n",
3246                     msginfo->msgnum, to_folder->path);
3247 }
3248
3249 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3250 {
3251         GList *cur;
3252
3253         if (!to_folder) return;
3254         if (!summaryview->folder_item) return;
3255
3256         if (summary_is_locked(summaryview)) return;
3257
3258         if (summaryview->folder_item == to_folder) {
3259                 alertpanel_error
3260                         (_("Destination to copy is same as current folder."));
3261                 return;
3262         }
3263
3264         folder_item_update_freeze();
3265         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3266
3267         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3268              cur != NULL; cur = cur->next)
3269                 summary_copy_row_to
3270                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3271
3272         folder_item_update_thaw();
3273         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3274
3275         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3276
3277         if (prefs_common.immediate_exec)
3278                 summary_execute(summaryview);
3279         else {
3280                 summary_status_show(summaryview);
3281         }
3282 }
3283
3284 void summary_copy_to(SummaryView *summaryview)
3285 {
3286         FolderItem *to_folder;
3287
3288         if (!summaryview->folder_item) return;
3289
3290         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3291                                          FOLDER_SEL_COPY, NULL);
3292         summary_copy_selected_to(summaryview, to_folder);
3293 }
3294
3295 void summary_add_address(SummaryView *summaryview)
3296 {
3297         MsgInfo *msginfo;
3298         gchar *from;
3299
3300         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3301                                               summaryview->selected);
3302         if (!msginfo) return;
3303
3304         Xstrdup_a(from, msginfo->from, return);
3305         eliminate_address_comment(from);
3306         extract_address(from);
3307         addressbook_add_contact(msginfo->fromname, from, NULL);
3308 }
3309
3310 void summary_select_all(SummaryView *summaryview)
3311 {
3312         if (!summaryview->folder_item) return;
3313
3314         if (summaryview->folder_item->total_msgs >= 500) {
3315                 STATUSBAR_PUSH(summaryview->mainwin,
3316                                _("Selecting all messages..."));
3317                 main_window_cursor_wait(summaryview->mainwin);
3318         }
3319
3320         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3321
3322         if (summaryview->folder_item->total_msgs >= 500) {
3323                 STATUSBAR_POP(summaryview->mainwin);
3324                 main_window_cursor_normal(summaryview->mainwin);
3325         }
3326 }
3327
3328 void summary_unselect_all(SummaryView *summaryview)
3329 {
3330         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3331 }
3332
3333 void summary_select_thread(SummaryView *summaryview)
3334 {
3335         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3336         GtkCTreeNode *node = summaryview->selected;
3337
3338         if (!node) return;
3339
3340         while (GTK_CTREE_ROW(node)->parent != NULL)
3341                 node = GTK_CTREE_ROW(node)->parent;
3342
3343         if (node != summaryview->selected)
3344                 summary_select_node
3345                         (summaryview, node,
3346                          messageview_is_visible(summaryview->messageview),
3347                          FALSE);
3348
3349         gtk_ctree_select_recursive(ctree, node);
3350
3351         summary_status_show(summaryview);
3352 }
3353
3354 void summary_save_as(SummaryView *summaryview)
3355 {
3356         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3357         MsgInfo *msginfo;
3358         gchar *filename = NULL;
3359         gchar *src, *dest;
3360         gchar *tmp;
3361
3362         AlertValue aval = 0;
3363
3364         if (!summaryview->selected) return;
3365         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3366         if (!msginfo) return;
3367
3368         if (msginfo->subject) {
3369                 Xstrdup_a(filename, msginfo->subject, return);
3370                 subst_for_filename(filename);
3371         }
3372         if (g_getenv ("G_BROKEN_FILENAMES") &&
3373             filename && !g_utf8_validate(filename, -1, NULL)) {
3374                 gchar *oldstr = filename;
3375                 filename = conv_codeset_strdup(filename,
3376                                                conv_get_locale_charset_str(),
3377                                                CS_UTF_8);
3378                 if (!filename) {
3379                         g_warning("summary_save_as(): faild to convert character set.");
3380                         filename = g_strdup(oldstr);
3381                 }
3382                 dest = filesel_select_file_save(_("Save as"), filename);
3383                 g_free(filename);
3384         } else
3385                 dest = filesel_select_file_save(_("Save as"), filename);
3386         filename = NULL;
3387         if (!dest) return;
3388         if (is_file_exist(dest)) {
3389                 aval = alertpanel(_("Append or Overwrite"),
3390                                   _("Append or overwrite existing file?"),
3391                                   _("Append"), _("Overwrite"),
3392                                   GTK_STOCK_CANCEL);
3393                 if (aval != 0 && aval != 1)
3394                         return;
3395         }
3396
3397         src = procmsg_get_message_file(msginfo);
3398         tmp = g_path_get_basename(dest);
3399
3400         if ( aval==0 ) { /* append */
3401                 if (append_file(src, dest, TRUE) < 0) 
3402                         alertpanel_error(_("Can't save the file `%s'."), tmp);
3403         } else { /* overwrite */
3404                 if (copy_file(src, dest, TRUE) < 0)
3405                         alertpanel_error(_("Can't save the file `%s'."), tmp);
3406         }
3407         g_free(src);
3408         
3409         /*
3410          * If two or more msgs are selected,
3411          * append them to the output file.
3412          */
3413         if (GTK_CLIST(ctree)->selection->next) {
3414                 GList *item;
3415                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3416                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3417                         if (!msginfo) break;
3418                         src = procmsg_get_message_file(msginfo);
3419                         if (append_file(src, dest, TRUE) < 0)
3420                                 alertpanel_error(_("Can't save the file `%s'."), tmp);
3421                 }
3422                 g_free(src);
3423         }
3424         g_free(dest);
3425         g_free(tmp);
3426 }
3427
3428 void summary_print(SummaryView *summaryview)
3429 {
3430         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3431         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3432         MsgInfo *msginfo;
3433         GList *cur;
3434         gchar *cmdline;
3435         gchar *p;
3436
3437         if (clist->selection == NULL) return;
3438
3439         cmdline = input_dialog(_("Print"),
3440                                _("Enter the print command line:\n"
3441                                  "(`%s' will be replaced with file name)"),
3442                                prefs_common.print_cmd);
3443         if (!cmdline) return;
3444         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3445             strchr(p + 2, '%')) {
3446                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3447                                  cmdline);
3448                 g_free(cmdline);
3449                 return;
3450         }
3451
3452         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3453                 msginfo = gtk_ctree_node_get_row_data
3454                         (ctree, GTK_CTREE_NODE(cur->data));
3455                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3456         }
3457
3458         g_free(cmdline);
3459 }
3460
3461 gboolean summary_execute(SummaryView *summaryview)
3462 {
3463         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3464         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3465         GtkCTreeNode *node, *next;
3466         GtkCTreeNode *new_selected = NULL;
3467
3468         if (!summaryview->folder_item) return FALSE;
3469
3470         if (summary_is_locked(summaryview)) return FALSE;
3471         summary_lock(summaryview);
3472
3473         gtk_clist_freeze(clist);
3474
3475         if (summaryview->threaded)
3476                 summary_unthread_for_exec(summaryview);
3477
3478         folder_item_update_freeze();
3479         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3480         summary_execute_move(summaryview);
3481         summary_execute_copy(summaryview);
3482         summary_execute_delete(summaryview);
3483         folder_item_update_thaw();
3484         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3485         
3486         node = GTK_CTREE_NODE(clist->row_list);
3487         for (; node != NULL; node = next) {
3488                 next = gtkut_ctree_node_next(ctree, node);
3489                 if (gtk_ctree_node_get_row_data(ctree, node) != NULL) continue;
3490
3491                 if (node == summaryview->displayed) {
3492                         messageview_clear(summaryview->messageview);
3493                         summaryview->displayed = NULL;
3494                 }
3495                 if (GTK_CTREE_ROW(node)->children != NULL) {
3496                         g_warning("summary_execute(): children != NULL\n");
3497                         continue;
3498                 }
3499
3500                 if (!new_selected &&
3501                     gtkut_ctree_node_is_selected(ctree, node)) {
3502                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
3503                         new_selected = summary_find_next_msg(summaryview, node);
3504                         if (!new_selected)
3505                                 new_selected = summary_find_prev_msg
3506                                         (summaryview, node);
3507                 }
3508
3509                 gtk_ctree_remove_node(ctree, node);
3510         }
3511
3512         if (new_selected) {
3513                 gtk_sctree_select
3514                         (GTK_SCTREE(ctree),
3515                          summaryview->displayed ? summaryview->displayed
3516                          : new_selected);
3517         }
3518
3519         if (summaryview->threaded)
3520                 summary_thread_build(summaryview);
3521
3522         summaryview->selected = clist->selection ?
3523                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3524
3525         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3526                 menu_set_insensitive_all
3527                         (GTK_MENU_SHELL(summaryview->popupmenu));
3528                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3529         } else
3530                 gtk_widget_grab_focus(summaryview->ctree);
3531
3532         summary_update_status(summaryview);
3533         summary_status_show(summaryview);
3534
3535         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3536
3537         gtk_clist_thaw(clist);
3538
3539         summary_unlock(summaryview);
3540         return TRUE;
3541 }
3542
3543 static void summary_execute_move(SummaryView *summaryview)
3544 {
3545         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3546         GSList *cur;
3547
3548         /* search moving messages and execute */
3549         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3550                                 summaryview);
3551
3552         if (summaryview->mlist) {
3553                 procmsg_move_messages(summaryview->mlist);
3554
3555                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3556                         procmsg_msginfo_free((MsgInfo *)cur->data);
3557                 g_slist_free(summaryview->mlist);
3558                 summaryview->mlist = NULL;
3559         }
3560 }
3561
3562 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3563                                       gpointer data)
3564 {
3565         SummaryView *summaryview = data;
3566         MsgInfo *msginfo;
3567
3568         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3569
3570         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3571                 summaryview->mlist =
3572                         g_slist_prepend(summaryview->mlist, msginfo);
3573                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3574
3575                 if (msginfo->msgid && *msginfo->msgid &&
3576                     node == g_hash_table_lookup(summaryview->msgid_table,
3577                                                 msginfo->msgid))
3578                         g_hash_table_remove(summaryview->msgid_table,
3579                                             msginfo->msgid);
3580         }
3581 }
3582
3583 static void summary_execute_copy(SummaryView *summaryview)
3584 {
3585         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3586
3587         /* search copying messages and execute */
3588         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3589                                 summaryview);
3590
3591         if (summaryview->mlist) {
3592                 summaryview->mlist = g_slist_reverse(summaryview->mlist);
3593                 procmsg_copy_messages(summaryview->mlist);
3594
3595                 g_slist_free(summaryview->mlist);
3596                 summaryview->mlist = NULL;
3597         }
3598 }
3599
3600 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3601                                       gpointer data)
3602 {
3603         SummaryView *summaryview = data;
3604         MsgInfo *msginfo;
3605
3606         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3607
3608         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3609                 summaryview->mlist =
3610                         g_slist_prepend(summaryview->mlist, msginfo);
3611
3612                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3613                 summary_set_row_marks(summaryview, node);
3614         }
3615 }
3616
3617 static void summary_execute_delete(SummaryView *summaryview)
3618 {
3619         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3620         FolderItem *trash;
3621         GSList *cur;
3622
3623         trash = summaryview->folder_item->folder->trash;
3624
3625         /* search deleting messages and execute */
3626         gtk_ctree_pre_recursive
3627                 (ctree, NULL, summary_execute_delete_func, summaryview);
3628
3629         if (!summaryview->mlist) return;
3630
3631         if (trash == NULL || summaryview->folder_item == trash)
3632                 folder_item_remove_msgs(summaryview->folder_item,
3633                                         summaryview->mlist);
3634         else
3635                 folder_item_move_msgs(trash, summaryview->mlist);
3636
3637         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3638                 procmsg_msginfo_free((MsgInfo *)cur->data);
3639
3640         g_slist_free(summaryview->mlist);
3641         summaryview->mlist = NULL;
3642 }
3643
3644 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3645                                         gpointer data)
3646 {
3647         SummaryView *summaryview = data;
3648         MsgInfo *msginfo;
3649
3650         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3651
3652         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3653                 summaryview->mlist =
3654                         g_slist_append(summaryview->mlist, msginfo);
3655                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3656
3657                 if (msginfo->msgid && *msginfo->msgid &&
3658                     node == g_hash_table_lookup(summaryview->msgid_table,
3659                                                 msginfo->msgid)) {
3660                         g_hash_table_remove(summaryview->msgid_table,
3661                                             msginfo->msgid);
3662                 }       
3663                 if (msginfo->subject && *msginfo->subject && 
3664                     node == subject_table_lookup(summaryview->subject_table,
3665                                                  msginfo->subject)) {
3666                         subject_table_remove(summaryview->subject_table,
3667                                              msginfo->subject);
3668                 }                                           
3669         }
3670 }
3671
3672 /* thread functions */
3673
3674 void summary_thread_build(SummaryView *summaryview)
3675 {
3676         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3677         GtkCTreeNode *node;
3678         GtkCTreeNode *next;
3679         GtkCTreeNode *parent;
3680         MsgInfo *msginfo;
3681         GSList *reflist;
3682
3683         summary_lock(summaryview);
3684
3685         debug_print("Building threads...");
3686         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3687         main_window_cursor_wait(summaryview->mainwin);
3688
3689         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3690                                        G_CALLBACK(summary_tree_expanded), summaryview);
3691         gtk_clist_freeze(GTK_CLIST(ctree));
3692
3693         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3694         while (node) {
3695                 next = GTK_CTREE_ROW(node)->sibling;
3696
3697                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3698
3699                 parent = NULL;
3700
3701                 if (msginfo && msginfo->inreplyto) {
3702                         parent = g_hash_table_lookup(summaryview->msgid_table,
3703                                                      msginfo->inreplyto);
3704                                                      
3705                         if (!parent && msginfo->references) {
3706                                 for (reflist = msginfo->references;
3707                                      reflist != NULL; reflist = reflist->next)
3708                                         if ((parent = g_hash_table_lookup
3709                                                 (summaryview->msgid_table,
3710                                                  reflist->data)))
3711                                                 break;
3712                         }
3713                 }
3714
3715                 if (prefs_common.thread_by_subject && parent == NULL) {
3716                         parent = subject_table_lookup
3717                                 (summaryview->subject_table,
3718                                  msginfo->subject);
3719                 }
3720
3721                 if (parent && parent != node) {
3722                         gtk_ctree_move(ctree, node, parent, NULL);
3723                 }
3724
3725                 node = next;
3726         }
3727
3728         gtkut_ctree_set_focus_row(ctree, summaryview->selected);
3729
3730         gtk_clist_thaw(GTK_CLIST(ctree));
3731         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3732                                          G_CALLBACK(summary_tree_expanded), summaryview);
3733
3734         debug_print("done.\n");
3735         STATUSBAR_POP(summaryview->mainwin);
3736         main_window_cursor_normal(summaryview->mainwin);
3737
3738         summaryview->threaded = TRUE;
3739
3740         summary_unlock(summaryview);
3741 }
3742
3743 static void summary_thread_init(SummaryView *summaryview)
3744 {
3745         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3746         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3747         GtkCTreeNode *next;
3748
3749         if (!summaryview->thread_collapsed) {
3750                 g_signal_handlers_block_by_func(G_OBJECT(ctree),
3751                                        G_CALLBACK(summary_tree_expanded), summaryview);
3752                 while (node) {
3753                         next = GTK_CTREE_ROW(node)->sibling;
3754                         if (GTK_CTREE_ROW(node)->children)
3755                                 gtk_ctree_expand_recursive(ctree, node);
3756                         node = next;
3757                 }
3758                 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3759                                        G_CALLBACK(summary_tree_expanded), summaryview);
3760         } 
3761 }
3762
3763 void summary_unthread(SummaryView *summaryview)
3764 {
3765         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3766         GtkCTreeNode *node;
3767         GtkCTreeNode *child;
3768         GtkCTreeNode *sibling;
3769         GtkCTreeNode *next_child;
3770
3771         summary_lock(summaryview);
3772
3773         debug_print("Unthreading...");
3774         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
3775         main_window_cursor_wait(summaryview->mainwin);
3776         
3777         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3778                                         summary_tree_collapsed, summaryview);
3779         gtk_clist_freeze(GTK_CLIST(ctree));
3780
3781         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3782              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3783                 child = GTK_CTREE_ROW(node)->children;
3784                 sibling = GTK_CTREE_ROW(node)->sibling;
3785
3786                 while (child != NULL) {
3787                         next_child = GTK_CTREE_ROW(child)->sibling;
3788                         gtk_ctree_move(ctree, child, NULL, sibling);
3789                         child = next_child;
3790                 }
3791         }
3792
3793         /* CLAWS: and sort it */
3794         gtk_sctree_sort_recursive(ctree, NULL); 
3795
3796         gtk_clist_thaw(GTK_CLIST(ctree));
3797         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3798                                            G_CALLBACK(summary_tree_collapsed), summaryview);
3799
3800         debug_print("done.\n");
3801         STATUSBAR_POP(summaryview->mainwin);
3802         main_window_cursor_normal(summaryview->mainwin);
3803
3804         summaryview->threaded = FALSE;
3805
3806         summary_unlock(summaryview);
3807 }
3808
3809 static void summary_unthread_for_exec(SummaryView *summaryview)
3810 {
3811         GtkCTreeNode *node;
3812         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3813
3814         summary_lock(summaryview);
3815
3816         debug_print("Unthreading for execution...");
3817
3818         gtk_clist_freeze(GTK_CLIST(ctree));
3819
3820         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3821              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3822                 summary_unthread_for_exec_func(ctree, node, NULL);
3823         }
3824
3825         gtk_clist_thaw(GTK_CLIST(ctree));
3826
3827         debug_print("done.\n");
3828
3829         summary_unlock(summaryview);
3830 }
3831
3832 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
3833                                            gpointer data)
3834 {
3835         MsgInfo *msginfo;
3836         GtkCTreeNode *top_parent;
3837         GtkCTreeNode *child;
3838         GtkCTreeNode *sibling;
3839
3840         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3841
3842         if (!msginfo ||
3843             (!MSG_IS_MOVE(msginfo->flags) &&
3844              !MSG_IS_DELETED(msginfo->flags)))
3845                 return;
3846         child = GTK_CTREE_ROW(node)->children;
3847         if (!child) return;
3848
3849         for (top_parent = node;
3850              GTK_CTREE_ROW(top_parent)->parent != NULL;
3851              top_parent = GTK_CTREE_ROW(top_parent)->parent)
3852                 ;
3853         sibling = GTK_CTREE_ROW(top_parent)->sibling;
3854
3855         while (child != NULL) {
3856                 GtkCTreeNode *next_child;
3857
3858                 next_child = GTK_CTREE_ROW(child)->sibling;
3859                 gtk_ctree_move(ctree, child, NULL, sibling);
3860                 child = next_child;
3861         }
3862 }
3863
3864 void summary_expand_threads(SummaryView *summaryview)
3865 {
3866         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3867         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3868
3869         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3870                                        G_CALLBACK(summary_tree_expanded), summaryview);
3871         gtk_clist_freeze(GTK_CLIST(ctree));
3872
3873         while (node) {
3874                 if (GTK_CTREE_ROW(node)->children) {
3875                         gtk_ctree_expand(ctree, node);
3876                         summary_set_row_marks(summaryview, node);
3877                 }
3878                 node = GTK_CTREE_NODE_NEXT(node);
3879         }
3880
3881         gtk_clist_thaw(GTK_CLIST(ctree));
3882         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3883                                          G_CALLBACK(summary_tree_expanded), summaryview);
3884
3885         summaryview->thread_collapsed = FALSE;
3886
3887         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3888 }
3889
3890 void