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