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