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