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