make lock stuff added some days ago more functional
[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 static void summary_filter_func         (GtkCTree               *ctree,
254                                          GtkCTreeNode           *node,
255                                          gpointer                data);
256
257 static void summary_colorlabel_menu_item_activate_cb
258                                           (GtkWidget    *widget,
259                                            gpointer      data);
260 static void summary_colorlabel_menu_item_activate_item_cb
261                                           (GtkMenuItem  *label_menu_item,
262                                            gpointer      data);
263 static void summary_colorlabel_menu_create(SummaryView  *summaryview);
264
265 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
266
267 /* callback functions */
268 static void summary_toggle_pressed      (GtkWidget              *eventbox,
269                                          GdkEventButton         *event,
270                                          SummaryView            *summaryview);
271 static void summary_button_pressed      (GtkWidget              *ctree,
272                                          GdkEventButton         *event,
273                                          SummaryView            *summaryview);
274 static void summary_button_released     (GtkWidget              *ctree,
275                                          GdkEventButton         *event,
276                                          SummaryView            *summaryview);
277 static void summary_key_pressed         (GtkWidget              *ctree,
278                                          GdkEventKey            *event,
279                                          SummaryView            *summaryview);
280 static void summary_open_row            (GtkSCTree              *sctree,
281                                          SummaryView            *summaryview);
282 static void summary_tree_expanded       (GtkCTree               *ctree,
283                                          GtkCTreeNode           *node,
284                                          SummaryView            *summaryview);
285 static void summary_tree_collapsed      (GtkCTree               *ctree,
286                                          GtkCTreeNode           *node,
287                                          SummaryView            *summaryview);
288 static void summary_selected            (GtkCTree               *ctree,
289                                          GtkCTreeNode           *row,
290                                          gint                    column,
291                                          SummaryView            *summaryview);
292 static void summary_col_resized         (GtkCList               *clist,
293                                          gint                    column,
294                                          gint                    width,
295                                          SummaryView            *summaryview);
296 static void summary_reply_cb            (SummaryView            *summaryview,
297                                          guint                   action,
298                                          GtkWidget              *widget);
299 static void summary_execute_cb          (SummaryView            *summaryview,
300                                          guint                   action,
301                                          GtkWidget              *widget);
302 static void summary_show_all_header_cb  (SummaryView            *summaryview,
303                                          guint                   action,
304                                          GtkWidget              *widget);
305
306 static void summary_add_address_cb      (SummaryView            *summaryview,
307                                          guint                   action,
308                                          GtkWidget              *widget);
309
310 static void summary_mark_clicked        (GtkWidget              *button,
311                                          SummaryView            *summaryview);
312 static void summary_unread_clicked      (GtkWidget              *button,
313                                          SummaryView            *summaryview);
314 static void summary_mime_clicked        (GtkWidget              *button,
315                                          SummaryView            *summaryview);
316 static void summary_num_clicked         (GtkWidget              *button,
317                                          SummaryView            *summaryview);
318 static void summary_score_clicked       (GtkWidget *button,
319                                          SummaryView *summaryview);
320 static void summary_size_clicked        (GtkWidget              *button,
321                                          SummaryView            *summaryview);
322 static void summary_date_clicked        (GtkWidget              *button,
323                                          SummaryView            *summaryview);
324 static void summary_from_clicked        (GtkWidget              *button,
325                                          SummaryView            *summaryview);
326 static void summary_subject_clicked     (GtkWidget              *button,
327                                          SummaryView            *summaryview);
328 static void summary_score_clicked       (GtkWidget              *button,
329                                          SummaryView            *summaryview);
330
331 static void summary_start_drag          (GtkWidget        *widget, 
332                                          int button,
333                                          GdkEvent *event,
334                                          SummaryView      *summaryview);
335 static void summary_drag_data_get       (GtkWidget        *widget,
336                                          GdkDragContext   *drag_context,
337                                          GtkSelectionData *selection_data,
338                                          guint             info,
339                                          guint             time,
340                                          SummaryView      *summaryview);
341
342 /* custom compare functions for sorting */
343
344 static gint summary_cmp_by_mark         (GtkCList               *clist,
345                                          gconstpointer           ptr1,
346                                          gconstpointer           ptr2);
347 static gint summary_cmp_by_unread       (GtkCList               *clist,
348                                          gconstpointer           ptr1,
349                                          gconstpointer           ptr2);
350 static gint summary_cmp_by_mime         (GtkCList               *clist,
351                                          gconstpointer           ptr1,
352                                          gconstpointer           ptr2);
353 static gint summary_cmp_by_num          (GtkCList               *clist,
354                                          gconstpointer           ptr1,
355                                          gconstpointer           ptr2);
356 static gint summary_cmp_by_size         (GtkCList               *clist,
357                                          gconstpointer           ptr1,
358                                          gconstpointer           ptr2);
359 static gint summary_cmp_by_date         (GtkCList               *clist,
360                                          gconstpointer           ptr1,
361                                          gconstpointer           ptr2);
362 static gint summary_cmp_by_from         (GtkCList               *clist,
363                                          gconstpointer           ptr1,
364                                          gconstpointer           ptr2);
365 static gint summary_cmp_by_subject      (GtkCList               *clist,
366                                          gconstpointer           ptr1,
367                                          gconstpointer           ptr2);
368 static gint summary_cmp_by_score        (GtkCList               *clist,
369                                          gconstpointer           ptr1,
370                                          gconstpointer           ptr2);
371 static gint summary_cmp_by_label        (GtkCList               *clist,
372                                          gconstpointer           ptr1,
373                                          gconstpointer           ptr2);
374
375 #if MARK_ALL_READ
376 static void summary_mark_all_read (SummaryView *summaryview);                                    
377 #endif
378
379 GtkTargetEntry summary_drag_types[1] =
380 {
381         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
382 };
383
384 static GtkItemFactoryEntry summary_popup_entries[] =
385 {
386         {N_("/_Reply"),                 NULL, summary_reply_cb, COMPOSE_REPLY, NULL},
387         {N_("/Repl_y to sender"),       NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
388         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
389         {N_("/Reply to a_ll"),          NULL, summary_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
390         {N_("/_Forward"),               NULL, summary_reply_cb, COMPOSE_FORWARD, NULL},
391         {N_("/Forward as a_ttachment"),
392                                         NULL, summary_reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
393         {N_("/Bounce"),                 NULL, summary_reply_cb, COMPOSE_BOUNCE, NULL},
394         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
395         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
396         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
397         {N_("/M_ove..."),               NULL, summary_move_to,  0, NULL},
398         {N_("/_Copy..."),               NULL, summary_copy_to,  0, NULL},
399         {N_("/_Delete"),                NULL, summary_delete,   0, NULL},
400         {N_("/E_xecute"),               NULL, summary_execute_cb,       0, NULL},
401         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
402         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
403         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
404         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
405         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
406         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
407         {N_("/_Mark/Mark as rea_d"),    NULL, summary_mark_as_read, 0, NULL},
408         {N_("/_Mark/Mark all read"),    NULL, summary_mark_all_read, 0, NULL},
409         {N_("/_Mark/Ignore thread"),    NULL, summary_ignore_thread, 0, NULL},
410         {N_("/_Mark/Unignore thread"),  NULL, summary_unignore_thread, 0, NULL},
411         {N_("/Color la_bel"),           NULL, NULL,             0, NULL},
412
413         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
414         {N_("/Add sender to address boo_k"),
415                                         NULL, summary_add_address_cb, 0, NULL},
416         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
417         {N_("/_View"),                  NULL, NULL,             0, "<Branch>"},
418         {N_("/_View/Open in new _window"),
419                                         NULL, summary_open_msg, 0, NULL},
420         {N_("/_View/_Source"),          NULL, summary_view_source, 0, NULL},
421         {N_("/_View/All _header"),      NULL, summary_show_all_header_cb, 0, NULL},
422         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
423         {N_("/_Save as..."),            NULL, summary_save_as,  0, NULL},
424         {N_("/_Print..."),              NULL, summary_print,    0, NULL},
425         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
426         {N_("/Select _all"),            NULL, summary_select_all, 0, NULL}
427 };
428
429 static const gchar *const col_label[N_SUMMARY_COLS] = {
430         N_("M"),        /* S_COL_MARK    */
431         N_("U"),        /* S_COL_UNREAD  */
432         "",             /* S_COL_MIME    */
433         N_("Subject"),  /* S_COL_SUBJECT */
434         N_("From"),     /* S_COL_FROM    */
435         N_("Date"),     /* S_COL_DATE    */
436         N_("Size"),     /* S_COL_SIZE    */
437         N_("No."),      /* S_COL_NUMBER  */
438         N_("Score"),    /* S_COL_SCORE   */
439         N_("L")         /* S_COL_LOCKED  */
440 };
441
442 SummaryView *summary_create(void)
443 {
444         SummaryView *summaryview;
445         GtkWidget *vbox;
446         GtkWidget *scrolledwin;
447         GtkWidget *ctree;
448         GtkWidget *hbox;
449         GtkWidget *statlabel_folder;
450         GtkWidget *statlabel_select;
451         GtkWidget *statlabel_msgs;
452         GtkWidget *toggle_eventbox;
453         GtkWidget *toggle_arrow;
454         GtkWidget *popupmenu;
455         GtkItemFactory *popupfactory;
456         gint n_entries;
457
458         debug_print(_("Creating summary view...\n"));
459         summaryview = g_new0(SummaryView, 1);
460
461         vbox = gtk_vbox_new(FALSE, 2);
462
463         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
464         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
465                                        GTK_POLICY_AUTOMATIC,
466                                        GTK_POLICY_ALWAYS);
467         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
468         gtk_widget_set_usize(vbox,
469                              prefs_common.summaryview_width,
470                              prefs_common.summaryview_height);
471
472         ctree = summary_ctree_create(summaryview);
473
474         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
475                                             GTK_CLIST(ctree)->hadjustment);
476         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
477                                             GTK_CLIST(ctree)->vadjustment);
478         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
479
480         /* create status label */
481         hbox = gtk_hbox_new(FALSE, 0);
482         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
483
484         statlabel_folder = gtk_label_new("");
485         gtk_box_pack_start(GTK_BOX(hbox), statlabel_folder, FALSE, FALSE, 2);
486         statlabel_select = gtk_label_new("");
487         gtk_box_pack_start(GTK_BOX(hbox), statlabel_select, FALSE, FALSE, 16);
488
489         /* toggle view button */
490         toggle_eventbox = gtk_event_box_new();
491         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
492         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
493         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
494         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
495                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
496                            summaryview);
497
498         statlabel_msgs = gtk_label_new("");
499         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
500
501         /* create popup menu */
502         n_entries = sizeof(summary_popup_entries) /
503                 sizeof(summary_popup_entries[0]);
504         popupmenu = menu_create_items(summary_popup_entries, n_entries,
505                                       "<SummaryView>", &popupfactory,
506                                       summaryview);
507
508         summaryview->vbox = vbox;
509         summaryview->scrolledwin = scrolledwin;
510         summaryview->ctree = ctree;
511         summaryview->hbox = hbox;
512         summaryview->statlabel_folder = statlabel_folder;
513         summaryview->statlabel_select = statlabel_select;
514         summaryview->statlabel_msgs = statlabel_msgs;
515         summaryview->toggle_eventbox = toggle_eventbox;
516         summaryview->toggle_arrow = toggle_arrow;
517         summaryview->popupmenu = popupmenu;
518         summaryview->popupfactory = popupfactory;
519         summaryview->msg_is_toggled_on = TRUE;
520         summaryview->lock_count = 0;
521         summaryview->sort_mode = SORT_BY_NONE;
522         summaryview->sort_type = GTK_SORT_ASCENDING;
523
524         gtk_widget_show_all(vbox);
525
526         return summaryview;
527 }
528
529 void summary_init(SummaryView *summaryview)
530 {
531         GtkStyle *style;
532         GtkWidget *pixmap;
533
534         PIXMAP_CREATE(summaryview->ctree, markxpm, markxpmmask, mark_xpm);
535         PIXMAP_CREATE(summaryview->ctree, deletedxpm, deletedxpmmask,
536                       deleted_xpm);
537         PIXMAP_CREATE(summaryview->ctree, newxpm, newxpmmask, new_xpm);
538         PIXMAP_CREATE(summaryview->ctree, unreadxpm, unreadxpmmask, unread_xpm);
539         PIXMAP_CREATE(summaryview->ctree, repliedxpm, repliedxpmmask,
540                       replied_xpm);
541         PIXMAP_CREATE(summaryview->ctree, forwardedxpm, forwardedxpmmask,
542                       forwarded_xpm);
543         PIXMAP_CREATE(summaryview->ctree, ignorethreadxpm, ignorethreadxpmmask,
544                       ignorethread_xpm);
545         PIXMAP_CREATE(summaryview->ctree, lockedxpm, lockedxpmmask, locked_xpm);                      
546         PIXMAP_CREATE(summaryview->ctree, clipxpm, clipxpmmask, clip_xpm);
547         PIXMAP_CREATE(summaryview->hbox, folderxpm, folderxpmmask,
548                       dir_open_xpm);
549
550         if (!small_style) {
551                 small_style = gtk_style_copy
552                         (gtk_widget_get_style(summaryview->ctree));
553                 if (!smallfont)
554                         smallfont = gdk_fontset_load(SMALL_FONT);
555                 small_style->font = smallfont;
556                 small_marked_style = gtk_style_copy(small_style);
557                 small_marked_style->fg[GTK_STATE_NORMAL] =
558                         summaryview->color_marked;
559                 small_deleted_style = gtk_style_copy(small_style);
560                 small_deleted_style->fg[GTK_STATE_NORMAL] =
561                         summaryview->color_dim;
562         }
563         if (!bold_style) {
564                 bold_style = gtk_style_copy
565                         (gtk_widget_get_style(summaryview->ctree));
566                 if (!boldfont)
567                         boldfont = gdk_fontset_load(BOLD_FONT);
568                 bold_style->font = boldfont;
569                 bold_marked_style = gtk_style_copy(bold_style);
570                 bold_marked_style->fg[GTK_STATE_NORMAL] =
571                         summaryview->color_marked;
572                 bold_deleted_style = gtk_style_copy(bold_style);
573                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
574                         summaryview->color_dim;
575         }
576
577         style = gtk_style_copy(gtk_widget_get_style
578                                 (summaryview->statlabel_folder));
579
580         gtk_widget_set_style(summaryview->statlabel_folder, style);
581         gtk_widget_set_style(summaryview->statlabel_select, style);
582         gtk_widget_set_style(summaryview->statlabel_msgs, style);
583
584         pixmap = gtk_pixmap_new(folderxpm, folderxpmmask);
585         gtk_box_pack_start(GTK_BOX(summaryview->hbox), pixmap, FALSE, FALSE, 4);
586         gtk_box_reorder_child(GTK_BOX(summaryview->hbox), pixmap, 0);
587         gtk_widget_show(pixmap);
588
589         summary_clear_list(summaryview);
590         summary_set_column_titles(summaryview);
591         summary_colorlabel_menu_create(summaryview);
592         summary_set_menu_sensitive(summaryview);
593
594 }
595
596 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
597                                                  GtkCTreeNode *current_node)
598 {
599         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
600         GtkCTreeNode *node;
601         MsgInfo *msginfo;
602         gint best_score = MIN_SCORE;
603         GtkCTreeNode *best_node = NULL;
604
605         if (current_node)
606                 /*node = current_node;*/
607                 node = GTK_CTREE_NODE_NEXT(current_node);
608         else
609                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
610
611         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
612                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
613                 if (msginfo->score >= summaryview->important_score)
614                         break;
615                 if (msginfo->score > best_score) {
616                         best_score = msginfo->score;
617                         best_node = node;
618                 }
619         }
620
621         if (node != NULL)
622                 return node;
623         else
624                 return best_node;
625 }
626
627 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
628                                                  GtkCTreeNode *current_node)
629 {
630         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
631         GtkCTreeNode *node;
632         MsgInfo *msginfo;
633         gint best_score = MIN_SCORE;
634         GtkCTreeNode *best_node = NULL;
635
636         if (current_node)
637                 /*node = current_node;*/
638                 node = GTK_CTREE_NODE_PREV(current_node);
639         else
640                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
641
642         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
643                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
644                 if (msginfo->score >= summaryview->important_score)
645                         break;
646                 if (msginfo->score > best_score) {
647                         best_score = msginfo->score;
648                         best_node = node;
649                 }
650         }
651
652         if (node != NULL)
653                 return node;
654         else
655                 return best_node;
656 }
657
658 gboolean summary_show(SummaryView *summaryview, FolderItem *item,
659                       gboolean update_cache)
660 {
661         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
662         GtkCTreeNode *node;
663         GSList *mlist = NULL;
664         gchar *buf;
665         gboolean is_refresh;
666         guint selected_msgnum = 0;
667         guint displayed_msgnum = 0;
668         GtkCTreeNode *selected_node = summaryview->folderview->selected;
669         GSList *cur;
670         gint sort_mode;
671         gint sort_type;
672         static gboolean locked = FALSE;
673
674         if (summary_is_locked(summaryview)) return FALSE;
675
676         inc_lock();
677         summary_lock(summaryview);
678
679         STATUSBAR_POP(summaryview->mainwin);
680
681         is_refresh = (!prefs_common.open_inbox_on_inc &&
682                       item == summaryview->folder_item &&
683                       update_cache == FALSE) ? TRUE : FALSE;
684         if (is_refresh) {
685                 selected_msgnum = summary_get_msgnum(summaryview,
686                                                      summaryview->selected);
687                 displayed_msgnum = summary_get_msgnum(summaryview,
688                                                       summaryview->displayed);
689         }
690
691         /* process the marks if any */
692         if (summaryview->mainwin->lock_count == 0 &&
693             (summaryview->moved > 0 || summaryview->copied > 0)) {
694                 AlertValue val;
695
696                 val = alertpanel(_("Process mark"),
697                                  _("Some marks are left. Process it?"),
698                                  _("Yes"), _("No"), _("Cancel"));
699                 if (G_ALERTDEFAULT == val)
700                         summary_execute(summaryview);
701                 else if (G_ALERTALTERNATE == val)
702                         summary_write_cache(summaryview);
703                 else {
704                         summary_unlock(summaryview);
705                         inc_unlock();
706                         return FALSE;
707                 }
708                 folder_update_op_count();
709         }
710         else if (!summaryview->filtering_happened) {
711                 summary_write_cache(summaryview);
712         }
713
714         summaryview->filtering_happened = FALSE;
715
716         summaryview->folderview->opened = selected_node;
717
718         gtk_clist_freeze(GTK_CLIST(ctree));
719
720         summary_clear_list(summaryview);
721         summary_set_column_titles(summaryview);
722         summary_set_menu_sensitive(summaryview);
723         if (!is_refresh)
724                 messageview_clear(summaryview->messageview);
725
726         buf = NULL;
727         if (!item || !item->path || !item->parent || item->no_select ||
728             (item->folder->type == F_MH &&
729              ((buf = folder_item_get_path(item)) == NULL ||
730               change_dir(buf) < 0))) {
731                 g_free(buf);
732                 debug_print(_("empty folder\n\n"));
733                 if (is_refresh)
734                         messageview_clear(summaryview->messageview);
735                 summary_clear_all(summaryview);
736                 summaryview->folder_item = item;
737                 gtk_clist_thaw(GTK_CLIST(ctree));
738                 summary_unlock(summaryview);
739                 inc_unlock();
740                 return TRUE;
741         }
742         g_free(buf);
743
744         summaryview->folder_item = item;
745
746         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
747
748         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
749         debug_print("%s\n", buf);
750         STATUSBAR_PUSH(summaryview->mainwin, buf);
751         g_free(buf);
752
753         main_window_cursor_wait(summaryview->mainwin);
754
755         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
756
757         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_LABEL:
1835                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
1836                 break;
1837         default:
1838                 return;
1839         }
1840
1841         debug_print(_("Sorting summary..."));
1842         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1843
1844         main_window_cursor_wait(summaryview->mainwin);
1845
1846         gtk_clist_set_compare_func(clist, cmp_func);
1847
1848         /* toggle sort type if the same column is selected */
1849         if (summaryview->sort_mode == type)
1850                 summaryview->sort_type =
1851                         summaryview->sort_type == GTK_SORT_ASCENDING
1852                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1853         else
1854                 summaryview->sort_type = GTK_SORT_ASCENDING;
1855         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1856         summaryview->sort_mode = type;
1857
1858         summary_set_column_titles(summaryview);
1859
1860         gtk_ctree_sort_node(ctree, NULL);
1861
1862         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1863         prefs_folder_item_set_config(summaryview->folder_item,
1864                                      summaryview->sort_type,
1865                                      summaryview->sort_mode);
1866         prefs_folder_item_save_config(summaryview->folder_item);
1867
1868         debug_print(_("done.\n"));
1869         STATUSBAR_POP(summaryview->mainwin);
1870
1871         main_window_cursor_normal(summaryview->mainwin);
1872 }
1873
1874 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
1875                                    GtkCTreeNode *cnode, gpointer data)
1876 {
1877         SummaryView *summaryview = (SummaryView *)data;
1878         MsgInfo *msginfo = (MsgInfo *)gnode->data;
1879         gchar *text[N_SUMMARY_COLS];
1880         gint *col_pos = summaryview->col_pos;
1881         const gchar *msgid = msginfo->msgid;
1882         GHashTable *msgid_table = summaryview->msgid_table;
1883
1884         summary_set_header(summaryview, text, msginfo);
1885
1886         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
1887                                 NULL, NULL, NULL, NULL, FALSE,
1888                                 gnode->parent->parent ? TRUE : FALSE);
1889 #define SET_TEXT(col) \
1890         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
1891                                 text[col_pos[col]])
1892
1893         SET_TEXT(S_COL_NUMBER);
1894         SET_TEXT(S_COL_SCORE);
1895         SET_TEXT(S_COL_SIZE);
1896         SET_TEXT(S_COL_DATE);
1897         SET_TEXT(S_COL_FROM);
1898         SET_TEXT(S_COL_SUBJECT);
1899
1900 #undef SET_TEXT
1901
1902         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
1903         summary_set_marks_func(ctree, cnode, summaryview);
1904
1905         if (msgid)
1906                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
1907
1908         return TRUE;
1909 }
1910
1911 static void summary_set_ctree_from_list(SummaryView *summaryview,
1912                                         GSList *mlist)
1913 {
1914         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1915         MsgInfo *msginfo;
1916         MsgInfo *parentinfo;
1917         MsgInfo *cur_msginfo;
1918         GtkCTreeNode *node = NULL;
1919         GHashTable *msgid_table;
1920         GHashTable *subject_table;
1921         GSList * cur;
1922         GtkCTreeNode *cur_parent;
1923
1924         if (!mlist) return;
1925
1926         debug_print(_("\tSetting summary from message data..."));
1927         STATUSBAR_PUSH(summaryview->mainwin,
1928                        _("Setting summary from message data..."));
1929         gdk_flush();
1930
1931         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1932         summaryview->msgid_table = msgid_table;
1933         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
1934         summaryview->subject_table = subject_table;
1935
1936         if (prefs_common.use_addr_book)
1937                 start_address_completion();
1938         
1939         for (cur = mlist ; cur != NULL; cur = cur->next) {
1940                 msginfo = (MsgInfo *)cur->data;
1941                 msginfo->threadscore = msginfo->score;
1942         }
1943
1944         if (global_scoring || summaryview->folder_item->prefs->scoring) {
1945                 summaryview->important_score = prefs_common.important_score;
1946                 if (summaryview->folder_item->prefs->important_score >
1947                     summaryview->important_score)
1948                         summaryview->important_score =
1949                                 summaryview->folder_item->prefs->important_score;
1950         }
1951         
1952         if (summaryview->folder_item->threaded) {
1953                 GNode *root, *gnode;
1954
1955                 root = procmsg_get_thread_tree(mlist);
1956
1957                 for (gnode = root->children; gnode != NULL;
1958                      gnode = gnode->next) {
1959                         node = gtk_ctree_insert_gnode
1960                                 (ctree, NULL, node, gnode,
1961                                  summary_insert_gnode_func, summaryview);
1962                 }
1963
1964                 g_node_destroy(root);
1965
1966                 summary_thread_init(summaryview);
1967         } else {
1968                 gchar *text[N_SUMMARY_COLS];
1969
1970                 mlist = g_slist_reverse(mlist);
1971                 for (; mlist != NULL; mlist = mlist->next) {
1972                         msginfo = (MsgInfo *)mlist->data;
1973
1974                         summary_set_header(summaryview, text, msginfo);
1975
1976                         node = gtk_ctree_insert_node
1977                                 (ctree, NULL, node, text, 2,
1978                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
1979                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1980                         summary_set_marks_func(ctree, node, summaryview);
1981
1982                         if (msginfo->msgid)
1983                                 g_hash_table_insert(msgid_table,
1984                                                     msginfo->msgid, node);
1985
1986                         subject_table_insert(subject_table,
1987                                              msginfo->subject,
1988                                              node);
1989                 }
1990                 mlist = g_slist_reverse(mlist);
1991         }
1992
1993         if (prefs_common.enable_hscrollbar &&
1994             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1995                 gint optimal_width;
1996
1997                 optimal_width = gtk_clist_optimal_column_width
1998                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
1999                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2000                                            summaryview->col_pos[S_COL_SUBJECT],
2001                                            optimal_width);
2002         }
2003
2004         if (prefs_common.use_addr_book)
2005                 end_address_completion();
2006
2007         debug_print(_("done.\n"));
2008         STATUSBAR_POP(summaryview->mainwin);
2009         if (debug_mode) {
2010                 debug_print("\tmsgid hash table size = %d\n",
2011                             g_hash_table_size(msgid_table));
2012                 debug_print("\tsubject hash table size = %d\n",
2013                             g_hash_table_size(subject_table));
2014         }
2015 }
2016
2017 struct wcachefp
2018 {
2019         FILE *cache_fp;
2020         FILE *mark_fp;
2021 };
2022
2023 gint summary_write_cache(SummaryView *summaryview)
2024 {
2025         struct wcachefp fps;
2026         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2027         gint ver = CACHE_VERSION;
2028         gchar *buf;
2029         gchar *cachefile, *markfile;
2030         GSList * cur;
2031         gint filemode = 0;
2032         PrefsFolderItem *prefs;
2033
2034         if (!summaryview->folder_item || !summaryview->folder_item->path)
2035                 return -1;
2036
2037         if (summaryview->folder_item->folder->update_mark != NULL)
2038                 summaryview->folder_item->folder->update_mark(summaryview->folder_item->folder, summaryview->folder_item);
2039
2040         cachefile = folder_item_get_cache_file(summaryview->folder_item);
2041         g_return_val_if_fail(cachefile != NULL, -1);
2042         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
2043                 FILE_OP_ERROR(cachefile, "fopen");
2044                 g_free(cachefile);
2045                 return -1;
2046         }
2047         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
2048                 FILE_OP_ERROR(cachefile, "chmod");
2049
2050         prefs = summaryview->folder_item->prefs;
2051         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2052                 /* for cache file */
2053                 filemode = prefs->folder_chmod;
2054                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
2055                 if (filemode & S_IROTH) filemode |= S_IWOTH;
2056 #if HAVE_FCHMOD
2057                 fchmod(fileno(fps.cache_fp), filemode);
2058 #else
2059                 chmod(cachefile, filemode);
2060 #endif
2061         }
2062
2063         g_free(cachefile);
2064
2065         markfile = folder_item_get_mark_file(summaryview->folder_item);
2066         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
2067                 FILE_OP_ERROR(markfile, "fopen");
2068                 fclose(fps.cache_fp);
2069                 g_free(markfile);
2070                 return -1;
2071         }
2072         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
2073                 FILE_OP_ERROR(markfile, "chmod");
2074         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2075 #if HAVE_FCHMOD
2076                 fchmod(fileno(fps.mark_fp), filemode);
2077 #else
2078                 chmod(markfile, filemode);
2079 #endif
2080         }
2081
2082         g_free(markfile);
2083
2084         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
2085                               summaryview->folder_item->path);
2086         debug_print(buf);
2087         STATUSBAR_PUSH(summaryview->mainwin, buf);
2088         g_free(buf);
2089
2090         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
2091         ver = MARK_VERSION;
2092         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
2093
2094         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
2095
2096         for(cur = summaryview->killed_messages ; cur != NULL ;
2097             cur = g_slist_next(cur)) {
2098                 MsgInfo *msginfo = (MsgInfo *) cur->data;
2099
2100                 procmsg_write_cache(msginfo, fps.cache_fp);
2101                 procmsg_write_flags(msginfo, fps.mark_fp);
2102         }
2103
2104         fclose(fps.cache_fp);
2105         fclose(fps.mark_fp);
2106
2107         debug_print(_("done.\n"));
2108         STATUSBAR_POP(summaryview->mainwin);
2109
2110         return 0;
2111 }
2112
2113 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
2114                                      gpointer data)
2115 {
2116         struct wcachefp *fps = data;
2117         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
2118
2119         if (msginfo == NULL) return;
2120
2121         procmsg_write_cache(msginfo, fps->cache_fp);
2122         procmsg_write_flags(msginfo, fps->mark_fp);
2123 }
2124
2125 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2126                                MsgInfo *msginfo)
2127 {
2128         static gchar date_modified[80];
2129         static gchar *to = NULL;
2130         static gchar *from_name = NULL;
2131         static gchar col_number[11];
2132         static gchar col_score[11];
2133         gint *col_pos = summaryview->col_pos;
2134
2135         text[col_pos[S_COL_MARK]]   = NULL;
2136         text[col_pos[S_COL_UNREAD]] = NULL;
2137         text[col_pos[S_COL_MIME]]   = NULL;
2138         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2139         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2140
2141 #if 0
2142         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->threadscore);
2143 #else
2144         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2145 #endif
2146
2147         if (msginfo->date_t) {
2148                 procheader_date_get_localtime(date_modified,
2149                                               sizeof(date_modified),
2150                                               msginfo->date_t);
2151                 text[col_pos[S_COL_DATE]] = date_modified;
2152         } else if (msginfo->date)
2153                 text[col_pos[S_COL_DATE]] = msginfo->date;
2154         else
2155                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2156
2157         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2158                 _("(No From)");
2159         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2160             !MSG_IS_NEWS(msginfo->flags)) {
2161                 gchar *from;
2162
2163                 Xstrdup_a(from, msginfo->from, return);
2164                 extract_address(from);
2165                 if (account_find_from_address(from)) {
2166                         g_free(to);
2167                         to = g_strconcat("-->", msginfo->to, NULL);
2168                         text[col_pos[S_COL_FROM]] = to;
2169                 }
2170         }
2171
2172         if ((text[col_pos[S_COL_FROM]] != to) && prefs_common.use_addr_book &&
2173             msginfo->from) {
2174                 gint count;
2175                 gchar *from;
2176   
2177                 Xstrdup_a(from, msginfo->from, return);
2178                 extract_address(from);
2179                 if (*from) {
2180                         count = complete_address(from);
2181                         if (count > 1) {
2182                                 g_free(from_name);
2183                                 from = get_complete_address(1);
2184                                 from_name = procheader_get_fromname(from);
2185                                 g_free(from);
2186                                 text[col_pos[S_COL_FROM]] = from_name;
2187                         }
2188                 }
2189         }
2190
2191         text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2192                 _("(No Subject)");
2193 }
2194
2195 #define CHANGE_FLAGS(msginfo) \
2196 { \
2197 if (msginfo->folder->folder->change_flags != NULL) \
2198 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2199                                       msginfo->folder, \
2200                                       msginfo); \
2201 }
2202
2203 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
2204                                 gboolean new_window)
2205 {
2206         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2207         MsgInfo *msginfo;
2208         gchar *filename;
2209
2210         if (!new_window && summaryview->displayed == row) return;
2211         g_return_if_fail(row != NULL);
2212
2213         if (summary_is_locked(summaryview)) return;
2214         summary_lock(summaryview);
2215
2216         STATUSBAR_POP(summaryview->mainwin);
2217         GTK_EVENTS_FLUSH();
2218
2219         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2220
2221         filename = procmsg_get_message_file(msginfo);
2222         if (!filename) {
2223                 summary_unlock(summaryview);
2224                 return;
2225         }
2226         g_free(filename);
2227
2228         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2229                 summaryview->newmsgs--;
2230         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2231                 summaryview->unread--;
2232
2233         if (new_window) {
2234                 MessageView *msgview;
2235
2236                 msgview = messageview_create_with_new_window();
2237                 messageview_show(msgview, msginfo);
2238         } else {
2239                 MessageView *msgview;
2240
2241                 msgview = summaryview->messageview;
2242
2243                 summaryview->displayed = row;
2244                 if (!summaryview->msg_is_toggled_on)
2245                         summary_toggle_view(summaryview);
2246                 messageview_show(msgview, msginfo);
2247                 if (msgview->type == MVIEW_TEXT ||
2248                     (msgview->type == MVIEW_MIME &&
2249                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
2250                         gtk_widget_grab_focus(summaryview->ctree);
2251                 GTK_EVENTS_FLUSH();
2252                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2253         }
2254
2255         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2256                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2257                 CHANGE_FLAGS(msginfo);
2258                 summary_set_row_marks(summaryview, row);
2259                 gtk_clist_thaw(GTK_CLIST(ctree));
2260                 summary_status_show(summaryview);
2261         }
2262
2263         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
2264                 header_window_show(summaryview->headerwin, msginfo);
2265
2266         summary_unlock(summaryview);
2267 }
2268
2269 void summary_redisplay_msg(SummaryView *summaryview)
2270 {
2271         GtkCTreeNode *node;
2272
2273         if (summaryview->displayed) {
2274                 node = summaryview->displayed;
2275                 summaryview->displayed = NULL;
2276                 summary_display_msg(summaryview, node, FALSE);
2277         }
2278 }
2279
2280 void summary_open_msg(SummaryView *summaryview)
2281 {
2282         if (!summaryview->selected) return;
2283
2284         summary_display_msg(summaryview, summaryview->selected, TRUE);
2285 }
2286
2287 void summary_view_source(SummaryView * summaryview)
2288 {
2289         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2290         MsgInfo *msginfo;
2291         SourceWindow *srcwin;
2292
2293         if (!summaryview->selected) return;
2294
2295         srcwin = source_window_create();
2296         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2297         source_window_show_msg(srcwin, msginfo);
2298         source_window_show(srcwin);
2299 }
2300
2301 void summary_reedit(SummaryView *summaryview)
2302 {
2303         MsgInfo *msginfo;
2304
2305         if (!summaryview->selected) return;
2306         if (!summaryview->folder_item) return;
2307         if (summaryview->folder_item->stype != F_OUTBOX &&
2308             summaryview->folder_item->stype != F_DRAFT  &&
2309             summaryview->folder_item->stype != F_QUEUE) return;
2310
2311         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2312                                               summaryview->selected);
2313         if (!msginfo) return;
2314
2315         compose_reedit(msginfo);
2316 }
2317
2318 void summary_step(SummaryView *summaryview, GtkScrollType type)
2319 {
2320         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2321
2322         if (type == GTK_SCROLL_STEP_FORWARD) {
2323                 GtkCTreeNode *node;
2324                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2325                 if (node)
2326                         gtkut_ctree_expand_parent_all(ctree, node);
2327         }
2328
2329         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2330                                 type, 0.0);
2331         
2332         if (summaryview->msg_is_toggled_on)
2333                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2334 }
2335
2336 static void summary_toggle_view(SummaryView *summaryview)
2337 {
2338         MainWindow *mainwin = summaryview->mainwin;
2339         union CompositeWin *cwin = &mainwin->win;
2340         GtkWidget *vpaned = NULL;
2341         GtkWidget *container = NULL;
2342
2343         switch (mainwin->type) {
2344         case SEPARATE_NONE:
2345                 vpaned = cwin->sep_none.vpaned;
2346                 container = cwin->sep_none.hpaned;
2347                 break;
2348         case SEPARATE_FOLDER:
2349                 vpaned = cwin->sep_folder.vpaned;
2350                 container = mainwin->vbox_body;
2351                 break;
2352         case SEPARATE_MESSAGE:
2353         case SEPARATE_BOTH:
2354                 return;
2355         }
2356
2357         if (vpaned->parent != NULL) {
2358                 summaryview->msg_is_toggled_on = FALSE;
2359                 summaryview->displayed = NULL;
2360                 gtk_widget_ref(vpaned);
2361                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
2362                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
2363                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2364                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2365         } else {
2366                 summaryview->msg_is_toggled_on = TRUE;
2367                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
2368                 gtk_container_add(GTK_CONTAINER(container), vpaned);
2369                 gtk_widget_unref(vpaned);
2370                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2371                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2372         }
2373
2374         gtk_widget_grab_focus(summaryview->ctree);
2375 }
2376
2377 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2378                                                 GtkCTreeNode *node)
2379 {
2380         MsgInfo *msginfo;
2381
2382         if (node) {
2383                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2384                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2385                         return TRUE;
2386                 node = GTK_CTREE_ROW(node)->children;
2387         } else
2388                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2389
2390         while (node) {
2391                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2392                         return TRUE;
2393                 node = GTK_CTREE_ROW(node)->sibling;
2394         }
2395
2396         return FALSE;
2397 }
2398
2399 static gboolean summary_have_unread_children(SummaryView *summaryview,
2400                                              GtkCTreeNode *node)
2401 {
2402         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2403
2404         if (!node) return FALSE;
2405
2406         node = GTK_CTREE_ROW(node)->children;
2407
2408         while (node) {
2409                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2410                         return TRUE;
2411                 node = GTK_CTREE_ROW(node)->sibling;
2412         }
2413
2414         return FALSE;
2415 }
2416
2417 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2418 {
2419         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2420         GtkStyle *style = NULL;
2421         MsgInfo *msginfo;
2422         MsgFlags flags;
2423         gint *col_pos = summaryview->col_pos;
2424
2425         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2426         if (!msginfo) return;
2427
2428         flags = msginfo->flags;
2429
2430         gtk_ctree_node_set_foreground(ctree, row, NULL);
2431
2432         /* set new/unread column */
2433         if (MSG_IS_IGNORE_THREAD(flags)) {
2434                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2435                                           ignorethreadxpm, ignorethreadxpmmask);
2436         } else if (MSG_IS_NEW(flags)) {
2437                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2438                                           newxpm, newxpmmask);
2439         } else if (MSG_IS_UNREAD(flags)) {
2440                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2441                                           unreadxpm, unreadxpmmask);
2442         } else if (MSG_IS_REPLIED(flags)) {
2443                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2444                                           repliedxpm, repliedxpmmask);
2445         } else if (MSG_IS_FORWARDED(flags)) {
2446                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2447                                           forwardedxpm, forwardedxpmmask);
2448         } else {
2449                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2450                                         NULL);
2451         }
2452
2453         if (prefs_common.bold_unread &&
2454             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2455              (!GTK_CTREE_ROW(row)->expanded &&
2456               GTK_CTREE_ROW(row)->children &&
2457               summary_have_unread_children(summaryview, row))))
2458                 style = bold_style;
2459
2460         /* set mark column */
2461         if (MSG_IS_DELETED(flags)) {
2462                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2463                                           deletedxpm, deletedxpmmask);
2464                 if (style)
2465                         style = bold_deleted_style;
2466                 else {
2467                         style = small_deleted_style;
2468                 }
2469                         gtk_ctree_node_set_foreground
2470                                 (ctree, row, &summaryview->color_dim);
2471         } else if (MSG_IS_MARKED(flags)) {
2472                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2473                                           markxpm, markxpmmask);
2474         } else if (MSG_IS_MOVE(flags)) {
2475                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2476                 if (style)
2477                         style = bold_marked_style;
2478                 else {
2479                         style = small_marked_style;
2480                 }
2481                         gtk_ctree_node_set_foreground
2482                                 (ctree, row, &summaryview->color_marked);
2483         } else if (MSG_IS_COPY(flags)) {
2484                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2485                 if (style)
2486                         style = bold_marked_style;
2487                 else {
2488                         style = small_marked_style;
2489                 }
2490                         gtk_ctree_node_set_foreground
2491                                 (ctree, row, &summaryview->color_marked);
2492         }
2493         else if ((global_scoring ||
2494                   summaryview->folder_item->prefs->scoring) &&
2495                  (msginfo->score >= summaryview->important_score) &&
2496                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2497                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2498                 gtk_ctree_node_set_foreground(ctree, row,
2499                                               &summaryview->color_important);
2500         } else {
2501                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2502         }
2503
2504         if (MSG_IS_LOCKED(flags)) {
2505                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2506                                           lockedxpm, lockedxpmmask);
2507         }
2508         else {
2509                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2510         }
2511
2512         if (MSG_IS_MIME(flags)) {
2513                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2514                                           clipxpm, clipxpmmask);
2515         } else {
2516                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2517         }
2518         if (!style)
2519                 style = small_style;
2520
2521         gtk_ctree_node_set_row_style(ctree, row, style);
2522
2523         if (MSG_GET_COLORLABEL(flags))
2524                 summary_set_colorlabel_color(ctree, row,
2525                                              MSG_GET_COLORLABEL_VALUE(flags));
2526 }
2527
2528 void summary_set_marks_selected(SummaryView *summaryview)
2529 {
2530         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2531         GList *cur;
2532
2533         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2534                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2535 }
2536
2537 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2538 {
2539         gboolean changed = FALSE;
2540         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2541         MsgInfo *msginfo;
2542
2543         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2544         if (MSG_IS_DELETED(msginfo->flags))
2545                 summaryview->deleted--;
2546         if (MSG_IS_MOVE(msginfo->flags)) {
2547                 summaryview->moved--;
2548                 changed = TRUE;
2549         }
2550         if (MSG_IS_COPY(msginfo->flags)) {
2551                 summaryview->copied--;
2552                 changed = TRUE;
2553         }
2554         if (changed && !prefs_common.immediate_exec) {
2555                 msginfo->to_folder->op_count--;
2556                 if (msginfo->to_folder->op_count == 0)
2557                         folderview_update_item(msginfo->to_folder, 0);
2558         }
2559         msginfo->to_folder = NULL;
2560         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2561         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2562         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2563         CHANGE_FLAGS(msginfo);
2564         summary_set_row_marks(summaryview, row);
2565         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
2566 }
2567
2568 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2569 {
2570         /* almost verbatim summary_mark_row(); may want a menu action? */
2571         gboolean changed = FALSE;
2572         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2573         MsgInfo *msginfo;
2574
2575         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2576         if (MSG_IS_DELETED(msginfo->flags))
2577                 summaryview->deleted--;
2578         if (MSG_IS_MOVE(msginfo->flags)) {
2579                 summaryview->moved--;
2580                 changed = TRUE;
2581         }
2582         if (MSG_IS_COPY(msginfo->flags)) {
2583                 summaryview->copied--;
2584                 changed = TRUE;
2585         }
2586         if (changed && !prefs_common.immediate_exec) {
2587                 msginfo->to_folder->op_count--;
2588                 if (msginfo->to_folder->op_count == 0)
2589                         folderview_update_item(msginfo->to_folder, 0);
2590         }
2591         msginfo->to_folder = NULL;
2592         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2593         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2594         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_LOCKED);
2595         CHANGE_FLAGS(msginfo);
2596         summary_set_row_marks(summaryview, row);
2597         debug_print(_("Message %d is locked\n"), msginfo->msgnum);
2598 }
2599
2600 void summary_mark(SummaryView *summaryview)
2601 {
2602         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2603         GList *cur;
2604
2605         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2606                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2607
2608         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2609         summary_status_show(summaryview);
2610 }
2611
2612 static void summary_mark_row_as_read(SummaryView *summaryview,
2613                                      GtkCTreeNode *row)
2614 {
2615         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2616         MsgInfo *msginfo;
2617
2618         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2619         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2620                 summaryview->newmsgs--;
2621         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2622                 summaryview->unread--;
2623         if (MSG_IS_NEW(msginfo->flags) ||
2624             MSG_IS_UNREAD(msginfo->flags)) {
2625                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2626                 CHANGE_FLAGS(msginfo);
2627                 summary_set_row_marks(summaryview, row);
2628                 debug_print(_("Message %d is marked as read\n"),
2629                             msginfo->msgnum);
2630         }
2631 }
2632
2633 void summary_mark_as_read(SummaryView *summaryview)
2634 {
2635         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2636         GList *cur;
2637
2638         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2639                 summary_mark_row_as_read(summaryview,
2640                                          GTK_CTREE_NODE(cur->data));
2641
2642         summary_status_show(summaryview);
2643 }
2644
2645 void summary_mark_all_read(SummaryView *summaryview)
2646 {
2647         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2648         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2649         GtkCTreeNode *node;
2650
2651         gtk_clist_freeze(clist);
2652         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2653              node = gtkut_ctree_node_next(ctree, node))
2654                 summary_mark_row_as_read(summaryview, node);
2655         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2656              node = gtkut_ctree_node_next(ctree, node)) {
2657                 if (!GTK_CTREE_ROW(node)->expanded)
2658                         summary_set_row_marks(summaryview, node);
2659         }
2660         gtk_clist_thaw(clist);
2661
2662         summary_status_show(summaryview);
2663 }
2664
2665 static void summary_mark_row_as_unread(SummaryView *summaryview,
2666                                        GtkCTreeNode *row)
2667 {
2668         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2669         MsgInfo *msginfo;
2670
2671         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2672         if (MSG_IS_DELETED(msginfo->flags)) {
2673                 msginfo->to_folder = NULL;
2674                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2675                 summaryview->deleted--;
2676         }
2677         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2678         if (!MSG_IS_UNREAD(msginfo->flags)) {
2679                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
2680                 summaryview->unread++;
2681                 debug_print(_("Message %d is marked as unread\n"),
2682                             msginfo->msgnum);
2683         }
2684
2685         CHANGE_FLAGS(msginfo);
2686
2687         summary_set_row_marks(summaryview, row);
2688 }
2689
2690 void summary_mark_as_unread(SummaryView *summaryview)
2691 {
2692         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2693         GList *cur;
2694
2695         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2696                 summary_mark_row_as_unread(summaryview,
2697                                            GTK_CTREE_NODE(cur->data));
2698
2699         summary_status_show(summaryview);
2700 }
2701
2702 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2703 {
2704         gboolean changed = FALSE;
2705         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2706         MsgInfo *msginfo;
2707
2708         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2709
2710         if (MSG_IS_LOCKED(msginfo->flags)) return;
2711
2712         if (MSG_IS_DELETED(msginfo->flags)) return;
2713
2714         if (MSG_IS_MOVE(msginfo->flags)) {
2715                 summaryview->moved--;
2716                 changed = TRUE;
2717         }
2718         if (MSG_IS_COPY(msginfo->flags)) {
2719                 summaryview->copied--;
2720                 changed = TRUE;
2721         }
2722         if (changed && !prefs_common.immediate_exec) {
2723                 msginfo->to_folder->op_count--;
2724                 if (msginfo->to_folder->op_count == 0)
2725                         folderview_update_item(msginfo->to_folder, 0);
2726         }
2727         msginfo->to_folder = NULL;
2728         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2729         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2730         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2731         CHANGE_FLAGS(msginfo);
2732         summaryview->deleted++;
2733
2734         if (!prefs_common.immediate_exec && 
2735             summaryview->folder_item->stype != F_TRASH)
2736                 summary_set_row_marks(summaryview, row);
2737
2738         debug_print(_("Message %s/%d is set to delete\n"),
2739                     msginfo->folder->path, msginfo->msgnum);
2740 }
2741
2742 void summary_delete(SummaryView *summaryview)
2743 {
2744         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2745         FolderItem *item = summaryview->folder_item;
2746         GList *cur;
2747         GtkCTreeNode *sel_last = NULL;
2748         GtkCTreeNode *node;
2749
2750         if (!item || item->folder->type == F_NEWS) return;
2751
2752         if (summary_is_locked(summaryview)) return;
2753
2754         /* if current folder is trash, ask for confirmation */
2755         if (item->stype == F_TRASH) {
2756                 AlertValue aval;
2757
2758                 aval = alertpanel(_("Delete message(s)"),
2759                                   _("Do you really want to delete message(s) from the trash?"),
2760                                   _("Yes"), _("No"), NULL);
2761                 if (aval != G_ALERTDEFAULT) return;
2762         }
2763
2764         /* next code sets current row focus right. We need to find a row
2765          * that is not deleted. */
2766         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2767                 sel_last = GTK_CTREE_NODE(cur->data);
2768                 summary_delete_row(summaryview, sel_last);
2769         }
2770
2771         node = summary_find_next_msg(summaryview, sel_last);
2772         if (!node)
2773                 node = summary_find_prev_msg(summaryview, sel_last);
2774
2775         if (node) {
2776                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2777                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2778                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2779                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2780                 else
2781                         summary_select_node(summaryview, node,
2782                                             summaryview->msg_is_toggled_on);
2783         }
2784
2785         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2786                 summary_execute(summaryview);
2787         else
2788                 summary_status_show(summaryview);
2789 }
2790
2791 void summary_delete_duplicated(SummaryView *summaryview)
2792 {
2793         if (!summaryview->folder_item ||
2794             summaryview->folder_item->folder->type == F_NEWS) return;
2795         if (summaryview->folder_item->stype == F_TRASH) return;
2796
2797         main_window_cursor_wait(summaryview->mainwin);
2798         debug_print(_("Deleting duplicated messages..."));
2799         STATUSBAR_PUSH(summaryview->mainwin,
2800                        _("Deleting duplicated messages..."));
2801
2802         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2803                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2804                                 summaryview);
2805
2806         if (prefs_common.immediate_exec)
2807                 summary_execute(summaryview);
2808         else
2809                 summary_status_show(summaryview);
2810
2811         debug_print(_("done.\n"));
2812         STATUSBAR_POP(summaryview->mainwin);
2813         main_window_cursor_normal(summaryview->mainwin);
2814 }
2815
2816 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2817                                            SummaryView *summaryview)
2818 {
2819         GtkCTreeNode *found;
2820         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2821
2822         if (!msginfo->msgid || !*msginfo->msgid) return;
2823
2824         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2825
2826         if (found && found != node)
2827                 summary_delete_row(summaryview, node);
2828 }
2829
2830 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2831 {
2832         gboolean changed = FALSE;
2833         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2834         MsgInfo *msginfo;
2835
2836         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2837         if (MSG_IS_DELETED(msginfo->flags))
2838                 summaryview->deleted--;
2839         if (MSG_IS_MOVE(msginfo->flags)) {
2840                 summaryview->moved--;
2841                 changed = TRUE;
2842         }
2843         if (MSG_IS_COPY(msginfo->flags)) {
2844                 summaryview->copied--;
2845                 changed = TRUE;
2846         }
2847         if (changed && !prefs_common.immediate_exec) {
2848                 msginfo->to_folder->op_count--;
2849                 if (msginfo->to_folder->op_count == 0)
2850                         folderview_update_item(msginfo->to_folder, 0);
2851         }
2852         msginfo->to_folder = NULL;
2853         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
2854         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2855         CHANGE_FLAGS(msginfo);
2856         summary_set_row_marks(summaryview, row);
2857
2858         debug_print(_("Message %s/%d is unmarked\n"),
2859                     msginfo->folder->path, msginfo->msgnum);
2860 }
2861
2862 void summary_unmark(SummaryView *summaryview)
2863 {
2864         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2865         GList *cur;
2866
2867         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2868              cur = cur->next)
2869                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2870
2871         summary_status_show(summaryview);
2872 }
2873
2874 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2875                                 FolderItem *to_folder)
2876 {
2877         gboolean changed = FALSE;
2878         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2879         MsgInfo *msginfo;
2880
2881         g_return_if_fail(to_folder != NULL);
2882
2883         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2884         if (MSG_IS_MOVE(msginfo->flags)) {
2885                 if (!prefs_common.immediate_exec) {
2886                         msginfo->to_folder->op_count--;
2887                         if (msginfo->to_folder->op_count == 0) {
2888                                 folderview_update_item(msginfo->to_folder, 0);
2889                                 changed = TRUE;
2890                         }
2891                 }
2892         }
2893         msginfo->to_folder = to_folder;
2894         if (MSG_IS_DELETED(msginfo->flags))
2895                 summaryview->deleted--;
2896         if (MSG_IS_COPY(msginfo->flags)) {
2897                 summaryview->copied--;
2898                 if (!prefs_common.immediate_exec)
2899                         msginfo->to_folder->op_count--;
2900         }
2901         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
2902         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_COPY);
2903         if (!MSG_IS_MOVE(msginfo->flags)) {
2904                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
2905                 summaryview->moved++;
2906                 changed = TRUE;
2907         }
2908         if (!prefs_common.immediate_exec) {
2909                 summary_set_row_marks(summaryview, row);
2910                 if (changed) {
2911                         msginfo->to_folder->op_count++;
2912                         if (msginfo->to_folder->op_count == 1)
2913                                 folderview_update_item(msginfo->to_folder, 0);
2914                 }
2915         }
2916
2917         debug_print(_("Message %d is set to move to %s\n"),
2918                     msginfo->msgnum, to_folder->path);
2919 }
2920
2921 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2922 {
2923         GList *cur;
2924
2925         if (!to_folder) return;
2926         if (!summaryview->folder_item ||
2927             summaryview->folder_item->folder->type == F_NEWS) return;
2928
2929         if (summary_is_locked(summaryview)) return;
2930
2931         if (summaryview->folder_item == to_folder) {
2932                 alertpanel_notice(_("Destination is same as current folder."));
2933                 return;
2934         }
2935
2936         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2937              cur != NULL; cur = cur->next)
2938                 summary_move_row_to
2939                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2940
2941         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2942
2943         if (prefs_common.immediate_exec)
2944                 summary_execute(summaryview);
2945         else {
2946                 summary_status_show(summaryview);
2947
2948                 folderview_update_item(to_folder, 0);
2949         }
2950 }
2951
2952 void summary_move_to(SummaryView *summaryview)
2953 {
2954         FolderItem *to_folder;
2955
2956         if (!summaryview->folder_item ||
2957             summaryview->folder_item->folder->type == F_NEWS) return;
2958
2959         to_folder = foldersel_folder_sel(NULL, NULL);
2960         summary_move_selected_to(summaryview, to_folder);
2961 }
2962
2963 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2964                                 FolderItem *to_folder)
2965 {
2966         gboolean changed = FALSE;
2967         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2968         MsgInfo *msginfo;
2969
2970         g_return_if_fail(to_folder != NULL);
2971
2972         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2973         if (MSG_IS_COPY(msginfo->flags)) {
2974                 if (!prefs_common.immediate_exec) {
2975                         msginfo->to_folder->op_count--;
2976                         if (msginfo->to_folder->op_count == 0) {
2977                                 folderview_update_item(msginfo->to_folder, 0);
2978                                 changed = TRUE;
2979                         }
2980                 }
2981         }
2982         msginfo->to_folder = to_folder;
2983         if (MSG_IS_DELETED(msginfo->flags))
2984                 summaryview->deleted--;
2985         if (MSG_IS_MOVE(msginfo->flags)) {
2986                 summaryview->moved--;
2987                 if (!prefs_common.immediate_exec)
2988                         msginfo->to_folder->op_count--;
2989         }
2990         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
2991         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
2992         if (!MSG_IS_COPY(msginfo->flags)) {
2993                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_COPY);
2994                 summaryview->copied++;
2995                 changed = TRUE;
2996         }
2997         if (!prefs_common.immediate_exec) {
2998                 summary_set_row_marks(summaryview, row);
2999                 if (changed) {
3000                         msginfo->to_folder->op_count++;
3001                         if (msginfo->to_folder->op_count == 1)
3002                                 folderview_update_item(msginfo->to_folder, 0);
3003                 }
3004         }
3005
3006         debug_print(_("Message %d is set to copy to %s\n"),
3007                     msginfo->msgnum, to_folder->path);
3008 }
3009
3010 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3011 {
3012         GList *cur;
3013
3014         if (!to_folder) return;
3015         if (!summaryview->folder_item ||
3016             summaryview->folder_item->folder->type == F_NEWS) return;
3017
3018         if (summary_is_locked(summaryview)) return;
3019
3020         if (summaryview->folder_item == to_folder) {
3021                 alertpanel_notice
3022                         (_("Destination to copy is same as current folder."));
3023                 return;
3024         }
3025
3026         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3027              cur != NULL; cur = cur->next)
3028                 summary_copy_row_to
3029                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3030
3031         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3032
3033         if (prefs_common.immediate_exec)
3034                 summary_execute(summaryview);
3035         else {
3036                 summary_status_show(summaryview);
3037
3038                 folderview_update_item(to_folder, 0);
3039         }
3040 }
3041
3042 void summary_copy_to(SummaryView *summaryview)
3043 {
3044         FolderItem *to_folder;
3045
3046         if (!summaryview->folder_item ||
3047             summaryview->folder_item->folder->type == F_NEWS) return;
3048
3049         to_folder = foldersel_folder_sel(NULL, NULL);
3050         summary_copy_selected_to(summaryview, to_folder);
3051 }
3052
3053 void summary_add_address(SummaryView *summaryview)
3054 {
3055         MsgInfo *msginfo;
3056         gchar *from;
3057
3058         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3059                                               summaryview->selected);
3060         if (!msginfo) return;
3061
3062         Xstrdup_a(from, msginfo->from, return);
3063         eliminate_address_comment(from);
3064         extract_address(from);
3065         addressbook_add_contact(msginfo->fromname, from, NULL);
3066 }
3067
3068 void summary_select_all(SummaryView *summaryview)
3069 {
3070         if (summaryview->messages >= 500) {
3071                 STATUSBAR_PUSH(summaryview->mainwin,
3072                                _("Selecting all messages..."));
3073                 main_window_cursor_wait(summaryview->mainwin);
3074         }
3075
3076         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3077
3078         if (summaryview->messages >= 500) {
3079                 STATUSBAR_POP(summaryview->mainwin);
3080                 main_window_cursor_normal(summaryview->mainwin);
3081         }
3082 }
3083
3084 void summary_unselect_all(SummaryView *summaryview)
3085 {
3086         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3087 }
3088
3089 void summary_save_as(SummaryView *summaryview)
3090 {
3091         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3092         MsgInfo *msginfo;
3093         gchar *src, *dest;
3094
3095         if (!summaryview->selected) return;
3096         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3097         if (!msginfo) return;
3098
3099         dest = filesel_select_file(_("Save as"), NULL);
3100         if (!dest) return;
3101         if (is_file_exist(dest)) {
3102                 AlertValue aval;
3103
3104                 aval = alertpanel(_("Overwrite"),
3105                                   _("Overwrite existing file?"),
3106                                   _("OK"), _("Cancel"), NULL);
3107                 if (G_ALERTDEFAULT != aval) return;
3108         }
3109
3110         src = procmsg_get_message_file(msginfo);
3111         copy_file(src, dest);
3112         g_free(src);
3113 }
3114
3115 void summary_print(SummaryView *summaryview)
3116 {
3117         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3118         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3119         MsgInfo *msginfo;
3120         GList *cur;
3121         gchar *cmdline;
3122         gchar *p;
3123
3124         if (clist->selection == NULL) return;
3125
3126         cmdline = input_dialog(_("Print"),
3127                                _("Enter the print command line:\n"
3128                                  "(`%s' will be replaced with file name)"),
3129                                prefs_common.print_cmd);
3130         if (!cmdline) return;
3131         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3132             strchr(p + 2, '%')) {
3133                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3134                                  cmdline);
3135                 g_free(cmdline);
3136                 return;
3137         }
3138
3139         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3140                 msginfo = gtk_ctree_node_get_row_data
3141                         (ctree, GTK_CTREE_NODE(cur->data));
3142                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3143         }
3144
3145         g_free(cmdline);
3146 }
3147
3148 gboolean summary_execute(SummaryView *summaryview)
3149 {
3150         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3151         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3152         GtkCTreeNode *node, *next;
3153
3154         if (!summaryview->folder_item ||
3155             summaryview->folder_item->folder->type == F_NEWS) return FALSE;
3156
3157         if (summary_is_locked(summaryview)) return FALSE;
3158         summary_lock(summaryview);
3159
3160         gtk_clist_freeze(clist);
3161
3162         if (summaryview->folder_item->threaded)
3163                 summary_unthread_for_exec(summaryview);
3164
3165         summary_execute_move(summaryview);
3166         summary_execute_copy(summaryview);
3167         summary_execute_delete(summaryview);
3168
3169         node = GTK_CTREE_NODE(clist->row_list);
3170         while (node != NULL) {
3171                 next = gtkut_ctree_node_next(ctree, node);
3172                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
3173                         if (node == summaryview->displayed) {
3174                                 messageview_clear(summaryview->messageview);
3175                                 summaryview->displayed = NULL;
3176                         }
3177                         if (GTK_CTREE_ROW(node)->children != NULL)
3178                                 g_warning("summary_execute(): children != NULL\n");
3179                         else
3180                                 gtk_ctree_remove_node(ctree, node);
3181                 }
3182                 node = next;
3183         }
3184
3185         if (summaryview->folder_item->threaded)
3186                 summary_thread_build(summaryview);
3187
3188         summaryview->selected = clist->selection ?
3189                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3190
3191         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3192                 menu_set_insensitive_all
3193                         (GTK_MENU_SHELL(summaryview->popupmenu));
3194                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3195         } else
3196                 gtk_widget_grab_focus(summaryview->ctree);
3197
3198         summary_update_status(summaryview);
3199         summary_status_show(summaryview);
3200
3201         summary_write_cache(summaryview);
3202
3203         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3204
3205         gtk_clist_thaw(clist);
3206
3207         summary_unlock(summaryview);
3208         return TRUE;
3209 }
3210
3211 static void summary_execute_move(SummaryView *summaryview)
3212 {
3213         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3214         GSList *cur;
3215
3216         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3217
3218         /* search moving messages and execute */
3219         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3220                                 summaryview);
3221
3222         if (summaryview->mlist) {
3223                 procmsg_move_messages(summaryview->mlist);
3224
3225                 folder_item_scan_foreach(summaryview->folder_table);
3226                 folderview_update_item_foreach(summaryview->folder_table);
3227
3228                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3229                         procmsg_msginfo_free((MsgInfo *)cur->data);
3230                 g_slist_free(summaryview->mlist);
3231                 summaryview->mlist = NULL;
3232         }
3233
3234         g_hash_table_destroy(summaryview->folder_table);
3235         summaryview->folder_table = NULL;
3236 }
3237
3238 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3239                                       gpointer data)
3240 {
3241         SummaryView *summaryview = data;
3242         MsgInfo *msginfo;
3243
3244         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3245
3246         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3247                 g_hash_table_insert(summaryview->folder_table,
3248                                     msginfo->to_folder, GINT_TO_POINTER(1));
3249
3250                 summaryview->mlist =
3251                         g_slist_append(summaryview->mlist, msginfo);
3252                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3253
3254                 if (msginfo->msgid && *msginfo->msgid &&
3255                     node == g_hash_table_lookup(summaryview->msgid_table,
3256                                                 msginfo->msgid))
3257                         g_hash_table_remove(summaryview->msgid_table,
3258                                             msginfo->msgid);
3259         }
3260 }
3261
3262 static void summary_execute_copy(SummaryView *summaryview)
3263 {
3264         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3265
3266         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3267
3268         /* search copying messages and execute */
3269         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3270                                 summaryview);
3271
3272         if (summaryview->mlist) {
3273                 procmsg_copy_messages(summaryview->mlist);
3274
3275                 folder_item_scan_foreach(summaryview->folder_table);
3276                 folderview_update_item_foreach(summaryview->folder_table);
3277
3278                 g_slist_free(summaryview->mlist);
3279                 summaryview->mlist = NULL;
3280         }
3281
3282         g_hash_table_destroy(summaryview->folder_table);
3283         summaryview->folder_table = NULL;
3284 }
3285
3286 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3287                                       gpointer data)
3288 {
3289         SummaryView *summaryview = data;
3290         MsgInfo *msginfo;
3291
3292         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3293
3294         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3295                 g_hash_table_insert(summaryview->folder_table,
3296                                     msginfo->to_folder, GINT_TO_POINTER(1));
3297
3298                 summaryview->mlist =
3299                         g_slist_append(summaryview->mlist, msginfo);
3300
3301                 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_COPY);
3302                 summary_set_row_marks(summaryview, node);
3303         }
3304 }
3305
3306 static void summary_execute_delete(SummaryView *summaryview)
3307 {
3308         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3309         FolderItem *trash;
3310         GSList *cur;
3311
3312         trash = summaryview->folder_item->folder->trash;
3313         if (summaryview->folder_item->folder->type == F_MH) {
3314                 g_return_if_fail(trash != NULL);
3315         }
3316
3317         /* search deleting messages and execute */
3318         gtk_ctree_pre_recursive
3319                 (ctree, NULL, summary_execute_delete_func, summaryview);
3320
3321         if (!summaryview->mlist) return;
3322
3323         if (summaryview->folder_item != trash)
3324                 folder_item_move_msgs_with_dest(trash, summaryview->mlist);
3325         else
3326                 folder_item_remove_msgs(trash, summaryview->mlist);
3327
3328         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3329                 procmsg_msginfo_free((MsgInfo *)cur->data);
3330
3331         g_slist_free(summaryview->mlist);
3332         summaryview->mlist = NULL;
3333
3334         if (summaryview->folder_item != trash) {
3335                 folder_item_scan(trash);
3336                 folderview_update_item(trash, FALSE);
3337         }
3338 }
3339
3340 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3341                                         gpointer data)
3342 {
3343         SummaryView *summaryview = data;
3344         MsgInfo *msginfo;
3345
3346         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3347
3348         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3349                 summaryview->mlist =
3350                         g_slist_append(summaryview->mlist, msginfo);
3351                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3352
3353                 if (msginfo->msgid && *msginfo->msgid &&
3354                     node == g_hash_table_lookup(summaryview->msgid_table,
3355                                                 msginfo->msgid))
3356                         g_hash_table_remove(summaryview->msgid_table,
3357                                             msginfo->msgid);
3358         }
3359 }
3360
3361 /* thread functions */
3362
3363 void summary_thread_build(SummaryView *summaryview)
3364 {
3365         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3366         GtkCTreeNode *node;
3367         GtkCTreeNode *next;
3368         GtkCTreeNode *parent;
3369         MsgInfo *msginfo;
3370
3371         summary_lock(summaryview);
3372
3373         debug_print(_("Building threads..."));
3374         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3375         main_window_cursor_wait(summaryview->mainwin);
3376