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