2005-04-03 [colin] 1.9.6cvs23
[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
2081                 if (summaryview->threaded)
2082                         summary_thread_init(summaryview);
2083                         
2084                 gtk_clist_thaw(clist);
2085
2086                 debug_print("done.\n");
2087                 STATUSBAR_POP(summaryview->mainwin);
2088         }
2089 }
2090
2091 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2092                                    GtkCTreeNode *cnode, gpointer data)
2093 {
2094         SummaryView *summaryview = (SummaryView *)data;
2095         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2096         gchar *text[N_SUMMARY_COLS];
2097         gint *col_pos = summaryview->col_pos;
2098         const gchar *msgid = msginfo->msgid;
2099         GHashTable *msgid_table = summaryview->msgid_table;
2100
2101         summary_set_header(summaryview, text, msginfo);
2102
2103         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2104                                 NULL, NULL, NULL, NULL, FALSE,
2105                                 gnode->parent->parent ? TRUE : FALSE);
2106 #define SET_TEXT(col) \
2107         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2108                                 text[col_pos[col]])
2109
2110         SET_TEXT(S_COL_NUMBER);
2111         SET_TEXT(S_COL_SCORE);
2112         SET_TEXT(S_COL_SIZE);
2113         SET_TEXT(S_COL_DATE);
2114         SET_TEXT(S_COL_FROM);
2115         SET_TEXT(S_COL_SUBJECT);
2116
2117 #undef SET_TEXT
2118
2119         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2120         summary_set_marks_func(ctree, cnode, summaryview);
2121
2122         if (msgid && msgid[0] != '\0')
2123                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2124
2125         return TRUE;
2126 }
2127
2128 static void summary_set_ctree_from_list(SummaryView *summaryview,
2129                                         GSList *mlist)
2130 {
2131         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2132         MsgInfo *msginfo;
2133                 GtkCTreeNode *node = NULL;
2134         GHashTable *msgid_table;
2135         GHashTable *subject_table;
2136         GSList * cur;
2137         
2138         if (!mlist) return;
2139
2140         debug_print("\tSetting summary from message data...");
2141         STATUSBAR_PUSH(summaryview->mainwin,
2142                        _("Setting summary from message data..."));
2143         gdk_flush();
2144
2145         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2146         summaryview->msgid_table = msgid_table;
2147         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2148         summaryview->subject_table = subject_table;
2149
2150         if (prefs_common.use_addr_book)
2151                 start_address_completion();
2152         
2153         for (cur = mlist ; cur != NULL; cur = cur->next) {
2154                 msginfo = (MsgInfo *)cur->data;
2155                 msginfo->threadscore = msginfo->score;
2156         }
2157
2158         if (summaryview->threaded) {
2159                 GNode *root, *gnode;
2160
2161                 root = procmsg_get_thread_tree(mlist);
2162
2163                 for (gnode = root->children; gnode != NULL;
2164                      gnode = gnode->next) {
2165                         node = gtk_ctree_insert_gnode
2166                                 (ctree, NULL, node, gnode,
2167                                  summary_insert_gnode_func, summaryview);
2168                 }
2169
2170                 g_node_destroy(root);
2171         } else {
2172                 gchar *text[N_SUMMARY_COLS];
2173                 cur = mlist;
2174                 for (; mlist != NULL; mlist = mlist->next) {
2175                         msginfo = (MsgInfo *)mlist->data;
2176
2177                         summary_set_header(summaryview, text, msginfo);
2178
2179                         node = gtk_ctree_insert_node
2180                                 (ctree, NULL, node, text, 2,
2181                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2182                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2183                         summary_set_marks_func(ctree, node, summaryview);
2184
2185                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2186                                 g_hash_table_insert(msgid_table,
2187                                                     msginfo->msgid, node);
2188
2189                         subject_table_insert(subject_table,
2190                                              msginfo->subject,
2191                                              node);
2192                 }
2193                 mlist = cur;
2194         }
2195
2196         if (prefs_common.enable_hscrollbar &&
2197             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2198                 gint optimal_width;
2199
2200                 optimal_width = gtk_clist_optimal_column_width
2201                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2202                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2203                                            summaryview->col_pos[S_COL_SUBJECT],
2204                                            optimal_width);
2205         }
2206
2207         if (prefs_common.use_addr_book)
2208                 end_address_completion();
2209
2210         debug_print("done.\n");
2211         STATUSBAR_POP(summaryview->mainwin);
2212         if (debug_get_mode()) {
2213                 debug_print("\tmsgid hash table size = %d\n",
2214                             g_hash_table_size(msgid_table));
2215                 debug_print("\tsubject hash table size = %d\n",
2216                             g_hash_table_size(subject_table));
2217         }
2218
2219         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2220 }
2221
2222 static gchar *summary_complete_address(const gchar *addr)
2223 {
2224         gint count;
2225         gchar *res, *tmp, *email_addr;
2226
2227         Xstrdup_a(email_addr, addr, return NULL);
2228         extract_address(email_addr);
2229         g_return_val_if_fail(*email_addr, NULL);
2230
2231         /*
2232          * completion stuff must be already initialized
2233          */
2234         res = NULL;
2235         if (1 < (count = complete_address(email_addr))) {
2236                 tmp = get_complete_address(1);
2237 /*      tmp = addressbook_lookup_name( email_addr );
2238         if( tmp ) { */
2239                 res = procheader_get_fromname(tmp);
2240                 g_free(tmp);
2241         }
2242
2243         return res;
2244 }
2245
2246 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2247                                MsgInfo *msginfo)
2248 {
2249         static gchar date_modified[80];
2250         static gchar *to = NULL;
2251         static gchar col_score[11];
2252         static gchar buf[BUFFSIZE];
2253         gint *col_pos = summaryview->col_pos;
2254         FolderType ftype = F_UNKNOWN;
2255
2256         text[col_pos[S_COL_MARK]]   = NULL;
2257         text[col_pos[S_COL_STATUS]] = NULL;
2258         text[col_pos[S_COL_MIME]]   = NULL;
2259         text[col_pos[S_COL_LOCKED]] = NULL;
2260         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2261         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2262         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2263
2264         if (msginfo->date_t) {
2265                 procheader_date_get_localtime(date_modified,
2266                                               sizeof(date_modified),
2267                                               msginfo->date_t);
2268                 text[col_pos[S_COL_DATE]] = date_modified;
2269         } else if (msginfo->date)
2270                 text[col_pos[S_COL_DATE]] = msginfo->date;
2271         else
2272                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2273
2274         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2275                 _("(No From)");
2276         
2277         if (msginfo->folder && msginfo->folder->folder)
2278                 ftype = msginfo->folder->folder->klass->type; 
2279                 
2280         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2281                 gchar *addr = NULL;
2282
2283                 Xstrdup_a(addr, msginfo->from, return);
2284                 extract_address(addr);
2285                 if (prefs_common.use_addr_book) {
2286                         if (account_find_from_address(addr)) {
2287                                 addr = summary_complete_address(msginfo->to);
2288                                 g_free(to);
2289                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2290                                 text[col_pos[S_COL_FROM]] = to;
2291                                 g_free(addr);
2292                         }
2293                 } else {
2294                         if (account_find_from_address(addr)) {
2295                                 g_free(to);
2296                                 to = g_strconcat("-->", msginfo->to, NULL);
2297                                 text[col_pos[S_COL_FROM]] = to;
2298                         }
2299                 }
2300         }
2301
2302         /*
2303          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2304          * checking whether the above block (which handles the special case of
2305          * the --> in sent boxes) was executed.
2306          */
2307         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2308                 gchar *from = summary_complete_address(msginfo->from);
2309                 if (from) {
2310                         g_free(to);
2311                         to = from;
2312                         text[col_pos[S_COL_FROM]] = to;
2313                 }                       
2314         }
2315
2316         if (summaryview->simplify_subject_preg != NULL)
2317                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2318                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2319                                         summaryview->simplify_subject_preg) : 
2320                         _("(No Subject)");
2321         else 
2322                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2323                         _("(No Subject)");
2324 }
2325
2326 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2327 {
2328         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2329 }
2330
2331 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2332                                       GtkCTreeNode *row)
2333 {
2334         g_return_if_fail(summaryview != NULL);
2335         g_return_if_fail(msginfo != NULL);
2336         g_return_if_fail(row != NULL);
2337
2338         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2339         && procmsg_msg_has_marked_parent(msginfo))
2340                 summaryview->unreadmarked--;
2341         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2342                 procmsg_msginfo_unset_flags
2343                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2344                 summary_set_row_marks(summaryview, row);
2345                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2346                 summary_status_show(summaryview);
2347         }
2348 }
2349
2350 typedef struct  {
2351         MsgInfo *msginfo;
2352         SummaryView *summaryview;
2353         GtkCTreeNode *row;
2354 } MarkAsReadData;
2355
2356 static int msginfo_mark_as_read_timeout(void *data)
2357 {
2358         MarkAsReadData *mdata = (MarkAsReadData *)data;
2359         if (!mdata)
2360                 return FALSE;
2361         
2362         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2363                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2364                                      mdata->row); 
2365
2366         g_free(mdata);
2367
2368         return FALSE;   
2369 }
2370
2371 static void summary_display_msg_full(SummaryView *summaryview,
2372                                      GtkCTreeNode *row,
2373                                      gboolean new_window, gboolean all_headers)
2374 {
2375         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2376         MsgInfo *msginfo;
2377         gint val;
2378
2379         if (!new_window) {
2380                 if (summaryview->displayed == row)
2381                         return;
2382                 else
2383                         summaryview->messageview->filtered = FALSE;
2384         }                       
2385         
2386         g_return_if_fail(row != NULL);
2387
2388         if (summary_is_locked(summaryview)) return;
2389         summary_lock(summaryview);
2390
2391         STATUSBAR_POP(summaryview->mainwin);
2392         GTK_EVENTS_FLUSH();
2393
2394         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2395
2396         if (new_window) {
2397                 MessageView *msgview;
2398
2399                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2400                 val = messageview_show(msgview, msginfo, all_headers);
2401         } else {
2402                 MessageView *msgview;
2403
2404                 msgview = summaryview->messageview;
2405
2406                 summaryview->displayed = row;
2407                 if (!messageview_is_visible(msgview)) {
2408                         main_window_toggle_message_view(summaryview->mainwin);
2409                         GTK_EVENTS_FLUSH();
2410                 }
2411                 val = messageview_show(msgview, msginfo, all_headers);
2412                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2413                         gtk_widget_grab_focus(summaryview->ctree);
2414                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2415         }
2416
2417         if (val == 0) {
2418                 if (prefs_common.mark_as_read_delay) {
2419                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2420                         data->summaryview = summaryview;
2421                         data->msginfo = msginfo;
2422                         data->row = row;
2423                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2424                                 msginfo_mark_as_read_timeout, data);
2425                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2426                         msginfo_mark_as_read(summaryview, msginfo, row);
2427                 }
2428         }
2429
2430         summary_set_menu_sensitive(summaryview);
2431         toolbar_main_set_sensitive(summaryview->mainwin);
2432         messageview_set_menu_sensitive(summaryview->messageview);
2433
2434         summary_unlock(summaryview);
2435 }
2436
2437 void summary_display_msg_selected(SummaryView *summaryview,
2438                                   gboolean all_headers)
2439 {
2440         if (summary_is_locked(summaryview)) return;
2441         summaryview->displayed = NULL;
2442         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2443                                  all_headers);
2444 }
2445
2446 void summary_redisplay_msg(SummaryView *summaryview)
2447 {
2448         GtkCTreeNode *node;
2449
2450         if (summaryview->displayed) {
2451                 node = summaryview->displayed;
2452                 summaryview->displayed = NULL;
2453                 summary_display_msg(summaryview, node);
2454         }
2455 }
2456
2457 void summary_open_msg(SummaryView *summaryview)
2458 {
2459         if (!summaryview->selected) return;
2460         
2461         /* CLAWS: if separate message view, don't open a new window
2462          * but rather use the current separated message view */
2463         summary_display_msg_full(summaryview, summaryview->selected,
2464                                  prefs_common.sep_msg ? FALSE : TRUE, 
2465                                  FALSE);
2466 }
2467
2468 void summary_view_source(SummaryView * summaryview)
2469 {
2470         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2471         MsgInfo *msginfo;
2472         SourceWindow *srcwin;
2473
2474         if (!summaryview->selected) return;
2475
2476         srcwin = source_window_create();
2477         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2478         source_window_show_msg(srcwin, msginfo);
2479         source_window_show(srcwin);
2480 }
2481
2482 void summary_reedit(SummaryView *summaryview)
2483 {
2484         MsgInfo *msginfo;
2485
2486         if (!summaryview->selected) return;
2487         if (!summaryview->folder_item) return;
2488         if (summaryview->folder_item->stype != F_OUTBOX &&
2489             summaryview->folder_item->stype != F_DRAFT  &&
2490             summaryview->folder_item->stype != F_QUEUE) return;
2491
2492         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2493                                               summaryview->selected);
2494         if (!msginfo) return;
2495
2496         compose_reedit(msginfo);
2497 }
2498
2499 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2500 {
2501         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2502         GtkCTreeNode *node;
2503
2504         if (summary_is_locked(summaryview)) return FALSE;
2505
2506         if (type == GTK_SCROLL_STEP_FORWARD) {
2507                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2508                 if (node)
2509                         gtkut_ctree_expand_parent_all(ctree, node);
2510                 else
2511                         return FALSE;
2512         } else {
2513                 if (summaryview->selected) {
2514                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2515                         if (!node) return FALSE;
2516                 }
2517         }
2518
2519         if (messageview_is_visible(summaryview->messageview))
2520                 summaryview->display_msg = TRUE;
2521
2522         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2523
2524         if (GTK_CLIST(ctree)->selection)
2525                 gtk_sctree_set_anchor_row
2526                         (GTK_SCTREE(ctree),
2527                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2528
2529         return TRUE;
2530 }
2531
2532 void summary_toggle_view(SummaryView *summaryview)
2533 {
2534         if (!messageview_is_visible(summaryview->messageview) &&
2535             summaryview->selected)
2536                 summary_display_msg(summaryview,
2537                                     summaryview->selected);
2538         else
2539                 main_window_toggle_message_view(summaryview->mainwin);
2540 }
2541
2542 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2543                                                 GtkCTreeNode *node)
2544 {
2545         MsgInfo *msginfo;
2546
2547         if (node) {
2548                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2549                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2550                         return TRUE;
2551                 node = GTK_CTREE_ROW(node)->children;
2552         } else
2553                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2554
2555         while (node) {
2556                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2557                         return TRUE;
2558                 node = GTK_CTREE_ROW(node)->sibling;
2559         }
2560
2561         return FALSE;
2562 }
2563
2564 static gboolean summary_have_unread_children(SummaryView *summaryview,
2565                                              GtkCTreeNode *node)
2566 {
2567         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2568
2569         if (!node) return FALSE;
2570
2571         node = GTK_CTREE_ROW(node)->children;
2572
2573         while (node) {
2574                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2575                         return TRUE;
2576                 node = GTK_CTREE_ROW(node)->sibling;
2577         }
2578
2579         return FALSE;
2580 }
2581
2582 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2583 {
2584         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2585         GtkStyle *style = NULL;
2586         MsgInfo *msginfo;
2587         MsgFlags flags;
2588         gint *col_pos = summaryview->col_pos;
2589
2590         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2591         if (!msginfo) return;
2592
2593         flags = msginfo->flags;
2594
2595         gtk_ctree_node_set_foreground(ctree, row, NULL);
2596
2597         /* set new/unread column */
2598         if (MSG_IS_IGNORE_THREAD(flags)) {
2599                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2600                                           ignorethreadxpm, ignorethreadxpmmask);
2601         } else if (MSG_IS_NEW(flags)) {
2602                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2603                                           newxpm, newxpmmask);
2604         } else if (MSG_IS_UNREAD(flags)) {
2605                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2606                                           unreadxpm, unreadxpmmask);
2607         } else if (MSG_IS_REPLIED(flags)) {
2608                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2609                                           repliedxpm, repliedxpmmask);
2610         } else if (MSG_IS_FORWARDED(flags)) {
2611                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2612                                           forwardedxpm, forwardedxpmmask);
2613         } else {
2614                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2615                                         NULL);
2616         }
2617
2618         if (prefs_common.bold_unread &&
2619             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2620              (!GTK_CTREE_ROW(row)->expanded &&
2621               GTK_CTREE_ROW(row)->children &&
2622               summary_have_unread_children(summaryview, row))))
2623                 style = bold_style;
2624
2625         /* set mark column */
2626         if (MSG_IS_DELETED(flags)) {
2627                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2628                                           deletedxpm, deletedxpmmask);
2629                 if (style)
2630                         style = bold_deleted_style;
2631                 else {
2632                         style = small_deleted_style;
2633                 }
2634                         gtk_ctree_node_set_foreground
2635                                 (ctree, row, &summaryview->color_dim);
2636         } else if (MSG_IS_MARKED(flags)) {
2637                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2638                                           markxpm, markxpmmask);
2639         } else if (MSG_IS_MOVE(flags)) {
2640                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2641                 if (style)
2642                         style = bold_marked_style;
2643                 else {
2644                         style = small_marked_style;
2645                 }
2646                         gtk_ctree_node_set_foreground
2647                                 (ctree, row, &summaryview->color_marked);
2648         } else if (MSG_IS_COPY(flags)) {
2649                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2650                 if (style)
2651                         style = bold_marked_style;
2652                 else {
2653                         style = small_marked_style;
2654                 }
2655                         gtk_ctree_node_set_foreground
2656                                 (ctree, row, &summaryview->color_marked);
2657 #if 0
2658         } else if ((global_scoring ||
2659                   summaryview->folder_item->prefs->scoring) &&
2660                  (msginfo->score >= summaryview->important_score) &&
2661                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2662                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2663                 gtk_ctree_node_set_foreground(ctree, row,
2664                                               &summaryview->color_important);
2665 #endif
2666         } else {
2667                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2668         }
2669
2670         if (MSG_IS_LOCKED(flags)) {
2671                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2672                                           lockedxpm, lockedxpmmask);
2673         }
2674         else {
2675                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2676         }
2677
2678         if (MSG_IS_SIGNED(flags)) {
2679                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2680                                           gpgsignedxpm, gpgsignedxpmmask);
2681         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2682                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2683                                           clipkeyxpm, clipkeyxpmmask);
2684         } else if (MSG_IS_ENCRYPTED(flags)) {
2685                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2686                                           keyxpm, keyxpmmask);
2687         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2688                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2689                                           clipxpm, clipxpmmask);
2690         } else {
2691                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2692         }
2693         if (!style)
2694                 style = small_style;
2695
2696         gtk_ctree_node_set_row_style(ctree, row, style);
2697
2698         if (MSG_GET_COLORLABEL(flags))
2699                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2700 }
2701
2702 void summary_set_marks_selected(SummaryView *summaryview)
2703 {
2704         GList *cur;
2705
2706         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2707              cur = cur->next)
2708                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2709 }
2710
2711 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2712 {
2713         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2714         MsgInfo *msginfo;
2715
2716         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2717         if (MSG_IS_DELETED(msginfo->flags))
2718                 summaryview->deleted--;
2719         if (MSG_IS_MOVE(msginfo->flags))
2720                 summaryview->moved--;
2721         if (MSG_IS_COPY(msginfo->flags))
2722                 summaryview->copied--;
2723         procmsg_update_unread_children (msginfo, TRUE);
2724
2725         procmsg_msginfo_set_to_folder(msginfo, NULL);
2726         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2727         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2728         summary_set_row_marks(summaryview, row);
2729         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2730 }
2731
2732 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2733 {
2734         gboolean changed = FALSE;
2735         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2736         MsgInfo *msginfo;
2737
2738         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2739         if (MSG_IS_DELETED(msginfo->flags))
2740                 summaryview->deleted--;
2741         if (MSG_IS_MOVE(msginfo->flags)) {
2742                 summaryview->moved--;
2743                 changed = TRUE;
2744         }
2745         if (MSG_IS_COPY(msginfo->flags)) {
2746                 summaryview->copied--;
2747                 changed = TRUE;
2748         }
2749         procmsg_msginfo_set_to_folder(msginfo, NULL);
2750         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2751         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2752         summary_set_row_marks(summaryview, row);
2753         debug_print("Message %d is locked\n", msginfo->msgnum);
2754 }
2755
2756 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2757 {
2758         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2759         MsgInfo *msginfo;
2760
2761         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2762         if (!MSG_IS_LOCKED(msginfo->flags))
2763                 return;
2764         procmsg_msginfo_set_to_folder(msginfo, NULL);
2765         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2766         summary_set_row_marks(summaryview, row);
2767         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2768 }
2769
2770 void summary_mark(SummaryView *summaryview)
2771 {
2772         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2773         GList *cur;
2774
2775         folder_item_update_freeze();
2776         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2777         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2778                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2779         folder_item_update_thaw();
2780         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2781
2782         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2783         summary_status_show(summaryview);
2784 }
2785
2786 static void summary_mark_row_as_read(SummaryView *summaryview,
2787                                      GtkCTreeNode *row)
2788 {
2789         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2790         MsgInfo *msginfo;
2791
2792         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2793
2794         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2795                 return;
2796
2797         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2798         && procmsg_msg_has_marked_parent(msginfo))
2799                 summaryview->unreadmarked--;
2800
2801         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2802         summary_set_row_marks(summaryview, row);
2803         debug_print("Message %d is marked as read\n",
2804                 msginfo->msgnum);
2805 }
2806
2807 void summary_mark_as_read(SummaryView *summaryview)
2808 {
2809         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2810         GList *cur;
2811
2812         folder_item_update_freeze();
2813         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2814         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2815                 summary_mark_row_as_read(summaryview,
2816                                          GTK_CTREE_NODE(cur->data));
2817         folder_item_update_thaw();
2818         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2819
2820         summary_status_show(summaryview);
2821 }
2822
2823 void summary_msgs_lock(SummaryView *summaryview)
2824 {
2825         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2826         GList *cur;
2827
2828         folder_item_update_freeze();
2829         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2830         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2831                 summary_lock_row(summaryview,
2832                                          GTK_CTREE_NODE(cur->data));
2833         folder_item_update_thaw();
2834         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2835         
2836         summary_status_show(summaryview);
2837 }
2838
2839 void summary_msgs_unlock(SummaryView *summaryview)
2840 {
2841         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2842         GList *cur;
2843
2844         folder_item_update_freeze();
2845         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2846         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2847                 summary_unlock_row(summaryview,
2848                                    GTK_CTREE_NODE(cur->data));
2849         folder_item_update_thaw();
2850         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2851         
2852         summary_status_show(summaryview);
2853 }
2854
2855 void summary_mark_all_read(SummaryView *summaryview)
2856 {
2857         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2858         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2859         GtkCTreeNode *node;
2860
2861         gtk_clist_freeze(clist);
2862         folder_item_update_freeze();
2863         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2864              node = gtkut_ctree_node_next(ctree, node))
2865                 summary_mark_row_as_read(summaryview, node);
2866         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2867              node = gtkut_ctree_node_next(ctree, node)) {
2868                 if (!GTK_CTREE_ROW(node)->expanded)
2869                         summary_set_row_marks(summaryview, node);
2870         }
2871         folder_item_update_thaw();
2872         gtk_clist_thaw(clist);
2873
2874         summary_status_show(summaryview);
2875 }
2876
2877 static void summary_mark_row_as_unread(SummaryView *summaryview,
2878                                        GtkCTreeNode *row)
2879 {
2880         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2881         MsgInfo *msginfo;
2882
2883         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2884         if (MSG_IS_DELETED(msginfo->flags)) {
2885                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2886                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2887                 summaryview->deleted--;
2888         }
2889
2890         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2891         && procmsg_msg_has_marked_parent(msginfo))
2892                 summaryview->unreadmarked++;
2893
2894         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2895         debug_print("Message %d is marked as unread\n",
2896                 msginfo->msgnum);
2897
2898         summary_set_row_marks(summaryview, row);
2899 }
2900
2901 void summary_mark_as_unread(SummaryView *summaryview)
2902 {
2903         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2904         GList *cur;
2905
2906         folder_item_update_freeze();
2907         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2908         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2909                 summary_mark_row_as_unread(summaryview,
2910                                            GTK_CTREE_NODE(cur->data));
2911         folder_item_update_thaw();
2912         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2913         
2914         summary_status_show(summaryview);
2915 }
2916
2917 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2918 {
2919         GList * cur;
2920         gboolean found;
2921
2922         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2923
2924         case F_NEWS:
2925
2926                 /*
2927                   security : checks if one the accounts correspond to
2928                   the author of the post
2929                 */
2930
2931                 found = FALSE;
2932                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2933                         PrefsAccount * account;
2934                         gchar * from_name;
2935                         
2936                         account = cur->data;
2937                         if (account->name && *account->name)
2938                                 from_name =
2939                                         g_strdup_printf("%s <%s>",
2940                                                         account->name,
2941                                                         account->address);
2942                         else
2943                                 from_name =
2944                                         g_strdup_printf("%s",
2945                                                         account->address);
2946                         
2947                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
2948                                 g_free(from_name);
2949                                 found = TRUE;
2950                                 break;
2951                         }
2952                         g_free(from_name);
2953                 }
2954
2955                 if (!found) {
2956                         alertpanel_error(_("You're not the author of the article\n"));
2957                 }
2958                 
2959                 return found;
2960
2961         default:
2962                 return TRUE;
2963         }
2964 }
2965
2966 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2967 {
2968         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2969         MsgInfo *msginfo;
2970
2971         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2972
2973         if (!check_permission(summaryview, msginfo))
2974                 return;
2975
2976         if (MSG_IS_LOCKED(msginfo->flags)) return;
2977
2978         if (MSG_IS_DELETED(msginfo->flags)) return;
2979
2980         if (MSG_IS_MOVE(msginfo->flags))
2981                 summaryview->moved--;
2982         if (MSG_IS_COPY(msginfo->flags))
2983                 summaryview->copied--;
2984         procmsg_update_unread_children (msginfo, FALSE);
2985
2986         procmsg_msginfo_set_to_folder(msginfo, NULL);
2987         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2988         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2989         summaryview->deleted++;
2990
2991         if (!prefs_common.immediate_exec && 
2992             summaryview->folder_item->stype != F_TRASH)
2993                 summary_set_row_marks(summaryview, row);
2994
2995         debug_print("Message %s/%d is set to delete\n",
2996                     msginfo->folder->path, msginfo->msgnum);
2997 }
2998
2999 void summary_cancel(SummaryView *summaryview)
3000 {
3001         MsgInfo * msginfo;
3002         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3003
3004         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3005                                               summaryview->selected);
3006         if (!msginfo) return;
3007
3008         if (!check_permission(summaryview, msginfo))
3009                 return;
3010
3011         news_cancel_article(summaryview->folder_item->folder, msginfo);
3012         
3013         if (summary_is_locked(summaryview)) return;
3014
3015         summary_lock(summaryview);
3016
3017         gtk_clist_freeze(clist);
3018
3019         summary_update_status(summaryview);
3020         summary_status_show(summaryview);
3021
3022         gtk_clist_thaw(clist);
3023
3024         summary_unlock(summaryview);
3025 }
3026
3027 void summary_delete(SummaryView *summaryview)
3028 {
3029         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3030         FolderItem *item = summaryview->folder_item;
3031         GList *cur;
3032         GtkCTreeNode *sel_last = NULL;
3033         GtkCTreeNode *node;
3034
3035         if (!item) return;
3036
3037         if (summary_is_locked(summaryview)) return;
3038
3039         /* if current folder is trash, ask for confirmation */
3040         if (item->stype == F_TRASH) {
3041                 AlertValue aval;
3042                 MsgInfo *msginfo;
3043
3044                 aval = alertpanel(_("Delete message(s)"),
3045                                   _("Do you really want to delete message(s) from the trash?"),
3046                                   GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3047                 if (aval != G_ALERTDEFAULT) return;
3048
3049                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3050                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3051                         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3052                         partial_mark_for_delete(msginfo);
3053                 }
3054         
3055         }
3056
3057         main_window_cursor_wait(summaryview->mainwin);
3058
3059         /* next code sets current row focus right. We need to find a row
3060          * that is not deleted. */
3061         folder_item_update_freeze();
3062         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3063         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3064                 sel_last = GTK_CTREE_NODE(cur->data);
3065                 summary_delete_row(summaryview, sel_last);
3066         }
3067         folder_item_update_thaw();
3068         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3069
3070         node = summary_find_next_msg(summaryview, sel_last);
3071         if (!node)
3072                 node = summary_find_prev_msg(summaryview, sel_last);
3073
3074         summary_select_node(summaryview, node, FALSE, FALSE);
3075         
3076         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3077                 summary_execute(summaryview);
3078                 /* after deleting, the anchor may be at an invalid row
3079                  * so reset it to the node we found earlier */
3080                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3081         } else
3082                 summary_status_show(summaryview);
3083                 
3084         main_window_cursor_normal(summaryview->mainwin);
3085 }
3086
3087 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3088 {
3089         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3090         MsgInfo *msginfo;
3091
3092         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3093         if (MSG_IS_DELETED(msginfo->flags))
3094                 summaryview->deleted--;
3095         if (MSG_IS_MOVE(msginfo->flags))
3096                 summaryview->moved--;
3097         if (MSG_IS_COPY(msginfo->flags))
3098                 summaryview->copied--;
3099         procmsg_update_unread_children (msginfo, FALSE);
3100
3101         procmsg_msginfo_set_to_folder(msginfo, NULL);
3102         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3103         summary_set_row_marks(summaryview, row);
3104
3105         debug_print("Message %s/%d is unmarked\n",
3106                     msginfo->folder->path, msginfo->msgnum);
3107 }
3108
3109 void summary_unmark(SummaryView *summaryview)
3110 {
3111         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3112         GList *cur;
3113
3114         folder_item_update_freeze();
3115         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3116
3117         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3118                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3119
3120         folder_item_update_thaw();
3121         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3122
3123         summary_status_show(summaryview);
3124 }
3125
3126 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3127                                 FolderItem *to_folder)
3128 {
3129         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3130         MsgInfo *msginfo;
3131
3132         g_return_if_fail(to_folder != NULL);
3133
3134         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3135         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3136         if (MSG_IS_DELETED(msginfo->flags))
3137                 summaryview->deleted--;
3138         if (MSG_IS_COPY(msginfo->flags)) {
3139                 summaryview->copied--;
3140         }
3141         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3142         if (!MSG_IS_MOVE(msginfo->flags)) {
3143                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3144                 summaryview->moved++;
3145         }
3146         if (!prefs_common.immediate_exec) {
3147                 summary_set_row_marks(summaryview, row);
3148         }
3149
3150         debug_print("Message %d is set to move to %s\n",
3151                     msginfo->msgnum, to_folder->path);
3152 }
3153
3154 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3155 {
3156         GList *cur;
3157
3158         if (!to_folder) return;
3159         if (!summaryview->folder_item ||
3160             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3161
3162         if (summary_is_locked(summaryview)) return;
3163
3164         if (summaryview->folder_item == to_folder) {
3165                 alertpanel_error(_("Destination is same as current folder."));
3166                 return;
3167         }
3168
3169         folder_item_update_freeze();
3170         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3171
3172         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3173              cur != NULL; cur = cur->next)
3174                 summary_move_row_to
3175                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3176
3177         folder_item_update_thaw();
3178         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3179
3180         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3181
3182         if (prefs_common.immediate_exec)
3183                 summary_execute(summaryview);
3184         else {
3185                 summary_status_show(summaryview);
3186         }
3187         
3188         if (!summaryview->selected) { /* this was the last message */
3189                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3190                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3191                 if (node)
3192                         summary_select_node(summaryview, node, TRUE, TRUE);
3193         }
3194
3195 }
3196
3197 void summary_move_to(SummaryView *summaryview)
3198 {
3199         FolderItem *to_folder;
3200
3201         if (!summaryview->folder_item ||
3202             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3203
3204         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3205                                          FOLDER_SEL_MOVE, NULL);
3206         summary_move_selected_to(summaryview, to_folder);
3207 }
3208
3209 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3210                                 FolderItem *to_folder)
3211 {
3212         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3213         MsgInfo *msginfo;
3214
3215         g_return_if_fail(to_folder != NULL);
3216
3217         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3218         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3219         if (MSG_IS_DELETED(msginfo->flags))
3220                 summaryview->deleted--;
3221         if (MSG_IS_MOVE(msginfo->flags)) {
3222                 summaryview->moved--;
3223         }
3224         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3225         if (!MSG_IS_COPY(msginfo->flags)) {
3226                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3227                 summaryview->copied++;
3228         }
3229         if (!prefs_common.immediate_exec) {
3230                 summary_set_row_marks(summaryview, row);
3231         }
3232
3233         debug_print("Message %d is set to copy to %s\n",
3234                     msginfo->msgnum, to_folder->path);
3235 }
3236
3237 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3238 {
3239         GList *cur;
3240
3241         if (!to_folder) return;
3242         if (!summaryview->folder_item) return;
3243
3244         if (summary_is_locked(summaryview)) return;
3245
3246         if (summaryview->folder_item == to_folder) {
3247                 alertpanel_error
3248                         (_("Destination to copy is same as current folder."));
3249                 return;
3250         }
3251
3252         folder_item_update_freeze();
3253         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3254
3255         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3256              cur != NULL; cur = cur->next)
3257                 summary_copy_row_to
3258                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3259
3260         folder_item_update_thaw();
3261         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3262
3263         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3264
3265         if (prefs_common.immediate_exec)
3266                 summary_execute(summaryview);
3267         else {
3268                 summary_status_show(summaryview);
3269         }
3270 }
3271
3272 void summary_copy_to(SummaryView *summaryview)
3273 {
3274         FolderItem *to_folder;
3275
3276         if (!summaryview->folder_item) return;
3277
3278         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3279                                          FOLDER_SEL_COPY, NULL);
3280         summary_copy_selected_to(summaryview, to_folder);
3281 }
3282
3283 void summary_add_address(SummaryView *summaryview)
3284 {
3285         MsgInfo *msginfo;
3286         gchar *from;
3287
3288         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3289                                               summaryview->selected);
3290         if (!msginfo) return;
3291
3292         Xstrdup_a(from, msginfo->from, return);
3293         eliminate_address_comment(from);
3294         extract_address(from);
3295         addressbook_add_contact(msginfo->fromname, from, NULL);
3296 }
3297
3298 void summary_select_all(SummaryView *summaryview)
3299 {
3300         if (!summaryview->folder_item) return;
3301
3302         if (summaryview->folder_item->total_msgs >= 500) {
3303                 STATUSBAR_PUSH(summaryview->mainwin,
3304                                _("Selecting all messages..."));
3305                 main_window_cursor_wait(summaryview->mainwin);
3306         }
3307
3308         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3309
3310         if (summaryview->folder_item->total_msgs >= 500) {
3311                 STATUSBAR_POP(summaryview->mainwin);
3312                 main_window_cursor_normal(summaryview->mainwin);
3313         }
3314 }
3315
3316 void summary_unselect_all(SummaryView *summaryview)
3317 {
3318         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3319 }