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