2005-05-24 [colin] 1.9.11cvs8
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkpixmap.h>
28 #include <gtk/gtkctree.h>
29 #include <gtk/gtkcontainer.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkitemfactory.h>
35 #include <gtk/gtkvbox.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkwindow.h>
38 #include <gtk/gtkstyle.h>
39 #include <gtk/gtkarrow.h>
40 #include <gtk/gtkeventbox.h>
41 #include <gtk/gtkstatusbar.h>
42 #include <gtk/gtkmenuitem.h>
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48
49 #include "main.h"
50 #include "menu.h"
51 #include "mainwindow.h"
52 #include "folderview.h"
53 #include "summaryview.h"
54 #include "messageview.h"
55 #include "foldersel.h"
56 #include "procmsg.h"
57 #include "procheader.h"
58 #include "sourcewindow.h"
59 #include "prefs_common.h"
60 #include "prefs_summary_column.h"
61 #include "prefs_filtering.h"
62 #include "account.h"
63 #include "compose.h"
64 #include "utils.h"
65 #include "gtkutils.h"
66 #include "stock_pixmap.h"
67 #include "filesel.h"
68 #include "alertpanel.h"
69 #include "inputdialog.h"
70 #include "statusbar.h"
71 #include "folder.h"
72 #include "colorlabel.h"
73 #include "inc.h"
74 #include "imap.h"
75 #include "addressbook.h"
76 #include "addr_compl.h"
77 #include "folder_item_prefs.h"
78 #include "filtering.h"
79 #include "string_match.h"
80 #include "toolbar.h"
81 #include "news.h"
82 #include "hooks.h"
83 #include "description_window.h"
84 #include "folderutils.h"
85 #include "quicksearch.h"
86 #include "partial_download.h"
87
88 #define SUMMARY_COL_MARK_WIDTH          10
89 #define SUMMARY_COL_STATUS_WIDTH        13
90 #define SUMMARY_COL_LOCKED_WIDTH        13
91 #define SUMMARY_COL_MIME_WIDTH          11
92
93
94 static GtkStyle *bold_style;
95 static GtkStyle *bold_marked_style;
96 static GtkStyle *bold_deleted_style;
97 static GtkStyle *small_style;
98 static GtkStyle *small_marked_style;
99 static GtkStyle *small_deleted_style;
100
101 static GdkPixmap *markxpm;
102 static GdkBitmap *markxpmmask;
103 static GdkPixmap *deletedxpm;
104 static GdkBitmap *deletedxpmmask;
105
106 static GdkPixmap *newxpm;
107 static GdkBitmap *newxpmmask;
108 static GdkPixmap *unreadxpm;
109 static GdkBitmap *unreadxpmmask;
110 static GdkPixmap *repliedxpm;
111 static GdkBitmap *repliedxpmmask;
112 static GdkPixmap *forwardedxpm;
113 static GdkBitmap *forwardedxpmmask;
114 static GdkPixmap *ignorethreadxpm;
115 static GdkBitmap *ignorethreadxpmmask;
116 static GdkPixmap *lockedxpm;
117 static GdkBitmap *lockedxpmmask;
118
119 static GdkPixmap *clipxpm;
120 static GdkBitmap *clipxpmmask;
121 static GdkPixmap *keyxpm;
122 static GdkBitmap *keyxpmmask;
123 static GdkPixmap *clipkeyxpm;
124 static GdkBitmap *clipkeyxpmmask;
125 static GdkPixmap *gpgsignedxpm;
126 static GdkBitmap *gpgsignedxpmmask;
127 static GdkPixmap *clipgpgsignedxpm;
128 static GdkBitmap *clipgpgsignedxpmmask;
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_unlock_row          (SummaryView            *summaryview,
192                                          GtkCTreeNode           *row);
193 static void summary_mark_row_as_read    (SummaryView            *summaryview,
194                                          GtkCTreeNode           *row);
195 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
196                                          GtkCTreeNode           *row);
197 static void summary_delete_row          (SummaryView            *summaryview,
198                                          GtkCTreeNode           *row);
199 static void summary_unmark_row          (SummaryView            *summaryview,
200                                          GtkCTreeNode           *row);
201 static void summary_move_row_to         (SummaryView            *summaryview,
202                                          GtkCTreeNode           *row,
203                                          FolderItem             *to_folder);
204 static void summary_copy_row_to         (SummaryView            *summaryview,
205                                          GtkCTreeNode           *row,
206                                          FolderItem             *to_folder);
207
208 static void summary_execute_move        (SummaryView            *summaryview);
209 static void summary_execute_move_func   (GtkCTree               *ctree,
210                                          GtkCTreeNode           *node,
211                                          gpointer                data);
212 static void summary_execute_copy        (SummaryView            *summaryview);
213 static void summary_execute_copy_func   (GtkCTree               *ctree,
214                                          GtkCTreeNode           *node,
215                                          gpointer                data);
216 static void summary_execute_delete      (SummaryView            *summaryview);
217 static void summary_execute_delete_func (GtkCTree               *ctree,
218                                          GtkCTreeNode           *node,
219                                          gpointer                data);
220
221 static void summary_thread_init         (SummaryView            *summaryview);
222 static void summary_ignore_thread       (SummaryView            *summaryview);
223 static void summary_unignore_thread     (SummaryView            *summaryview);
224
225 static void summary_unthread_for_exec           (SummaryView    *summaryview);
226 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
227                                                  GtkCTreeNode   *node,
228                                                  gpointer        data);
229
230 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
231                               GSList * mlist);
232
233 #if 0
234 void summary_processing(SummaryView *summaryview, GSList * mlist);
235 #endif
236 static void summary_filter_func         (GtkCTree               *ctree,
237                                          GtkCTreeNode           *node,
238                                          gpointer                data);
239
240 static void summary_colorlabel_menu_item_activate_cb
241                                           (GtkWidget    *widget,
242                                            gpointer      data);
243 static void summary_colorlabel_menu_item_activate_item_cb
244                                           (GtkMenuItem  *label_menu_item,
245                                            gpointer      data);
246 static void summary_colorlabel_menu_create(SummaryView  *summaryview);
247
248 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
249
250 /* callback functions */
251 static gint summary_toggle_pressed      (GtkWidget              *eventbox,
252                                          GdkEventButton         *event,
253                                          SummaryView            *summaryview);
254 static gboolean summary_button_pressed  (GtkWidget              *ctree,
255                                          GdkEventButton         *event,
256                                          SummaryView            *summaryview);
257 static gboolean summary_button_released (GtkWidget              *ctree,
258                                          GdkEventButton         *event,
259                                          SummaryView            *summaryview);
260 static gboolean summary_key_pressed     (GtkWidget              *ctree,
261                                          GdkEventKey            *event,
262                                          SummaryView            *summaryview);
263 static void summary_open_row            (GtkSCTree              *sctree,
264                                          SummaryView            *summaryview);
265 static void summary_tree_expanded       (GtkCTree               *ctree,
266                                          GtkCTreeNode           *node,
267                                          SummaryView            *summaryview);
268 static void summary_tree_collapsed      (GtkCTree               *ctree,
269                                          GtkCTreeNode           *node,
270                                          SummaryView            *summaryview);
271 static void summary_selected            (GtkCTree               *ctree,
272                                          GtkCTreeNode           *row,
273                                          gint                    column,
274                                          SummaryView            *summaryview);
275 static void summary_col_resized         (GtkCList               *clist,
276                                          gint                    column,
277                                          gint                    width,
278                                          SummaryView            *summaryview);
279 static void summary_reply_cb            (SummaryView            *summaryview,
280                                          guint                   action,
281                                          GtkWidget              *widget);
282 static void summary_show_all_header_cb  (SummaryView            *summaryview,
283                                          guint                   action,
284                                          GtkWidget              *widget);
285
286 static void summary_add_address_cb      (SummaryView            *summaryview,
287                                          guint                   action,
288                                          GtkWidget              *widget);
289 static void summary_create_filter_cb    (SummaryView            *summaryview,
290                                          guint                   action,
291                                          GtkWidget              *widget);
292 static void summary_create_processing_cb(SummaryView            *summaryview,
293                                          guint                   action,
294                                          GtkWidget              *widget);
295
296 static void summary_mark_clicked        (GtkWidget              *button,
297                                          SummaryView            *summaryview);
298 static void summary_status_clicked      (GtkWidget              *button,
299                                          SummaryView            *summaryview);
300 static void summary_mime_clicked        (GtkWidget              *button,
301                                          SummaryView            *summaryview);
302 static void summary_num_clicked         (GtkWidget              *button,
303                                          SummaryView            *summaryview);
304 static void summary_score_clicked       (GtkWidget *button,
305                                          SummaryView *summaryview);
306 static void summary_size_clicked        (GtkWidget              *button,
307                                          SummaryView            *summaryview);
308 static void summary_date_clicked        (GtkWidget              *button,
309                                          SummaryView            *summaryview);
310 static void summary_from_clicked        (GtkWidget              *button,
311                                          SummaryView            *summaryview);
312 static void summary_subject_clicked     (GtkWidget              *button,
313                                          SummaryView            *summaryview);
314 static void summary_score_clicked       (GtkWidget              *button,
315                                          SummaryView            *summaryview);
316 static void summary_locked_clicked      (GtkWidget              *button,
317                                          SummaryView            *summaryview);
318
319 static void summary_start_drag          (GtkWidget        *widget, 
320                                          int button,
321                                          GdkEvent *event,
322                                          SummaryView      *summaryview);
323 static void summary_drag_data_get       (GtkWidget        *widget,
324                                          GdkDragContext   *drag_context,
325                                          GtkSelectionData *selection_data,
326                                          guint             info,
327                                          guint             time,
328                                          SummaryView      *summaryview);
329
330 /* custom compare functions for sorting */
331
332 static gint summary_cmp_by_mark         (GtkCList               *clist,
333                                          gconstpointer           ptr1,
334                                          gconstpointer           ptr2);
335 static gint summary_cmp_by_status       (GtkCList               *clist,
336                                          gconstpointer           ptr1,
337                                          gconstpointer           ptr2);
338 static gint summary_cmp_by_mime         (GtkCList               *clist,
339                                          gconstpointer           ptr1,
340                                          gconstpointer           ptr2);
341 static gint summary_cmp_by_num          (GtkCList               *clist,
342                                          gconstpointer           ptr1,
343                                          gconstpointer           ptr2);
344 static gint summary_cmp_by_size         (GtkCList               *clist,
345                                          gconstpointer           ptr1,
346                                          gconstpointer           ptr2);
347 static gint summary_cmp_by_date         (GtkCList               *clist,
348                                          gconstpointer           ptr1,
349                                          gconstpointer           ptr2);
350 static gint summary_cmp_by_from         (GtkCList               *clist,
351                                          gconstpointer           ptr1,
352                                          gconstpointer           ptr2);
353 static gint summary_cmp_by_simplified_subject
354                                         (GtkCList               *clist, 
355                                          gconstpointer           ptr1, 
356                                          gconstpointer           ptr2);
357 static gint summary_cmp_by_score        (GtkCList               *clist,
358                                          gconstpointer           ptr1,
359                                          gconstpointer           ptr2);
360 static gint summary_cmp_by_label        (GtkCList               *clist,
361                                          gconstpointer           ptr1,
362                                          gconstpointer           ptr2);
363 static gint summary_cmp_by_to           (GtkCList               *clist,
364                                          gconstpointer           ptr1,
365                                          gconstpointer           ptr2);
366 static gint summary_cmp_by_subject      (GtkCList               *clist,
367                                          gconstpointer           ptr1,
368                                          gconstpointer           ptr2);
369 static gint summary_cmp_by_locked       (GtkCList               *clist,
370                                          gconstpointer           ptr1, 
371                                          gconstpointer           ptr2);
372
373 static void news_flag_crosspost         (MsgInfo *msginfo);
374
375 static void quicksearch_execute_cb      (QuickSearch    *quicksearch,
376                                          gpointer        data);
377 static void tog_searchbar_cb            (GtkWidget      *w,
378                                          gpointer        data);
379
380 static void summary_find_answers        (SummaryView    *summaryview, 
381                                          MsgInfo        *msg);
382
383 static gboolean summary_update_msg      (gpointer source, gpointer data);
384
385 GtkTargetEntry summary_drag_types[1] =
386 {
387         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
388 };
389
390 static GtkItemFactoryEntry summary_popup_entries[] =
391 {
392         {N_("/_Reply"),                 "<control>R", summary_reply_cb, COMPOSE_REPLY, NULL},
393         {N_("/Repl_y to"),              NULL, NULL,             0, "<Branch>"},
394         {N_("/Repl_y to/_all"),         "<shift><control>R", summary_reply_cb,  COMPOSE_REPLY_TO_ALL, NULL},
395         {N_("/Repl_y to/_sender"),      NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
396         {N_("/Repl_y to/mailing _list"),
397                                         "<control>L", summary_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
398         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
399         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
400         {N_("/_Forward"),               "<control><alt>F", summary_reply_cb, COMPOSE_FORWARD, NULL},
401         {N_("/Redirect"),               NULL, summary_reply_cb, COMPOSE_REDIRECT, NULL},
402         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
403         {N_("/M_ove..."),               "<control>O", summary_move_to,  0, NULL},
404         {N_("/_Copy..."),               "<shift><control>O", summary_copy_to,   0, NULL},
405         {N_("/_Delete"),                "<control>D", summary_delete,   0, NULL},
406         {N_("/Cancel a news message"),  NULL, summary_cancel,   0, NULL},
407         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
408         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
409         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
410         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
411         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
412         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
413         {N_("/_Mark/Mark as rea_d"),    NULL, summary_mark_as_read, 0, NULL},
414         {N_("/_Mark/Mark all read"),    NULL, summary_mark_all_read, 0, NULL},
415         {N_("/_Mark/Ignore thread"),    NULL, summary_ignore_thread, 0, NULL},
416         {N_("/_Mark/Unignore thread"),  NULL, summary_unignore_thread, 0, NULL},
417         {N_("/_Mark/Lock"),             NULL, summary_msgs_lock, 0, NULL},
418         {N_("/_Mark/Unlock"),           NULL, summary_msgs_unlock, 0, NULL},
419         {N_("/Color la_bel"),           NULL, NULL,             0, NULL},
420
421         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
422         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
423         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
424         {N_("/Add sender to address boo_k"),
425                                         NULL, summary_add_address_cb, 0, NULL},
426         {N_("/Create f_ilter rule"),    NULL, NULL,             0, "<Branch>"},
427         {N_("/Create f_ilter rule/_Automatically"),
428                                         NULL, summary_create_filter_cb, FILTER_BY_AUTO, NULL},
429         {N_("/Create f_ilter rule/by _From"),
430                                         NULL, summary_create_filter_cb, FILTER_BY_FROM, NULL},
431         {N_("/Create f_ilter rule/by _To"),
432                                         NULL, summary_create_filter_cb, FILTER_BY_TO, NULL},
433         {N_("/Create f_ilter rule/by _Subject"),
434                                         NULL, summary_create_filter_cb, FILTER_BY_SUBJECT, NULL},
435         {N_("/Create processing rule"), NULL, NULL,             0, "<Branch>"},
436         {N_("/Create processing rule/_Automatically"),
437                                         NULL, summary_create_processing_cb, FILTER_BY_AUTO, NULL},
438         {N_("/Create processing rule/by _From"),
439                                         NULL, summary_create_processing_cb, FILTER_BY_FROM, NULL},
440         {N_("/Create processing rule/by _To"),
441                                         NULL, summary_create_processing_cb, FILTER_BY_TO, NULL},
442         {N_("/Create processing rule/by _Subject"),
443                                         NULL, summary_create_processing_cb, FILTER_BY_SUBJECT, NULL},
444         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
445         {N_("/_View"),                  NULL, NULL,             0, "<Branch>"},
446         {N_("/_View/Open in new _window"),
447                                         "<control><alt>N", summary_open_msg,    0, NULL},
448         {N_("/_View/_Source"),          "<control>U", summary_view_source, 0, NULL},
449         {N_("/_View/All _header"),      "<control>H", summary_show_all_header_cb, 0, "<ToggleItem>"},
450 };  /* see also list in menu_connect_identical_items() in menu.c if this changes */
451
452 static const gchar *const col_label[N_SUMMARY_COLS] = {
453         N_("M"),        /* S_COL_MARK    */
454         N_("S"),        /* S_COL_STATUS  */
455         "",             /* S_COL_MIME    */
456         N_("Subject"),  /* S_COL_SUBJECT */
457         N_("From"),     /* S_COL_FROM    */
458         N_("Date"),     /* S_COL_DATE    */
459         N_("Size"),     /* S_COL_SIZE    */
460         N_("No."),      /* S_COL_NUMBER  */
461         N_("Score"),    /* S_COL_SCORE   */
462         N_("L")         /* S_COL_LOCKED  */
463 };
464
465 SummaryView *summary_create(void)
466 {
467         SummaryView *summaryview;
468         GtkWidget *vbox;
469         GtkWidget *scrolledwin;
470         GtkWidget *ctree;
471         GtkWidget *hbox;
472         GtkWidget *hbox_l;
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 *toggle_search;
481         GtkTooltips *search_tip;
482         GtkItemFactory *popupfactory;
483         gint n_entries;
484         QuickSearch *quicksearch;
485
486         debug_print("Creating summary view...\n");
487         summaryview = g_new0(SummaryView, 1);
488
489 #define SUMMARY_VBOX_SPACING 3
490         vbox = gtk_vbox_new(FALSE, SUMMARY_VBOX_SPACING);
491         
492         /* create status label */
493         hbox = gtk_hbox_new(FALSE, 0);
494         gtk_widget_show(hbox);
495
496         search_tip = gtk_tooltips_new();
497         toggle_search = gtk_toggle_button_new();
498         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_search),
499                                      prefs_common.show_searchbar);
500         gtk_widget_show(toggle_search);
501
502         gtk_tooltips_set_tip(GTK_TOOLTIPS(search_tip),
503                              toggle_search,
504                              _("Toggle quick-search bar"), NULL);
505         
506         gtk_box_pack_start(GTK_BOX(hbox), toggle_search, FALSE, FALSE, 2);      
507
508         hbox_l = gtk_hbox_new(FALSE, 0);
509         gtk_widget_show(hbox_l);
510         gtk_box_pack_start(GTK_BOX(hbox), hbox_l, TRUE, TRUE, 0);
511  
512         statlabel_folder = gtk_label_new("");
513         gtk_widget_show(statlabel_folder);
514         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
515         statlabel_select = gtk_label_new("");
516         gtk_widget_show(statlabel_select);
517         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
518  
519         /* toggle view button */
520         toggle_eventbox = gtk_event_box_new();
521         gtk_widget_show(toggle_eventbox);
522         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
523         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
524         gtk_widget_show(toggle_arrow);
525         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
526         g_signal_connect(G_OBJECT(toggle_eventbox), "button_press_event",
527                          G_CALLBACK(summary_toggle_pressed),
528                          summaryview);
529         
530         
531         statlabel_msgs = gtk_label_new("");
532         gtk_widget_show(statlabel_msgs);
533         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
534
535         hbox_spc = gtk_hbox_new(FALSE, 0);
536         gtk_widget_show(hbox_spc);
537         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
538
539         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
540         gtk_widget_show(scrolledwin);
541         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
542                                        GTK_POLICY_AUTOMATIC,
543                                        GTK_POLICY_AUTOMATIC);
544         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
545         gtk_widget_set_size_request(vbox,
546                              prefs_common.summaryview_width,
547                              prefs_common.summaryview_height);
548
549         ctree = summary_ctree_create(summaryview);
550         gtk_widget_show(ctree);
551
552         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
553                                             GTK_CLIST(ctree)->hadjustment);
554         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
555                                             GTK_CLIST(ctree)->vadjustment);
556         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
557
558         /* status label */
559         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
560
561         /* quick search */
562         quicksearch = quicksearch_new();
563         gtk_box_pack_start(GTK_BOX(vbox), quicksearch_get_widget(quicksearch), FALSE, FALSE, 0);
564
565         quicksearch_set_execute_callback(quicksearch, quicksearch_execute_cb, summaryview);
566
567         g_signal_connect (G_OBJECT(toggle_search), "toggled",
568                           G_CALLBACK(tog_searchbar_cb), summaryview);
569
570         /* create popup menu */
571         n_entries = sizeof(summary_popup_entries) /
572                 sizeof(summary_popup_entries[0]);
573         popupmenu = menu_create_items(summary_popup_entries, n_entries,
574                                       "<SummaryView>", &popupfactory,
575                                       summaryview);
576
577         summaryview->vbox = vbox;
578         summaryview->scrolledwin = scrolledwin;
579         summaryview->ctree = ctree;
580         summaryview->hbox = hbox;
581         summaryview->hbox_l = hbox_l;
582         summaryview->statlabel_folder = statlabel_folder;
583         summaryview->statlabel_select = statlabel_select;
584         summaryview->statlabel_msgs = statlabel_msgs;
585         summaryview->toggle_eventbox = toggle_eventbox;
586         summaryview->toggle_arrow = toggle_arrow;
587         summaryview->toggle_search = toggle_search;
588         summaryview->popupmenu = popupmenu;
589         summaryview->popupfactory = popupfactory;
590         summaryview->lock_count = 0;
591         summaryview->msginfo_update_callback_id =
592                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
593
594         summaryview->target_list = gtk_target_list_new(summary_drag_types, 1);
595
596         summaryview->quicksearch = quicksearch;
597
598         /* CLAWS: need this to get the SummaryView * from
599          * the CList */
600         g_object_set_data(G_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
601
602         gtk_widget_show_all(vbox);
603
604         gtk_widget_show(vbox);
605
606         if (prefs_common.show_searchbar)
607                 quicksearch_show(quicksearch);
608         else
609                 quicksearch_hide(quicksearch);
610         
611         return summaryview;
612 }
613
614 void summary_init(SummaryView *summaryview)
615 {
616         GtkWidget *pixmap;
617         PangoFontDescription *font_desc;
618         gint size;
619
620         gtk_widget_realize(summaryview->ctree);
621         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
622                          &markxpm, &markxpmmask);
623         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
624                          &deletedxpm, &deletedxpmmask);
625         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
626                          &newxpm, &newxpmmask);
627         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
628                          &unreadxpm, &unreadxpmmask);
629         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
630                          &repliedxpm, &repliedxpmmask);
631         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
632                          &forwardedxpm, &forwardedxpmmask);
633         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
634                          &clipxpm, &clipxpmmask);
635         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
636                          &lockedxpm, &lockedxpmmask);
637         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
638                          &ignorethreadxpm, &ignorethreadxpmmask);
639         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
640                          &clipkeyxpm, &clipkeyxpmmask);
641         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
642                          &keyxpm, &keyxpmmask);
643         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_GPG_SIGNED,
644                          &gpgsignedxpm, &gpgsignedxpmmask);
645         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_GPG_SIGNED,
646                          &clipgpgsignedxpm, &clipgpgsignedxpmmask);
647
648         font_desc = pango_font_description_from_string(NORMAL_FONT);
649         gtk_widget_modify_font(summaryview->ctree, font_desc);
650         pango_font_description_free(font_desc);
651
652         if (!bold_style) {
653                 bold_style = gtk_style_copy
654                         (gtk_widget_get_style(summaryview->ctree));
655                 font_desc = pango_font_description_from_string(BOLD_FONT);
656                 if (font_desc) {
657                         pango_font_description_free(bold_style->font_desc);
658                         bold_style->font_desc = font_desc;
659                 }
660                 
661                 pango_font_description_set_weight
662                                 (bold_style->font_desc, PANGO_WEIGHT_BOLD);
663                 bold_marked_style = gtk_style_copy(bold_style);
664                 bold_marked_style->fg[GTK_STATE_NORMAL] =
665                         summaryview->color_marked;
666                 bold_deleted_style = gtk_style_copy(bold_style);
667                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
668                         summaryview->color_dim;
669         }
670
671         font_desc = pango_font_description_new();
672         size = pango_font_description_get_size
673                 (summaryview->statlabel_folder->style->font_desc);
674         pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
675         gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
676         gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
677         gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
678         pango_font_description_free(font_desc);
679
680         pixmap = stock_pixmap_widget(summaryview->hbox_l, STOCK_PIXMAP_DIR_OPEN);
681         gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), pixmap, FALSE, FALSE, 4);
682         gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), pixmap, 0);
683         gtk_widget_show(pixmap);
684         summaryview->folder_pixmap = pixmap;
685
686         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
687         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
688         gtk_widget_show(pixmap);
689         summaryview->quick_search_pixmap = pixmap;
690
691         /* Init summaryview prefs */
692         summaryview->sort_key = SORT_BY_NONE;
693         summaryview->sort_type = SORT_ASCENDING;
694
695         /* Init summaryview extra data */
696         summaryview->simplify_subject_preg = NULL;
697
698         summary_clear_list(summaryview);
699         summary_set_column_titles(summaryview);
700         summary_colorlabel_menu_create(summaryview);
701         summary_set_menu_sensitive(summaryview);
702
703 }
704
705 #if 0
706 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
707                                                  GtkCTreeNode *current_node)
708 {
709         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
710         GtkCTreeNode *node;
711         MsgInfo *msginfo;
712         gint best_score = MIN_SCORE;
713         GtkCTreeNode *best_node = NULL;
714
715         if (current_node)
716                 /*node = current_node;*/
717                 node = GTK_CTREE_NODE_NEXT(current_node);
718         else
719                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
720
721         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
722                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
723                 if (msginfo->score >= summaryview->important_score)
724                         break;
725                 if (msginfo->score > best_score) {
726                         best_score = msginfo->score;
727                         best_node = node;
728                 }
729         }
730
731         if (node != NULL)
732                 return node;
733         else
734                 return best_node;
735 }
736
737 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
738                                                  GtkCTreeNode *current_node)
739 {
740         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
741         GtkCTreeNode *node;
742         MsgInfo *msginfo;
743         gint best_score = MIN_SCORE;
744         GtkCTreeNode *best_node = NULL;
745
746         if (current_node)
747                 /*node = current_node;*/
748                 node = GTK_CTREE_NODE_PREV(current_node);
749         else
750                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
751
752         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
753                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
754                 if (msginfo->score >= summaryview->important_score)
755                         break;
756                 if (msginfo->score > best_score) {
757                         best_score = msginfo->score;
758                         best_node = node;
759                 }
760         }
761
762         if (node != NULL)
763                 return node;
764         else
765                 return best_node;
766 }
767 #endif
768
769 #define CURRENTLY_DISPLAYED(m) \
770 ( (m->msgnum == displayed_msgnum) \
771   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
772
773 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
774 {
775         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
776         GtkCTreeNode *node;
777         GSList *mlist = NULL;
778         gchar *buf;
779         gboolean is_refresh;
780         guint selected_msgnum = 0;
781         guint displayed_msgnum = 0;
782         GSList *cur;
783         GSList *not_killed;
784
785         if (summary_is_locked(summaryview)) return FALSE;
786
787         inc_lock();
788         summary_lock(summaryview);
789
790         if (!prefs_common.summary_quicksearch_sticky
791          && !prefs_common.summary_quicksearch_recurse
792          && !quicksearch_is_running(summaryview->quicksearch)) {
793                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
794         }
795
796         /* STATUSBAR_POP(summaryview->mainwin); */
797
798         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
799         if (is_refresh) {
800                 selected_msgnum = summary_get_msgnum(summaryview,
801                                                      summaryview->selected);
802                 displayed_msgnum = summary_get_msgnum(summaryview,
803                                                       summaryview->displayed);
804         }
805
806         /* process the marks if any */
807         if (summaryview->mainwin->lock_count == 0 &&
808             (summaryview->moved > 0 || summaryview->copied > 0)) {
809                 AlertValue val;
810                 gboolean changed = FALSE;
811
812                 val = alertpanel(_("Process mark"),
813                                  _("Some marks are left. Process it?"),
814                                  GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL);
815                 if (G_ALERTDEFAULT == val) {
816                         summary_unlock(summaryview);
817                         summary_execute(summaryview);
818                         summary_lock(summaryview);
819                         changed = TRUE;
820                 } else if (G_ALERTALTERNATE == val) {
821                         /* DO NOTHING */
822                 } else {
823                         summary_unlock(summaryview);
824                         inc_unlock();
825                         return FALSE;
826                 }
827                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
828                         folder_update_op_count();
829         }
830         
831         gtk_clist_freeze(GTK_CLIST(ctree));
832
833         summary_clear_list(summaryview);
834         summary_set_column_titles(summaryview);
835
836         buf = NULL;
837         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
838                 g_free(buf);
839                 debug_print("empty folder\n\n");
840                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
841                 summary_clear_all(summaryview);
842                 summaryview->folder_item = item;
843                 gtk_clist_thaw(GTK_CLIST(ctree));
844                 summary_unlock(summaryview);
845                 inc_unlock();
846                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
847                         main_window_cursor_wait(summaryview->mainwin);
848                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
849                         if (quicksearch_is_active(summaryview->quicksearch))
850                                 quicksearch_search_subfolders(summaryview->quicksearch, 
851                                               summaryview->folderview,
852                                               summaryview->folder_item);
853                         main_window_cursor_normal(summaryview->mainwin);
854                 }                       
855                 return TRUE;
856         }
857         g_free(buf);
858
859         if (!is_refresh)
860                 messageview_clear(summaryview->messageview);
861
862         summaryview->folder_item = item;
863         item->opened = TRUE;
864
865         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
866         debug_print("%s\n", buf);
867         STATUSBAR_PUSH(summaryview->mainwin, buf);
868         g_free(buf);
869
870         main_window_cursor_wait(summaryview->mainwin);
871
872 /*
873         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
874
875         !!! NEW !!!
876         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
877         BY OTHER PROGRAMS TO THE FOLDER
878 */
879         mlist = folder_item_get_msg_list(item);
880
881         if (summaryview->folder_item->hide_read_msgs) {
882                 GSList *not_killed;
883                 
884                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
885                 not_killed = NULL;
886                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
887                         MsgInfo * msginfo = (MsgInfo *) cur->data;
888                         
889                         if ((MSG_IS_UNREAD(msginfo->flags)
890                              || MSG_IS_MARKED(msginfo->flags)
891                              || MSG_IS_LOCKED(msginfo->flags)
892                              || CURRENTLY_DISPLAYED(msginfo))
893                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
894                                 not_killed = g_slist_prepend(not_killed, msginfo);
895                         else
896                                 procmsg_msginfo_free(msginfo);
897                 }
898                 g_slist_free(mlist);
899                 mlist = not_killed;
900         } else {
901                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
902         }
903
904         if (quicksearch_is_active(summaryview->quicksearch)) {
905                 GSList *not_killed;
906                 
907                 not_killed = NULL;
908                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
909                         MsgInfo * msginfo = (MsgInfo *) cur->data;
910
911                         if (quicksearch_match(summaryview->quicksearch, msginfo))
912                                 not_killed = g_slist_prepend(not_killed, msginfo);
913                         else
914                                 procmsg_msginfo_free(msginfo);
915                 }
916                 
917                 if (quicksearch_is_running(summaryview->quicksearch)) {
918                         /* only scan subfolders when quicksearch changed,
919                          * not when search is the same and folder changed */
920                         main_window_cursor_wait(summaryview->mainwin);
921                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
922                         quicksearch_search_subfolders(summaryview->quicksearch, 
923                                               summaryview->folderview,
924                                               summaryview->folder_item);
925                         main_window_cursor_normal(summaryview->mainwin);
926                 }
927                 
928                 g_slist_free(mlist);
929                 mlist = not_killed;
930         }
931
932 #if 0   
933         if ((global_scoring || item->prefs->scoring)) {
934                 GSList *not_killed;
935                 gint kill_score;
936
937                 not_killed = NULL;
938                 kill_score = prefs_common.kill_score;
939                 if (item->prefs->kill_score > kill_score)
940                         kill_score = item->prefs->kill_score;
941                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
942                         MsgInfo * msginfo = (MsgInfo *) cur->data;
943
944                         if (msginfo->score > kill_score)
945                                 not_killed = g_slist_prepend(not_killed, msginfo);
946                         else
947                                 procmsg_msginfo_free(msginfo);
948                 }
949                 g_slist_free(mlist);
950                 mlist = not_killed;
951         }
952 #endif
953         not_killed = NULL;
954         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
955                 MsgInfo * msginfo = (MsgInfo *) cur->data;
956                 
957                 if (!msginfo->hidden)
958                         not_killed = g_slist_prepend(not_killed, msginfo);
959                 else
960                         procmsg_msginfo_free(msginfo);
961         }
962         g_slist_free(mlist);
963         mlist = not_killed;
964
965         STATUSBAR_POP(summaryview->mainwin);
966
967         /* set ctree and hash table from the msginfo list, and
968            create the thread */
969         summary_set_ctree_from_list(summaryview, mlist);
970
971         g_slist_free(mlist);
972
973         gtk_clist_thaw(GTK_CLIST(ctree));
974
975         if (is_refresh) {
976                 summaryview->displayed =
977                         summary_find_msg_by_msgnum(summaryview,
978                                                    displayed_msgnum);
979                 if (!summaryview->displayed)
980                         messageview_clear(summaryview->messageview);
981                 summary_select_by_msgnum(summaryview, selected_msgnum);
982                 if (!summaryview->selected) {
983                         /* no selected message - select first unread
984                            message, but do not display it */
985                         node = summary_find_next_flagged_msg(summaryview, NULL,
986                                                              MSG_UNREAD, FALSE);
987                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
988                                 node = gtk_ctree_node_nth
989                                         (ctree,
990                                          item->sort_type == SORT_DESCENDING
991                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
992                         summary_select_node(summaryview, node, FALSE, TRUE);
993                 }
994         } else {
995                 /* select first unread message */
996 #if 0
997                 if (summaryview->sort_key == SORT_BY_SCORE)
998                         node = summary_find_next_important_score(summaryview,
999                                                                  NULL);
1000                 else
1001 #endif
1002                 node = summary_find_next_flagged_msg(summaryview, NULL,
1003                                                      MSG_UNREAD, FALSE);
1004                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1005                         node = gtk_ctree_node_nth
1006                                 (ctree,
1007                                  item->sort_type == SORT_DESCENDING
1008                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1009                 }
1010                 if (prefs_common.open_unread_on_enter ||
1011                     prefs_common.always_show_msg) {
1012                         summary_unlock(summaryview);
1013                         summary_select_node(summaryview, node, 
1014                                             messageview_is_visible(summaryview->messageview), 
1015                                             TRUE);
1016                         summary_lock(summaryview);
1017                 } else
1018                         summary_select_node(summaryview, node, FALSE, TRUE);
1019         }
1020
1021         summary_set_column_titles(summaryview);
1022         summary_status_show(summaryview);
1023         summary_set_menu_sensitive(summaryview);
1024         toolbar_main_set_sensitive(summaryview->mainwin);
1025
1026         debug_print("\n");
1027         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1028         STATUSBAR_POP(summaryview->mainwin);
1029         main_window_cursor_normal(summaryview->mainwin);
1030         summary_unlock(summaryview);
1031         inc_unlock();
1032
1033         return TRUE;
1034 }
1035
1036 #undef CURRENTLY_DISPLAYED
1037
1038 void summary_clear_list(SummaryView *summaryview)
1039 {
1040         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1041         gint optimal_width;
1042
1043         gtk_clist_freeze(clist);
1044
1045         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1046                                 NULL, summary_free_msginfo_func, NULL);
1047
1048         if (summaryview->folder_item) {
1049                 summaryview->folder_item->opened = FALSE;
1050                 summaryview->folder_item = NULL;
1051         }
1052
1053         summaryview->display_msg = FALSE;
1054
1055         summaryview->selected = NULL;
1056         summaryview->displayed = NULL;
1057         summaryview->total_size = 0;
1058         summaryview->deleted = summaryview->moved = 0;
1059         summaryview->copied = 0;
1060         if (summaryview->msgid_table) {
1061                 g_hash_table_destroy(summaryview->msgid_table);
1062                 summaryview->msgid_table = NULL;
1063         }
1064         if (summaryview->subject_table) {
1065                 g_hash_table_destroy(summaryview->subject_table);
1066                 summaryview->subject_table = NULL;
1067         }
1068         summaryview->mlist = NULL;
1069
1070         gtk_clist_clear(clist);
1071         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1072                 optimal_width = gtk_clist_optimal_column_width
1073                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1074                 gtk_clist_set_column_width
1075                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1076                          optimal_width);
1077         }
1078
1079         gtk_clist_thaw(clist);
1080 }
1081
1082 void summary_clear_all(SummaryView *summaryview)
1083 {
1084         messageview_clear(summaryview->messageview);
1085         summary_clear_list(summaryview);
1086         summary_set_menu_sensitive(summaryview);
1087         toolbar_main_set_sensitive(summaryview->mainwin);
1088         summary_status_show(summaryview);
1089 }
1090
1091 void summary_lock(SummaryView *summaryview)
1092 {
1093         summaryview->lock_count++;
1094 }
1095
1096 void summary_unlock(SummaryView *summaryview)
1097 {
1098         if (summaryview->lock_count)
1099                 summaryview->lock_count--;
1100 }
1101
1102 gboolean summary_is_locked(SummaryView *summaryview)
1103 {
1104         return summaryview->lock_count > 0;
1105 }
1106
1107 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1108 {
1109         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1110         SummarySelection selection;
1111
1112         if (!clist->row_list)
1113                 selection = SUMMARY_NONE;
1114         else if (!clist->selection)
1115                 selection = SUMMARY_SELECTED_NONE;
1116         else if (!clist->selection->next)
1117                 selection = SUMMARY_SELECTED_SINGLE;
1118         else
1119                 selection = SUMMARY_SELECTED_MULTIPLE;
1120
1121         return selection;
1122 }
1123
1124 /*!
1125  *\return       MsgInfo * Selected message if there's one selected;
1126  *              if multiple selected, or none, return NULL.
1127  */ 
1128 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1129 {
1130         /* summaryview->selected may be valid when multiple 
1131          * messages were selected */
1132         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1133
1134         if (sellist == NULL || sellist->next) 
1135                 return NULL;
1136         
1137         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1138 }
1139
1140 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1141 {
1142         GSList *mlist = NULL;
1143         GList *cur;
1144         MsgInfo *msginfo;
1145
1146         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
1147              cur = cur->next) {
1148                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1149                 mlist = g_slist_prepend(mlist, msginfo);
1150         }
1151
1152         mlist = g_slist_reverse(mlist);
1153
1154         return mlist;
1155 }
1156
1157 GSList *summary_get_msg_list(SummaryView *summaryview)
1158 {
1159         GSList *mlist = NULL;
1160         GtkCTree *ctree;
1161         GtkCTreeNode *node;
1162         MsgInfo *msginfo;
1163
1164         ctree = GTK_CTREE(summaryview->ctree);
1165
1166         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1167              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1168                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1169                 mlist = g_slist_prepend(mlist, msginfo);
1170         }
1171
1172         mlist = g_slist_reverse(mlist);
1173
1174         return mlist;
1175 }
1176
1177 static void summary_set_menu_sensitive(SummaryView *summaryview)
1178 {
1179         GtkItemFactory *ifactory = summaryview->popupfactory;
1180         SensitiveCond state;
1181         gboolean sensitive;
1182         GtkWidget *menuitem;
1183         gint i;
1184
1185         static const struct {
1186                 gchar *const entry;
1187                 SensitiveCond cond;
1188         } entry[] = {
1189                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1190                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1191                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1192                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1193                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1194                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1195
1196                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1197                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1198
1199                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1200
1201                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1202                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1203                 {"/Delete"                      , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1204                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1205
1206                 {"/Mark"                        , M_TARGET_EXIST},
1207                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1208                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1209                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1210                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1211                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1212                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1213                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1214                 {"/Color label"                 , M_TARGET_EXIST},
1215
1216                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1217                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1218                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1219
1220                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1221                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1222                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1223                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1224 #if 0
1225                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1226                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1227
1228                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1229                 {"/Select all"                  , M_TARGET_EXIST},
1230 #endif
1231                 {NULL, 0}
1232         };
1233
1234         main_window_set_menu_sensitive(summaryview->mainwin);
1235
1236         state = main_window_get_current_state(summaryview->mainwin);
1237
1238         for (i = 0; entry[i].entry != NULL; i++) {
1239                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1240                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1241         }
1242
1243
1244         summary_lock(summaryview);
1245         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1246         gtk_check_menu_item_set_active
1247                 (GTK_CHECK_MENU_ITEM(menuitem),
1248                  summaryview->messageview->mimeview->textview->show_all_headers);
1249         summary_unlock(summaryview);
1250 }
1251
1252 void summary_select_prev_unread(SummaryView *summaryview)
1253 {
1254         GtkCTreeNode *node;
1255         gboolean skip_cur = FALSE;
1256
1257         if (summaryview->displayed 
1258         &&  summaryview->selected == summaryview->displayed) {
1259                 debug_print("skipping current\n");
1260                 skip_cur = TRUE;
1261         }
1262
1263         node = summary_find_prev_flagged_msg
1264                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1265
1266         if (!node) {
1267                 AlertValue val = 0;
1268
1269                 switch (prefs_common.next_unread_msg_dialog) {
1270                         case NEXTUNREADMSGDIALOG_ALWAYS:
1271                                 val = alertpanel(_("No more unread messages"),
1272                                                  _("No unread message found. "
1273                                                    "Search from the end?"),
1274                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1275                                 break;
1276                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1277                                 val = G_ALERTDEFAULT;
1278                                 break;
1279                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1280                                 val = !G_ALERTDEFAULT;
1281                                 break;
1282                         default:
1283                                 debug_print(
1284                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1285                 }
1286                 if (val != G_ALERTDEFAULT) return;
1287                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1288                                                      MSG_UNREAD, FALSE);
1289         }
1290
1291         if (!node)
1292                 alertpanel_notice(_("No unread messages."));
1293         else
1294                 summary_select_node(summaryview, node, TRUE, FALSE);
1295 }
1296
1297 void summary_select_next_unread(SummaryView *summaryview)
1298 {
1299         GtkCTreeNode *node = summaryview->selected;
1300         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1301         gboolean skip_cur = FALSE;
1302         
1303         if (summaryview->displayed 
1304         &&  summaryview->selected == summaryview->displayed) {
1305                 debug_print("skipping cur\n");
1306                 skip_cur = TRUE;
1307         }
1308
1309
1310         node = summary_find_next_flagged_msg
1311                 (summaryview, node, MSG_UNREAD, skip_cur);
1312         
1313         if (node)
1314                 summary_select_node(summaryview, node, TRUE, FALSE);
1315         else {
1316                 node = summary_find_next_flagged_msg
1317                         (summaryview, NULL, MSG_UNREAD, FALSE);
1318                 if (node == NULL) {
1319                         AlertValue val = 0;
1320
1321                         switch (prefs_common.next_unread_msg_dialog) {
1322                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1323                                         val = alertpanel(_("No more unread messages"),
1324                                                          _("No unread message found. "
1325                                                            "Go to next folder?"),
1326                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1327                                         break;
1328                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1329                                         val = G_ALERTDEFAULT;
1330                                         break;
1331                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1332                                         val = G_ALERTOTHER;
1333                                         break;
1334                                 default:
1335                                         debug_print(
1336                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1337                         }
1338
1339                         if (val == G_ALERTDEFAULT) {
1340                                 g_signal_stop_emission_by_name(G_OBJECT(ctree),
1341                                                          "key_press_event");
1342                                 folderview_select_next_unread(summaryview->folderview);
1343                                 return;
1344                         } 
1345                         else
1346                                 return;
1347                 } else
1348                         summary_select_node(summaryview, node, TRUE, FALSE);
1349
1350         }
1351 }
1352
1353 void summary_select_prev_new(SummaryView *summaryview)
1354 {
1355         GtkCTreeNode *node;
1356
1357         node = summary_find_prev_flagged_msg
1358                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1359
1360         if (!node) {
1361                 AlertValue val;
1362
1363                 val = alertpanel(_("No more new messages"),
1364                                  _("No new message found. "
1365                                    "Search from the end?"),
1366                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1367                 if (val != G_ALERTDEFAULT) return;
1368                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1369                                                      MSG_NEW, FALSE);
1370         }
1371
1372         if (!node)
1373                 alertpanel_notice(_("No new messages."));
1374         else
1375                 summary_select_node(summaryview, node, TRUE, FALSE);
1376 }
1377
1378 void summary_select_next_new(SummaryView *summaryview)
1379 {
1380         GtkCTreeNode *node = summaryview->selected;
1381         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1382
1383         while ((node = summary_find_next_flagged_msg
1384                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1385                 AlertValue val;
1386
1387                 val = alertpanel(_("No more new messages"),
1388                                  _("No new message found. "
1389                                    "Go to next folder?"),
1390                                  GTK_STOCK_YES, _("Search again"),
1391                                  GTK_STOCK_NO);
1392                 if (val == G_ALERTDEFAULT) {
1393                         g_signal_stop_emission_by_name(G_OBJECT(ctree),"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                                  GTK_STOCK_YES, GTK_STOCK_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                                  GTK_STOCK_YES, GTK_STOCK_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                                  GTK_STOCK_YES, GTK_STOCK_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                                  GTK_STOCK_YES, GTK_STOCK_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 (msginfo && !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 (msginfo && !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 && (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 && (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 && 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 (msginfo->folder && msginfo->folder->folder &&
1773             msginfo->folder->folder->klass->type == F_NEWS)
1774                 news_flag_crosspost(msginfo);
1775
1776         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1777         && procmsg_msg_has_marked_parent(msginfo))
1778                 summaryview->unreadmarked++;
1779
1780         if (MSG_IS_DELETED(msginfo->flags))
1781                 summaryview->deleted++;
1782
1783         summaryview->total_size += msginfo->size;
1784
1785         summary_set_row_marks(summaryview, node);
1786 }
1787
1788 static void summary_update_status(SummaryView *summaryview)
1789 {
1790         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1791         GtkCTreeNode *node;
1792         MsgInfo *msginfo;
1793
1794         summaryview->total_size =
1795         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1796
1797         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1798              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1799                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1800
1801                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1802                 && procmsg_msg_has_marked_parent(msginfo))
1803                         summaryview->unreadmarked++;
1804                 if (MSG_IS_DELETED(msginfo->flags))
1805                         summaryview->deleted++;
1806                 if (MSG_IS_MOVE(msginfo->flags))
1807                         summaryview->moved++;
1808                 if (MSG_IS_COPY(msginfo->flags))
1809                         summaryview->copied++;
1810                 summaryview->total_size += msginfo->size;
1811         }
1812 }
1813
1814 static void summary_status_show(SummaryView *summaryview)
1815 {
1816         gchar *str;
1817         gchar *del, *mv, *cp;
1818         gchar *sel;
1819         gchar *spc;
1820         gchar *itstr;
1821         GList *rowlist, *cur;
1822         guint n_selected = 0;
1823         off_t sel_size = 0;
1824         MsgInfo *msginfo;
1825         gchar *name;
1826         
1827         if (!summaryview->folder_item) {
1828                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1829                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1830                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1831                 return;
1832         }
1833
1834         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1835         for (cur = rowlist; cur != NULL; cur = cur->next) {
1836                 msginfo = gtk_ctree_node_get_row_data
1837                         (GTK_CTREE(summaryview->ctree),
1838                          GTK_CTREE_NODE(cur->data));
1839                 if (!msginfo)
1840                         g_warning("summary_status_show(): msginfo == NULL\n");
1841                 else {
1842                         sel_size += msginfo->size;
1843                         n_selected++;
1844                 }
1845         }
1846
1847         name = folder_item_get_name(summaryview->folder_item);
1848         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1849         g_free(name);
1850
1851         if (summaryview->deleted)
1852                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1853         else
1854                 del = g_strdup("");
1855         if (summaryview->moved)
1856                 mv = g_strdup_printf(_("%s%d moved"),
1857                                      summaryview->deleted ? _(", ") : "",
1858                                      summaryview->moved);
1859         else
1860                 mv = g_strdup("");
1861         if (summaryview->copied)
1862                 cp = g_strdup_printf(_("%s%d copied"),
1863                                      summaryview->deleted ||
1864                                      summaryview->moved ? _(", ") : "",
1865                                      summaryview->copied);
1866         else
1867                 cp = g_strdup("");
1868
1869         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1870                 spc = "    ";
1871         else
1872                 spc = "";
1873
1874         if (n_selected) {
1875                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1876                 if (n_selected == 1)
1877                         itstr = g_strdup(_(" item selected"));
1878                 else
1879                         itstr = g_strdup(_(" items selected"));
1880         } else {
1881                 sel = g_strdup("");
1882                 itstr = g_strdup("");
1883         }
1884                 
1885         str = g_strconcat(n_selected ? itos(n_selected) : "",
1886                                         itstr, sel, spc, del, mv, cp, NULL);
1887         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1888         g_free(str);
1889         g_free(sel);
1890         g_free(del);
1891         g_free(mv);
1892         g_free(cp);
1893         g_free(itstr);
1894
1895         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1896
1897                                       summaryview->folder_item->new_msgs,
1898                                       summaryview->folder_item->unread_msgs,
1899                                       summaryview->folder_item->total_msgs,
1900                                       to_human_readable(summaryview->total_size));
1901         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1902         g_free(str);
1903 }
1904
1905 static void summary_set_column_titles(SummaryView *summaryview)
1906 {
1907         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1908         GtkWidget *hbox;
1909         GtkWidget *label;
1910         GtkWidget *arrow;
1911         gint pos;
1912         const gchar *title;
1913         SummaryColumnType type;
1914         gboolean single_char;
1915         GtkJustification justify;
1916
1917         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1918                 SORT_BY_MARK,
1919                 SORT_BY_STATUS,
1920                 SORT_BY_MIME,
1921                 SORT_BY_SUBJECT,
1922                 SORT_BY_FROM,
1923                 SORT_BY_DATE,
1924                 SORT_BY_SIZE,
1925                 SORT_BY_NUMBER,
1926                 SORT_BY_SCORE,
1927                 SORT_BY_LOCKED
1928         };
1929
1930         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1931                 type = summaryview->col_state[pos].type;
1932
1933                 /* CLAWS: mime and unread are single char headers */
1934                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1935                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1936                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1937
1938                 switch (type) {
1939                 case S_COL_SUBJECT:
1940                 case S_COL_FROM:
1941                 case S_COL_DATE:
1942                 case S_COL_NUMBER:
1943                         if (prefs_common.trans_hdr)
1944                                 title = gettext(col_label[type]);
1945                         else
1946                                 title = col_label[type];
1947                         break;
1948                 /* CLAWS: dummies for mark and locked headers */        
1949                 case S_COL_MARK:        
1950                 case S_COL_LOCKED:
1951                         title = "";
1952                         break;
1953                 default:
1954                         title = gettext(col_label[type]);
1955                 }
1956
1957                 if (type == S_COL_MIME) {
1958                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
1959                         gtk_widget_show(label);
1960                         gtk_clist_set_column_widget(clist, pos, label);
1961                         continue;
1962                 }
1963                 if (single_char) {
1964                         gtk_clist_set_column_title(clist, pos, title);
1965                         continue;
1966                 }
1967
1968                 /* CLAWS: changed so that locked and mark headers
1969                  * show a pixmap instead of single character */
1970                 hbox  = gtk_hbox_new(FALSE, 4);
1971                 
1972                 if (type == S_COL_LOCKED)
1973                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1974                 else if (type == S_COL_MARK) 
1975                         label = gtk_pixmap_new(markxpm, markxpmmask);
1976                 else 
1977                         label = gtk_label_new(title);
1978                 
1979                 if (justify == GTK_JUSTIFY_RIGHT)
1980                         gtk_box_pack_end(GTK_BOX(hbox), label,
1981                                          FALSE, FALSE, 0);
1982                 else
1983                         gtk_box_pack_start(GTK_BOX(hbox), label,
1984                                            FALSE, FALSE, 0);
1985
1986                 if (summaryview->sort_key == sort_by[type]) {
1987                         arrow = gtk_arrow_new
1988                                 (summaryview->sort_type == SORT_ASCENDING
1989                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1990                                  GTK_SHADOW_IN);
1991                         if (justify == GTK_JUSTIFY_RIGHT)
1992                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1993                                                    FALSE, FALSE, 0);
1994                         else
1995                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1996                                                  FALSE, FALSE, 0);
1997                 }
1998
1999                 gtk_widget_show_all(hbox);
2000                 gtk_clist_set_column_widget(clist, pos, hbox);
2001         }
2002 }
2003
2004 void summary_sort(SummaryView *summaryview,
2005                   FolderSortKey sort_key, FolderSortType sort_type)
2006 {
2007         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2008         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2009         GtkCListCompareFunc cmp_func = NULL;
2010
2011         switch (sort_key) {
2012         case SORT_BY_MARK:
2013                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2014                 break;
2015         case SORT_BY_STATUS:
2016                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2017                 break;
2018         case SORT_BY_MIME:
2019                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2020                 break;
2021         case SORT_BY_NUMBER:
2022                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2023                 break;
2024         case SORT_BY_SIZE:
2025                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2026                 break;
2027         case SORT_BY_DATE:
2028                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2029                 break;
2030         case SORT_BY_FROM:
2031                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2032                 break;
2033         case SORT_BY_SUBJECT:
2034                 if (summaryview->simplify_subject_preg)
2035                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2036                 else
2037                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2038                 break;
2039         case SORT_BY_SCORE:
2040                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2041                 break;
2042         case SORT_BY_LABEL:
2043                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2044                 break;
2045         case SORT_BY_TO:
2046                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2047                 break;
2048         case SORT_BY_LOCKED:
2049                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2050                 break;
2051         case SORT_BY_NONE:
2052                 break;
2053         default:
2054                 return;
2055         }
2056
2057         summaryview->sort_key = sort_key;
2058         summaryview->sort_type = sort_type;
2059
2060         summary_set_column_titles(summaryview);
2061         summary_set_menu_sensitive(summaryview);
2062
2063         /* allow fallback to don't sort */
2064         if (summaryview->sort_key == SORT_BY_NONE)
2065                 return;
2066
2067         if (cmp_func != NULL) {
2068                 debug_print("Sorting summary...");
2069                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2070
2071                 main_window_cursor_wait(summaryview->mainwin);
2072
2073                 gtk_clist_freeze(clist);
2074                 gtk_clist_set_compare_func(clist, cmp_func);
2075
2076                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2077
2078                 gtk_sctree_sort_recursive(ctree, NULL);
2079
2080                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2081
2082                 main_window_cursor_normal(summaryview->mainwin);
2083                 gtk_clist_thaw(clist);
2084
2085                 debug_print("done.\n");
2086                 STATUSBAR_POP(summaryview->mainwin);
2087         }
2088 }
2089
2090 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2091                                    GtkCTreeNode *cnode, gpointer data)
2092 {
2093         SummaryView *summaryview = (SummaryView *)data;
2094         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2095         gchar *text[N_SUMMARY_COLS];
2096         gint *col_pos = summaryview->col_pos;
2097         const gchar *msgid = msginfo->msgid;
2098         GHashTable *msgid_table = summaryview->msgid_table;
2099
2100         summary_set_header(summaryview, text, msginfo);
2101
2102         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2103                                 NULL, NULL, NULL, NULL, FALSE,
2104                                 gnode->parent->parent ? TRUE : FALSE);
2105 #define SET_TEXT(col) \
2106         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2107                                 text[col_pos[col]])
2108
2109         SET_TEXT(S_COL_NUMBER);
2110         SET_TEXT(S_COL_SCORE);
2111         SET_TEXT(S_COL_SIZE);
2112         SET_TEXT(S_COL_DATE);
2113         SET_TEXT(S_COL_FROM);
2114         SET_TEXT(S_COL_SUBJECT);
2115
2116 #undef SET_TEXT
2117
2118         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2119         summary_set_marks_func(ctree, cnode, summaryview);
2120
2121         if (msgid && msgid[0] != '\0')
2122                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2123
2124         return TRUE;
2125 }
2126
2127 static void summary_set_ctree_from_list(SummaryView *summaryview,
2128                                         GSList *mlist)
2129 {
2130         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2131         MsgInfo *msginfo;
2132                 GtkCTreeNode *node = NULL;
2133         GHashTable *msgid_table;
2134         GHashTable *subject_table;
2135         GSList * cur;
2136         
2137         if (!mlist) return;
2138
2139         debug_print("\tSetting summary from message data...");
2140         STATUSBAR_PUSH(summaryview->mainwin,
2141                        _("Setting summary from message data..."));
2142         gdk_flush();
2143
2144         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2145                                        G_CALLBACK(summary_tree_expanded), summaryview);
2146
2147         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2148         summaryview->msgid_table = msgid_table;
2149         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2150         summaryview->subject_table = subject_table;
2151
2152         if (prefs_common.use_addr_book)
2153                 start_address_completion();
2154         
2155         for (cur = mlist ; cur != NULL; cur = cur->next) {
2156                 msginfo = (MsgInfo *)cur->data;
2157                 msginfo->threadscore = msginfo->score;
2158         }
2159
2160         if (summaryview->threaded) {
2161                 GNode *root, *gnode;
2162
2163                 root = procmsg_get_thread_tree(mlist);
2164
2165                 for (gnode = root->children; gnode != NULL;
2166                      gnode = gnode->next) {
2167                         node = gtk_ctree_insert_gnode
2168                                 (ctree, NULL, node, gnode,
2169                                  summary_insert_gnode_func, summaryview);
2170                 }
2171
2172                 g_node_destroy(root);
2173                 
2174                 summary_thread_init(summaryview);
2175         } else {
2176                 gchar *text[N_SUMMARY_COLS];
2177                 cur = mlist;
2178                 for (; mlist != NULL; mlist = mlist->next) {
2179                         msginfo = (MsgInfo *)mlist->data;
2180
2181                         summary_set_header(summaryview, text, msginfo);
2182
2183                         node = gtk_ctree_insert_node
2184                                 (ctree, NULL, node, text, 2,
2185                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2186                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2187                         summary_set_marks_func(ctree, node, summaryview);
2188
2189                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2190                                 g_hash_table_insert(msgid_table,
2191                                                     msginfo->msgid, node);
2192
2193                         subject_table_insert(subject_table,
2194                                              msginfo->subject,
2195                                              node);
2196                 }
2197                 mlist = cur;
2198         }
2199
2200         if (prefs_common.enable_hscrollbar &&
2201             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2202                 gint optimal_width;
2203
2204                 optimal_width = gtk_clist_optimal_column_width
2205                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2206                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2207                                            summaryview->col_pos[S_COL_SUBJECT],
2208                                            optimal_width);
2209         }
2210
2211         if (prefs_common.use_addr_book)
2212                 end_address_completion();
2213
2214         debug_print("done.\n");
2215         STATUSBAR_POP(summaryview->mainwin);
2216         if (debug_get_mode()) {
2217                 debug_print("\tmsgid hash table size = %d\n",
2218                             g_hash_table_size(msgid_table));
2219                 debug_print("\tsubject hash table size = %d\n",
2220                             g_hash_table_size(subject_table));
2221         }
2222
2223         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2224
2225         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2226
2227         while (prefs_common.bold_unread && node) {
2228                 GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2229                 if (GTK_CTREE_ROW(node)->children)
2230                         summary_set_row_marks(summaryview, node);
2231                 node = next;
2232         }
2233
2234         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2235                                        G_CALLBACK(summary_tree_expanded), summaryview);
2236
2237 }
2238
2239 static gchar *summary_complete_address(const gchar *addr)
2240 {
2241         gint count;
2242         gchar *res, *tmp, *email_addr;
2243
2244         Xstrdup_a(email_addr, addr, return NULL);
2245         extract_address(email_addr);
2246         g_return_val_if_fail(*email_addr, NULL);
2247
2248         /*
2249          * completion stuff must be already initialized
2250          */
2251         res = NULL;
2252         if (1 < (count = complete_address(email_addr))) {
2253                 tmp = get_complete_address(1);
2254 /*      tmp = addressbook_lookup_name( email_addr );
2255         if( tmp ) { */
2256                 res = procheader_get_fromname(tmp);
2257                 g_free(tmp);
2258         }
2259
2260         return res;
2261 }
2262
2263 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2264                                MsgInfo *msginfo)
2265 {
2266         static gchar date_modified[80];
2267         static gchar *to = NULL;
2268         static gchar col_score[11];
2269         static gchar buf[BUFFSIZE];
2270         gint *col_pos = summaryview->col_pos;
2271         FolderType ftype = F_UNKNOWN;
2272
2273         text[col_pos[S_COL_MARK]]   = NULL;
2274         text[col_pos[S_COL_STATUS]] = NULL;
2275         text[col_pos[S_COL_MIME]]   = NULL;
2276         text[col_pos[S_COL_LOCKED]] = NULL;
2277         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2278         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2279         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2280
2281         if (msginfo->date_t) {
2282                 procheader_date_get_localtime(date_modified,
2283                                               sizeof(date_modified),
2284                                               msginfo->date_t);
2285                 text[col_pos[S_COL_DATE]] = date_modified;
2286         } else if (msginfo->date)
2287                 text[col_pos[S_COL_DATE]] = msginfo->date;
2288         else
2289                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2290
2291         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2292                 _("(No From)");
2293         
2294         if (msginfo->folder && msginfo->folder->folder)
2295                 ftype = msginfo->folder->folder->klass->type; 
2296                 
2297         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2298                 gchar *addr = NULL;
2299
2300                 Xstrdup_a(addr, msginfo->from, return);
2301                 extract_address(addr);
2302                 if (prefs_common.use_addr_book) {
2303                         if (account_find_from_address(addr)) {
2304                                 addr = summary_complete_address(msginfo->to);
2305                                 g_free(to);
2306                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2307                                 text[col_pos[S_COL_FROM]] = to;
2308                                 g_free(addr);
2309                         }
2310                 } else {
2311                         if (account_find_from_address(addr)) {
2312                                 g_free(to);
2313                                 to = g_strconcat("-->", msginfo->to, NULL);
2314                                 text[col_pos[S_COL_FROM]] = to;
2315                         }
2316                 }
2317         }
2318
2319         /*
2320          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2321          * checking whether the above block (which handles the special case of
2322          * the --> in sent boxes) was executed.
2323          */
2324         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2325                 gchar *from = summary_complete_address(msginfo->from);
2326                 if (from) {
2327                         g_free(to);
2328                         to = from;
2329                         text[col_pos[S_COL_FROM]] = to;
2330                 }                       
2331         }
2332
2333         if (summaryview->simplify_subject_preg != NULL)
2334                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2335                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2336                                         summaryview->simplify_subject_preg) : 
2337                         _("(No Subject)");
2338         else 
2339                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2340                         _("(No Subject)");
2341 }
2342
2343 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2344 {
2345         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2346 }
2347
2348 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2349                                       GtkCTreeNode *row)
2350 {
2351         g_return_if_fail(summaryview != NULL);
2352         g_return_if_fail(msginfo != NULL);
2353         g_return_if_fail(row != NULL);
2354
2355         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2356         && procmsg_msg_has_marked_parent(msginfo))
2357                 summaryview->unreadmarked--;
2358         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2359                 procmsg_msginfo_unset_flags
2360                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2361                 summary_set_row_marks(summaryview, row);
2362                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2363                 summary_status_show(summaryview);
2364         }
2365 }
2366
2367 typedef struct  {
2368         MsgInfo *msginfo;
2369         SummaryView *summaryview;
2370         GtkCTreeNode *row;
2371 } MarkAsReadData;
2372
2373 static int msginfo_mark_as_read_timeout(void *data)
2374 {
2375         MarkAsReadData *mdata = (MarkAsReadData *)data;
2376         if (!mdata)
2377                 return FALSE;
2378         
2379         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2380                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2381                                      mdata->row); 
2382
2383         g_free(mdata);
2384
2385         return FALSE;   
2386 }
2387
2388 static void summary_display_msg_full(SummaryView *summaryview,
2389                                      GtkCTreeNode *row,
2390                                      gboolean new_window, gboolean all_headers)
2391 {
2392         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2393         MsgInfo *msginfo;
2394         gint val;
2395
2396         if (!new_window) {
2397                 if (summaryview->displayed == row)
2398                         return;
2399                 else
2400                         summaryview->messageview->filtered = FALSE;
2401         }                       
2402         
2403         g_return_if_fail(row != NULL);
2404
2405         if (summary_is_locked(summaryview)) return;
2406         summary_lock(summaryview);
2407
2408         STATUSBAR_POP(summaryview->mainwin);
2409         GTK_EVENTS_FLUSH();
2410
2411         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2412
2413         if (new_window) {
2414                 MessageView *msgview;
2415
2416                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2417                 val = messageview_show(msgview, msginfo, all_headers);
2418         } else {
2419                 MessageView *msgview;
2420
2421                 msgview = summaryview->messageview;
2422
2423                 summaryview->displayed = row;
2424                 if (!messageview_is_visible(msgview)) {
2425                         main_window_toggle_message_view(summaryview->mainwin);
2426                         GTK_EVENTS_FLUSH();
2427                 }
2428                 val = messageview_show(msgview, msginfo, all_headers);
2429                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2430                         gtk_widget_grab_focus(summaryview->ctree);
2431                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2432         }
2433
2434         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2435                 if (prefs_common.mark_as_read_delay) {
2436                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2437                         data->summaryview = summaryview;
2438                         data->msginfo = msginfo;
2439                         data->row = row;
2440                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2441                                 msginfo_mark_as_read_timeout, data);
2442                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2443                         msginfo_mark_as_read(summaryview, msginfo, row);
2444                 }
2445         }
2446
2447         summary_set_menu_sensitive(summaryview);
2448         toolbar_main_set_sensitive(summaryview->mainwin);
2449         messageview_set_menu_sensitive(summaryview->messageview);
2450
2451         summary_unlock(summaryview);
2452 }
2453
2454 void summary_display_msg_selected(SummaryView *summaryview,
2455                                   gboolean all_headers)
2456 {
2457         if (summary_is_locked(summaryview)) return;
2458         summaryview->displayed = NULL;
2459         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2460                                  all_headers);
2461 }
2462
2463 void summary_redisplay_msg(SummaryView *summaryview)
2464 {
2465         GtkCTreeNode *node;
2466
2467         if (summaryview->displayed) {
2468                 node = summaryview->displayed;
2469                 summaryview->displayed = NULL;
2470                 summary_display_msg(summaryview, node);
2471         }
2472 }
2473
2474 void summary_open_msg(SummaryView *summaryview)
2475 {
2476         if (!summaryview->selected) return;
2477         
2478         /* CLAWS: if separate message view, don't open a new window
2479          * but rather use the current separated message view */
2480         summary_display_msg_full(summaryview, summaryview->selected,
2481                                  prefs_common.sep_msg ? FALSE : TRUE, 
2482                                  FALSE);
2483 }
2484
2485 void summary_view_source(SummaryView * summaryview)
2486 {
2487         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2488         MsgInfo *msginfo;
2489         SourceWindow *srcwin;
2490
2491         if (!summaryview->selected) return;
2492
2493         srcwin = source_window_create();
2494         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2495         source_window_show_msg(srcwin, msginfo);
2496         source_window_show(srcwin);
2497 }
2498
2499 void summary_reedit(SummaryView *summaryview)
2500 {
2501         MsgInfo *msginfo;
2502
2503         if (!summaryview->selected) return;
2504         if (!summaryview->folder_item) return;
2505         if (summaryview->folder_item->stype != F_OUTBOX &&
2506             summaryview->folder_item->stype != F_DRAFT  &&
2507             summaryview->folder_item->stype != F_QUEUE) return;
2508
2509         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2510                                               summaryview->selected);
2511         if (!msginfo) return;
2512
2513         compose_reedit(msginfo);
2514 }
2515
2516 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2517 {
2518         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2519         GtkCTreeNode *node;
2520
2521         if (summary_is_locked(summaryview)) return FALSE;
2522
2523         if (type == GTK_SCROLL_STEP_FORWARD) {
2524                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2525                 if (node)
2526                         gtkut_ctree_expand_parent_all(ctree, node);
2527                 else
2528                         return FALSE;
2529         } else {
2530                 if (summaryview->selected) {
2531                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2532                         if (!node) return FALSE;
2533                 }
2534         }
2535
2536         if (messageview_is_visible(summaryview->messageview))
2537                 summaryview->display_msg = TRUE;
2538
2539         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2540
2541         if (GTK_CLIST(ctree)->selection)
2542                 gtk_sctree_set_anchor_row
2543                         (GTK_SCTREE(ctree),
2544                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2545
2546         return TRUE;
2547 }
2548
2549 void summary_toggle_view(SummaryView *summaryview)
2550 {
2551         if (!messageview_is_visible(summaryview->messageview) &&
2552             summaryview->selected)
2553                 summary_display_msg(summaryview,
2554                                     summaryview->selected);
2555         else
2556                 main_window_toggle_message_view(summaryview->mainwin);
2557 }
2558
2559 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2560                                                 GtkCTreeNode *node)
2561 {
2562         MsgInfo *msginfo;
2563
2564         if (node) {
2565                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2566                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2567                         return TRUE;
2568                 node = GTK_CTREE_ROW(node)->children;
2569         } else
2570                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2571
2572         while (node) {
2573                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2574                         return TRUE;
2575                 node = GTK_CTREE_ROW(node)->sibling;
2576         }
2577
2578         return FALSE;
2579 }
2580
2581 static gboolean summary_have_unread_children(SummaryView *summaryview,
2582                                              GtkCTreeNode *node)
2583 {
2584         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2585
2586         if (!node) return FALSE;
2587
2588         node = GTK_CTREE_ROW(node)->children;
2589
2590         while (node) {
2591                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2592                         return TRUE;
2593                 node = GTK_CTREE_ROW(node)->sibling;
2594         }
2595         return FALSE;
2596 }
2597
2598 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2599 {
2600         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2601         GtkStyle *style = NULL;
2602         MsgInfo *msginfo;
2603         MsgFlags flags;
2604         gint *col_pos = summaryview->col_pos;
2605
2606         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2607         if (!msginfo) return;
2608
2609         flags = msginfo->flags;
2610
2611         gtk_ctree_node_set_foreground(ctree, row, NULL);
2612
2613         /* set new/unread column */
2614         if (MSG_IS_IGNORE_THREAD(flags)) {
2615                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2616                                           ignorethreadxpm, ignorethreadxpmmask);
2617         } else if (MSG_IS_NEW(flags)) {
2618                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2619                                           newxpm, newxpmmask);
2620         } else if (MSG_IS_UNREAD(flags)) {
2621                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2622                                           unreadxpm, unreadxpmmask);
2623         } else if (MSG_IS_REPLIED(flags)) {
2624                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2625                                           repliedxpm, repliedxpmmask);
2626         } else if (MSG_IS_FORWARDED(flags)) {
2627                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2628                                           forwardedxpm, forwardedxpmmask);
2629         } else {
2630                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2631                                         NULL);
2632         }
2633
2634         if (prefs_common.bold_unread &&
2635             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2636              (!GTK_CTREE_ROW(row)->expanded &&
2637               GTK_CTREE_ROW(row)->children &&
2638               summary_have_unread_children(summaryview, row))))
2639                 style = bold_style;
2640
2641         /* set mark column */
2642         if (MSG_IS_DELETED(flags)) {
2643                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2644                                           deletedxpm, deletedxpmmask);
2645                 if (style)
2646                         style = bold_deleted_style;
2647                 else {
2648                         style = small_deleted_style;
2649                 }
2650                         gtk_ctree_node_set_foreground
2651                                 (ctree, row, &summaryview->color_dim);
2652         } else if (MSG_IS_MARKED(flags)) {
2653                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2654                                           markxpm, markxpmmask);
2655         } else if (MSG_IS_MOVE(flags)) {
2656                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2657                 if (style)
2658                         style = bold_marked_style;
2659                 else {
2660                         style = small_marked_style;
2661                 }
2662                         gtk_ctree_node_set_foreground
2663                                 (ctree, row, &summaryview->color_marked);
2664         } else if (MSG_IS_COPY(flags)) {
2665                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2666                 if (style)
2667                         style = bold_marked_style;
2668                 else {
2669                         style = small_marked_style;
2670                 }
2671                         gtk_ctree_node_set_foreground
2672                                 (ctree, row, &summaryview->color_marked);
2673 #if 0
2674         } else if ((global_scoring ||
2675                   summaryview->folder_item->prefs->scoring) &&
2676                  (msginfo->score >= summaryview->important_score) &&
2677                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2678                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2679                 gtk_ctree_node_set_foreground(ctree, row,
2680                                               &summaryview->color_important);
2681 #endif
2682         } else {
2683                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2684         }
2685
2686         if (MSG_IS_LOCKED(flags)) {
2687                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2688                                           lockedxpm, lockedxpmmask);
2689         }
2690         else {
2691                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2692         }
2693
2694         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2695                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2696                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2697         } else if (MSG_IS_SIGNED(flags)) {
2698                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2699                                           gpgsignedxpm, gpgsignedxpmmask);
2700         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2701                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2702                                           clipkeyxpm, clipkeyxpmmask);
2703         } else if (MSG_IS_ENCRYPTED(flags)) {
2704                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2705                                           keyxpm, keyxpmmask);
2706         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2707                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2708                                           clipxpm, clipxpmmask);
2709         } else {
2710                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2711         }
2712         if (!style)
2713                 style = small_style;
2714
2715         gtk_ctree_node_set_row_style(ctree, row, style);
2716
2717         if (MSG_GET_COLORLABEL(flags))
2718                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2719 }
2720
2721 void summary_set_marks_selected(SummaryView *summaryview)
2722 {
2723         GList *cur;
2724
2725         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2726              cur = cur->next)
2727                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2728 }
2729
2730 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2731 {
2732         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2733         MsgInfo *msginfo;
2734
2735         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2736         if (MSG_IS_DELETED(msginfo->flags))
2737                 summaryview->deleted--;
2738         if (MSG_IS_MOVE(msginfo->flags))
2739                 summaryview->moved--;
2740         if (MSG_IS_COPY(msginfo->flags))
2741                 summaryview->copied--;
2742         procmsg_update_unread_children (msginfo, TRUE);
2743
2744         procmsg_msginfo_set_to_folder(msginfo, NULL);
2745         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2746         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2747         summary_set_row_marks(summaryview, row);
2748         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2749 }
2750
2751 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2752 {
2753         gboolean changed = FALSE;
2754         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2755         MsgInfo *msginfo;
2756
2757         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2758         if (MSG_IS_DELETED(msginfo->flags))
2759                 summaryview->deleted--;
2760         if (MSG_IS_MOVE(msginfo->flags)) {
2761                 summaryview->moved--;
2762                 changed = TRUE;
2763         }
2764         if (MSG_IS_COPY(msginfo->flags)) {
2765                 summaryview->copied--;
2766                 changed = TRUE;
2767         }
2768         procmsg_msginfo_set_to_folder(msginfo, NULL);
2769         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2770         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2771         summary_set_row_marks(summaryview, row);
2772         debug_print("Message %d is locked\n", msginfo->msgnum);
2773 }
2774
2775 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2776 {
2777         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2778         MsgInfo *msginfo;
2779
2780         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2781         if (!MSG_IS_LOCKED(msginfo->flags))
2782                 return;
2783         procmsg_msginfo_set_to_folder(msginfo, NULL);
2784         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2785         summary_set_row_marks(summaryview, row);
2786         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2787 }
2788
2789 void summary_mark(SummaryView *summaryview)
2790 {
2791         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2792         GList *cur;
2793
2794         folder_item_update_freeze();
2795         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2796         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2797                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2798         folder_item_update_thaw();
2799         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2800
2801         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2802         summary_status_show(summaryview);
2803 }
2804
2805 static void summary_mark_row_as_read(SummaryView *summaryview,
2806                                      GtkCTreeNode *row)
2807 {
2808         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2809         MsgInfo *msginfo;
2810
2811         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2812
2813         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2814                 return;
2815
2816         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2817         && procmsg_msg_has_marked_parent(msginfo))
2818                 summaryview->unreadmarked--;
2819
2820         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2821         summary_set_row_marks(summaryview, row);
2822         debug_print("Message %d is marked as read\n",
2823                 msginfo->msgnum);
2824 }
2825
2826 void summary_mark_as_read(SummaryView *summaryview)
2827 {
2828         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2829         GList *cur;
2830
2831         folder_item_update_freeze();
2832         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2833         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2834                 summary_mark_row_as_read(summaryview,
2835                                          GTK_CTREE_NODE(cur->data));
2836         folder_item_update_thaw();
2837         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2838
2839         summary_status_show(summaryview);
2840 }
2841
2842 void summary_msgs_lock(SummaryView *summaryview)
2843 {
2844         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2845         GList *cur;
2846
2847         folder_item_update_freeze();
2848         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2849         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2850                 summary_lock_row(summaryview,
2851                                          GTK_CTREE_NODE(cur->data));
2852         folder_item_update_thaw();
2853         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2854         
2855         summary_status_show(summaryview);
2856 }
2857
2858 void summary_msgs_unlock(SummaryView *summaryview)
2859 {
2860         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2861         GList *cur;
2862
2863         folder_item_update_freeze();
2864         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2865         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2866                 summary_unlock_row(summaryview,
2867                                    GTK_CTREE_NODE(cur->data));
2868         folder_item_update_thaw();
2869         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2870         
2871         summary_status_show(summaryview);
2872 }
2873
2874 void summary_mark_all_read(SummaryView *summaryview)
2875 {
2876         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2877         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2878         GtkCTreeNode *node;
2879
2880         gtk_clist_freeze(clist);
2881         folder_item_update_freeze();
2882         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2883              node = gtkut_ctree_node_next(ctree, node))
2884                 summary_mark_row_as_read(summaryview, node);
2885         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2886              node = gtkut_ctree_node_next(ctree, node)) {
2887                 if (!GTK_CTREE_ROW(node)->expanded)
2888                         summary_set_row_marks(summaryview, node);
2889         }
2890         folder_item_update_thaw();
2891         gtk_clist_thaw(clist);
2892
2893         summary_status_show(summaryview);
2894 }
2895
2896 static void summary_mark_row_as_unread(SummaryView *summaryview,
2897                                        GtkCTreeNode *row)
2898 {
2899         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2900         MsgInfo *msginfo;
2901
2902         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2903         if (MSG_IS_DELETED(msginfo->flags)) {
2904                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2905                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2906                 summaryview->deleted--;
2907         }
2908
2909         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2910         && procmsg_msg_has_marked_parent(msginfo))
2911                 summaryview->unreadmarked++;
2912
2913         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2914         debug_print("Message %d is marked as unread\n",
2915                 msginfo->msgnum);
2916
2917         summary_set_row_marks(summaryview, row);
2918 }
2919
2920 void summary_mark_as_unread(SummaryView *summaryview)
2921 {
2922         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2923         GList *cur;
2924
2925         folder_item_update_freeze();
2926         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2927         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2928                 summary_mark_row_as_unread(summaryview,
2929                                            GTK_CTREE_NODE(cur->data));
2930         folder_item_update_thaw();
2931         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2932         
2933         summary_status_show(summaryview);
2934 }
2935
2936 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2937 {
2938         GList * cur;
2939         gboolean found;
2940
2941         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2942
2943         case F_NEWS:
2944
2945                 /*
2946                   security : checks if one the accounts correspond to
2947                   the author of the post
2948                 */
2949
2950                 found = FALSE;
2951                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2952                         PrefsAccount * account;
2953                         gchar * from_name;
2954                         
2955                         account = cur->data;
2956                         if (account->name && *account->name)
2957                                 from_name =
2958                                         g_strdup_printf("%s <%s>",
2959                                                         account->name,
2960                                                         account->address);
2961                         else
2962                                 from_name =
2963                                         g_strdup_printf("%s",
2964                                                         account->address);
2965                         
2966                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
2967                                 g_free(from_name);
2968                                 found = TRUE;
2969                                 break;
2970                         }
2971                         g_free(from_name);
2972                 }
2973
2974                 if (!found) {
2975                         alertpanel_error(_("You're not the author of the article\n"));
2976                 }
2977                 
2978                 return found;
2979
2980         default:
2981                 return TRUE;
2982         }
2983 }
2984
2985 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2986 {
2987         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2988         MsgInfo *msginfo;
2989
2990         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2991
2992         if (!check_permission(summaryview, msginfo))
2993                 return;
2994
2995         if (MSG_IS_LOCKED(msginfo->flags)) return;
2996
2997         if (MSG_IS_DELETED(msginfo->flags)) return;
2998
2999         if (MSG_IS_MOVE(msginfo->flags))
3000                 summaryview->moved--;
3001         if (MSG_IS_COPY(msginfo->flags))
3002                 summaryview->copied--;
3003         procmsg_update_unread_children (msginfo, FALSE);
3004
3005         procmsg_msginfo_set_to_folder(msginfo, NULL);
3006         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
3007         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
3008         summaryview->deleted++;
3009
3010         if (!prefs_common.immediate_exec && 
3011             summaryview->folder_item->stype != F_TRASH)
3012                 summary_set_row_marks(summaryview, row);
3013
3014         debug_print("Message %s/%d is set to delete\n",
3015                     msginfo->folder->path, msginfo->msgnum);
3016 }
3017
3018 void summary_cancel(SummaryView *summaryview)
3019 {
3020         MsgInfo * msginfo;
3021         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3022
3023         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3024                                               summaryview->selected);
3025         if (!msginfo) return;
3026
3027         if (!check_permission(summaryview, msginfo))
3028                 return;
3029
3030         news_cancel_article(summaryview->folder_item->folder, msginfo);
3031         
3032         if (summary_is_locked(summaryview)) return;
3033
3034         summary_lock(summaryview);
3035
3036         gtk_clist_freeze(clist);
3037
3038         summary_update_status(summaryview);
3039         summary_status_show(summaryview);
3040
3041         gtk_clist_thaw(clist);
3042
3043         summary_unlock(summaryview);
3044 }
3045
3046 void summary_delete(SummaryView *summaryview)
3047 {
3048         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3049         FolderItem *item = summaryview->folder_item;
3050         GList *cur;
3051         GtkCTreeNode *sel_last = NULL;
3052         GtkCTreeNode *node;
3053
3054         if (!item) return;
3055
3056         if (summary_is_locked(summaryview)) return;
3057
3058         /* if current folder is trash, ask for confirmation */
3059         if (item->stype == F_TRASH) {
3060                 AlertValue aval;
3061                 MsgInfo *msginfo;
3062
3063                 aval = alertpanel(_("Delete message(s)"),
3064                                   _("Do you really want to delete message(s) from the trash?"),
3065                                   GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3066                 if (aval != G_ALERTDEFAULT) return;
3067
3068                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3069                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3070                         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3071                         partial_mark_for_delete(msginfo);
3072                 }
3073         
3074         }
3075
3076         main_window_cursor_wait(summaryview->mainwin);
3077
3078         /* next code sets current row focus right. We need to find a row
3079          * that is not deleted. */
3080         folder_item_update_freeze();
3081         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3082         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3083                 sel_last = GTK_CTREE_NODE(cur->data);
3084                 summary_delete_row(summaryview, sel_last);
3085         }
3086         folder_item_update_thaw();
3087         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3088
3089         node = summary_find_next_msg(summaryview, sel_last);
3090         if (!node)
3091                 node = summary_find_prev_msg(summaryview, sel_last);
3092
3093         summary_select_node(summaryview, node, FALSE, FALSE);
3094         
3095         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3096                 summary_execute(summaryview);
3097                 /* after deleting, the anchor may be at an invalid row
3098                  * so reset it to the node we found earlier */
3099                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3100         } else
3101                 summary_status_show(summaryview);
3102                 
3103         main_window_cursor_normal(summaryview->mainwin);
3104 }
3105
3106 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3107 {
3108         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3109         MsgInfo *msginfo;
3110
3111         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3112         if (MSG_IS_DELETED(msginfo->flags))
3113                 summaryview->deleted--;
3114         if (MSG_IS_MOVE(msginfo->flags))
3115                 summaryview->moved--;
3116         if (MSG_IS_COPY(msginfo->flags))
3117                 summaryview->copied--;
3118         procmsg_update_unread_children (msginfo, FALSE);
3119
3120         procmsg_msginfo_set_to_folder(msginfo, 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         folder_item_update_freeze();
3134         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3135
3136         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3137                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3138
3139         folder_item_update_thaw();
3140         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3141
3142         summary_status_show(summaryview);
3143 }
3144
3145 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3146                                 FolderItem *to_folder)
3147 {
3148         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3149         MsgInfo *msginfo;
3150
3151         g_return_if_fail(to_folder != NULL);
3152
3153         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3154         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3155         if (MSG_IS_DELETED(msginfo->flags))
3156                 summaryview->deleted--;
3157         if (MSG_IS_COPY(msginfo->flags)) {
3158                 summaryview->copied--;
3159         }
3160         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3161         if (!MSG_IS_MOVE(msginfo->flags)) {
3162                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3163                 summaryview->moved++;
3164         }
3165         if (!prefs_common.immediate_exec) {
3166                 summary_set_row_marks(summaryview, row);
3167         }
3168
3169         debug_print("Message %d is set to move to %s\n",
3170                     msginfo->msgnum, to_folder->path);
3171 }
3172
3173 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3174 {
3175         GList *cur;
3176
3177         if (!to_folder) return;
3178         if (!summaryview->folder_item ||
3179             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3180
3181         if (summary_is_locked(summaryview)) return;
3182
3183         if (summaryview->folder_item == to_folder) {
3184                 alertpanel_error(_("Destination is same as current folder."));
3185                 return;
3186         }
3187
3188         folder_item_update_freeze();
3189         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3190
3191         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3192              cur != NULL; cur = cur->next)
3193                 summary_move_row_to
3194                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3195
3196         folder_item_update_thaw();
3197         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3198
3199         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3200
3201         if (prefs_common.immediate_exec)
3202                 summary_execute(summaryview);
3203         else {
3204                 summary_status_show(summaryview);
3205         }
3206         
3207         if (!summaryview->selected) { /* this was the last message */
3208                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3209                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3210                 if (node)
3211                         summary_select_node(summaryview, node, TRUE, TRUE);
3212         }
3213
3214 }
3215
3216 void summary_move_to(SummaryView *summaryview)
3217 {
3218         FolderItem *to_folder;
3219
3220         if (!summaryview->folder_item ||
3221             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3222
3223         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3224                                          FOLDER_SEL_MOVE, NULL);
3225         summary_move_selected_to(summaryview, to_folder);
3226 }
3227
3228 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3229                                 FolderItem *to_folder)
3230 {
3231         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3232         MsgInfo *msginfo;
3233
3234         g_return_if_fail(to_folder != NULL);
3235
3236         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3237         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3238         if (MSG_IS_DELETED(msginfo->flags))
3239                 summaryview->deleted--;
3240         if (MSG_IS_MOVE(msginfo->flags)) {
3241                 summaryview->moved--;
3242         }
3243         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3244         if (!MSG_IS_COPY(msginfo->flags)) {
3245                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3246                 summaryview->copied++;
3247         }
3248         if (!prefs_common.immediate_exec) {
3249                 summary_set_row_marks(summaryview, row);
3250         }
3251
3252         debug_print("Message %d is set to copy to %s\n",
3253                     msginfo->msgnum, to_folder->path);
3254 }
3255
3256 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3257 {
3258         GList *cur;
3259
3260         if (!to_folder) return;
3261         if (!summaryview->folder_item) return;
3262
3263         if (summary_is_locked(summaryview)) return;
3264
3265         if (summaryview->folder_item == to_folder) {
3266                 alertpanel_error
3267                         (_("Destination to copy is same as current folder."));
3268                 return;
3269         }
3270
3271         folder_item_update_freeze();
3272         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3273
3274         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3275              cur != NULL; cur = cur->next)
3276                 summary_copy_row_to
3277                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3278
3279         folder_item_update_thaw();
3280         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3281
3282         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3283
3284         if (prefs_common.immediate_exec)
3285                 summary_execute(summaryview);
3286         else {
3287                 summary_status_show(summaryview);
3288         }
3289 }
3290
3291 void summary_copy_to(SummaryView *summaryview)
3292 {
3293         FolderItem *to_folder;
3294
3295         if (!summaryview->folder_item) return;
3296
3297         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3298                                          FOLDER_SEL_COPY, NULL);
3299         summary_copy_selected_to(summaryview, to_folder);
3300 }
3301
3302 void summary_add_address(SummaryView *summaryview)
3303 {
3304         MsgInfo *msginfo;
3305         gchar *from;
3306
3307         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3308                                               summaryview->selected);
3309         if (!msginfo) return;
3310
3311         Xstrdup_a(from, msginfo->from, return);
3312         eliminate_address_comment(from);
3313         extract_address(from);
3314         addressbook_add_contact(msginfo->fromname, from, NULL);