51e8c265dab8b724dd54b3ce8894abab43f23f31
[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 = gtk_entry_get_text(GTK_ENTRY(summaryview->search_string));
900                 gchar *searched_header = NULL;
901                 
902                 not_killed = NULL;
903                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
904                         MsgInfo * msginfo = (MsgInfo *) cur->data;
905
906                         switch (search_type) {
907                         case S_SEARCH_SUBJECT:
908                                 searched_header = msginfo->subject;
909                                 break;
910                         case S_SEARCH_FROM:
911                                 searched_header = msginfo->from;
912                                 break;
913                         case S_SEARCH_TO:
914                                 searched_header = msginfo->to;
915                                 break;
916                         default:
917                                 debug_print("unknown search type (%d)\n", search_type);
918                                 break;
919                         }
920                         if (searched_header && strcasestr(searched_header, search_string) != NULL)
921                                 not_killed = g_slist_append(not_killed, msginfo);
922                         else
923                                 procmsg_msginfo_free(msginfo);
924                 }
925                 g_slist_free(mlist);
926                 mlist = not_killed;
927         }
928         
929         if ((global_scoring || item->prefs->scoring)) {
930                 GSList *not_killed;
931                 gint kill_score;
932
933                 not_killed = NULL;
934                 kill_score = prefs_common.kill_score;
935                 if (item->prefs->kill_score > kill_score)
936                         kill_score = item->prefs->kill_score;
937                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
938                         MsgInfo * msginfo = (MsgInfo *) cur->data;
939
940                         if (msginfo->score > kill_score)
941                                 not_killed = g_slist_append(not_killed, msginfo);
942                         else
943                                 procmsg_msginfo_free(msginfo);
944                 }
945                 g_slist_free(mlist);
946                 mlist = not_killed;
947         }
948
949         STATUSBAR_POP(summaryview->mainwin);
950
951         /* set ctree and hash table from the msginfo list
952            creating thread, and count the number of messages */
953         summary_set_ctree_from_list(summaryview, mlist);
954
955         g_slist_free(mlist);
956
957         if (summaryview->sort_key != SORT_BY_NONE)
958                 summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
959
960         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
961
962         gtk_clist_thaw(GTK_CLIST(ctree));
963
964         if (is_refresh) {
965                 summaryview->displayed =
966                         summary_find_msg_by_msgnum(summaryview,
967                                                    displayed_msgnum);
968                 if (!summaryview->displayed)
969                         messageview_clear(summaryview->messageview);
970                 summary_select_by_msgnum(summaryview, selected_msgnum);
971                 if (!summaryview->selected) {
972                         /* no selected message - select first unread
973                            message, but do not display it */
974                         node = summary_find_next_flagged_msg(summaryview, NULL,
975                                                              MSG_UNREAD, FALSE);
976                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
977                                 node = gtk_ctree_node_nth
978                                         (ctree,
979                                          item->sort_type == SORT_DESCENDING
980                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
981                         summary_select_node(summaryview, node, FALSE, TRUE);
982                 }
983         } else {
984                 /* select first unread message */
985                 if (summaryview->sort_key == SORT_BY_SCORE)
986                         node = summary_find_next_important_score(summaryview,
987                                                                  NULL);
988                 else
989                 node = summary_find_next_flagged_msg(summaryview, NULL,
990                                                      MSG_UNREAD, FALSE);
991                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
992                         node = gtk_ctree_node_nth
993                                 (ctree,
994                                  item->sort_type == SORT_DESCENDING
995                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
996                 }
997                 if (prefs_common.open_unread_on_enter) {
998                         summary_unlock(summaryview);
999                         summary_select_node(summaryview, node, TRUE, TRUE);
1000                         summary_lock(summaryview);
1001                 } else
1002                         summary_select_node(summaryview, node, FALSE, TRUE);
1003         }
1004
1005         summary_status_show(summaryview);
1006         summary_set_menu_sensitive(summaryview);
1007         toolbar_set_sensitive(summaryview->mainwin);
1008
1009         debug_print("\n");
1010         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1011
1012         main_window_cursor_normal(summaryview->mainwin);
1013         summary_unlock(summaryview);
1014         inc_unlock();
1015
1016         return TRUE;
1017 }
1018
1019 void summary_clear_list(SummaryView *summaryview)
1020 {
1021         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1022         gint optimal_width;
1023
1024         gtk_clist_freeze(clist);
1025
1026         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1027                                 NULL, summary_free_msginfo_func, NULL);
1028
1029         if (summaryview->folder_item) {
1030                 summaryview->folder_item->opened = FALSE;
1031                 summaryview->folder_item = NULL;
1032         }
1033
1034         summaryview->display_msg = FALSE;
1035
1036         summaryview->selected  = NULL;
1037         summaryview->displayed = NULL;
1038         summaryview->newmsgs   = summaryview->unread     = 0;
1039         summaryview->messages  = summaryview->total_size = 0;
1040         summaryview->deleted   = summaryview->moved      = 0;
1041         summaryview->copied    = 0;
1042         if (summaryview->msgid_table) {
1043                 g_hash_table_destroy(summaryview->msgid_table);
1044                 summaryview->msgid_table = NULL;
1045         }
1046         if (summaryview->subject_table) {
1047                 g_hash_table_destroy(summaryview->subject_table);
1048                 summaryview->subject_table = NULL;
1049         }
1050         summaryview->mlist = NULL;
1051         if (summaryview->folder_table) {
1052                 g_hash_table_destroy(summaryview->folder_table);
1053                 summaryview->folder_table = NULL;
1054         }
1055
1056         gtk_clist_clear(clist);
1057         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1058                 optimal_width = gtk_clist_optimal_column_width
1059                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1060                 gtk_clist_set_column_width
1061                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1062                          optimal_width);
1063         }
1064
1065         gtk_clist_thaw(clist);
1066 }
1067
1068 void summary_clear_all(SummaryView *summaryview)
1069 {
1070         summary_clear_list(summaryview);
1071         summary_set_menu_sensitive(summaryview);
1072         toolbar_set_sensitive(summaryview->mainwin);
1073         summary_status_show(summaryview);
1074 }
1075
1076 void summary_lock(SummaryView *summaryview)
1077 {
1078         summaryview->lock_count++;
1079 }
1080
1081 void summary_unlock(SummaryView *summaryview)
1082 {
1083         if (summaryview->lock_count)
1084                 summaryview->lock_count--;
1085 }
1086
1087 gboolean summary_is_locked(SummaryView *summaryview)
1088 {
1089         return summaryview->lock_count > 0;
1090 }
1091
1092 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1093 {
1094         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1095         SummarySelection selection;
1096
1097         if (!clist->row_list)
1098                 selection = SUMMARY_NONE;
1099         else if (!clist->selection)
1100                 selection = SUMMARY_SELECTED_NONE;
1101         else if (!clist->selection->next)
1102                 selection = SUMMARY_SELECTED_SINGLE;
1103         else
1104                 selection = SUMMARY_SELECTED_MULTIPLE;
1105
1106         return selection;
1107 }
1108
1109 static void summary_set_menu_sensitive(SummaryView *summaryview)
1110 {
1111         GtkItemFactory *ifactory = summaryview->popupfactory;
1112         SummarySelection selection;
1113         GtkWidget *menuitem;
1114         gboolean sens;
1115
1116         selection = summary_get_selection_type(summaryview);
1117         main_window_set_menu_sensitive(summaryview->mainwin);
1118
1119         if (selection == SUMMARY_NONE) {
1120                 GtkWidget *submenu;
1121
1122                 submenu = gtk_item_factory_get_widget
1123                         (summaryview->popupfactory, "/Mark");
1124                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1125                 menu_set_insensitive_all
1126                         (GTK_MENU_SHELL(summaryview->popupmenu));
1127                 return;
1128         }
1129
1130         if (summaryview->folder_item->folder->type != F_NEWS)
1131                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1132         else
1133                 menu_set_sensitive(ifactory, "/Move...", FALSE);
1134
1135         menu_set_sensitive(ifactory, "/Delete", TRUE);
1136         menu_set_sensitive(ifactory, "/Select all", TRUE);
1137         menu_set_sensitive(ifactory, "/Copy...", TRUE);
1138         menu_set_sensitive(ifactory, "/Execute", TRUE);
1139
1140         menu_set_sensitive(ifactory, "/Mark", TRUE);
1141         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1142         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1143
1144         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1145         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1146         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1147         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1148         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1149
1150         menu_set_sensitive(ifactory, "/Color label", TRUE);
1151
1152         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1153         menu_set_sensitive(ifactory, "/Reply",                    sens);
1154         menu_set_sensitive(ifactory, "/Reply to sender",          sens);
1155         menu_set_sensitive(ifactory, "/Reply to all",             sens);
1156         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1157         menu_set_sensitive(ifactory, "/Redirect",                 sens);
1158
1159         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1160         menu_set_sensitive(ifactory, "/Create filter rule",         sens);
1161
1162         menu_set_sensitive(ifactory, "/View", sens);
1163         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1164         menu_set_sensitive(ifactory, "/View/Source", sens);
1165         menu_set_sensitive(ifactory, "/View/All header", sens);
1166         if (summaryview->folder_item->stype == F_OUTBOX ||
1167             summaryview->folder_item->stype == F_DRAFT  ||
1168             summaryview->folder_item->stype == F_QUEUE)
1169                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1170         else
1171                 menu_set_sensitive(ifactory, "/Re-edit", FALSE);
1172
1173         menu_set_sensitive(ifactory, "/Select thread", sens);
1174         menu_set_sensitive(ifactory, "/Save as...", sens);
1175         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1176
1177         if (summaryview->folder_item->folder->account)
1178                 sens = summaryview->folder_item->folder->account->protocol
1179                         == A_NNTP;
1180         else
1181                 sens = FALSE;
1182         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1183         summary_lock(summaryview);
1184         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1185         gtk_check_menu_item_set_active
1186                 (GTK_CHECK_MENU_ITEM(menuitem),
1187                  summaryview->messageview->textview->show_all_headers);
1188         summary_unlock(summaryview);
1189 }
1190
1191 void summary_select_prev_unread(SummaryView *summaryview)
1192 {
1193         GtkCTreeNode *node;
1194
1195         node = summary_find_prev_flagged_msg
1196                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1197
1198         if (!node) {
1199                 AlertValue val = 0;
1200
1201                 switch (prefs_common.next_unread_msg_dialog) {
1202                         case NEXTUNREADMSGDIALOG_ALWAYS:
1203                                 val = alertpanel(_("No more unread messages"),
1204                                                  _("No unread message found. "
1205                                                    "Search from the end?"),
1206                                                  _("Yes"), _("No"), NULL);
1207                                 break;
1208                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1209                                 val = G_ALERTDEFAULT;
1210                                 break;
1211                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1212                                 val = !G_ALERTDEFAULT;
1213                                 break;
1214                         default:
1215                                 debug_print(
1216                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1217                 }
1218                 if (val != G_ALERTDEFAULT) return;
1219                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1220                                                      MSG_UNREAD, FALSE);
1221         }
1222
1223         if (!node)
1224                 alertpanel_notice(_("No unread messages."));
1225         else
1226                 summary_select_node(summaryview, node, TRUE, FALSE);
1227 }
1228
1229 void summary_select_next_unread(SummaryView *summaryview)
1230 {
1231         GtkCTreeNode *node = summaryview->selected;
1232         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1233
1234         node = summary_find_next_flagged_msg
1235                 (summaryview, node, MSG_UNREAD, FALSE);
1236         
1237         if (node)
1238                 summary_select_node(summaryview, node, TRUE, FALSE);
1239         else {
1240                 node = summary_find_next_flagged_msg
1241                         (summaryview, NULL, MSG_UNREAD, FALSE);
1242                 if (node == NULL) {
1243                         AlertValue val = 0;
1244
1245                         switch (prefs_common.next_unread_msg_dialog) {
1246                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1247                                         val = alertpanel(_("No more unread messages"),
1248                                                          _("No unread message found. "
1249                                                            "Go to next folder?"),
1250                                                          _("Yes"), _("No"), NULL);
1251                                         break;
1252                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1253                                         val = G_ALERTDEFAULT;
1254                                         break;
1255                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1256                                         val = G_ALERTOTHER;
1257                                         break;
1258                                 default:
1259                                         debug_print(
1260                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1261                         }
1262
1263                         if (val == G_ALERTDEFAULT) {
1264                                 if (gtk_signal_n_emissions_by_name
1265                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1266                                                 gtk_signal_emit_stop_by_name
1267                                                         (GTK_OBJECT(ctree),
1268                                                          "key_press_event");
1269                                 folderview_select_next_unread(summaryview->folderview);
1270                                 return;
1271                         } 
1272                         else
1273                                 return;
1274                 } else
1275                         summary_select_node(summaryview, node, TRUE, FALSE);
1276
1277         }
1278 }
1279
1280 void summary_select_prev_new(SummaryView *summaryview)
1281 {
1282         GtkCTreeNode *node;
1283
1284         node = summary_find_prev_flagged_msg
1285                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1286
1287         if (!node) {
1288                 AlertValue val;
1289
1290                 val = alertpanel(_("No more new messages"),
1291                                  _("No new message found. "
1292                                    "Search from the end?"),
1293                                  _("Yes"), _("No"), NULL);
1294                 if (val != G_ALERTDEFAULT) return;
1295                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1296                                                      MSG_NEW, FALSE);
1297         }
1298
1299         if (!node)
1300                 alertpanel_notice(_("No new messages."));
1301         else
1302                 summary_select_node(summaryview, node, TRUE, FALSE);
1303 }
1304
1305 void summary_select_next_new(SummaryView *summaryview)
1306 {
1307         GtkCTreeNode *node = summaryview->selected;
1308         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1309
1310         while ((node = summary_find_next_flagged_msg
1311                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1312                 AlertValue val;
1313
1314                 val = alertpanel(_("No more new messages"),
1315                                  _("No new message found. "
1316                                    "Go to next folder?"),
1317                                  _("Yes"), _("Search again"), _("No"));
1318                 if (val == G_ALERTDEFAULT) {
1319                         if (gtk_signal_n_emissions_by_name
1320                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1321                                         gtk_signal_emit_stop_by_name
1322                                                 (GTK_OBJECT(ctree),
1323                                                  "key_press_event");
1324                         folderview_select_next_unread(summaryview->folderview);
1325                         return;
1326                 } else if (val == G_ALERTALTERNATE)
1327                         node = NULL;
1328                 else
1329                         return;
1330         }
1331
1332         if (node)
1333                 summary_select_node(summaryview, node, TRUE, FALSE);
1334 }
1335
1336 void summary_select_prev_marked(SummaryView *summaryview)
1337 {
1338         GtkCTreeNode *node;
1339
1340         node = summary_find_prev_flagged_msg
1341                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1342
1343         if (!node) {
1344                 AlertValue val;
1345
1346                 val = alertpanel(_("No more marked messages"),
1347                                  _("No marked message found. "
1348                                    "Search from the end?"),
1349                                  _("Yes"), _("No"), NULL);
1350                 if (val != G_ALERTDEFAULT) return;
1351                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1352                                                      MSG_MARKED, TRUE);
1353         }
1354
1355         if (!node)
1356                 alertpanel_notice(_("No marked messages."));
1357         else
1358                 summary_select_node(summaryview, node, TRUE, FALSE);
1359 }
1360
1361 void summary_select_next_marked(SummaryView *summaryview)
1362 {
1363         GtkCTreeNode *node;
1364
1365         node = summary_find_next_flagged_msg
1366                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1367
1368         if (!node) {
1369                 AlertValue val;
1370
1371                 val = alertpanel(_("No more marked messages"),
1372                                  _("No marked message found. "
1373                                    "Search from the beginning?"),
1374                                  _("Yes"), _("No"), NULL);
1375                 if (val != G_ALERTDEFAULT) return;
1376                 node = summary_find_next_flagged_msg(summaryview, NULL,
1377                                                      MSG_MARKED, TRUE);
1378         }
1379
1380         if (!node)
1381                 alertpanel_notice(_("No marked messages."));
1382         else
1383                 summary_select_node(summaryview, node, TRUE, FALSE);
1384 }
1385
1386 void summary_select_prev_labeled(SummaryView *summaryview)
1387 {
1388         GtkCTreeNode *node;
1389
1390         node = summary_find_prev_flagged_msg
1391                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1392
1393         if (!node) {
1394                 AlertValue val;
1395
1396                 val = alertpanel(_("No more labeled messages"),
1397                                  _("No labeled message found. "
1398                                    "Search from the end?"),
1399                                  _("Yes"), _("No"), NULL);
1400                 if (val != G_ALERTDEFAULT) return;
1401                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1402                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1403         }
1404
1405         if (!node)
1406                 alertpanel_notice(_("No labeled messages."));
1407         else
1408                 summary_select_node(summaryview, node, TRUE, FALSE);
1409 }
1410
1411 void summary_select_next_labeled(SummaryView *summaryview)
1412 {
1413         GtkCTreeNode *node;
1414
1415         node = summary_find_next_flagged_msg
1416                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1417
1418         if (!node) {
1419                 AlertValue val;
1420
1421                 val = alertpanel(_("No more labeled messages"),
1422                                  _("No labeled message found. "
1423                                    "Search from the beginning?"),
1424                                  _("Yes"), _("No"), NULL);
1425                 if (val != G_ALERTDEFAULT) return;
1426                 node = summary_find_next_flagged_msg(summaryview, NULL,
1427                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1428         }
1429
1430         if (!node)
1431                 alertpanel_notice(_("No labeled messages."));
1432         else
1433                 summary_select_node(summaryview, node, TRUE, FALSE);
1434 }
1435
1436 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1437 {
1438         GtkCTreeNode *node;
1439
1440         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1441         summary_select_node(summaryview, node, FALSE, TRUE);
1442 }
1443
1444 /**
1445  * summary_select_node:
1446  * @summaryview: Summary view.
1447  * @node: Summary tree node.
1448  * @display_msg: TRUE to display the selected message.
1449  * @do_refresh: TRUE to refresh the widget.
1450  *
1451  * Select @node (bringing it into view by scrolling and expanding its
1452  * thread, if necessary) and unselect all others.  If @display_msg is
1453  * TRUE, display the corresponding message in the message view.
1454  * If @do_refresh is TRUE, the widget is refreshed.
1455  **/
1456 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1457                          gboolean display_msg, gboolean do_refresh)
1458 {
1459         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1460
1461         if (node) {
1462                 gtkut_ctree_expand_parent_all(ctree, node);
1463                 if (do_refresh) {
1464                         GTK_EVENTS_FLUSH();
1465                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1466                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1467                 }
1468                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1469                 if (display_msg && summaryview->displayed == node)
1470                         summaryview->displayed = NULL;
1471                 summaryview->display_msg = display_msg;
1472                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1473         }
1474 }
1475
1476 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1477 {
1478         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1479         MsgInfo *msginfo;
1480
1481         if (!node)
1482                 return 0;
1483         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1484         return msginfo->msgnum;
1485 }
1486
1487 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1488                                            GtkCTreeNode *current_node)
1489 {
1490         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1491         GtkCTreeNode *node;
1492         MsgInfo *msginfo;
1493
1494         if (current_node)
1495                 node = current_node;
1496         else
1497                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1498
1499         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1500                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1501                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1502         }
1503
1504         return node;
1505 }
1506
1507 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1508                                            GtkCTreeNode *current_node)
1509 {
1510         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1511         GtkCTreeNode *node;
1512         MsgInfo *msginfo;
1513
1514         if (current_node)
1515                 node = current_node;
1516         else
1517                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1518
1519         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1520                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1521                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1522         }
1523
1524         return node;
1525 }
1526
1527 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1528                                                    GtkCTreeNode *current_node,
1529                                                    MsgPermFlags flags,
1530                                                    gboolean start_from_prev)
1531 {
1532         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1533         GtkCTreeNode *node;
1534         MsgInfo *msginfo;
1535
1536         if (current_node) {
1537                 if (start_from_prev)
1538                         node = GTK_CTREE_NODE_PREV(current_node);
1539                 else
1540                         node = current_node;
1541         } else
1542                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1543
1544         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1545                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1546                 if ((msginfo->flags.perm_flags & flags) != 0) break;
1547         }
1548
1549         return node;
1550 }
1551
1552 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1553                                                    GtkCTreeNode *current_node,
1554                                                    MsgPermFlags flags,
1555                                                    gboolean start_from_next)
1556 {
1557         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1558         GtkCTreeNode *node;
1559         MsgInfo *msginfo;
1560
1561         if (current_node) {
1562                 if (start_from_next)
1563                         node = gtkut_ctree_node_next(ctree, current_node);
1564                 else
1565                         node = current_node;
1566         } else
1567                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1568
1569         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1570                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1571                 /* Find msg with matching flags but ignore messages with
1572                    ignore flags, if searching for new or unread messages */
1573                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1574                         ((msginfo->flags.perm_flags & flags) != 0))
1575                         break;
1576         }
1577
1578         return node;
1579 }
1580
1581 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1582                                                 guint msgnum)
1583 {
1584         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1585         GtkCTreeNode *node;
1586         MsgInfo *msginfo;
1587
1588         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1589
1590         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1591                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1592                 if (msginfo->msgnum == msgnum) break;
1593         }
1594
1595         return node;
1596 }
1597
1598 static guint attract_hash_func(gconstpointer key)
1599 {
1600         gchar *str;
1601         gchar *p;
1602         guint h;
1603
1604         Xstrdup_a(str, (const gchar *)key, return 0);
1605         trim_subject(str);
1606
1607         p = str;
1608         h = *p;
1609
1610         if (h) {
1611                 for (p += 1; *p != '\0'; p++)
1612                         h = (h << 5) - h + *p;
1613         }
1614
1615         return h;
1616 }
1617
1618 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1619 {
1620         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1621 }
1622
1623 void summary_attract_by_subject(SummaryView *summaryview)
1624 {
1625         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1626         GtkCList *clist = GTK_CLIST(ctree);
1627         GtkCTreeNode *src_node;
1628         GtkCTreeNode *dst_node, *sibling;
1629         GtkCTreeNode *tmp;
1630         MsgInfo *src_msginfo, *dst_msginfo;
1631         GHashTable *subject_table;
1632
1633         debug_print("Attracting messages by subject...");
1634         STATUSBAR_PUSH(summaryview->mainwin,
1635                        _("Attracting messages by subject..."));
1636
1637         main_window_cursor_wait(summaryview->mainwin);
1638         gtk_clist_freeze(clist);
1639
1640         subject_table = g_hash_table_new(attract_hash_func,
1641                                          attract_compare_func);
1642
1643         for (src_node = GTK_CTREE_NODE(clist->row_list);
1644              src_node != NULL;
1645              src_node = tmp) {
1646                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1647                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1648                 if (!src_msginfo) continue;
1649                 if (!src_msginfo->subject) continue;
1650
1651                 /* find attracting node */
1652                 dst_node = g_hash_table_lookup(subject_table,
1653                                                src_msginfo->subject);
1654
1655                 if (dst_node) {
1656                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1657
1658                         /* if the time difference is more than 20 days,
1659                            don't attract */
1660                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1661                             > 60 * 60 * 24 * 20)
1662                                 continue;
1663
1664                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1665                         if (src_node != sibling)
1666                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1667                 }
1668
1669                 g_hash_table_insert(subject_table,
1670                                     src_msginfo->subject, src_node);
1671         }
1672
1673         g_hash_table_destroy(subject_table);
1674
1675         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1676
1677         gtk_clist_thaw(clist);
1678
1679         debug_print("done.\n");
1680         STATUSBAR_POP(summaryview->mainwin);
1681
1682         main_window_cursor_normal(summaryview->mainwin);
1683 }
1684
1685 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1686                                       gpointer data)
1687 {
1688         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1689
1690         if (msginfo)
1691                 procmsg_msginfo_free(msginfo);
1692 }
1693
1694 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1695                                    gpointer data)
1696 {
1697         SummaryView *summaryview = data;
1698         MsgInfo *msginfo;
1699
1700         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1701
1702         if (MSG_IS_NEWS(msginfo->flags))
1703                 news_flag_crosspost(msginfo);
1704
1705         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1706                 summaryview->newmsgs++;
1707         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1708                 summaryview->unread++;
1709         if (MSG_IS_DELETED(msginfo->flags))
1710                 summaryview->deleted++;
1711
1712         summaryview->messages++;
1713         summaryview->total_size += msginfo->size;
1714
1715         summary_set_row_marks(summaryview, node);
1716 }
1717
1718 static void summary_update_status(SummaryView *summaryview)
1719 {
1720         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1721         GtkCTreeNode *node;
1722         MsgInfo *msginfo;
1723
1724         summaryview->newmsgs = summaryview->unread =
1725         summaryview->messages = summaryview->total_size =
1726         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1727
1728         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1729              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1730                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1731
1732                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1733                         summaryview->newmsgs++;
1734                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1735                         summaryview->unread++;
1736                 if (MSG_IS_DELETED(msginfo->flags))
1737                         summaryview->deleted++;
1738                 if (MSG_IS_MOVE(msginfo->flags))
1739                         summaryview->moved++;
1740                 if (MSG_IS_COPY(msginfo->flags))
1741                         summaryview->copied++;
1742                 summaryview->messages++;
1743                 summaryview->total_size += msginfo->size;
1744         }
1745 }
1746
1747 static void summary_status_show(SummaryView *summaryview)
1748 {
1749         gchar *str;
1750         gchar *del, *mv, *cp;
1751         gchar *sel;
1752         gchar *spc;
1753         gchar *itstr;
1754         GList *rowlist, *cur;
1755         guint n_selected = 0;
1756         off_t sel_size = 0;
1757         MsgInfo *msginfo;
1758
1759         if (!summaryview->folder_item) {
1760                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1761                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1762                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1763                 return;
1764         }
1765
1766         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1767         for (cur = rowlist; cur != NULL; cur = cur->next) {
1768                 msginfo = gtk_ctree_node_get_row_data
1769                         (GTK_CTREE(summaryview->ctree),
1770                          GTK_CTREE_NODE(cur->data));
1771                 sel_size += msginfo->size;
1772                 n_selected++;
1773         }
1774
1775         if (summaryview->folder_item->folder->type == F_NEWS &&
1776             prefs_common.ng_abbrev_len < strlen(summaryview->folder_item->path)) {
1777                 gchar *group;
1778                 group = get_abbrev_newsgroup_name
1779                         (g_basename(summaryview->folder_item->path));
1780                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), group);
1781                 g_free(group);
1782         } else {
1783                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1784                               summaryview->folder_item->path);
1785         }
1786
1787         if (summaryview->deleted)
1788                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1789         else
1790                 del = g_strdup("");
1791         if (summaryview->moved)
1792                 mv = g_strdup_printf(_("%s%d moved"),
1793                                      summaryview->deleted ? _(", ") : "",
1794                                      summaryview->moved);
1795         else
1796                 mv = g_strdup("");
1797         if (summaryview->copied)
1798                 cp = g_strdup_printf(_("%s%d copied"),
1799                                      summaryview->deleted ||
1800                                      summaryview->moved ? _(", ") : "",
1801                                      summaryview->copied);
1802         else
1803                 cp = g_strdup("");
1804
1805         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1806                 spc = "    ";
1807         else
1808                 spc = "";
1809
1810         if (n_selected) {
1811                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1812                 if (n_selected == 1)
1813                         itstr = g_strdup(_(" item selected"));
1814                 else
1815                         itstr = g_strdup(_(" items selected"));
1816         } else {
1817                 sel = g_strdup("");
1818                 itstr = g_strdup("");
1819         }
1820                 
1821         str = g_strconcat(n_selected ? itos(n_selected) : "",
1822                                         itstr, sel, spc, del, mv, cp, NULL);
1823         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1824         g_free(str);
1825         g_free(sel);
1826         g_free(del);
1827         g_free(mv);
1828         g_free(cp);
1829         g_free(itstr);
1830
1831         if (FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1832                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1833                                       summaryview->newmsgs,
1834                                       summaryview->unread,
1835                                       summaryview->messages,
1836                                       to_human_readable(summaryview->total_size));
1837         } else {
1838                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1839                                       summaryview->newmsgs,
1840                                       summaryview->unread,
1841                                       summaryview->messages);
1842         }
1843         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1844         g_free(str);
1845 }
1846
1847 static void summary_set_column_titles(SummaryView *summaryview)
1848 {
1849         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1850         GtkWidget *hbox;
1851         GtkWidget *label;
1852         GtkWidget *arrow;
1853         gint pos;
1854         const gchar *title;
1855         SummaryColumnType type;
1856         gboolean single_char;
1857         GtkJustification justify;
1858
1859         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1860                 SORT_BY_MARK,
1861                 SORT_BY_UNREAD,
1862                 SORT_BY_MIME,
1863                 SORT_BY_SUBJECT,
1864                 SORT_BY_FROM,
1865                 SORT_BY_DATE,
1866                 SORT_BY_SIZE,
1867                 SORT_BY_NUMBER,
1868                 SORT_BY_SCORE,
1869                 SORT_BY_LOCKED
1870         };
1871
1872         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1873                 type = summaryview->col_state[pos].type;
1874
1875                 /* CLAWS: mime and unread are single char headers */
1876                 single_char = (type == S_COL_MIME || type == S_COL_UNREAD);
1877                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1878                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1879
1880                 switch (type) {
1881                 case S_COL_SUBJECT:
1882                 case S_COL_FROM:
1883                 case S_COL_DATE:
1884                 case S_COL_NUMBER:
1885                         if (prefs_common.trans_hdr)
1886                                 title = gettext(col_label[type]);
1887                         else
1888                                 title = col_label[type];
1889                         break;
1890                 /* CLAWS: dummies for mark and locked headers */        
1891                 case S_COL_MARK:        
1892                 case S_COL_LOCKED:
1893                         title = "";
1894                         break;
1895                 default:
1896                         title = gettext(col_label[type]);
1897                 }
1898
1899                 if (type == S_COL_MIME) {
1900                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1901                         gtk_widget_show(label);
1902                         gtk_clist_set_column_widget(clist, pos, label);
1903                         continue;
1904                 }
1905                 if (single_char) {
1906                         gtk_clist_set_column_title(clist, pos, title);
1907                         continue;
1908                 }
1909
1910                 /* CLAWS: changed so that locked and mark headers
1911                  * show a pixmap instead of single character */
1912                 hbox  = gtk_hbox_new(FALSE, 4);
1913                 
1914                 if (type == S_COL_LOCKED)
1915                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1916                 else if (type == S_COL_MARK) 
1917                         label = gtk_pixmap_new(markxpm, markxpmmask);
1918                 else 
1919                         label = gtk_label_new(title);
1920                 
1921                 if (justify == GTK_JUSTIFY_RIGHT)
1922                         gtk_box_pack_end(GTK_BOX(hbox), label,
1923                                          FALSE, FALSE, 0);
1924                 else
1925                         gtk_box_pack_start(GTK_BOX(hbox), label,
1926                                            FALSE, FALSE, 0);
1927
1928                 if (summaryview->sort_key == sort_by[type]) {
1929                         arrow = gtk_arrow_new
1930                                 (summaryview->sort_type == SORT_ASCENDING
1931                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1932                                  GTK_SHADOW_IN);
1933                         if (justify == GTK_JUSTIFY_RIGHT)
1934                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1935                                                    FALSE, FALSE, 0);
1936                         else
1937                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1938                                                  FALSE, FALSE, 0);
1939                 }
1940
1941                 gtk_widget_show_all(hbox);
1942                 gtk_clist_set_column_widget(clist, pos, hbox);
1943         }
1944 }
1945
1946 void summary_sort(SummaryView *summaryview,
1947                   FolderSortKey sort_key, FolderSortType sort_type)
1948 {
1949         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1950         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1951         GtkCListCompareFunc cmp_func = NULL;
1952
1953         switch (sort_key) {
1954         case SORT_BY_MARK:
1955                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
1956                 break;
1957         case SORT_BY_UNREAD:
1958                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_unread;
1959                 break;
1960         case SORT_BY_MIME:
1961                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
1962                 break;
1963         case SORT_BY_NUMBER:
1964                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1965                 break;
1966         case SORT_BY_SIZE:
1967                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1968                 break;
1969         case SORT_BY_DATE:
1970                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1971                 break;
1972         case SORT_BY_FROM:
1973                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1974                 break;
1975         case SORT_BY_SUBJECT:
1976                 if (summaryview->simplify_subject_preg)
1977                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
1978                 else
1979                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1980                 break;
1981         case SORT_BY_SCORE:
1982                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1983                 break;
1984         case SORT_BY_LOCKED:
1985                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
1986                 break;
1987         case SORT_BY_LABEL:
1988                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
1989                 break;
1990         case SORT_BY_NONE:
1991                 cmp_func = NULL;
1992                 return;
1993         default:
1994                 return;
1995         }
1996
1997         summaryview->sort_key = sort_key;
1998         summaryview->sort_type = sort_type;
1999
2000         summary_set_column_titles(summaryview);
2001         summary_set_menu_sensitive(summaryview);
2002         if(cmp_func != NULL) {
2003                 debug_print("Sorting summary...");
2004                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2005
2006                 main_window_cursor_wait(summaryview->mainwin);
2007
2008                 gtk_clist_set_compare_func(clist, cmp_func);
2009
2010                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2011
2012                 gtk_sctree_sort_recursive(ctree, NULL);
2013
2014                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2015
2016                 main_window_cursor_normal(summaryview->mainwin);
2017
2018                 debug_print("done.\n");
2019                 STATUSBAR_POP(summaryview->mainwin);
2020         }
2021 }
2022
2023 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2024                                    GtkCTreeNode *cnode, gpointer data)
2025 {
2026         SummaryView *summaryview = (SummaryView *)data;
2027         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2028         gchar *text[N_SUMMARY_COLS];
2029         gint *col_pos = summaryview->col_pos;
2030         const gchar *msgid = msginfo->msgid;
2031         GHashTable *msgid_table = summaryview->msgid_table;
2032
2033         summary_set_header(summaryview, text, msginfo);
2034
2035         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2036                                 NULL, NULL, NULL, NULL, FALSE,
2037                                 gnode->parent->parent ? TRUE : FALSE);
2038 #define SET_TEXT(col) \
2039         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2040                                 text[col_pos[col]])
2041
2042         SET_TEXT(S_COL_NUMBER);
2043         SET_TEXT(S_COL_SCORE);
2044         SET_TEXT(S_COL_SIZE);
2045         SET_TEXT(S_COL_DATE);
2046         SET_TEXT(S_COL_FROM);
2047         SET_TEXT(S_COL_SUBJECT);
2048
2049 #undef SET_TEXT
2050
2051         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2052         summary_set_marks_func(ctree, cnode, summaryview);
2053
2054         if (msgid && msgid[0] != '\0')
2055                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2056
2057         return TRUE;
2058 }
2059
2060 static void summary_set_ctree_from_list(SummaryView *summaryview,
2061                                         GSList *mlist)
2062 {
2063         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2064         MsgInfo *msginfo;
2065                 GtkCTreeNode *node = NULL;
2066         GHashTable *msgid_table;
2067         GHashTable *subject_table;
2068         GSList * cur;
2069         
2070         if (!mlist) return;
2071
2072         debug_print("\tSetting summary from message data...");
2073         STATUSBAR_PUSH(summaryview->mainwin,
2074                        _("Setting summary from message data..."));
2075         gdk_flush();
2076
2077         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2078         summaryview->msgid_table = msgid_table;
2079         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2080         summaryview->subject_table = subject_table;
2081
2082         if (prefs_common.use_addr_book)
2083                 start_address_completion();
2084         
2085         for (cur = mlist ; cur != NULL; cur = cur->next) {
2086                 msginfo = (MsgInfo *)cur->data;
2087                 msginfo->threadscore = msginfo->score;
2088         }
2089
2090         if (global_scoring || summaryview->folder_item->prefs->scoring) {
2091                 summaryview->important_score = prefs_common.important_score;
2092                 if (summaryview->folder_item->prefs->important_score >
2093                     summaryview->important_score)
2094                         summaryview->important_score =
2095                                 summaryview->folder_item->prefs->important_score;
2096         }
2097
2098         if (summaryview->threaded) {
2099                 GNode *root, *gnode;
2100
2101                 root = procmsg_get_thread_tree(mlist);
2102
2103                 for (gnode = root->children; gnode != NULL;
2104                      gnode = gnode->next) {
2105                         node = gtk_ctree_insert_gnode
2106                                 (ctree, NULL, node, gnode,
2107                                  summary_insert_gnode_func, summaryview);
2108                 }
2109
2110                 g_node_destroy(root);
2111
2112                 summary_thread_init(summaryview);
2113         } else {
2114                 gchar *text[N_SUMMARY_COLS];
2115
2116                 mlist = g_slist_reverse(mlist);
2117                 for (; mlist != NULL; mlist = mlist->next) {
2118                         msginfo = (MsgInfo *)mlist->data;
2119
2120                         summary_set_header(summaryview, text, msginfo);
2121
2122                         node = gtk_ctree_insert_node
2123                                 (ctree, NULL, node, text, 2,
2124                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2125                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2126                         summary_set_marks_func(ctree, node, summaryview);
2127
2128                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2129                                 g_hash_table_insert(msgid_table,
2130                                                     msginfo->msgid, node);
2131
2132                         subject_table_insert(subject_table,
2133                                              msginfo->subject,
2134                                              node);
2135                 }
2136                 mlist = g_slist_reverse(mlist);
2137         }
2138
2139         if (prefs_common.enable_hscrollbar &&
2140             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2141                 gint optimal_width;
2142
2143                 optimal_width = gtk_clist_optimal_column_width
2144                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2145                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2146                                            summaryview->col_pos[S_COL_SUBJECT],
2147                                            optimal_width);
2148         }
2149
2150         if (prefs_common.use_addr_book)
2151                 end_address_completion();
2152
2153         debug_print("done.\n");
2154         STATUSBAR_POP(summaryview->mainwin);
2155         if (debug_mode) {
2156                 debug_print("\tmsgid hash table size = %d\n",
2157                             g_hash_table_size(msgid_table));
2158                 debug_print("\tsubject hash table size = %d\n",
2159                             g_hash_table_size(subject_table));
2160         }
2161 }
2162
2163 static gchar *summary_complete_address(const gchar *addr)
2164 {
2165         gint count;
2166         gchar *res, *tmp, *email_addr;
2167
2168         Xstrdup_a(email_addr, addr, return NULL);
2169         extract_address(email_addr);
2170         g_return_val_if_fail(*email_addr, NULL);
2171
2172         /*
2173          * completion stuff must be already initialized
2174          */
2175         res = NULL;
2176         if (1 < (count = complete_address(email_addr))) {
2177                 tmp = get_complete_address(1);
2178                 res = procheader_get_fromname(tmp);
2179                 g_free(tmp);    
2180         }
2181         
2182         return res;
2183 }
2184
2185 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2186                                MsgInfo *msginfo)
2187 {
2188         static gchar date_modified[80];
2189         static gchar *to = NULL;
2190         static gchar col_score[11];
2191         static gchar buf[BUFFSIZE];
2192         gint *col_pos = summaryview->col_pos;
2193
2194         text[col_pos[S_COL_MARK]]   = NULL;
2195         text[col_pos[S_COL_UNREAD]] = NULL;
2196         text[col_pos[S_COL_MIME]]   = NULL;
2197         text[col_pos[S_COL_LOCKED]] = NULL;
2198         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2199         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2200         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2201
2202         if (msginfo->date_t) {
2203                 procheader_date_get_localtime(date_modified,
2204                                               sizeof(date_modified),
2205                                               msginfo->date_t);
2206                 text[col_pos[S_COL_DATE]] = date_modified;
2207         } else if (msginfo->date)
2208                 text[col_pos[S_COL_DATE]] = msginfo->date;
2209         else
2210                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2211
2212         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2213                 _("(No From)");
2214         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2215             !MSG_IS_NEWS(msginfo->flags)) {
2216                 gchar *addr = NULL;
2217
2218                 Xstrdup_a(addr, msginfo->from, return);
2219                 extract_address(addr);
2220
2221                 if (prefs_common.use_addr_book) {
2222                         if (account_find_from_address(addr)) {
2223                                 addr = summary_complete_address(msginfo->to);
2224                                 g_free(to);
2225                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2226                                 text[col_pos[S_COL_FROM]] = to;
2227                                 g_free(addr);
2228                         }
2229                 } else {
2230                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2231                                 g_free(to);
2232                                 to = g_strconcat("-->", msginfo->to, NULL);
2233                                 text[col_pos[S_COL_FROM]] = to;
2234                         }
2235                 }
2236         }
2237
2238         /*
2239          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2240          * checking whether the above block (which handles the special case of
2241          * the --> in sent boxes) was executed.
2242          */
2243         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2244                 gchar *from = summary_complete_address(msginfo->from);
2245                 if (from) {
2246                         g_free(to);
2247                         to = from;
2248                         text[col_pos[S_COL_FROM]] = to;
2249                 }                       
2250         }
2251
2252         if (summaryview->simplify_subject_preg != NULL)
2253                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2254                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2255                                         summaryview->simplify_subject_preg) : 
2256                         _("(No Subject)");
2257         else 
2258                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2259                         _("(No Subject)");
2260 }
2261
2262 #define CHANGE_FLAGS(msginfo) \
2263 { \
2264 if (msginfo->folder->folder->change_flags != NULL) \
2265 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2266                                       msginfo->folder, \
2267                                       msginfo); \
2268 }
2269
2270 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2271 {
2272         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2273 }
2274
2275 static void summary_display_msg_full(SummaryView *summaryview,
2276                                      GtkCTreeNode *row,
2277                                      gboolean new_window, gboolean all_headers)
2278 {
2279         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2280         MsgInfo *msginfo;
2281         MsgFlags flags;
2282         gchar *filename;
2283
2284         if (!new_window && summaryview->displayed == row) return;
2285         g_return_if_fail(row != NULL);
2286
2287         if (summary_is_locked(summaryview)) return;
2288         summary_lock(summaryview);
2289
2290         STATUSBAR_POP(summaryview->mainwin);
2291         GTK_EVENTS_FLUSH();
2292
2293         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2294
2295         filename = procmsg_get_message_file(msginfo);
2296         if (!filename) {
2297                 summary_unlock(summaryview);
2298                 return;
2299         }
2300         g_free(filename);
2301
2302         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2303                 summaryview->newmsgs--;
2304         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2305                 summaryview->unread--;
2306
2307         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2308         summary_set_row_marks(summaryview, row);
2309         gtk_clist_thaw(GTK_CLIST(ctree));
2310         summary_status_show(summaryview);
2311
2312         flags = msginfo->flags;
2313
2314         if (new_window) {
2315                 MessageView *msgview;
2316
2317                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2318                 messageview_show(msgview, msginfo, all_headers);
2319         } else {
2320                 MessageView *msgview;
2321
2322                 msgview = summaryview->messageview;
2323
2324                 summaryview->displayed = row;
2325                 if (!messageview_is_visible(msgview))
2326                         main_window_toggle_message_view(summaryview->mainwin);
2327                 messageview_show(msgview, msginfo, all_headers);
2328                 if (msgview->type == MVIEW_TEXT ||
2329                     (msgview->type == MVIEW_MIME &&
2330                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2331                       gtk_notebook_get_current_page
2332                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2333                         gtk_widget_grab_focus(summaryview->ctree);
2334                 GTK_EVENTS_FLUSH();
2335                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2336         }
2337
2338         summary_set_menu_sensitive(summaryview);
2339         toolbar_set_sensitive(summaryview->mainwin);
2340
2341         summary_unlock(summaryview);
2342 }
2343
2344 void summary_display_msg_selected(SummaryView *summaryview,
2345                                   gboolean all_headers)
2346 {
2347         if (summary_is_locked(summaryview)) return;
2348         summaryview->displayed = NULL;
2349         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2350                                  all_headers);
2351 }
2352
2353 void summary_redisplay_msg(SummaryView *summaryview)
2354 {
2355         GtkCTreeNode *node;
2356
2357         if (summaryview->displayed) {
2358                 node = summaryview->displayed;
2359                 summaryview->displayed = NULL;
2360                 summary_display_msg(summaryview, node);
2361         }
2362 }
2363
2364 void summary_open_msg(SummaryView *summaryview)
2365 {
2366         if (!summaryview->selected) return;
2367
2368         summary_display_msg_full(summaryview, summaryview->selected,
2369                                  TRUE, FALSE);
2370 }
2371
2372 void summary_view_source(SummaryView * summaryview)
2373 {
2374         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2375         MsgInfo *msginfo;
2376         SourceWindow *srcwin;
2377
2378         if (!summaryview->selected) return;
2379
2380         srcwin = source_window_create();
2381         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2382         source_window_show_msg(srcwin, msginfo);
2383         source_window_show(srcwin);
2384 }
2385
2386 void summary_reedit(SummaryView *summaryview)
2387 {
2388         MsgInfo *msginfo;
2389
2390         if (!summaryview->selected) return;
2391         if (!summaryview->folder_item) return;
2392         if (summaryview->folder_item->stype != F_OUTBOX &&
2393             summaryview->folder_item->stype != F_DRAFT  &&
2394             summaryview->folder_item->stype != F_QUEUE) return;
2395
2396         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2397                                               summaryview->selected);
2398         if (!msginfo) return;
2399
2400         compose_reedit(msginfo);
2401 }
2402
2403 void summary_step(SummaryView *summaryview, GtkScrollType type)
2404 {
2405         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2406         GtkCTreeNode *node;
2407
2408         if (summary_is_locked(summaryview)) return;
2409
2410         if (type == GTK_SCROLL_STEP_FORWARD) {
2411                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2412                 if (node)
2413                         gtkut_ctree_expand_parent_all(ctree, node);
2414                 else
2415                         return;
2416         } else {
2417                 if (summaryview->selected) {
2418                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2419                         if (!node) return;
2420                 }
2421         }
2422
2423         if (messageview_is_visible(summaryview->messageview))
2424                 summaryview->display_msg = TRUE;
2425
2426         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2427                                 type, 0.0);
2428 }
2429
2430 void summary_toggle_view(SummaryView *summaryview)
2431 {
2432         if (!messageview_is_visible(summaryview->messageview) &&
2433             summaryview->selected)
2434                 summary_display_msg(summaryview,
2435                                     summaryview->selected);
2436         else
2437                 main_window_toggle_message_view(summaryview->mainwin);
2438 }
2439
2440 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2441                                                 GtkCTreeNode *node)
2442 {
2443         MsgInfo *msginfo;
2444
2445         if (node) {
2446                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2447                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2448                         return TRUE;
2449                 node = GTK_CTREE_ROW(node)->children;
2450         } else
2451                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2452
2453         while (node) {
2454                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2455                         return TRUE;
2456                 node = GTK_CTREE_ROW(node)->sibling;
2457         }
2458
2459         return FALSE;
2460 }
2461
2462 static gboolean summary_have_unread_children(SummaryView *summaryview,
2463                                              GtkCTreeNode *node)
2464 {
2465         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2466
2467         if (!node) return FALSE;
2468
2469         node = GTK_CTREE_ROW(node)->children;
2470
2471         while (node) {
2472                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2473                         return TRUE;
2474                 node = GTK_CTREE_ROW(node)->sibling;
2475         }
2476
2477         return FALSE;
2478 }
2479
2480 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2481 {
2482         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2483         GtkStyle *style = NULL;
2484         MsgInfo *msginfo;
2485         MsgFlags flags;
2486         gint *col_pos = summaryview->col_pos;
2487
2488         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2489         if (!msginfo) return;
2490
2491         flags = msginfo->flags;
2492
2493         gtk_ctree_node_set_foreground(ctree, row, NULL);
2494
2495         /* set new/unread column */
2496         if (MSG_IS_IGNORE_THREAD(flags)) {
2497                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2498                                           ignorethreadxpm, ignorethreadxpmmask);
2499         } else if (MSG_IS_NEW(flags)) {
2500                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2501                                           newxpm, newxpmmask);
2502         } else if (MSG_IS_UNREAD(flags)) {
2503                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2504                                           unreadxpm, unreadxpmmask);
2505         } else if (MSG_IS_REPLIED(flags)) {
2506                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2507                                           repliedxpm, repliedxpmmask);
2508         } else if (MSG_IS_FORWARDED(flags)) {
2509                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2510                                           forwardedxpm, forwardedxpmmask);
2511         } else {
2512                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2513                                         NULL);
2514         }
2515
2516         if (prefs_common.bold_unread &&
2517             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2518              (!GTK_CTREE_ROW(row)->expanded &&
2519               GTK_CTREE_ROW(row)->children &&
2520               summary_have_unread_children(summaryview, row))))
2521                 style = bold_style;
2522
2523         /* set mark column */
2524         if (MSG_IS_DELETED(flags)) {
2525                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2526                                           deletedxpm, deletedxpmmask);
2527                 if (style)
2528                         style = bold_deleted_style;
2529                 else {
2530                         style = small_deleted_style;
2531                 }
2532                         gtk_ctree_node_set_foreground
2533                                 (ctree, row, &summaryview->color_dim);
2534         } else if (MSG_IS_MARKED(flags)) {
2535                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2536                                           markxpm, markxpmmask);
2537         } else if (MSG_IS_MOVE(flags)) {
2538                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2539                 if (style)
2540                         style = bold_marked_style;
2541                 else {
2542                         style = small_marked_style;
2543                 }
2544                         gtk_ctree_node_set_foreground
2545                                 (ctree, row, &summaryview->color_marked);
2546         } else if (MSG_IS_COPY(flags)) {
2547                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2548                 if (style)
2549                         style = bold_marked_style;
2550                 else {
2551                         style = small_marked_style;
2552                 }
2553                         gtk_ctree_node_set_foreground
2554                                 (ctree, row, &summaryview->color_marked);
2555         }
2556         else if ((global_scoring ||
2557                   summaryview->folder_item->prefs->scoring) &&
2558                  (msginfo->score >= summaryview->important_score) &&
2559                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2560                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2561                 gtk_ctree_node_set_foreground(ctree, row,
2562                                               &summaryview->color_important);
2563         } else {
2564                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2565         }
2566
2567         if (MSG_IS_LOCKED(flags)) {
2568                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2569                                           lockedxpm, lockedxpmmask);
2570         }
2571         else {
2572                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2573         }
2574
2575         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2576                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2577                                           clipkeyxpm, clipkeyxpmmask);
2578         } else if (MSG_IS_ENCRYPTED(flags)) {
2579                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2580                                           keyxpm, keyxpmmask);
2581         } else if (MSG_IS_MIME(flags)) {
2582                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2583                                           clipxpm, clipxpmmask);
2584         } else {
2585                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2586         }
2587         if (!style)
2588                 style = small_style;
2589
2590         gtk_ctree_node_set_row_style(ctree, row, style);
2591
2592         if (MSG_GET_COLORLABEL(flags))
2593                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2594 }
2595
2596 void summary_set_marks_selected(SummaryView *summaryview)
2597 {
2598         GList *cur;
2599
2600         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2601              cur = cur->next)
2602                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2603 }
2604
2605 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2606 {
2607         gboolean changed = FALSE;
2608         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2609         MsgInfo *msginfo;
2610
2611         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2612         if (MSG_IS_DELETED(msginfo->flags))
2613                 summaryview->deleted--;
2614         if (MSG_IS_MOVE(msginfo->flags)) {
2615                 summaryview->moved--;
2616                 changed = TRUE;
2617         }
2618         if (MSG_IS_COPY(msginfo->flags)) {
2619                 summaryview->copied--;
2620                 changed = TRUE;
2621         }
2622         if (changed && !prefs_common.immediate_exec) {
2623                 msginfo->to_folder->op_count--;
2624                 if (msginfo->to_folder->op_count == 0)
2625                         folderview_update_item(msginfo->to_folder, 0);
2626         }
2627         msginfo->to_folder = NULL;
2628         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2629         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2630         summary_set_row_marks(summaryview, row);
2631         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2632 }
2633
2634 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2635 {
2636         /* almost verbatim summary_mark_row(); may want a menu action? */
2637         gboolean changed = FALSE;
2638         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2639         MsgInfo *msginfo;
2640
2641         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2642         if (MSG_IS_DELETED(msginfo->flags))
2643                 summaryview->deleted--;
2644         if (MSG_IS_MOVE(msginfo->flags)) {
2645                 summaryview->moved--;
2646                 changed = TRUE;
2647         }
2648         if (MSG_IS_COPY(msginfo->flags)) {
2649                 summaryview->copied--;
2650                 changed = TRUE;
2651         }
2652         if (changed && !prefs_common.immediate_exec) {
2653                 msginfo->to_folder->op_count--;
2654                 if (msginfo->to_folder->op_count == 0)
2655                         folderview_update_item(msginfo->to_folder, 0);
2656         }
2657         msginfo->to_folder = NULL;
2658         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2659         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2660         summary_set_row_marks(summaryview, row);
2661         debug_print("Message %d is locked\n", msginfo->msgnum);
2662 }
2663
2664 void summary_mark(SummaryView *summaryview)
2665 {
2666         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2667         GList *cur;
2668
2669         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2670                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2671
2672         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2673         summary_status_show(summaryview);
2674 }
2675
2676 static void summary_mark_row_as_read(SummaryView *summaryview,
2677                                      GtkCTreeNode *row)
2678 {
2679         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2680         MsgInfo *msginfo;
2681
2682         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2683
2684         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2685                 return;
2686
2687         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2688                 summaryview->newmsgs--;
2689         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2690                 summaryview->unread--;
2691
2692         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2693         summary_set_row_marks(summaryview, row);
2694         debug_print("Message %d is marked as read\n",
2695                 msginfo->msgnum);
2696 }
2697
2698 void summary_mark_as_read(SummaryView *summaryview)
2699 {
2700         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2701         GList *cur;
2702
2703         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2704                 summary_mark_row_as_read(summaryview,
2705                                          GTK_CTREE_NODE(cur->data));
2706
2707         summary_status_show(summaryview);
2708 }
2709
2710 void summary_mark_all_read(SummaryView *summaryview)
2711 {
2712         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2713         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2714         GtkCTreeNode *node;
2715
2716         gtk_clist_freeze(clist);
2717         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2718              node = gtkut_ctree_node_next(ctree, node))
2719                 summary_mark_row_as_read(summaryview, node);
2720         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2721              node = gtkut_ctree_node_next(ctree, node)) {
2722                 if (!GTK_CTREE_ROW(node)->expanded)
2723                         summary_set_row_marks(summaryview, node);
2724         }
2725         gtk_clist_thaw(clist);
2726
2727         summary_status_show(summaryview);
2728 }
2729
2730 static void summary_mark_row_as_unread(SummaryView *summaryview,
2731                                        GtkCTreeNode *row)
2732 {
2733         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2734         MsgInfo *msginfo;
2735
2736         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2737         if (MSG_IS_DELETED(msginfo->flags)) {
2738                 msginfo->to_folder = NULL;
2739                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2740                 summaryview->deleted--;
2741         }
2742
2743         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2744                 summaryview->unread++;
2745
2746         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2747         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2748         debug_print("Message %d is marked as unread\n",
2749                 msginfo->msgnum);
2750
2751         summary_set_row_marks(summaryview, row);
2752 }
2753
2754 void summary_mark_as_unread(SummaryView *summaryview)
2755 {
2756         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2757         GList *cur;
2758
2759         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2760                 summary_mark_row_as_unread(summaryview,
2761                                            GTK_CTREE_NODE(cur->data));
2762
2763         summary_status_show(summaryview);
2764 }
2765
2766 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2767 {
2768         GList * cur;
2769         gboolean found;
2770
2771         switch (summaryview->folder_item->folder->type) {
2772
2773         case F_NEWS:
2774
2775                 /*
2776                   security : checks if one the accounts correspond to
2777                   the author of the post
2778                 */
2779
2780                 found = FALSE;
2781                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2782                         PrefsAccount * account;
2783                         gchar * from_name;
2784                         
2785                         account = cur->data;
2786                         if (account->name && *account->name)
2787                                 from_name =
2788                                         g_strdup_printf("%s <%s>",
2789                                                         account->name,
2790                                                         account->address);
2791                         else
2792                                 from_name =
2793                                         g_strdup_printf("%s",
2794                                                         account->address);
2795                         
2796                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2797                                 g_free(from_name);
2798                                 found = TRUE;
2799                                 break;
2800                         }
2801                         g_free(from_name);
2802                 }
2803
2804                 if (!found) {
2805                         alertpanel_error(_("You're not the author of the article\n"));
2806                 }
2807                 
2808                 return found;
2809
2810         default:
2811                 return TRUE;
2812         }
2813 }
2814
2815 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2816 {
2817         gboolean changed = FALSE;
2818         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2819         MsgInfo *msginfo;
2820
2821         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2822
2823         if (!check_permission(summaryview, msginfo))
2824                 return;
2825
2826         if (MSG_IS_LOCKED(msginfo->flags)) return;
2827
2828         if (MSG_IS_DELETED(msginfo->flags)) return;
2829
2830         if (MSG_IS_MOVE(msginfo->flags)) {
2831                 summaryview->moved--;
2832                 changed = TRUE;
2833         }
2834         if (MSG_IS_COPY(msginfo->flags)) {
2835                 summaryview->copied--;
2836                 changed = TRUE;
2837         }
2838         if (changed && !prefs_common.immediate_exec) {
2839                 msginfo->to_folder->op_count--;
2840                 if (msginfo->to_folder->op_count == 0)
2841                         folderview_update_item(msginfo->to_folder, 0);
2842         }
2843         msginfo->to_folder = NULL;
2844         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2845         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2846         summaryview->deleted++;
2847
2848         if (!prefs_common.immediate_exec && 
2849             summaryview->folder_item->stype != F_TRASH)
2850                 summary_set_row_marks(summaryview, row);
2851
2852         debug_print("Message %s/%d is set to delete\n",
2853                     msginfo->folder->path, msginfo->msgnum);
2854 }
2855
2856 void summary_delete(SummaryView *summaryview)
2857 {
2858         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2859         FolderItem *item = summaryview->folder_item;
2860         GList *cur;
2861         GtkCTreeNode *sel_last = NULL;
2862         GtkCTreeNode *node;
2863
2864         if (!item) return;
2865 #if 0
2866         if (!item || item->folder->type == F_NEWS) return;
2867 #endif
2868
2869         if (summary_is_locked(summaryview)) return;
2870
2871         /* if current folder is trash, ask for confirmation */
2872         if (item->stype == F_TRASH) {
2873                 AlertValue aval;
2874
2875                 aval = alertpanel(_("Delete message(s)"),
2876                                   _("Do you really want to delete message(s) from the trash?"),
2877                                   _("Yes"), _("No"), NULL);
2878                 if (aval != G_ALERTDEFAULT) return;
2879         }
2880
2881         /* next code sets current row focus right. We need to find a row
2882          * that is not deleted. */
2883         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2884                 sel_last = GTK_CTREE_NODE(cur->data);
2885                 summary_delete_row(summaryview, sel_last);
2886         }
2887
2888         node = summary_find_next_msg(summaryview, sel_last);
2889         if (!node)
2890                 node = summary_find_prev_msg(summaryview, sel_last);
2891
2892         if (node) {
2893                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2894                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2895                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2896                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2897                 else
2898                         summary_select_node
2899                                 (summaryview, node,
2900                                  messageview_is_visible(summaryview->messageview),
2901                                  FALSE);
2902         }
2903
2904         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2905                 summary_execute(summaryview);
2906         else
2907                 summary_status_show(summaryview);
2908 }
2909
2910 void summary_delete_duplicated(SummaryView *summaryview)
2911 {
2912         if (!summaryview->folder_item ||
2913             summaryview->folder_item->folder->type == F_NEWS) return;
2914         if (summaryview->folder_item->stype == F_TRASH) return;
2915
2916         main_window_cursor_wait(summaryview->mainwin);
2917         debug_print("Deleting duplicated messages...");
2918         STATUSBAR_PUSH(summaryview->mainwin,
2919                        _("Deleting duplicated messages..."));
2920
2921         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2922                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2923                                 summaryview);
2924
2925         if (prefs_common.immediate_exec)
2926                 summary_execute(summaryview);
2927         else
2928                 summary_status_show(summaryview);
2929
2930         debug_print("done.\n");
2931         STATUSBAR_POP(summaryview->mainwin);
2932         main_window_cursor_normal(summaryview->mainwin);
2933 }
2934
2935 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2936                                            SummaryView *summaryview)
2937 {
2938         GtkCTreeNode *found;
2939         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2940
2941         if (!msginfo->msgid || !*msginfo->msgid) return;
2942
2943         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2944
2945         if (found && found != node)
2946                 summary_delete_row(summaryview, node);
2947 }
2948
2949 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2950 {
2951         gboolean changed = FALSE;
2952         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2953         MsgInfo *msginfo;
2954
2955         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2956         if (MSG_IS_DELETED(msginfo->flags))
2957                 summaryview->deleted--;
2958         if (MSG_IS_MOVE(msginfo->flags)) {
2959                 summaryview->moved--;
2960                 changed = TRUE;
2961         }
2962         if (MSG_IS_COPY(msginfo->flags)) {
2963                 summaryview->copied--;
2964                 changed = TRUE;
2965         }
2966         if (changed && !prefs_common.immediate_exec) {
2967                 msginfo->to_folder->op_count--;
2968                 if (msginfo->to_folder->op_count == 0)
2969                         folderview_update_item(msginfo->to_folder, 0);
2970         }
2971         msginfo->to_folder = NULL;
2972         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
2973         summary_set_row_marks(summaryview, row);
2974
2975         debug_print("Message %s/%d is unmarked\n",
2976                     msginfo->folder->path, msginfo->msgnum);
2977 }
2978
2979 void summary_unmark(SummaryView *summaryview)
2980 {
2981         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2982         GList *cur;
2983
2984         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2985                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2986
2987         summary_status_show(summaryview);
2988 }
2989
2990 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2991                                 FolderItem *to_folder)
2992 {
2993         gboolean changed = FALSE;
2994         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2995         MsgInfo *msginfo;
2996
2997         g_return_if_fail(to_folder != NULL);
2998
2999         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3000         if (MSG_IS_MOVE(msginfo->flags)) {
3001                 if (!prefs_common.immediate_exec) {
3002                         msginfo->to_folder->op_count--;
3003                         if (msginfo->to_folder->op_count == 0) {
3004                                 folderview_update_item(msginfo->to_folder, 0);
3005                                 changed = TRUE;
3006                         }
3007                 }
3008         }
3009         msginfo->to_folder = to_folder;
3010         if (MSG_IS_DELETED(msginfo->flags))
3011                 summaryview->deleted--;
3012         if (MSG_IS_COPY(msginfo->flags)) {
3013                 summaryview->copied--;
3014                 if (!prefs_common.immediate_exec)
3015                         msginfo->to_folder->op_count--;
3016         }
3017         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3018         if (!MSG_IS_MOVE(msginfo->flags)) {
3019                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3020                 summaryview->moved++;
3021                 changed = TRUE;
3022         }
3023         if (!prefs_common.immediate_exec) {
3024                 summary_set_row_marks(summaryview, row);
3025                 if (changed) {
3026                         msginfo->to_folder->op_count++;
3027                         if (msginfo->to_folder->op_count == 1)
3028                                 folderview_update_item(msginfo->to_folder, 0);
3029                 }
3030         }
3031
3032         debug_print("Message %d is set to move to %s\n",
3033                     msginfo->msgnum, to_folder->path);
3034 }
3035
3036 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3037 {
3038         GList *cur;
3039
3040         if (!to_folder) return;
3041         if (!summaryview->folder_item ||
3042             summaryview->folder_item->folder->type == F_NEWS) return;
3043
3044         if (summary_is_locked(summaryview)) return;
3045
3046         if (summaryview->folder_item == to_folder) {
3047                 alertpanel_notice(_("Destination is same as current folder."));
3048                 return;
3049         }
3050
3051         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3052              cur != NULL; cur = cur->next)
3053                 summary_move_row_to
3054                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3055
3056         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3057
3058         if (prefs_common.immediate_exec)
3059                 summary_execute(summaryview);
3060         else {
3061                 summary_status_show(summaryview);
3062
3063                 folderview_update_item(to_folder, 0);
3064         }
3065 }
3066
3067 void summary_move_to(SummaryView *summaryview)
3068 {
3069         FolderItem *to_folder;
3070
3071         if (!summaryview->folder_item ||
3072             summaryview->folder_item->folder->type == F_NEWS) return;
3073
3074         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3075                                          FOLDER_SEL_MOVE, NULL);
3076         summary_move_selected_to(summaryview, to_folder);
3077 }
3078
3079 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3080                                 FolderItem *to_folder)
3081 {
3082         gboolean changed = FALSE;
3083         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3084         MsgInfo *msginfo;
3085
3086         g_return_if_fail(to_folder != NULL);
3087
3088         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3089         if (MSG_IS_COPY(msginfo->flags)) {
3090                 if (!prefs_common.immediate_exec) {
3091                         msginfo->to_folder->op_count--;
3092                         if (msginfo->to_folder->op_count == 0) {
3093                                 folderview_update_item(msginfo->to_folder, 0);
3094                                 changed = TRUE;
3095                         }
3096                 }
3097         }
3098         msginfo->to_folder = to_folder;
3099         if (MSG_IS_DELETED(msginfo->flags))
3100                 summaryview->deleted--;
3101         if (MSG_IS_MOVE(msginfo->flags)) {
3102                 summaryview->moved--;
3103                 if (!prefs_common.immediate_exec)
3104                         msginfo->to_folder->op_count--;
3105         }
3106         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3107         if (!MSG_IS_COPY(msginfo->flags)) {
3108                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3109                 summaryview->copied++;
3110                 changed = TRUE;
3111         }
3112         if (!prefs_common.immediate_exec) {
3113                 summary_set_row_marks(summaryview, row);
3114                 if (changed) {
3115                         msginfo->to_folder->op_count++;
3116                         if (msginfo->to_folder->op_count == 1)
3117                                 folderview_update_item(msginfo->to_folder, 0);
3118                 }
3119         }
3120
3121         debug_print("Message %d is set to copy to %s\n",
3122                     msginfo->msgnum, to_folder->path);
3123 }
3124
3125 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3126 {
3127         GList *cur;
3128
3129         if (!to_folder) return;
3130         if (!summaryview->folder_item) return;
3131
3132         if (summary_is_locked(summaryview)) return;
3133
3134         if (summaryview->folder_item == to_folder) {
3135                 alertpanel_notice
3136                         (_("Destination to copy is same as current folder."));
3137                 return;
3138         }
3139
3140         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3141              cur != NULL; cur = cur->next)
3142                 summary_copy_row_to
3143                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3144
3145         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3146
3147         if (prefs_common.immediate_exec)
3148                 summary_execute(summaryview);
3149         else {
3150                 summary_status_show(summaryview);
3151
3152                 folderview_update_item(to_folder, 0);
3153         }
3154 }
3155
3156 void summary_copy_to(SummaryView *summaryview)
3157 {
3158         FolderItem *to_folder;
3159
3160         if (!summaryview->folder_item) return;
3161
3162         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3163                                          FOLDER_SEL_COPY, NULL);
3164         summary_copy_selected_to(summaryview, to_folder);
3165 }
3166
3167 void summary_add_address(SummaryView *summaryview)
3168 {
3169         MsgInfo *msginfo;
3170         gchar *from;
3171
3172         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3173                                               summaryview->selected);
3174         if (!msginfo) return;
3175
3176         Xstrdup_a(from, msginfo->from, return);
3177         eliminate_address_comment(from);
3178         extract_address(from);
3179         addressbook_add_contact(msginfo->fromname, from, NULL);
3180 }
3181
3182 void summary_select_all(SummaryView *summaryview)
3183 {
3184         if (summaryview->messages >= 500) {
3185                 STATUSBAR_PUSH(summaryview->mainwin,
3186                                _("Selecting all messages..."));
3187                 main_window_cursor_wait(summaryview->mainwin);
3188         }
3189
3190         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3191
3192         if (summaryview->messages >= 500) {
3193                 STATUSBAR_POP(summaryview->mainwin);
3194                 main_window_cursor_normal(summaryview->mainwin);
3195         }
3196 }
3197
3198 void summary_unselect_all(SummaryView *summaryview)
3199 {
3200         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3201 }
3202
3203 void summary_save_as(SummaryView *summaryview)
3204 {
3205         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3206         MsgInfo *msginfo;
3207         gchar *filename = NULL;
3208         gchar *src, *dest;
3209
3210         if (!summaryview->selected) return;
3211         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3212         if (!msginfo) return;
3213
3214         if (msginfo->subject) {
3215                 Xstrdup_a(filename, msginfo->subject, return);
3216                 subst_for_filename(filename);
3217         }
3218         dest = filesel_select_file(_("Save as"), filename);
3219         if (!dest) return;
3220         if (is_file_exist(dest)) {
3221                 AlertValue aval;
3222
3223                 aval = alertpanel(_("Overwrite"),
3224                                   _("Overwrite existing file?"),
3225                                   _("OK"), _("Cancel"), NULL);
3226                 if (G_ALERTDEFAULT != aval) return;
3227         }
3228
3229         src = procmsg_get_message_file(msginfo);
3230         if (copy_file(src, dest, TRUE) < 0) {
3231                 alertpanel_error(_("Can't save the file `%s'."),
3232                                  g_basename(dest));
3233         }
3234         g_free(src);
3235 }
3236
3237 void summary_print(SummaryView *summaryview)
3238 {
3239         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3240         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3241         MsgInfo *msginfo;
3242         GList *cur;
3243         gchar *cmdline;
3244         gchar *p;
3245
3246         if (clist->selection == NULL) return;
3247
3248         cmdline = input_dialog(_("Print"),
3249                                _("Enter the print command line:\n"
3250                                  "(`%s' will be replaced with file name)"),
3251                                prefs_common.print_cmd);
3252         if (!cmdline) return;
3253         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3254             strchr(p + 2, '%')) {
3255                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3256                                  cmdline);
3257                 g_free(cmdline);
3258                 return;
3259         }
3260
3261         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3262                 msginfo = gtk_ctree_node_get_row_data
3263                         (ctree, GTK_CTREE_NODE(cur->data));
3264                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3265         }
3266
3267         g_free(cmdline);
3268 }
3269
3270 gboolean summary_execute(SummaryView *summaryview)
3271 {
3272         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3273         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3274         GtkCTreeNode *node, *next;
3275
3276         if (!summaryview->folder_item) return FALSE;
3277
3278         if (summary_is_locked(summaryview)) return FALSE;
3279         summary_lock(summaryview);
3280
3281         gtk_clist_freeze(clist);
3282
3283         if (summaryview->threaded)
3284                 summary_unthread_for_exec(summaryview);
3285
3286         summary_execute_move(summaryview);
3287         summary_execute_copy(summaryview);
3288         summary_execute_delete(summaryview);
3289
3290         node = GTK_CTREE_NODE(clist->row_list);
3291         while (node != NULL) {
3292                 next = gtkut_ctree_node_next(ctree, node);
3293                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
3294                         if (node == summaryview->displayed) {
3295                                 messageview_clear(summaryview->messageview);
3296                                 summaryview->displayed = NULL;
3297                         }
3298                         if (GTK_CTREE_ROW(node)->children != NULL)
3299                                 g_warning("summary_execute(): children != NULL\n");
3300                         else
3301                                 gtk_ctree_remove_node(ctree, node);
3302                 }
3303                 node = next;
3304         }
3305
3306         if (summaryview->threaded)
3307                 summary_thread_build(summaryview);
3308
3309         summaryview->selected = clist->selection ?
3310                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3311
3312         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3313                 menu_set_insensitive_all
3314                         (GTK_MENU_SHELL(summaryview->popupmenu));
3315                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3316         } else
3317                 gtk_widget_grab_focus(summaryview->ctree);
3318
3319         summary_update_status(summaryview);
3320         summary_status_show(summaryview);
3321
3322         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3323
3324         gtk_clist_thaw(clist);
3325
3326         summary_unlock(summaryview);
3327         return TRUE;
3328 }
3329
3330 static void summary_execute_move(SummaryView *summaryview)
3331 {
3332         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3333         GSList *cur;
3334
3335         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3336
3337         /* search moving messages and execute */
3338         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3339                                 summaryview);
3340
3341         if (summaryview->mlist) {
3342                 procmsg_move_messages(summaryview->mlist);
3343
3344                 folderview_update_item_foreach(summaryview->folder_table, FALSE);
3345
3346                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3347                         procmsg_msginfo_free((MsgInfo *)cur->data);
3348                 g_slist_free(summaryview->mlist);
3349                 summaryview->mlist = NULL;
3350         }
3351
3352         folderview_update_item(summaryview->folder_item, FALSE);
3353         g_hash_table_destroy(summaryview->folder_table);
3354         summaryview->folder_table = NULL;
3355 }
3356
3357 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3358                                       gpointer data)
3359 {
3360         SummaryView *summaryview = data;
3361         MsgInfo *msginfo;
3362
3363         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3364
3365         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3366                 if (!prefs_common.immediate_exec &&
3367                     msginfo->to_folder->op_count > 0)
3368                         msginfo->to_folder->op_count--;
3369
3370                 g_hash_table_insert(summaryview->folder_table,
3371                                     msginfo->to_folder, GINT_TO_POINTER(1));
3372
3373                 summaryview->mlist =
3374                         g_slist_append(summaryview->mlist, msginfo);
3375                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3376
3377                 if (msginfo->msgid && *msginfo->msgid &&
3378                     node == g_hash_table_lookup(summaryview->msgid_table,
3379                                                 msginfo->msgid))
3380                         g_hash_table_remove(summaryview->msgid_table,
3381                                             msginfo->msgid);
3382         }
3383 }
3384
3385 static void summary_execute_copy(SummaryView *summaryview)
3386 {
3387         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3388
3389         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3390
3391         /* search copying messages and execute */
3392         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3393                                 summaryview);
3394
3395         if (summaryview->mlist) {
3396                 procmsg_copy_messages(summaryview->mlist);
3397
3398                 /* folder_item_scan_foreach(summaryview->folder_table); */
3399                 folderview_update_item_foreach(summaryview->folder_table, FALSE);
3400
3401                 g_slist_free(summaryview->mlist);
3402                 summaryview->mlist = NULL;
3403         }
3404
3405         g_hash_table_destroy(summaryview->folder_table);
3406         summaryview->folder_table = NULL;
3407 }
3408
3409 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3410                                       gpointer data)
3411 {
3412         SummaryView *summaryview = data;
3413         MsgInfo *msginfo;
3414
3415         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3416
3417         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3418                 if (!prefs_common.immediate_exec &&
3419                     msginfo->to_folder->op_count > 0)
3420                         msginfo->to_folder->op_count--;
3421
3422                 g_hash_table_insert(summaryview->folder_table,
3423                                     msginfo->to_folder, GINT_TO_POINTER(1));
3424
3425                 summaryview->mlist =
3426                         g_slist_append(summaryview->mlist, msginfo);
3427
3428                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3429                 summary_set_row_marks(summaryview, node);
3430         }
3431 }
3432
3433 static void summary_execute_delete(SummaryView *summaryview)
3434 {
3435         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3436         FolderItem *trash;
3437         GSList *cur;
3438
3439         trash = summaryview->folder_item->folder->trash;
3440         if (summaryview->folder_item->folder->type == F_MH) {
3441                 g_return_if_fail(trash != NULL);
3442         }
3443
3444         /* search deleting messages and execute */
3445         gtk_ctree_pre_recursive
3446                 (ctree, NULL, summary_execute_delete_func, summaryview);
3447
3448         if (!summaryview->mlist) return;
3449
3450         if (trash == NULL || summaryview->folder_item == trash)
3451                 folder_item_remove_msgs(summaryview->folder_item,
3452                                         summaryview->mlist);
3453         else
3454                 folder_item_move_msgs_with_dest(trash, summaryview->mlist);
3455
3456         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3457                 procmsg_msginfo_free((MsgInfo *)cur->data);
3458
3459         g_slist_free(summaryview->mlist);
3460         summaryview->mlist = NULL;
3461
3462         if ((summaryview->folder_item != trash) && (trash != NULL)) {
3463                 folderview_update_item(trash, FALSE);
3464         }
3465         folderview_update_item(summaryview->folder_item, FALSE);
3466 }
3467
3468 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3469                                         gpointer data)
3470 {
3471         SummaryView *summaryview = data;
3472         MsgInfo *msginfo;
3473
3474         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3475
3476         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3477                 summaryview->mlist =
3478                         g_slist_append(summaryview->mlist, msginfo);
3479                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3480
3481                 if (msginfo->msgid && *msginfo->msgid &&
3482                     node == g_hash_table_lookup(summaryview->msgid_table,
3483                                                 msginfo->msgid))
3484                         g_hash_table_remove(summaryview->msgid_table,
3485                                             msginfo->msgid);
3486         }
3487 }
3488
3489 /* thread functions */
3490
3491 void summary_thread_build(SummaryView *summaryview)
3492 {
3493         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3494         GtkCTreeNode *node;
3495         GtkCTreeNode *next;
3496         GtkCTreeNode *parent;
3497         MsgInfo *msginfo;
3498
3499         summary_lock(summaryview);
3500
3501         debug_print("Building threads...");
3502         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3503         main_window_cursor_wait(summaryview->mainwin);
3504
3505         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
3506                                          summary_tree_expanded, summaryview);
3507         gtk_clist_freeze(GTK_CLIST(ctree));
3508
3509         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3510         while (node) {
3511                 next = GTK_CTREE_ROW(node)->sibling;
3512
3513                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3514
3515                 parent = NULL;
3516
3517                 /* alfons - claws seems to prefer subject threading before
3518                  * inreplyto threading. we should look more deeply in this,
3519                  * because inreplyto should have precedence... */
3520                 if (msginfo && msginfo->inreplyto) {
3521                         parent = g_hash_table_lookup(summaryview->msgid_table,
3522                                                      msginfo->inreplyto);
3523                 }
3524
3525                 if (parent == NULL) {
3526                         parent = subject_table_lookup
3527                                 (summaryview->subject_table,
3528                                  msginfo->subject);
3529                 }
3530
3531                 if (parent && parent != node) {
3532                         gtk_ctree_move(ctree, node, parent, NULL);
3533                         gtk_ctree_expand(ctree, node);
3534                 }
3535
3536                 node = next;
3537         }
3538
3539         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3540
3541         while (node) {
3542                 next = GTK_CTREE_NODE_NEXT(node);
3543                 if (prefs_common.expand_thread)
3544                         gtk_ctree_expand(ctree, node);
3545                 if (prefs_common.bold_unread &&
3546                     GTK_CTREE_ROW(node)->children)
3547                         summary_set_row_marks(summaryview, node);
3548                 node = next;
3549         }
3550
3551         gtk_clist_thaw(GTK_CLIST(ctree));
3552         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
3553                                            summary_tree_expanded, summaryview);
3554
3555         debug_print("done.\n");
3556         STATUSBAR_POP(summaryview->mainwin);
3557         main_window_cursor_normal(summaryview->mainwin);
3558
3559         summaryview->threaded = TRUE;
3560
3561         summary_unlock(summaryview);
3562 }
3563
3564 static void summary_thread_init(SummaryView *summaryview)
3565 {
3566         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3567         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3568         GtkCTreeNode *next;
3569
3570         if (prefs_common.expand_thread) {
3571                 while (node) {
3572                         next = GTK_CTREE_ROW(node)->sibling;
3573                         if (GTK_CTREE_ROW(node)->children)
3574                                 gtk_ctree_expand(ctree, node);
3575                         node = next;
3576                 }
3577         } else if (prefs_common.bold_unread) {
3578                 while (node) {
3579                         next = GTK_CTREE_ROW(node)->sibling;
3580                         if (GTK_CTREE_ROW(node)->children)
3581                                 summary_set_row_marks(summaryview, node);
3582                         node = next;
3583                 }
3584         }
3585 }
3586
3587 void summary_unthread(SummaryView *summaryview)
3588 {
3589         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3590         GtkCTreeNode *node;
3591         GtkCTreeNode *child;
3592         GtkCTreeNode *sibling;
3593         GtkCTreeNode *next_child;
3594
3595         summary_lock(summaryview);
3596
3597         debug_print("Unthreading...");
3598         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
3599         main_window_cursor_wait(summaryview->mainwin);
3600         
3601         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
3602                                          summary_tree_collapsed, summaryview);
3603         gtk_clist_freeze(GTK_CLIST(ctree));
3604
3605         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3606              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3607                 child = GTK_CTREE_ROW(node)->children;
3608                 sibling = GTK_CTREE_ROW(node)->sibling;
3609
3610                 while (child != NULL) {
3611                         next_child = GTK_CTREE_ROW(child)->sibling;
3612                         gtk_ctree_move(ctree, child, NULL, sibling);
3613                         child = next_child;
3614                 }
3615         }
3616
3617         /* CLAWS: and sort it */
3618         gtk_sctree_sort_recursive(ctree, NULL); 
3619
3620         gtk_clist_thaw(GTK_CLIST(ctree));
3621         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
3622                                            summary_tree_collapsed, summaryview);
3623
3624         debug_print("done.\n");
3625         STATUSBAR_POP(summaryview->mainwin);
3626         main_window_cursor_normal(summaryview->mainwin);
3627
3628         summaryview->threaded = FALSE;
3629
3630         summary_unlock(summaryview);
3631 }
3632
3633 static void summary_unthread_for_exec(SummaryView *summaryview)
3634 {
3635         GtkCTreeNode *node;
3636         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3637
3638         summary_lock(summaryview);
3639
3640         debug_print("Unthreading for execution...");
3641
3642         gtk_clist_freeze(GTK_CLIST(ctree));
3643
3644         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3645              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3646                 summary_unthread_for_exec_func(ctree, node, NULL);
3647         }
3648
3649         gtk_clist_thaw(GTK_CLIST(ctree));
3650
3651         debug_print("done.\n");
3652
3653         summary_unlock(summaryview);
3654 }
3655
3656 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
3657                                            gpointer data)
3658 {
3659         MsgInfo *msginfo;
3660         GtkCTreeNode *top_parent;
3661         GtkCTreeNode *child;
3662         GtkCTreeNode *sibling;
3663
3664         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3665
3666         if (!msginfo ||
3667             (!MSG_IS_MOVE(msginfo->flags) &&
3668              !MSG_IS_DELETED(msginfo->flags)))
3669                 return;
3670         child = GTK_CTREE_ROW(node)->children;
3671         if (!child) return;
3672
3673         for (top_parent = node;
3674              GTK_CTREE_ROW(top_parent)->parent != NULL;
3675              top_parent = GTK_CTREE_ROW(top_parent)->parent)
3676                 ;
3677         sibling = GTK_CTREE_ROW(top_parent)->sibling;
3678
3679         while (child != NULL) {
3680                 GtkCTreeNode *next_child;
3681
3682                 next_child = GTK_CTREE_ROW(child)->sibling;
3683                 gtk_ctree_move(ctree, child, NULL, sibling);
3684                 child = next_child;
3685         }
3686 }
3687
3688 void summary_expand_threads(SummaryView *summaryview)
3689 {
3690         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3691         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3692
3693         gtk_clist_freeze(GTK_CLIST(ctree));
3694
3695         while (node) {
3696                 if (GTK_CTREE_ROW(node)->children)
3697                         gtk_ctree_expand(ctree, node);
3698                 node = GTK_CTREE_NODE_NEXT(node);
3699         }
3700
3701         gtk_clist_thaw(GTK_CLIST(ctree));
3702
3703         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3704 }
3705
3706 void summary_collapse_threads(SummaryView *summaryview)
3707 {
3708         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3709         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3710
3711         gtk_clist_freeze(GTK_CLIST(ctree));
3712
3713         while (node) {
3714                 if (GTK_CTREE_ROW(node)->children)
3715                         gtk_ctree_collapse(ctree, node);
3716                 node = GTK_CTREE_ROW(node)->sibling;
3717         }
3718
3719         gtk_clist_thaw(GTK_CLIST(ctree));
3720
3721         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3722 }
3723
3724 void summary_filter(SummaryView *summaryview)
3725 {
3726         if (!prefs_common.fltlist && !global_processing) {
3727                 alertpanel_error(_("No filter rules defined."));
3728                 return;
3729         }
3730
3731         summary_lock(summaryview);
3732
3733         debug_print("filtering...");
3734         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
3735         main_window_cursor_wait(summaryview->mainwin);
3736
3737         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3738
3739         if (global_processing == NULL) {
3740                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3741                                         GTK_CTREE_FUNC(summary_filter_func),
3742                                         summaryview);
3743                 
3744                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3745                 
3746                 if (prefs_common.immediate_exec) {
3747                         summary_unlock(summaryview);
3748                         summary_execute(summaryview);
3749                         summary_lock(summaryview);
3750                 } else
3751                         summary_status_show(summaryview);
3752         }
3753         else {
3754                 summaryview->folder_table = g_hash_table_new(NULL, NULL);
3755
3756                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3757                                         GTK_CTREE_FUNC(summary_filter_func),
3758                                         summaryview);
3759
3760                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3761
3762                 /* folder_item_scan_foreach(summaryview->folder_table); */
3763                 folderview_update_item_foreach(summaryview->folder_table, FALSE);
3764
3765                 g_hash_table_destroy(summaryview->folder_table);
3766                 summaryview->folder_table = NULL;
3767         }
3768
3769         debug_print("done.\n");
3770         STATUSBAR_POP(summaryview->mainwin);
3771         main_window_cursor_normal(summaryview->mainwin);
3772
3773         summary_unlock(summaryview);
3774
3775         /* 
3776          * CLAWS: summary_show() only valid after having a lock. ideally
3777          * we want the lock to be context aware...  
3778          */
3779         if (global_processing) {
3780                 summary_show(summaryview, summaryview->folder_item);
3781         }               
3782 }
3783
3784 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
3785                                 gpointer data)
3786 {
3787         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3788         SummaryView *summaryview = data;
3789         gchar *file;
3790         FolderItem *dest;
3791
3792         if (global_processing == NULL) {
3793                 /* old filtering */
3794                 file = procmsg_get_message_file(msginfo);
3795                 dest = filter_get_dest_folder(prefs_common.fltlist, file);
3796                 g_free(file);
3797
3798                 if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
3799                     summaryview->folder_item != dest)
3800                         summary_move_row_to(summaryview, node, dest);
3801         } else 
3802                 filter_message_by_msginfo(global_processing, msginfo, summaryview->folder_table);
3803 }
3804
3805 void summary_filter_open(SummaryView *summaryview, PrefsFilterType type)
3806 {
3807         static HeaderEntry hentry[] = {{"X-BeenThere:",    NULL, FALSE},
3808                                        {"X-ML-Name:",      NULL, FALSE},
3809                                        {"X-List:",         NULL, FALSE},
3810                                        {"X-Mailing-list:", NULL, FALSE},
3811                                        {"List-Id:",        NULL, FALSE},
3812                                        {NULL,              NULL, FALSE}};
3813
3814         static gchar *header_strs[] = {"From", "from", "To", "to", "Subject", "subject"};
3815
3816         static gchar *hentry_strs[]   = {"X-BeenThere", "X-ML-Name", "X-List",
3817                                          "X-Mailing-List", "List-Id",
3818                                          "header \"X-BeenThere\"", "header \"X-ML-Name\"",
3819                                          "header \"X-List\"", "header \"X-Mailing-List\"",
3820                                          "header \"List-Id\""};
3821         enum
3822         {
3823                 H_X_BEENTHERE    = 0,
3824                 H_X_ML_NAME      = 1,
3825                 H_X_LIST         = 2,
3826                 H_X_MAILING_LIST = 3,
3827                 H_LIST_ID        = 4
3828         };
3829
3830         enum
3831         {
3832                 H_FROM    = 0,
3833                 H_TO      = 2,
3834                 H_SUBJECT = 4
3835         };
3836
3837         MsgInfo *msginfo;
3838         gchar *header = NULL;
3839         gchar *key = NULL;
3840         FILE *fp;
3841         int header_offset;
3842         int hentry_offset;
3843
3844         if (!summaryview->selected) return;
3845
3846         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3847                                               summaryview->selected);
3848         if (!msginfo) return;
3849
3850         if (global_processing) {
3851                 header_offset = 1;
3852                 hentry_offset = 5;
3853         }
3854         else {
3855                 header_offset = 0;
3856                 hentry_offset = 0;
3857         }
3858
3859         switch (type) {
3860         case FILTER_BY_NONE:
3861                 break;
3862         case FILTER_BY_AUTO:
3863                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
3864                 procheader_get_header_fields(fp, hentry);
3865                 fclose(fp);
3866
3867                 if (hentry[H_X_BEENTHERE].body != NULL) {
3868                         header = hentry_strs[H_X_BEENTHERE + hentry_offset];
3869                         Xstrdup_a(key, hentry[H_X_BEENTHERE].body, );
3870                 } else if (hentry[H_X_ML_NAME].body != NULL) {
3871                         header = hentry_strs[H_X_ML_NAME + hentry_offset];
3872                         Xstrdup_a(key, hentry[H_X_ML_NAME].body, );
3873                 } else if (hentry[H_X_LIST].body != NULL) {
3874                         header = hentry_strs[H_X_LIST + hentry_offset];
3875                         Xstrdup_a(key, hentry[H_X_LIST].body, );
3876                 } else if (hentry[H_X_MAILING_LIST].body != NULL) {
3877                         header = hentry_strs[H_X_MAILING_LIST + hentry_offset];
3878                         Xstrdup_a(key, hentry[H_X_MAILING_LIST].body, );
3879