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