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