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