add ability to 'Select thread'
[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                 menu_set_sensitive(ifactory, "/Delete", TRUE);
1045         }
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, "w")) == 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, "w")) == 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 void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2863 {
2864         gboolean changed = FALSE;
2865         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2866         MsgInfo *msginfo;
2867
2868         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2869
2870         if (MSG_IS_LOCKED(msginfo->flags)) return;
2871
2872         if (MSG_IS_DELETED(msginfo->flags)) return;
2873
2874         if (MSG_IS_MOVE(msginfo->flags)) {
2875                 summaryview->moved--;
2876                 changed = TRUE;
2877         }
2878         if (MSG_IS_COPY(msginfo->flags)) {
2879                 summaryview->copied--;
2880                 changed = TRUE;
2881         }
2882         if (changed && !prefs_common.immediate_exec) {
2883                 msginfo->to_folder->op_count--;
2884                 if (msginfo->to_folder->op_count == 0)
2885                         folderview_update_item(msginfo->to_folder, 0);
2886         }
2887         msginfo->to_folder = NULL;
2888         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2889         if (MSG_IS_IMAP(msginfo->flags))
2890                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
2891         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2892         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2893         CHANGE_FLAGS(msginfo);
2894         summaryview->deleted++;
2895
2896         if (!prefs_common.immediate_exec && 
2897             summaryview->folder_item->stype != F_TRASH)
2898                 summary_set_row_marks(summaryview, row);
2899
2900         debug_print(_("Message %s/%d is set to delete\n"),
2901                     msginfo->folder->path, msginfo->msgnum);
2902 }
2903
2904 void summary_delete(SummaryView *summaryview)
2905 {
2906         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2907         FolderItem *item = summaryview->folder_item;
2908         GList *cur;
2909         GtkCTreeNode *sel_last = NULL;
2910         GtkCTreeNode *node;
2911
2912         if (!item || item->folder->type == F_NEWS) return;
2913
2914         if (summary_is_locked(summaryview)) return;
2915
2916         /* if current folder is trash, ask for confirmation */
2917         if (item->stype == F_TRASH) {
2918                 AlertValue aval;
2919
2920                 aval = alertpanel(_("Delete message(s)"),
2921                                   _("Do you really want to delete message(s) from the trash?"),
2922                                   _("Yes"), _("No"), NULL);
2923                 if (aval != G_ALERTDEFAULT) return;
2924         }
2925
2926         /* next code sets current row focus right. We need to find a row
2927          * that is not deleted. */
2928         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2929                 sel_last = GTK_CTREE_NODE(cur->data);
2930                 summary_delete_row(summaryview, sel_last);
2931         }
2932
2933         node = summary_find_next_msg(summaryview, sel_last);
2934         if (!node)
2935                 node = summary_find_prev_msg(summaryview, sel_last);
2936
2937         if (node) {
2938                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2939                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2940                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2941                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2942                 else
2943                         summary_select_node(summaryview, node,
2944                                             summaryview->msg_is_toggled_on,
2945                                             FALSE);
2946         }
2947
2948         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2949                 summary_execute(summaryview);
2950         else
2951                 summary_status_show(summaryview);
2952 }
2953
2954 void summary_delete_duplicated(SummaryView *summaryview)
2955 {
2956         if (!summaryview->folder_item ||
2957             summaryview->folder_item->folder->type == F_NEWS) return;
2958         if (summaryview->folder_item->stype == F_TRASH) return;
2959
2960         main_window_cursor_wait(summaryview->mainwin);
2961         debug_print(_("Deleting duplicated messages..."));
2962         STATUSBAR_PUSH(summaryview->mainwin,
2963                        _("Deleting duplicated messages..."));
2964
2965         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2966                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2967                                 summaryview);
2968
2969         if (prefs_common.immediate_exec)
2970                 summary_execute(summaryview);
2971         else
2972                 summary_status_show(summaryview);
2973
2974         debug_print(_("done.\n"));
2975         STATUSBAR_POP(summaryview->mainwin);
2976         main_window_cursor_normal(summaryview->mainwin);
2977 }
2978
2979 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2980                                            SummaryView *summaryview)
2981 {
2982         GtkCTreeNode *found;
2983         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2984
2985         if (!msginfo->msgid || !*msginfo->msgid) return;
2986
2987         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2988
2989         if (found && found != node)
2990                 summary_delete_row(summaryview, node);
2991 }
2992
2993 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2994 {
2995         gboolean changed = FALSE;
2996         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2997         MsgInfo *msginfo;
2998
2999         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3000         if (MSG_IS_DELETED(msginfo->flags))
3001                 summaryview->deleted--;
3002         if (MSG_IS_MOVE(msginfo->flags)) {
3003                 summaryview->moved--;
3004                 changed = TRUE;
3005         }
3006         if (MSG_IS_COPY(msginfo->flags)) {
3007                 summaryview->copied--;
3008                 changed = TRUE;
3009         }
3010         if (changed && !prefs_common.immediate_exec) {
3011                 msginfo->to_folder->op_count--;
3012                 if (msginfo->to_folder->op_count == 0)
3013                         folderview_update_item(msginfo->to_folder, 0);
3014         }
3015         msginfo->to_folder = NULL;
3016         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
3017         if (MSG_IS_IMAP(msginfo->flags))
3018                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
3019         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
3020         CHANGE_FLAGS(msginfo);
3021         summary_set_row_marks(summaryview, row);
3022         debug_print(_("Message %s/%d is unmarked\n"),
3023                     msginfo->folder->path, msginfo->msgnum);
3024 }
3025
3026 void summary_unmark(SummaryView *summaryview)
3027 {
3028         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3029         GList *cur;
3030
3031         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3032                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3033
3034         summary_status_show(summaryview);
3035 }
3036
3037 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3038                                 FolderItem *to_folder)
3039 {
3040         gboolean changed = FALSE;
3041         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3042         MsgInfo *msginfo;
3043
3044         g_return_if_fail(to_folder != NULL);
3045
3046         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3047         if (MSG_IS_MOVE(msginfo->flags)) {
3048                 if (!prefs_common.immediate_exec) {
3049                         msginfo->to_folder->op_count--;
3050                         if (msginfo->to_folder->op_count == 0) {
3051                                 folderview_update_item(msginfo->to_folder, 0);
3052                                 changed = TRUE;
3053                         }
3054                 }
3055         }
3056         msginfo->to_folder = to_folder;
3057         if (MSG_IS_DELETED(msginfo->flags))
3058                 summaryview->deleted--;
3059         if (MSG_IS_COPY(msginfo->flags)) {
3060                 summaryview->copied--;
3061                 if (!prefs_common.immediate_exec)
3062                         msginfo->to_folder->op_count--;
3063         }
3064         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
3065         if (MSG_IS_IMAP(msginfo->flags))
3066                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
3067         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_COPY);
3068         if (!MSG_IS_MOVE(msginfo->flags)) {
3069                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
3070                 summaryview->moved++;
3071                 changed = TRUE;
3072         }
3073         if (!prefs_common.immediate_exec) {
3074                 summary_set_row_marks(summaryview, row);
3075                 if (changed) {
3076                         msginfo->to_folder->op_count++;
3077                         if (msginfo->to_folder->op_count == 1)
3078                                 folderview_update_item(msginfo->to_folder, 0);
3079                 }
3080         }
3081
3082         debug_print(_("Message %d is set to move to %s\n"),
3083                     msginfo->msgnum, to_folder->path);
3084 }
3085
3086 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3087 {
3088         GList *cur;
3089
3090         if (!to_folder) return;
3091         if (!summaryview->folder_item ||
3092             summaryview->folder_item->folder->type == F_NEWS) return;
3093
3094         if (summary_is_locked(summaryview)) return;
3095
3096         if (summaryview->folder_item == to_folder) {
3097                 alertpanel_notice(_("Destination is same as current folder."));
3098                 return;
3099         }
3100
3101         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3102              cur != NULL; cur = cur->next)
3103                 summary_move_row_to
3104                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3105
3106         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3107
3108         if (prefs_common.immediate_exec)
3109                 summary_execute(summaryview);
3110         else {
3111                 summary_status_show(summaryview);
3112
3113                 folderview_update_item(to_folder, 0);
3114         }
3115 }
3116
3117 void summary_move_to(SummaryView *summaryview)
3118 {
3119         FolderItem *to_folder;
3120
3121         if (!summaryview->folder_item ||
3122             summaryview->folder_item->folder->type == F_NEWS) return;
3123
3124         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3125                                          FOLDER_SEL_MOVE, NULL);
3126         summary_move_selected_to(summaryview, to_folder);
3127 }
3128
3129 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3130                                 FolderItem *to_folder)
3131 {
3132         gboolean changed = FALSE;
3133         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3134         MsgInfo *msginfo;
3135
3136         g_return_if_fail(to_folder != NULL);
3137
3138         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3139         if (MSG_IS_COPY(msginfo->flags)) {
3140                 if (!prefs_common.immediate_exec) {
3141                         msginfo->to_folder->op_count--;
3142                         if (msginfo->to_folder->op_count == 0) {
3143                                 folderview_update_item(msginfo->to_folder, 0);
3144                                 changed = TRUE;
3145                         }
3146                 }
3147         }
3148         msginfo->to_folder = to_folder;
3149         if (MSG_IS_DELETED(msginfo->flags))
3150                 summaryview->deleted--;
3151         if (MSG_IS_MOVE(msginfo->flags)) {
3152                 summaryview->moved--;
3153                 if (!prefs_common.immediate_exec)
3154                         msginfo->to_folder->op_count--;
3155         }
3156         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
3157         if (MSG_IS_IMAP(msginfo->flags))
3158                 imap_msg_unset_perm_flags(msginfo, MSG_MARKED);
3159         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
3160         if (!MSG_IS_COPY(msginfo->flags)) {
3161                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_COPY);
3162                 summaryview->copied++;
3163                 changed = TRUE;
3164         }
3165         if (!prefs_common.immediate_exec) {
3166                 summary_set_row_marks(summaryview, row);
3167                 if (changed) {
3168                         msginfo->to_folder->op_count++;
3169                         if (msginfo->to_folder->op_count == 1)
3170                                 folderview_update_item(msginfo->to_folder, 0);
3171                 }
3172         }
3173
3174         debug_print(_("Message %d is set to copy to %s\n"),
3175                     msginfo->msgnum, to_folder->path);
3176 }
3177
3178 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3179 {
3180         GList *cur;
3181
3182         if (!to_folder) return;
3183         if (!summaryview->folder_item) return;
3184
3185         if (summary_is_locked(summaryview)) return;
3186
3187         if (summaryview->folder_item == to_folder) {
3188                 alertpanel_notice
3189                         (_("Destination to copy is same as current folder."));
3190                 return;
3191         }
3192
3193         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3194              cur != NULL; cur = cur->next)
3195                 summary_copy_row_to
3196                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3197
3198         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3199
3200         if (prefs_common.immediate_exec)
3201                 summary_execute(summaryview);
3202         else {
3203                 summary_status_show(summaryview);
3204
3205                 folderview_update_item(to_folder, 0);
3206         }
3207 }
3208
3209 void summary_copy_to(SummaryView *summaryview)
3210 {
3211         FolderItem *to_folder;
3212
3213         if (!summaryview->folder_item) return;
3214
3215         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3216                                          FOLDER_SEL_COPY, NULL);
3217         summary_copy_selected_to(summaryview, to_folder);
3218 }
3219
3220 void summary_add_address(SummaryView *summaryview)
3221 {
3222         MsgInfo *msginfo;
3223         gchar *from;
3224
3225         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3226                                               summaryview->selected);
3227         if (!msginfo) return;
3228
3229         Xstrdup_a(from, msginfo->from, return);
3230         eliminate_address_comment(from);
3231         extract_address(from);
3232         addressbook_add_contact(msginfo->fromname, from, NULL);
3233 }
3234
3235 void summary_select_all(SummaryView *summaryview)
3236 {
3237         if (summaryview->messages >= 500) {
3238                 STATUSBAR_PUSH(summaryview->mainwin,
3239                                _("Selecting all messages..."));
3240                 main_window_cursor_wait(summaryview->mainwin);
3241         }
3242
3243         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3244
3245         if (summaryview->messages >= 500) {
3246                 STATUSBAR_POP(summaryview->mainwin);
3247                 main_window_cursor_normal(summaryview->mainwin);
3248         }
3249 }
3250
3251 void summary_unselect_all(SummaryView *summaryview)
3252 {
3253         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3254 }
3255
3256 void summary_save_as(SummaryView *summaryview)
3257 {
3258         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3259         MsgInfo *msginfo;
3260         gchar *filename = NULL;
3261         gchar *src, *dest;
3262
3263         if (!summaryview->selected) return;
3264         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3265         if (!msginfo) return;
3266
3267         if (msginfo->subject) {
3268                 Xstrdup_a(filename, msginfo->subject, return);
3269                 subst_for_filename(filename);
3270         }
3271         dest = filesel_select_file(_("Save as"), filename);
3272         if (!dest) return;
3273         if (is_file_exist(dest)) {
3274                 AlertValue aval;
3275
3276                 aval = alertpanel(_("Overwrite"),
3277                                   _("Overwrite existing file?"),
3278                                   _("OK"), _("Cancel"), NULL);
3279                 if (G_ALERTDEFAULT != aval) return;
3280         }
3281
3282         src = procmsg_get_message_file(msginfo);
3283         copy_file(src, dest);
3284         g_free(src);
3285 }
3286
3287 void summary_print(SummaryView *summaryview)
3288 {
3289         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3290         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3291         MsgInfo *msginfo;
3292         GList *cur;
3293         gchar *cmdline;
3294         gchar *p;
3295
3296         if (clist->selection == NULL) return;
3297
3298         cmdline = input_dialog(_("Print"),
3299                                _("Enter the print command line:\n"
3300                                  "(`%s' will be replaced with file name)"),
3301                                prefs_common.print_cmd);
3302         if (!cmdline) return;
3303         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3304             strchr(p + 2, '%')) {
3305                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3306                                  cmdline);
3307                 g_free(cmdline);
3308                 return;
3309         }
3310
3311         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3312                 msginfo = gtk_ctree_node_get_row_data
3313                         (ctree, GTK_CTREE_NODE(cur->data));
3314                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3315         }
3316
3317         g_free(cmdline);
3318 }
3319
3320 gboolean summary_execute(SummaryView *summaryview)
3321 {
3322         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3323         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3324         GtkCTreeNode *node, *next;
3325
3326         if (!summaryview->folder_item) return FALSE;
3327
3328         if (summary_is_locked(summaryview)) return FALSE;
3329         summary_lock(summaryview);
3330
3331         gtk_clist_freeze(clist);
3332
3333         if (summaryview->folder_item->threaded)
3334                 summary_unthread_for_exec(summaryview);
3335
3336         summary_execute_move(summaryview);
3337         summary_execute_copy(summaryview);
3338         summary_execute_delete(summaryview);
3339
3340         node = GTK_CTREE_NODE(clist->row_list);
3341         while (node != NULL) {
3342                 next = gtkut_ctree_node_next(ctree,&n