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