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