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