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