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