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