remove debug print
[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         summary_set_label(view, color, NULL);
390 }
391
392 /* summary_set_label_color() - labelcolor parameter is the color *flag*
393  * for the messsage; not the color index */
394 void summary_set_label_color(GtkCTree *ctree, GtkCTreeNode *node,
395                              guint labelcolor)
396 {
397         GdkColor  color;
398         GtkStyle *style, *prev_style, *ctree_style;
399         MsgInfo  *msginfo;
400         gint     color_index = ((gint)(labelcolor >> 7)) - 1;
401
402         ctree_style = gtk_widget_get_style(GTK_WIDGET(ctree));
403
404         prev_style = gtk_ctree_node_get_row_style(ctree, node);
405
406         if (!prev_style)
407                 prev_style = ctree_style;
408
409         style = gtk_style_copy(prev_style);
410
411         if (color_index < 0 || color_index >= LABEL_COLORS_ELEMS) {
412                 labelcolor = 0;
413                 color.red = ctree_style->fg[GTK_STATE_NORMAL].red;
414                 color.green = ctree_style->fg[GTK_STATE_NORMAL].green;
415                 color.blue = ctree_style->fg[GTK_STATE_NORMAL].blue;
416                 style->fg[GTK_STATE_NORMAL] = color;
417
418                 color.red = ctree_style->fg[GTK_STATE_SELECTED].red;
419                 color.green = ctree_style->fg[GTK_STATE_SELECTED].green;
420                 color.blue = ctree_style->fg[GTK_STATE_SELECTED].blue;
421                 style->fg[GTK_STATE_SELECTED] = color;
422                 gtk_ctree_node_set_row_style(ctree, node, style);
423         }
424         else
425                 color = label_colors[color_index].color;
426
427         msginfo = gtk_ctree_node_get_row_data(ctree, node);
428
429         MSG_UNSET_FLAGS(msginfo->flags, MSG_LABEL);
430         MSG_SET_FLAGS(msginfo->flags, labelcolor);
431
432         if ( style ) {
433                 style->fg[GTK_STATE_NORMAL] = color;
434                 style->fg[GTK_STATE_SELECTED] = color;
435                 gtk_ctree_node_set_row_style(ctree, node, style);
436         }
437 }
438
439 void summary_set_label(SummaryView *summaryview, guint labelcolor, GtkWidget *widget)
440 {
441         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
442         GtkCList *clist = GTK_CLIST(summaryview->ctree);
443         GList *cur;
444   
445         for (cur = clist->selection; cur != NULL; cur = cur->next)
446                 summary_set_label_color(ctree, GTK_CTREE_NODE(cur->data), labelcolor);
447 }
448
449 /* summary_create_label_pixmaps() - creates label pixmaps. perhaps a little 
450  * bit contrived. */
451 static gboolean summary_create_label_pixmaps(SummaryView *summaryview)
452 {
453         const char *FMT = "+      c #%2.2X%2.2X%2.2X";
454         char buf[40];
455         char * dummy_xpm[] = {
456                 "16 16 3 1",
457                 "       c None",
458                 ".      c #000000",
459                 "+      c #000000",
460                 "................",
461                 ".++++++++++++++.",
462                 ".++++++++++++++.",
463                 ".++++++++++++++.",
464                 ".++++++++++++++.",
465                 ".++++++++++++++.",
466                 ".++++++++++++++.",
467                 ".++++++++++++++.",
468                 ".++++++++++++++.",
469                 ".++++++++++++++.",
470                 ".++++++++++++++.",
471                 ".++++++++++++++.",
472                 ".++++++++++++++.",
473                 ".++++++++++++++.",
474                 ".++++++++++++++.",
475                 "................"
476         };
477
478         gint n;
479
480         for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
481                 GdkBitmap *xpmmask;
482                 GdkPixmap *xpm;
483                 GtkPixmap *pixmap;
484
485                 /* put correct color in xpm data */
486                 sprintf(buf, FMT, label_colors[n].color.red >> 8,
487                         label_colors[n].color.green >> 8, 
488                         label_colors[n].color.blue >> 8);
489                 dummy_xpm[3] = buf;                             
490
491                 /* create pixmaps */
492                 xpm = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(summaryview->scrolledwin)->window, &xpmmask, NULL, 
493                                                    (gchar **) &dummy_xpm);
494                 pixmap = GTK_PIXMAP(gtk_pixmap_new(xpm, xpmmask)); 
495
496                 /* store it somewhere */
497                 label_colors[n].xpm = xpm;
498                 label_colors[n].xbm = xpmmask;
499                 label_colors[n].pixmap = pixmap;
500         }
501 }
502
503 static void label_menu_item_activate_item_cb(GtkMenuItem *label_menu_item, gpointer data)
504 {
505         SummaryView  *summaryview;
506         GtkMenuShell *label_menu;
507         GtkCheckMenuItem **items;
508         int  n;
509         GList *cur, *sel;
510
511         summaryview = (SummaryView *) data;
512         g_return_if_fail(summaryview);
513         if (NULL == (sel = GTK_CLIST(summaryview->ctree)->selection))
514                 return;
515         
516         label_menu = GTK_MENU_SHELL(summaryview->label_menu);
517         g_return_if_fail(label_menu);
518
519         items = alloca( (LABEL_COLORS_ELEMS + 1) * sizeof(GtkWidget *));
520         g_return_if_fail(items);
521
522         /* NOTE: don't return prematurely because we set the "dont_toggle" state
523          * for check menu items */
524         gtk_object_set_data(GTK_OBJECT(label_menu), "dont_toggle", GINT_TO_POINTER(1));
525
526         /* clear items. get item pointers. */
527         for (n = 0, cur = label_menu->children; cur != NULL; cur = cur->next) {
528                 if (GTK_IS_CHECK_MENU_ITEM(cur->data)) {
529                         gtk_check_menu_item_set_state(GTK_CHECK_MENU_ITEM(cur->data), FALSE);
530                         items[n] = GTK_CHECK_MENU_ITEM(cur->data);
531                         n++;
532                 }
533         }
534
535         if (n == (LABEL_COLORS_ELEMS + 1)) {
536                 /* iterate all messages and set the state of the appropriate items */
537                 for (; sel != NULL; sel = sel->next) {
538                         MsgInfo *msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
539                                                 GTK_CTREE_NODE(sel->data));
540                         gint menu_item;                         
541                         if (msginfo) {
542                                 menu_item = ((msginfo->flags & MSG_LABEL) >> 7);
543                                 if (!items[menu_item]->active)
544                                         gtk_check_menu_item_set_state(items[menu_item], TRUE);
545                         }
546                 }
547         }
548         else 
549                 g_warning("invalid number of color elements (%d)\n", n);
550         
551         /* reset "dont_toggle" state */
552         gtk_object_set_data(GTK_OBJECT(label_menu), "dont_toggle", GINT_TO_POINTER(0));
553 }
554
555 static void summary_create_label_menu(SummaryView *summaryview)
556 {
557         const gint LABEL_MENU_POS = 5;
558         GtkWidget *label_menu_item;
559         GtkWidget *label_menu;
560         GtkWidget *item;
561         gint       i;
562
563         summary_create_label_pixmaps(summaryview);
564         
565         label_menu_item = gtk_menu_item_new_with_label(_("Label"));
566         gtk_menu_insert(GTK_MENU(summaryview->popupmenu), label_menu_item, LABEL_MENU_POS);
567         gtk_signal_connect(GTK_OBJECT(label_menu_item), "activate",
568                 GTK_SIGNAL_FUNC(label_menu_item_activate_item_cb), summaryview);
569                 
570         gtk_widget_show(label_menu_item);
571         summaryview->label_menu_item = label_menu_item;
572
573         label_menu = gtk_menu_new();
574
575         /* create sub items. for the menu item activation callback we pass the 
576          * index of label_colors[] as data parameter. for the None color we pass
577          * an invalid (high) value. also we attach a data pointer so we can
578          * always get back the SummaryView pointer. */
579         item = gtk_check_menu_item_new_with_label(_("None"));
580         gtk_menu_append(GTK_MENU(label_menu), item);
581         gtk_signal_connect(GTK_OBJECT(item), "activate",  
582                 GTK_SIGNAL_FUNC(label_menu_item_activate_cb),
583                 GUINT_TO_POINTER(-1));
584         gtk_object_set_data(GTK_OBJECT(item), "view", summaryview);     
585         gtk_widget_show(item);
586         
587         item = gtk_menu_item_new();
588         gtk_menu_append(GTK_MENU(label_menu), item);
589         gtk_widget_show(item);
590
591         /* create pixmap/label menu items */
592         for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
593                 GtkWidget *label, *hbox, *align, *pixmap;
594                 item = gtk_check_menu_item_new();
595                 
596                 label = gtk_label_new(label_colors[i].label);
597                 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
598                 gtk_widget_show(label);
599                 hbox = gtk_hbox_new(FALSE, 0);
600                 gtk_widget_show(hbox);
601                 gtk_container_add(GTK_CONTAINER(item), hbox);
602
603                 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
604                 gtk_widget_show(align);
605                 gtk_container_set_border_width (GTK_CONTAINER(align), 1);
606
607                 pixmap = GTK_WIDGET(label_colors[i].pixmap);
608                 
609                 gtk_container_add (GTK_CONTAINER (align), pixmap);
610                 gtk_widget_set_usize (align, 16, 16);
611                 gtk_widget_show(pixmap);
612
613                 gtk_box_pack_start(GTK_BOX (hbox), align, FALSE, FALSE, 0);
614                 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
615
616                 gtk_menu_append(GTK_MENU(label_menu), item);
617                 gtk_signal_connect(GTK_OBJECT(item), "activate", 
618                                    GTK_SIGNAL_FUNC(label_menu_item_activate_cb),
619                                    GUINT_TO_POINTER(i + 1));
620                 gtk_object_set_data(GTK_OBJECT(item), "view", summaryview);
621                 gtk_widget_show(item);
622
623         }
624         
625         gtk_widget_show(label_menu);
626         gtk_menu_item_set_submenu(GTK_MENU_ITEM(label_menu_item), label_menu);
627         summaryview->label_menu = label_menu;   
628 }
629
630 SummaryView *summary_create(void)
631 {
632         SummaryView *summaryview;
633         gchar *titles[N_SUMMARY_COLS] = {_("M"), _("U")};
634         GtkWidget *vbox;
635         GtkWidget *scrolledwin;
636         GtkWidget *ctree;
637         GtkWidget *hbox;
638         GtkWidget *statlabel_folder;
639         GtkWidget *statlabel_select;
640         GtkWidget *statlabel_msgs;
641         GtkWidget *toggle_eventbox;
642         GtkWidget *toggle_arrow;
643         GtkWidget *popupmenu;
644         GtkItemFactory *popupfactory;
645         gint n_entries;
646         gint i;
647
648         debug_print(_("Creating summary view...\n"));
649         summaryview = g_new0(SummaryView, 1);
650
651         vbox = gtk_vbox_new(FALSE, 2);
652
653         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
654         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
655                                        GTK_POLICY_AUTOMATIC,
656                                        GTK_POLICY_ALWAYS);
657         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
658         gtk_widget_set_usize(vbox,
659                              prefs_common.summaryview_width,
660                              prefs_common.summaryview_height);
661
662         if (prefs_common.trans_hdr) {
663                 titles[S_COL_NUMBER]  = _("No.");
664                 titles[S_COL_DATE]    = _("Date");
665                 titles[S_COL_FROM]    = _("From");
666                 titles[S_COL_SUBJECT] = _("Subject");
667         } else {
668                 titles[S_COL_NUMBER]  = "No.";
669                 titles[S_COL_DATE]    = "Date";
670                 titles[S_COL_FROM]    = "From";
671                 titles[S_COL_SUBJECT] = "Subject";
672         }
673         titles[S_COL_SIZE]  = _("Size");
674         titles[S_COL_SCORE] = _("Score");
675
676         ctree = gtk_sctree_new_with_titles(N_SUMMARY_COLS, S_COL_SUBJECT, titles);
677         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
678                                             GTK_CLIST(ctree)->hadjustment);
679         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
680                                             GTK_CLIST(ctree)->vadjustment);
681         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
682         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_EXTENDED);
683         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_MARK,
684                                            GTK_JUSTIFY_CENTER);
685         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_UNREAD,
686                                            GTK_JUSTIFY_CENTER);
687         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_MIME,
688                                            GTK_JUSTIFY_CENTER);
689         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_NUMBER,
690                                            GTK_JUSTIFY_RIGHT);
691         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_SCORE,
692                                            GTK_JUSTIFY_RIGHT);
693         gtk_clist_set_column_justification(GTK_CLIST(ctree), S_COL_SIZE,
694                                            GTK_JUSTIFY_RIGHT);
695         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_MARK,
696                                    SUMMARY_COL_MARK_WIDTH);
697         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_UNREAD,
698                                    SUMMARY_COL_UNREAD_WIDTH);
699         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_MIME,
700                                    SUMMARY_COL_MIME_WIDTH);
701         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_NUMBER,
702                                    prefs_common.summary_col_number);
703         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SCORE,
704                                    prefs_common.summary_col_score);
705         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SIZE,
706                                    prefs_common.summary_col_size);
707         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_DATE,
708                                    prefs_common.summary_col_date);
709         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_FROM,
710                                    prefs_common.summary_col_from);
711         gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
712                                    prefs_common.summary_col_subject);
713         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
714         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
715                                      GTK_CTREE_EXPANDER_SQUARE);
716 #if 0
717         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
718         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
719                                      GTK_CTREE_EXPANDER_TRIANGLE);
720 #endif
721         gtk_ctree_set_indent(GTK_CTREE(ctree), 18);
722         gtk_object_set_user_data(GTK_OBJECT(ctree), summaryview);
723
724         /* don't let title buttons take key focus */
725         for (i = 0; i < N_SUMMARY_COLS; i++)
726                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
727                                        GTK_CAN_FOCUS);
728
729         /* connect signal to the buttons for sorting */
730         gtk_signal_connect
731                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_NUMBER].button),
732                  "clicked",
733                  GTK_SIGNAL_FUNC(summary_num_clicked),
734                  summaryview);
735         gtk_signal_connect
736                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SCORE].button),
737                  "clicked",
738                  GTK_SIGNAL_FUNC(summary_score_clicked),
739                  summaryview);
740         gtk_signal_connect
741                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SIZE].button),
742                  "clicked",
743                  GTK_SIGNAL_FUNC(summary_size_clicked),
744                  summaryview);
745         gtk_signal_connect
746                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_DATE].button),
747                  "clicked",
748                  GTK_SIGNAL_FUNC(summary_date_clicked),
749                  summaryview);
750         gtk_signal_connect
751                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_FROM].button),
752                  "clicked",
753                  GTK_SIGNAL_FUNC(summary_from_clicked),
754                  summaryview);
755         gtk_signal_connect
756                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_SUBJECT].button),
757                  "clicked",
758                  GTK_SIGNAL_FUNC(summary_subject_clicked),
759                  summaryview);
760         gtk_signal_connect
761                 (GTK_OBJECT(GTK_CLIST(ctree)->column[S_COL_MARK].button),
762                  "clicked",
763                  GTK_SIGNAL_FUNC(summary_mark_clicked),
764                  summaryview);
765
766         /* create status label */
767         hbox = gtk_hbox_new(FALSE, 0);
768         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
769
770         statlabel_folder = gtk_label_new("");
771         gtk_box_pack_start(GTK_BOX(hbox), statlabel_folder, FALSE, FALSE, 2);
772         statlabel_select = gtk_label_new("");
773         gtk_box_pack_start(GTK_BOX(hbox), statlabel_select, FALSE, FALSE, 16);
774
775         /* toggle view button */
776         toggle_eventbox = gtk_event_box_new();
777         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
778         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
779         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
780
781         statlabel_msgs = gtk_label_new("");
782         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
783
784         /* create popup menu */
785         n_entries = sizeof(summary_popup_entries) /
786                 sizeof(summary_popup_entries[0]);
787         popupmenu = menu_create_items(summary_popup_entries, n_entries,
788                                       "<SummaryView>", &popupfactory,
789                                       summaryview);
790
791         /* connect signals */
792         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
793                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
794         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
795                            GTK_SIGNAL_FUNC(summary_button_pressed),
796                            summaryview);
797         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
798                            GTK_SIGNAL_FUNC(summary_button_released),
799                            summaryview);
800         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
801                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
802         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
803                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
804         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
805                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
806         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
807                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
808                            summaryview);
809
810         summaryview->vbox = vbox;
811         summaryview->scrolledwin = scrolledwin;
812         summaryview->ctree = ctree;
813         summaryview->hbox = hbox;
814         summaryview->statlabel_folder = statlabel_folder;
815         summaryview->statlabel_select = statlabel_select;
816         summaryview->statlabel_msgs = statlabel_msgs;
817         summaryview->toggle_eventbox = toggle_eventbox;
818         summaryview->toggle_arrow = toggle_arrow;
819         summaryview->popupmenu = popupmenu;
820         summaryview->popupfactory = popupfactory;
821         summaryview->msg_is_toggled_on = TRUE;
822         summaryview->sort_mode = SORT_BY_NONE;
823         summaryview->sort_type = GTK_SORT_ASCENDING;
824
825 //      summary_create_label_menu(summaryview);
826
827         summary_change_display_item(summaryview);
828
829         gtk_widget_show_all(vbox);
830
831         return summaryview;
832 }
833
834 void summary_init(SummaryView *summaryview)
835 {
836         GtkStyle *style;
837         GtkWidget *pixmap;
838
839         PIXMAP_CREATE(summaryview->ctree, markxpm, markxpmmask, mark_xpm);
840         PIXMAP_CREATE(summaryview->ctree, deletedxpm, deletedxpmmask,
841                       deleted_xpm);
842         PIXMAP_CREATE(summaryview->ctree, newxpm, newxpmmask, new_xpm);
843         PIXMAP_CREATE(summaryview->ctree, unreadxpm, unreadxpmmask, unread_xpm);
844         PIXMAP_CREATE(summaryview->ctree, repliedxpm, repliedxpmmask,
845                       replied_xpm);
846         PIXMAP_CREATE(summaryview->ctree, forwardedxpm, forwardedxpmmask,
847                       forwarded_xpm);
848         PIXMAP_CREATE(summaryview->ctree, ignorethreadxpm, ignorethreadxpmmask,
849                       ignorethread_xpm);
850         PIXMAP_CREATE(summaryview->ctree, clipxpm, clipxpmmask, clip_xpm);
851         PIXMAP_CREATE(summaryview->hbox, folderxpm, folderxpmmask,
852                       DIRECTORY_OPEN_XPM);
853
854         pixmap = gtk_pixmap_new(clipxpm, clipxpmmask);
855         gtk_clist_set_column_widget(GTK_CLIST(summaryview->ctree),
856                                     S_COL_MIME, pixmap);
857         gtk_widget_show(pixmap);
858
859         if (!smallfont)
860                 smallfont = gdk_fontset_load(SMALL_FONT);
861
862         style = gtk_style_copy(gtk_widget_get_style
863                                 (summaryview->statlabel_folder));
864         if (smallfont) style->font = smallfont;
865         gtk_widget_set_style(summaryview->statlabel_folder, style);
866         gtk_widget_set_style(summaryview->statlabel_select, style);
867         gtk_widget_set_style(summaryview->statlabel_msgs, style);
868
869         pixmap = gtk_pixmap_new(folderxpm, folderxpmmask);
870         gtk_box_pack_start(GTK_BOX(summaryview->hbox), pixmap, FALSE, FALSE, 4);
871         gtk_box_reorder_child(GTK_BOX(summaryview->hbox), pixmap, 0);
872         gtk_widget_show(pixmap);
873
874         summary_clear_list(summaryview);
875         summary_set_menu_sensitive(summaryview);
876         summary_create_label_menu(summaryview);
877
878 }
879
880 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
881                                                  GtkCTreeNode *current_node)
882 {
883         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
884         GtkCTreeNode *node;
885         MsgInfo *msginfo;
886         gint best_score = MIN_SCORE;
887         GtkCTreeNode *best_node = NULL;
888
889         if (current_node)
890                 /*node = current_node;*/
891                 node = GTK_CTREE_NODE_NEXT(current_node);
892         else
893                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
894
895         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
896                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
897                 if (msginfo->score >= summaryview->important_score)
898                         break;
899                 if (msginfo->score > best_score) {
900                         best_score = msginfo->score;
901                         best_node = node;
902                 }
903         }
904
905         if (node != NULL)
906                 return node;
907         else
908                 return best_node;
909 }
910
911 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
912                                                  GtkCTreeNode *current_node)
913 {
914         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
915         GtkCTreeNode *node;
916         MsgInfo *msginfo;
917         gint best_score = MIN_SCORE;
918         GtkCTreeNode *best_node = NULL;
919
920         if (current_node)
921                 /*node = current_node;*/
922                 node = GTK_CTREE_NODE_PREV(current_node);
923         else
924                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
925
926         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
927                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
928                 if (msginfo->score >= summaryview->important_score)
929                         break;
930                 if (msginfo->score > best_score) {
931                         best_score = msginfo->score;
932                         best_node = node;
933                 }
934         }
935
936         if (node != NULL)
937                 return node;
938         else
939                 return best_node;
940 }
941
942 gboolean summary_show(SummaryView *summaryview, FolderItem *item,
943                       gboolean update_cache)
944 {
945         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
946         GtkCTreeNode *node;
947         GSList *mlist = NULL;
948         gchar *buf;
949         gboolean is_refresh;
950         guint prev_msgnum = 0;
951         GtkCTreeNode *selected_node = summaryview->folderview->selected;
952         GSList *cur;
953         gint sort_mode;
954         gint sort_type;
955
956         STATUSBAR_POP(summaryview->mainwin);
957
958         is_refresh = (!prefs_common.open_inbox_on_inc &&
959                       item == summaryview->folder_item) ? TRUE : FALSE;
960         if (is_refresh) {
961                 prev_msgnum = summary_get_current_msgnum(summaryview);
962                 if (prev_msgnum < 1)
963                         is_refresh = FALSE;
964         }
965
966         /* process the marks if any */
967         if (summaryview->moved > 0 || summaryview->copied > 0) {
968                 AlertValue val;
969
970                 val = alertpanel(_("Process mark"),
971                                  _("Some marks are left. Process it?"),
972                                  _("Yes"), _("No"), _("Cancel"));
973                 if (G_ALERTDEFAULT == val)
974                         summary_execute(summaryview);
975                 else if (G_ALERTALTERNATE == val)
976                         summary_write_cache(summaryview);
977                 else
978                         return FALSE;
979         } else
980                 summary_write_cache(summaryview);
981
982         summaryview->folderview->opened = selected_node;
983
984         gtk_clist_freeze(GTK_CLIST(ctree));
985
986         summary_clear_list(summaryview);
987         summary_set_menu_sensitive(summaryview);
988         messageview_clear(summaryview->messageview);
989
990         buf = NULL;
991         if (!item || !item->path || !item->parent || item->no_select ||
992             (item->folder->type == F_MH &&
993              ((buf = folder_item_get_path(item)) == NULL ||
994               change_dir(buf) < 0))) {
995                 g_free(buf);
996                 debug_print(_("empty folder\n\n"));
997                 summary_clear_all(summaryview);
998                 summaryview->folder_item = item;
999                 gtk_clist_thaw(GTK_CLIST(ctree));
1000                 return TRUE;
1001         }
1002         g_free(buf);
1003
1004         summaryview->folder_item = item;
1005
1006         gtk_signal_disconnect_by_data(GTK_OBJECT(ctree), summaryview);
1007
1008         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
1009         debug_print("%s\n", buf);
1010         STATUSBAR_PUSH(summaryview->mainwin, buf);
1011         g_free(buf);
1012
1013         main_window_cursor_wait(summaryview->mainwin);
1014
1015         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
1016
1017         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
1018                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1019
1020                 msginfo->score = score_message(global_scoring, msginfo);
1021                 if (msginfo->score != MAX_SCORE &&
1022                     msginfo->score != MIN_SCORE) {
1023                         msginfo->score += score_message(item->prefs->scoring,
1024                                                         msginfo);
1025                 }
1026         }
1027
1028         summaryview->killed_messages = NULL;
1029         if ((global_scoring || item->prefs->scoring) &&
1030             (item->folder->type == F_NEWS)) {
1031                 GSList *not_killed;
1032                 gint kill_score;
1033
1034                 not_killed = NULL;
1035                 kill_score = prefs_common.kill_score;
1036                 if (item->prefs->kill_score > kill_score)
1037                         kill_score = item->prefs->kill_score;
1038                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
1039                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1040
1041                         if (MSG_IS_NEWS(msginfo->flags) &&
1042                             (msginfo->score <= kill_score))
1043                                 summaryview->killed_messages = g_slist_append(summaryview->killed_messages, msginfo);
1044                         else
1045                                 not_killed = g_slist_append(not_killed,
1046                                                             msginfo);
1047                 }
1048                 g_slist_free(mlist);
1049                 mlist = not_killed;
1050         }
1051
1052         STATUSBAR_POP(summaryview->mainwin);
1053
1054         /* set ctree and hash table from the msginfo list
1055            creating thread, and count the number of messages */
1056         summary_set_ctree_from_list(summaryview, mlist);
1057
1058         g_slist_free(mlist);
1059
1060         summary_write_cache(summaryview);
1061
1062         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
1063                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
1064         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
1065                            GTK_SIGNAL_FUNC(summary_button_pressed),
1066                            summaryview);
1067         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
1068                            GTK_SIGNAL_FUNC(summary_button_released),
1069                            summaryview);
1070         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
1071                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
1072         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
1073                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
1074         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
1075                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
1076
1077         /*connect drag and drop signal*/
1078         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
1079                            GTK_SIGNAL_FUNC (summary_start_drag),
1080                            summaryview);
1081         gtk_signal_connect(GTK_OBJECT (ctree),"drag_data_get",
1082                            GTK_SIGNAL_FUNC (summary_drag_data_get),
1083                            summaryview);
1084
1085         gtk_clist_thaw(GTK_CLIST(ctree));
1086
1087         /* sort before */
1088         sort_mode = prefs_folder_item_get_sort_mode(item);
1089         sort_type = prefs_folder_item_get_sort_type(item);
1090
1091         if (sort_mode != SORT_BY_NONE) {
1092                 summaryview->sort_mode = sort_mode;
1093                 if (sort_type == GTK_SORT_DESCENDING)
1094                         summaryview->sort_type = GTK_SORT_ASCENDING;
1095                 else
1096                         summaryview->sort_type = GTK_SORT_DESCENDING;
1097
1098                 summary_sort(summaryview, sort_mode);
1099         }
1100
1101         if (is_refresh) {
1102                 summary_select_by_msgnum(summaryview, prev_msgnum);
1103         } else {
1104                 /* select first unread message */
1105                 if (sort_mode == SORT_BY_SCORE)
1106                         node = summary_find_next_important_score(summaryview,
1107                                                                  NULL);
1108                 else
1109                         node = summary_find_next_unread_msg(summaryview, NULL);
1110
1111                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
1112                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1113                 if (node) {
1114                         GTK_EVENTS_FLUSH();
1115                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1116                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1117                         if (prefs_common.open_unread_on_enter)
1118                                 summaryview->display_msg = TRUE;
1119                         gtk_sctree_select(GTK_SCTREE(ctree), node);
1120                 }
1121         }
1122
1123         summary_status_show(summaryview);
1124
1125         summary_set_menu_sensitive(summaryview);
1126
1127         main_window_set_toolbar_sensitive
1128                 (summaryview->mainwin, summaryview->selected ? TRUE : FALSE);
1129
1130         debug_print("\n");
1131         STATUSBAR_PUSH(summaryview->mainwin, _("done."));
1132
1133         main_window_cursor_normal(summaryview->mainwin);
1134
1135         return TRUE;
1136 }
1137
1138 void summary_clear_list(SummaryView *summaryview)
1139 {
1140         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1141         gint optimal_width;
1142         GSList * cur;
1143
1144         gtk_clist_freeze(clist);
1145
1146         for(cur = summaryview->killed_messages ; cur != NULL ; 
1147             cur = g_slist_next(cur)) {
1148                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1149
1150                 procmsg_msginfo_free(msginfo);
1151         }
1152         g_slist_free(summaryview->killed_messages);
1153         summaryview->killed_messages = NULL;
1154
1155         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1156                                 NULL, summary_free_msginfo_func, NULL);
1157
1158         summaryview->folder_item = NULL;
1159
1160         summaryview->display_msg = FALSE;
1161
1162         summaryview->selected  = NULL;
1163         summaryview->displayed = NULL;
1164         summaryview->newmsgs   = summaryview->unread     = 0;
1165         summaryview->messages  = summaryview->total_size = 0;
1166         summaryview->deleted   = summaryview->moved      = 0;
1167         summaryview->copied    = 0;
1168         if (summaryview->msgid_table) {
1169                 g_hash_table_destroy(summaryview->msgid_table);
1170                 summaryview->msgid_table = NULL;
1171         }
1172         if (summaryview->subject_table) {
1173                 g_hash_table_destroy(summaryview->subject_table);
1174                 summaryview->subject_table = NULL;
1175         }
1176         summaryview->mlist = NULL;
1177         if (summaryview->folder_table) {
1178                 g_hash_table_destroy(summaryview->folder_table);
1179                 summaryview->folder_table = NULL;
1180         }
1181         summaryview->sort_mode = SORT_BY_NONE;
1182         summaryview->sort_type = GTK_SORT_ASCENDING;
1183
1184         gtk_clist_clear(clist);
1185         optimal_width = gtk_clist_optimal_column_width(clist, S_COL_SUBJECT);
1186         gtk_clist_set_column_width(clist, S_COL_SUBJECT, optimal_width);
1187
1188         gtk_clist_thaw(clist);
1189 }
1190
1191 void summary_clear_all(SummaryView *summaryview)
1192 {
1193         summary_clear_list(summaryview);
1194         summary_set_menu_sensitive(summaryview);
1195         main_window_set_toolbar_sensitive(summaryview->mainwin, FALSE);
1196         summary_status_show(summaryview);
1197 }
1198
1199 static void summary_set_menu_sensitive(SummaryView *summaryview)
1200 {
1201         GtkItemFactory *ifactory = summaryview->popupfactory;
1202         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1203         gboolean sens;
1204         SummarySelection selection;
1205
1206         if (!clist->row_list)
1207                 selection = SUMMARY_NONE;
1208         else if (!clist->selection)
1209                 selection = SUMMARY_SELECTED_NONE;
1210         else if (!clist->selection->next)
1211                 selection = SUMMARY_SELECTED_SINGLE;
1212         else
1213                 selection = SUMMARY_SELECTED_MULTIPLE;
1214
1215         main_window_set_menu_sensitive(summaryview->mainwin, selection);
1216
1217         if (selection == SUMMARY_NONE) {
1218                 GtkWidget *submenu;
1219
1220                 submenu = gtk_item_factory_get_widget
1221                         (summaryview->popupfactory, "/Mark");
1222                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1223                 menu_set_insensitive_all
1224                         (GTK_MENU_SHELL(summaryview->popupmenu));
1225                 return;
1226         } 
1227
1228         if (summaryview->folder_item->folder->type != F_NEWS) {
1229                 if (summaryview->folder_item->stype != F_TRASH)
1230                         menu_set_sensitive(ifactory, "/Delete", TRUE);
1231                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1232                 menu_set_sensitive(ifactory, "/Copy...", TRUE);
1233         }
1234
1235         gtk_widget_set_sensitive(summaryview->label_menu_item, TRUE);
1236         menu_set_sensitive(ifactory, "/Execute", TRUE);
1237
1238         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1239         menu_set_sensitive(ifactory, "/Reply",                    sens);
1240         menu_set_sensitive(ifactory, "/Reply to sender",          sens);
1241         menu_set_sensitive(ifactory, "/Reply to all",             sens);
1242         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1243         menu_set_sensitive(ifactory, "/Forward as attachment",    TRUE);
1244         
1245         menu_set_sensitive(ifactory, "/Open in new window", sens);
1246         menu_set_sensitive(ifactory, "/View source", sens);
1247         menu_set_sensitive(ifactory, "/Show all header", sens);
1248         if ((summaryview->folder_item->stype == F_DRAFT) ||
1249             (summaryview->folder_item->stype == F_OUTBOX) ||
1250             (summaryview->folder_item->stype == F_QUEUE))
1251                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1252
1253         menu_set_sensitive(ifactory, "/Save as...", sens);
1254         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1255
1256         menu_set_sensitive(ifactory, "/Mark", TRUE);
1257
1258         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1259         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1260
1261         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1262         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1263         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1264         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1265
1266         menu_set_sensitive(ifactory, "/Select all", TRUE);
1267
1268         if (summaryview->folder_item->folder->account)
1269                 sens = summaryview->folder_item->folder->account->protocol
1270                         == A_NNTP;
1271         else
1272                 sens = FALSE;
1273         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1274 }
1275
1276 void summary_select_next_unread(SummaryView *summaryview)
1277 {
1278         GtkCTreeNode *node;
1279         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1280
1281         node = summary_find_next_unread_msg(summaryview,
1282                                             summaryview->selected);
1283
1284         if (node) {
1285                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1286                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1287
1288                 /* BUGFIX: select next unread message 
1289                  * REVERT: causes incorrect folder stats
1290                  * gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0.0); 
1291                  */
1292
1293                 if (summaryview->displayed == node)
1294                         summaryview->displayed = NULL;
1295                 summary_display_msg(summaryview, node, FALSE);
1296         } else {
1297                 AlertValue val;
1298
1299                 val = alertpanel(_("No unread message"),
1300                                  _("No unread message found. Go to next folder?"),
1301                                  _("Yes"), _("No"), NULL);
1302                 if (val == G_ALERTDEFAULT) {
1303                         if (gtk_signal_n_emissions_by_name
1304                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1305                                         gtk_signal_emit_stop_by_name
1306                                                 (GTK_OBJECT(ctree),
1307                                                  "key_press_event");
1308                         folderview_select_next_unread(summaryview->folderview);
1309                 }
1310         }
1311 }
1312
1313 void summary_select_next_marked(SummaryView *summaryview)
1314 {
1315         GtkCTreeNode *node;
1316         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1317
1318         node = summary_find_next_marked_msg(summaryview,
1319                                             summaryview->selected);
1320
1321         if (!node) {
1322                 AlertValue val;
1323
1324                 val = alertpanel(_("No more marked messages"),
1325                                  _("No marked message found. "
1326                                    "Search from the beginning?"),
1327                                  _("Yes"), _("No"), NULL);
1328                 if (val != G_ALERTDEFAULT) return;
1329                 node = summary_find_next_marked_msg(summaryview,
1330                                                     NULL);
1331         }
1332         if (!node) {
1333                 alertpanel_notice(_("No marked messages."));
1334         } else {
1335                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1336                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1337                 if (summaryview->displayed == node)
1338                         summaryview->displayed = NULL;
1339                 summary_display_msg(summaryview, node, FALSE);
1340         }
1341 }
1342
1343 void summary_select_prev_marked(SummaryView *summaryview)
1344 {
1345         GtkCTreeNode *node;
1346         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1347
1348         node = summary_find_prev_marked_msg(summaryview,
1349                                             summaryview->selected);
1350
1351         if (!node) {
1352                 AlertValue val;
1353
1354                 val = alertpanel(_("No more marked messages"),
1355                                  _("No marked message found. "
1356                                    "Search from the end?"),
1357                                  _("Yes"), _("No"), NULL);
1358                 if (val != G_ALERTDEFAULT) return;
1359                 node = summary_find_prev_marked_msg(summaryview,
1360                                                     NULL);
1361         }
1362         if (!node) {
1363                 alertpanel_notice(_("No marked messages."));
1364         } else {
1365                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1366                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1367                 if (summaryview->displayed == node)
1368                         summaryview->displayed = NULL;
1369                 summary_display_msg(summaryview, node, FALSE);
1370         }
1371 }
1372
1373 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1374 {
1375         GtkCTreeNode *node;
1376         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1377
1378         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1379
1380         if (node) {
1381                 GTK_EVENTS_FLUSH();
1382                 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1383                 gtk_widget_grab_focus(GTK_WIDGET(ctree));
1384                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1385                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1386                 if (summaryview->msg_is_toggled_on) {
1387                         if (summaryview->displayed == node)
1388                                 summaryview->displayed = NULL;
1389                         summary_display_msg(summaryview, node, FALSE);
1390                 }
1391         }
1392 }
1393
1394 guint summary_get_current_msgnum(SummaryView *summaryview)
1395 {
1396         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1397         MsgInfo *msginfo;
1398
1399         if (!summaryview->selected)
1400                 return 0;
1401         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
1402         return msginfo->msgnum;
1403 }
1404
1405 static GtkCTreeNode *summary_find_next_unread_msg(SummaryView *summaryview,
1406                                                   GtkCTreeNode *current_node)
1407 {
1408         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1409         GtkCTreeNode *node;
1410         MsgInfo *msginfo;
1411
1412         if (current_node)
1413                 node = current_node;
1414                 /*node = GTK_CTREE_NODE_NEXT(current_node);*/
1415         else
1416                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1417
1418         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1419                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1420                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) break;
1421         }
1422
1423         return node;
1424 }
1425
1426 static GtkCTreeNode *summary_find_next_marked_msg(SummaryView *summaryview,
1427                                                   GtkCTreeNode *current_node)
1428 {
1429         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1430         GtkCTreeNode *node;
1431         MsgInfo *msginfo;
1432
1433         if (current_node)
1434                 node = GTK_CTREE_NODE_NEXT(current_node);
1435         else
1436                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1437
1438         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1439                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1440                 if (MSG_IS_MARKED(msginfo->flags)) break;
1441         }
1442
1443         return node;
1444 }
1445
1446 static GtkCTreeNode *summary_find_prev_marked_msg(SummaryView *summaryview,
1447                                                   GtkCTreeNode *current_node)
1448 {
1449         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1450         GtkCTreeNode *node;
1451         MsgInfo *msginfo;
1452
1453         if (current_node)
1454                 node = GTK_CTREE_NODE_PREV(current_node);
1455         else
1456                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1457
1458         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1459                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1460                 if (MSG_IS_MARKED(msginfo->flags)) break;
1461         }
1462
1463         return node;
1464 }
1465
1466 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1467                                                 guint msgnum)
1468 {
1469         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1470         GtkCTreeNode *node;
1471         MsgInfo *msginfo;
1472
1473         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1474
1475         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1476                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1477                 if (msginfo->msgnum == msgnum) break;
1478         }
1479
1480         return node;
1481 }
1482
1483 #if 0
1484 static GtkCTreeNode *summary_find_prev_unread_msg(SummaryView *summaryview,
1485                                                   GtkCTreeNode *current_node)
1486 {
1487         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1488         GtkCTreeNode *node;
1489         MsgInfo *msginfo;
1490
1491         if (current_node)
1492                 node = current_node;
1493                 /*node = GTK_CTREE_NODE_PREV(current_node);*/
1494         else
1495                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
1496
1497         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1498                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1499                 if (MSG_IS_UNREAD(msginfo->flags)) break;
1500         }
1501
1502         return node;
1503 }
1504 #endif
1505
1506 static guint attract_hash_func(gconstpointer key)
1507 {
1508         gchar *str;
1509         gchar *p;
1510         guint h;
1511
1512         Xstrdup_a(str, (const gchar *)key, return 0);
1513         trim_subject(str);
1514
1515         p = str;
1516         h = *p;
1517
1518         if (h) {
1519                 for (p += 1; *p != '\0'; p++)
1520                         h = (h << 5) - h + *p;
1521         }
1522
1523         return h;
1524 }
1525
1526 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1527 {
1528         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1529 }
1530
1531 void summary_attract_by_subject(SummaryView *summaryview)
1532 {
1533         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1534         GtkCList *clist = GTK_CLIST(ctree);
1535         GtkCTreeNode *src_node;
1536         GtkCTreeNode *dst_node, *sibling;
1537         GtkCTreeNode *tmp;
1538         MsgInfo *src_msginfo, *dst_msginfo;
1539         GHashTable *subject_table;
1540
1541         debug_print(_("Attracting messages by subject..."));
1542         STATUSBAR_PUSH(summaryview->mainwin,
1543                        _("Attracting messages by subject..."));
1544
1545         main_window_cursor_wait(summaryview->mainwin);
1546         gtk_clist_freeze(clist);
1547
1548         subject_table = g_hash_table_new(attract_hash_func,
1549                                          attract_compare_func);
1550
1551         for (src_node = GTK_CTREE_NODE(clist->row_list);
1552              src_node != NULL;
1553              src_node = tmp) {
1554                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1555                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1556                 if (!src_msginfo) continue;
1557                 if (!src_msginfo->subject) continue;
1558
1559                 /* find attracting node */
1560                 dst_node = g_hash_table_lookup(subject_table,
1561                                                src_msginfo->subject);
1562
1563                 if (dst_node) {
1564                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1565
1566                         /* if the time difference is more than 20 days,
1567                            don't attract */
1568                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1569                             > 60 * 60 * 24 * 20)
1570                                 continue;
1571
1572                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1573                         if (src_node != sibling)
1574                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1575                 }
1576
1577                 g_hash_table_insert(subject_table,
1578                                     src_msginfo->subject, src_node);
1579         }
1580
1581         g_hash_table_destroy(subject_table);
1582
1583         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1584
1585         gtk_clist_thaw(clist);
1586
1587         debug_print(_("done.\n"));
1588         STATUSBAR_POP(summaryview->mainwin);
1589
1590         main_window_cursor_normal(summaryview->mainwin);
1591 }
1592
1593 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1594                                       gpointer data)
1595 {
1596         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1597
1598         if (msginfo)
1599                 procmsg_msginfo_free(msginfo);
1600 }
1601
1602 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1603                                    gpointer data)
1604 {
1605         SummaryView *summaryview = data;
1606         MsgInfo *msginfo;
1607
1608         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1609
1610         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1611                 summaryview->newmsgs++;
1612         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1613                 summaryview->unread++;
1614         if (MSG_IS_DELETED(msginfo->flags))
1615                 summaryview->deleted++;
1616
1617         summaryview->messages++;
1618         summaryview->total_size += msginfo->size;
1619
1620         summary_set_row_marks(summaryview, node);
1621 }
1622
1623 static void summary_update_status(SummaryView *summaryview)
1624 {
1625         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1626         GtkCTreeNode *node;
1627         MsgInfo *msginfo;
1628
1629         summaryview->newmsgs = summaryview->unread =
1630         summaryview->messages = summaryview->total_size =
1631         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1632
1633         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1634              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
1635                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1636
1637                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1638                         summaryview->newmsgs++;
1639                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1640                         summaryview->unread++;
1641                 if (MSG_IS_DELETED(msginfo->flags))
1642                         summaryview->deleted++;
1643                 if (MSG_IS_MOVE(msginfo->flags))
1644                         summaryview->moved++;
1645                 if (MSG_IS_COPY(msginfo->flags))
1646                         summaryview->copied++;
1647                 summaryview->messages++;
1648                 summaryview->total_size += msginfo->size;
1649         }
1650 }
1651
1652 static void summary_status_show(SummaryView *summaryview)
1653 {
1654         gchar *str;
1655         gchar *del, *mv, *cp;
1656         gchar *sel;
1657         gchar *spc;
1658         GList *rowlist, *cur;
1659         guint n_selected = 0;
1660         off_t sel_size = 0;
1661         MsgInfo *msginfo;
1662
1663         if (!summaryview->folder_item) {
1664                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1665                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1666                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1667                 return;
1668         }
1669
1670         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1671         for (cur = rowlist; cur != NULL; cur = cur->next) {
1672                 msginfo = gtk_ctree_node_get_row_data
1673                         (GTK_CTREE(summaryview->ctree),
1674                          GTK_CTREE_NODE(cur->data));
1675                 sel_size += msginfo->size;
1676                 n_selected++;
1677         }
1678
1679         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1680                       summaryview->folder_item &&
1681                       summaryview->folder_item->folder->type == F_NEWS
1682                       ? g_basename(summaryview->folder_item->path)
1683                       : summaryview->folder_item->path);
1684
1685         if (summaryview->deleted)
1686                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1687         else
1688                 del = g_strdup("");
1689         if (summaryview->moved)
1690                 mv = g_strdup_printf(_("%s%d moved"),
1691                                      summaryview->deleted ? _(", ") : "",
1692                                      summaryview->moved);
1693         else
1694                 mv = g_strdup("");
1695         if (summaryview->copied)
1696                 cp = g_strdup_printf(_("%s%d copied"),
1697                                      summaryview->deleted ||
1698                                      summaryview->moved ? _(", ") : "",
1699                                      summaryview->copied);
1700         else
1701                 cp = g_strdup("");
1702
1703         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1704                 spc = "    ";
1705         else
1706                 spc = "";
1707
1708         if (n_selected)
1709                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1710         else
1711                 sel = g_strdup("");
1712         str = g_strconcat(n_selected ? itos(n_selected) : "",
1713                           n_selected ? _(" item(s) selected") : "",
1714                           sel, spc, del, mv, cp, NULL);
1715         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1716         g_free(str);
1717         g_free(sel);
1718         g_free(del);
1719         g_free(mv);
1720         g_free(cp);
1721
1722         if (summaryview->folder_item &&
1723             summaryview->folder_item->folder->type == F_MH) {
1724                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1725                                       summaryview->newmsgs,
1726                                       summaryview->unread,
1727                                       summaryview->messages,
1728                                       to_human_readable(summaryview->total_size));
1729         } else {
1730                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1731                                       summaryview->newmsgs,
1732                                       summaryview->unread,
1733                                       summaryview->messages);
1734         }
1735         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1736         g_free(str);
1737
1738         folderview_update_msg_num(summaryview->folderview,
1739                                   summaryview->folderview->opened,
1740                                   summaryview->newmsgs,
1741                                   summaryview->unread,
1742                                   summaryview->messages);
1743 }
1744
1745 void summary_sort(SummaryView *summaryview, SummarySortType type)
1746 {
1747         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1748         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1749         GtkCListCompareFunc cmp_func;
1750
1751         if (!summaryview->folder_item)
1752                 return;
1753
1754         switch (type) {
1755         case SORT_BY_NUMBER:
1756                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1757                 break;
1758         case SORT_BY_SIZE:
1759                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1760                 break;
1761         case SORT_BY_DATE:
1762                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1763                 break;
1764         case SORT_BY_FROM:
1765                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1766                 break;
1767         case SORT_BY_SUBJECT:
1768                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1769                 break;
1770         case SORT_BY_SCORE:
1771                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1772                 break;
1773         case SORT_BY_LABEL:
1774                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
1775                 break;
1776         default:
1777                 return;
1778         }
1779
1780         debug_print(_("Sorting summary..."));
1781         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1782
1783         main_window_cursor_wait(summaryview->mainwin);
1784
1785         gtk_clist_set_compare_func(clist, cmp_func);
1786
1787         /* toggle sort type if the same column is selected */
1788         if (summaryview->sort_mode == type)
1789                 summaryview->sort_type =
1790                         summaryview->sort_type == GTK_SORT_ASCENDING
1791                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1792         else
1793                 summaryview->sort_type = GTK_SORT_ASCENDING;
1794         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1795         summaryview->sort_mode = type;
1796
1797         gtk_ctree_sort_node(ctree, NULL);
1798
1799         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1800         /*gtkut_ctree_set_focus_row(ctree, summaryview->selected);*/
1801
1802         prefs_folder_item_set_config(summaryview->folder_item,
1803                                      summaryview->sort_type,
1804                                      summaryview->sort_mode);
1805         prefs_folder_item_save_config(summaryview->folder_item);
1806
1807         debug_print(_("done.\n"));
1808         STATUSBAR_POP(summaryview->mainwin);
1809
1810         main_window_cursor_normal(summaryview->mainwin);
1811 }
1812
1813 static GtkCTreeNode * subject_table_lookup(GHashTable *subject_table,
1814                                            gchar * subject)
1815 {
1816         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1817                 return g_hash_table_lookup(subject_table, subject + 4);
1818         else
1819                 return g_hash_table_lookup(subject_table, subject);
1820 }
1821
1822 static void subject_table_insert(GHashTable *subject_table, gchar * subject,
1823                                  GtkCTreeNode * node)
1824 {
1825         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1826                 g_hash_table_insert(subject_table, subject + 4, node);
1827         else
1828                 g_hash_table_insert(subject_table, subject, node);
1829 }
1830
1831 static void summary_set_ctree_from_list(SummaryView *summaryview,
1832                                         GSList *mlist)
1833 {
1834         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1835         MsgInfo *msginfo;
1836         MsgInfo *parentinfo;
1837         MsgInfo *cur_msginfo;
1838         GtkCTreeNode *node, *parent;
1839         gchar *text[N_SUMMARY_COLS];
1840         GHashTable *msgid_table;
1841         GHashTable *subject_table;
1842         GSList * cur;
1843         GtkCTreeNode *cur_parent;
1844
1845         if (!mlist) return;
1846
1847         debug_print(_("\tSetting summary from message data..."));
1848         STATUSBAR_PUSH(summaryview->mainwin,
1849                        _("Setting summary from message data..."));
1850
1851         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1852         summaryview->msgid_table = msgid_table;
1853         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
1854         summaryview->subject_table = subject_table;
1855
1856         if (prefs_common.use_addr_book)
1857                 start_address_completion();
1858         
1859         main_window_set_thread_option(summaryview->mainwin);
1860
1861         for (cur = mlist ; cur != NULL; cur = cur->next) {
1862                 msginfo = (MsgInfo *)cur->data;
1863                 msginfo->threadscore = msginfo->score;
1864         }
1865
1866         if (global_scoring || summaryview->folder_item->prefs->scoring) {
1867                 summaryview->important_score = prefs_common.important_score;
1868                 if (summaryview->folder_item->prefs->important_score >
1869                     summaryview->important_score)
1870                         summaryview->important_score =
1871                                 summaryview->folder_item->prefs->important_score;
1872         }
1873         
1874         /*      if (prefs_common.enable_thread) { */
1875         if (summaryview->folder_item->prefs->enable_thread) {
1876                 for (; mlist != NULL; mlist = mlist->next) {
1877                         msginfo = (MsgInfo *)mlist->data;
1878                         parent = NULL;
1879
1880                         summary_set_header(text, msginfo);
1881
1882                         /* search parent node for threading */
1883                         if (msginfo->inreplyto && *msginfo->inreplyto) {
1884                                 parent = g_hash_table_lookup
1885                                         (msgid_table, msginfo->inreplyto);
1886                         }
1887                         if (parent == NULL && msginfo->subject &&
1888                             g_strncasecmp(msginfo->subject, "Re: ", 4) == 0) {
1889                                 parent = subject_table_lookup
1890                                         (subject_table, msginfo->subject);
1891                         }
1892                         if(parent) {
1893                                 parentinfo = gtk_ctree_node_get_row_data(ctree, parent);
1894                                 if(parentinfo && MSG_IS_IGNORE_THREAD(parentinfo->flags)) {
1895 /*
1896                                         if (MSG_IS_NEW(msginfo->flags))
1897                                                 summaryview->newmsgs--;
1898                                         if (MSG_IS_UNREAD(msginfo->flags))
1899                                                 summaryview->unread--;
1900 */
1901                                         MSG_SET_FLAGS(msginfo->flags, MSG_IGNORE_THREAD);
1902                                 }
1903                         }
1904
1905                         node = gtk_ctree_insert_node
1906                                 (ctree, parent, NULL, text, 2,
1907                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1908                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1909
1910                         summary_set_marks_func(ctree, node, summaryview);
1911                         
1912                         if ( MSG_GET_LABEL(msginfo->flags) )
1913                           summary_set_label_color(ctree, node, (msginfo->flags & MSG_LABEL));
1914
1915                         /* preserve previous node if the message is
1916                            duplicated */
1917                         if (msginfo->msgid && *msginfo->msgid &&
1918                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1919                             == NULL)
1920                                 g_hash_table_insert(msgid_table,
1921                                                     msginfo->msgid, node);
1922                         if (msginfo->subject &&
1923                             subject_table_lookup(subject_table,
1924                                                  msginfo->subject) == NULL) {
1925                                 subject_table_insert(subject_table,
1926                                                      msginfo->subject, node);
1927                         }
1928
1929                         cur_parent = parent;
1930                         cur_msginfo = msginfo;
1931                         while (cur_parent != NULL) {
1932                                 parentinfo = gtk_ctree_node_get_row_data(ctree, cur_parent);
1933
1934                                 if (!parentinfo)
1935                                         break;
1936                                 
1937                                 if (parentinfo->threadscore <
1938                                     cur_msginfo->threadscore) {
1939                                         gchar * s;
1940                                         parentinfo->threadscore =
1941                                                 cur_msginfo->threadscore;
1942 #if 0
1943                                         s = itos(parentinfo->threadscore);
1944                                         gtk_ctree_node_set_text(ctree, cur_parent, S_COL_SCORE, s);
1945 #endif
1946                                 }
1947                                 else break;
1948                                 
1949                                 cur_msginfo = parentinfo;
1950                                 if (cur_msginfo->inreplyto &&
1951                                     *cur_msginfo->inreplyto) {
1952                                         cur_parent = g_hash_table_lookup(msgid_table, cur_msginfo->inreplyto);
1953                                 }
1954                                 if (cur_parent == NULL &&
1955                                     cur_msginfo->subject &&
1956                                     g_strncasecmp(cur_msginfo->subject,
1957                                                   "Re: ", 4) == 0) {
1958                                         cur_parent = subject_table_lookup(subject_table, cur_msginfo->subject);
1959                                 }
1960                         }
1961                 }
1962
1963                 /* complete the thread */
1964                 summary_thread_build(summaryview);
1965         } else {
1966                 for (; mlist != NULL; mlist = mlist->next) {
1967                         msginfo = (MsgInfo *)mlist->data;
1968
1969                         summary_set_header(text, msginfo);
1970
1971                         node = gtk_ctree_insert_node
1972                                 (ctree, NULL, NULL, text, 2,
1973                                  NULL, NULL, NULL, NULL, FALSE, TRUE);
1974                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1975                         summary_set_marks_func(ctree, node, summaryview);
1976
1977                         if ( MSG_GET_LABEL(msginfo->flags) )
1978                           summary_set_label_color(ctree, node, (msginfo->flags & MSG_LABEL));
1979
1980                         if (msginfo->msgid && *msginfo->msgid &&
1981                             g_hash_table_lookup(msgid_table, msginfo->msgid)
1982                             == NULL)
1983                                 g_hash_table_insert(msgid_table,
1984                                                     msginfo->msgid, node);
1985
1986                         if (msginfo->subject &&
1987                             subject_table_lookup(subject_table,
1988                                                  msginfo->subject) == NULL)
1989                                 subject_table_insert(subject_table,
1990                                                      msginfo->subject, node);
1991                 }
1992         }
1993
1994         if (prefs_common.enable_hscrollbar) {
1995                 gint optimal_width;
1996
1997                 optimal_width = gtk_clist_optimal_column_width
1998                         (GTK_CLIST(ctree), S_COL_SUBJECT);
1999                 gtk_clist_set_column_width(GTK_CLIST(ctree), S_COL_SUBJECT,
2000                                            optimal_width);
2001         }
2002
2003         if (prefs_common.use_addr_book)
2004                 end_address_completion();
2005
2006         debug_print(_("done.\n"));
2007         STATUSBAR_POP(summaryview->mainwin);
2008         if (debug_mode) {
2009                 debug_print("\tmsgid hash table size = %d\n",
2010                             g_hash_table_size(msgid_table));
2011                 debug_print("\tsubject hash table size = %d\n",
2012                             g_hash_table_size(subject_table));
2013         }
2014 }
2015
2016 struct wcachefp
2017 {
2018         FILE *cache_fp;
2019         FILE *mark_fp;
2020 };
2021
2022 gint summary_write_cache(SummaryView *summaryview)
2023 {
2024         struct wcachefp fps;
2025         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2026         gint ver = CACHE_VERSION;
2027         gchar *buf;
2028         gchar *cachefile, *markfile;
2029         GSList * cur;
2030
2031         if (!summaryview->folder_item || !summaryview->folder_item->path)
2032                 return -1;
2033
2034         if (summaryview->folder_item->folder->update_mark != NULL)
2035                 summaryview->folder_item->folder->update_mark(summaryview->folder_item->folder, summaryview->folder_item);
2036
2037         cachefile = folder_item_get_cache_file(summaryview->folder_item);
2038         g_return_val_if_fail(cachefile != NULL, -1);
2039         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
2040                 FILE_OP_ERROR(cachefile, "fopen");
2041                 g_free(cachefile);
2042                 return -1;
2043         }
2044         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
2045                 FILE_OP_ERROR(cachefile, "chmod");
2046         g_free(cachefile);
2047
2048         markfile = folder_item_get_mark_file(summaryview->folder_item);
2049         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
2050                 FILE_OP_ERROR(markfile, "fopen");
2051                 fclose(fps.cache_fp);
2052                 g_free(markfile);
2053                 return -1;
2054         }
2055         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
2056                 FILE_OP_ERROR(markfile, "chmod");
2057         g_free(markfile);
2058
2059         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
2060                               summaryview->folder_item->path);
2061         debug_print(buf);
2062         STATUSBAR_PUSH(summaryview->mainwin, buf);
2063         g_free(buf);
2064
2065         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
2066         ver = MARK_VERSION;
2067         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
2068
2069         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
2070
2071         for(cur = summaryview->killed_messages ; cur != NULL ;
2072             cur = g_slist_next(cur)) {
2073                 MsgInfo *msginfo = (MsgInfo *) cur->data;
2074
2075                 procmsg_write_cache(msginfo, fps.cache_fp);
2076                 procmsg_write_flags(msginfo, fps.mark_fp);
2077         }
2078
2079         fclose(fps.cache_fp);
2080         fclose(fps.mark_fp);
2081
2082         debug_print(_("done.\n"));
2083         STATUSBAR_POP(summaryview->mainwin);
2084
2085         return 0;
2086 }
2087
2088 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
2089                                      gpointer data)
2090 {
2091         struct wcachefp *fps = data;
2092         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
2093
2094         if (msginfo == NULL) return;
2095
2096         procmsg_write_cache(msginfo, fps->cache_fp);
2097         procmsg_write_flags(msginfo, fps->mark_fp);
2098 }
2099
2100 static void summary_set_header(gchar *text[], MsgInfo *msginfo)
2101 {
2102         static gchar date_modified[80];
2103         static gchar *to = NULL;
2104         static gchar *from_name = NULL;
2105         static gchar col_number[11];
2106         static gchar col_score[11];
2107
2108         text[S_COL_MARK]   = NULL;
2109         text[S_COL_UNREAD] = NULL;
2110         text[S_COL_MIME]   = NULL;
2111         text[S_COL_NUMBER] = itos_buf(col_number, msginfo->msgnum);
2112         text[S_COL_SIZE]   = to_human_readable(msginfo->size);
2113 #if 0
2114         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->threadscore);
2115 #else
2116         text[S_COL_SCORE]  = itos_buf(col_score, msginfo->score);
2117 #endif
2118
2119         if (msginfo->date_t) {
2120                 procheader_date_get_localtime(date_modified,
2121                                               sizeof(date_modified),
2122                                               msginfo->date_t);
2123                 text[S_COL_DATE] = date_modified;
2124         } else if (msginfo->date)
2125                 text[S_COL_DATE] = msginfo->date;
2126         else
2127                 text[S_COL_DATE] = _("(No Date)");
2128
2129         text[S_COL_FROM] = msginfo->fromname ? msginfo->fromname :
2130                 _("(No From)");
2131         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2132             !MSG_IS_NEWS(msginfo->flags)) {
2133                 gchar *from;
2134
2135                 Xalloca(from, strlen(msginfo->from) + 1, return);
2136                 strcpy(from, msginfo->from);
2137                 extract_address(from);
2138                 if (account_find_mail_from_address(from)) {
2139                         g_free(to);
2140                         to = g_strconcat("-->", msginfo->to, NULL);
2141                         text[S_COL_FROM] = to;
2142                 }
2143         }
2144
2145         if ((text[S_COL_FROM] != to) && prefs_common.use_addr_book &&
2146             msginfo->from) {
2147                 gint count;
2148                 gchar *from;
2149   
2150                 Xalloca(from, strlen(msginfo->from) + 1, return);
2151                 strcpy(from, msginfo->from);
2152                 extract_address(from);
2153                 count = complete_address(from);
2154                 if (count > 1) {
2155                         g_free(from_name);
2156                         from = get_complete_address(1);
2157                         from_name = procheader_get_fromname(from);
2158                         g_free(from);
2159                         text[S_COL_FROM] = from_name;
2160                 }
2161         }
2162
2163         text[S_COL_SUBJECT] = msginfo->subject ? msginfo->subject :
2164                 _("(No Subject)");
2165 }
2166
2167 #define CHANGE_FLAGS(msginfo) \
2168 { \
2169 if (msginfo->folder->folder->change_flags != NULL) \
2170 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2171                                       msginfo->folder, \
2172                                       msginfo); \
2173 }
2174
2175 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
2176                                 gboolean new_window)
2177 {
2178         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2179         MsgInfo *msginfo;
2180         gchar *filename;
2181         static gboolean lock = FALSE;
2182
2183         if (!new_window && summaryview->displayed == row) return;
2184         g_return_if_fail(row != NULL);
2185
2186         if (lock) return;
2187         lock = TRUE;
2188
2189         STATUSBAR_POP(summaryview->mainwin);
2190
2191         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2192
2193         filename = procmsg_get_message_file(msginfo);
2194         if (!filename) {
2195                 lock = FALSE;
2196                 return;
2197         }
2198         g_free(filename);
2199
2200         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2201                 summaryview->newmsgs--;
2202         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2203                 summaryview->unread--;
2204         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2205                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2206
2207                 CHANGE_FLAGS(msginfo);
2208
2209                 summary_set_row_marks(summaryview, row);
2210                 gtk_clist_thaw(GTK_CLIST(ctree));
2211                 summary_status_show(summaryview);
2212         }
2213
2214         if (new_window) {
2215                 MessageView *msgview;
2216
2217                 msgview = messageview_create_with_new_window();
2218                 messageview_show(msgview, msginfo);
2219         } else {
2220                 MessageView *msgview;
2221
2222                 msgview = summaryview->messageview;
2223
2224                 summaryview->displayed = row;
2225                 if (!summaryview->msg_is_toggled_on)
2226                         summary_toggle_view(summaryview);
2227                 messageview_show(msgview, msginfo);
2228                 if (msgview->type == MVIEW_TEXT ||
2229                     (msgview->type == MVIEW_MIME &&
2230                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
2231                         gtk_widget_grab_focus(summaryview->ctree);
2232                 GTK_EVENTS_FLUSH();
2233                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2234         }
2235
2236         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
2237                 header_window_show(summaryview->headerwin, msginfo);
2238
2239         lock = FALSE;
2240 }
2241
2242 void summary_redisplay_msg(SummaryView *summaryview)
2243 {
2244         GtkCTreeNode *node;
2245
2246         if (summaryview->displayed) {
2247                 node = summaryview->displayed;
2248                 summaryview->displayed = NULL;
2249                 summary_display_msg(summaryview, node, FALSE);
2250         }
2251 }
2252
2253 void summary_open_msg(SummaryView *summaryview)
2254 {
2255         if (!summaryview->selected) return;
2256
2257         summary_display_msg(summaryview, summaryview->selected, TRUE);
2258 }
2259
2260 void summary_view_source(SummaryView * summaryview)
2261 {
2262         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2263         MsgInfo *msginfo;
2264         SourceWindow *srcwin;
2265
2266         if (!summaryview->selected) return;
2267
2268         srcwin = source_window_create();
2269         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2270         source_window_show_msg(srcwin, msginfo);
2271         source_window_show(srcwin);
2272 }
2273
2274 void summary_reedit(SummaryView *summaryview)
2275 {
2276         MsgInfo *msginfo;
2277
2278         if (!summaryview->selected) return;
2279         if (!summaryview->folder_item ||
2280             (summaryview->folder_item->stype != F_DRAFT &&
2281              summaryview->folder_item->stype != F_OUTBOX &&
2282              summaryview->folder_item->stype != F_QUEUE)) return;
2283
2284         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2285                                               summaryview->selected);
2286         if (!msginfo) return;
2287
2288         compose_reedit(msginfo);
2289 }
2290
2291 void summary_step(SummaryView *summaryview, GtkScrollType type)
2292 {
2293         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2294
2295         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2296                                 type, 0.0);
2297
2298         if (summaryview->msg_is_toggled_on)
2299                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2300 }
2301
2302 static void summary_toggle_view(SummaryView *summaryview)
2303 {
2304         MainWindow *mainwin = summaryview->mainwin;
2305         union CompositeWin *cwin = &mainwin->win;
2306         GtkWidget *vpaned = NULL;
2307         GtkWidget *container = NULL;
2308
2309         switch (mainwin->type) {
2310         case SEPARATE_NONE:
2311                 vpaned = cwin->sep_none.vpaned;
2312                 container = cwin->sep_none.hpaned;
2313                 break;
2314         case SEPARATE_FOLDER:
2315                 vpaned = cwin->sep_folder.vpaned;
2316                 container = mainwin->vbox_body;
2317                 break;
2318         case SEPARATE_MESSAGE:
2319         case SEPARATE_BOTH:
2320                 return;
2321         }
2322
2323         if (vpaned->parent != NULL) {
2324                 summaryview->msg_is_toggled_on = FALSE;
2325                 summaryview->displayed = NULL;
2326                 gtk_widget_ref(vpaned);
2327                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
2328                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
2329                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2330                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2331         } else {
2332                 summaryview->msg_is_toggled_on = TRUE;
2333                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
2334                 gtk_container_add(GTK_CONTAINER(container), vpaned);
2335                 gtk_widget_unref(vpaned);
2336                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2337                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2338         }
2339
2340         gtk_widget_grab_focus(summaryview->ctree);
2341 }
2342
2343 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2344 {
2345         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2346         MsgInfo *msginfo;
2347         MsgFlags flags;
2348
2349         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2350         if (!msginfo) return;
2351
2352         flags = msginfo->flags;
2353
2354         gtk_ctree_node_set_foreground(ctree, row, NULL);
2355
2356         /* set new/unread column */
2357         if (MSG_IS_IGNORE_THREAD(flags)) {
2358                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2359                                           ignorethreadxpm, ignorethreadxpmmask);
2360         } else if (MSG_IS_NEW(flags)) {
2361                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2362                                           newxpm, newxpmmask);
2363         } else if (MSG_IS_UNREAD(flags)) {
2364                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2365                                           unreadxpm, unreadxpmmask);
2366         } else if (MSG_IS_REPLIED(flags)) {
2367                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2368                                           repliedxpm, repliedxpmmask);
2369         } else if (MSG_IS_FORWARDED(flags)) {
2370                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2371                                           forwardedxpm, forwardedxpmmask);
2372         } else {
2373                 gtk_ctree_node_set_text(ctree, row, S_COL_UNREAD, NULL);
2374         }
2375
2376         /* set mark column */
2377         if (MSG_IS_DELETED(flags)) {
2378                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2379                                           deletedxpm, deletedxpmmask);
2380                 gtk_ctree_node_set_foreground(ctree, row,
2381                                               &summaryview->color_dim);
2382         } else if (MSG_IS_MARKED(flags)) {
2383                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MARK,
2384                                           markxpm, markxpmmask);
2385         } else if (MSG_IS_MOVE(flags)) {
2386                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "o");
2387                 gtk_ctree_node_set_foreground(ctree, row,
2388                                               &summaryview->color_marked);
2389         } else if (MSG_IS_COPY(flags)) {
2390                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "O");
2391                 gtk_ctree_node_set_foreground(ctree, row,
2392                                               &summaryview->color_marked);
2393         }
2394         else if ((global_scoring ||
2395                   summaryview->folder_item->prefs->scoring) &&
2396                  (msginfo->score >= summaryview->important_score) &&
2397                  ((msginfo->flags &
2398                    (MSG_MARKED | MSG_MOVE | MSG_COPY)) == 0)) {
2399                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2400                 gtk_ctree_node_set_foreground(ctree, row,
2401                                               &summaryview->color_important);
2402         } else {
2403                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, NULL);
2404         }
2405
2406         if (MSG_IS_MIME(flags)) {
2407                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_MIME,
2408                                           clipxpm, clipxpmmask);
2409         } else {
2410                 gtk_ctree_node_set_text(ctree, row, S_COL_MIME, NULL);
2411         }
2412 }
2413
2414 void summary_set_marks_selected(SummaryView *summaryview)
2415 {
2416         summary_set_row_marks(summaryview, summaryview->selected);
2417 }
2418
2419 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2420 {
2421         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2422         MsgInfo *msginfo;
2423
2424         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2425         msginfo->to_folder = NULL;
2426         if (MSG_IS_DELETED(msginfo->flags))
2427                 summaryview->deleted--;
2428         if (MSG_IS_MOVE(msginfo->flags))
2429                 summaryview->moved--;
2430         if (MSG_IS_COPY(msginfo->flags))
2431                 summaryview->copied--;
2432         MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED | MSG_MOVE | MSG_COPY);
2433         MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
2434
2435         CHANGE_FLAGS(msginfo);
2436
2437         summary_set_row_marks(summaryview, row);
2438         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
2439 }
2440
2441 void summary_mark(SummaryView *summaryview)
2442 {
2443         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2444         GList *cur;
2445
2446         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2447                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2448
2449         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2450         summary_status_show(summaryview);
2451 }
2452
2453 static void summary_mark_row_as_read(SummaryView *summaryview,
2454                                      GtkCTreeNode *row)
2455 {
2456         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2457         MsgInfo *msginfo;
2458
2459         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2460         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2461                 summaryview->newmsgs--;
2462         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2463                 summaryview->unread--;
2464         if (MSG_IS_NEW(msginfo->flags) ||
2465             MSG_IS_UNREAD(msginfo->flags)) {
2466                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2467
2468                 CHANGE_FLAGS(msginfo);
2469                 
2470                 summary_set_row_marks(summaryview, row);
2471                 debug_print(_("Message %d is marked as read\n"),
2472                             msginfo->msgnum);
2473         }
2474 }
2475
2476 void summary_mark_as_read(SummaryView *summaryview)
2477 {
2478         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2479         GList *cur;
2480
2481         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2482                 summary_mark_row_as_read(summaryview,
2483                                          GTK_CTREE_NODE(cur->data));
2484
2485         summary_status_show(summaryview);
2486 }
2487
2488 static void summary_mark_row_as_unread(SummaryView *summaryview,
2489                                        GtkCTreeNode *row)
2490 {
2491         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2492         MsgInfo *msginfo;
2493
2494         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2495         if (MSG_IS_DELETED(msginfo->flags)) {
2496                 msginfo->to_folder = NULL;
2497                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
2498                 summaryview->deleted--;
2499         }
2500         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2501         if (!MSG_IS_UNREAD(msginfo->flags)) {
2502                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
2503                 gtk_ctree_node_set_pixmap(ctree, row, S_COL_UNREAD,
2504                                           unreadxpm, unreadxpmmask);
2505                 summaryview->unread++;
2506                 debug_print(_("Message %d is marked as unread\n"),
2507                             msginfo->msgnum);
2508         }
2509
2510         CHANGE_FLAGS(msginfo);
2511
2512         summary_set_row_marks(summaryview, row);
2513 }
2514
2515 void summary_mark_as_unread(SummaryView *summaryview)
2516 {
2517         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2518         GList *cur;
2519
2520         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2521                 summary_mark_row_as_unread(summaryview,
2522                                            GTK_CTREE_NODE(cur->data));
2523
2524         summary_status_show(summaryview);
2525 }
2526
2527 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2528 {
2529         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2530         MsgInfo *msginfo;
2531
2532         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2533
2534         if (MSG_IS_DELETED(msginfo->flags)) return;
2535
2536         msginfo->to_folder = NULL;
2537         if (MSG_IS_MOVE(msginfo->flags))
2538                 summaryview->moved--;
2539         if (MSG_IS_COPY(msginfo->flags))
2540                 summaryview->copied--;
2541         MSG_UNSET_FLAGS(msginfo->flags,
2542                         MSG_MARKED |
2543                         MSG_MOVE |
2544                         MSG_COPY);
2545         MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
2546
2547         CHANGE_FLAGS(msginfo);
2548
2549         summaryview->deleted++;
2550
2551         if (!prefs_common.immediate_exec)
2552                 summary_set_row_marks(summaryview, row);
2553
2554         debug_print(_("Message %s/%d is set to delete\n"),
2555                     msginfo->folder->path, msginfo->msgnum);
2556 }
2557
2558 void summary_delete(SummaryView *summaryview)
2559 {
2560         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2561         GList *cur;
2562
2563         if (!summaryview->folder_item ||
2564             summaryview->folder_item->folder->type == F_NEWS) return;
2565
2566         /* if current folder is trash, don't delete */
2567         if (summaryview->folder_item->stype == F_TRASH) {
2568                 alertpanel_notice(_("Current folder is Trash."));
2569                 return;
2570         }
2571
2572         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2573                 summary_delete_row(summaryview, GTK_CTREE_NODE(cur->data));
2574
2575         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2576
2577         if (prefs_common.immediate_exec)
2578                 summary_execute(summaryview);
2579         else
2580                 summary_status_show(summaryview);
2581 }
2582
2583 void summary_delete_duplicated(SummaryView *summaryview)
2584 {
2585         if (!summaryview->folder_item ||
2586             summaryview->folder_item->folder->type == F_NEWS) return;
2587         if (summaryview->folder_item->stype == F_TRASH) return;
2588
2589         main_window_cursor_wait(summaryview->mainwin);
2590         debug_print(_("Deleting duplicated messages..."));
2591         STATUSBAR_PUSH(summaryview->mainwin,
2592                        _("Deleting duplicated messages..."));
2593
2594         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2595                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2596                                 summaryview);
2597
2598         if (prefs_common.immediate_exec)
2599                 summary_execute(summaryview);
2600         else
2601                 summary_status_show(summaryview);
2602
2603         debug_print(_("done.\n"));
2604         STATUSBAR_POP(summaryview->mainwin);
2605         main_window_cursor_normal(summaryview->mainwin);
2606 }
2607
2608 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2609                                            SummaryView *summaryview)
2610 {
2611         GtkCTreeNode *found;
2612         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2613
2614         if (!msginfo->msgid || !*msginfo->msgid) return;
2615
2616         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2617
2618         if (found && found != node)
2619                 summary_delete_row(summaryview, node);
2620 }
2621
2622 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2623 {
2624         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2625         MsgInfo *msginfo;
2626
2627         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2628         msginfo->to_folder = NULL;
2629         if (MSG_IS_DELETED(msginfo->flags))
2630                 summaryview->deleted--;
2631         if (MSG_IS_MOVE(msginfo->flags))
2632                 summaryview->moved--;
2633         if (MSG_IS_COPY(msginfo->flags))
2634                 summaryview->copied--;
2635         MSG_UNSET_FLAGS(msginfo->flags,
2636                         MSG_MARKED |
2637                         MSG_DELETED |
2638                         MSG_MOVE |
2639                         MSG_COPY);
2640
2641         CHANGE_FLAGS(msginfo);
2642
2643         summary_set_row_marks(summaryview, row);
2644
2645         debug_print(_("Message %s/%d is unmarked\n"),
2646                     msginfo->folder->path, msginfo->msgnum);
2647 }
2648
2649 void summary_unmark(SummaryView *summaryview)
2650 {
2651         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2652         GList *cur;
2653
2654         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2655              cur = cur->next)
2656                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2657
2658         summary_status_show(summaryview);
2659 }
2660
2661 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2662                                 FolderItem *to_folder)
2663 {
2664         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2665         MsgInfo *msginfo;
2666
2667         g_return_if_fail(to_folder != NULL);
2668
2669         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2670         msginfo->to_folder = to_folder;
2671         if (MSG_IS_DELETED(msginfo->flags))
2672                 summaryview->deleted--;
2673         MSG_UNSET_FLAGS(msginfo->flags,
2674                         MSG_MARKED | MSG_DELETED | MSG_COPY);
2675         if (!MSG_IS_MOVE(msginfo->flags)) {
2676                 MSG_SET_FLAGS(msginfo->flags, MSG_MOVE);
2677                 summaryview->moved++;
2678         }
2679
2680         if (!prefs_common.immediate_exec)
2681                 summary_set_row_marks(summaryview, row);
2682
2683         debug_print(_("Message %d is set to move to %s\n"),
2684                     msginfo->msgnum, to_folder->path);
2685 }
2686
2687 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2688 {
2689         GList *cur;
2690
2691         if (!to_folder) return;
2692         if (!summaryview->folder_item ||
2693             summaryview->folder_item->folder->type == F_NEWS) return;
2694         if (summaryview->folder_item == to_folder) {
2695                 alertpanel_notice(_("Destination is same as current folder."));
2696                 return;
2697         }
2698
2699         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2700              cur != NULL; cur = cur->next)
2701                 summary_move_row_to
2702                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2703
2704         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2705
2706         if (prefs_common.immediate_exec)
2707                 summary_execute(summaryview);
2708         else
2709                 summary_status_show(summaryview);
2710 }
2711
2712 void summary_move_to(SummaryView *summaryview)
2713 {
2714         FolderItem *to_folder;
2715
2716         if (!summaryview->folder_item ||
2717             summaryview->folder_item->folder->type == F_NEWS) return;
2718
2719         to_folder = foldersel_folder_sel(NULL, NULL);
2720         summary_move_selected_to(summaryview, to_folder);
2721 }
2722
2723 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2724                                 FolderItem *to_folder)
2725 {
2726         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2727         MsgInfo *msginfo;
2728
2729         g_return_if_fail(to_folder != NULL);
2730
2731         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2732         msginfo->to_folder = to_folder;
2733         if (MSG_IS_DELETED(msginfo->flags))
2734                 summaryview->deleted--;
2735         MSG_UNSET_FLAGS(msginfo->flags,
2736                         MSG_MARKED | MSG_DELETED | MSG_MOVE);
2737         if (!MSG_IS_COPY(msginfo->flags)) {
2738                 MSG_SET_FLAGS(msginfo->flags, MSG_COPY);
2739                 summaryview->copied++;
2740         }
2741
2742         if (!prefs_common.immediate_exec)
2743                 summary_set_row_marks(summaryview, row);
2744
2745         debug_print(_("Message %d is set to copy to %s\n"),
2746                     msginfo->msgnum, to_folder->path);
2747 }
2748
2749 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2750 {
2751         GList *cur;
2752
2753         if (!to_folder) return;
2754         if (!summaryview->folder_item ||
2755             summaryview->folder_item->folder->type == F_NEWS) return;
2756         if (summaryview->folder_item == to_folder) {
2757                 alertpanel_notice
2758                         (_("Destination to copy is same as current folder."));
2759                 return;
2760         }
2761
2762         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2763              cur != NULL; cur = cur->next)
2764                 summary_copy_row_to
2765                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2766
2767         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2768
2769         if (prefs_common.immediate_exec)
2770                 summary_execute(summaryview);
2771         else
2772                 summary_status_show(summaryview);
2773 }
2774
2775 void summary_copy_to(SummaryView *summaryview)
2776 {
2777         FolderItem *to_folder;
2778
2779         if (!summaryview->folder_item ||
2780             summaryview->folder_item->folder->type == F_NEWS) return;
2781
2782         to_folder = foldersel_folder_sel(NULL, NULL);
2783         summary_copy_selected_to(summaryview, to_folder);
2784 }
2785
2786 void summary_select_all(SummaryView *summaryview)
2787 {
2788         if (summaryview->messages >= 500) {
2789                 STATUSBAR_PUSH(summaryview->mainwin,
2790                                _("Selecting all messages..."));
2791                 main_window_cursor_wait(summaryview->mainwin);
2792         }
2793
2794         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2795
2796         if (summaryview->messages >= 500) {
2797                 STATUSBAR_POP(summaryview->mainwin);
2798                 main_window_cursor_normal(summaryview->mainwin);
2799         }
2800 }
2801
2802 void summary_unselect_all(SummaryView *summaryview)
2803 {
2804         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2805 }
2806
2807 void summary_save_as(SummaryView *summaryview)
2808 {
2809         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2810         MsgInfo *msginfo;
2811         gchar *src, *dest;
2812
2813         if (!summaryview->selected) return;
2814         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2815         if (!msginfo) return;
2816
2817         dest = filesel_select_file(_("Save as"), NULL);
2818         if (!dest) return;
2819         if (is_file_exist(dest)) {
2820                 AlertValue aval;
2821
2822                 aval = alertpanel(_("Overwrite"),
2823                                   _("Overwrite existing file?"),
2824                                   _("OK"), _("Cancel"), NULL);
2825                 if (G_ALERTDEFAULT != aval) return;
2826         }
2827
2828         src = procmsg_get_message_file(msginfo);
2829         copy_file(src, dest);
2830         g_free(src);
2831 }
2832
2833 void summary_print(SummaryView *summaryview)
2834 {
2835         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2836         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2837         MsgInfo *msginfo;
2838         GList *cur;
2839         gchar *cmdline;
2840         gchar *p;
2841
2842         if (clist->selection == NULL) return;
2843
2844         cmdline = input_dialog(_("Print"),
2845                                _("Enter the print command line:\n"
2846                                  "(`%s' will be replaced with file name)"),
2847                                prefs_common.print_cmd);
2848         if (!cmdline) return;
2849         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
2850             strchr(p + 2, '%')) {
2851                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
2852                                  cmdline);
2853                 g_free(cmdline);
2854                 return;
2855         }
2856
2857         for (cur = clist->selection; cur != NULL; cur = cur->next) {
2858                 msginfo = gtk_ctree_node_get_row_data
2859                         (ctree, GTK_CTREE_NODE(cur->data));
2860                 if (msginfo) procmsg_print_message(msginfo, cmdline);
2861         }
2862         
2863         g_free(cmdline);
2864 }
2865
2866 void summary_execute(SummaryView *summaryview)
2867 {
2868         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2869         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2870         GtkCTreeNode *node, *next;
2871
2872         if (!summaryview->folder_item ||
2873             summaryview->folder_item->folder->type == F_NEWS) return;
2874
2875         gtk_clist_freeze(clist);
2876
2877         if (summaryview->folder_item->prefs->enable_thread)
2878                 /*      if (prefs_common.enable_thread) */
2879                 summary_unthread_for_exec(summaryview);
2880
2881         summary_execute_move(summaryview);
2882         summary_execute_copy(summaryview);
2883         summary_execute_delete(summaryview);
2884
2885         node = GTK_CTREE_NODE(clist->row_list);
2886         while (node != NULL) {
2887                 next = GTK_CTREE_NODE_NEXT(node);
2888                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
2889                         if (node == summaryview->displayed) {
2890                                 messageview_clear(summaryview->messageview);
2891                                 summaryview->displayed = NULL;
2892                         }
2893                         if (GTK_CTREE_ROW(node)->children != NULL)
2894                                 g_warning("summary_execute(): children != NULL\n");
2895                         else
2896                                 gtk_ctree_remove_node(ctree, node);
2897                 }
2898                 node = next;
2899         }
2900
2901         if (summaryview->folder_item->prefs->enable_thread)
2902                 /*      if (prefs_common.enable_thread) */
2903                 summary_thread_build(summaryview);
2904
2905         summaryview->selected = clist->selection ?
2906                 GTK_CTREE_NODE(clist->selection->data) : NULL;
2907
2908         if (!GTK_CLIST(summaryview->ctree)->row_list) {
2909                 menu_set_insensitive_all
2910                         (GTK_MENU_SHELL(summaryview->popupmenu));
2911                 gtk_widget_grab_focus(summaryview->folderview->ctree);
2912         } else
2913                 gtk_widget_grab_focus(summaryview->ctree);
2914
2915         summary_update_status(summaryview);
2916         summary_status_show(summaryview);
2917
2918         summary_write_cache(summaryview);
2919
2920         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2921
2922         gtk_clist_thaw(clist);
2923 }
2924
2925 static void summary_execute_move(SummaryView *summaryview)
2926 {
2927         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2928         GSList *cur;
2929
2930         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2931
2932         /* search moving messages and execute */
2933         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
2934                                 summaryview);
2935
2936         if (summaryview->mlist) {
2937                 procmsg_move_messages(summaryview->mlist);
2938
2939                 folder_item_scan_foreach(summaryview->folder_table);
2940                 folderview_update_item_foreach(summaryview->folder_table);
2941
2942                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
2943                         procmsg_msginfo_free((MsgInfo *)cur->data);
2944                 g_slist_free(summaryview->mlist);
2945                 summaryview->mlist = NULL;
2946         }
2947
2948         g_hash_table_destroy(summaryview->folder_table);
2949         summaryview->folder_table = NULL;
2950 }
2951
2952 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
2953                                       gpointer data)
2954 {
2955         SummaryView *summaryview = data;
2956         MsgInfo *msginfo;
2957
2958         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2959
2960         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
2961                 g_hash_table_insert(summaryview->folder_table,
2962                                     msginfo->to_folder, GINT_TO_POINTER(1));
2963
2964                 summaryview->mlist =
2965                         g_slist_append(summaryview->mlist, msginfo);
2966                 gtk_ctree_node_set_row_data(ctree, node, NULL);
2967
2968                 if (msginfo->msgid && *msginfo->msgid &&
2969                     node == g_hash_table_lookup(summaryview->msgid_table,
2970                                                 msginfo->msgid))
2971                         g_hash_table_remove(summaryview->msgid_table,
2972                                             msginfo->msgid);
2973         }
2974 }
2975
2976 static void summary_execute_copy(SummaryView *summaryview)
2977 {
2978         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2979
2980         summaryview->folder_table = g_hash_table_new(NULL, NULL);
2981
2982         /* search copying messages and execute */
2983         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
2984                                 summaryview);
2985
2986         if (summaryview->mlist) {
2987                 procmsg_copy_messages(summaryview->mlist);
2988
2989                 folder_item_scan_foreach(summaryview->folder_table);
2990                 folderview_update_item_foreach(summaryview->folder_table);
2991
2992                 g_slist_free(summaryview->mlist);
2993                 summaryview->mlist = NULL;
2994         }
2995
2996         g_hash_table_destroy(summaryview->folder_table);
2997         summaryview->folder_table = NULL;
2998 }
2999
3000 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3001                                       gpointer data)
3002 {
3003         SummaryView *summaryview = data;
3004         MsgInfo *msginfo;
3005
3006         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3007
3008         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3009                 g_hash_table_insert(summaryview->folder_table,
3010                                     msginfo->to_folder, GINT_TO_POINTER(1));
3011
3012                 summaryview->mlist =
3013                         g_slist_append(summaryview->mlist, msginfo);
3014
3015                 MSG_UNSET_FLAGS(msginfo->flags, MSG_COPY);
3016                 
3017                 summary_set_row_marks(summaryview, node);
3018         }
3019 }
3020
3021 static void summary_execute_delete(SummaryView *summaryview)
3022 {
3023         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3024         FolderItem *trash;
3025         GSList *cur;
3026
3027         trash = summaryview->folder_item->folder->trash;
3028         if (summaryview->folder_item->folder->type == F_MH) {
3029                 g_return_if_fail(trash != NULL);
3030         }
3031         if (summaryview->folder_item == trash) return;
3032
3033         /* search deleting messages and execute */
3034         gtk_ctree_pre_recursive
3035                 (ctree, NULL, summary_execute_delete_func, summaryview);
3036
3037         if (!summaryview->mlist) return;
3038
3039         for(cur = summaryview->mlist ; cur != NULL ; cur = cur->next) {
3040                 MsgInfo * msginfo = cur->data;
3041                 MSG_UNSET_FLAGS(msginfo->flags, MSG_DELETED);
3042         }
3043
3044         folder_item_move_msgs_with_dest(trash, summaryview->mlist);
3045
3046         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3047                 procmsg_msginfo_free((MsgInfo *)cur->data);
3048
3049         g_slist_free(summaryview->mlist);
3050         summaryview->mlist = NULL;
3051
3052         folder_item_scan(trash);
3053         folderview_update_item(trash, FALSE);
3054 }
3055
3056 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3057                                         gpointer data)
3058 {
3059         SummaryView *summaryview = data;
3060         MsgInfo *msginfo;
3061
3062         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3063
3064         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3065                 summaryview->mlist =
3066                         g_slist_append(summaryview->mlist, msginfo);
3067                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3068
3069                 if (msginfo->msgid && *msginfo->msgid &&
3070                     node == g_hash_table_lookup(summaryview->msgid_table,
3071                                                 msginfo->msgid))
3072                         g_hash_table_remove(summaryview->msgid_table,
3073                                             msginfo->msgid);
3074
3075                 if (msginfo->subject && 
3076                     node == subject_table_lookup(summaryview->subject_table, 
3077                                                 msginfo->subject)) {
3078                         gchar *s = msginfo->subject + (g_strncasecmp(msginfo->subject, "Re: ", 4) == 0 ? 4 : 0);
3079                         g_hash_table_remove(summaryview->subject_table, s);
3080                 }                       
3081         }
3082 }
3083
3084 /* thread functions */
3085
3086 void summary_thread_build(SummaryView *summaryview)
3087 {
3088         debug_print(_("Building threads..."));
3089         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3090         main_window_cursor_wait(summaryview->mainwin);
3091
3092         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3093
3094         gtk_ctree_pre_recursive_to_depth
3095                 (GTK_CTREE(summaryview->ctree), NULL, 1,
3096                  GTK_CTREE_FUNC(summary_thread_func),
3097                  summaryview);
3098
3099         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3100
3101         debug_print(_("done.\n"));
3102         STATUSBAR_POP(summaryview->mainwin);
3103         main_window_cursor_normal(summaryview->mainwin);
3104 }
3105
3106 void summary_unthread(SummaryView *summaryview)
3107 {
3108         GtkCTreeNode *node;
3109         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3110
3111         debug_print(_("Unthreading..."));
3112         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
3113         main_window_cursor_wait(summaryview->mainwin);
3114
3115         gtk_clist_freeze(GTK_CLIST(ctree));
3116
3117         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3118              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3119                 summary_unthread_func(ctree, node, NULL);
3120         }
3121
3122         gtk_clist_thaw(GTK_CLIST(ctree));
3123
3124         debug_print(_("done.\n"));
3125         STATUSBAR_POP(summaryview->mainwin);
3126         main_window_cursor_normal(summaryview->mainwin);
3127 }
3128
3129 static void summary_unthread_for_exec(SummaryView *summaryview)
3130 {
3131         GtkCTreeNode *node;
3132         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3133
3134         debug_print(_("Unthreading for execution..."));
3135
3136         gtk_clist_freeze(GTK_CLIST(ctree));
3137
3138         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3139              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3140                 summary_unthread_for_exec_func(ctree, node, NULL);
3141         }
3142
3143         gtk_clist_thaw(GTK_CLIST(ctree));
3144
3145         debug_print(_("done.\n"));
3146 }
3147
3148 static void summary_thread_func(GtkCTree *ctree, GtkCTreeNode *node,
3149                                 gpointer data)
3150 {
3151         MsgInfo *msginfo;
3152         GtkCTreeNode *parent = NULL;
3153         
3154         SummaryView * summaryview = (SummaryView *) data;
3155         GHashTable *msgid_table = summaryview->msgid_table;
3156         GHashTable *subject_table = summaryview->subject_table;
3157
3158         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3159
3160         if (!msginfo) return;
3161
3162         if(msginfo->inreplyto) {
3163             parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
3164         }
3165         if (parent == NULL && msginfo->subject &&
3166             g_strncasecmp(msginfo->subject, "Re: ", 4) == 0) {
3167                 parent = subject_table_lookup(subject_table, msginfo->subject);
3168         }
3169
3170         if (parent && parent != node) {
3171                 gtk_ctree_move(ctree, node, parent, NULL);
3172                 gtk_ctree_expand(ctree, parent);
3173         }
3174 }
3175
3176 static void summary_unthread_func(GtkCTree *ctree, GtkCTreeNode *node,
3177                                   gpointer data)
3178 {
3179         GtkCTreeNode *child;
3180         GtkCTreeNode *sibling;
3181
3182         child = GTK_CTREE_ROW(node)->children;
3183         sibling = GTK_CTREE_ROW(node)->sibling;
3184
3185         while (child != NULL) {
3186                 GtkCTreeNode *next_child;
3187
3188                 next_child = GTK_CTREE_ROW(child)->sibling;
3189                 gtk_ctree_move(ctree, child, NULL, sibling);
3190                 child = next_child;
3191         }
3192 }
3193
3194 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
3195                                            gpointer data)
3196 {
3197         MsgInfo *msginfo;
3198         GtkCTreeNode *top_parent;
3199         GtkCTreeNode *child;
3200         GtkCTreeNode *sibling;
3201
3202         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3203
3204         if (!msginfo ||
3205             (!MSG_IS_MOVE(msginfo->flags) &&
3206              !MSG_IS_DELETED(msginfo->flags)))
3207                 return;
3208         child = GTK_CTREE_ROW(node)->children;
3209         if (!child) return;
3210
3211         for (top_parent = node;
3212              GTK_CTREE_ROW(top_parent)->parent != NULL;
3213              top_parent = GTK_CTREE_ROW(top_parent)->parent)
3214                 ;
3215         sibling = GTK_CTREE_ROW(top_parent)->sibling;
3216
3217         while (child != NULL) {
3218                 GtkCTreeNode *next_child;
3219
3220                 next_child = GTK_CTREE_ROW(child)->sibling;
3221                 gtk_ctree_move(ctree, child, NULL, sibling);
3222                 child = next_child;
3223         }
3224 }
3225
3226 void summary_filter(SummaryView *summaryview)
3227 {
3228         if (!prefs_common.fltlist) return;
3229
3230         debug_print(_("filtering..."));
3231         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
3232         main_window_cursor_wait(summaryview->mainwin);
3233
3234         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3235
3236         if (prefs_filtering == NULL) {
3237                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3238                                         GTK_CTREE_FUNC(summary_filter_func),
3239                                         summaryview);
3240                 
3241                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3242
3243                 if (prefs_common.immediate_exec)
3244                         summary_execute(summaryview);
3245                 else
3246                         summary_status_show(summaryview);
3247         }
3248         else {
3249                 summaryview->folder_table = g_hash_table_new(NULL, NULL);
3250
3251                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3252                                         GTK_CTREE_FUNC(summary_filter_func),
3253                                         summaryview);
3254
3255                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3256
3257                 folder_item_scan_foreach(summaryview->folder_table);
3258                 folderview_update_item_foreach(summaryview->folder_table);
3259
3260                 g_hash_table_destroy(summaryview->folder_table);
3261                 summaryview->folder_table = NULL;
3262
3263                 summary_show(summaryview, summaryview->folder_item, FALSE);
3264         }
3265
3266         debug_print(_("done.\n"));
3267         STATUSBAR_POP(summaryview->mainwin);
3268         main_window_cursor_normal(summaryview->mainwin);
3269 }
3270
3271 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
3272                                 gpointer data)
3273 {
3274         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3275         SummaryView *summaryview = data;
3276         gchar *file;
3277         FolderItem *dest;
3278
3279         if (prefs_filtering == NULL) {
3280                 /* old filtering */
3281                 file = procmsg_get_message_file_path(msginfo);
3282                 dest = filter_get_dest_folder(prefs_common.fltlist, file);
3283                 g_free(file);
3284
3285                 if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
3286                     summaryview->folder_item != dest)
3287                         summary_move_row_to(summaryview, node, dest);
3288         }
3289         else
3290                 filter_msginfo_move_or_delete(prefs_filtering, msginfo,
3291                                               summaryview->folder_table);
3292 }
3293
3294 /* callback functions */
3295
3296 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
3297                                    SummaryView *summaryview)
3298 {
3299         if (!event)
3300                 return;
3301
3302         if (!summaryview->msg_is_toggled_on && summaryview->selected)
3303                 summary_display_msg(summaryview, summaryview->selected, FALSE);
3304         else
3305                 summary_toggle_view(summaryview);
3306 }
3307
3308 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
3309                                    SummaryView *summaryview)
3310 {
3311         if (!event) return;
3312
3313         if (event->button == 3) {
3314                 /* right clicked */
3315                 summary_add_sender_to_cb(summaryview, 0, 0);    
3316                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
3317                                NULL, NULL, event->button, event->time);
3318         } else if (event->button == 2) {
3319                 summaryview->display_msg = TRUE;
3320         } else if (event->button == 1) {
3321                 if (!prefs_common.emulate_emacs &&
3322                     summaryview->msg_is_toggled_on)
3323                         summaryview->display_msg = TRUE;
3324         }
3325 }
3326
3327 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
3328                                     SummaryView *summaryview)
3329 {
3330 }
3331
3332 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
3333 {
3334         summary_key_pressed(summaryview->ctree, event, summaryview);
3335 }
3336
3337 #define BREAK_ON_MODIFIER_KEY() \
3338         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3339
3340 #define KEY_PRESS_EVENT_STOP() \
3341         if (gtk_signal_n_emissions_by_name \
3342                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
3343                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
3344                                              "key_press_event"); \
3345         }
3346
3347 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
3348                                 SummaryView *summaryview)
3349 {
3350         GtkCTree *ctree = GTK_CTREE(widget);
3351         GtkCTreeNode *node;
3352         FolderItem *to_folder;
3353
3354         if (!event) return;
3355
3356         switch (event->keyval) {
3357         case GDK_g:             /* Go */
3358         case GDK_G:
3359                 BREAK_ON_MODIFIER_KEY();
3360                 KEY_PRESS_EVENT_STOP();
3361                 to_folder = foldersel_folder_sel(NULL, NULL);
3362                 if (to_folder) {
3363                         debug_print(_("Go to %s\n"), to_folder->path);
3364                         folderview_select(summaryview->folderview, to_folder);
3365                 }
3366                 return;
3367         case GDK_w:             /* Write new message */
3368                 BREAK_ON_MODIFIER_KEY();
3369                 if (summaryview->folder_item) {
3370                         PrefsAccount *ac;
3371                         ac = summaryview->folder_item->folder->account;
3372                         if (ac && ac->protocol == A_NNTP)
3373                                 compose_new_with_recipient
3374                                         (ac, summaryview->folder_item->path);
3375                         else
3376                                 compose_new(ac);
3377                 } else
3378                         compose_new(NULL);
3379                 return;
3380         case GDK_D:             /* Empty trash */
3381                 BREAK_ON_MODIFIER_KEY();
3382                 KEY_PRESS_EVENT_STOP();
3383                 main_window_empty_trash(summaryview->mainwin, TRUE);
3384                 return;
3385         case GDK_Q:             /* Quit */
3386                 BREAK_ON_MODIFIER_KEY();
3387
3388                 if (prefs_common.confirm_on_exit) {
3389                         if (alertpanel(_("Exit"), _("Exit this program?"),
3390                                        _("OK"), _("Cancel"), NULL)
3391                                        == G_ALERTDEFAULT) {
3392                                 manage_window_focus_in
3393                                         (summaryview->mainwin->window,
3394                                          NULL, NULL);
3395                                 app_will_exit(NULL, summaryview->mainwin);
3396                         }
3397                 }
3398                 return;
3399         case GDK_Left:          /* Move focus */
3400         case GDK_Escape:
3401                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3402                 return;
3403         default:
3404                 break;
3405         }
3406
3407         if (!summaryview->selected) {
3408                 node = gtk_ctree_node_nth(ctree, 0);
3409                 if (node)
3410                         gtk_ctree_select(ctree, node);
3411                 else
3412                         return;
3413         }
3414
3415         switch (event->keyval) {
3416         case GDK_space:         /* Page down or go to the next */
3417                 if (summaryview->displayed != summaryview->selected) {
3418                         summary_display_msg(summaryview,
3419                                             summaryview->selected, FALSE);
3420                         break;
3421                 }
3422                 if (!textview_scroll_page(summaryview->messageview->textview,
3423                                           FALSE))
3424                         summary_select_next_unread(summaryview);
3425                 break;
3426         case GDK_n:             /* Next */
3427         case GDK_N:
3428                 BREAK_ON_MODIFIER_KEY();
3429                 summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3430                 break;
3431         case GDK_BackSpace:     /* Page up */
3432         case GDK_Delete:
3433                 textview_scroll_page(summaryview->messageview->textview, TRUE);
3434                 break;
3435         case GDK_p:             /* Prev */
3436         case GDK_P:
3437                 BREAK_ON_MODIFIER_KEY();
3438                 summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
3439                 break;
3440         case GDK_v:             /* Toggle summary mode / message mode */
3441         case GDK_V:
3442                 BREAK_ON_MODIFIER_KEY();
3443
3444                 if (!summaryview->msg_is_toggled_on && summaryview->selected)
3445                         summary_display_msg(summaryview,
3446                                             summaryview->selected, FALSE);
3447                 else
3448                         summary_toggle_view(summaryview);
3449                 break;
3450         case GDK_Return:        /* Scroll up/down one line */
3451                 if (summaryview->displayed != summaryview->selected) {
3452                         summary_display_msg(summaryview,
3453                                             summaryview->selected, FALSE);
3454                         break;
3455                 }
3456                 textview_scroll_one_line(summaryview->messageview->textview,
3457                                          (event->state & GDK_MOD1_MASK) != 0);
3458                 break;
3459         case GDK_asterisk:      /* Mark */
3460                 summary_mark(summaryview);
3461                 break;
3462         case GDK_exclam:        /* Mark as unread */
3463                 summary_mark_as_unread(summaryview);
3464                 break;
3465         case GDK_d:             /* Delete */
3466                 BREAK_ON_MODIFIER_KEY();
3467                 summary_delete(summaryview);
3468                 break;
3469         case GDK_u:             /* Unmark */
3470         case GDK_U:
3471                 BREAK_ON_MODIFIER_KEY();
3472                 summary_unmark(summaryview);
3473                 break;
3474         case GDK_o:             /* Move */
3475                 BREAK_ON_MODIFIER_KEY();
3476                 summary_move_to(summaryview);
3477                 break;
3478         case GDK_O:             /* Copy */
3479                 BREAK_ON_MODIFIER_KEY();
3480                 summary_copy_to(summaryview);
3481                 break;
3482         case GDK_x:             /* Execute */
3483         case GDK_X:
3484                 BREAK_ON_MODIFIER_KEY();
3485                 KEY_PRESS_EVENT_STOP();
3486                 summary_execute(summaryview);
3487                 break;
3488         case GDK_a:             /* Reply to the message */
3489                 BREAK_ON_MODIFIER_KEY();
3490                 summary_reply_cb(summaryview,
3491                                  COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL);
3492                 break;
3493         case GDK_A:             /* Reply to the message with quotation */
3494                 BREAK_ON_MODIFIER_KEY();
3495                 summary_reply_cb(summaryview,
3496                                  COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL);
3497                 break;
3498         case GDK_f:             /* Forward the message */
3499                 BREAK_ON_MODIFIER_KEY();
3500                 summary_reply_cb(summaryview, COMPOSE_FORWARD, NULL);
3501                 break;
3502         case GDK_F:
3503                 BREAK_ON_MODIFIER_KEY();
3504                 summary_reply_cb(summaryview, COMPOSE_FORWARD_AS_ATTACH, NULL);
3505                 break;
3506         case GDK_y:             /* Save the message */
3507                 BREAK_ON_MODIFIER_KEY();
3508                 summary_save_as(summaryview);
3509                 break;
3510         default:
3511                 break;
3512         }
3513 }
3514
3515 #undef BREAK_ON_MODIFIER_KEY
3516 #undef KEY_PRESS_EVENT_STOP
3517
3518 static void summary_open_row(GtkSCTree *sctree, SummaryView *summaryview)
3519 {
3520         if (summaryview->folder_item->stype == F_DRAFT)
3521                 summary_reedit(summaryview);
3522         else
3523                 summary_open_msg(summaryview);
3524
3525         summaryview->display_msg = FALSE;
3526 }
3527
3528 static void summary_selected(GtkCTree *ctree, GtkCTreeNode *row,
3529                              gint column, SummaryView *summaryview)
3530 {
3531         MsgInfo *msginfo;
3532
3533         summary_status_show(summaryview);
3534         summary_set_menu_sensitive(summaryview);
3535
3536         if (GTK_CLIST(ctree)->selection &&
3537              GTK_CLIST(ctree)->selection->next) {
3538                 summaryview->display_msg = FALSE;
3539                 return;
3540         }
3541
3542         summaryview->selected = row;
3543
3544         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3545
3546         switch (column) {
3547         case S_COL_MARK:
3548                 if (MSG_IS_MARKED(msginfo->flags)) {
3549                         MSG_UNSET_FLAGS(msginfo->flags, MSG_MARKED);
3550
3551                         CHANGE_FLAGS(msginfo);
3552                         
3553                         summary_set_row_marks(summaryview, row);
3554                 } else
3555                         summary_mark_row(summaryview, row);
3556                 break;
3557         case S_COL_UNREAD:
3558                 if (MSG_IS_UNREAD(msginfo->flags)) {
3559                         summary_mark_row_as_read(summaryview, row);
3560                         summary_status_show(summaryview);
3561                 } else if (!MSG_IS_REPLIED(msginfo->flags) &&
3562                          !MSG_IS_FORWARDED(msginfo->flags)) {
3563                         summary_mark_row_as_unread(summaryview, row);
3564                         summary_status_show(summaryview);
3565                 }
3566                 break;
3567         default:
3568                 break;
3569         }
3570
3571         if (summaryview->display_msg)
3572                 summary_display_msg(summaryview, row, FALSE);
3573
3574         summaryview->display_msg = FALSE;
3575 }
3576
3577 static void summary_col_resized(GtkCList *clist, gint column, gint width,
3578                                 SummaryView *summaryview)
3579 {
3580         switch (column) {
3581         case S_COL_MARK:
3582                 prefs_common.summary_col_mark = width;
3583                 break;
3584         case S_COL_UNREAD:
3585                 prefs_common.summary_col_unread = width;
3586                 break;
3587         case S_COL_MIME:
3588                 prefs_common.summary_col_mime = width;
3589                 break;
3590         case S_COL_NUMBER:
3591                 prefs_common.summary_col_number = width;
3592                 break;
3593         case S_COL_SCORE:
3594                 prefs_common.summary_col_score = width;
3595                 break;
3596         case S_COL_SIZE:
3597                 prefs_common.summary_col_size = width;
3598                 break;
3599         case S_COL_DATE:
3600                 prefs_common.summary_col_date = width;
3601                 break;
3602         case S_COL_FROM:
3603                 prefs_common.summary_col_from = width;
3604                 break;
3605         case S_COL_SUBJECT:
3606                 prefs_common.summary_col_subject = width;
3607                 break;
3608         default:
3609                 break;
3610         }
3611 }
3612
3613 static void summary_reply_cb(SummaryView *summaryview, guint action,
3614                              GtkWidget *widget)
3615 {
3616         MsgInfo *msginfo;
3617         GList  *sel = GTK_CLIST(summaryview->ctree)->selection;
3618
3619         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3620                                               summaryview->selected);
3621         if (!msginfo) return;
3622
3623         switch ((ComposeReplyMode)action) {
3624         case COMPOSE_REPLY:
3625                 compose_reply(msginfo, prefs_common.reply_with_quote,
3626                               FALSE, FALSE);
3627                 break;
3628         case COMPOSE_REPLY_WITH_QUOTE:
3629                 compose_reply(msginfo, TRUE, FALSE, FALSE);
3630                 break;
3631         case COMPOSE_REPLY_WITHOUT_QUOTE:
3632                 compose_reply(msginfo, FALSE, FALSE, FALSE);
3633                 break;
3634         case COMPOSE_REPLY_TO_SENDER:
3635                 compose_reply(msginfo, prefs_common.reply_with_quote,
3636                               FALSE, TRUE);
3637                 break;
3638         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
3639                 compose_followup_and_reply_to(msginfo,
3640                                               prefs_common.reply_with_quote,
3641                                               FALSE, TRUE);
3642                 break;
3643         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
3644                 compose_reply(msginfo, TRUE, FALSE, TRUE);
3645                 break;
3646         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
3647                 compose_reply(msginfo, FALSE, FALSE, TRUE);
3648                 break;
3649         case COMPOSE_REPLY_TO_ALL:
3650                 compose_reply(msginfo, prefs_common.reply_with_quote,
3651                               TRUE, FALSE);
3652                 break;
3653         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
3654                 compose_reply(msginfo, TRUE, TRUE, FALSE);
3655                 break;
3656         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
3657                 compose_reply(msginfo, FALSE, TRUE, FALSE);
3658                 break;
3659         case COMPOSE_FORWARD:
3660                 if (!sel->next) {
3661                         compose_forward(NULL, msginfo, FALSE);
3662                         break;
3663                 }
3664                 /* if (sel->next) FALL THROUGH */
3665         case COMPOSE_FORWARD_AS_ATTACH:
3666                 {
3667                         GSList *msginfo_list = NULL;
3668                         for ( ; sel != NULL; sel = sel->next)
3669                                 msginfo_list = g_slist_append(msginfo_list, 
3670                                         gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3671                                                 GTK_CTREE_NODE(sel->data)));
3672                         compose_forward_multiple(NULL, msginfo_list);
3673                         g_slist_free(msginfo_list);
3674                 }                       
3675                 break;
3676         default:
3677                 g_warning("summary_reply_cb(): invalid action: %d\n", action);
3678         }
3679
3680         summary_set_marks_selected(summaryview);
3681 }
3682
3683 static void summary_show_all_header_cb(SummaryView *summaryview,
3684                                        guint action, GtkWidget *widget)
3685 {
3686         header_window_show_cb(summaryview->mainwin, action, widget);
3687 }
3688
3689 static void summary_add_sender_to_cb (SummaryView                       *summaryview,
3690                                          guint                   action,
3691                                          GtkWidget              *widget_)
3692 {
3693         const gint       ADD_SENDER_TO_ADDRESSBOOK_MENU_POS = 13;
3694         GtkWidget       *submenu;                                               
3695         GList           *groups, *tmp;
3696         GtkMenuShell    *menushell;
3697         GtkWidget       *menu;
3698         GtkWidget       *menuitem;
3699         GList           *child;
3700         gboolean        found = FALSE;
3701         MsgInfo        *msginfo;
3702         gchar          *from_address;
3703
3704         menushell = GTK_MENU_SHELL(summaryview->popupmenu);
3705         g_return_if_fail(menushell != NULL);
3706         child = menushell->children;
3707         g_return_if_fail(child);
3708
3709         /* we're iterating each menu item searching for the one with 
3710          * a "contacts" object data. if not found add the menu,
3711          * else update it */
3712         for (child = g_list_first(menushell->children); child; child = g_list_next(child)) {
3713                 if (gtk_object_get_data(GTK_OBJECT(child->data), "contacts")) {
3714                         found = TRUE;
3715                         break;
3716                 }
3717         }
3718
3719         /* add item to default context menu if not present */
3720         if (!found) {
3721                 submenu = gtk_menu_item_new_with_label(_("Add sender to address book"));
3722                 gtk_object_set_data(GTK_OBJECT(submenu), "contacts", (gpointer)1);
3723                 gtk_menu_insert(GTK_MENU(summaryview->popupmenu), submenu, ADD_SENDER_TO_ADDRESSBOOK_MENU_POS);
3724                 gtk_widget_show(submenu);
3725         }
3726         else {
3727                 submenu = (GtkWidget *) child->data;
3728         }
3729
3730         /* get the address info from the summary view */
3731         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3732                                                                                                    summaryview->selected);
3733
3734         if (msginfo != NULL && msginfo->fromname != NULL && msginfo->from != NULL) {
3735                 gtk_widget_set_sensitive(GTK_WIDGET(submenu), TRUE);
3736                 from_address = g_strdup(msginfo->from);
3737                 eliminate_address_comment(from_address);
3738                 extract_address(from_address);
3739                 log_message("adding %s %s\n", msginfo->fromname, from_address);
3740                 addressbook_add_contact_by_menu(submenu, msginfo->fromname, from_address, NULL);
3741                 g_free(from_address);
3742         }               
3743         else {
3744                 gtk_widget_set_sensitive(GTK_WIDGET(submenu), FALSE);
3745         }
3746 }
3747
3748 static void summary_num_clicked(GtkWidget *button, SummaryView *summaryview)
3749 {
3750         summary_sort(summaryview, SORT_BY_NUMBER);
3751 }
3752
3753 static void summary_score_clicked(GtkWidget *button,
3754                                   SummaryView *summaryview)
3755 {
3756         summary_sort(summaryview, SORT_BY_SCORE);
3757 }
3758
3759 static void summary_size_clicked(GtkWidget *button, SummaryView *summaryview)
3760 {
3761         summary_sort(summaryview, SORT_BY_SIZE);
3762 }
3763
3764 static void summary_date_clicked(GtkWidget *button, SummaryView *summaryview)
3765 {
3766         summary_sort(summaryview, SORT_BY_DATE);
3767 }
3768
3769 static void summary_from_clicked(GtkWidget *button, SummaryView *summaryview)
3770 {
3771         summary_sort(summaryview, SORT_BY_FROM);
3772 }
3773
3774 static void summary_subject_clicked(GtkWidget *button,
3775                                     SummaryView *summaryview)
3776 {
3777         summary_sort(summaryview, SORT_BY_SUBJECT);
3778 }
3779
3780 static void summary_mark_clicked(GtkWidget *button,
3781                                  SummaryView *summaryview)
3782 {
3783         summary_sort(summaryview, SORT_BY_LABEL);
3784 }
3785
3786 void summary_change_display_item(SummaryView *summaryview)
3787 {
3788         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3789
3790         gtk_clist_set_column_visibility(clist, S_COL_MARK, prefs_common.show_mark);
3791         gtk_clist_set_column_visibility(clist, S_COL_UNREAD, prefs_common.show_unread);
3792         gtk_clist_set_column_visibility(clist, S_COL_MIME, prefs_common.show_mime);
3793         gtk_clist_set_column_visibility(clist, S_COL_NUMBER, prefs_common.show_number);
3794         gtk_clist_set_column_visibility(clist, S_COL_SCORE, prefs_common.show_score);
3795         gtk_clist_set_column_visibility(clist, S_COL_SIZE, prefs_common.show_size);
3796         gtk_clist_set_column_visibility(clist, S_COL_DATE, prefs_common.show_date);
3797         gtk_clist_set_column_visibility(clist, S_COL_FROM, prefs_common.show_from);
3798         gtk_clist_set_column_visibility(clist, S_COL_SUBJECT, prefs_common.show_subject);
3799 }
3800
3801 static void summary_start_drag (GtkWidget   *widget,
3802                                 gint         button,
3803                                 GdkEvent    *event,
3804                                 SummaryView *summaryview)
3805 {
3806         GtkTargetList *list;
3807         GdkDragContext *context;
3808
3809         g_return_if_fail(summaryview != NULL);
3810         g_return_if_fail(summaryview->folder_item != NULL);
3811         g_return_if_fail(summaryview->folder_item->folder != NULL);
3812         if (summaryview->folder_item->folder->type == F_NEWS ||
3813             summaryview->selected == NULL)
3814                 return;
3815
3816         list = gtk_target_list_new(summary_drag_types, 1);
3817
3818         context = gtk_drag_begin(widget, list,
3819                                  GDK_ACTION_MOVE, button, event);
3820         gtk_drag_set_icon_default(context);
3821 }
3822
3823 static void summary_drag_data_get(GtkWidget        *widget,
3824                                   GdkDragContext   *drag_context,
3825                                   GtkSelectionData *selection_data,
3826                                   guint             info,
3827                                   guint             time,
3828                                   SummaryView      *summaryview)
3829 {
3830         if (info == TARGET_MAIL_URI_LIST) {
3831                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3832                 GList *cur;
3833                 MsgInfo *msginfo;
3834                 gchar *mail_list = NULL, *tmp1, *tmp2;
3835
3836                 for (cur = GTK_CLIST(ctree)->selection;
3837                      cur != NULL; cur = cur->next) {
3838                         msginfo = gtk_ctree_node_get_row_data
3839                                 (ctree, GTK_CTREE_NODE(cur->data));
3840                         tmp2 = procmsg_get_message_file_path(msginfo);
3841                         if (!tmp2) continue;
3842                         tmp1 = g_strconcat("file:/", tmp2, NULL);
3843                         g_free(tmp2);
3844
3845                         if (!mail_list) {
3846                                 mail_list = tmp1;
3847                         } else {
3848                                 tmp2 = g_strconcat(mail_list, tmp1, NULL);
3849                                 g_free(mail_list);
3850                                 g_free(tmp1);
3851                                 mail_list = tmp2;
3852                         }
3853                 }
3854
3855                 if (mail_list != NULL) {
3856                         gtk_selection_data_set(selection_data,
3857                                                selection_data->target, 8,
3858                                                mail_list, strlen(mail_list));
3859                         g_free(mail_list);
3860                 } 
3861         } else if (info == TARGET_DUMMY) {
3862                 if (GTK_CLIST(summaryview->ctree)->selection)
3863                         gtk_selection_data_set(selection_data,
3864                                                selection_data->target, 8,
3865                                                "Dummy", 6);
3866         }
3867 }
3868
3869
3870 /* custom compare functions for sorting */
3871
3872 static gint summary_cmp_by_num(GtkCList *clist,
3873                                gconstpointer ptr1, gconstpointer ptr2)
3874 {
3875         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3876         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3877
3878         return msginfo1->msgnum - msginfo2->msgnum;
3879 }
3880
3881 static gint summary_cmp_by_size(GtkCList *clist,
3882                                 gconstpointer ptr1, gconstpointer ptr2)
3883 {
3884         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3885         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3886
3887         return msginfo1->size - msginfo2->size;
3888 }
3889
3890 static gint summary_cmp_by_date(GtkCList *clist,
3891                                gconstpointer ptr1, gconstpointer ptr2)
3892 {
3893         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3894         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3895
3896         return msginfo1->date_t - msginfo2->date_t;
3897 }
3898
3899 static gint summary_cmp_by_from(GtkCList *clist,
3900                                gconstpointer ptr1, gconstpointer ptr2)
3901 {
3902         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3903         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3904
3905         if (!msginfo1->fromname)
3906                 return (msginfo2->fromname != NULL);
3907         if (!msginfo2->fromname)
3908                 return -1;
3909
3910         return strcasecmp(msginfo1->fromname, msginfo2->fromname);
3911 }
3912
3913 static gint summary_cmp_by_subject(GtkCList *clist,
3914                                gconstpointer ptr1, gconstpointer ptr2)
3915 {
3916         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3917         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3918
3919         if (!msginfo1->subject)
3920                 return (msginfo2->subject != NULL);
3921         if (!msginfo2->subject)
3922                 return -1;
3923
3924         return strcasecmp(msginfo1->subject, msginfo2->subject);
3925 }
3926
3927 static gint summary_cmp_by_label(GtkCList *clist,
3928                                  gconstpointer ptr1, gconstpointer ptr2)
3929 {
3930         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3931         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3932
3933         return MSG_GET_LABEL(msginfo1->flags) - MSG_GET_LABEL(msginfo2->flags);
3934 }
3935
3936 static gint summary_cmp_by_score(GtkCList *clist,
3937                                  gconstpointer ptr1, gconstpointer ptr2)
3938 {
3939         MsgInfo *msginfo1 = ((GtkCListRow *)ptr1)->data;
3940         MsgInfo *msginfo2 = ((GtkCListRow *)ptr2)->data;
3941         int diff;
3942
3943         /* if score are equal, sort by date */
3944
3945         diff = msginfo1->threadscore - msginfo2->threadscore;
3946         if (diff != 0)
3947                 return diff;
3948         else
3949                 return summary_cmp_by_date(clist, ptr1, ptr2);
3950 }
3951
3952 static void summary_ignore_thread_func(GtkCTree *ctree, GtkCTreeNode *row, gpointer data)
3953 {
3954         SummaryView *summaryview = (SummaryView *) data;
3955         MsgInfo *msginfo;
3956
3957         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3958         if (MSG_IS_NEW(msginfo->flags))
3959                 summaryview->newmsgs--;
3960         if (MSG_IS_UNREAD(msginfo->flags))
3961                 summaryview->unread--;
3962         MSG_SET_FLAGS(msginfo->flags, MSG_IGNORE_THREAD);
3963
3964         CHANGE_FLAGS(msginfo);
3965                 
3966         summary_set_row_marks(summaryview, row);
3967         debug_print(_("Message %d is marked as ignore thread\n"),
3968             msginfo->msgnum);
3969 }
3970
3971 static void summary_ignore_thread(SummaryView *summaryview)
3972 {
3973         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3974         GList *cur;
3975
3976         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3977                 gtk_ctree_pre_recursive(ctree, GTK_CTREE_NODE(cur->data), GTK_CTREE_FUNC(summary_ignore_thread_func), summaryview);
3978         }
3979
3980         summary_status_show(summaryview);
3981 }
3982
3983 static void summary_unignore_thread_func(GtkCTree *ctree, GtkCTreeNode *row, gpointer data)
3984 {
3985         SummaryView *summaryview = (SummaryView *) data;
3986         MsgInfo *msginfo;
3987
3988         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3989         if (MSG_IS_NEW(msginfo->flags))
3990                 summaryview->newmsgs++;
3991         if (MSG_IS_UNREAD(msginfo->flags))
3992                 summaryview->unread++;
3993         MSG_UNSET_FLAGS(msginfo->flags, MSG_IGNORE_THREAD);
3994
3995         CHANGE_FLAGS(msginfo);
3996                 
3997         summary_set_row_marks(summaryview, row);
3998         debug_print(_("Message %d is marked as unignore thread\n"),
3999             msginfo->msgnum);
4000 }
4001
4002 static void summary_unignore_thread(SummaryView *summaryview)
4003 {
4004         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
4005         GList *cur;
4006
4007         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
4008                 gtk_ctree_pre_recursive(ctree, GTK_CTREE_NODE(cur->data), GTK_CTREE_FUNC(summary_unignore_thread_func), summaryview);
4009         }
4010
4011         summary_status_show(summaryview);
4012 }
4013