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