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