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