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