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