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