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