online, offline and quicksearch pixmaps follow theme changes
[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 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
769 {
770         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
771         GtkCTreeNode *node;
772         GSList *mlist = NULL;
773         gchar *buf;
774         gboolean is_refresh;
775         guint selected_msgnum = 0;
776         guint displayed_msgnum = 0;
777         GSList *cur;
778
779         if (summary_is_locked(summaryview)) return FALSE;
780
781         inc_lock();
782         summary_lock(summaryview);
783
784         if (item != summaryview->folder_item) {
785                 /* changing folder, reset search */
786                 gtk_entry_set_text(GTK_ENTRY(summaryview->search_string), "");
787         }
788         
789         STATUSBAR_POP(summaryview->mainwin);
790
791         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
792         if (is_refresh) {
793                 selected_msgnum = summary_get_msgnum(summaryview,
794                                                      summaryview->selected);
795                 displayed_msgnum = summary_get_msgnum(summaryview,
796                                                       summaryview->displayed);
797         }
798
799         /* process the marks if any */
800         if (summaryview->mainwin->lock_count == 0 &&
801             (summaryview->moved > 0 || summaryview->copied > 0)) {
802                 AlertValue val;
803
804                 val = alertpanel(_("Process mark"),
805                                  _("Some marks are left. Process it?"),
806                                  _("Yes"), _("No"), _("Cancel"));
807                 if (G_ALERTDEFAULT == val) {
808                         summary_unlock(summaryview);
809                         summary_execute(summaryview);
810                         summary_lock(summaryview);
811                 } else if (G_ALERTALTERNATE == val) {
812                         /* DO NOTHING */
813                 } else {
814                         summary_unlock(summaryview);
815                         inc_unlock();
816                         return FALSE;
817                 }
818                 folder_update_op_count();
819         }
820         
821         gtk_clist_freeze(GTK_CLIST(ctree));
822
823         summary_clear_list(summaryview);
824         summary_set_column_titles(summaryview);
825         if (!is_refresh)
826                 messageview_clear(summaryview->messageview);
827
828         buf = NULL;
829         if (!item || !item->path || !item->parent || item->no_select ||
830             (item->folder->type == F_MH &&
831              ((buf = folder_item_get_path(item)) == NULL ||
832               change_dir(buf) < 0))) {
833                 g_free(buf);
834                 debug_print("empty folder\n\n");
835                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
836                 if (is_refresh)
837                         messageview_clear(summaryview->messageview);
838                 summary_clear_all(summaryview);
839                 summaryview->folder_item = item;
840                 gtk_clist_thaw(GTK_CLIST(ctree));
841                 summary_unlock(summaryview);
842                 inc_unlock();
843                 return TRUE;
844         }
845         g_free(buf);
846
847         summaryview->folder_item = item;
848         item->opened = TRUE;
849
850         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
851
852         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
853         debug_print("%s\n", buf);
854         STATUSBAR_PUSH(summaryview->mainwin, buf);
855         g_free(buf);
856
857         main_window_cursor_wait(summaryview->mainwin);
858
859 /*
860         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
861
862         !!! NEW !!!
863         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
864         BY OTHER PROGRAMS TO THE FOLDER
865 */
866         mlist = folder_item_get_msg_list(item);
867 #if 0
868         summary_processing(summaryview, mlist);
869 #endif
870         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
871                 MsgInfo * msginfo = (MsgInfo *) cur->data;
872
873                 msginfo->score = score_message(global_scoring, msginfo);
874                 if (msginfo->score != MAX_SCORE &&
875                     msginfo->score != MIN_SCORE) {
876                         msginfo->score += score_message(item->prefs->scoring,
877                                                         msginfo);
878                 }
879         }
880
881         if (summaryview->folder_item->hide_read_msgs) {
882                 GSList *not_killed;
883                 
884                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
885                 not_killed = NULL;
886                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
887                         MsgInfo * msginfo = (MsgInfo *) cur->data;
888                         
889                         if ((MSG_IS_UNREAD(msginfo->flags)
890                              || MSG_IS_MARKED(msginfo->flags)
891                              || MSG_IS_LOCKED(msginfo->flags))
892                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
893                             not_killed = g_slist_append(not_killed, msginfo);
894                         else
895                                 procmsg_msginfo_free(msginfo);
896                 }
897                 g_slist_free(mlist);
898                 mlist = not_killed;
899         } else {
900                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
901         }
902
903         if (strlen(gtk_entry_get_text(GTK_ENTRY(summaryview->search_string))) > 0) {
904                 GSList *not_killed;
905                 gint search_type = GPOINTER_TO_INT(gtk_object_get_user_data(
906                                    GTK_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
907                                    GTK_MENU(summaryview->search_type))))));
908                 gchar *search_string = gtk_entry_get_text(GTK_ENTRY(summaryview->search_string));
909                 gchar *searched_header = NULL;
910                 
911                 not_killed = NULL;
912                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
913                         MsgInfo * msginfo = (MsgInfo *) cur->data;
914
915                         switch (search_type) {
916                         case S_SEARCH_SUBJECT:
917                                 searched_header = msginfo->subject;
918                                 break;
919                         case S_SEARCH_FROM:
920                                 searched_header = msginfo->from;
921                                 break;
922                         case S_SEARCH_TO:
923                                 searched_header = msginfo->to;
924                                 break;
925                         default:
926                                 debug_print("unknown search type (%d)\n", search_type);
927                                 break;
928                         }
929                         if (searched_header && strcasestr(searched_header, search_string) != NULL)
930                                 not_killed = g_slist_append(not_killed, msginfo);
931                         else
932                                 procmsg_msginfo_free(msginfo);
933                 }
934                 g_slist_free(mlist);
935                 mlist = not_killed;
936         }
937         
938         if ((global_scoring || item->prefs->scoring)) {
939                 GSList *not_killed;
940                 gint kill_score;
941
942                 not_killed = NULL;
943                 kill_score = prefs_common.kill_score;
944                 if (item->prefs->kill_score > kill_score)
945                         kill_score = item->prefs->kill_score;
946                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
947                         MsgInfo * msginfo = (MsgInfo *) cur->data;
948
949                         if (msginfo->score > kill_score)
950                                 not_killed = g_slist_append(not_killed, msginfo);
951                         else
952                                 procmsg_msginfo_free(msginfo);
953                 }
954                 g_slist_free(mlist);
955                 mlist = not_killed;
956         }
957
958         STATUSBAR_POP(summaryview->mainwin);
959
960         /* set ctree and hash table from the msginfo list
961            creating thread, and count the number of messages */
962         summary_set_ctree_from_list(summaryview, mlist);
963
964         g_slist_free(mlist);
965
966         if (summaryview->sort_key != SORT_BY_NONE)
967                 summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
968
969         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
970
971         gtk_clist_thaw(GTK_CLIST(ctree));
972
973         if (is_refresh) {
974                 summaryview->displayed =
975                         summary_find_msg_by_msgnum(summaryview,
976                                                    displayed_msgnum);
977                 if (!summaryview->displayed)
978                         messageview_clear(summaryview->messageview);
979                 summary_select_by_msgnum(summaryview, selected_msgnum);
980                 if (!summaryview->selected) {
981                         /* no selected message - select first unread
982                            message, but do not display it */
983                         node = summary_find_next_flagged_msg(summaryview, NULL,
984                                                              MSG_UNREAD, FALSE);
985                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
986                                 node = gtk_ctree_node_nth
987                                         (ctree,
988                                          item->sort_type == SORT_DESCENDING
989                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
990                         summary_select_node(summaryview, node, FALSE, TRUE);
991                 }
992         } else {
993                 /* select first unread message */
994                 if (summaryview->sort_key == SORT_BY_SCORE)
995                         node = summary_find_next_important_score(summaryview,
996                                                                  NULL);
997                 else
998                 node = summary_find_next_flagged_msg(summaryview, NULL,
999                                                      MSG_UNREAD, FALSE);
1000                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1001                         node = gtk_ctree_node_nth
1002                                 (ctree,
1003                                  item->sort_type == SORT_DESCENDING
1004                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1005                 }
1006                 if (prefs_common.open_unread_on_enter) {
1007                         summary_unlock(summaryview);
1008                         summary_select_node(summaryview, node, TRUE, TRUE);
1009                         summary_lock(summaryview);
1010                 } else
1011                         summary_select_node(summaryview, node, FALSE, TRUE);
1012         }
1013
1014         summary_status_show(summaryview);
1015         summary_set_menu_sensitive(summaryview);
1016         toolbar_set_sensitive(summaryview->mainwin);
1017
1018         debug_print("\n");
1019         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1020
1021         main_window_cursor_normal(summaryview->mainwin);
1022         summary_unlock(summaryview);
1023         inc_unlock();
1024
1025         return TRUE;
1026 }
1027
1028 void summary_clear_list(SummaryView *summaryview)
1029 {
1030         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1031         gint optimal_width;
1032
1033         gtk_clist_freeze(clist);
1034
1035         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1036                                 NULL, summary_free_msginfo_func, NULL);
1037
1038         if (summaryview->folder_item) {
1039                 summaryview->folder_item->opened = FALSE;
1040                 summaryview->folder_item = NULL;
1041         }
1042
1043         summaryview->display_msg = FALSE;
1044
1045         summaryview->selected  = NULL;
1046         summaryview->displayed = NULL;
1047         summaryview->newmsgs   = summaryview->unread     = 0;
1048         summaryview->messages  = summaryview->total_size = 0;
1049         summaryview->deleted   = summaryview->moved      = 0;
1050         summaryview->copied    = 0;
1051         if (summaryview->msgid_table) {
1052                 g_hash_table_destroy(summaryview->msgid_table);
1053                 summaryview->msgid_table = NULL;
1054         }
1055         if (summaryview->subject_table) {
1056                 g_hash_table_destroy(summaryview->subject_table);
1057                 summaryview->subject_table = NULL;
1058         }
1059         summaryview->mlist = NULL;
1060         if (summaryview->folder_table) {
1061                 g_hash_table_destroy(summaryview->folder_table);
1062                 summaryview->folder_table = NULL;
1063         }
1064
1065         gtk_clist_clear(clist);
1066         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1067                 optimal_width = gtk_clist_optimal_column_width
1068                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1069                 gtk_clist_set_column_width
1070                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1071                          optimal_width);
1072         }
1073
1074         gtk_clist_thaw(clist);
1075 }
1076
1077 void summary_clear_all(SummaryView *summaryview)
1078 {
1079         summary_clear_list(summaryview);
1080         summary_set_menu_sensitive(summaryview);
1081         toolbar_set_sensitive(summaryview->mainwin);
1082         summary_status_show(summaryview);
1083 }
1084
1085 void summary_lock(SummaryView *summaryview)
1086 {
1087         summaryview->lock_count++;
1088 }
1089
1090 void summary_unlock(SummaryView *summaryview)
1091 {
1092         if (summaryview->lock_count)
1093                 summaryview->lock_count--;
1094 }
1095
1096 gboolean summary_is_locked(SummaryView *summaryview)
1097 {
1098         return summaryview->lock_count > 0;
1099 }
1100
1101 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1102 {
1103         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1104         SummarySelection selection;
1105
1106         if (!clist->row_list)
1107                 selection = SUMMARY_NONE;
1108         else if (!clist->selection)
1109                 selection = SUMMARY_SELECTED_NONE;
1110         else if (!clist->selection->next)
1111                 selection = SUMMARY_SELECTED_SINGLE;
1112         else
1113                 selection = SUMMARY_SELECTED_MULTIPLE;
1114
1115         return selection;
1116 }
1117
1118 static void summary_set_menu_sensitive(SummaryView *summaryview)
1119 {
1120         GtkItemFactory *ifactory = summaryview->popupfactory;
1121         SummarySelection selection;
1122         GtkWidget *menuitem;
1123         gboolean sens;
1124
1125         selection = summary_get_selection_type(summaryview);
1126         main_window_set_menu_sensitive(summaryview->mainwin);
1127
1128         if (selection == SUMMARY_NONE) {
1129                 GtkWidget *submenu;
1130
1131                 submenu = gtk_item_factory_get_widget
1132                         (summaryview->popupfactory, "/Mark");
1133                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1134                 menu_set_insensitive_all
1135                         (GTK_MENU_SHELL(summaryview->popupmenu));
1136                 return;
1137         }
1138
1139         if (summaryview->folder_item->folder->type != F_NEWS)
1140                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1141         else
1142                 menu_set_sensitive(ifactory, "/Move...", FALSE);
1143
1144 #if 0
1145         menu_set_sensitive(ifactory, "/Delete", TRUE);
1146 #endif
1147         menu_set_sensitive(ifactory, "/Copy...", TRUE);
1148         menu_set_sensitive(ifactory, "/Execute", TRUE);
1149
1150         menu_set_sensitive(ifactory, "/Mark", TRUE);
1151         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1152         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1153
1154         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1155         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1156         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1157         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1158         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1159
1160         menu_set_sensitive(ifactory, "/Color label", TRUE);
1161
1162         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1163         menu_set_sensitive(ifactory, "/Reply",                    sens);
1164         menu_set_sensitive(ifactory, "/Reply to",                 sens);
1165         menu_set_sensitive(ifactory, "/Reply to/all",             sens);
1166         menu_set_sensitive(ifactory, "/Reply to/sender",          sens);
1167         menu_set_sensitive(ifactory, "/Reply to/mailing list",    sens);
1168         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1169         menu_set_sensitive(ifactory, "/Redirect",                 sens);
1170
1171         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1172         menu_set_sensitive(ifactory, "/Create filter rule",         sens);
1173
1174         menu_set_sensitive(ifactory, "/View", sens);
1175         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1176         menu_set_sensitive(ifactory, "/View/Source", sens);
1177         menu_set_sensitive(ifactory, "/View/All header", sens);
1178         if (summaryview->folder_item->stype == F_OUTBOX ||
1179             summaryview->folder_item->stype == F_DRAFT  ||
1180             summaryview->folder_item->stype == F_QUEUE)
1181                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1182         else
1183                 menu_set_sensitive(ifactory, "/Re-edit", FALSE);
1184
1185         menu_set_sensitive(ifactory, "/Save as...", TRUE);
1186         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1187
1188         menu_set_sensitive(ifactory, "/Select all", TRUE);
1189         menu_set_sensitive(ifactory, "/Select thread", sens);
1190         if (summaryview->folder_item->folder->account)
1191                 sens = summaryview->folder_item->folder->account->protocol
1192                         == A_NNTP;
1193         else
1194                 sens = FALSE;
1195         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1196         menu_set_sensitive(ifactory, "/Cancel a news message", sens);
1197         menu_set_sensitive(ifactory, "/Delete", !sens);
1198
1199         summary_lock(summaryview);
1200         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1201         gtk_check_menu_item_set_active
1202                 (GTK_CHECK_MENU_ITEM(menuitem),
1203                  summaryview->messageview->textview->show_all_headers);
1204         summary_unlock(summaryview);
1205 }
1206
1207 void summary_select_prev_unread(SummaryView *summaryview)
1208 {
1209         GtkCTreeNode *node;
1210
1211         node = summary_find_prev_flagged_msg
1212                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1213
1214         if (!node) {
1215                 AlertValue val = 0;
1216
1217                 switch (prefs_common.next_unread_msg_dialog) {
1218                         case NEXTUNREADMSGDIALOG_ALWAYS:
1219                                 val = alertpanel(_("No more unread messages"),
1220                                                  _("No unread message found. "
1221                                                    "Search from the end?"),
1222                                                  _("Yes"), _("No"), NULL);
1223                                 break;
1224                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1225                                 val = G_ALERTDEFAULT;
1226                                 break;
1227                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1228                                 val = !G_ALERTDEFAULT;
1229                                 break;
1230                         default:
1231                                 debug_print(
1232                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1233                 }
1234                 if (val != G_ALERTDEFAULT) return;
1235                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1236                                                      MSG_UNREAD, FALSE);
1237         }
1238
1239         if (!node)
1240                 alertpanel_notice(_("No unread messages."));
1241         else
1242                 summary_select_node(summaryview, node, TRUE, FALSE);
1243 }
1244
1245 void summary_select_next_unread(SummaryView *summaryview)
1246 {
1247         GtkCTreeNode *node = summaryview->selected;
1248         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1249
1250         node = summary_find_next_flagged_msg
1251                 (summaryview, node, MSG_UNREAD, FALSE);
1252         
1253         if (node)
1254                 summary_select_node(summaryview, node, TRUE, FALSE);
1255         else {
1256                 node = summary_find_next_flagged_msg
1257                         (summaryview, NULL, MSG_UNREAD, FALSE);
1258                 if (node == NULL) {
1259                         AlertValue val = 0;
1260
1261                         switch (prefs_common.next_unread_msg_dialog) {
1262                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1263                                         val = alertpanel(_("No more unread messages"),
1264                                                          _("No unread message found. "
1265                                                            "Go to next folder?"),
1266                                                          _("Yes"), _("No"), NULL);
1267                                         break;
1268                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1269                                         val = G_ALERTDEFAULT;
1270                                         break;
1271                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1272                                         val = G_ALERTOTHER;
1273                                         break;
1274                                 default:
1275                                         debug_print(
1276                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1277                         }
1278
1279                         if (val == G_ALERTDEFAULT) {
1280                                 if (gtk_signal_n_emissions_by_name
1281                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1282                                                 gtk_signal_emit_stop_by_name
1283                                                         (GTK_OBJECT(ctree),
1284                                                          "key_press_event");
1285                                 folderview_select_next_unread(summaryview->folderview);
1286                                 return;
1287                         } 
1288                         else
1289                                 return;
1290                 } else
1291                         summary_select_node(summaryview, node, TRUE, FALSE);
1292
1293         }
1294 }
1295
1296 void summary_select_prev_new(SummaryView *summaryview)
1297 {
1298         GtkCTreeNode *node;
1299
1300         node = summary_find_prev_flagged_msg
1301                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1302
1303         if (!node) {
1304                 AlertValue val;
1305
1306                 val = alertpanel(_("No more new messages"),
1307                                  _("No new message found. "
1308                                    "Search from the end?"),
1309                                  _("Yes"), _("No"), NULL);
1310                 if (val != G_ALERTDEFAULT) return;
1311                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1312                                                      MSG_NEW, FALSE);
1313         }
1314
1315         if (!node)
1316                 alertpanel_notice(_("No new messages."));
1317         else
1318                 summary_select_node(summaryview, node, TRUE, FALSE);
1319 }
1320
1321 void summary_select_next_new(SummaryView *summaryview)
1322 {
1323         GtkCTreeNode *node = summaryview->selected;
1324         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1325
1326         while ((node = summary_find_next_flagged_msg
1327                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1328                 AlertValue val;
1329
1330                 val = alertpanel(_("No more new messages"),
1331                                  _("No new message found. "
1332                                    "Go to next folder?"),
1333                                  _("Yes"), _("Search again"), _("No"));
1334                 if (val == G_ALERTDEFAULT) {
1335                         if (gtk_signal_n_emissions_by_name
1336                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1337                                         gtk_signal_emit_stop_by_name
1338                                                 (GTK_OBJECT(ctree),
1339                                                  "key_press_event");
1340                         folderview_select_next_unread(summaryview->folderview);
1341                         return;
1342                 } else if (val == G_ALERTALTERNATE)
1343                         node = NULL;
1344                 else
1345                         return;
1346         }
1347
1348         if (node)
1349                 summary_select_node(summaryview, node, TRUE, FALSE);
1350 }
1351
1352 void summary_select_prev_marked(SummaryView *summaryview)
1353 {
1354         GtkCTreeNode *node;
1355
1356         node = summary_find_prev_flagged_msg
1357                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1358
1359         if (!node) {
1360                 AlertValue val;
1361
1362                 val = alertpanel(_("No more marked messages"),
1363                                  _("No marked message found. "
1364                                    "Search from the end?"),
1365                                  _("Yes"), _("No"), NULL);
1366                 if (val != G_ALERTDEFAULT) return;
1367                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1368                                                      MSG_MARKED, TRUE);
1369         }
1370
1371         if (!node)
1372                 alertpanel_notice(_("No marked messages."));
1373         else
1374                 summary_select_node(summaryview, node, TRUE, FALSE);
1375 }
1376
1377 void summary_select_next_marked(SummaryView *summaryview)
1378 {
1379         GtkCTreeNode *node;
1380
1381         node = summary_find_next_flagged_msg
1382                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1383
1384         if (!node) {
1385                 AlertValue val;
1386
1387                 val = alertpanel(_("No more marked messages"),
1388                                  _("No marked message found. "
1389                                    "Search from the beginning?"),
1390                                  _("Yes"), _("No"), NULL);
1391                 if (val != G_ALERTDEFAULT) return;
1392                 node = summary_find_next_flagged_msg(summaryview, NULL,
1393                                                      MSG_MARKED, TRUE);
1394         }
1395
1396         if (!node)
1397                 alertpanel_notice(_("No marked messages."));
1398         else
1399                 summary_select_node(summaryview, node, TRUE, FALSE);
1400 }
1401
1402 void summary_select_prev_labeled(SummaryView *summaryview)
1403 {
1404         GtkCTreeNode *node;
1405
1406         node = summary_find_prev_flagged_msg
1407                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1408
1409         if (!node) {
1410                 AlertValue val;
1411
1412                 val = alertpanel(_("No more labeled messages"),
1413                                  _("No labeled message found. "
1414                                    "Search from the end?"),
1415                                  _("Yes"), _("No"), NULL);
1416                 if (val != G_ALERTDEFAULT) return;
1417                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1418                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1419         }
1420
1421         if (!node)
1422                 alertpanel_notice(_("No labeled messages."));
1423         else
1424                 summary_select_node(summaryview, node, TRUE, FALSE);
1425 }
1426
1427 void summary_select_next_labeled(SummaryView *summaryview)
1428 {
1429         GtkCTreeNode *node;
1430
1431         node = summary_find_next_flagged_msg
1432                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1433
1434         if (!node) {
1435                 AlertValue val;
1436
1437                 val = alertpanel(_("No more labeled messages"),
1438                                  _("No labeled message found. "
1439                                    "Search from the beginning?"),
1440                                  _("Yes"), _("No"), NULL);
1441                 if (val != G_ALERTDEFAULT) return;
1442                 node = summary_find_next_flagged_msg(summaryview, NULL,
1443                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1444         }
1445
1446         if (!node)
1447                 alertpanel_notice(_("No labeled messages."));
1448         else
1449                 summary_select_node(summaryview, node, TRUE, FALSE);
1450 }
1451
1452 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1453 {
1454         GtkCTreeNode *node;
1455
1456         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1457         summary_select_node(summaryview, node, FALSE, TRUE);
1458 }
1459
1460 /**
1461  * summary_select_node:
1462  * @summaryview: Summary view.
1463  * @node: Summary tree node.
1464  * @display_msg: TRUE to display the selected message.
1465  * @do_refresh: TRUE to refresh the widget.
1466  *
1467  * Select @node (bringing it into view by scrolling and expanding its
1468  * thread, if necessary) and unselect all others.  If @display_msg is
1469  * TRUE, display the corresponding message in the message view.
1470  * If @do_refresh is TRUE, the widget is refreshed.
1471  **/
1472 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1473                          gboolean display_msg, gboolean do_refresh)
1474 {
1475         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1476
1477         if (node) {
1478                 gtkut_ctree_expand_parent_all(ctree, node);
1479                 if (do_refresh) {
1480                         GTK_EVENTS_FLUSH();
1481                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1482                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1483                 }
1484                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1485                 if (display_msg && summaryview->displayed == node)
1486                         summaryview->displayed = NULL;
1487                 summaryview->display_msg = display_msg;
1488                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1489         }
1490 }
1491
1492 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1493 {
1494         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1495         MsgInfo *msginfo;
1496
1497         if (!node)
1498                 return 0;
1499         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1500         return msginfo->msgnum;
1501 }
1502
1503 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1504                                            GtkCTreeNode *current_node)
1505 {
1506         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1507         GtkCTreeNode *node;
1508         MsgInfo *msginfo;
1509
1510         if (current_node)
1511                 node = current_node;
1512         else
1513                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1514
1515         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1516                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1517                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1518         }
1519
1520         return node;
1521 }
1522
1523 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1524                                            GtkCTreeNode *current_node)
1525 {
1526         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1527         GtkCTreeNode *node;
1528         MsgInfo *msginfo;
1529
1530         if (current_node)
1531                 node = current_node;
1532         else
1533                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1534
1535         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1536                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1537                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1538         }
1539
1540         return node;
1541 }
1542
1543 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1544                                                    GtkCTreeNode *current_node,
1545                                                    MsgPermFlags flags,
1546                                                    gboolean start_from_prev)
1547 {
1548         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1549         GtkCTreeNode *node;
1550         MsgInfo *msginfo;
1551
1552         if (current_node) {
1553                 if (start_from_prev)
1554                         node = GTK_CTREE_NODE_PREV(current_node);
1555                 else
1556                         node = current_node;
1557         } else
1558                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1559
1560         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1561                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1562                 if ((msginfo->flags.perm_flags & flags) != 0) break;
1563         }
1564
1565         return node;
1566 }
1567
1568 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1569                                                    GtkCTreeNode *current_node,
1570                                                    MsgPermFlags flags,
1571                                                    gboolean start_from_next)
1572 {
1573         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1574         GtkCTreeNode *node;
1575         MsgInfo *msginfo;
1576
1577         if (current_node) {
1578                 if (start_from_next)
1579                         node = gtkut_ctree_node_next(ctree, current_node);
1580                 else
1581                         node = current_node;
1582         } else
1583                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1584
1585         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1586                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1587                 /* Find msg with matching flags but ignore messages with
1588                    ignore flags, if searching for new or unread messages */
1589                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1590                         ((msginfo->flags.perm_flags & flags) != 0))
1591                         break;
1592         }
1593
1594         return node;
1595 }
1596
1597 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1598                                                 guint msgnum)
1599 {
1600         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1601         GtkCTreeNode *node;
1602         MsgInfo *msginfo;
1603
1604         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1605
1606         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1607                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1608                 if (msginfo->msgnum == msgnum) break;
1609         }
1610
1611         return node;
1612 }
1613
1614 static guint attract_hash_func(gconstpointer key)
1615 {
1616         gchar *str;
1617         gchar *p;
1618         guint h;
1619
1620         Xstrdup_a(str, (const gchar *)key, return 0);
1621         trim_subject(str);
1622
1623         p = str;
1624         h = *p;
1625
1626         if (h) {
1627                 for (p += 1; *p != '\0'; p++)
1628                         h = (h << 5) - h + *p;
1629         }
1630
1631         return h;
1632 }
1633
1634 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1635 {
1636         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1637 }
1638
1639 void summary_attract_by_subject(SummaryView *summaryview)
1640 {
1641         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1642         GtkCList *clist = GTK_CLIST(ctree);
1643         GtkCTreeNode *src_node;
1644         GtkCTreeNode *dst_node, *sibling;
1645         GtkCTreeNode *tmp;
1646         MsgInfo *src_msginfo, *dst_msginfo;
1647         GHashTable *subject_table;
1648
1649         debug_print("Attracting messages by subject...");
1650         STATUSBAR_PUSH(summaryview->mainwin,
1651                        _("Attracting messages by subject..."));
1652
1653         main_window_cursor_wait(summaryview->mainwin);
1654         gtk_clist_freeze(clist);
1655
1656         subject_table = g_hash_table_new(attract_hash_func,
1657                                          attract_compare_func);
1658
1659         for (src_node = GTK_CTREE_NODE(clist->row_list);
1660              src_node != NULL;
1661              src_node = tmp) {
1662                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1663                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1664                 if (!src_msginfo) continue;
1665                 if (!src_msginfo->subject) continue;
1666
1667                 /* find attracting node */
1668                 dst_node = g_hash_table_lookup(subject_table,
1669                                                src_msginfo->subject);
1670
1671                 if (dst_node) {
1672                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1673
1674                         /* if the time difference is more than 20 days,
1675                            don't attract */
1676                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1677                             > 60 * 60 * 24 * 20)
1678                                 continue;
1679
1680                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1681                         if (src_node != sibling)
1682                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1683                 }
1684
1685                 g_hash_table_insert(subject_table,
1686                                     src_msginfo->subject, src_node);
1687         }
1688
1689         g_hash_table_destroy(subject_table);
1690
1691         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1692
1693         gtk_clist_thaw(clist);
1694
1695         debug_print("done.\n");
1696         STATUSBAR_POP(summaryview->mainwin);
1697
1698         main_window_cursor_normal(summaryview->mainwin);
1699 }
1700
1701 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1702                                       gpointer data)
1703 {
1704         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1705
1706         if (msginfo)
1707                 procmsg_msginfo_free(msginfo);
1708 }
1709
1710 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1711                                    gpointer data)
1712 {
1713         SummaryView *summaryview = data;
1714         MsgInfo *msginfo;
1715
1716         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1717
1718         if (MSG_IS_NEWS(msginfo->flags))
1719                 news_flag_crosspost(msginfo);
1720
1721         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1722                 summaryview->newmsgs++;
1723         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1724                 summaryview->unread++;
1725         if (MSG_IS_DELETED(msginfo->flags))
1726                 summaryview->deleted++;
1727
1728         summaryview->messages++;
1729         summaryview->total_size += msginfo->size;
1730
1731         summary_set_row_marks(summaryview, node);
1732 }
1733
1734 static void summary_update_status(SummaryView *summaryview)
1735 {
1736         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1737         GtkCTreeNode *node;
1738         MsgInfo *msginfo;
1739
1740         summaryview->newmsgs = summaryview->unread =
1741         summaryview->messages = summaryview->total_size =
1742         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1743
1744         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1745              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1746                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1747
1748                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1749                         summaryview->newmsgs++;
1750                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1751                         summaryview->unread++;
1752                 if (MSG_IS_DELETED(msginfo->flags))
1753                         summaryview->deleted++;
1754                 if (MSG_IS_MOVE(msginfo->flags))
1755                         summaryview->moved++;
1756                 if (MSG_IS_COPY(msginfo->flags))
1757                         summaryview->copied++;
1758                 summaryview->messages++;
1759                 summaryview->total_size += msginfo->size;
1760         }
1761 }
1762
1763 static void summary_status_show(SummaryView *summaryview)
1764 {
1765         gchar *str;
1766         gchar *del, *mv, *cp;
1767         gchar *sel;
1768         gchar *spc;
1769         gchar *itstr;
1770         GList *rowlist, *cur;
1771         guint n_selected = 0;
1772         off_t sel_size = 0;
1773         MsgInfo *msginfo;
1774
1775         if (!summaryview->folder_item) {
1776                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1777                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1778                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1779                 return;
1780         }
1781
1782         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1783         for (cur = rowlist; cur != NULL; cur = cur->next) {
1784                 msginfo = gtk_ctree_node_get_row_data
1785                         (GTK_CTREE(summaryview->ctree),
1786                          GTK_CTREE_NODE(cur->data));
1787                 sel_size += msginfo->size;
1788                 n_selected++;
1789         }
1790
1791         if (summaryview->folder_item->folder->type == F_NEWS &&
1792             prefs_common.ng_abbrev_len < strlen(summaryview->folder_item->path)) {
1793                 gchar *group;
1794                 group = get_abbrev_newsgroup_name
1795                         (g_basename(summaryview->folder_item->path));
1796                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), group);
1797                 g_free(group);
1798         } else {
1799                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1800                               summaryview->folder_item->path);
1801         }
1802
1803         if (summaryview->deleted)
1804                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1805         else
1806                 del = g_strdup("");
1807         if (summaryview->moved)
1808                 mv = g_strdup_printf(_("%s%d moved"),
1809                                      summaryview->deleted ? _(", ") : "",
1810                                      summaryview->moved);
1811         else
1812                 mv = g_strdup("");
1813         if (summaryview->copied)
1814                 cp = g_strdup_printf(_("%s%d copied"),
1815                                      summaryview->deleted ||
1816                                      summaryview->moved ? _(", ") : "",
1817                                      summaryview->copied);
1818         else
1819                 cp = g_strdup("");
1820
1821         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1822                 spc = "    ";
1823         else
1824                 spc = "";
1825
1826         if (n_selected) {
1827                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1828                 if (n_selected == 1)
1829                         itstr = g_strdup(_(" item selected"));
1830                 else
1831                         itstr = g_strdup(_(" items selected"));
1832         } else {
1833                 sel = g_strdup("");
1834                 itstr = g_strdup("");
1835         }
1836                 
1837         str = g_strconcat(n_selected ? itos(n_selected) : "",
1838                                         itstr, sel, spc, del, mv, cp, NULL);
1839         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1840         g_free(str);
1841         g_free(sel);
1842         g_free(del);
1843         g_free(mv);
1844         g_free(cp);
1845         g_free(itstr);
1846
1847         if (FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1848                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1849                                       summaryview->newmsgs,
1850                                       summaryview->unread,
1851                                       summaryview->messages,
1852                                       to_human_readable(summaryview->total_size));
1853         } else {
1854                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1855                                       summaryview->newmsgs,
1856                                       summaryview->unread,
1857                                       summaryview->messages);
1858         }
1859         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1860         g_free(str);
1861 }
1862
1863 static void summary_set_column_titles(SummaryView *summaryview)
1864 {
1865         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1866         GtkWidget *hbox;
1867         GtkWidget *label;
1868         GtkWidget *arrow;
1869         gint pos;
1870         const gchar *title;
1871         SummaryColumnType type;
1872         gboolean single_char;
1873         GtkJustification justify;
1874
1875         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1876                 SORT_BY_MARK,
1877                 SORT_BY_UNREAD,
1878                 SORT_BY_MIME,
1879                 SORT_BY_SUBJECT,
1880                 SORT_BY_FROM,
1881                 SORT_BY_DATE,
1882                 SORT_BY_SIZE,
1883                 SORT_BY_NUMBER,
1884                 SORT_BY_SCORE,
1885                 SORT_BY_LOCKED
1886         };
1887
1888         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1889                 type = summaryview->col_state[pos].type;
1890
1891                 /* CLAWS: mime and unread are single char headers */
1892                 single_char = (type == S_COL_MIME || type == S_COL_UNREAD);
1893                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1894                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1895
1896                 switch (type) {
1897                 case S_COL_SUBJECT:
1898                 case S_COL_FROM:
1899                 case S_COL_DATE:
1900                 case S_COL_NUMBER:
1901                         if (prefs_common.trans_hdr)
1902                                 title = gettext(col_label[type]);
1903                         else
1904                                 title = col_label[type];
1905                         break;
1906                 /* CLAWS: dummies for mark and locked headers */        
1907                 case S_COL_MARK:        
1908                 case S_COL_LOCKED:
1909                         title = "";
1910                         break;
1911                 default:
1912                         title = gettext(col_label[type]);
1913                 }
1914
1915                 if (type == S_COL_MIME) {
1916                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1917                         gtk_widget_show(label);
1918                         gtk_clist_set_column_widget(clist, pos, label);
1919                         continue;
1920                 }
1921                 if (single_char) {
1922                         gtk_clist_set_column_title(clist, pos, title);
1923                         continue;
1924                 }
1925
1926                 /* CLAWS: changed so that locked and mark headers
1927                  * show a pixmap instead of single character */
1928                 hbox  = gtk_hbox_new(FALSE, 4);
1929                 
1930                 if (type == S_COL_LOCKED)
1931                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1932                 else if (type == S_COL_MARK) 
1933                         label = gtk_pixmap_new(markxpm, markxpmmask);
1934                 else 
1935                         label = gtk_label_new(title);
1936                 
1937                 if (justify == GTK_JUSTIFY_RIGHT)
1938                         gtk_box_pack_end(GTK_BOX(hbox), label,
1939                                          FALSE, FALSE, 0);
1940                 else
1941                         gtk_box_pack_start(GTK_BOX(hbox), label,
1942                                            FALSE, FALSE, 0);
1943
1944                 if (summaryview->sort_key == sort_by[type]) {
1945                         arrow = gtk_arrow_new
1946                                 (summaryview->sort_type == SORT_ASCENDING
1947                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1948                                  GTK_SHADOW_IN);
1949                         if (justify == GTK_JUSTIFY_RIGHT)
1950                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1951                                                    FALSE, FALSE, 0);
1952                         else
1953                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1954                                                  FALSE, FALSE, 0);
1955                 }
1956
1957                 gtk_widget_show_all(hbox);
1958                 gtk_clist_set_column_widget(clist, pos, hbox);
1959         }
1960 }
1961
1962 void summary_sort(SummaryView *summaryview,
1963                   FolderSortKey sort_key, FolderSortType sort_type)
1964 {
1965         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1966         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1967         GtkCListCompareFunc cmp_func = NULL;
1968
1969         switch (sort_key) {
1970         case SORT_BY_MARK:
1971                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
1972                 break;
1973         case SORT_BY_UNREAD:
1974                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_unread;
1975                 break;
1976         case SORT_BY_MIME:
1977                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
1978                 break;
1979         case SORT_BY_NUMBER:
1980                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1981                 break;
1982         case SORT_BY_SIZE:
1983                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1984                 break;
1985         case SORT_BY_DATE:
1986                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1987                 break;
1988         case SORT_BY_FROM:
1989                 if (summaryview->folder_item && summaryview->folder_item->stype != F_OUTBOX)
1990                         cmp_func = (GtkCListCompareFunc) summary_cmp_by_from;
1991                 else                    
1992                         cmp_func = (GtkCListCompareFunc) summary_cmp_by_to;
1993                 break;
1994         case SORT_BY_SUBJECT:
1995                 if (summaryview->simplify_subject_preg)
1996                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
1997                 else
1998                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1999                 break;
2000         case SORT_BY_SCORE:
2001                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2002                 break;
2003         case SORT_BY_LOCKED:
2004                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2005                 break;
2006         case SORT_BY_LABEL:
2007                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2008                 break;
2009         case SORT_BY_NONE:
2010                 cmp_func = NULL;
2011                 return;
2012         default:
2013                 return;
2014         }
2015
2016         summaryview->sort_key = sort_key;
2017         summaryview->sort_type = sort_type;
2018
2019         summary_set_column_titles(summaryview);
2020         summary_set_menu_sensitive(summaryview);
2021         if(cmp_func != NULL) {
2022                 debug_print("Sorting summary...");
2023                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2024
2025                 main_window_cursor_wait(summaryview->mainwin);
2026
2027                 gtk_clist_set_compare_func(clist, cmp_func);
2028
2029                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2030
2031                 gtk_sctree_sort_recursive(ctree, NULL);
2032
2033                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2034
2035                 main_window_cursor_normal(summaryview->mainwin);
2036
2037                 debug_print("done.\n");
2038                 STATUSBAR_POP(summaryview->mainwin);
2039         }
2040 }
2041
2042 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2043                                    GtkCTreeNode *cnode, gpointer data)
2044 {
2045         SummaryView *summaryview = (SummaryView *)data;
2046         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2047         gchar *text[N_SUMMARY_COLS];
2048         gint *col_pos = summaryview->col_pos;
2049         const gchar *msgid = msginfo->msgid;
2050         GHashTable *msgid_table = summaryview->msgid_table;
2051
2052         summary_set_header(summaryview, text, msginfo);
2053
2054         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2055                                 NULL, NULL, NULL, NULL, FALSE,
2056                                 gnode->parent->parent ? TRUE : FALSE);
2057 #define SET_TEXT(col) \
2058         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2059                                 text[col_pos[col]])
2060
2061         SET_TEXT(S_COL_NUMBER);
2062         SET_TEXT(S_COL_SCORE);
2063         SET_TEXT(S_COL_SIZE);
2064         SET_TEXT(S_COL_DATE);
2065         SET_TEXT(S_COL_FROM);
2066         SET_TEXT(S_COL_SUBJECT);
2067
2068 #undef SET_TEXT
2069
2070         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2071         summary_set_marks_func(ctree, cnode, summaryview);
2072
2073         if (msgid && msgid[0] != '\0')
2074                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2075
2076         return TRUE;
2077 }
2078
2079 static void summary_set_ctree_from_list(SummaryView *summaryview,
2080                                         GSList *mlist)
2081 {
2082         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2083         MsgInfo *msginfo;
2084                 GtkCTreeNode *node = NULL;
2085         GHashTable *msgid_table;
2086         GHashTable *subject_table;
2087         GSList * cur;
2088         
2089         if (!mlist) return;
2090
2091         debug_print("\tSetting summary from message data...");
2092         STATUSBAR_PUSH(summaryview->mainwin,
2093                        _("Setting summary from message data..."));
2094         gdk_flush();
2095
2096         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2097         summaryview->msgid_table = msgid_table;
2098         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2099         summaryview->subject_table = subject_table;
2100
2101         if (prefs_common.use_addr_book)
2102                 start_address_completion();
2103         
2104         for (cur = mlist ; cur != NULL; cur = cur->next) {
2105                 msginfo = (MsgInfo *)cur->data;
2106                 msginfo->threadscore = msginfo->score;
2107         }
2108
2109         if (global_scoring || summaryview->folder_item->prefs->scoring) {
2110                 summaryview->important_score = prefs_common.important_score;
2111                 if (summaryview->folder_item->prefs->important_score >
2112                     summaryview->important_score)
2113                         summaryview->important_score =
2114                                 summaryview->folder_item->prefs->important_score;
2115         }
2116
2117         if (summaryview->threaded) {
2118                 GNode *root, *gnode;
2119
2120                 root = procmsg_get_thread_tree(mlist);
2121
2122                 for (gnode = root->children; gnode != NULL;
2123                      gnode = gnode->next) {
2124                         node = gtk_ctree_insert_gnode
2125                                 (ctree, NULL, node, gnode,
2126                                  summary_insert_gnode_func, summaryview);
2127                 }
2128
2129                 g_node_destroy(root);
2130
2131                 summary_thread_init(summaryview);
2132         } else {
2133                 gchar *text[N_SUMMARY_COLS];
2134
2135                 mlist = g_slist_reverse(mlist);
2136                 for (; mlist != NULL; mlist = mlist->next) {
2137                         msginfo = (MsgInfo *)mlist->data;
2138
2139                         summary_set_header(summaryview, text, msginfo);
2140
2141                         node = gtk_ctree_insert_node
2142                                 (ctree, NULL, node, text, 2,
2143                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2144                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2145                         summary_set_marks_func(ctree, node, summaryview);
2146
2147                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2148                                 g_hash_table_insert(msgid_table,
2149                                                     msginfo->msgid, node);
2150
2151                         subject_table_insert(subject_table,
2152                                              msginfo->subject,
2153                                              node);
2154                 }
2155                 mlist = g_slist_reverse(mlist);
2156         }
2157
2158         if (prefs_common.enable_hscrollbar &&
2159             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2160                 gint optimal_width;
2161
2162                 optimal_width = gtk_clist_optimal_column_width
2163                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2164                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2165                                            summaryview->col_pos[S_COL_SUBJECT],
2166                                            optimal_width);
2167         }
2168
2169         if (prefs_common.use_addr_book)
2170                 end_address_completion();
2171
2172         debug_print("done.\n");
2173         STATUSBAR_POP(summaryview->mainwin);
2174         if (debug_mode) {
2175                 debug_print("\tmsgid hash table size = %d\n",
2176                             g_hash_table_size(msgid_table));
2177                 debug_print("\tsubject hash table size = %d\n",
2178                             g_hash_table_size(subject_table));
2179         }
2180 }
2181
2182 static gchar *summary_complete_address(const gchar *addr)
2183 {
2184         gint count;
2185         gchar *res, *tmp, *email_addr;
2186
2187         Xstrdup_a(email_addr, addr, return NULL);
2188         extract_address(email_addr);
2189         g_return_val_if_fail(*email_addr, NULL);
2190
2191         /*
2192          * completion stuff must be already initialized
2193          */
2194         res = NULL;
2195         if (1 < (count = complete_address(email_addr))) {
2196                 tmp = get_complete_address(1);
2197                 res = procheader_get_fromname(tmp);
2198                 g_free(tmp);    
2199         }
2200         
2201         return res;
2202 }
2203
2204 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2205                                MsgInfo *msginfo)
2206 {
2207         static gchar date_modified[80];
2208         static gchar *to = NULL;
2209         static gchar col_score[11];
2210         static gchar buf[BUFFSIZE];
2211         gint *col_pos = summaryview->col_pos;
2212
2213         text[col_pos[S_COL_MARK]]   = NULL;
2214         text[col_pos[S_COL_UNREAD]] = NULL;
2215         text[col_pos[S_COL_MIME]]   = NULL;
2216         text[col_pos[S_COL_LOCKED]] = NULL;
2217         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2218         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2219         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2220
2221         if (msginfo->date_t) {
2222                 procheader_date_get_localtime(date_modified,
2223                                               sizeof(date_modified),
2224                                               msginfo->date_t);
2225                 text[col_pos[S_COL_DATE]] = date_modified;
2226         } else if (msginfo->date)
2227                 text[col_pos[S_COL_DATE]] = msginfo->date;
2228         else
2229                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2230
2231         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2232                 _("(No From)");
2233         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2234             !MSG_IS_NEWS(msginfo->flags)) {
2235                 gchar *addr = NULL;
2236
2237                 Xstrdup_a(addr, msginfo->from, return);
2238                 extract_address(addr);
2239
2240                 if (prefs_common.use_addr_book) {
2241                         if (account_find_from_address(addr)) {
2242                                 addr = summary_complete_address(msginfo->to);
2243                                 g_free(to);
2244                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2245                                 text[col_pos[S_COL_FROM]] = to;
2246                                 g_free(addr);
2247                         }
2248                 } else {
2249                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2250                                 g_free(to);
2251                                 to = g_strconcat("-->", msginfo->to, NULL);
2252                                 text[col_pos[S_COL_FROM]] = to;
2253                         }
2254                 }
2255         }
2256
2257         /*
2258          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2259          * checking whether the above block (which handles the special case of
2260          * the --> in sent boxes) was executed.
2261          */
2262         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2263                 gchar *from = summary_complete_address(msginfo->from);
2264                 if (from) {
2265                         g_free(to);
2266                         to = from;
2267                         text[col_pos[S_COL_FROM]] = to;
2268                 }                       
2269         }
2270
2271         if (summaryview->simplify_subject_preg != NULL)
2272                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2273                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2274                                         summaryview->simplify_subject_preg) : 
2275                         _("(No Subject)");
2276         else 
2277                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2278                         _("(No Subject)");
2279 }
2280
2281 #define CHANGE_FLAGS(msginfo) \
2282 { \
2283 if (msginfo->folder->folder->change_flags != NULL) \
2284 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2285                                       msginfo->folder, \
2286                                       msginfo); \
2287 }
2288
2289 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2290 {
2291         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2292 }
2293
2294 static void summary_display_msg_full(SummaryView *summaryview,
2295                                      GtkCTreeNode *row,
2296                                      gboolean new_window, gboolean all_headers)
2297 {
2298         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2299         MsgInfo *msginfo;
2300         MsgFlags flags;
2301         gchar *filename;
2302
2303         if (!new_window && summaryview->displayed == row) return;
2304         g_return_if_fail(row != NULL);
2305
2306         if (summary_is_locked(summaryview)) return;
2307         summary_lock(summaryview);
2308
2309         STATUSBAR_POP(summaryview->mainwin);
2310         GTK_EVENTS_FLUSH();
2311
2312         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2313
2314         filename = procmsg_get_message_file(msginfo);
2315         if (!filename) {
2316                 summary_unlock(summaryview);
2317                 return;
2318         }
2319         g_free(filename);
2320
2321         if (new_window || !prefs_common.mark_as_read_on_new_window) {
2322                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2323                         summaryview->newmsgs--;
2324                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2325                         summaryview->unread--;
2326                 if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2327                         procmsg_msginfo_unset_flags
2328                                 (msginfo, MSG_NEW | MSG_UNREAD, 0);
2329                         summary_set_row_marks(summaryview, row);
2330                         gtk_clist_thaw(GTK_CLIST(ctree));
2331                         summary_status_show(summaryview);
2332                         
2333                         flags = msginfo->flags;
2334                 }
2335         }
2336
2337         if (new_window) {
2338                 MessageView *msgview;
2339
2340                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2341                 messageview_show(msgview, msginfo, all_headers);
2342         } else {
2343                 MessageView *msgview;
2344
2345                 msgview = summaryview->messageview;
2346
2347                 summaryview->displayed = row;
2348                 if (!messageview_is_visible(msgview))
2349                         main_window_toggle_message_view(summaryview->mainwin);
2350                 messageview_show(msgview, msginfo, all_headers);
2351                 if (msgview->type == MVIEW_TEXT ||
2352                     (msgview->type == MVIEW_MIME &&
2353                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2354                       gtk_notebook_get_current_page
2355                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2356                         gtk_widget_grab_focus(summaryview->ctree);
2357                 GTK_EVENTS_FLUSH();
2358                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2359         }
2360
2361         summary_set_menu_sensitive(summaryview);
2362         toolbar_set_sensitive(summaryview->mainwin);
2363
2364         summary_unlock(summaryview);
2365 }
2366
2367 void summary_display_msg_selected(SummaryView *summaryview,
2368                                   gboolean all_headers)
2369 {
2370         if (summary_is_locked(summaryview)) return;
2371         summaryview->displayed = NULL;
2372         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2373                                  all_headers);
2374 }
2375
2376 void summary_redisplay_msg(SummaryView *summaryview)
2377 {
2378         GtkCTreeNode *node;
2379
2380         if (summaryview->displayed) {
2381                 node = summaryview->displayed;
2382                 summaryview->displayed = NULL;
2383                 summary_display_msg(summaryview, node);
2384         }
2385 }
2386
2387 void summary_open_msg(SummaryView *summaryview)
2388 {
2389         if (!summaryview->selected) return;
2390
2391         summary_display_msg_full(summaryview, summaryview->selected,
2392                                  TRUE, FALSE);
2393 }
2394
2395 void summary_view_source(SummaryView * summaryview)
2396 {
2397         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2398         MsgInfo *msginfo;
2399         SourceWindow *srcwin;
2400
2401         if (!summaryview->selected) return;
2402
2403         srcwin = source_window_create();
2404         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2405         source_window_show_msg(srcwin, msginfo);
2406         source_window_show(srcwin);
2407 }
2408
2409 void summary_reedit(SummaryView *summaryview)
2410 {
2411         MsgInfo *msginfo;
2412
2413         if (!summaryview->selected) return;
2414         if (!summaryview->folder_item) return;
2415         if (summaryview->folder_item->stype != F_OUTBOX &&
2416             summaryview->folder_item->stype != F_DRAFT  &&
2417             summaryview->folder_item->stype != F_QUEUE) return;
2418
2419         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2420                                               summaryview->selected);
2421         if (!msginfo) return;
2422
2423         compose_reedit(msginfo);
2424 }
2425
2426 void summary_step(SummaryView *summaryview, GtkScrollType type)
2427 {
2428         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2429         GtkCTreeNode *node;
2430
2431         if (summary_is_locked(summaryview)) return;
2432
2433         if (type == GTK_SCROLL_STEP_FORWARD) {
2434                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2435                 if (node)
2436                         gtkut_ctree_expand_parent_all(ctree, node);
2437                 else
2438                         return;
2439         } else {
2440                 if (summaryview->selected) {
2441                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2442                         if (!node) return;
2443                 }
2444         }
2445
2446         if (messageview_is_visible(summaryview->messageview))
2447                 summaryview->display_msg = TRUE;
2448
2449         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2450                                 type, 0.0);
2451 }
2452
2453 void summary_toggle_view(SummaryView *summaryview)
2454 {
2455         if (!messageview_is_visible(summaryview->messageview) &&
2456             summaryview->selected)
2457                 summary_display_msg(summaryview,
2458                                     summaryview->selected);
2459         else
2460                 main_window_toggle_message_view(summaryview->mainwin);
2461 }
2462
2463 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2464                                                 GtkCTreeNode *node)
2465 {
2466         MsgInfo *msginfo;
2467
2468         if (node) {
2469                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2470                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2471                         return TRUE;
2472                 node = GTK_CTREE_ROW(node)->children;
2473         } else
2474                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2475
2476         while (node) {
2477                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2478                         return TRUE;
2479                 node = GTK_CTREE_ROW(node)->sibling;
2480         }
2481
2482         return FALSE;
2483 }
2484
2485 static gboolean summary_have_unread_children(SummaryView *summaryview,
2486                                              GtkCTreeNode *node)
2487 {
2488         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2489
2490         if (!node) return FALSE;
2491
2492         node = GTK_CTREE_ROW(node)->children;
2493
2494         while (node) {
2495                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2496                         return TRUE;
2497                 node = GTK_CTREE_ROW(node)->sibling;
2498         }
2499
2500         return FALSE;
2501 }
2502
2503 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2504 {
2505         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2506         GtkStyle *style = NULL;
2507         MsgInfo *msginfo;
2508         MsgFlags flags;
2509         gint *col_pos = summaryview->col_pos;
2510
2511         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2512         if (!msginfo) return;
2513
2514         flags = msginfo->flags;
2515
2516         gtk_ctree_node_set_foreground(ctree, row, NULL);
2517
2518         /* set new/unread column */
2519         if (MSG_IS_IGNORE_THREAD(flags)) {
2520                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2521                                           ignorethreadxpm, ignorethreadxpmmask);
2522         } else if (MSG_IS_NEW(flags)) {
2523                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2524                                           newxpm, newxpmmask);
2525         } else if (MSG_IS_UNREAD(flags)) {
2526                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2527                                           unreadxpm, unreadxpmmask);
2528         } else if (MSG_IS_REPLIED(flags)) {
2529                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2530                                           repliedxpm, repliedxpmmask);
2531         } else if (MSG_IS_FORWARDED(flags)) {
2532                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2533                                           forwardedxpm, forwardedxpmmask);
2534         } else {
2535                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2536                                         NULL);
2537         }
2538
2539         if (prefs_common.bold_unread &&
2540             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2541              (!GTK_CTREE_ROW(row)->expanded &&
2542               GTK_CTREE_ROW(row)->children &&
2543               summary_have_unread_children(summaryview, row))))
2544                 style = bold_style;
2545
2546         /* set mark column */
2547         if (MSG_IS_DELETED(flags)) {
2548                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2549                                           deletedxpm, deletedxpmmask);
2550                 if (style)
2551                         style = bold_deleted_style;
2552                 else {
2553                         style = small_deleted_style;
2554                 }
2555                         gtk_ctree_node_set_foreground
2556                                 (ctree, row, &summaryview->color_dim);
2557         } else if (MSG_IS_MARKED(flags)) {
2558                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2559                                           markxpm, markxpmmask);
2560         } else if (MSG_IS_MOVE(flags)) {
2561                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2562                 if (style)
2563                         style = bold_marked_style;
2564                 else {
2565                         style = small_marked_style;
2566                 }
2567                         gtk_ctree_node_set_foreground
2568                                 (ctree, row, &summaryview->color_marked);
2569         } else if (MSG_IS_COPY(flags)) {
2570                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2571                 if (style)
2572                         style = bold_marked_style;
2573                 else {
2574                         style = small_marked_style;
2575                 }
2576                         gtk_ctree_node_set_foreground
2577                                 (ctree, row, &summaryview->color_marked);
2578         }
2579         else if ((global_scoring ||
2580                   summaryview->folder_item->prefs->scoring) &&
2581                  (msginfo->score >= summaryview->important_score) &&
2582                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2583                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2584                 gtk_ctree_node_set_foreground(ctree, row,
2585                                               &summaryview->color_important);
2586         } else {
2587                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2588         }
2589
2590         if (MSG_IS_LOCKED(flags)) {
2591                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2592                                           lockedxpm, lockedxpmmask);
2593         }
2594         else {
2595                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2596         }
2597
2598         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2599                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2600                                           clipkeyxpm, clipkeyxpmmask);
2601         } else if (MSG_IS_ENCRYPTED(flags)) {
2602                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2603                                           keyxpm, keyxpmmask);
2604         } else if (MSG_IS_MIME(flags)) {
2605                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2606                                           clipxpm, clipxpmmask);
2607         } else {
2608                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2609         }
2610         if (!style)
2611                 style = small_style;
2612
2613         gtk_ctree_node_set_row_style(ctree, row, style);
2614
2615         if (MSG_GET_COLORLABEL(flags))
2616                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2617 }
2618
2619 void summary_set_marks_selected(SummaryView *summaryview)
2620 {
2621         GList *cur;
2622
2623         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2624              cur = cur->next)
2625                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2626 }
2627
2628 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2629 {
2630         gboolean changed = FALSE;
2631         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2632         MsgInfo *msginfo;
2633
2634         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2635         if (MSG_IS_DELETED(msginfo->flags))
2636                 summaryview->deleted--;
2637         if (MSG_IS_MOVE(msginfo->flags)) {
2638                 summaryview->moved--;
2639                 changed = TRUE;
2640         }
2641         if (MSG_IS_COPY(msginfo->flags)) {
2642                 summaryview->copied--;
2643                 changed = TRUE;
2644         }
2645         if (changed && !prefs_common.immediate_exec) {
2646                 msginfo->to_folder->op_count--;
2647                 if (msginfo->to_folder->op_count == 0)
2648                         folderview_update_item(msginfo->to_folder, 0);
2649         }
2650         msginfo->to_folder = NULL;
2651         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2652         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2653         summary_set_row_marks(summaryview, row);
2654         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2655 }
2656
2657 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2658 {
2659         /* almost verbatim summary_mark_row(); may want a menu action? */
2660         gboolean changed = FALSE;
2661         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2662         MsgInfo *msginfo;
2663
2664         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2665         if (MSG_IS_DELETED(msginfo->flags))
2666                 summaryview->deleted--;
2667         if (MSG_IS_MOVE(msginfo->flags)) {
2668                 summaryview->moved--;
2669                 changed = TRUE;
2670         }
2671         if (MSG_IS_COPY(msginfo->flags)) {
2672                 summaryview->copied--;
2673                 changed = TRUE;
2674         }
2675         if (changed && !prefs_common.immediate_exec) {
2676                 msginfo->to_folder->op_count--;
2677                 if (msginfo->to_folder->op_count == 0)
2678                         folderview_update_item(msginfo->to_folder, 0);
2679         }
2680         msginfo->to_folder = NULL;
2681         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2682         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2683         summary_set_row_marks(summaryview, row);
2684         debug_print("Message %d is locked\n", msginfo->msgnum);
2685 }
2686
2687 void summary_mark(SummaryView *summaryview)
2688 {
2689         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2690         GList *cur;
2691
2692         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2693                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2694
2695         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2696         summary_status_show(summaryview);
2697 }
2698
2699 static void summary_mark_row_as_read(SummaryView *summaryview,
2700                                      GtkCTreeNode *row)
2701 {
2702         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2703         MsgInfo *msginfo;
2704
2705         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2706
2707         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2708                 return;
2709
2710         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2711                 summaryview->newmsgs--;
2712         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2713                 summaryview->unread--;
2714
2715         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2716         summary_set_row_marks(summaryview, row);
2717         debug_print("Message %d is marked as read\n",
2718                 msginfo->msgnum);
2719 }
2720
2721 void summary_mark_as_read(SummaryView *summaryview)
2722 {
2723         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2724         GList *cur;
2725
2726         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2727                 summary_mark_row_as_read(summaryview,
2728                                          GTK_CTREE_NODE(cur->data));
2729
2730         summary_status_show(summaryview);
2731 }
2732
2733 void summary_mark_all_read(SummaryView *summaryview)
2734 {
2735         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2736         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2737         GtkCTreeNode *node;
2738
2739         gtk_clist_freeze(clist);
2740         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2741              node = gtkut_ctree_node_next(ctree, node))
2742                 summary_mark_row_as_read(summaryview, node);
2743         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2744              node = gtkut_ctree_node_next(ctree, node)) {
2745                 if (!GTK_CTREE_ROW(node)->expanded)
2746                         summary_set_row_marks(summaryview, node);
2747         }
2748         gtk_clist_thaw(clist);
2749
2750         summary_status_show(summaryview);
2751 }
2752
2753 static void summary_mark_row_as_unread(SummaryView *summaryview,
2754                                        GtkCTreeNode *row)
2755 {
2756         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2757         MsgInfo *msginfo;
2758
2759         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2760         if (MSG_IS_DELETED(msginfo->flags)) {
2761                 msginfo->to_folder = NULL;
2762                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2763                 summaryview->deleted--;
2764         }
2765
2766         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2767                 summaryview->unread++;
2768
2769         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2770         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2771         debug_print("Message %d is marked as unread\n",
2772                 msginfo->msgnum);
2773
2774         summary_set_row_marks(summaryview, row);
2775 }
2776
2777 void summary_mark_as_unread(SummaryView *summaryview)
2778 {
2779         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2780         GList *cur;
2781
2782         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2783                 summary_mark_row_as_unread(summaryview,
2784                                            GTK_CTREE_NODE(cur->data));
2785
2786         summary_status_show(summaryview);
2787 }
2788
2789 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2790 {
2791         GList * cur;
2792         gboolean found;
2793
2794         switch (summaryview->folder_item->folder->type) {
2795
2796         case F_NEWS:
2797
2798                 /*
2799                   security : checks if one the accounts correspond to
2800                   the author of the post
2801                 */
2802
2803                 found = FALSE;
2804                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2805                         PrefsAccount * account;
2806                         gchar * from_name;
2807                         
2808                         account = cur->data;
2809                         if (account->name && *account->name)
2810                                 from_name =
2811                                         g_strdup_printf("%s <%s>",
2812                                                         account->name,
2813                                                         account->address);
2814                         else
2815                                 from_name =
2816                                         g_strdup_printf("%s",
2817                                                         account->address);
2818                         
2819                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2820                                 g_free(from_name);
2821                                 found = TRUE;
2822                                 break;
2823                         }
2824                         g_free(from_name);
2825                 }
2826
2827                 if (!found) {
2828                         alertpanel_error(_("You're not the author of the article\n"));
2829                 }
2830                 
2831                 return found;
2832
2833         default:
2834                 return TRUE;
2835         }
2836 }
2837
2838 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2839 {
2840         gboolean changed = FALSE;
2841         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2842         MsgInfo *msginfo;
2843
2844         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2845
2846         if (!check_permission(summaryview, msginfo))
2847                 return;
2848
2849         if (MSG_IS_LOCKED(msginfo->flags)) return;
2850
2851         if (MSG_IS_DELETED(msginfo->flags)) return;
2852
2853         if (MSG_IS_MOVE(msginfo->flags)) {
2854                 summaryview->moved--;
2855                 changed = TRUE;
2856         }
2857         if (MSG_IS_COPY(msginfo->flags)) {
2858                 summaryview->copied--;
2859                 changed = TRUE;
2860         }
2861         if (changed && !prefs_common.immediate_exec) {
2862                 msginfo->to_folder->op_count--;
2863                 if (msginfo->to_folder->op_count == 0)
2864                         folderview_update_item(msginfo->to_folder, 0);
2865         }
2866         msginfo->to_folder = NULL;
2867         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2868         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2869         summaryview->deleted++;
2870
2871         if (!prefs_common.immediate_exec && 
2872             summaryview->folder_item->stype != F_TRASH)
2873                 summary_set_row_marks(summaryview, row);
2874
2875         debug_print("Message %s/%d is set to delete\n",
2876                     msginfo->folder->path, msginfo->msgnum);
2877 }
2878
2879 void summary_cancel(SummaryView *summaryview)
2880 {
2881         MsgInfo * msginfo;
2882         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2883
2884         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2885                                               summaryview->selected);
2886         if (!msginfo) return;
2887
2888         if (!check_permission(summaryview, msginfo))
2889                 return;
2890
2891         news_cancel_article(summaryview->folder_item->folder, msginfo);
2892         
2893         if (summary_is_locked(summaryview)) return;
2894
2895         summary_lock(summaryview);
2896
2897         gtk_clist_freeze(clist);
2898
2899         summary_update_status(summaryview);
2900         summary_status_show(summaryview);
2901
2902         gtk_clist_thaw(clist);
2903
2904         summary_unlock(summaryview);
2905 }
2906
2907 void summary_delete(SummaryView *summaryview)
2908 {
2909         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2910         FolderItem *item = summaryview->folder_item;
2911         GList *cur;
2912         GtkCTreeNode *sel_last = NULL;
2913         GtkCTreeNode *node;
2914
2915         if (!item) return;
2916 #if 0
2917         if (!item || item->folder->type == F_NEWS) return;
2918 #endif
2919
2920         if (summary_is_locked(summaryview)) return;
2921
2922         /* if current folder is trash, ask for confirmation */
2923         if (item->stype == F_TRASH) {
2924                 AlertValue aval;
2925
2926                 aval = alertpanel(_("Delete message(s)"),
2927                                   _("Do you really want to delete message(s) from the trash?"),
2928                                   _("Yes"), _("No"), NULL);
2929                 if (aval != G_ALERTDEFAULT) return;
2930         }
2931
2932         /* next code sets current row focus right. We need to find a row
2933          * that is not deleted. */
2934         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2935                 sel_last = GTK_CTREE_NODE(cur->data);
2936                 summary_delete_row(summaryview, sel_last);
2937         }
2938
2939         node = summary_find_next_msg(summaryview, sel_last);
2940         if (!node)
2941                 node = summary_find_prev_msg(summaryview, sel_last);
2942
2943         if (node) {
2944                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2945                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2946                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2947                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2948                 else
2949                         summary_select_node
2950                                 (summaryview, node,
2951                                  messageview_is_visible(summaryview->messageview),
2952                                  FALSE);
2953         }
2954
2955         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2956                 summary_execute(summaryview);
2957         else
2958                 summary_status_show(summaryview);
2959 }
2960
2961 void summary_delete_duplicated(SummaryView *summaryview)
2962 {
2963         if (!summaryview->folder_item ||
2964             summaryview->folder_item->folder->type == F_NEWS) return;
2965         if (summaryview->folder_item->stype == F_TRASH) return;
2966
2967         main_window_cursor_wait(summaryview->mainwin);
2968         debug_print("Deleting duplicated messages...");
2969         STATUSBAR_PUSH(summaryview->mainwin,
2970                        _("Deleting duplicated messages..."));
2971
2972         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2973                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2974                                 summaryview);
2975
2976         if (prefs_common.immediate_exec)
2977                 summary_execute(summaryview);
2978         else
2979                 summary_status_show(summaryview);
2980
2981         debug_print("done.\n");
2982         STATUSBAR_POP(summaryview->mainwin);
2983         main_window_cursor_normal(summaryview->mainwin);
2984 }
2985
2986 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2987                                            SummaryView *summaryview)
2988 {
2989         GtkCTreeNode *found;
2990         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2991
2992         if (!msginfo->msgid || !*msginfo->msgid) return;
2993
2994         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2995
2996         if (found && found != node)
2997                 summary_delete_row(summaryview, node);
2998 }
2999
3000 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3001 {
3002         gboolean changed = FALSE;
3003         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3004         MsgInfo *msginfo;
3005
3006         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3007         if (MSG_IS_DELETED(msginfo->flags))
3008                 summaryview->deleted--;
3009         if (MSG_IS_MOVE(msginfo->flags)) {
3010                 summaryview->moved--;
3011                 changed = TRUE;
3012         }
3013         if (MSG_IS_COPY(msginfo->flags)) {
3014                 summaryview->copied--;
3015                 changed = TRUE;
3016         }
3017         if (changed && !prefs_common.immediate_exec) {
3018                 msginfo->to_folder->op_count--;
3019                 if (msginfo->to_folder->op_count == 0)
3020                         folderview_update_item(msginfo->to_folder, 0);
3021         }
3022         msginfo->to_folder = NULL;
3023         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3024         summary_set_row_marks(summaryview, row);
3025
3026         debug_print("Message %s/%d is unmarked\n",
3027                     msginfo->folder->path, msginfo->msgnum);
3028 }
3029
3030 void summary_unmark(SummaryView *summaryview)
3031 {
3032         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3033         GList *cur;
3034
3035         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3036                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3037
3038         summary_status_show(summaryview);
3039 }
3040
3041 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3042                                 FolderItem *to_folder)
3043 {
3044         gboolean changed = FALSE;
3045         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3046         MsgInfo *msginfo;
3047
3048         g_return_if_fail(to_folder != NULL);
3049
3050         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3051         if (MSG_IS_MOVE(msginfo->flags)) {
3052                 if (!prefs_common.immediate_exec) {
3053                         msginfo->to_folder->op_count--;
3054                         if (msginfo->to_folder->op_count == 0) {
3055                                 folderview_update_item(msginfo->to_folder, 0);
3056                                 changed = TRUE;
3057                         }
3058                 }
3059         }
3060         msginfo->to_folder = to_folder;
3061         if (MSG_IS_DELETED(msginfo->flags))
3062                 summaryview->deleted--;
3063         if (MSG_IS_COPY(msginfo->flags)) {
3064                 summaryview->copied--;
3065                 if (!prefs_common.immediate_exec)
3066                         msginfo->to_folder->op_count--;
3067         }
3068         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3069         if (!MSG_IS_MOVE(msginfo->flags)) {
3070                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3071                 summaryview->moved++;
3072                 changed = TRUE;
3073         }
3074         if (!prefs_common.immediate_exec) {
3075                 summary_set_row_marks(summaryview, row);
3076                 if (changed) {
3077                         msginfo->to_folder->op_count++;
3078                         if (msginfo->to_folder->op_count == 1)
3079                                 folderview_update_item(msginfo->to_folder, 0);
3080                 }
3081         }
3082
3083         debug_print("Message %d is set to move to %s\n",
3084                     msginfo->msgnum, to_folder->path);
3085 }
3086
3087 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3088 {
3089         GList *cur;
3090
3091         if (!to_folder) return;
3092         if (!summaryview->folder_item ||
3093             summaryview->folder_item->folder->type == F_NEWS) return;
3094
3095         if (summary_is_locked(summaryview)) return;
3096
3097         if (summaryview->folder_item == to_folder) {
3098                 alertpanel_notice(_("Destination is same as current folder."));
3099                 return;
3100         }
3101
3102         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3103              cur != NULL; cur = cur->next)
3104                 summary_move_row_to
3105                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3106
3107         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3108
3109         if (prefs_common.immediate_exec)
3110                 summary_execute(summaryview);
3111         else {
3112                 summary_status_show(summaryview);
3113
3114                 folderview_update_item(to_folder, 0);
3115         }
3116 }
3117
3118 void summary_move_to(SummaryView *summaryview)
3119 {
3120         FolderItem *to_folder;
3121
3122         if (!summaryview->folder_item ||
3123             summaryview->folder_item->folder->type == F_NEWS) return;
3124
3125         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3126                                          FOLDER_SEL_MOVE, NULL);
3127         summary_move_selected_to(summaryview, to_folder);
3128 }
3129
3130 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3131                                 FolderItem *to_folder)
3132 {
3133         gboolean changed = FALSE;
3134         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3135         MsgInfo *msginfo;
3136
3137         g_return_if_fail(to_folder != NULL);
3138
3139         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3140         if (MSG_IS_COPY(msginfo->flags)) {
3141                 if (!prefs_common.immediate_exec) {
3142                         msginfo->to_folder->op_count--;
3143                         if (msginfo->to_folder->op_count == 0) {
3144                                 folderview_update_item(msginfo->to_folder, 0);
3145                                 changed = TRUE;
3146                         }
3147                 }
3148         }
3149         msginfo->to_folder = to_folder;
3150         if (MSG_IS_DELETED(msginfo->flags))
3151                 summaryview->deleted--;
3152         if (MSG_IS_MOVE(msginfo->flags)) {
3153                 summaryview->moved--;
3154                 if (!prefs_common.immediate_exec)
3155                         msginfo->to_folder->op_count--;
3156         }
3157         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3158         if (!MSG_IS_COPY(msginfo->flags)) {
3159                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3160                 summaryview->copied++;
3161                 changed = TRUE;
3162         }
3163         if (!prefs_common.immediate_exec) {
3164                 summary_set_row_marks(summaryview, row);
3165                 if (changed) {
3166                         msginfo->to_folder->op_count++;
3167                         if (msginfo->to_folder->op_count == 1)
3168                                 folderview_update_item(msginfo->to_folder, 0);
3169                 }
3170         }
3171
3172         debug_print("Message %d is set to copy to %s\n",
3173                     msginfo->msgnum, to_folder->path);
3174 }
3175
3176 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3177 {
3178         GList *cur;
3179
3180         if (!to_folder) return;
3181         if (!summaryview->folder_item) return;
3182
3183         if (summary_is_locked(summaryview)) return;
3184
3185         if (summaryview->folder_item == to_folder) {
3186                 alertpanel_notice
3187                         (_("Destination to copy is same as current folder."));
3188                 return;
3189         }
3190
3191         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3192              cur != NULL; cur = cur->next)
3193                 summary_copy_row_to
3194                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3195
3196         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3197
3198         if (prefs_common.immediate_exec)
3199                 summary_execute(summaryview);
3200         else {
3201                 summary_status_show(summaryview);
3202
3203                 folderview_update_item(to_folder, 0);
3204         }
3205 }
3206
3207 void summary_copy_to(SummaryView *summaryview)
3208 {
3209         FolderItem *to_folder;
3210
3211         if (!summaryview->folder_item) return;
3212
3213         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3214                                          FOLDER_SEL_COPY, NULL);
3215         summary_copy_selected_to(summaryview, to_folder);
3216 }
3217
3218 void summary_add_address(SummaryView *summaryview)
3219 {
3220         MsgInfo *msginfo;
3221         gchar *from;
3222
3223         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3224                                               summaryview->selected);
3225         if (!msginfo) return;
3226
3227         Xstrdup_a(from, msginfo->from, return);
3228         eliminate_address_comment(from);
3229         extract_address(from);
3230         addressbook_add_contact(msginfo->fromname, from, NULL);
3231 }
3232
3233 void summary_select_all(SummaryView *summaryview)
3234 {
3235         if (summaryview->messages >= 500) {
3236                 STATUSBAR_PUSH(summaryview->mainwin,
3237                                _("Selecting all messages..."));
3238                 main_window_cursor_wait(summaryview->mainwin);
3239         }
3240
3241         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3242
3243         if (summaryview->messages >= 500) {
3244                 STATUSBAR_POP(summaryview->mainwin);
3245                 main_window_cursor_normal(summaryview->mainwin);
3246         }
3247 }
3248
3249 void summary_unselect_all(SummaryView *summaryview)
3250 {
3251         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3252 }
3253
3254 void summary_select_thread(SummaryView *summaryview)
3255 {
3256         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3257         GtkCTreeNode *node = summaryview->selected;
3258
3259         if (!node) return;
3260
3261         while (GTK_CTREE_ROW(node)->parent != NULL)
3262                 node = GTK_CTREE_ROW(node)->parent;
3263
3264         if (node != summaryview->selected)
3265                 summary_select_node
3266                         (summaryview, node,
3267                          messageview_is_visible(summaryview->messageview),
3268                          FALSE);
3269
3270         gtk_ctree_select_recursive(ctree, node);
3271
3272         summary_status_show(summaryview);
3273 }
3274
3275 void summary_save_as(SummaryView *summaryview)
3276 {
3277         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3278         MsgInfo *msginfo;
3279         gchar *filename = NULL;
3280         gchar *src, *dest;
3281
3282         AlertValue aval;
3283
3284         if (!summaryview->selected) return;
3285         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3286         if (!msginfo) return;
3287
3288         if (msginfo->subject) {
3289                 Xstrdup_a(filename, msginfo->subject, return);
3290                 subst_for_filename(filename);
3291         }
3292         dest = filesel_select_file(_("Save as"), filename);
3293         if (!dest) return;
3294         if (is_file_exist(dest)) {
3295                 aval = alertpanel(_("Append or Overwrite"),
3296                                   _("Append or overwrite existing file?"),
3297                                   _("Append"), _("Overwrite"), _("Cancel"));
3298                 if (aval!=0 && aval!=1) return;
3299         }
3300
3301         src = procmsg_get_message_file(msginfo);