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