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