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