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