anchor row and multiple selection fixes
[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         if (summaryview->folder_table) {
1068                 g_hash_table_destroy(summaryview->folder_table);
1069                 summaryview->folder_table = NULL;
1070         }
1071
1072         gtk_clist_clear(clist);
1073         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1074                 optimal_width = gtk_clist_optimal_column_width
1075                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1076                 gtk_clist_set_column_width
1077                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1078                          optimal_width);
1079         }
1080
1081         gtk_clist_thaw(clist);
1082 }
1083
1084 void summary_clear_all(SummaryView *summaryview)
1085 {
1086         summary_clear_list(summaryview);
1087         summary_set_menu_sensitive(summaryview);
1088         toolbar_set_sensitive(summaryview->mainwin);
1089         summary_status_show(summaryview);
1090 }
1091
1092 void summary_lock(SummaryView *summaryview)
1093 {
1094         summaryview->lock_count++;
1095 }
1096
1097 void summary_unlock(SummaryView *summaryview)
1098 {
1099         if (summaryview->lock_count)
1100                 summaryview->lock_count--;
1101 }
1102
1103 gboolean summary_is_locked(SummaryView *summaryview)
1104 {
1105         return summaryview->lock_count > 0;
1106 }
1107
1108 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1109 {
1110         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1111         SummarySelection selection;
1112
1113         if (!clist->row_list)
1114                 selection = SUMMARY_NONE;
1115         else if (!clist->selection)
1116                 selection = SUMMARY_SELECTED_NONE;
1117         else if (!clist->selection->next)
1118                 selection = SUMMARY_SELECTED_SINGLE;
1119         else
1120                 selection = SUMMARY_SELECTED_MULTIPLE;
1121
1122         return selection;
1123 }
1124
1125 static void summary_set_menu_sensitive(SummaryView *summaryview)
1126 {
1127         GtkItemFactory *ifactory = summaryview->popupfactory;
1128         SummarySelection selection;
1129         GtkWidget *menuitem;
1130         gboolean sens;
1131
1132         selection = summary_get_selection_type(summaryview);
1133         main_window_set_menu_sensitive(summaryview->mainwin);
1134
1135         if (selection == SUMMARY_NONE) {
1136                 GtkWidget *submenu;
1137
1138                 submenu = gtk_item_factory_get_widget
1139                         (summaryview->popupfactory, "/Mark");
1140                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1141                 menu_set_insensitive_all
1142                         (GTK_MENU_SHELL(summaryview->popupmenu));
1143                 return;
1144         }
1145
1146         if (summaryview->folder_item->folder->type != F_NEWS)
1147                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1148         else
1149                 menu_set_sensitive(ifactory, "/Move...", FALSE);
1150
1151 #if 0
1152         menu_set_sensitive(ifactory, "/Delete", TRUE);
1153 #endif
1154         menu_set_sensitive(ifactory, "/Copy...", TRUE);
1155         menu_set_sensitive(ifactory, "/Execute", TRUE);
1156
1157         menu_set_sensitive(ifactory, "/Mark", TRUE);
1158         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1159         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1160
1161         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1162         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1163         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1164         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1165         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1166
1167         menu_set_sensitive(ifactory, "/Color label", TRUE);
1168
1169         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1170         menu_set_sensitive(ifactory, "/Reply",                    sens);
1171         menu_set_sensitive(ifactory, "/Reply to",                 sens);
1172         menu_set_sensitive(ifactory, "/Reply to/all",             sens);
1173         menu_set_sensitive(ifactory, "/Reply to/sender",          sens);
1174         menu_set_sensitive(ifactory, "/Reply to/mailing list",    sens);
1175         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1176         menu_set_sensitive(ifactory, "/Redirect",                 sens);
1177
1178         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1179         menu_set_sensitive(ifactory, "/Create filter rule",         sens);
1180
1181         menu_set_sensitive(ifactory, "/View", sens);
1182         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1183         menu_set_sensitive(ifactory, "/View/Source", sens);
1184         menu_set_sensitive(ifactory, "/View/All header", sens);
1185         if (summaryview->folder_item->stype == F_OUTBOX ||
1186             summaryview->folder_item->stype == F_DRAFT  ||
1187             summaryview->folder_item->stype == F_QUEUE)
1188                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1189         else
1190                 menu_set_sensitive(ifactory, "/Re-edit", FALSE);
1191
1192         menu_set_sensitive(ifactory, "/Save as...", TRUE);
1193         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1194
1195         menu_set_sensitive(ifactory, "/Select all", TRUE);
1196         menu_set_sensitive(ifactory, "/Select thread", sens);
1197         if (summaryview->folder_item->folder->account)
1198                 sens = summaryview->folder_item->folder->account->protocol
1199                         == A_NNTP;
1200         else
1201                 sens = FALSE;
1202         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1203         menu_set_sensitive(ifactory, "/Cancel a news message", sens);
1204         menu_set_sensitive(ifactory, "/Delete", !sens);
1205
1206         summary_lock(summaryview);
1207         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1208         gtk_check_menu_item_set_active
1209                 (GTK_CHECK_MENU_ITEM(menuitem),
1210                  summaryview->messageview->textview->show_all_headers);
1211         summary_unlock(summaryview);
1212 }
1213
1214 void summary_select_prev_unread(SummaryView *summaryview)
1215 {
1216         GtkCTreeNode *node;
1217
1218         node = summary_find_prev_flagged_msg
1219                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1220
1221         if (!node) {
1222                 AlertValue val = 0;
1223
1224                 switch (prefs_common.next_unread_msg_dialog) {
1225                         case NEXTUNREADMSGDIALOG_ALWAYS:
1226                                 val = alertpanel(_("No more unread messages"),
1227                                                  _("No unread message found. "
1228                                                    "Search from the end?"),
1229                                                  _("Yes"), _("No"), NULL);
1230                                 break;
1231                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1232                                 val = G_ALERTDEFAULT;
1233                                 break;
1234                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1235                                 val = !G_ALERTDEFAULT;
1236                                 break;
1237                         default:
1238                                 debug_print(
1239                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1240                 }
1241                 if (val != G_ALERTDEFAULT) return;
1242                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1243                                                      MSG_UNREAD, FALSE);
1244         }
1245
1246         if (!node)
1247                 alertpanel_notice(_("No unread messages."));
1248         else
1249                 summary_select_node(summaryview, node, TRUE, FALSE);
1250 }
1251
1252 void summary_select_next_unread(SummaryView *summaryview)
1253 {
1254         GtkCTreeNode *node = summaryview->selected;
1255         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1256
1257         node = summary_find_next_flagged_msg
1258                 (summaryview, node, MSG_UNREAD, FALSE);
1259         
1260         if (node)
1261                 summary_select_node(summaryview, node, TRUE, FALSE);
1262         else {
1263                 node = summary_find_next_flagged_msg
1264                         (summaryview, NULL, MSG_UNREAD, FALSE);
1265                 if (node == NULL) {
1266                         AlertValue val = 0;
1267
1268                         switch (prefs_common.next_unread_msg_dialog) {
1269                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1270                                         val = alertpanel(_("No more unread messages"),
1271                                                          _("No unread message found. "
1272                                                            "Go to next folder?"),
1273                                                          _("Yes"), _("No"), NULL);
1274                                         break;
1275                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1276                                         val = G_ALERTDEFAULT;
1277                                         break;
1278                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1279                                         val = G_ALERTOTHER;
1280                                         break;
1281                                 default:
1282                                         debug_print(
1283                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1284                         }
1285
1286                         if (val == G_ALERTDEFAULT) {
1287                                 if (gtk_signal_n_emissions_by_name
1288                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1289                                                 gtk_signal_emit_stop_by_name
1290                                                         (GTK_OBJECT(ctree),
1291                                                          "key_press_event");
1292                                 folderview_select_next_unread(summaryview->folderview);
1293                                 return;
1294                         } 
1295                         else
1296                                 return;
1297                 } else
1298                         summary_select_node(summaryview, node, TRUE, FALSE);
1299
1300         }
1301 }
1302
1303 void summary_select_prev_new(SummaryView *summaryview)
1304 {
1305         GtkCTreeNode *node;
1306
1307         node = summary_find_prev_flagged_msg
1308                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1309
1310         if (!node) {
1311                 AlertValue val;
1312
1313                 val = alertpanel(_("No more new messages"),
1314                                  _("No new message found. "
1315                                    "Search from the end?"),
1316                                  _("Yes"), _("No"), NULL);
1317                 if (val != G_ALERTDEFAULT) return;
1318                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1319                                                      MSG_NEW, FALSE);
1320         }
1321
1322         if (!node)
1323                 alertpanel_notice(_("No new messages."));
1324         else
1325                 summary_select_node(summaryview, node, TRUE, FALSE);
1326 }
1327
1328 void summary_select_next_new(SummaryView *summaryview)
1329 {
1330         GtkCTreeNode *node = summaryview->selected;
1331         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1332
1333         while ((node = summary_find_next_flagged_msg
1334                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1335                 AlertValue val;
1336
1337                 val = alertpanel(_("No more new messages"),
1338                                  _("No new message found. "
1339                                    "Go to next folder?"),
1340                                  _("Yes"), _("Search again"), _("No"));
1341                 if (val == G_ALERTDEFAULT) {
1342                         if (gtk_signal_n_emissions_by_name
1343                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1344                                         gtk_signal_emit_stop_by_name
1345                                                 (GTK_OBJECT(ctree),
1346                                                  "key_press_event");
1347                         folderview_select_next_unread(summaryview->folderview);
1348                         return;
1349                 } else if (val == G_ALERTALTERNATE)
1350                         node = NULL;
1351                 else
1352                         return;
1353         }
1354
1355         if (node)
1356                 summary_select_node(summaryview, node, TRUE, FALSE);
1357 }
1358
1359 void summary_select_prev_marked(SummaryView *summaryview)
1360 {
1361         GtkCTreeNode *node;
1362
1363         node = summary_find_prev_flagged_msg
1364                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1365
1366         if (!node) {
1367                 AlertValue val;
1368
1369                 val = alertpanel(_("No more marked messages"),
1370                                  _("No marked message found. "
1371                                    "Search from the end?"),
1372                                  _("Yes"), _("No"), NULL);
1373                 if (val != G_ALERTDEFAULT) return;
1374                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1375                                                      MSG_MARKED, TRUE);
1376         }
1377
1378         if (!node)
1379                 alertpanel_notice(_("No marked messages."));
1380         else
1381                 summary_select_node(summaryview, node, TRUE, FALSE);
1382 }
1383
1384 void summary_select_next_marked(SummaryView *summaryview)
1385 {
1386         GtkCTreeNode *node;
1387
1388         node = summary_find_next_flagged_msg
1389                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1390
1391         if (!node) {
1392                 AlertValue val;
1393
1394                 val = alertpanel(_("No more marked messages"),
1395                                  _("No marked message found. "
1396                                    "Search from the beginning?"),
1397                                  _("Yes"), _("No"), NULL);
1398                 if (val != G_ALERTDEFAULT) return;
1399                 node = summary_find_next_flagged_msg(summaryview, NULL,
1400                                                      MSG_MARKED, TRUE);
1401         }
1402
1403         if (!node)
1404                 alertpanel_notice(_("No marked messages."));
1405         else
1406                 summary_select_node(summaryview, node, TRUE, FALSE);
1407 }
1408
1409 void summary_select_prev_labeled(SummaryView *summaryview)
1410 {
1411         GtkCTreeNode *node;
1412
1413         node = summary_find_prev_flagged_msg
1414                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1415
1416         if (!node) {
1417                 AlertValue val;
1418
1419                 val = alertpanel(_("No more labeled messages"),
1420                                  _("No labeled message found. "
1421                                    "Search from the end?"),
1422                                  _("Yes"), _("No"), NULL);
1423                 if (val != G_ALERTDEFAULT) return;
1424                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1425                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1426         }
1427
1428         if (!node)
1429                 alertpanel_notice(_("No labeled messages."));
1430         else
1431                 summary_select_node(summaryview, node, TRUE, FALSE);
1432 }
1433
1434 void summary_select_next_labeled(SummaryView *summaryview)
1435 {
1436         GtkCTreeNode *node;
1437
1438         node = summary_find_next_flagged_msg
1439                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1440
1441         if (!node) {
1442                 AlertValue val;
1443
1444                 val = alertpanel(_("No more labeled messages"),
1445                                  _("No labeled message found. "
1446                                    "Search from the beginning?"),
1447                                  _("Yes"), _("No"), NULL);
1448                 if (val != G_ALERTDEFAULT) return;
1449                 node = summary_find_next_flagged_msg(summaryview, NULL,
1450                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1451         }
1452
1453         if (!node)
1454                 alertpanel_notice(_("No labeled messages."));
1455         else
1456                 summary_select_node(summaryview, node, TRUE, FALSE);
1457 }
1458
1459 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1460 {
1461         GtkCTreeNode *node;
1462
1463         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1464         summary_select_node(summaryview, node, FALSE, TRUE);
1465 }
1466
1467 /**
1468  * summary_select_node:
1469  * @summaryview: Summary view.
1470  * @node: Summary tree node.
1471  * @display_msg: TRUE to display the selected message.
1472  * @do_refresh: TRUE to refresh the widget.
1473  *
1474  * Select @node (bringing it into view by scrolling and expanding its
1475  * thread, if necessary) and unselect all others.  If @display_msg is
1476  * TRUE, display the corresponding message in the message view.
1477  * If @do_refresh is TRUE, the widget is refreshed.
1478  **/
1479 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1480                          gboolean display_msg, gboolean do_refresh)
1481 {
1482         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1483
1484         if (node) {
1485                 gtkut_ctree_expand_parent_all(ctree, node);
1486                 if (do_refresh) {
1487                         GTK_EVENTS_FLUSH();
1488                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1489                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1490                 }
1491                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1492                 if (display_msg && summaryview->displayed == node)
1493                         summaryview->displayed = NULL;
1494                 summaryview->display_msg = display_msg;
1495                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1496         }
1497 }
1498
1499 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1500 {
1501         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1502         MsgInfo *msginfo;
1503
1504         if (!node)
1505                 return 0;
1506         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1507         return msginfo->msgnum;
1508 }
1509
1510 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1511                                            GtkCTreeNode *current_node)
1512 {
1513         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1514         GtkCTreeNode *node;
1515         MsgInfo *msginfo;
1516
1517         if (current_node)
1518                 node = current_node;
1519         else
1520                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1521
1522         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1523                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1524                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1525         }
1526
1527         return node;
1528 }
1529
1530 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1531                                            GtkCTreeNode *current_node)
1532 {
1533         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1534         GtkCTreeNode *node;
1535         MsgInfo *msginfo;
1536
1537         if (current_node)
1538                 node = current_node;
1539         else
1540                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1541
1542         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1543                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1544                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1545         }
1546
1547         return node;
1548 }
1549
1550 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1551                                                    GtkCTreeNode *current_node,
1552                                                    MsgPermFlags flags,
1553                                                    gboolean start_from_prev)
1554 {
1555         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1556         GtkCTreeNode *node;
1557         MsgInfo *msginfo;
1558
1559         if (current_node) {
1560                 if (start_from_prev)
1561                         node = GTK_CTREE_NODE_PREV(current_node);
1562                 else
1563                         node = current_node;
1564         } else
1565                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1566
1567         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1568                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1569                 if ((msginfo->flags.perm_flags & flags) != 0) break;
1570         }
1571
1572         return node;
1573 }
1574
1575 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1576                                                    GtkCTreeNode *current_node,
1577                                                    MsgPermFlags flags,
1578                                                    gboolean start_from_next)
1579 {
1580         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1581         GtkCTreeNode *node;
1582         MsgInfo *msginfo;
1583
1584         if (current_node) {
1585                 if (start_from_next)
1586                         node = gtkut_ctree_node_next(ctree, current_node);
1587                 else
1588                         node = current_node;
1589         } else
1590                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1591
1592         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1593                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1594                 /* Find msg with matching flags but ignore messages with
1595                    ignore flags, if searching for new or unread messages */
1596                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1597                         ((msginfo->flags.perm_flags & flags) != 0))
1598                         break;
1599         }
1600
1601         return node;
1602 }
1603
1604 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1605                                                 guint msgnum)
1606 {
1607         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1608         GtkCTreeNode *node;
1609         MsgInfo *msginfo;
1610
1611         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1612
1613         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1614                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1615                 if (msginfo->msgnum == msgnum) break;
1616         }
1617
1618         return node;
1619 }
1620
1621 static guint attract_hash_func(gconstpointer key)
1622 {
1623         gchar *str;
1624         gchar *p;
1625         guint h;
1626
1627         Xstrdup_a(str, (const gchar *)key, return 0);
1628         trim_subject(str);
1629
1630         p = str;
1631         h = *p;
1632
1633         if (h) {
1634                 for (p += 1; *p != '\0'; p++)
1635                         h = (h << 5) - h + *p;
1636         }
1637
1638         return h;
1639 }
1640
1641 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1642 {
1643         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1644 }
1645
1646 void summary_attract_by_subject(SummaryView *summaryview)
1647 {
1648         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1649         GtkCList *clist = GTK_CLIST(ctree);
1650         GtkCTreeNode *src_node;
1651         GtkCTreeNode *dst_node, *sibling;
1652         GtkCTreeNode *tmp;
1653         MsgInfo *src_msginfo, *dst_msginfo;
1654         GHashTable *subject_table;
1655
1656         debug_print("Attracting messages by subject...");
1657         STATUSBAR_PUSH(summaryview->mainwin,
1658                        _("Attracting messages by subject..."));
1659
1660         main_window_cursor_wait(summaryview->mainwin);
1661         gtk_clist_freeze(clist);
1662
1663         subject_table = g_hash_table_new(attract_hash_func,
1664                                          attract_compare_func);
1665
1666         for (src_node = GTK_CTREE_NODE(clist->row_list);
1667              src_node != NULL;
1668              src_node = tmp) {
1669                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1670                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1671                 if (!src_msginfo) continue;
1672                 if (!src_msginfo->subject) continue;
1673
1674                 /* find attracting node */
1675                 dst_node = g_hash_table_lookup(subject_table,
1676                                                src_msginfo->subject);
1677
1678                 if (dst_node) {
1679                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1680
1681                         /* if the time difference is more than 20 days,
1682                            don't attract */
1683                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1684                             > 60 * 60 * 24 * 20)
1685                                 continue;
1686
1687                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1688                         if (src_node != sibling)
1689                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1690                 }
1691
1692                 g_hash_table_insert(subject_table,
1693                                     src_msginfo->subject, src_node);
1694         }
1695
1696         g_hash_table_destroy(subject_table);
1697
1698         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1699
1700         gtk_clist_thaw(clist);
1701
1702         debug_print("done.\n");
1703         STATUSBAR_POP(summaryview->mainwin);
1704
1705         main_window_cursor_normal(summaryview->mainwin);
1706 }
1707
1708 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1709                                       gpointer data)
1710 {
1711         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1712
1713         if (msginfo)
1714                 procmsg_msginfo_free(msginfo);
1715 }
1716
1717 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1718                                    gpointer data)
1719 {
1720         SummaryView *summaryview = data;
1721         MsgInfo *msginfo;
1722
1723         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1724
1725         if (MSG_IS_NEWS(msginfo->flags))
1726                 news_flag_crosspost(msginfo);
1727
1728         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1729                 summaryview->newmsgs++;
1730         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1731                 summaryview->unread++;
1732         if (MSG_IS_DELETED(msginfo->flags))
1733                 summaryview->deleted++;
1734
1735         summaryview->messages++;
1736         summaryview->total_size += msginfo->size;
1737
1738         summary_set_row_marks(summaryview, node);
1739 }
1740
1741 static void summary_update_status(SummaryView *summaryview)
1742 {
1743         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1744         GtkCTreeNode *node;
1745         MsgInfo *msginfo;
1746
1747         summaryview->newmsgs = summaryview->unread =
1748         summaryview->messages = summaryview->total_size =
1749         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1750
1751         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1752              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1753                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1754
1755                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1756                         summaryview->newmsgs++;
1757                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1758                         summaryview->unread++;
1759                 if (MSG_IS_DELETED(msginfo->flags))
1760                         summaryview->deleted++;
1761                 if (MSG_IS_MOVE(msginfo->flags))
1762                         summaryview->moved++;
1763                 if (MSG_IS_COPY(msginfo->flags))
1764                         summaryview->copied++;
1765                 summaryview->messages++;
1766                 summaryview->total_size += msginfo->size;
1767         }
1768 }
1769
1770 static void summary_status_show(SummaryView *summaryview)
1771 {
1772         gchar *str;
1773         gchar *del, *mv, *cp;
1774         gchar *sel;
1775         gchar *spc;
1776         gchar *itstr;
1777         GList *rowlist, *cur;
1778         guint n_selected = 0;
1779         off_t sel_size = 0;
1780         MsgInfo *msginfo;
1781
1782         if (!summaryview->folder_item) {
1783                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1784                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1785                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1786                 return;
1787         }
1788
1789         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1790         for (cur = rowlist; cur != NULL; cur = cur->next) {
1791                 msginfo = gtk_ctree_node_get_row_data
1792                         (GTK_CTREE(summaryview->ctree),
1793                          GTK_CTREE_NODE(cur->data));
1794                 sel_size += msginfo->size;
1795                 n_selected++;
1796         }
1797
1798         if (summaryview->folder_item->folder->type == F_NEWS &&
1799             prefs_common.ng_abbrev_len < strlen(summaryview->folder_item->path)) {
1800                 gchar *group;
1801                 group = get_abbrev_newsgroup_name
1802                         (g_basename(summaryview->folder_item->path));
1803                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), group);
1804                 g_free(group);
1805         } else {
1806                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1807                               summaryview->folder_item->path);
1808         }
1809
1810         if (summaryview->deleted)
1811                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1812         else
1813                 del = g_strdup("");
1814         if (summaryview->moved)
1815                 mv = g_strdup_printf(_("%s%d moved"),
1816                                      summaryview->deleted ? _(", ") : "",
1817                                      summaryview->moved);
1818         else
1819                 mv = g_strdup("");
1820         if (summaryview->copied)
1821                 cp = g_strdup_printf(_("%s%d copied"),
1822                                      summaryview->deleted ||
1823                                      summaryview->moved ? _(", ") : "",
1824                                      summaryview->copied);
1825         else
1826                 cp = g_strdup("");
1827
1828         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1829                 spc = "    ";
1830         else
1831                 spc = "";
1832
1833         if (n_selected) {
1834                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1835                 if (n_selected == 1)
1836                         itstr = g_strdup(_(" item selected"));
1837                 else
1838                         itstr = g_strdup(_(" items selected"));
1839         } else {
1840                 sel = g_strdup("");
1841                 itstr = g_strdup("");
1842         }
1843                 
1844         str = g_strconcat(n_selected ? itos(n_selected) : "",
1845                                         itstr, sel, spc, del, mv, cp, NULL);
1846         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1847         g_free(str);
1848         g_free(sel);
1849         g_free(del);
1850         g_free(mv);
1851         g_free(cp);
1852         g_free(itstr);
1853
1854         if (FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1855                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1856                                       summaryview->newmsgs,
1857                                       summaryview->unread,
1858                                       summaryview->messages,
1859                                       to_human_readable(summaryview->total_size));
1860         } else {
1861                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1862                                       summaryview->newmsgs,
1863                                       summaryview->unread,
1864                                       summaryview->messages);
1865         }
1866         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1867         g_free(str);
1868 }
1869
1870 static void summary_set_column_titles(SummaryView *summaryview)
1871 {
1872         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1873         GtkWidget *hbox;
1874         GtkWidget *label;
1875         GtkWidget *arrow;
1876         gint pos;
1877         const gchar *title;
1878         SummaryColumnType type;
1879         gboolean single_char;
1880         GtkJustification justify;
1881
1882         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1883                 SORT_BY_MARK,
1884                 SORT_BY_UNREAD,
1885                 SORT_BY_MIME,
1886                 SORT_BY_SUBJECT,
1887                 SORT_BY_FROM,
1888                 SORT_BY_DATE,
1889                 SORT_BY_SIZE,
1890                 SORT_BY_NUMBER,
1891                 SORT_BY_SCORE,
1892                 SORT_BY_LOCKED
1893         };
1894
1895         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1896                 type = summaryview->col_state[pos].type;
1897
1898                 /* CLAWS: mime and unread are single char headers */
1899                 single_char = (type == S_COL_MIME || type == S_COL_UNREAD);
1900                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1901                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1902
1903                 switch (type) {
1904                 case S_COL_SUBJECT:
1905                 case S_COL_FROM:
1906                 case S_COL_DATE:
1907                 case S_COL_NUMBER:
1908                         if (prefs_common.trans_hdr)
1909                                 title = gettext(col_label[type]);
1910                         else
1911                                 title = col_label[type];
1912                         break;
1913                 /* CLAWS: dummies for mark and locked headers */        
1914                 case S_COL_MARK:        
1915                 case S_COL_LOCKED:
1916                         title = "";
1917                         break;
1918                 default:
1919                         title = gettext(col_label[type]);
1920                 }
1921
1922                 if (type == S_COL_MIME) {
1923                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1924                         gtk_widget_show(label);
1925                         gtk_clist_set_column_widget(clist, pos, label);
1926                         continue;
1927                 }
1928                 if (single_char) {
1929                         gtk_clist_set_column_title(clist, pos, title);
1930                         continue;
1931                 }
1932
1933                 /* CLAWS: changed so that locked and mark headers
1934                  * show a pixmap instead of single character */
1935                 hbox  = gtk_hbox_new(FALSE, 4);
1936                 
1937                 if (type == S_COL_LOCKED)
1938                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1939                 else if (type == S_COL_MARK) 
1940                         label = gtk_pixmap_new(markxpm, markxpmmask);
1941                 else 
1942                         label = gtk_label_new(title);
1943                 
1944                 if (justify == GTK_JUSTIFY_RIGHT)
1945                         gtk_box_pack_end(GTK_BOX(hbox), label,
1946                                          FALSE, FALSE, 0);
1947                 else
1948                         gtk_box_pack_start(GTK_BOX(hbox), label,
1949                                            FALSE, FALSE, 0);
1950
1951                 if (summaryview->sort_key == sort_by[type]) {
1952                         arrow = gtk_arrow_new
1953                                 (summaryview->sort_type == SORT_ASCENDING
1954                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1955                                  GTK_SHADOW_IN);
1956                         if (justify == GTK_JUSTIFY_RIGHT)
1957                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1958                                                    FALSE, FALSE, 0);
1959                         else
1960                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1961                                                  FALSE, FALSE, 0);
1962                 }
1963
1964                 gtk_widget_show_all(hbox);
1965                 gtk_clist_set_column_widget(clist, pos, hbox);
1966         }
1967 }
1968
1969 void summary_sort(SummaryView *summaryview,
1970                   FolderSortKey sort_key, FolderSortType sort_type)
1971 {
1972         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1973         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1974         GtkCListCompareFunc cmp_func = NULL;
1975
1976         switch (sort_key) {
1977         case SORT_BY_MARK:
1978                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
1979                 break;
1980         case SORT_BY_UNREAD:
1981                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_unread;
1982                 break;
1983         case SORT_BY_MIME:
1984                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
1985                 break;
1986         case SORT_BY_NUMBER:
1987                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1988                 break;
1989         case SORT_BY_SIZE:
1990                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1991                 break;
1992         case SORT_BY_DATE:
1993                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1994                 break;
1995         case SORT_BY_FROM:
1996                 if (summaryview->folder_item && summaryview->folder_item->stype != F_OUTBOX)
1997                         cmp_func = (GtkCListCompareFunc) summary_cmp_by_from;
1998                 else                    
1999                         cmp_func = (GtkCListCompareFunc) summary_cmp_by_to;
2000                 break;
2001         case SORT_BY_SUBJECT:
2002                 if (summaryview->simplify_subject_preg)
2003                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2004                 else
2005                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2006                 break;
2007         case SORT_BY_SCORE:
2008                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2009                 break;
2010         case SORT_BY_LOCKED:
2011                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2012                 break;
2013         case SORT_BY_LABEL:
2014                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2015                 break;
2016         case SORT_BY_NONE:
2017                 cmp_func = NULL;
2018                 return;
2019         default:
2020                 return;
2021         }
2022
2023         summaryview->sort_key = sort_key;
2024         summaryview->sort_type = sort_type;
2025
2026         summary_set_column_titles(summaryview);
2027         summary_set_menu_sensitive(summaryview);
2028         if(cmp_func != NULL) {
2029                 debug_print("Sorting summary...");
2030                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2031
2032                 main_window_cursor_wait(summaryview->mainwin);
2033
2034                 gtk_clist_set_compare_func(clist, cmp_func);
2035
2036                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2037
2038                 gtk_sctree_sort_recursive(ctree, NULL);
2039
2040                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2041
2042                 main_window_cursor_normal(summaryview->mainwin);
2043
2044                 debug_print("done.\n");
2045                 STATUSBAR_POP(summaryview->mainwin);
2046         }
2047 }
2048
2049 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2050                                    GtkCTreeNode *cnode, gpointer data)
2051 {
2052         SummaryView *summaryview = (SummaryView *)data;
2053         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2054         gchar *text[N_SUMMARY_COLS];
2055         gint *col_pos = summaryview->col_pos;
2056         const gchar *msgid = msginfo->msgid;
2057         GHashTable *msgid_table = summaryview->msgid_table;
2058
2059         summary_set_header(summaryview, text, msginfo);
2060
2061         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2062                                 NULL, NULL, NULL, NULL, FALSE,
2063                                 gnode->parent->parent ? TRUE : FALSE);
2064 #define SET_TEXT(col) \
2065         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2066                                 text[col_pos[col]])
2067
2068         SET_TEXT(S_COL_NUMBER);
2069         SET_TEXT(S_COL_SCORE);
2070         SET_TEXT(S_COL_SIZE);
2071         SET_TEXT(S_COL_DATE);
2072         SET_TEXT(S_COL_FROM);
2073         SET_TEXT(S_COL_SUBJECT);
2074
2075 #undef SET_TEXT
2076
2077         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2078         summary_set_marks_func(ctree, cnode, summaryview);
2079
2080         if (msgid && msgid[0] != '\0')
2081                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2082
2083         return TRUE;
2084 }
2085
2086 static void summary_set_ctree_from_list(SummaryView *summaryview,
2087                                         GSList *mlist)
2088 {
2089         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2090         MsgInfo *msginfo;
2091                 GtkCTreeNode *node = NULL;
2092         GHashTable *msgid_table;
2093         GHashTable *subject_table;
2094         GSList * cur;
2095         
2096         if (!mlist) return;
2097
2098         debug_print("\tSetting summary from message data...");
2099         STATUSBAR_PUSH(summaryview->mainwin,
2100                        _("Setting summary from message data..."));
2101         gdk_flush();
2102
2103         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2104         summaryview->msgid_table = msgid_table;
2105         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2106         summaryview->subject_table = subject_table;
2107
2108         if (prefs_common.use_addr_book)
2109                 start_address_completion();
2110         
2111         for (cur = mlist ; cur != NULL; cur = cur->next) {
2112                 msginfo = (MsgInfo *)cur->data;
2113                 msginfo->threadscore = msginfo->score;
2114         }
2115
2116         if (global_scoring || summaryview->folder_item->prefs->scoring) {
2117                 summaryview->important_score = prefs_common.important_score;
2118                 if (summaryview->folder_item->prefs->important_score >
2119                     summaryview->important_score)
2120                         summaryview->important_score =
2121                                 summaryview->folder_item->prefs->important_score;
2122         }
2123
2124         if (summaryview->threaded) {
2125                 GNode *root, *gnode;
2126
2127                 root = procmsg_get_thread_tree(mlist);
2128
2129                 for (gnode = root->children; gnode != NULL;
2130                      gnode = gnode->next) {
2131                         node = gtk_ctree_insert_gnode
2132                                 (ctree, NULL, node, gnode,
2133                                  summary_insert_gnode_func, summaryview);
2134                 }
2135
2136                 g_node_destroy(root);
2137
2138                 summary_thread_init(summaryview);
2139         } else {
2140                 gchar *text[N_SUMMARY_COLS];
2141
2142                 mlist = g_slist_reverse(mlist);
2143                 for (; mlist != NULL; mlist = mlist->next) {
2144                         msginfo = (MsgInfo *)mlist->data;
2145
2146                         summary_set_header(summaryview, text, msginfo);
2147
2148                         node = gtk_ctree_insert_node
2149                                 (ctree, NULL, node, text, 2,
2150                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2151                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2152                         summary_set_marks_func(ctree, node, summaryview);
2153
2154                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2155                                 g_hash_table_insert(msgid_table,
2156                                                     msginfo->msgid, node);
2157
2158                         subject_table_insert(subject_table,
2159                                              msginfo->subject,
2160                                              node);
2161                 }
2162                 mlist = g_slist_reverse(mlist);
2163         }
2164
2165         if (prefs_common.enable_hscrollbar &&
2166             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2167                 gint optimal_width;
2168
2169                 optimal_width = gtk_clist_optimal_column_width
2170                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2171                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2172                                            summaryview->col_pos[S_COL_SUBJECT],
2173                                            optimal_width);
2174         }
2175
2176         if (prefs_common.use_addr_book)
2177                 end_address_completion();
2178
2179         debug_print("done.\n");
2180         STATUSBAR_POP(summaryview->mainwin);
2181         if (debug_mode) {
2182                 debug_print("\tmsgid hash table size = %d\n",
2183                             g_hash_table_size(msgid_table));
2184                 debug_print("\tsubject hash table size = %d\n",
2185                             g_hash_table_size(subject_table));
2186         }
2187 }
2188
2189 static gchar *summary_complete_address(const gchar *addr)
2190 {
2191         gint count;
2192         gchar *res, *tmp, *email_addr;
2193
2194         Xstrdup_a(email_addr, addr, return NULL);
2195         extract_address(email_addr);
2196         g_return_val_if_fail(*email_addr, NULL);
2197
2198         /*
2199          * completion stuff must be already initialized
2200          */
2201         res = NULL;
2202         if (1 < (count = complete_address(email_addr))) {
2203                 tmp = get_complete_address(1);
2204                 res = procheader_get_fromname(tmp);
2205                 g_free(tmp);    
2206         }
2207         
2208         return res;
2209 }
2210
2211 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2212                                MsgInfo *msginfo)
2213 {
2214         static gchar date_modified[80];
2215         static gchar *to = NULL;
2216         static gchar col_score[11];
2217         static gchar buf[BUFFSIZE];
2218         gint *col_pos = summaryview->col_pos;
2219
2220         text[col_pos[S_COL_MARK]]   = NULL;
2221         text[col_pos[S_COL_UNREAD]] = NULL;
2222         text[col_pos[S_COL_MIME]]   = NULL;
2223         text[col_pos[S_COL_LOCKED]] = NULL;
2224         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2225         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2226         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2227
2228         if (msginfo->date_t) {
2229                 procheader_date_get_localtime(date_modified,
2230                                               sizeof(date_modified),
2231                                               msginfo->date_t);
2232                 text[col_pos[S_COL_DATE]] = date_modified;
2233         } else if (msginfo->date)
2234                 text[col_pos[S_COL_DATE]] = msginfo->date;
2235         else
2236                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2237
2238         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2239                 _("(No From)");
2240         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2241             !MSG_IS_NEWS(msginfo->flags)) {
2242                 gchar *addr = NULL;
2243
2244                 Xstrdup_a(addr, msginfo->from, return);
2245                 extract_address(addr);
2246
2247                 if (prefs_common.use_addr_book) {
2248                         if (account_find_from_address(addr)) {
2249                                 addr = summary_complete_address(msginfo->to);
2250                                 g_free(to);
2251                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2252                                 text[col_pos[S_COL_FROM]] = to;
2253                                 g_free(addr);
2254                         }
2255                 } else {
2256                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2257                                 g_free(to);
2258                                 to = g_strconcat("-->", msginfo->to, NULL);
2259                                 text[col_pos[S_COL_FROM]] = to;
2260                         }
2261                 }
2262         }
2263
2264         /*
2265          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2266          * checking whether the above block (which handles the special case of
2267          * the --> in sent boxes) was executed.
2268          */
2269         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2270                 gchar *from = summary_complete_address(msginfo->from);
2271                 if (from) {
2272                         g_free(to);
2273                         to = from;
2274                         text[col_pos[S_COL_FROM]] = to;
2275                 }                       
2276         }
2277
2278         if (summaryview->simplify_subject_preg != NULL)
2279                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2280                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2281                                         summaryview->simplify_subject_preg) : 
2282                         _("(No Subject)");
2283         else 
2284                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2285                         _("(No Subject)");
2286 }
2287
2288 #define CHANGE_FLAGS(msginfo) \
2289 { \
2290 if (msginfo->folder->folder->change_flags != NULL) \
2291 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2292                                       msginfo->folder, \
2293                                       msginfo); \
2294 }
2295
2296 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2297 {
2298         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2299 }
2300
2301 static void summary_display_msg_full(SummaryView *summaryview,
2302                                      GtkCTreeNode *row,
2303                                      gboolean new_window, gboolean all_headers)
2304 {
2305         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2306         MsgInfo *msginfo;
2307         MsgFlags flags;
2308         gchar *filename;
2309
2310         if (!new_window && summaryview->displayed == row) return;
2311         g_return_if_fail(row != NULL);
2312
2313         if (summary_is_locked(summaryview)) return;
2314         summary_lock(summaryview);
2315
2316         STATUSBAR_POP(summaryview->mainwin);
2317         GTK_EVENTS_FLUSH();
2318
2319         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2320
2321         filename = procmsg_get_message_file(msginfo);
2322         if (!filename) {
2323                 summary_unlock(summaryview);
2324                 return;
2325         }
2326         g_free(filename);
2327
2328         if (new_window || !prefs_common.mark_as_read_on_new_window) {
2329                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2330                         summaryview->newmsgs--;
2331                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2332                         summaryview->unread--;
2333                 if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2334                         procmsg_msginfo_unset_flags
2335                                 (msginfo, MSG_NEW | MSG_UNREAD, 0);
2336                         summary_set_row_marks(summaryview, row);
2337                         gtk_clist_thaw(GTK_CLIST(ctree));
2338                         summary_status_show(summaryview);
2339                         
2340                         flags = msginfo->flags;
2341                 }
2342         }
2343
2344         if (new_window) {
2345                 MessageView *msgview;
2346
2347                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2348                 messageview_show(msgview, msginfo, all_headers);
2349         } else {
2350                 MessageView *msgview;
2351
2352                 msgview = summaryview->messageview;
2353
2354                 summaryview->displayed = row;
2355                 if (!messageview_is_visible(msgview))
2356                         main_window_toggle_message_view(summaryview->mainwin);
2357                 messageview_show(msgview, msginfo, all_headers);
2358                 if (msgview->type == MVIEW_TEXT ||
2359                     (msgview->type == MVIEW_MIME &&
2360                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2361                       gtk_notebook_get_current_page
2362                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2363                         gtk_widget_grab_focus(summaryview->ctree);
2364                 GTK_EVENTS_FLUSH();
2365                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2366         }
2367
2368         summary_set_menu_sensitive(summaryview);
2369         toolbar_set_sensitive(summaryview->mainwin);
2370
2371         summary_unlock(summaryview);
2372 }
2373
2374 void summary_display_msg_selected(SummaryView *summaryview,
2375                                   gboolean all_headers)
2376 {
2377         if (summary_is_locked(summaryview)) return;
2378         summaryview->displayed = NULL;
2379         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2380                                  all_headers);
2381 }
2382
2383 void summary_redisplay_msg(SummaryView *summaryview)
2384 {
2385         GtkCTreeNode *node;
2386
2387         if (summaryview->displayed) {
2388                 node = summaryview->displayed;
2389                 summaryview->displayed = NULL;
2390                 summary_display_msg(summaryview, node);
2391         }
2392 }
2393
2394 void summary_open_msg(SummaryView *summaryview)
2395 {
2396         if (!summaryview->selected) return;
2397
2398         summary_display_msg_full(summaryview, summaryview->selected,
2399                                  TRUE, FALSE);
2400 }
2401
2402 void summary_view_source(SummaryView * summaryview)
2403 {
2404         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2405         MsgInfo *msginfo;
2406         SourceWindow *srcwin;
2407
2408         if (!summaryview->selected) return;
2409
2410         srcwin = source_window_create();
2411         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2412         source_window_show_msg(srcwin, msginfo);
2413         source_window_show(srcwin);
2414 }
2415
2416 void summary_reedit(SummaryView *summaryview)
2417 {
2418         MsgInfo *msginfo;
2419
2420         if (!summaryview->selected) return;
2421         if (!summaryview->folder_item) return;
2422         if (summaryview->folder_item->stype != F_OUTBOX &&
2423             summaryview->folder_item->stype != F_DRAFT  &&
2424             summaryview->folder_item->stype != F_QUEUE) return;
2425
2426         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2427                                               summaryview->selected);
2428         if (!msginfo) return;
2429
2430         compose_reedit(msginfo);
2431 }
2432
2433 void summary_step(SummaryView *summaryview, GtkScrollType type)
2434 {
2435         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2436         GtkCTreeNode *node;
2437
2438         if (summary_is_locked(summaryview)) return;
2439
2440         if (type == GTK_SCROLL_STEP_FORWARD) {
2441                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2442                 if (node)
2443                         gtkut_ctree_expand_parent_all(ctree, node);
2444                 else
2445                         return;
2446         } else {
2447                 if (summaryview->selected) {
2448                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2449                         if (!node) return;
2450                 }
2451         }
2452
2453         if (messageview_is_visible(summaryview->messageview))
2454                 summaryview->display_msg = TRUE;
2455
2456         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2457                                 type, 0.0);
2458         
2459         if(summaryview->selected) {
2460                 gtk_sctree_reanchor (GTK_SCTREE(ctree), summaryview->selected);
2461         }
2462
2463
2464 }
2465
2466 void summary_toggle_view(SummaryView *summaryview)
2467 {
2468         if (!messageview_is_visible(summaryview->messageview) &&
2469             summaryview->selected)
2470                 summary_display_msg(summaryview,
2471                                     summaryview->selected);
2472         else
2473                 main_window_toggle_message_view(summaryview->mainwin);
2474 }
2475
2476 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2477                                                 GtkCTreeNode *node)
2478 {
2479         MsgInfo *msginfo;
2480
2481         if (node) {
2482                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2483                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2484                         return TRUE;
2485                 node = GTK_CTREE_ROW(node)->children;
2486         } else
2487                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2488
2489         while (node) {
2490                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2491                         return TRUE;
2492                 node = GTK_CTREE_ROW(node)->sibling;
2493         }
2494
2495         return FALSE;
2496 }
2497
2498 static gboolean summary_have_unread_children(SummaryView *summaryview,
2499                                              GtkCTreeNode *node)
2500 {
2501         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2502
2503         if (!node) return FALSE;
2504
2505         node = GTK_CTREE_ROW(node)->children;
2506
2507         while (node) {
2508                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2509                         return TRUE;
2510                 node = GTK_CTREE_ROW(node)->sibling;
2511         }
2512
2513         return FALSE;
2514 }
2515
2516 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2517 {
2518         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2519         GtkStyle *style = NULL;
2520         MsgInfo *msginfo;
2521         MsgFlags flags;
2522         gint *col_pos = summaryview->col_pos;
2523
2524         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2525         if (!msginfo) return;
2526
2527         flags = msginfo->flags;
2528
2529         gtk_ctree_node_set_foreground(ctree, row, NULL);
2530
2531         /* set new/unread column */
2532         if (MSG_IS_IGNORE_THREAD(flags)) {
2533                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2534                                           ignorethreadxpm, ignorethreadxpmmask);
2535         } else if (MSG_IS_NEW(flags)) {
2536                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2537                                           newxpm, newxpmmask);
2538         } else if (MSG_IS_UNREAD(flags)) {
2539                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2540                                           unreadxpm, unreadxpmmask);
2541         } else if (MSG_IS_REPLIED(flags)) {
2542                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2543                                           repliedxpm, repliedxpmmask);
2544         } else if (MSG_IS_FORWARDED(flags)) {
2545                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2546                                           forwardedxpm, forwardedxpmmask);
2547         } else {
2548                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2549                                         NULL);
2550         }
2551
2552         if (prefs_common.bold_unread &&
2553             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2554              (!GTK_CTREE_ROW(row)->expanded &&
2555               GTK_CTREE_ROW(row)->children &&
2556               summary_have_unread_children(summaryview, row))))
2557                 style = bold_style;
2558
2559         /* set mark column */
2560         if (MSG_IS_DELETED(flags)) {
2561                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2562                                           deletedxpm, deletedxpmmask);
2563                 if (style)
2564                         style = bold_deleted_style;
2565                 else {
2566                         style = small_deleted_style;
2567                 }
2568                         gtk_ctree_node_set_foreground
2569                                 (ctree, row, &summaryview->color_dim);
2570         } else if (MSG_IS_MARKED(flags)) {
2571                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2572                                           markxpm, markxpmmask);
2573         } else if (MSG_IS_MOVE(flags)) {
2574                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2575                 if (style)
2576                         style = bold_marked_style;
2577                 else {
2578                         style = small_marked_style;
2579                 }
2580                         gtk_ctree_node_set_foreground
2581                                 (ctree, row, &summaryview->color_marked);
2582         } else if (MSG_IS_COPY(flags)) {
2583                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2584                 if (style)
2585                         style = bold_marked_style;
2586                 else {
2587                         style = small_marked_style;
2588                 }
2589                         gtk_ctree_node_set_foreground
2590                                 (ctree, row, &summaryview->color_marked);
2591         }
2592         else if ((global_scoring ||
2593                   summaryview->folder_item->prefs->scoring) &&
2594                  (msginfo->score >= summaryview->important_score) &&
2595                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2596                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2597                 gtk_ctree_node_set_foreground(ctree, row,
2598                                               &summaryview->color_important);
2599         } else {
2600                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2601         }
2602
2603         if (MSG_IS_LOCKED(flags)) {
2604                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2605                                           lockedxpm, lockedxpmmask);
2606         }
2607         else {
2608                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2609         }
2610
2611         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2612                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2613                                           clipkeyxpm, clipkeyxpmmask);
2614         } else if (MSG_IS_ENCRYPTED(flags)) {
2615                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2616                                           keyxpm, keyxpmmask);
2617         } else if (MSG_IS_MIME(flags)) {
2618                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2619                                           clipxpm, clipxpmmask);
2620         } else {
2621                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2622         }
2623         if (!style)
2624                 style = small_style;
2625
2626         gtk_ctree_node_set_row_style(ctree, row, style);
2627
2628         if (MSG_GET_COLORLABEL(flags))
2629                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2630 }
2631
2632 void summary_set_marks_selected(SummaryView *summaryview)
2633 {
2634         GList *cur;
2635
2636         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2637              cur = cur->next)
2638                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2639 }
2640
2641 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2642 {
2643         gboolean changed = FALSE;
2644         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2645         MsgInfo *msginfo;
2646
2647         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2648         if (MSG_IS_DELETED(msginfo->flags))
2649                 summaryview->deleted--;
2650         if (MSG_IS_MOVE(msginfo->flags)) {
2651                 summaryview->moved--;
2652                 changed = TRUE;
2653         }
2654         if (MSG_IS_COPY(msginfo->flags)) {
2655                 summaryview->copied--;
2656                 changed = TRUE;
2657         }
2658         if (changed && !prefs_common.immediate_exec) {
2659                 msginfo->to_folder->op_count--;
2660                 if (msginfo->to_folder->op_count == 0)
2661                         folderview_update_item(msginfo->to_folder, 0);
2662         }
2663         msginfo->to_folder = NULL;
2664         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2665         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2666         summary_set_row_marks(summaryview, row);
2667         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2668 }
2669
2670 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2671 {
2672         /* almost verbatim summary_mark_row(); may want a menu action? */
2673         gboolean changed = FALSE;
2674         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2675         MsgInfo *msginfo;
2676
2677         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2678         if (MSG_IS_DELETED(msginfo->flags))
2679                 summaryview->deleted--;
2680         if (MSG_IS_MOVE(msginfo->flags)) {
2681                 summaryview->moved--;
2682                 changed = TRUE;
2683         }
2684         if (MSG_IS_COPY(msginfo->flags)) {
2685                 summaryview->copied--;
2686                 changed = TRUE;
2687         }
2688         if (changed && !prefs_common.immediate_exec) {
2689                 msginfo->to_folder->op_count--;
2690                 if (msginfo->to_folder->op_count == 0)
2691                         folderview_update_item(msginfo->to_folder, 0);
2692         }
2693         msginfo->to_folder = NULL;
2694         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2695         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2696         summary_set_row_marks(summaryview, row);
2697         debug_print("Message %d is locked\n", msginfo->msgnum);
2698 }
2699
2700 void summary_mark(SummaryView *summaryview)
2701 {
2702         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2703         GList *cur;
2704
2705         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2706                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2707
2708         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2709         summary_status_show(summaryview);
2710 }
2711
2712 static void summary_mark_row_as_read(SummaryView *summaryview,
2713                                      GtkCTreeNode *row)
2714 {
2715         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2716         MsgInfo *msginfo;
2717
2718         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2719
2720         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2721                 return;
2722
2723         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2724                 summaryview->newmsgs--;
2725         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2726                 summaryview->unread--;
2727
2728         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2729         summary_set_row_marks(summaryview, row);
2730         debug_print("Message %d is marked as read\n",
2731                 msginfo->msgnum);
2732 }
2733
2734 void summary_mark_as_read(SummaryView *summaryview)
2735 {
2736         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2737         GList *cur;
2738
2739         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2740                 summary_mark_row_as_read(summaryview,
2741                                          GTK_CTREE_NODE(cur->data));
2742
2743         summary_status_show(summaryview);
2744 }
2745
2746 void summary_mark_all_read(SummaryView *summaryview)
2747 {
2748         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2749         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2750         GtkCTreeNode *node;
2751
2752         gtk_clist_freeze(clist);
2753         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2754              node = gtkut_ctree_node_next(ctree, node))
2755                 summary_mark_row_as_read(summaryview, node);
2756         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2757              node = gtkut_ctree_node_next(ctree, node)) {
2758                 if (!GTK_CTREE_ROW(node)->expanded)
2759                         summary_set_row_marks(summaryview, node);
2760         }
2761         gtk_clist_thaw(clist);
2762
2763         summary_status_show(summaryview);
2764 }
2765
2766 static void summary_mark_row_as_unread(SummaryView *summaryview,
2767                                        GtkCTreeNode *row)
2768 {
2769         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2770         MsgInfo *msginfo;
2771
2772         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2773         if (MSG_IS_DELETED(msginfo->flags)) {
2774                 msginfo->to_folder = NULL;
2775                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2776                 summaryview->deleted--;
2777         }
2778
2779         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2780                 summaryview->unread++;
2781
2782         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2783         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2784         debug_print("Message %d is marked as unread\n",
2785                 msginfo->msgnum);
2786
2787         summary_set_row_marks(summaryview, row);
2788 }
2789
2790 void summary_mark_as_unread(SummaryView *summaryview)
2791 {
2792         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2793         GList *cur;
2794
2795         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2796                 summary_mark_row_as_unread(summaryview,
2797                                            GTK_CTREE_NODE(cur->data));
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         }