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