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