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