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