9afef154ccacd8adf06dd53826121e4f8c8c248c
[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
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include <unistd.h>
47
48 #include "intl.h"
49 #include "main.h"
50 #include "menu.h"
51 #include "mainwindow.h"
52 #include "folderview.h"
53 #include "summaryview.h"
54 #include "messageview.h"
55 #include "foldersel.h"
56 #include "procmsg.h"
57 #include "procheader.h"
58 #include "headerwindow.h"
59 #include "sourcewindow.h"
60 #include "prefs_common.h"
61 #include "account.h"
62 #include "compose.h"
63 #include "utils.h"
64 #include "gtkutils.h"
65 #include "filesel.h"
66 #include "alertpanel.h"
67 #include "inputdialog.h"
68 #include "statusbar.h"
69 #include "filter.h"
70 #include "folder.h"
71
72 #include "pixmaps/dir-open.xpm"
73 #include "pixmaps/mark.xpm"
74 #include "pixmaps/deleted.xpm"
75 #include "pixmaps/new.xpm"
76 #include "pixmaps/unread.xpm"
77 #include "pixmaps/replied.xpm"
78 #include "pixmaps/forwarded.xpm"
79 #include "pixmaps/clip.xpm"
80
81 #define STATUSBAR_PUSH(mainwin, str) \
82 { \
83         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
84                            mainwin->summaryview_cid, str); \
85         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
86 }
87
88 #define STATUSBAR_POP(mainwin) \
89 { \
90         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
91                           mainwin->summaryview_cid); \
92 }
93
94 #define SUMMARY_COL_MARK_WIDTH          10
95 #define SUMMARY_COL_UNREAD_WIDTH        13
96 #define SUMMARY_COL_MIME_WIDTH          10
97
98 static GdkFont *smallfont;
99
100 static GdkPixmap *folderxpm;
101 static GdkBitmap *folderxpmmask;
102
103 static GdkPixmap *markxpm;
104 static GdkBitmap *markxpmmask;
105 static GdkPixmap *deletedxpm;
106 static GdkBitmap *deletedxpmmask;
107
108 static GdkPixmap *newxpm;
109 static GdkBitmap *newxpmmask;
110 static GdkPixmap *unreadxpm;
111 static GdkBitmap *unreadxpmmask;
112 static GdkPixmap *repliedxpm;
113 static GdkBitmap *repliedxpmmask;
114 static GdkPixmap *forwardedxpm;
115 static GdkBitmap *forwardedxpmmask;
116
117 static GdkPixmap *clipxpm;
118 static GdkBitmap *clipxpmmask;
119
120 static void summary_free_msginfo_func   (GtkCTree               *ctree,
121                                          GtkCTreeNode           *node,
122                                          gpointer                data);
123 static void summary_set_marks_func      (GtkCTree               *ctree,
124                                          GtkCTreeNode           *node,
125                                          gpointer                data);
126 static void summary_write_cache_func    (GtkCTree               *ctree,
127                                          GtkCTreeNode           *node,
128                                          gpointer                data);
129
130 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
131
132 static GtkCTreeNode *summary_find_next_unread_msg
133                                         (SummaryView            *summaryview,
134                                          GtkCTreeNode           *current_node);
135 static GtkCTreeNode *summary_find_msg_by_msgnum
136                                         (SummaryView            *summaryview,
137                                          guint                   msgnum);
138 #if 0
139 static GtkCTreeNode *summary_find_prev_unread_msg
140                                         (SummaryView            *summaryview,
141                                          GtkCTreeNode           *current_node);
142 #endif
143
144 static void summary_update_status       (SummaryView            *summaryview);
145
146 /* display functions */
147 static void summary_status_show         (SummaryView            *summaryview);
148 static void summary_set_ctree_from_list (SummaryView            *summaryview,
149                                          GSList                 *mlist);
150 static void summary_set_header          (gchar                  *text[],
151                                          MsgInfo                *msginfo);
152 static void summary_display_msg         (SummaryView            *summaryview,
153                                          GtkCTreeNode           *row,
154                                          gboolean                new_window);
155 static void summary_toggle_view         (SummaryView            *summaryview);
156 static void summary_set_row_marks       (SummaryView            *summaryview,
157                                          GtkCTreeNode           *row);
158
159 /* message handling */
160 static void summary_mark_row            (SummaryView            *summaryview,
161                                          GtkCTreeNode           *row);
162 static void summary_mark_row_as_read    (SummaryView            *summaryview,
163                                          GtkCTreeNode           *row);
164 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
165                                          GtkCTreeNode           *row);
166 static void summary_delete_row          (SummaryView            *summaryview,
167                                          GtkCTreeNode           *row);
168 static void summary_unmark_row          (SummaryView            *summaryview,
169                                          GtkCTreeNode           *row);
170 static void summary_move_row_to         (SummaryView            *summaryview,
171                                          GtkCTreeNode           *row,
172                                          FolderItem             *to_folder);
173 static void summary_copy_row_to         (SummaryView            *summaryview,
174                                          GtkCTreeNode           *row,
175                                          FolderItem             *to_folder);
176
177 static void summary_delete_duplicated_func
178                                         (GtkCTree               *ctree,
179                                          GtkCTreeNode           *node,
180                                          SummaryView            *summaryview);
181
182 static void summary_execute_move        (SummaryView            *summaryview);
183 static void summary_execute_move_func   (GtkCTree               *ctree,
184                                          GtkCTreeNode           *node,
185                                          gpointer                data);
186 static void summary_execute_copy        (SummaryView            *summaryview);
187 static void summary_execute_copy_func   (GtkCTree               *ctree,
188                                          GtkCTreeNode           *node,
189                                          gpointer                data);
190 static void summary_execute_delete      (SummaryView            *summaryview);
191 static void summary_execute_delete_func (GtkCTree               *ctree,
192                                          GtkCTreeNode           *node,
193                                          gpointer                data);
194
195 /* thread functions */
196 static void summary_thread_func                 (GtkCTree       *ctree,
197                                                  GtkCTreeNode   *node,
198                                                  gpointer        data);
199 static void summary_unthread_func               (GtkCTree       *ctree,
200                                                  GtkCTreeNode   *node,
201                                                  gpointer        data);
202 static void summary_unthread_for_exec           (SummaryView    *summaryview);
203 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
204                                                  GtkCTreeNode   *node,
205                                                  gpointer        data);
206
207 static void summary_filter_func         (GtkCTree               *ctree,
208                                          GtkCTreeNode           *node,
209                                          gpointer                data);
210
211 /* callback functions */
212 static void summary_toggle_pressed      (GtkWidget              *eventbox,
213                                          GdkEventButton         *event,
214                                          SummaryView            *summaryview);
215 static void summary_button_pressed      (GtkWidget              *ctree,
216                                          GdkEventButton         *event,
217                                          SummaryView            *summaryview);
218 static void summary_button_released     (GtkWidget              *ctree,
219                                          GdkEventButton         *event,
220                                          SummaryView            *summaryview);
221 static void summary_key_pressed         (GtkWidget              *ctree,
222                                          GdkEventKey            *event,
223                                          SummaryView            *summaryview);
224 static void summary_open_row            (GtkSCTree              *sctree,
225                                          SummaryView            *summaryview);
226 static void summary_selected            (GtkCTree               *ctree,
227                                          GtkCTreeNode           *row,
228                                          gint                    column,
229                                          SummaryView            *summaryview);
230 static void summary_col_resized         (GtkCList               *clist,
231                                          gint                    column,
232                                          gint                    width,
233                                          SummaryView            *summaryview);
234 static void summary_reply_cb            (SummaryView            *summaryview,
235                                          guint                   action,
236                                          GtkWidget              *widget);
237 static void summary_show_all_header_cb  (SummaryView            *summaryview,
238                                          guint                   action,
239                                          GtkWidget              *widget);
240
241 static void summary_num_clicked         (GtkWidget              *button,
242                                          SummaryView            *summaryview);
243 static void summary_size_clicked        (GtkWidget              *button,
244                                          SummaryView            *summaryview);
245 static void summary_date_clicked        (GtkWidget              *button,
246                                          SummaryView            *summaryview);
247 static void summary_from_clicked        (GtkWidget              *button,
248                                          SummaryView            *summaryview);
249 static void summary_subject_clicked     (GtkWidget              *button,
250                                          SummaryView            *summaryview);
251
252 static void summary_start_drag          (GtkWidget        *widget, 
253                                          int button,
254                                          GdkEvent *event,
255                                          SummaryView      *summaryview);
256 static void summary_drag_data_get       (GtkWidget        *widget,
257                                          GdkDragContext   *drag_context,
258                                          GtkSelectionData *selection_data,
259                                          guint             info,
260                                          guint             time,
261                                          SummaryView      *summaryview);
262
263 /* custom compare functions for sorting */
264
265 static gint summary_cmp_by_num          (GtkCList               *clist,
266                                          gconstpointer           ptr1,
267                                          gconstpointer           ptr2);
268 static gint summary_cmp_by_size         (GtkCList               *clist,
269                                          gconstpointer           ptr1,
270                                          gconstpointer           ptr2);
271 static gint summary_cmp_by_date         (GtkCList               *clist,
272                                          gconstpointer           ptr1,
273                                          gconstpointer           ptr2);
274 static gint summary_cmp_by_from         (GtkCList               *clist,
275                                          gconstpointer           ptr1,
276                                          gconstpointer           ptr2);
277 static gint summary_cmp_by_subject      (GtkCList               *clist,
278                                          gconstpointer           ptr1,
279                                          gconstpointer           ptr2);
280
281 GtkTargetEntry summary_drag_types[1] =
282 {
283         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
284 };
285
286 static GtkItemFactoryEntry summary_popup_entries[] =
287 {
288         {N_("/M_ove..."),               NULL, summary_move_to,  0, NULL},
289         {N_("/_Copy..."),               NULL, summary_copy_to,  0, NULL},
290         {N_("/_Delete"),                NULL, summary_delete,   0, NULL},
291         {N_("/E_xecute"),               NULL, summary_execute,  0, NULL},
292         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
293         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
294         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
295         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
296         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
297         {N_("/_Mark/Make it as _being read"),
298                                         NULL, summary_mark_as_read, 0, NULL},
299         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
300         {N_("/_Reply"),                 NULL, summary_reply_cb, COMPOSE_REPLY, NULL},
301         {N_("/Reply to a_ll"),          NULL, summary_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
302         {N_("/_Forward"),               NULL, summary_reply_cb, COMPOSE_FORWARD, NULL},
303         {N_("/Forward as an a_ttachment"),
304                                         NULL, summary_reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
305         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
306         {N_("/Open in new _window"),    NULL, summary_open_msg, 0, NULL},
307         {N_("/View so_urce"),           NULL, summary_view_source, 0, NULL},
308         {N_("/Show all _header"),       NULL, summary_show_all_header_cb, 0, NULL},
309         {N_("/Re_edit"),                NULL, summary_reedit,   0, NULL},
310         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
311         {N_("/_Save as..."),            NULL, summary_save_as,  0, NULL},
312         {N_("/_Print..."),              NULL, summary_print,    0, NULL},
313         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
314         {N_("/Select _all"),            NULL, summary_select_all, 0, NULL}
315 };
316
317 SummaryView *summary_create(void)
318 {
319         SummaryView *summaryview;
320         gchar *titles[N_SUMMARY_COLS] = {_("M"), _("U")};
321         GtkWidget *vbox;
322         GtkWidget *scrolledwin;
323         GtkWidget *ctree;
324         GtkWidget *hbox;
325         GtkWidget *statlabel_folder;
326         GtkWidget *statlabel_select;
327         GtkWidget *statlabel_msgs;
328         GtkWidget *toggle_eventbox;
329         GtkWidget *toggle_arrow;
330         GtkWidget *popupmenu;
331         GtkItemFactory *popupfactory;
332         gint n_entries;
333         gint i;
334
335         debug_print(_("Creating summary view...\n"));
336         summaryview = g_new0(SummaryView, 1);
337
338         vbox = gtk_vbox_new(FALSE, 2);
339
340         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
341         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
342                                        GTK_POLICY_AUTOMATIC,
343                                        GTK_POLICY_ALWAYS);
344         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
345         gtk_widget_set_usize(vbox,
346                              prefs_common.summaryview_width,
347                              prefs_common.summaryview_height);
348
349         if (prefs_common.trans_hdr) {
350                 titles[S_COL_NUMBER]  = _("No.");
351                 titles[S_COL_DATE]    = _("Date");
352                 titles[S_COL_FROM]    = _("From");
353                 titles[S_COL_SUBJECT] = _("Subject");
354         } else {
355                 titles[S_COL_NUMBER]  = "No.";
356                 titles[S_COL_DATE]    = "Date";
357                 titles[S_COL_FROM]    = "From";
358                 titles[S_COL_SUBJECT] = "Subject";
359         }
360         titles[S_COL_SIZE] = _("Size");
361
362         ctree = gtk_sctree_new_with_titles(N_SUMMARY_COLS, S_COL_SUBJECT, titles);
363         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
364                                             GTK_CLIST(ctree)->hadjustment);
365         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
366                                             GTK_CLIST(ctree)->vadjustment);
367         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
368         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_EXTENDED);
369         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_MARK,
370                                            GTK_JUSTIFY_CENTER);
371         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_UNREAD,
372                                            GTK_JUSTIFY_CENTER);
373         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_MIME,
374                                            GTK_JUSTIFY_CENTER);
375         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_NUMBER,
376                                            GTK_JUSTIFY_RIGHT);
377         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_SIZE,
378                                            GTK_JUSTIFY_RIGHT);
379         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_MARK,
380                                    SUMMARY_COL_MARK_WIDTH);
381         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_UNREAD,
382                                    SUMMARY_COL_UNREAD_WIDTH);
383         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_MIME,
384                                    SUMMARY_COL_MIME_WIDTH);
385         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_NUMBER,
386                                    prefs_common.summary_col_number);
387         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SIZE,
388                                    prefs_common.summary_col_size);
389         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_DATE,
390                                    prefs_common.summary_col_date);
391         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_FROM,
392                                    prefs_common.summary_col_from);
393         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
394                                    prefs_common.summary_col_subject);
395         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
396         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
397                                      GTK_CTREE_EXPANDER_SQUARE);
398 #if 0
399         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
400         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
401                                      GTK_CTREE_EXPANDER_TRIANGLE);
402 #endif
403         gtk_ctree_set_indent(GTK_CTREE(ctree), 18);
404         gtk_object_set_user_data(GTK_OBJECT(ctree), summaryview);
405
406         /* don't let title buttons take key focus */
407         for (i = 0; i < N_SUMMARY_COLS; i++)
408                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
409                                        GTK_CAN_FOCUS);
410
411         /* connect signal to the buttons for sorting */
412         gtk_signal_connect
413                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_NUMBER].button),
414                  "clicked",
415                  GTK_SIGNAL_FUNC(summary_num_clicked),
416                  summaryview);
417         gtk_signal_connect
418                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SIZE].button),
419                  "clicked",
420                  GTK_SIGNAL_FUNC(summary_size_clicked),
421                  summaryview);
422         gtk_signal_connect
423                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_DATE].button),
424                  "clicked",
425                  GTK_SIGNAL_FUNC(summary_date_clicked),
426                  summaryview);
427         gtk_signal_connect
428                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_FROM].button),
429                  "clicked",
430                  GTK_SIGNAL_FUNC(summary_from_clicked),
431                  summaryview);
432         gtk_signal_connect
433                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SUBJECT].button),
434                  "clicked",
435                  GTK_SIGNAL_FUNC(summary_subject_clicked),
436                  summaryview);
437
438         /* create status label */
439         hbox = gtk_hbox_new(FALSE, 0);
440         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
441
442         statlabel_folder = gtk_label_new("");
443         gtk_box_pack_start(GTK_BOX(hbox), statlabel_folder, FALSE, FALSE, 2);
444         statlabel_select = gtk_label_new("");
445         gtk_box_pack_start(GTK_BOX(hbox), statlabel_select, FALSE, FALSE, 16);
446
447         /* toggle view button */
448         toggle_eventbox = gtk_event_box_new();
449         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
450         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
451         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
452
453         statlabel_msgs = gtk_label_new("");
454         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
455
456         /* create popup menu */
457         n_entries = sizeof(summary_popup_entries) /
458                 sizeof(summary_popup_entries[0]);
459         popupmenu = menu_create_items(summary_popup_entries, n_entries,
460                                       "<SummaryView>", &popupfactory,
461                                       summaryview);
462
463         /* connect signals */
464         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
465                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
466         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
467                            GTK_SIGNAL_FUNC(summary_button_pressed),
468                            summaryview);
469         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
470                            GTK_SIGNAL_FUNC(summary_button_released),
471                            summaryview);
472         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
473                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
474         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
475                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
476         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
477                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
478         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
479                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
480                            summaryview);
481
482         summaryview->vbox = vbox;
483         summaryview->scrolledwin = scrolledwin;
484         summaryview->ctree = ctree;
485         summaryview->hbox = hbox;
486         summaryview->statlabel_folder = statlabel_folder;
487         summaryview->statlabel_select = statlabel_select;
488         summaryview->statlabel_msgs = statlabel_msgs;
489         summaryview->toggle_eventbox = toggle_eventbox;
490         summaryview->toggle_arrow = toggle_arrow;
491         summaryview->popupmenu = popupmenu;
492         summaryview->popupfactory = popupfactory;
493         summaryview->msg_is_toggled_on = TRUE;
494         summaryview->sort_mode = SORT_BY_NONE;
495         summaryview->sort_type = GTK_SORT_ASCENDING;
496
497         summary_change_display_item(summaryview);
498
499         gtk_widget_show_all(vbox);
500
501         return summaryview;
502 }
503
504 void summary_init(SummaryView *summaryview)
505 {
506         GtkStyle *style;
507         GtkWidget *pixmap;
508
509         PIXMAP_CREATE(summaryview->ctree, markxpm, markxpmmask, mark_xpm);
510         PIXMAP_CREATE(summaryview->ctree, deletedxpm, deletedxpmmask,
511                       deleted_xpm);
512         PIXMAP_CREATE(summaryview->ctree, newxpm, newxpmmask, new_xpm);
513         PIXMAP_CREATE(summaryview->ctree, unreadxpm, unreadxpmmask, unread_xpm);
514         PIXMAP_CREATE(summaryview->ctree, repliedxpm, repliedxpmmask,
515                       replied_xpm);
516         PIXMAP_CREATE(summaryview->ctree, forwardedxpm, forwardedxpmmask,
517                       forwarded_xpm);
518         PIXMAP_CREATE(summaryview->ctree, clipxpm, clipxpmmask, clip_xpm);
519         PIXMAP_CREATE(summaryview->hbox, folderxpm, folderxpmmask,
520                       DIRECTORY_OPEN_XPM);
521
522         pixmap = gtk_pixmap_new(clipxpm, clipxpmmask);
523         gtk_clist_set_column_widget(GTK_CLIST(summaryview->ctree),
524                                     S_COL_MIME, pixmap);
525         gtk_widget_show(pixmap);
526
527         if (!smallfont)
528                 smallfont = gdk_fontset_load(SMALL_FONT);
529
530         style = gtk_style_copy(gtk_widget_get_style
531                                 (summaryview->statlabel_folder));
532         if (smallfont) style->font = smallfont;
533         gtk_widget_set_style(summaryview->statlabel_folder, style);
534         gtk_widget_set_style(summaryview->statlabel_select, style);
535         gtk_widget_set_style(summaryview->statlabel_msgs, style);
536
537         pixmap = gtk_pixmap_new(folderxpm, folderxpmmask);
538         gtk_box_pack_start(GTK_BOX(summaryview->hbox), pixmap, FALSE, FALSE, 4);
539         gtk_box_reorder_child(GTK_BOX(summaryview->hbox), pixmap, 0);
540         gtk_widget_show(pixmap);
541
542         summary_clear_list(summaryview);
543         summary_set_menu_sensitive(summaryview);
544 }
545
546 gboolean summary_show(SummaryView *summaryview, FolderItem *item,
547                       gboolean update_cache)
548 {
549         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
550         GtkCTreeNode *node;
551         GSList *mlist = NULL;
552         gchar *buf;
553         gboolean is_refresh;
554         guint prev_msgnum = 0;
555
556         STATUSBAR_POP(summaryview->mainwin);
557
558         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
559         if (is_refresh)
560                 prev_msgnum = summary_get_current_msgnum(summaryview);
561
562 #if 0
563         /* process the marks if any */
564         if (summaryview->deleted > 0 || summaryview->moved > 0) {
565                 AlertValue val;
566
567                 val = alertpanel(_("Process mark"),
568                                  _("Some marks are left. Process it?"),
569                                  _("Yes"), _("No"), _("Cancel"));
570                 if (G_ALERTDEFAULT == val)
571                         summary_execute(summaryview);
572                 else if (G_ALERTALTERNATE == val)
573                         summary_write_cache(summaryview);
574                 else
575                         return FALSE;
576         } else
577 #endif
578                 summary_write_cache(summaryview);
579
580         gtk_clist_freeze(GTK_CLIST(ctree));
581
582         summary_clear_list(summaryview);
583         summary_set_menu_sensitive(summaryview);
584         messageview_clear(summaryview->messageview);
585
586         buf = NULL;
587         if (!item || !item->path || !item->parent ||
588             (item->folder->type == F_MH &&
589              ((buf = folder_item_get_path(item)) == NULL ||
590               change_dir(buf) < 0))) {
591                 g_free(buf);
592                 debug_print(_("empty folder\n\n"));
593                 summary_clear_all(summaryview);
594                 summaryview->folder_item = item;
595                 gtk_clist_thaw(GTK_CLIST(ctree));
596                 return TRUE;
597         }
598         g_free(buf);
599
600         summaryview->folder_item = item;
601
602         gtk_signal_disconnect_by_data(GTK_OBJECT(ctree), summaryview);
603
604         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
605         debug_print("%s\n", buf);
606         STATUSBAR_PUSH(summaryview->mainwin, buf);
607         g_free(buf);
608
609         main_window_cursor_wait(summaryview->mainwin);
610
611         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
612
613         STATUSBAR_POP(summaryview->mainwin);
614
615         /* set ctree and hash table from the msginfo list
616            creating thread, and count the number of messages */
617         summary_set_ctree_from_list(summaryview, mlist);
618
619         g_slist_free(mlist);
620
621         summary_write_cache(summaryview);
622
623         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
624                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
625         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
626                            GTK_SIGNAL_FUNC(summary_button_pressed),
627                            summaryview);
628         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
629                            GTK_SIGNAL_FUNC(summary_button_released),
630                            summaryview);
631         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
632                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
633         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
634                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
635         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
636                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
637
638         /*connect drag and drop signal*/
639         gtk_signal_connect (GTK_OBJECT (ctree),"start_drag",
640                             GTK_SIGNAL_FUNC (summary_start_drag),
641                             summaryview);
642         gtk_signal_connect (GTK_OBJECT (ctree),"drag_data_get",
643                             GTK_SIGNAL_FUNC (summary_drag_data_get),
644                             summaryview);
645         
646         gtk_clist_thaw(GTK_CLIST(ctree));
647
648         if (is_refresh) {
649                 summary_select_by_msgnum(summaryview, prev_msgnum);
650         } else {
651                 /* select first unread message */
652                 node = summary_find_next_unread_msg(summaryview, NULL);
653                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
654                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
655                 if (node) {
656                         GTK_EVENTS_FLUSH();
657                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
658                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
659                         if (prefs_common.open_unread_on_enter)
660                                 summaryview->display_msg = TRUE;
661                         gtk_sctree_select(GTK_SCTREE(ctree), node);
662                 }
663         }
664
665         summary_status_show(summaryview);
666
667         summary_set_menu_sensitive(summaryview);
668
669         main_window_set_toolbar_sensitive
670                 (summaryview->mainwin, summaryview->selected ? TRUE : FALSE);
671
672         debug_print("\n");
673         STATUSBAR_PUSH(summaryview->mainwin, _("done."));
674
675         main_window_cursor_normal(summaryview->mainwin);
676
677         return TRUE;
678 }
679
680 void summary_clear_list(SummaryView *summaryview)
681 {
682         GtkCList *clist = GTK_CLIST(summaryview->ctree);
683         gint optimal_width;
684
685         gtk_clist_freeze(clist);
686
687         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
688                                 NULL, summary_free_msginfo_func, NULL);
689
690         summaryview->folder_item = NULL;
691
692         summaryview->display_msg = FALSE;
693
694         summaryview->selected  = NULL;
695         summaryview->displayed = NULL;
696         summaryview->newmsgs   = summaryview->unread     = 0;
697         summaryview->messages  = summaryview->total_size = 0;
698         summaryview->deleted   = summaryview->moved      = 0;
699         summaryview->copied    = 0;
700         if (summaryview->msgid_table) {
701                 g_hash_table_destroy(summaryview->msgid_table);
702                 summaryview->msgid_table = NULL;
703         }
704         summaryview->mlist = NULL;
705         if (summaryview->folder_table) {
706                 g_hash_table_destroy(summaryview->folder_table);
707                 summaryview->folder_table = NULL;
708         }
709         summaryview->sort_mode = SORT_BY_NONE;
710         summaryview->sort_type = GTK_SORT_ASCENDING;
711
712         gtk_clist_clear(clist);
713         optimal_width = gtk_clist_optimal_column_width(clist, S_COL_SUBJECT);
714         gtk_clist_set_column_width(clist, S_COL_SUBJECT, optimal_width);
715
716         gtk_clist_thaw(clist);
717 }
718
719 void summary_clear_all(SummaryView *summaryview)
720 {
721         summary_clear_list(summaryview);
722         summary_set_menu_sensitive(summaryview);
723         main_window_set_toolbar_sensitive(summaryview->mainwin, FALSE);
724         summary_status_show(summaryview);
725 }
726
727 static void summary_set_menu_sensitive(SummaryView *summaryview)
728 {
729         GtkItemFactory *ifactory = summaryview->popupfactory;
730         GtkCList *clist = GTK_CLIST(summaryview->ctree);
731         gboolean sens;
732         SummarySelection selection;
733
734         if (!clist->row_list)
735                 selection = SUMMARY_NONE;
736         else if (!clist->selection)
737                 selection = SUMMARY_SELECTED_NONE;
738         else if (!clist->selection->next)
739                 selection = SUMMARY_SELECTED_SINGLE;
740         else
741                 selection = SUMMARY_SELECTED_MULTIPLE;
742
743         main_window_set_menu_sensitive(summaryview->mainwin, selection);
744
745         if (selection == SUMMARY_NONE) {
746                 GtkWidget *submenu;
747
748                 submenu = gtk_item_factory_get_widget
749                         (summaryview->popupfactory, "/Mark");
750                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
751                 menu_set_insensitive_all
752                         (GTK_MENU_SHELL(summaryview->popupmenu));
753                 return;
754         }
755
756         if (summaryview->folder_item->folder->type != F_NEWS) {
757                 if (summaryview->folder_item->stype != F_TRASH)
758                         menu_set_sensitive(ifactory, "/Delete", TRUE);
759                 menu_set_sensitive(ifactory, "/Move...", TRUE);
760                 menu_set_sensitive(ifactory, "/Copy...", TRUE);
761         }
762         menu_set_sensitive(ifactory, "/Execute", TRUE);
763
764         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
765         menu_set_sensitive(ifactory, "/Reply",                    sens);
766         menu_set_sensitive(ifactory, "/Reply to all",             sens);
767         menu_set_sensitive(ifactory, "/Forward",                  sens);
768         menu_set_sensitive(ifactory, "/Forward as an attachment", sens);
769
770         menu_set_sensitive(ifactory, "/Open in new window", sens);
771         menu_set_sensitive(ifactory, "/View source", sens);
772         menu_set_sensitive(ifactory, "/Show all header", sens);
773         if (summaryview->folder_item->stype == F_DRAFT)
774                 menu_set_sensitive(ifactory, "/Reedit", sens);
775
776         menu_set_sensitive(ifactory, "/Save as...", sens);
777         menu_set_sensitive(ifactory, "/Print...",   TRUE);
778
779         menu_set_sensitive(ifactory, "/Mark", TRUE);
780
781         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
782         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
783
784         menu_set_sensitive(ifactory, "/Mark/Mark as unread",        TRUE);
785         menu_set_sensitive(ifactory, "/Mark/Make it as being read", TRUE);
786
787         menu_set_sensitive(ifactory, "/Select all", TRUE);
788 }
789
790 void summary_select_next_unread(SummaryView *summaryview)
791 {
792         GtkCTreeNode *node;
793         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
794
795         node = summary_find_next_unread_msg(summaryview,
796                                             summaryview->selected);
797
798         if (node) {
799                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
800                 gtk_sctree_select(GTK_SCTREE(ctree), node);
801                 if (summaryview->displayed == node)
802                         summaryview->displayed = NULL;
803                 summary_display_msg(summaryview, node, FALSE);
804         } else {
805                 AlertValue val;
806
807                 val = alertpanel(_("No unread message"),
808                                  _("No unread message found. Go to next folder?"),
809                                  _("Yes"), _("No"), NULL);
810                 if (val == G_ALERTDEFAULT) {
811                         if (gtk_signal_n_emissions_by_name
812                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
813                                         gtk_signal_emit_stop_by_name
814                                                 (GTK_OBJECT(ctree),
815                                                  "key_press_event");
816                         folderview_select_next_unread(summaryview->folderview);
817                 }
818         }
819 }
820
821 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
822 {
823         GtkCTreeNode *node;
824         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
825
826         node = summary_find_msg_by_msgnum(summaryview, msgnum);
827
828         if (node) {
829                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
830                 gtk_sctree_select(GTK_SCTREE(ctree), node);
831                 if (summaryview->displayed == node)
832                         summaryview->displayed = NULL;
833                 summary_display_msg(summaryview, node, FALSE);
834         }
835 }
836
837 guint summary_get_current_msgnum(SummaryView *summaryview)
838 {
839         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
840         MsgInfo *msginfo;
841
842         if (!summaryview->selected)
843                 return 0;
844         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
845         return msginfo->msgnum;
846 }
847
848 static GtkCTreeNode *summary_find_next_unread_msg(SummaryView *summaryview,
849                                                   GtkCTreeNode *current_node)
850 {
851         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
852         GtkCTreeNode *node;
853         MsgInfo *msginfo;
854
855         if (current_node)
856                 node = current_node;
857                 //node = GTK_CTREE_NODE_NEXT(current_node);
858         else
859                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
860
861         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
862                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
863                 if (MSG_IS_UNREAD(msginfo->flags)) break;
864         }
865
866         return node;
867 }
868
869 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
870                                                 guint msgnum)
871 {
872         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
873         GtkCTreeNode *node;
874         MsgInfo *msginfo;
875
876         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
877
878         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
879                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
880                 if (msginfo->msgnum == msgnum) break;
881         }
882
883         return node;
884 }
885
886 #if 0
887 static GtkCTreeNode *summary_find_prev_unread_msg(SummaryView *summaryview,
888                                                   GtkCTreeNode *current_node)
889 {
890         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
891         GtkCTreeNode *node;
892         MsgInfo *msginfo;
893
894         if (current_node)
895                 node = current_node;
896                 //node = GTK_CTREE_NODE_PREV(current_node);
897         else
898                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
899
900         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
901                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
902                 if (MSG_IS_UNREAD(msginfo->flags)) break;
903         }
904
905         return node;
906 }
907 #endif
908
909 static guint attract_hash_func(gconstpointer key)
910 {
911         gchar *str;
912         gchar *p;
913         guint h;
914
915         Xstrdup_a(str, (const gchar *)key, return 0);
916         trim_subject(str);
917
918         p = str;
919         h = *p;
920
921         if (h) {
922                 for (p += 1; *p != '\0'; p++)
923                         h = (h << 5) - h + *p;
924         }
925
926         return h;
927 }
928
929 static gint attract_compare_func(gconstpointer a, gconstpointer b)
930 {
931         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
932 }
933
934 void summary_attract_by_subject(SummaryView *summaryview)
935 {
936         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
937         GtkCList *clist = GTK_CLIST(ctree);
938         GtkCTreeNode *src_node;
939         GtkCTreeNode *dst_node, *sibling;
940         GtkCTreeNode *tmp;
941         MsgInfo *src_msginfo, *dst_msginfo;
942         GHashTable *subject_table;
943
944         debug_print(_("Attracting messages by subject..."));
945         STATUSBAR_PUSH(summaryview->mainwin,
946                        _("Attracting messages by subject..."));
947
948         main_window_cursor_wait(summaryview->mainwin);
949         gtk_clist_freeze(clist);
950
951         subject_table = g_hash_table_new(attract_hash_func,
952                                          attract_compare_func);
953
954         for (src_node = GTK_CTREE_NODE(clist->row_list);
955              src_node != NULL;
956              src_node = tmp) {
957                 tmp = GTK_CTREE_ROW(src_node)->sibling;
958                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
959                 if (!src_msginfo) continue;
960                 if (!src_msginfo->subject) continue;
961
962                 /* find attracting node */
963                 dst_node = g_hash_table_lookup(subject_table,
964                                                src_msginfo->subject);
965
966                 if (dst_node) {
967                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
968
969                         /* if the time difference is more than 20 days,
970                            don't attract */
971                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
972                             > 60 * 60 * 24 * 20)
973                                 continue;
974
975                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
976                         if (src_node != sibling)
977                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
978                 }
979
980                 g_hash_table_insert(subject_table,
981                                     src_msginfo->subject, src_node);
982         }
983
984         g_hash_table_destroy(subject_table);
985
986         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
987
988         gtk_clist_thaw(clist);
989
990         debug_print(_("done.\n"));
991         STATUSBAR_POP(summaryview->mainwin);
992
993         main_window_cursor_normal(summaryview->mainwin);
994 }
995
996 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
997                                       gpointer data)
998 {
999         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1000
1001         if (msginfo)
1002                 procmsg_msginfo_free(msginfo);
1003 }
1004
1005 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1006                                    gpointer data)
1007 {
1008         SummaryView *summaryview = data;
1009         MsgInfo *msginfo;
1010
1011         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1012
1013         if (MSG_IS_NEW(msginfo->flags))
1014                 summaryview->newmsgs++;
1015         if (MSG_IS_UNREAD(msginfo->flags))
1016                 summaryview->unread++;
1017         if (MSG_IS_DELETED(msginfo->flags))
1018                 summaryview->deleted++;
1019
1020         summaryview->messages++;
1021         summaryview->total_size += msginfo->size;
1022
1023         summary_set_row_marks(summaryview, node);
1024 }
1025
1026 static void summary_update_status(SummaryView *summaryview)
1027 {
1028         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1029         GtkCTreeNode *node;
1030         MsgInfo *msginfo;
1031
1032         summaryview->newmsgs = summaryview->unread =
1033         summaryview->messages = summaryview->total_size =
1034         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1035
1036         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1037              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1038                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1039
1040                 if (MSG_IS_NEW(msginfo->flags))
1041                         summaryview->newmsgs++;
1042                 if (MSG_IS_UNREAD(msginfo->flags))
1043                         summaryview->unread++;
1044                 if (MSG_IS_DELETED(msginfo->flags))
1045                         summaryview->deleted++;
1046                 if (MSG_IS_MOVE(msginfo->flags))
1047                         summaryview->moved++;
1048                 if (MSG_IS_COPY(msginfo->flags))
1049                         summaryview->copied++;
1050                 summaryview->messages++;
1051                 summaryview->total_size += msginfo->size;
1052         }
1053 }
1054
1055 static void summary_status_show(SummaryView *summaryview)
1056 {
1057         gchar *str;
1058         gchar *del, *mv, *cp;
1059         gchar *sel;
1060         gchar *spc;
1061         GList *rowlist, *cur;
1062         guint n_selected = 0;
1063         off_t sel_size = 0;
1064         MsgInfo *msginfo;
1065
1066         if (!summaryview->folder_item) {
1067                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1068                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1069                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1070                 return;
1071         }
1072
1073         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1074         for (cur = rowlist; cur != NULL; cur = cur->next) {
1075                 msginfo = gtk_ctree_node_get_row_data
1076                         (GTK_CTREE(summaryview->ctree),
1077                          GTK_CTREE_NODE(cur->data));
1078                 sel_size += msginfo->size;
1079                 n_selected++;
1080         }
1081
1082         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1083                       summaryview->folder_item &&
1084                       summaryview->folder_item->folder->type == F_NEWS
1085                       ? g_basename(summaryview->folder_item->path)
1086                       : summaryview->folder_item->path);
1087
1088         if (summaryview->deleted)
1089                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1090         else
1091                 del = g_strdup("");
1092         if (summaryview->moved)
1093                 mv = g_strdup_printf(_("%s%d moved"),
1094                                      summaryview->deleted ? _(", ") : "",
1095                                      summaryview->moved);
1096         else
1097                 mv = g_strdup("");
1098         if (summaryview->copied)
1099                 cp = g_strdup_printf(_("%s%d copied"),
1100                                      summaryview->deleted ||
1101                                      summaryview->moved ? _(", ") : "",
1102                                      summaryview->copied);
1103         else
1104                 cp = g_strdup("");
1105
1106         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1107                 spc = "    ";
1108         else
1109                 spc = "";
1110
1111         if (n_selected)
1112                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1113         else
1114                 sel = g_strdup("");
1115         str = g_strconcat(n_selected ? itos(n_selected) : "",
1116                           n_selected ? _(" item(s) selected") : "",
1117                           sel, spc, del, mv, cp, NULL);
1118         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1119         g_free(str);
1120         g_free(sel);
1121         g_free(del);
1122         g_free(mv);
1123         g_free(cp);
1124
1125         if (summaryview->folder_item &&
1126             summaryview->folder_item->folder->type == F_MH) {
1127                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1128                                       summaryview->newmsgs,
1129                                       summaryview->unread,
1130                                       summaryview->messages,
1131                                       to_human_readable(summaryview->total_size));
1132         } else {
1133                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1134                                       summaryview->newmsgs,
1135                                       summaryview->unread,
1136                                       summaryview->messages);
1137         }
1138         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1139         g_free(str);
1140
1141         folderview_update_msg_num(summaryview->folderview,
1142                                   summaryview->folderview->opened,
1143                                   summaryview->newmsgs,
1144                                   summaryview->unread,
1145                                   summaryview->messages);
1146 }
1147
1148 void summary_sort(SummaryView *summaryview, SummarySortType type)
1149 {
1150         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1151         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1152         GtkCListCompareFunc cmp_func;
1153
1154         switch (type) {
1155         case SORT_BY_NUMBER:
1156                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1157                 break;
1158         case SORT_BY_SIZE:
1159                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1160                 break;
1161         case SORT_BY_DATE:
1162                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1163                 break;
1164         case SORT_BY_FROM:
1165                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1166                 break;
1167         case SORT_BY_SUBJECT:
1168                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1169                 break;
1170         default:
1171                 return;
1172         }
1173
1174         debug_print(_("Sorting summary..."));
1175         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1176
1177         main_window_cursor_wait(summaryview->mainwin);
1178
1179         gtk_clist_set_compare_func(clist, cmp_func);
1180
1181         /* toggle sort type if the same column is selected */
1182         if (summaryview->sort_mode == type)
1183                 summaryview->sort_type =
1184                         summaryview->sort_type == GTK_SORT_ASCENDING
1185                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1186         else
1187                 summaryview->sort_type = GTK_SORT_ASCENDING;
1188         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1189         summaryview->sort_mode = type;
1190
1191         gtk_ctree_sort_node(ctree, NULL);
1192
1193         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1194         //gtkut_ctree_set_focus_row(ctree, summaryview->selected);
1195
1196         debug_print(_("done.\n"));
1197         STATUSBAR_POP(summaryview->mainwin);
1198
1199         main_window_cursor_normal(summaryview->mainwin);
1200 }
1201
1202 static void summary_set_ctree_from_list(SummaryView *summaryview,
1203                                         GSList *mlist)
1204 {
1205         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1206         MsgInfo *msginfo;
1207         GtkCTreeNode *node, *parent;
1208         gchar *text[N_SUMMARY_COLS];
1209         GHashTable *msgid_table;
1210
1211         if (!mlist) return;
1212
1213         debug_print(_("\tSetting summary from message data..."));
1214         STATUSBAR_PUSH(summaryview->mainwin,
1215                        _("Setting summary from message data..."));
1216
1217         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1218         summaryview->msgid_table = msgid_table;
1219
1220         if (prefs_common.enable_thread) {
1221                 for (; mlist != NULL; mlist = mlist->next) {
1222                         msginfo = (MsgInfo *)mlist->data;
1223                         parent = NULL;
1224
1225                         summary_set_header(text, msginfo);
1226
1227                         /* search parent node for threading */
1228                         if (msginfo->inreplyto && *msginfo->inreplyto)
1229                                 parent = g_hash_table_lookup
1230                                         (msgid_table, msginfo->inreplyto);
1231
1232                         node = gtk_ctree_insert_node
1233                                 (ctree, parent, NULL, text, 2,
1234                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1235                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1236                         summary_set_marks_func(ctree, node, summaryview);
1237
1238                         /* preserve previous node if the message is
1239                            duplicated */
1240                         if (msginfo->msgid && *msginfo->msgid &&
1241                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1242                             == NULL)
1243                                 g_hash_table_insert(msgid_table,
1244                                                     msginfo->msgid, node);
1245                 }
1246
1247                 /* complete the thread */
1248                 summary_thread_build(summaryview);
1249         } else {
1250                 for (; mlist != NULL; mlist = mlist->next) {
1251                         msginfo = (MsgInfo *)mlist->data;
1252
1253                         summary_set_header(text, msginfo);
1254
1255                         node = gtk_ctree_insert_node
1256                                 (ctree, NULL, NULL, text, 2,
1257                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1258                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1259                         summary_set_marks_func(ctree, node, summaryview);
1260
1261                         if (msginfo->msgid && *msginfo->msgid &&
1262                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1263                             == NULL)
1264                                 g_hash_table_insert(msgid_table,
1265                                                     msginfo->msgid, node);
1266                 }
1267         }
1268
1269         if (prefs_common.enable_hscrollbar) {
1270                 gint optimal_width;
1271
1272                 optimal_width = gtk_clist_optimal_column_width
1273                         (GTK_CLIST(ctree), S_COL_SUBJECT);
1274                 gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
1275                                            optimal_width);
1276         }
1277
1278         debug_print(_("done.\n"));
1279         STATUSBAR_POP(summaryview->mainwin);
1280         if (debug_mode)
1281                 debug_print("\tmsgid hash table size = %d\n",
1282                             g_hash_table_size(msgid_table));
1283 }
1284
1285 struct wcachefp
1286 {
1287         FILE *cache_fp;
1288         FILE *mark_fp;
1289 };
1290
1291 gint summary_write_cache(SummaryView *summaryview)
1292 {
1293         struct wcachefp fps;
1294         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1295         gint ver = CACHE_VERSION;
1296         gchar *buf;
1297         gchar *cachefile, *markfile;
1298
1299         if (!summaryview->folder_item || !summaryview->folder_item->path)
1300                 return -1;
1301
1302         cachefile = folder_item_get_cache_file(summaryview->folder_item);
1303         g_return_val_if_fail(cachefile != NULL, -1);
1304         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
1305                 FILE_OP_ERROR(cachefile, "fopen");
1306                 g_free(cachefile);
1307                 return -1;
1308         }
1309         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
1310                 FILE_OP_ERROR(cachefile, "chmod");
1311         g_free(cachefile);
1312
1313         markfile = folder_item_get_mark_file(summaryview->folder_item);
1314         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
1315                 FILE_OP_ERROR(markfile, "fopen");
1316                 fclose(fps.cache_fp);
1317                 g_free(markfile);
1318                 return -1;
1319         }
1320         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
1321                 FILE_OP_ERROR(markfile, "chmod");
1322         g_free(markfile);
1323
1324         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
1325                               summaryview->folder_item->path);
1326         debug_print(buf);
1327         STATUSBAR_PUSH(summaryview->mainwin, buf);
1328         g_free(buf);
1329
1330         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
1331         ver = MARK_VERSION;
1332         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
1333
1334         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
1335
1336         fclose(fps.cache_fp);
1337         fclose(fps.mark_fp);
1338
1339         debug_print(_("done.\n"));
1340         STATUSBAR_POP(summaryview->mainwin);
1341
1342         return 0;
1343 }
1344
1345 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
1346                                      gpointer data)
1347 {
1348         struct wcachefp *fps = data;
1349         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1350
1351         if (msginfo == NULL) return;
1352
1353         procmsg_write_cache(msginfo, fps->cache_fp);
1354         procmsg_write_flags(msginfo, fps->mark_fp);
1355 }
1356
1357 static void summary_set_header(gchar *text[], MsgInfo *msginfo)
1358 {
1359         static gchar date_modified[80];
1360         static gchar *to = NULL;
1361
1362         text[S_COL_MARK]   = NULL;
1363         text[S_COL_UNREAD] = NULL;
1364         text[S_COL_MIME] = NULL;
1365         text[S_COL_NUMBER] = itos(msginfo->msgnum);
1366         text[S_COL_SIZE]   = to_human_readable(msginfo->size);
1367
1368         if (msginfo->date_t) {
1369                 procheader_date_get_localtime(date_modified,
1370                                               sizeof(date_modified),
1371                                               msginfo->date_t);
1372                 text[S_COL_DATE] = date_modified;
1373         } else if (msginfo->date)
1374                 text[S_COL_DATE] = msginfo->date;
1375         else
1376                 text[S_COL_DATE] = _("(No Date)");
1377
1378         text[S_COL_FROM] = msginfo->fromname ? msginfo->fromname :
1379                 _("(No From)");
1380         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
1381             cur_account && cur_account->address) {
1382                 gchar *from;
1383
1384                 Xalloca(from, strlen(msginfo->from) + 1, return);
1385                 strcpy(from, msginfo->from);
1386                 extract_address(from);
1387                 if (!strcmp(from, cur_account->address)) {
1388                         g_free(to);
1389                         to = g_strconcat("-->", msginfo->to, NULL);
1390                         text[S_COL_FROM] = to;
1391                 }
1392         }
1393
1394         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
1395                 _("(No Subject)");
1396 }
1397
1398 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
1399                                 gboolean new_window)
1400 {
1401         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1402         MsgInfo *msginfo;
1403         gchar *filename;
1404         static gboolean lock = FALSE;
1405
1406         if (!new_window && summaryview->displayed == row) return;
1407         g_return_if_fail(row != NULL);
1408
1409         if (lock) return;
1410         lock = TRUE;
1411
1412         STATUSBAR_POP(summaryview->mainwin);
1413
1414         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1415
1416         filename = procmsg_get_message_file(msginfo);
1417         if (!filename) {
1418                 lock = FALSE;
1419                 return;
1420         }
1421         g_free(filename);
1422
1423         if (MSG_IS_NEW(msginfo->flags))
1424                 summaryview->newmsgs--;
1425         if (MSG_IS_UNREAD(msginfo->flags))
1426                 summaryview->unread--;
1427         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
1428                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1429                 summary_set_row_marks(summaryview, row);
1430                 gtk_clist_thaw(GTK_CLIST(ctree));
1431                 summary_status_show(summaryview);
1432         }
1433
1434         if (new_window) {
1435                 MessageView *msgview;
1436
1437                 msgview = messageview_create_with_new_window();
1438                 messageview_show(msgview, msginfo);
1439         } else {
1440                 MessageView *msgview;
1441
1442                 msgview = summaryview->messageview;
1443
1444                 summaryview->displayed = row;
1445                 if (!summaryview->msg_is_toggled_on)
1446                         summary_toggle_view(summaryview);
1447                 messageview_show(msgview, msginfo);
1448                 if (msgview->type == MVIEW_TEXT ||
1449                     (msgview->type == MVIEW_MIME &&
1450                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
1451                         gtk_widget_grab_focus(summaryview->ctree);
1452                 GTK_EVENTS_FLUSH();
1453                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
1454         }
1455
1456         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
1457                 header_window_show(summaryview->headerwin, msginfo);
1458
1459         lock = FALSE;
1460 }
1461
1462 void summary_redisplay_msg(SummaryView *summaryview)
1463 {
1464         GtkCTreeNode *node;
1465
1466         if (summaryview->displayed) {
1467                 node = summaryview->displayed;
1468                 summaryview->displayed = NULL;
1469                 summary_display_msg(summaryview, node, FALSE);
1470         }
1471 }
1472
1473 void summary_open_msg(SummaryView *summaryview)
1474 {
1475         if (!summaryview->selected) return;
1476
1477         summary_display_msg(summaryview, summaryview->selected, TRUE);
1478 }
1479
1480 void summary_view_source(SummaryView * summaryview)
1481 {
1482         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1483         MsgInfo *msginfo;
1484         SourceWindow *srcwin;
1485
1486         if (!summaryview->selected) return;
1487
1488         srcwin = source_window_create();
1489         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1490         source_window_show_msg(srcwin, msginfo);
1491         source_window_show(srcwin);
1492 }
1493
1494 void summary_reedit(SummaryView *summaryview)
1495 {
1496         MsgInfo *msginfo;
1497
1498         if (!summaryview->selected) return;
1499         if (!summaryview->folder_item ||
1500             summaryview->folder_item->stype != F_DRAFT) return;
1501
1502         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
1503                                               summaryview->selected);
1504         if (!msginfo) return;
1505
1506         compose_reedit(msginfo);
1507 }
1508
1509 void summary_step(SummaryView *summaryview, GtkScrollType type)
1510 {
1511         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1512
1513         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
1514                                 type, 0.0);
1515
1516         if (summaryview->msg_is_toggled_on)
1517                 summary_display_msg(summaryview, summaryview->selected, FALSE);
1518 }
1519
1520 static void summary_toggle_view(SummaryView *summaryview)
1521 {
1522         MainWindow *mainwin = summaryview->mainwin;
1523         union CompositeWin *cwin = &mainwin->win;
1524         GtkWidget *vpaned = NULL;
1525         GtkWidget *container = NULL;
1526
1527         switch (mainwin->type) {
1528         case SEPARATE_NONE:
1529                 vpaned = cwin->sep_none.vpaned;
1530                 container = cwin->sep_none.hpaned;
1531                 break;
1532         case SEPARATE_FOLDER:
1533                 vpaned = cwin->sep_folder.vpaned;
1534                 container = mainwin->vbox_body;
1535                 break;
1536         case SEPARATE_MESSAGE:
1537         case SEPARATE_BOTH:
1538                 return;
1539         }
1540
1541         if (vpaned->parent != NULL) {
1542                 summaryview->msg_is_toggled_on = FALSE;
1543                 summaryview->displayed = NULL;
1544                 gtk_widget_ref(vpaned);
1545                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
1546                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1547                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1548                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1549         } else {
1550                 summaryview->msg_is_toggled_on = TRUE;
1551                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1552                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1553                 gtk_widget_unref(vpaned);
1554                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1555                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1556         }
1557
1558         gtk_widget_grab_focus(summaryview->ctree);
1559 }
1560
1561 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
1562 {
1563         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1564         MsgInfo *msginfo;
1565         MsgFlags flags;
1566
1567         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1568         if (!msginfo) return;
1569
1570         flags = msginfo->flags;
1571
1572         gtk_ctree_node_set_foreground(ctree, row, &summaryview->color_normal);
1573
1574         /* set new/unread column */
1575         if (MSG_IS_NEW(flags)) {
1576                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1577                                           newxpm, newxpmmask);
1578         } else if (MSG_IS_UNREAD(flags)) {
1579                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1580                                           unreadxpm, unreadxpmmask);
1581         } else if (MSG_IS_REPLIED(flags)) {
1582                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1583                                           repliedxpm, repliedxpmmask);
1584         } else if (MSG_IS_FORWARDED(flags)) {
1585                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1586                                           forwardedxpm, forwardedxpmmask);
1587         } else {
1588                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
1589         }
1590
1591         /* set mark column */
1592         if (MSG_IS_DELETED(flags)) {
1593                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
1594                                           deletedxpm, deletedxpmmask);
1595                 gtk_ctree_node_set_foreground(ctree, row,
1596                                               &summaryview->color_dim);
1597         } else if (MSG_IS_MARKED(flags)) {
1598                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
1599                                           markxpm, markxpmmask);
1600         } else if (MSG_IS_MOVE(flags)) {
1601                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "o");
1602                 gtk_ctree_node_set_foreground(ctree, row,
1603                                               &summaryview->color_marked);
1604         } else if (MSG_IS_COPY(flags)) {
1605                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
1606                 gtk_ctree_node_set_foreground(ctree, row,
1607                                               &summaryview->color_marked);
1608         } else {
1609                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
1610         }
1611
1612         if (MSG_IS_MIME(flags)) {
1613                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
1614                                           clipxpm, clipxpmmask);
1615         } else {
1616                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
1617         }
1618 }
1619
1620 void summary_set_marks_selected(SummaryView *summaryview)
1621 {
1622         summary_set_row_marks(summaryview, summaryview->selected);
1623 }
1624
1625 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
1626 {
1627         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1628         MsgInfo *msginfo;
1629
1630         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1631         msginfo->to_folder = NULL;
1632         if (MSG_IS_DELETED(msginfo->flags))
1633                 summaryview->deleted--;
1634         if (MSG_IS_MOVE(msginfo->flags))
1635                 summaryview->moved--;
1636         if (MSG_IS_COPY(msginfo->flags))
1637                 summaryview->copied--;
1638         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
1639         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
1640         summary_set_row_marks(summaryview, row);
1641         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
1642 }
1643
1644 void summary_mark(SummaryView *summaryview)
1645 {
1646         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1647         GList *cur;
1648
1649         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1650                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
1651
1652         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
1653 }
1654
1655 static void summary_mark_row_as_read(SummaryView *summaryview,
1656                                      GtkCTreeNode *row)
1657 {
1658         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1659         MsgInfo *msginfo;
1660
1661         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1662         if (MSG_IS_NEW(msginfo->flags))
1663                 summaryview->newmsgs--;
1664         if (MSG_IS_UNREAD(msginfo->flags))
1665                 summaryview->unread--;
1666         if (MSG_IS_NEW(msginfo->flags) ||
1667             MSG_IS_UNREAD(msginfo->flags)) {
1668                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
1669                 summary_set_row_marks(summaryview, row);
1670                 debug_print(_("Message %d is marked as being read\n"),
1671                             msginfo->msgnum);
1672         }
1673 }
1674
1675 void summary_mark_as_read(SummaryView *summaryview)
1676 {
1677         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1678         GList *cur;
1679
1680         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1681                 summary_mark_row_as_read(summaryview,
1682                                          GTK_CTREE_NODE(cur->data));
1683
1684         summary_status_show(summaryview);
1685 }
1686
1687 static void summary_mark_row_as_unread(SummaryView *summaryview,
1688                                        GtkCTreeNode *row)
1689 {
1690         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1691         MsgInfo *msginfo;
1692
1693         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1694         if (MSG_IS_DELETED(msginfo->flags)) {
1695                 msginfo->to_folder = NULL;
1696                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
1697                 summaryview->deleted--;
1698         }
1699         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
1700         if (!MSG_IS_UNREAD(msginfo->flags)) {
1701                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
1702                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
1703                                           unreadxpm, unreadxpmmask);
1704                 summaryview->unread++;
1705                 debug_print(_("Message %d is marked as unread\n"),
1706                             msginfo->msgnum);
1707         }
1708         summary_set_row_marks(summaryview, row);
1709 }
1710
1711 void summary_mark_as_unread(SummaryView *summaryview)
1712 {
1713         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1714         GList *cur;
1715
1716         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1717                 summary_mark_row_as_unread(summaryview,
1718                                            GTK_CTREE_NODE(cur->data));
1719
1720         summary_status_show(summaryview);
1721 }
1722
1723 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
1724 {
1725         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1726         MsgInfo *msginfo;
1727
1728         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1729
1730         if (MSG_IS_DELETED(msginfo->flags)) return;
1731
1732         msginfo->to_folder = NULL;
1733         if (MSG_IS_MOVE(msginfo->flags))
1734                 summaryview->moved--;
1735         if (MSG_IS_COPY(msginfo->flags))
1736                 summaryview->copied--;
1737         MSG_UNSET_FLAGS(msginfo->flags,
1738                         MSG_MARKED |
1739                         MSG_MOVE |
1740                         MSG_COPY);
1741         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
1742         summaryview->deleted++;
1743
1744         if (!prefs_common.immediate_exec)
1745                 summary_set_row_marks(summaryview, row);
1746
1747         debug_print(_("Message %s/%d is set to delete\n"),
1748                     msginfo->folder->path, msginfo->msgnum);
1749 }
1750
1751 void summary_delete(SummaryView *summaryview)
1752 {
1753         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1754         GList *cur;
1755
1756         if (!summaryview->folder_item ||
1757             summaryview->folder_item->folder->type == F_NEWS) return;
1758
1759         /* if current folder is trash, don't delete */
1760         if (summaryview->folder_item->stype == F_TRASH) {
1761                 alertpanel_notice(_("Current folder is Trash."));
1762                 return;
1763         }
1764
1765         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
1766                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
1767
1768         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
1769
1770         if (prefs_common.immediate_exec)
1771                 summary_execute(summaryview);
1772         else
1773                 summary_status_show(summaryview);
1774 }
1775
1776 void summary_delete_duplicated(SummaryView *summaryview)
1777 {
1778         if (!summaryview->folder_item ||
1779             summaryview->folder_item->folder->type == F_NEWS) return;
1780         if (summaryview->folder_item->stype == F_TRASH) return;
1781
1782         main_window_cursor_wait(summaryview->mainwin);
1783         debug_print(_("Deleting duplicated messages..."));
1784         STATUSBAR_PUSH(summaryview->mainwin,
1785                        _("Deleting duplicated messages..."));
1786
1787         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
1788                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
1789                                 summaryview);
1790
1791         if (prefs_common.immediate_exec)
1792                 summary_execute(summaryview);
1793         else
1794                 summary_status_show(summaryview);
1795
1796         debug_print(_("done.\n"));
1797         STATUSBAR_POP(summaryview->mainwin);
1798         main_window_cursor_normal(summaryview->mainwin);
1799 }
1800
1801 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
1802                                            SummaryView *summaryview)
1803 {
1804         GtkCTreeNode *found;
1805         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
1806
1807         if (!msginfo->msgid || !*msginfo->msgid) return;
1808
1809         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
1810
1811         if (found && found != node)
1812                 summary_delete_row(summaryview, node);
1813 }
1814
1815 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
1816 {
1817         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1818         MsgInfo *msginfo;
1819
1820         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1821         msginfo->to_folder = NULL;
1822         if (MSG_IS_DELETED(msginfo->flags))
1823                 summaryview->deleted--;
1824         if (MSG_IS_MOVE(msginfo->flags))
1825                 summaryview->moved--;
1826         if (MSG_IS_COPY(msginfo->flags))
1827                 summaryview->copied--;
1828         MSG_UNSET_FLAGS(msginfo->flags,
1829                         MSG_MARKED |
1830                         MSG_DELETED |
1831                         MSG_MOVE |
1832                         MSG_COPY);
1833         summary_set_row_marks(summaryview, row);
1834
1835         debug_print(_("Message %s/%d is unmarked\n"),
1836                     msginfo->folder->path, msginfo->msgnum);
1837 }
1838
1839 void summary_unmark(SummaryView *summaryview)
1840 {
1841         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1842         GList *cur;
1843
1844         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
1845              cur = cur->next)
1846                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
1847
1848         summary_status_show(summaryview);
1849 }
1850
1851 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
1852                                 FolderItem *to_folder)
1853 {
1854         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1855         MsgInfo *msginfo;
1856
1857         g_return_if_fail(to_folder != NULL);
1858
1859         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1860         msginfo->to_folder = to_folder;
1861         if (MSG_IS_DELETED(msginfo->flags))
1862                 summaryview->deleted--;
1863         MSG_UNSET_FLAGS(msginfo->flags,
1864                         MSG_MARKED | MSG_DELETED | MSG_COPY);
1865         if (!MSG_IS_MOVE(msginfo->flags)) {
1866                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
1867                 summaryview->moved++;
1868         }
1869         if (!prefs_common.immediate_exec)
1870                 summary_set_row_marks(summaryview, row);
1871
1872         debug_print(_("Message %d is set to move to %s\n"),
1873                     msginfo->msgnum, to_folder->path);
1874 }
1875
1876 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
1877 {
1878         GList *cur;
1879
1880         if (!to_folder) return;
1881         if (!summaryview->folder_item ||
1882             summaryview->folder_item->folder->type == F_NEWS) return;
1883         if (summaryview->folder_item == to_folder) {
1884                 alertpanel_notice(_("Destination is same as current folder."));
1885                 return;
1886         }
1887
1888         for (cur = GTK_CLIST(summaryview->ctree)->selection;
1889              cur != NULL; cur = cur->next)
1890                 summary_move_row_to
1891                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
1892
1893         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
1894
1895         if (prefs_common.immediate_exec)
1896                 summary_execute(summaryview);
1897         else
1898                 summary_status_show(summaryview);
1899 }
1900
1901 void summary_move_to(SummaryView *summaryview)
1902 {
1903         FolderItem *to_folder;
1904
1905         if (!summaryview->folder_item ||
1906             summaryview->folder_item->folder->type == F_NEWS) return;
1907
1908         to_folder = foldersel_folder_sel(NULL);
1909         summary_move_selected_to(summaryview, to_folder);
1910 }
1911
1912 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
1913                                 FolderItem *to_folder)
1914 {
1915         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1916         MsgInfo *msginfo;
1917
1918         g_return_if_fail(to_folder != NULL);
1919
1920         msginfo = gtk_ctree_node_get_row_data(ctree, row);
1921         msginfo->to_folder = to_folder;
1922         if (MSG_IS_DELETED(msginfo->flags))
1923                 summaryview->deleted--;
1924         MSG_UNSET_FLAGS(msginfo->flags,
1925                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
1926         if (!MSG_IS_COPY(msginfo->flags)) {
1927                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
1928                 summaryview->copied++;
1929         }
1930         if (!prefs_common.immediate_exec)
1931                 summary_set_row_marks(summaryview, row);
1932
1933         debug_print(_("Message %d is set to copy to %s\n"),
1934                     msginfo->msgnum, to_folder->path);
1935 }
1936
1937 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
1938 {
1939         GList *cur;
1940
1941         if (!to_folder) return;
1942         if (!summaryview->folder_item ||
1943             summaryview->folder_item->folder->type == F_NEWS) return;
1944         if (summaryview->folder_item == to_folder) {
1945                 alertpanel_notice
1946                         (_("Destination to copy is same as current folder."));
1947                 return;
1948         }
1949
1950         for (cur = GTK_CLIST(summaryview->ctree)->selection;
1951              cur != NULL; cur = cur->next)
1952                 summary_copy_row_to
1953                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
1954
1955         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
1956
1957         if (prefs_common.immediate_exec)
1958                 summary_execute(summaryview);
1959         else
1960                 summary_status_show(summaryview);
1961 }
1962
1963 void summary_copy_to(SummaryView *summaryview)
1964 {
1965         FolderItem *to_folder;
1966
1967         if (!summaryview->folder_item ||
1968             summaryview->folder_item->folder->type == F_NEWS) return;
1969
1970         to_folder = foldersel_folder_sel(NULL);
1971         summary_copy_selected_to(summaryview, to_folder);
1972 }
1973
1974 void summary_select_all(SummaryView *summaryview)
1975 {
1976         if (summaryview->messages >= 500) {
1977                 STATUSBAR_PUSH(summaryview->mainwin,
1978                                _("Selecting all messages..."));
1979                 main_window_cursor_wait(summaryview->mainwin);
1980         }
1981
1982         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
1983
1984         if (summaryview->messages >= 500) {
1985                 STATUSBAR_POP(summaryview->mainwin);
1986                 main_window_cursor_normal(summaryview->mainwin);
1987         }
1988 }
1989
1990 void summary_unselect_all(SummaryView *summaryview)
1991 {
1992         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
1993 }
1994
1995 void summary_save_as(SummaryView *summaryview)
1996 {
1997         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1998         MsgInfo *msginfo;
1999         gchar *src, *dest;
2000
2001         if (!summaryview->selected) return;
2002         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2003         if (!msginfo) return;
2004
2005         dest = filesel_select_file(_("Save as"), NULL);
2006         if (!dest) return;
2007         if (is_file_exist(dest)) {
2008                 AlertValue aval;
2009
2010                 aval = alertpanel(_("Overwrite"),
2011                                   _("Overwrite existing file?"),
2012                                   _("OK"), _("Cancel"), NULL);
2013                 if (G_ALERTDEFAULT != aval) return;
2014         }
2015
2016         src = procmsg_get_message_file(msginfo);
2017         copy_file(src, dest);
2018         g_free(src);
2019 }
2020
2021 void summary_print(SummaryView *summaryview)
2022 {
2023         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2024         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2025         MsgInfo *msginfo;
2026         GList *cur;
2027         gchar *cmdline;
2028         gchar *p;
2029
2030         if (clist->selection == NULL) return;
2031
2032         cmdline = input_dialog(_("Print"),
2033                                _("Enter the print command line:\n"
2034                                  "(`%s' will be replaced with file name)"),
2035                                prefs_common.print_cmd);
2036         if (!cmdline) return;
2037         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2038             strchr(p + 2, '%')) {
2039                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2040                                  cmdline);
2041                 return;
2042         }
2043
2044         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2045                 msginfo = gtk_ctree_node_get_row_data
2046                         (ctree, GTK_CTREE_NODE(cur->data));
2047                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2048         }
2049 }
2050
2051 void summary_execute(SummaryView *summaryview)
2052 {
2053         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2054         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2055         GtkCTreeNode *node, *next;
2056
2057         if (!summaryview->folder_item ||
2058             summaryview->folder_item->folder->type == F_NEWS) return;
2059
2060         gtk_clist_freeze(clist);
2061
2062         if (prefs_common.enable_thread)
2063                 summary_unthread_for_exec(summaryview);
2064
2065         summary_execute_move(summaryview);
2066         summary_execute_copy(summaryview);
2067         summary_execute_delete(summaryview);
2068
2069         node = GTK_CTREE_NODE(clist->row_list);
2070         while (node != NULL) {
2071                 next = GTK_CTREE_NODE_NEXT(node);
2072                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2073                         if (node == summaryview->displayed) {
2074                                 messageview_clear(summaryview->messageview);
2075                                 summaryview->displayed = NULL;
2076                         }
2077                         if (GTK_CTREE_ROW(node)->children != NULL)
2078                                 g_warning("summary_execute(): children != NULL\n");
2079                         else
2080                                 gtk_ctree_remove_node(ctree, node);
2081                 }
2082                 node = next;
2083         }
2084
2085         if (prefs_common.enable_thread)
2086                 summary_thread_build(summaryview);
2087
2088         summaryview->selected = clist->selection ?
2089                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2090
2091         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2092                 menu_set_insensitive_all
2093                         (GTK_MENU_SHELL(summaryview->popupmenu));
2094                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2095         } else
2096                 gtk_widget_grab_focus(summaryview->ctree);
2097
2098         summary_update_status(summaryview);
2099         summary_status_show(summaryview);
2100
2101         summary_write_cache(summaryview);
2102
2103         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2104
2105         gtk_clist_thaw(clist);
2106 }
2107
2108 static void summary_execute_move(SummaryView *summaryview)
2109 {
2110         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2111         GSList *cur;
2112
2113         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2114
2115         /* search moving messages and execute */
2116         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2117                                 summaryview);
2118
2119         if (summaryview->mlist) {
2120                 procmsg_move_messages(summaryview->mlist);
2121
2122                 folder_item_scan_foreach(summaryview->folder_table);
2123                 folderview_update_item_foreach(summaryview->folder_table);
2124
2125                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2126                         procmsg_msginfo_free((MsgInfo *)cur->data);
2127                 g_slist_free(summaryview->mlist);
2128                 summaryview->mlist = NULL;
2129         }
2130
2131         g_hash_table_destroy(summaryview->folder_table);
2132         summaryview->folder_table = NULL;
2133 }
2134
2135 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2136                                       gpointer data)
2137 {
2138         SummaryView *summaryview = data;
2139         MsgInfo *msginfo;
2140
2141         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2142
2143         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2144                 g_hash_table_insert(summaryview->folder_table,
2145                                     msginfo->to_folder, GINT_TO_POINTER(1));
2146
2147                 summaryview->mlist =
2148                         g_slist_append(summaryview->mlist, msginfo);
2149                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2150
2151                 if (msginfo->msgid && *msginfo->msgid &&
2152                     node == g_hash_table_lookup(summaryview->msgid_table,
2153                                                 msginfo->msgid))
2154                         g_hash_table_remove(summaryview->msgid_table,
2155                                             msginfo->msgid);
2156         }
2157 }
2158
2159 static void summary_execute_copy(SummaryView *summaryview)
2160 {
2161         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2162
2163         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2164
2165         /* search copying messages and execute */
2166         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2167                                 summaryview);
2168
2169         if (summaryview->mlist) {
2170                 procmsg_copy_messages(summaryview->mlist);
2171
2172                 folder_item_scan_foreach(summaryview->folder_table);
2173                 folderview_update_item_foreach(summaryview->folder_table);
2174
2175                 g_slist_free(summaryview->mlist);
2176                 summaryview->mlist = NULL;
2177         }
2178
2179         g_hash_table_destroy(summaryview->folder_table);
2180         summaryview->folder_table = NULL;
2181 }
2182
2183 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
2184                                       gpointer data)
2185 {
2186         SummaryView *summaryview = data;
2187         MsgInfo *msginfo;
2188
2189         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2190
2191         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
2192                 g_hash_table_insert(summaryview->folder_table,
2193                                     msginfo->to_folder, GINT_TO_POINTER(1));
2194
2195                 summaryview->mlist =
2196                         g_slist_append(summaryview->mlist, msginfo);
2197
2198                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
2199                 summary_set_row_marks(summaryview, node);
2200         }
2201 }
2202
2203 static void summary_execute_delete(SummaryView *summaryview)
2204 {
2205         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2206         FolderItem *trash;
2207         GSList *cur;
2208
2209         trash = summaryview->folder_item->folder->trash;
2210         g_return_if_fail(trash != NULL);
2211         if (summaryview->folder_item == trash) return;
2212
2213         /* search deleting messages and execute */
2214         gtk_ctree_pre_recursive
2215                 (ctree, NULL, summary_execute_delete_func, summaryview);
2216
2217         if (!summaryview->mlist) return;
2218
2219         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
2220
2221         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2222                 procmsg_msginfo_free((MsgInfo *)cur->data);
2223
2224         g_slist_free(summaryview->mlist);
2225         summaryview->mlist = NULL;
2226
2227         folder_item_scan(trash);
2228         folderview_update_item(trash, FALSE);
2229 }
2230
2231 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
2232                                         gpointer data)
2233 {
2234         SummaryView *summaryview = data;
2235         MsgInfo *msginfo;
2236
2237         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2238
2239         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
2240                 summaryview->mlist =
2241                         g_slist_append(summaryview->mlist, msginfo);
2242                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2243
2244                 if (msginfo->msgid && *msginfo->msgid &&
2245                     node == g_hash_table_lookup(summaryview->msgid_table,
2246                                                 msginfo->msgid))
2247                         g_hash_table_remove(summaryview->msgid_table,
2248                                             msginfo->msgid);
2249         }
2250 }
2251
2252 /* thread functions */
2253
2254 void summary_thread_build(SummaryView *summaryview)
2255 {
2256         debug_print(_("Building threads..."));
2257         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
2258         main_window_cursor_wait(summaryview->mainwin);
2259
2260         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2261
2262         gtk_ctree_pre_recursive_to_depth
2263                 (GTK_CTREE(summaryview->ctree), NULL, 1,
2264                  GTK_CTREE_FUNC(summary_thread_func),
2265                  summaryview->msgid_table);
2266
2267         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2268
2269         debug_print(_("done.\n"));
2270         STATUSBAR_POP(summaryview->mainwin);
2271         main_window_cursor_normal(summaryview->mainwin);
2272 }
2273
2274 void summary_unthread(SummaryView *summaryview)
2275 {
2276         GtkCTreeNode *node;
2277         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2278
2279         debug_print(_("Unthreading..."));
2280         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
2281         main_window_cursor_wait(summaryview->mainwin);
2282
2283         gtk_clist_freeze(GTK_CLIST(ctree));
2284
2285         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2286              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2287                 summary_unthread_func(ctree, node, NULL);
2288         }
2289
2290         gtk_clist_thaw(GTK_CLIST(ctree));
2291
2292         debug_print(_("done.\n"));
2293         STATUSBAR_POP(summaryview->mainwin);
2294         main_window_cursor_normal(summaryview->mainwin);
2295 }
2296
2297 static void summary_unthread_for_exec(SummaryView *summaryview)
2298 {
2299         GtkCTreeNode *node;
2300         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2301
2302         debug_print(_("Unthreading for execution..."));
2303
2304         gtk_clist_freeze(GTK_CLIST(ctree));
2305
2306         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2307              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
2308                 summary_unthread_for_exec_func(ctree, node, NULL);
2309         }
2310
2311         gtk_clist_thaw(GTK_CLIST(ctree));
2312
2313         debug_print(_("done.\n"));
2314 }
2315
2316 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
2317                                 gpointer data)
2318 {
2319         MsgInfo *msginfo;
2320         GtkCTreeNode *parent;
2321         GHashTable *msgid_table = (GHashTable *)data;
2322
2323         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2324
2325         if (!msginfo || !msginfo->inreplyto) return;
2326
2327         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
2328
2329         if (parent && parent != node) {
2330                 gtk_ctree_move(ctree, node, parent, NULL);
2331                 gtk_ctree_expand(ctree, parent);
2332         }
2333 }
2334
2335 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
2336                                   gpointer data)
2337 {
2338         GtkCTreeNode *child;
2339         GtkCTreeNode *sibling;
2340
2341         child = GTK_CTREE_ROW(node)->children;
2342         sibling = GTK_CTREE_ROW(node)->sibling;
2343
2344         while (child != NULL) {
2345                 GtkCTreeNode *next_child;
2346
2347                 next_child = GTK_CTREE_ROW(child)->sibling;
2348                 gtk_ctree_move(ctree, child, NULL, sibling);
2349                 child = next_child;
2350         }
2351 }
2352
2353 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
2354                                            gpointer data)
2355 {
2356         MsgInfo *msginfo;
2357         GtkCTreeNode *top_parent;
2358         GtkCTreeNode *child;
2359         GtkCTreeNode *sibling;
2360
2361         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2362
2363         if (!msginfo ||
2364             (!MSG_IS_MOVE(msginfo->flags) &&
2365              !MSG_IS_DELETED(msginfo->flags)))
2366                 return;
2367         child = GTK_CTREE_ROW(node)->children;
2368         if (!child) return;
2369
2370         for (top_parent = node;
2371              GTK_CTREE_ROW(top_parent)->parent != NULL;
2372              top_parent = GTK_CTREE_ROW(top_parent)->parent)
2373                 ;
2374         sibling = GTK_CTREE_ROW(top_parent)->sibling;
2375
2376         while (child != NULL) {
2377                 GtkCTreeNode *next_child;
2378
2379                 next_child = GTK_CTREE_ROW(child)->sibling;
2380                 gtk_ctree_move(ctree, child, NULL, sibling);
2381                 child = next_child;
2382         }
2383 }
2384
2385 void summary_filter(SummaryView *summaryview)
2386 {
2387         if (!prefs_common.fltlist) return;
2388
2389         debug_print(_("filtering..."));
2390         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
2391         main_window_cursor_wait(summaryview->mainwin);
2392
2393         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2394
2395         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2396                                 GTK_CTREE_FUNC(summary_filter_func),
2397                                 summaryview);
2398
2399         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2400
2401         if (prefs_common.immediate_exec)
2402                 summary_execute(summaryview);
2403         else
2404                 summary_status_show(summaryview);
2405
2406         debug_print(_("done.\n"));
2407         STATUSBAR_POP(summaryview->mainwin);
2408         main_window_cursor_normal(summaryview->mainwin);
2409 }
2410
2411 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
2412                                 gpointer data)
2413 {
2414         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2415         SummaryView *summaryview = data;
2416         gchar *file;
2417         FolderItem *dest;
2418
2419         file = procmsg_get_message_file_path(msginfo);
2420         dest = filter_get_dest_folder(prefs_common.fltlist, file);
2421         g_free(file);
2422
2423         if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
2424             summaryview->folder_item != dest)
2425                 summary_move_row_to(summaryview, node, dest);
2426 }
2427
2428 /* callback functions */
2429
2430 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
2431                                    SummaryView *summaryview)
2432 {
2433         if (!event)
2434                 return;
2435
2436         if (!summaryview->msg_is_toggled_on && summaryview->selected)
2437                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2438         else
2439                 summary_toggle_view(summaryview);
2440 }
2441
2442 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
2443                                    SummaryView *summaryview)
2444 {
2445         if (!event) return;
2446
2447         if (event->button == 3) {
2448                 /* right clicked */
2449                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
2450                                NULL, NULL, event->button, event->time);
2451         } else if (event->button == 2) {
2452                 summaryview->display_msg = TRUE;
2453         } else if (event->button == 1) {
2454                 if (!prefs_common.emulate_emacs &&
2455                     summaryview->msg_is_toggled_on)
2456                         summaryview->display_msg = TRUE;
2457         }
2458 }
2459
2460 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
2461                                     SummaryView *summaryview)
2462 {
2463 }
2464
2465 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
2466 {
2467         summary_key_pressed(summaryview->ctree, event, summaryview);
2468 }
2469
2470 #define BREAK_ON_MODIFIER_KEY() \
2471         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2472
2473 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
2474                                 SummaryView *summaryview)
2475 {
2476         GtkCTree *ctree = GTK_CTREE(widget);
2477         GtkCTreeNode *node;
2478         FolderItem *to_folder;
2479
2480         if (!event) return;
2481
2482         switch (event->keyval) {
2483         case GDK_g:             /* Go */
2484         case GDK_G:
2485                 BREAK_ON_MODIFIER_KEY();
2486
2487                 if (gtk_signal_n_emissions_by_name
2488                         (GTK_OBJECT(ctree), "key_press_event") > 0)
2489                         gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree),
2490                                                      "key_press_event");
2491
2492                 to_folder = foldersel_folder_sel(NULL);
2493                 if (to_folder) {
2494                         debug_print(_("Go to %s\n"), to_folder->path);
2495                         folderview_select(summaryview->folderview, to_folder);
2496                 }
2497                 return;
2498         case GDK_w:             /* Write new message */
2499                 BREAK_ON_MODIFIER_KEY();
2500                 if (summaryview->folder_item)
2501                         compose_new(summaryview->folder_item->folder->account);
2502                 else
2503                         compose_new(NULL);
2504                 return;
2505         case GDK_D:             /* Empty trash */
2506                 BREAK_ON_MODIFIER_KEY();
2507                 if (gtk_signal_n_emissions_by_name
2508                         (GTK_OBJECT(ctree), "key_press_event") > 0)
2509                         gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree),
2510                                                      "key_press_event");
2511                 main_window_empty_trash(summaryview->mainwin, TRUE);
2512                 return;
2513         case GDK_Q:             /* Quit */
2514                 BREAK_ON_MODIFIER_KEY();
2515
2516                 if (prefs_common.confirm_on_exit) {
2517                         if (alertpanel(_("Exit"), _("Exit this program?"),
2518                                        _("OK"), _("Cancel"), NULL)
2519                                        == G_ALERTDEFAULT) {
2520                                 app_will_exit(NULL, summaryview->mainwin);
2521                         }
2522                 }
2523                 return;
2524         case GDK_Left:          /* Move focus */
2525         case GDK_Escape:
2526                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2527                 return;
2528         default:
2529         }
2530
2531         if (!summaryview->selected) {
2532                 node = gtk_ctree_node_nth(ctree, 0);
2533                 if (node)
2534                         gtk_ctree_select(ctree, node);
2535                 else
2536                         return;
2537         }
2538
2539         switch (event->keyval) {
2540         case GDK_space:         /* Page down or go to the next */
2541                 if (summaryview->displayed != summaryview->selected) {
2542                         summary_display_msg(summaryview,
2543                                             summaryview->selected, FALSE);
2544                         break;
2545                 }
2546                 if (!textview_scroll_page(summaryview->messageview->textview,
2547                                           FALSE))
2548                         summary_select_next_unread(summaryview);
2549                 break;
2550         case GDK_n:             /* Next */
2551         case GDK_N:
2552                 BREAK_ON_MODIFIER_KEY();
2553                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2554                 break;
2555         case GDK_BackSpace:     /* Page up */
2556         case GDK_Delete:
2557                 textview_scroll_page(summaryview->messageview->textview, TRUE);
2558                 break;
2559         case GDK_p:             /* Prev */
2560         case GDK_P:
2561                 BREAK_ON_MODIFIER_KEY();
2562                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2563                 break;
2564         case GDK_v:             /* Toggle summary mode / message mode */
2565         case GDK_V:
2566                 BREAK_ON_MODIFIER_KEY();
2567
2568                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
2569                         summary_display_msg(summaryview,
2570                                             summaryview->selected, FALSE);
2571                 else
2572                         summary_toggle_view(summaryview);
2573                 break;
2574         case GDK_Return:        /* Scroll up/down one line */
2575                 if (summaryview->displayed != summaryview->selected) {
2576                         summary_display_msg(summaryview,
2577                                             summaryview->selected, FALSE);
2578                         break;
2579                 }
2580                 textview_scroll_one_line(summaryview->messageview->textview,
2581                                          (event->state & GDK_MOD1_MASK) != 0);
2582                 break;
2583         case GDK_asterisk:      /* Mark */
2584                 summary_mark(summaryview);
2585                 break;
2586         case GDK_exclam:        /* Mark as unread */
2587                 summary_mark_as_unread(summaryview);
2588                 break;
2589         case GDK_d:             /* Delete */
2590                 BREAK_ON_MODIFIER_KEY();
2591                 summary_delete(summaryview);
2592                 break;
2593         case GDK_u:             /* Unmark */
2594         case GDK_U:
2595                 BREAK_ON_MODIFIER_KEY();
2596                 summary_unmark(summaryview);
2597                 break;
2598         case GDK_o:             /* Move */
2599                 BREAK_ON_MODIFIER_KEY();
2600                 summary_move_to(summaryview);
2601                 break;
2602         case GDK_O:             /* Copy */
2603                 BREAK_ON_MODIFIER_KEY();
2604                 summary_copy_to(summaryview);
2605                 break;
2606         case GDK_x:             /* Execute */
2607         case GDK_X:
2608                 BREAK_ON_MODIFIER_KEY();
2609                 summary_execute(summaryview);
2610                 break;
2611         case GDK_a:             /* Reply to the message */
2612                 BREAK_ON_MODIFIER_KEY();
2613                 summary_reply_cb(summaryview,
2614                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
2615                 break;
2616         case GDK_A:             /* Reply to the message with quotation */
2617                 BREAK_ON_MODIFIER_KEY();
2618                 summary_reply_cb(summaryview,
2619                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
2620                 break;
2621         case GDK_f:             /* Forward the message */
2622                 BREAK_ON_MODIFIER_KEY();
2623                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
2624                 break;
2625         case GDK_F:
2626                 BREAK_ON_MODIFIER_KEY();
2627                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
2628                 break;
2629         case GDK_y:             /* Save the message */
2630                 BREAK_ON_MODIFIER_KEY();
2631                 summary_save_as(summaryview);
2632                 break;
2633         default:
2634         }
2635 }
2636
2637 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
2638 {
2639         if (summaryview->folder_item->stype == F_DRAFT)
2640                 summary_reedit(summaryview);
2641         else
2642                 summary_open_msg(summaryview);
2643
2644         summaryview->display_msg = FALSE;
2645 }
2646
2647 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
2648                              gint column, SummaryView *summaryview)
2649 {
2650         MsgInfo *msginfo;
2651
2652         summary_status_show(summaryview);
2653         summary_set_menu_sensitive(summaryview);
2654
2655         if (GTK_CLIST(ctree)->selection &&
2656              GTK_CLIST(ctree)->selection->next) {
2657                 summaryview->display_msg = FALSE;
2658                 return;
2659         }
2660
2661         summaryview->selected = row;
2662
2663         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2664
2665         switch (column) {
2666         case S_COL_MARK:
2667                 if (MSG_IS_MARKED(msginfo->flags)) {
2668                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
2669                         summary_set_row_marks(summaryview, row);
2670                 } else
2671                         summary_mark_row(summaryview, row);
2672                 break;
2673         case S_COL_UNREAD:
2674                 if (MSG_IS_UNREAD(msginfo->flags)) {
2675                         summary_mark_row_as_read(summaryview, row);
2676                         summary_status_show(summaryview);
2677                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
2678                          !MSG_IS_FORWARDED(msginfo->flags)) {
2679                         summary_mark_row_as_unread(summaryview, row);
2680                         summary_status_show(summaryview);
2681                 }
2682                 break;
2683         default:
2684         }
2685
2686         if (summaryview->display_msg)
2687                 summary_display_msg(summaryview, row, FALSE);
2688
2689         summaryview->display_msg = FALSE;
2690 }
2691
2692 static void summary_col_resized(GtkCList *clist, gint column, gint width,
2693                                 SummaryView *summaryview)
2694 {
2695         switch (column) {
2696         case S_COL_MARK:
2697                 prefs_common.summary_col_mark = width;
2698                 break;
2699         case S_COL_UNREAD:
2700                 prefs_common.summary_col_unread = width;
2701                 break;
2702         case S_COL_MIME:
2703                 prefs_common.summary_col_mime = width;
2704                 break;
2705         case S_COL_NUMBER:
2706                 prefs_common.summary_col_number = width;
2707                 break;
2708         case S_COL_SIZE:
2709                 prefs_common.summary_col_size = width;
2710                 break;
2711         case S_COL_DATE:
2712                 prefs_common.summary_col_date = width;
2713                 break;
2714         case S_COL_FROM:
2715                 prefs_common.summary_col_from = width;
2716                 break;
2717         case S_COL_SUBJECT:
2718                 prefs_common.summary_col_subject = width;
2719                 break;
2720         default:
2721         }
2722 }
2723
2724 static void summary_reply_cb(SummaryView *summaryview, guint action,
2725                              GtkWidget *widget)
2726 {
2727         MsgInfo *msginfo;
2728
2729         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2730                                               summaryview->selected);
2731         if (!msginfo) return;
2732
2733         switch ((ComposeReplyMode)action) {
2734         case COMPOSE_REPLY:
2735                 compose_reply(msginfo, prefs_common.reply_with_quote, FALSE);
2736                 break;
2737         case COMPOSE_REPLY_WITH_QUOTE:
2738                 compose_reply(msginfo, TRUE, FALSE);
2739                 break;
2740         case COMPOSE_REPLY_WITHOUT_QUOTE:
2741                 compose_reply(msginfo, FALSE, FALSE);
2742                 break;
2743         case COMPOSE_REPLY_TO_ALL:
2744                 compose_reply(msginfo, prefs_common.reply_with_quote, TRUE);
2745                 break;
2746         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
2747                 compose_reply(msginfo, TRUE, TRUE);
2748                 break;
2749         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
2750                 compose_reply(msginfo, FALSE, TRUE);
2751                 break;
2752         case COMPOSE_FORWARD:
2753                 compose_forward(msginfo, FALSE);
2754                 break;
2755         case COMPOSE_FORWARD_AS_ATTACH:
2756                 compose_forward(msginfo, TRUE);
2757                 break;
2758         default:
2759                 compose_reply(msginfo, prefs_common.reply_with_quote, FALSE);
2760         }
2761
2762         summary_set_marks_selected(summaryview);
2763 }
2764
2765 static void summary_show_all_header_cb(SummaryView *summaryview,
2766                                        guint action, GtkWidget *widget)
2767 {
2768         header_window_show_cb(summaryview->mainwin, action, widget);
2769 }
2770
2771 static void summary_num_clicked(GtkWidget *button, SummaryView *summaryview)
2772 {
2773         summary_sort(summaryview, SORT_BY_NUMBER);
2774 }
2775
2776 static void summary_size_clicked(GtkWidget *button, SummaryView *summaryview)
2777 {
2778         summary_sort(summaryview, SORT_BY_SIZE);
2779 }
2780
2781 static void summary_date_clicked(GtkWidget *button, SummaryView *summaryview)
2782 {
2783         summary_sort(summaryview, SORT_BY_DATE);
2784 }
2785
2786 static void summary_from_clicked(GtkWidget *button, SummaryView *summaryview)
2787 {
2788         summary_sort(summaryview, SORT_BY_FROM);
2789 }
2790
2791 static void summary_subject_clicked(GtkWidget *button,
2792                                     SummaryView *summaryview)
2793 {
2794         summary_sort(summaryview, SORT_BY_SUBJECT);
2795 }
2796
2797 void summary_change_display_item(SummaryView *summaryview)
2798 {
2799         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2800
2801         gtk_clist_set_column_visibility(clist, S_COL_MARK, prefs_common.show_mark);
2802         gtk_clist_set_column_visibility(clist, S_COL_UNREAD, prefs_common.show_unread);
2803         gtk_clist_set_column_visibility(clist, S_COL_MIME, prefs_common.show_mime);
2804         gtk_clist_set_column_visibility(clist, S_COL_NUMBER, prefs_common.show_number);
2805         gtk_clist_set_column_visibility(clist, S_COL_SIZE, prefs_common.show_size);
2806         gtk_clist_set_column_visibility(clist, S_COL_DATE, prefs_common.show_date);
2807         gtk_clist_set_column_visibility(clist, S_COL_FROM, prefs_common.show_from);
2808         gtk_clist_set_column_visibility(clist, S_COL_SUBJECT, prefs_common.show_subject);
2809 }
2810
2811 static void summary_start_drag (GtkWidget   *widget,
2812                                 gint         button,
2813                                 GdkEvent    *event,
2814                                 SummaryView *summaryview)
2815 {
2816         GtkTargetList *list;
2817         GdkDragContext *context;
2818
2819         g_return_if_fail(summaryview != NULL);
2820         g_return_if_fail(summaryview->folder_item != NULL);
2821         g_return_if_fail(summaryview->folder_item->folder != NULL);
2822         if (summaryview->folder_item->folder->type == F_NEWS ||
2823             summaryview->selected == NULL)
2824                 return;
2825
2826         list = gtk_target_list_new(summary_drag_types, 1);
2827
2828         context = gtk_drag_begin(widget, list,
2829                                  GDK_ACTION_MOVE, button, event);
2830         gtk_drag_set_icon_default(context);
2831 }
2832
2833 static void summary_drag_data_get(GtkWidget        *widget,
2834                                   GdkDragContext   *drag_context,
2835                                   GtkSelectionData *selection_data,
2836                                   guint             info,
2837                                   guint             time,
2838                                   SummaryView      *summaryview)
2839 {
2840         if (info == TARGET_MAIL_URI_LIST) {
2841                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2842                 GList *cur;
2843                 MsgInfo *msginfo;
2844                 gchar *mail_list = NULL, *tmp1, *tmp2;
2845
2846                 for (cur = GTK_CLIST(ctree)->selection;
2847                      cur != NULL; cur = cur->next) {
2848                         msginfo = gtk_ctree_node_get_row_data
2849                                 (ctree, GTK_CTREE_NODE(cur->data));
2850                         tmp2 = procmsg_get_message_file_path(msginfo);
2851                         if (!tmp2) continue;
2852                         tmp1 = g_strconcat("file:/", tmp2, NULL);
2853                         g_free(tmp2);
2854
2855                         if (!mail_list) {
2856                                 mail_list = tmp1;
2857                         } else {
2858                                 tmp2 = g_strconcat(mail_list, tmp1, NULL);
2859                                 g_free(mail_list);
2860                                 g_free(tmp1);
2861                                 mail_list = tmp2;
2862                         }
2863                 }
2864
2865                 if (mail_list != NULL) {
2866                         gtk_selection_data_set(selection_data,
2867                                                selection_data->target, 8,
2868                                                mail_list, strlen(mail_list));
2869                         g_free(mail_list);
2870                 } 
2871         } else if (info == TARGET_DUMMY) {
2872                 if (GTK_CLIST(summaryview->ctree)->selection)
2873                         gtk_selection_data_set(selection_data,
2874                                                selection_data->target, 8,
2875                                                "Dummy", 6);
2876         }
2877 }
2878
2879
2880 /* custom compare functions for sorting */
2881
2882 static gint summary_cmp_by_num(GtkCList *clist,
2883                                gconstpointer ptr1, gconstpointer ptr2)
2884 {
2885         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
2886         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
2887
2888         return msginfo1->msgnum - msginfo2->msgnum;
2889 }
2890
2891 static gint summary_cmp_by_size(GtkCList *clist,
2892                                 gconstpointer ptr1, gconstpointer ptr2)
2893 {
2894         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
2895         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
2896
2897         return msginfo1->size - msginfo2->size;
2898 }
2899
2900 static gint summary_cmp_by_date(GtkCList *clist,
2901                                gconstpointer ptr1, gconstpointer ptr2)
2902 {
2903         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
2904         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
2905
2906         return msginfo1->date_t - msginfo2->date_t;
2907 }
2908
2909 static gint summary_cmp_by_from(GtkCList *clist,
2910                                gconstpointer ptr1, gconstpointer ptr2)
2911 {
2912         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
2913         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
2914
2915         if (!msginfo1->fromname)
2916                 return (msginfo2->fromname != NULL);
2917         if (!msginfo2->fromname)
2918                 return -1;
2919
2920         return strcasecmp(msginfo1->fromname, msginfo2->fromname);
2921 }
2922
2923 static gint summary_cmp_by_subject(GtkCList *clist,
2924                                gconstpointer ptr1, gconstpointer ptr2)
2925 {
2926         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
2927         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
2928
2929         if (!msginfo1->subject)
2930                 return (msginfo2->subject != NULL);
2931         if (!msginfo2->subject)
2932                 return -1;
2933
2934         return strcasecmp(msginfo1->subject, msginfo2->subject);
2935 }