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