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