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