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