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