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