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