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