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