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