2005-01-21 [colin] 1.0.0cvs4
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkscrolledwindow.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkpixmap.h>
27 #include <gtk/gtkctree.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtktext.h>
31 #include <gtk/gtkmenu.h>
32 #include <gtk/gtkmenuitem.h>
33 #include <gtk/gtkitemfactory.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtkwindow.h>
37 #include <gtk/gtkstyle.h>
38 #include <gtk/gtkarrow.h>
39 #include <gtk/gtkeventbox.h>
40 #include <gtk/gtkstatusbar.h>
41 #include <gtk/gtkmenuitem.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47
48 #include "intl.h"
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
128 static void summary_free_msginfo_func   (GtkCTree               *ctree,
129                                          GtkCTreeNode           *node,
130                                          gpointer                data);
131 static void summary_set_marks_func      (GtkCTree               *ctree,
132                                          GtkCTreeNode           *node,
133                                          gpointer                data);
134
135 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
136
137 static void summary_set_hide_read_msgs_menu (SummaryView *summaryview,
138                                              guint action);
139
140 static guint summary_get_msgnum         (SummaryView            *summaryview,
141                                          GtkCTreeNode           *node);
142
143 static GtkCTreeNode *summary_find_prev_msg
144                                         (SummaryView            *summaryview,
145                                          GtkCTreeNode           *current_node);
146 static GtkCTreeNode *summary_find_next_msg
147                                         (SummaryView            *summaryview,
148                                          GtkCTreeNode           *current_node);
149
150 static GtkCTreeNode *summary_find_prev_flagged_msg
151                                         (SummaryView    *summaryview,
152                                          GtkCTreeNode   *current_node,
153                                          MsgPermFlags    flags,
154                                          gboolean        start_from_prev);
155 static GtkCTreeNode *summary_find_next_flagged_msg
156                                         (SummaryView    *summaryview,
157                                          GtkCTreeNode   *current_node,
158                                          MsgPermFlags    flags,
159                                          gboolean        start_from_next);
160
161 static GtkCTreeNode *summary_find_msg_by_msgnum
162                                         (SummaryView            *summaryview,
163                                          guint                   msgnum);
164
165 static void summary_update_status       (SummaryView            *summaryview);
166
167 /* display functions */
168 static void summary_status_show         (SummaryView            *summaryview);
169 static void summary_set_column_titles   (SummaryView            *summaryview);
170 static void summary_set_ctree_from_list (SummaryView            *summaryview,
171                                          GSList                 *mlist);
172 static void summary_set_header          (SummaryView            *summaryview,
173                                          gchar                  *text[],
174                                          MsgInfo                *msginfo);
175 static void summary_display_msg         (SummaryView            *summaryview,
176                                          GtkCTreeNode           *row);
177 static void summary_display_msg_full    (SummaryView            *summaryview,
178                                          GtkCTreeNode           *row,
179                                          gboolean                new_window,
180                                          gboolean                all_headers);
181 static void summary_set_row_marks       (SummaryView            *summaryview,
182                                          GtkCTreeNode           *row);
183
184 /* message handling */
185 static void summary_mark_row            (SummaryView            *summaryview,
186                                          GtkCTreeNode           *row);
187 static void summary_lock_row            (SummaryView            *summaryview,
188                                          GtkCTreeNode           *row);
189 static void summary_unlock_row          (SummaryView            *summaryview,
190                                          GtkCTreeNode           *row);
191 static void summary_mark_row_as_read    (SummaryView            *summaryview,
192                                          GtkCTreeNode           *row);
193 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
194                                          GtkCTreeNode           *row);
195 static void summary_delete_row          (SummaryView            *summaryview,
196                                          GtkCTreeNode           *row);
197 static void summary_unmark_row          (SummaryView            *summaryview,
198                                          GtkCTreeNode           *row);
199 static void summary_move_row_to         (SummaryView            *summaryview,
200                                          GtkCTreeNode           *row,
201                                          FolderItem             *to_folder);
202 static void summary_copy_row_to         (SummaryView            *summaryview,
203                                          GtkCTreeNode           *row,
204                                          FolderItem             *to_folder);
205
206 static void summary_execute_move        (SummaryView            *summaryview);
207 static void summary_execute_move_func   (GtkCTree               *ctree,
208                                          GtkCTreeNode           *node,
209                                          gpointer                data);
210 static void summary_execute_copy        (SummaryView            *summaryview);
211 static void summary_execute_copy_func   (GtkCTree               *ctree,
212                                          GtkCTreeNode           *node,
213                                          gpointer                data);
214 static void summary_execute_delete      (SummaryView            *summaryview);
215 static void summary_execute_delete_func (GtkCTree               *ctree,
216                                          GtkCTreeNode           *node,
217                                          gpointer                data);
218
219 static void summary_thread_init         (SummaryView            *summaryview);
220 static void summary_ignore_thread       (SummaryView            *summaryview);
221 static void summary_unignore_thread     (SummaryView            *summaryview);
222
223 static void summary_unthread_for_exec           (SummaryView    *summaryview);
224 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
225                                                  GtkCTreeNode   *node,
226                                                  gpointer        data);
227
228 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
229                               GSList * mlist);
230
231 #if 0
232 void summary_processing(SummaryView *summaryview, GSList * mlist);
233 #endif
234 static void summary_filter_func         (GtkCTree               *ctree,
235                                          GtkCTreeNode           *node,
236                                          gpointer                data);
237
238 static void summary_colorlabel_menu_item_activate_cb
239                                           (GtkWidget    *widget,
240                                            gpointer      data);
241 static void summary_colorlabel_menu_item_activate_item_cb
242                                           (GtkMenuItem  *label_menu_item,
243                                            gpointer      data);
244 static void summary_colorlabel_menu_create(SummaryView  *summaryview);
245
246 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
247
248 /* callback functions */
249 static gint summary_toggle_pressed      (GtkWidget              *eventbox,
250                                          GdkEventButton         *event,
251                                          SummaryView            *summaryview);
252 static void summary_button_pressed      (GtkWidget              *ctree,
253                                          GdkEventButton         *event,
254                                          SummaryView            *summaryview);
255 static void summary_button_released     (GtkWidget              *ctree,
256                                          GdkEventButton         *event,
257                                          SummaryView            *summaryview);
258 static gint summary_key_pressed         (GtkWidget              *ctree,
259                                          GdkEventKey            *event,
260                                          SummaryView            *summaryview);
261 static void summary_open_row            (GtkSCTree              *sctree,
262                                          SummaryView            *summaryview);
263 static void summary_tree_expanded       (GtkCTree               *ctree,
264                                          GtkCTreeNode           *node,
265                                          SummaryView            *summaryview);
266 static void summary_tree_collapsed      (GtkCTree               *ctree,
267                                          GtkCTreeNode           *node,
268                                          SummaryView            *summaryview);
269 static void summary_selected            (GtkCTree               *ctree,
270                                          GtkCTreeNode           *row,
271                                          gint                    column,
272                                          SummaryView            *summaryview);
273 static void summary_col_resized         (GtkCList               *clist,
274                                          gint                    column,
275                                          gint                    width,
276                                          SummaryView            *summaryview);
277 static void summary_reply_cb            (SummaryView            *summaryview,
278                                          guint                   action,
279                                          GtkWidget              *widget);
280 static void summary_show_all_header_cb  (SummaryView            *summaryview,
281                                          guint                   action,
282                                          GtkWidget              *widget);
283
284 static void summary_add_address_cb      (SummaryView            *summaryview,
285                                          guint                   action,
286                                          GtkWidget              *widget);
287 static void summary_create_filter_cb    (SummaryView            *summaryview,
288                                          guint                   action,
289                                          GtkWidget              *widget);
290 static void summary_create_processing_cb(SummaryView            *summaryview,
291                                          guint                   action,
292                                          GtkWidget              *widget);
293
294 static void summary_mark_clicked        (GtkWidget              *button,
295                                          SummaryView            *summaryview);
296 static void summary_status_clicked      (GtkWidget              *button,
297                                          SummaryView            *summaryview);
298 static void summary_mime_clicked        (GtkWidget              *button,
299                                          SummaryView            *summaryview);
300 static void summary_num_clicked         (GtkWidget              *button,
301                                          SummaryView            *summaryview);
302 static void summary_score_clicked       (GtkWidget *button,
303                                          SummaryView *summaryview);
304 static void summary_size_clicked        (GtkWidget              *button,
305                                          SummaryView            *summaryview);
306 static void summary_date_clicked        (GtkWidget              *button,
307                                          SummaryView            *summaryview);
308 static void summary_from_clicked        (GtkWidget              *button,
309                                          SummaryView            *summaryview);
310 static void summary_subject_clicked     (GtkWidget              *button,
311                                          SummaryView            *summaryview);
312 static void summary_score_clicked       (GtkWidget              *button,
313                                          SummaryView            *summaryview);
314 static void summary_locked_clicked      (GtkWidget              *button,
315                                          SummaryView            *summaryview);
316
317 static void summary_start_drag          (GtkWidget        *widget, 
318                                          int button,
319                                          GdkEvent *event,
320                                          SummaryView      *summaryview);
321 static void summary_drag_data_get       (GtkWidget        *widget,
322                                          GdkDragContext   *drag_context,
323                                          GtkSelectionData *selection_data,
324                                          guint             info,
325                                          guint             time,
326                                          SummaryView      *summaryview);
327
328 /* custom compare functions for sorting */
329
330 static gint summary_cmp_by_mark         (GtkCList               *clist,
331                                          gconstpointer           ptr1,
332                                          gconstpointer           ptr2);
333 static gint summary_cmp_by_status       (GtkCList               *clist,
334                                          gconstpointer           ptr1,
335                                          gconstpointer           ptr2);
336 static gint summary_cmp_by_mime         (GtkCList               *clist,
337                                          gconstpointer           ptr1,
338                                          gconstpointer           ptr2);
339 static gint summary_cmp_by_num          (GtkCList               *clist,
340                                          gconstpointer           ptr1,
341                                          gconstpointer           ptr2);
342 static gint summary_cmp_by_size         (GtkCList               *clist,
343                                          gconstpointer           ptr1,
344                                          gconstpointer           ptr2);
345 static gint summary_cmp_by_date         (GtkCList               *clist,
346                                          gconstpointer           ptr1,
347                                          gconstpointer           ptr2);
348 static gint summary_cmp_by_from         (GtkCList               *clist,
349                                          gconstpointer           ptr1,
350                                          gconstpointer           ptr2);
351 static gint summary_cmp_by_simplified_subject
352                                         (GtkCList               *clist, 
353                                          gconstpointer           ptr1, 
354                                          gconstpointer           ptr2);
355 static gint summary_cmp_by_score        (GtkCList               *clist,
356                                          gconstpointer           ptr1,
357                                          gconstpointer           ptr2);
358 static gint summary_cmp_by_label        (GtkCList               *clist,
359                                          gconstpointer           ptr1,
360                                          gconstpointer           ptr2);
361 static gint summary_cmp_by_to           (GtkCList               *clist,
362                                          gconstpointer           ptr1,
363                                          gconstpointer           ptr2);
364 static gint summary_cmp_by_subject      (GtkCList               *clist,
365                                          gconstpointer           ptr1,
366                                          gconstpointer           ptr2);
367 static gint summary_cmp_by_locked       (GtkCList               *clist,
368                                          gconstpointer           ptr1, 
369                                          gconstpointer           ptr2);
370
371 static void news_flag_crosspost         (MsgInfo *msginfo);
372
373 static void quicksearch_execute_cb      (QuickSearch    *quicksearch,
374                                          gpointer        data);
375 static void tog_searchbar_cb            (GtkWidget      *w,
376                                          gpointer        data);
377
378 static void summary_find_answers        (SummaryView    *summaryview, 
379                                          MsgInfo        *msg);
380
381 static gboolean summary_update_msg      (gpointer source, gpointer data);
382
383 GtkTargetEntry summary_drag_types[1] =
384 {
385         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
386 };
387
388 static GtkItemFactoryEntry summary_popup_entries[] =
389 {
390         {N_("/_Reply"),                 "<control>R", summary_reply_cb, COMPOSE_REPLY, NULL},
391         {N_("/Repl_y to"),              NULL, NULL,             0, "<Branch>"},
392         {N_("/Repl_y to/_all"),         "<shift><control>R", summary_reply_cb,  COMPOSE_REPLY_TO_ALL, NULL},
393         {N_("/Repl_y to/_sender"),      NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
394         {N_("/Repl_y to/mailing _list"),
395                                         "<control>L", summary_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
396         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
397         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
398         {N_("/_Forward"),               "<control><alt>F", summary_reply_cb, COMPOSE_FORWARD, NULL},
399         {N_("/Redirect"),               NULL, summary_reply_cb, COMPOSE_REDIRECT, NULL},
400         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
401         {N_("/M_ove..."),               "<control>O", summary_move_to,  0, NULL},
402         {N_("/_Copy..."),               "<shift><control>O", summary_copy_to,   0, NULL},
403         {N_("/_Delete"),                "<control>D", summary_delete,   0, NULL},
404         {N_("/Cancel a news message"),  NULL, summary_cancel,   0, NULL},
405         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
406         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
407         {N_("/_Mark/_Mark"),            "<shift>asterisk", summary_mark,        0, NULL},
408         {N_("/_Mark/_Unmark"),          "U", summary_unmark,    0, NULL},
409         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
410         {N_("/_Mark/Mark as unr_ead"),  "<shift>exclam", summary_mark_as_unread, 0, NULL},
411         {N_("/_Mark/Mark as rea_d"),    NULL, summary_mark_as_read, 0, NULL},
412         {N_("/_Mark/Mark all read"),    NULL, summary_mark_all_read, 0, NULL},
413         {N_("/_Mark/Ignore thread"),    NULL, summary_ignore_thread, 0, NULL},
414         {N_("/_Mark/Unignore thread"),  NULL, summary_unignore_thread, 0, NULL},
415         {N_("/_Mark/Lock"),             NULL, summary_msgs_lock, 0, NULL},
416         {N_("/_Mark/Unlock"),           NULL, summary_msgs_unlock, 0, NULL},
417         {N_("/Color la_bel"),           NULL, NULL,             0, NULL},
418
419         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
420         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
421         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
422         {N_("/Add sender to address boo_k"),
423                                         NULL, summary_add_address_cb, 0, NULL},
424         {N_("/Create f_ilter rule"),    NULL, NULL,             0, "<Branch>"},
425         {N_("/Create f_ilter rule/_Automatically"),
426                                         NULL, summary_create_filter_cb, FILTER_BY_AUTO, NULL},
427         {N_("/Create f_ilter rule/by _From"),
428                                         NULL, summary_create_filter_cb, FILTER_BY_FROM, NULL},
429         {N_("/Create f_ilter rule/by _To"),
430                                         NULL, summary_create_filter_cb, FILTER_BY_TO, NULL},
431         {N_("/Create f_ilter rule/by _Subject"),
432                                         NULL, summary_create_filter_cb, FILTER_BY_SUBJECT, NULL},
433         {N_("/Create processing rule"), NULL, NULL,             0, "<Branch>"},
434         {N_("/Create processing rule/_Automatically"),
435                                         NULL, summary_create_processing_cb, FILTER_BY_AUTO, NULL},
436         {N_("/Create processing rule/by _From"),
437                                         NULL, summary_create_processing_cb, FILTER_BY_FROM, NULL},
438         {N_("/Create processing rule/by _To"),
439                                         NULL, summary_create_processing_cb, FILTER_BY_TO, NULL},
440         {N_("/Create processing rule/by _Subject"),
441                                         NULL, summary_create_processing_cb, FILTER_BY_SUBJECT, NULL},
442         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
443         {N_("/_View"),                  NULL, NULL,             0, "<Branch>"},
444         {N_("/_View/Open in new _window"),
445                                         "<control><alt>N", summary_open_msg,    0, NULL},
446         {N_("/_View/_Source"),          "<control>U", summary_view_source, 0, NULL},
447         {N_("/_View/All _header"),      "<control>H", summary_show_all_header_cb, 0, "<ToggleItem>"},
448 };  /* see also list in menu_connect_identical_items() in menu.c if this changes */
449
450 static const gchar *const col_label[N_SUMMARY_COLS] = {
451         N_("M"),        /* S_COL_MARK    */
452         N_("S"),        /* S_COL_STATUS  */
453         "",             /* S_COL_MIME    */
454         N_("Subject"),  /* S_COL_SUBJECT */
455         N_("From"),     /* S_COL_FROM    */
456         N_("Date"),     /* S_COL_DATE    */
457         N_("Size"),     /* S_COL_SIZE    */
458         N_("No."),      /* S_COL_NUMBER  */
459         N_("Score"),    /* S_COL_SCORE   */
460         N_("L")         /* S_COL_LOCKED  */
461 };
462
463 SummaryView *summary_create(void)
464 {
465         SummaryView *summaryview;
466         GtkWidget *vbox;
467         GtkWidget *scrolledwin;
468         GtkWidget *ctree;
469         GtkWidget *hbox;
470         GtkWidget *hbox_l;
471         GtkWidget *statlabel_folder;
472         GtkWidget *statlabel_select;
473         GtkWidget *statlabel_msgs;
474         GtkWidget *hbox_spc;
475         GtkWidget *toggle_eventbox;
476         GtkWidget *toggle_arrow;
477         GtkWidget *popupmenu;
478         GtkWidget *toggle_search;
479         GtkTooltips *search_tip;
480         GtkItemFactory *popupfactory;
481         gint n_entries;
482         QuickSearch *quicksearch;
483
484         debug_print("Creating summary view...\n");
485         summaryview = g_new0(SummaryView, 1);
486
487 #define SUMMARY_VBOX_SPACING 3
488         vbox = gtk_vbox_new(FALSE, SUMMARY_VBOX_SPACING);
489         
490         /* create status label */
491         hbox = gtk_hbox_new(FALSE, 0);
492         gtk_widget_show(hbox);
493
494         search_tip = gtk_tooltips_new();
495         toggle_search = gtk_toggle_button_new();
496         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_search),
497                                      prefs_common.show_searchbar);
498         gtk_widget_show(toggle_search);
499
500         gtk_tooltips_set_tip(GTK_TOOLTIPS(search_tip),
501                              toggle_search,
502                              _("Toggle quick-search bar"), NULL);
503         
504         gtk_box_pack_start(GTK_BOX(hbox), toggle_search, FALSE, FALSE, 2);      
505
506         hbox_l = gtk_hbox_new(FALSE, 0);
507         gtk_widget_show(hbox_l);
508         gtk_box_pack_start(GTK_BOX(hbox), hbox_l, TRUE, TRUE, 0);
509  
510         statlabel_folder = gtk_label_new("");
511         gtk_widget_show(statlabel_folder);
512         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
513         statlabel_select = gtk_label_new("");
514         gtk_widget_show(statlabel_select);
515         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
516  
517         /* toggle view button */
518         toggle_eventbox = gtk_event_box_new();
519         gtk_widget_show(toggle_eventbox);
520         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
521         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
522         gtk_widget_show(toggle_arrow);
523         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
524         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
525                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
526                            summaryview);
527         
528         
529         statlabel_msgs = gtk_label_new("");
530         gtk_widget_show(statlabel_msgs);
531         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
532
533         hbox_spc = gtk_hbox_new(FALSE, 0);
534         gtk_widget_show(hbox_spc);
535         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
536
537         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
538         gtk_widget_show(scrolledwin);
539         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
540                                        GTK_POLICY_AUTOMATIC,
541                                        GTK_POLICY_ALWAYS);
542         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
543         gtk_widget_set_usize(vbox,
544                              prefs_common.summaryview_width,
545                              prefs_common.summaryview_height);
546
547         ctree = summary_ctree_create(summaryview);
548         gtk_widget_show(ctree);
549
550         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
551                                             GTK_CLIST(ctree)->hadjustment);
552         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
553                                             GTK_CLIST(ctree)->vadjustment);
554         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
555
556         /* status label */
557         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
558
559         /* quick search */
560         quicksearch = quicksearch_new();
561         gtk_box_pack_start(GTK_BOX(vbox), quicksearch_get_widget(quicksearch), FALSE, FALSE, 0);
562         if (prefs_common.show_searchbar)
563                 quicksearch_show(quicksearch);
564         else
565                 quicksearch_hide(quicksearch);
566         quicksearch_set_execute_callback(quicksearch, quicksearch_execute_cb, summaryview);
567
568         gtk_signal_connect (GTK_OBJECT(toggle_search), "toggled",
569                         GTK_SIGNAL_FUNC(tog_searchbar_cb), summaryview);
570
571         /* create popup menu */
572         n_entries = sizeof(summary_popup_entries) /
573                 sizeof(summary_popup_entries[0]);
574         popupmenu = menu_create_items(summary_popup_entries, n_entries,
575                                       "<SummaryView>", &popupfactory,
576                                       summaryview);
577
578         summaryview->vbox = vbox;
579         summaryview->scrolledwin = scrolledwin;
580         summaryview->ctree = ctree;
581         summaryview->hbox = hbox;
582         summaryview->hbox_l = hbox_l;
583         summaryview->statlabel_folder = statlabel_folder;
584         summaryview->statlabel_select = statlabel_select;
585         summaryview->statlabel_msgs = statlabel_msgs;
586         summaryview->toggle_eventbox = toggle_eventbox;
587         summaryview->toggle_arrow = toggle_arrow;
588         summaryview->toggle_search = toggle_search;
589         summaryview->popupmenu = popupmenu;
590         summaryview->popupfactory = popupfactory;
591         summaryview->lock_count = 0;
592         summaryview->msginfo_update_callback_id =
593                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
594
595         summaryview->target_list = gtk_target_list_new(summary_drag_types, 1);
596
597         summaryview->quicksearch = quicksearch;
598
599         /* CLAWS: need this to get the SummaryView * from
600          * the CList */
601         gtk_object_set_data(GTK_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
602
603         gtk_widget_show(vbox);
604
605         return summaryview;
606 }
607
608 void summary_init(SummaryView *summaryview)
609 {
610         static GdkFont *boldfont = NULL;
611         static GdkFont *smallfont = NULL;
612         GtkStyle *style;
613         GtkWidget *pixmap;
614
615         gtk_widget_realize(summaryview->ctree);
616         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
617                          &markxpm, &markxpmmask);
618         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
619                          &deletedxpm, &deletedxpmmask);
620         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
621                          &newxpm, &newxpmmask);
622         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
623                          &unreadxpm, &unreadxpmmask);
624         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
625                          &repliedxpm, &repliedxpmmask);
626         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
627                          &forwardedxpm, &forwardedxpmmask);
628         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
629                          &clipxpm, &clipxpmmask);
630         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
631                          &lockedxpm, &lockedxpmmask);
632         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
633                          &ignorethreadxpm, &ignorethreadxpmmask);
634         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
635                          &clipkeyxpm, &clipkeyxpmmask);
636         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
637                          &keyxpm, &keyxpmmask);
638         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_GPG_SIGNED,
639                          &gpgsignedxpm, &gpgsignedxpmmask);
640
641         if (!bold_style) {
642                 bold_style = gtk_style_copy
643                         (gtk_widget_get_style(summaryview->ctree));
644                 if (!boldfont)
645                         boldfont = gtkut_font_load(BOLD_FONT);
646                 if (boldfont)
647                         bold_style->font = boldfont;
648                 bold_marked_style = gtk_style_copy(bold_style);
649                 bold_marked_style->fg[GTK_STATE_NORMAL] =
650                         summaryview->color_marked;
651                 bold_deleted_style = gtk_style_copy(bold_style);
652                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
653                         summaryview->color_dim;
654         }
655         
656         if (!small_style) {
657                 small_style = gtk_style_copy
658                         (gtk_widget_get_style(summaryview->ctree));
659                 if (!smallfont)
660                         smallfont = gtkut_font_load(SMALL_FONT);
661                 if (smallfont)
662                         small_style->font = smallfont;
663                 small_marked_style = gtk_style_copy(small_style);
664                 small_marked_style->fg[GTK_STATE_NORMAL] =
665                         summaryview->color_marked;
666                 small_deleted_style = gtk_style_copy(small_style);
667                 small_deleted_style->fg[GTK_STATE_NORMAL] =
668                         summaryview->color_dim;
669
670                 gtk_widget_set_style(summaryview->ctree, small_style);
671         }
672
673         style = gtk_style_copy(gtk_widget_get_style
674                                 (summaryview->statlabel_folder));
675         if (smallfont)
676                 style->font = smallfont;
677         gtk_widget_set_style(summaryview->statlabel_folder, style);
678         gtk_widget_set_style(summaryview->statlabel_select, style);
679         gtk_widget_set_style(summaryview->statlabel_msgs, style);
680
681         pixmap = stock_pixmap_widget(summaryview->hbox_l, STOCK_PIXMAP_DIR_OPEN);
682         gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), pixmap, FALSE, FALSE, 4);
683         gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), pixmap, 0);
684         gtk_widget_show(pixmap);
685         summaryview->folder_pixmap = pixmap;
686
687         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
688         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
689         gtk_widget_show(pixmap);
690         summaryview->quick_search_pixmap = pixmap;
691
692         /* Init summaryview prefs */
693         summaryview->sort_key = SORT_BY_NONE;
694         summaryview->sort_type = SORT_ASCENDING;
695
696         /* Init summaryview extra data */
697         summaryview->simplify_subject_preg = NULL;
698
699         summary_clear_list(summaryview);
700         summary_set_column_titles(summaryview);
701         summary_colorlabel_menu_create(summaryview);
702         summary_set_menu_sensitive(summaryview);
703
704 }
705
706 #if 0
707 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
708                                                  GtkCTreeNode *current_node)
709 {
710         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
711         GtkCTreeNode *node;
712         MsgInfo *msginfo;
713         gint best_score = MIN_SCORE;
714         GtkCTreeNode *best_node = NULL;
715
716         if (current_node)
717                 /*node = current_node;*/
718                 node = GTK_CTREE_NODE_NEXT(current_node);
719         else
720                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
721
722         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
723                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
724                 if (msginfo->score >= summaryview->important_score)
725                         break;
726                 if (msginfo->score > best_score) {
727                         best_score = msginfo->score;
728                         best_node = node;
729                 }
730         }
731
732         if (node != NULL)
733                 return node;
734         else
735                 return best_node;
736 }
737
738 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
739                                                  GtkCTreeNode *current_node)
740 {
741         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
742         GtkCTreeNode *node;
743         MsgInfo *msginfo;
744         gint best_score = MIN_SCORE;
745         GtkCTreeNode *best_node = NULL;
746
747         if (current_node)
748                 /*node = current_node;*/
749                 node = GTK_CTREE_NODE_PREV(current_node);
750         else
751                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
752
753         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
754                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
755                 if (msginfo->score >= summaryview->important_score)
756                         break;
757                 if (msginfo->score > best_score) {
758                         best_score = msginfo->score;
759                         best_node = node;
760                 }
761         }
762
763         if (node != NULL)
764                 return node;
765         else
766                 return best_node;
767 }
768 #endif
769
770 #define CURRENTLY_DISPLAYED(m) \
771 ( (m->msgnum == displayed_msgnum) \
772   && (!g_strcasecmp(m->folder->name,item->name)) )
773
774 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
775 {
776         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
777         GtkCTreeNode *node;
778         GSList *mlist = NULL;
779         gchar *buf;
780         gboolean is_refresh;
781         guint selected_msgnum = 0;
782         guint displayed_msgnum = 0;
783         GSList *cur;
784         GSList *not_killed;
785         gboolean quicksearch_changed = FALSE;
786
787         if (summary_is_locked(summaryview)) return FALSE;
788
789         inc_lock();
790         summary_lock(summaryview);
791
792         if (!prefs_common.summary_quicksearch_sticky
793          && !quicksearch_is_running(summaryview->quicksearch)) {
794                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
795         }
796
797         if (quicksearch_is_running(summaryview->quicksearch))
798                 quicksearch_changed = TRUE;
799
800         /* STATUSBAR_POP(summaryview->mainwin); */
801
802         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
803         if (is_refresh) {
804                 selected_msgnum = summary_get_msgnum(summaryview,
805                                                      summaryview->selected);
806                 displayed_msgnum = summary_get_msgnum(summaryview,
807                                                       summaryview->displayed);
808         }
809
810         /* process the marks if any */
811         if (summaryview->mainwin->lock_count == 0 &&
812             (summaryview->moved > 0 || summaryview->copied > 0)) {
813                 AlertValue val;
814                 gboolean changed = FALSE;
815
816                 val = alertpanel(_("Process mark"),
817                                  _("Some marks are left. Process it?"),
818                                  _("Yes"), _("No"), _("Cancel"));
819                 if (G_ALERTDEFAULT == val) {
820                         summary_unlock(summaryview);
821                         summary_execute(summaryview);
822                         summary_lock(summaryview);
823                         changed = TRUE;
824                 } else if (G_ALERTALTERNATE == val) {
825                         /* DO NOTHING */
826                 } else {
827                         summary_unlock(summaryview);
828                         inc_unlock();
829                         return FALSE;
830                 }
831                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
832                         folder_update_op_count();
833         }
834         
835         gtk_clist_freeze(GTK_CLIST(ctree));
836
837         summary_clear_list(summaryview);
838         summary_set_column_titles(summaryview);
839
840         buf = NULL;
841         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
842                 g_free(buf);
843                 debug_print("empty folder\n\n");
844                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
845                 summary_clear_all(summaryview);
846                 summaryview->folder_item = item;
847                 gtk_clist_thaw(GTK_CLIST(ctree));
848                 summary_unlock(summaryview);
849                 inc_unlock();
850                 return TRUE;
851         }
852         g_free(buf);
853
854         if (!is_refresh)
855                 messageview_clear(summaryview->messageview);
856
857         summaryview->folder_item = item;
858         item->opened = TRUE;
859
860         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
861
862         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
863         debug_print("%s\n", buf);
864         STATUSBAR_PUSH(summaryview->mainwin, buf);
865         g_free(buf);
866
867         main_window_cursor_wait(summaryview->mainwin);
868
869 /*
870         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
871
872         !!! NEW !!!
873         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
874         BY OTHER PROGRAMS TO THE FOLDER
875 */
876         mlist = folder_item_get_msg_list(item);
877
878         if (summaryview->folder_item->hide_read_msgs) {
879                 GSList *not_killed;
880                 
881                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
882                 not_killed = NULL;
883                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
884                         MsgInfo * msginfo = (MsgInfo *) cur->data;
885                         
886                         if ((MSG_IS_UNREAD(msginfo->flags)
887                              || MSG_IS_MARKED(msginfo->flags)
888                              || MSG_IS_LOCKED(msginfo->flags)
889                              || CURRENTLY_DISPLAYED(msginfo))
890                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
891                                 not_killed = g_slist_prepend(not_killed, msginfo);
892                         else
893                                 procmsg_msginfo_free(msginfo);
894                 }
895                 g_slist_free(mlist);
896                 mlist = not_killed;
897         } else {
898                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
899         }
900
901         if (quicksearch_is_active(summaryview->quicksearch)) {
902                 GSList *not_killed;
903                 
904                 not_killed = NULL;
905                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
906                         MsgInfo * msginfo = (MsgInfo *) cur->data;
907
908                         if (quicksearch_match(summaryview->quicksearch, msginfo))
909                                 not_killed = g_slist_prepend(not_killed, msginfo);
910                         else
911                                 procmsg_msginfo_free(msginfo);
912                 }
913                 
914                 if (quicksearch_changed) {
915                         /* only scan subfolders when quicksearch changed,
916                          * not when search is the same and folder changed */
917                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
918                         quicksearch_search_subfolders(summaryview->quicksearch, 
919                                               summaryview->folderview,
920                                               summaryview->folder_item);
921                 }
922                 
923                 g_slist_free(mlist);
924                 mlist = not_killed;
925         }
926
927 #if 0   
928         if ((global_scoring || item->prefs->scoring)) {
929                 GSList *not_killed;
930                 gint kill_score;
931
932                 not_killed = NULL;
933                 kill_score = prefs_common.kill_score;
934                 if (item->prefs->kill_score > kill_score)
935                         kill_score = item->prefs->kill_score;
936                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
937                         MsgInfo * msginfo = (MsgInfo *) cur->data;
938
939                         if (msginfo->score > kill_score)
940                                 not_killed = g_slist_prepend(not_killed, msginfo);
941                         else
942                                 procmsg_msginfo_free(msginfo);
943                 }
944                 g_slist_free(mlist);
945                 mlist = not_killed;
946         }
947 #endif
948         not_killed = NULL;
949         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
950                 MsgInfo * msginfo = (MsgInfo *) cur->data;
951                 
952                 if (!msginfo->hidden)
953                         not_killed = g_slist_prepend(not_killed, msginfo);
954                 else
955                         procmsg_msginfo_free(msginfo);
956         }
957         g_slist_free(mlist);
958         mlist = not_killed;
959
960         STATUSBAR_POP(summaryview->mainwin);
961
962         /* set ctree and hash table from the msginfo list, and
963            create the thread */
964         summary_set_ctree_from_list(summaryview, mlist);
965
966         g_slist_free(mlist);
967
968         if (summaryview->sort_key != SORT_BY_NONE)
969                 summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
970
971         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
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 cur\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                                                  _("Yes"), _("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         node = summary_find_next_flagged_msg
1310                 (summaryview, node, MSG_UNREAD, skip_cur);
1311         
1312         if (node)
1313                 summary_select_node(summaryview, node, TRUE, FALSE);
1314         else {
1315                 node = summary_find_next_flagged_msg
1316                         (summaryview, NULL, MSG_UNREAD, FALSE);
1317                 if (node == NULL) {
1318                         AlertValue val = 0;
1319
1320                         switch (prefs_common.next_unread_msg_dialog) {
1321                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1322                                         val = alertpanel(_("No more unread messages"),
1323                                                          _("No unread message found. "
1324                                                            "Go to next folder?"),
1325                                                          _("Yes"), _("No"), NULL);
1326                                         break;
1327                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1328                                         val = G_ALERTDEFAULT;
1329                                         break;
1330                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1331                                         val = G_ALERTOTHER;
1332                                         break;
1333                                 default:
1334                                         debug_print(
1335                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1336                         }
1337
1338                         if (val == G_ALERTDEFAULT) {
1339                                 if (gtk_signal_n_emissions_by_name
1340                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1341                                                 gtk_signal_emit_stop_by_name
1342                                                         (GTK_OBJECT(ctree),
1343                                                          "key_press_event");
1344                                 folderview_select_next_unread(summaryview->folderview);
1345                                 return;
1346                         } 
1347                         else
1348                                 return;
1349                 } else
1350                         summary_select_node(summaryview, node, TRUE, FALSE);
1351
1352         }
1353 }
1354
1355 void summary_select_prev_new(SummaryView *summaryview)
1356 {
1357         GtkCTreeNode *node;
1358
1359         node = summary_find_prev_flagged_msg
1360                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1361
1362         if (!node) {
1363                 AlertValue val;
1364
1365                 val = alertpanel(_("No more new messages"),
1366                                  _("No new message found. "
1367                                    "Search from the end?"),
1368                                  _("Yes"), _("No"), NULL);
1369                 if (val != G_ALERTDEFAULT) return;
1370                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1371                                                      MSG_NEW, FALSE);
1372         }
1373
1374         if (!node)
1375                 alertpanel_notice(_("No new messages."));
1376         else
1377                 summary_select_node(summaryview, node, TRUE, FALSE);
1378 }
1379
1380 void summary_select_next_new(SummaryView *summaryview)
1381 {
1382         GtkCTreeNode *node = summaryview->selected;
1383         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1384
1385         while ((node = summary_find_next_flagged_msg
1386                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1387                 AlertValue val;
1388
1389                 val = alertpanel(_("No more new messages"),
1390                                  _("No new message found. "
1391                                    "Go to next folder?"),
1392                                  _("Yes"), _("Search again"), _("No"));
1393                 if (val == G_ALERTDEFAULT) {
1394                         if (gtk_signal_n_emissions_by_name
1395                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1396                                         gtk_signal_emit_stop_by_name
1397                                                 (GTK_OBJECT(ctree),
1398                                                  "key_press_event");
1399                         folderview_select_next_unread(summaryview->folderview);
1400                         return;
1401                 } else if (val == G_ALERTALTERNATE)
1402                         node = NULL;
1403                 else
1404                         return;
1405         }
1406
1407         if (node)
1408                 summary_select_node(summaryview, node, TRUE, FALSE);
1409 }
1410
1411 void summary_select_prev_marked(SummaryView *summaryview)
1412 {
1413         GtkCTreeNode *node;
1414
1415         node = summary_find_prev_flagged_msg
1416                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1417
1418         if (!node) {
1419                 AlertValue val;
1420
1421                 val = alertpanel(_("No more marked messages"),
1422                                  _("No marked message found. "
1423                                    "Search from the end?"),
1424                                  _("Yes"), _("No"), NULL);
1425                 if (val != G_ALERTDEFAULT) return;
1426                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1427                                                      MSG_MARKED, TRUE);
1428         }
1429
1430         if (!node)
1431                 alertpanel_notice(_("No marked messages."));
1432         else
1433                 summary_select_node(summaryview, node, TRUE, FALSE);
1434 }
1435
1436 void summary_select_next_marked(SummaryView *summaryview)
1437 {
1438         GtkCTreeNode *node;
1439
1440         node = summary_find_next_flagged_msg
1441                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1442
1443         if (!node) {
1444                 AlertValue val;
1445
1446                 val = alertpanel(_("No more marked messages"),
1447                                  _("No marked message found. "
1448                                    "Search from the beginning?"),
1449                                  _("Yes"), _("No"), NULL);
1450                 if (val != G_ALERTDEFAULT) return;
1451                 node = summary_find_next_flagged_msg(summaryview, NULL,
1452                                                      MSG_MARKED, TRUE);
1453         }
1454
1455         if (!node)
1456                 alertpanel_notice(_("No marked messages."));
1457         else
1458                 summary_select_node(summaryview, node, TRUE, FALSE);
1459 }
1460
1461 void summary_select_prev_labeled(SummaryView *summaryview)
1462 {
1463         GtkCTreeNode *node;
1464
1465         node = summary_find_prev_flagged_msg
1466                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1467
1468         if (!node) {
1469                 AlertValue val;
1470
1471                 val = alertpanel(_("No more labeled messages"),
1472                                  _("No labeled message found. "
1473                                    "Search from the end?"),
1474                                  _("Yes"), _("No"), NULL);
1475                 if (val != G_ALERTDEFAULT) return;
1476                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1477                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1478         }
1479
1480         if (!node)
1481                 alertpanel_notice(_("No labeled messages."));
1482         else
1483                 summary_select_node(summaryview, node, TRUE, FALSE);
1484 }
1485
1486 void summary_select_next_labeled(SummaryView *summaryview)
1487 {
1488         GtkCTreeNode *node;
1489
1490         node = summary_find_next_flagged_msg
1491                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1492
1493         if (!node) {
1494                 AlertValue val;
1495
1496                 val = alertpanel(_("No more labeled messages"),
1497                                  _("No labeled message found. "
1498                                    "Search from the beginning?"),
1499                                  _("Yes"), _("No"), NULL);
1500                 if (val != G_ALERTDEFAULT) return;
1501                 node = summary_find_next_flagged_msg(summaryview, NULL,
1502                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1503         }
1504
1505         if (!node)
1506                 alertpanel_notice(_("No labeled messages."));
1507         else
1508                 summary_select_node(summaryview, node, TRUE, FALSE);
1509 }
1510
1511 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1512 {
1513         GtkCTreeNode *node;
1514
1515         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1516         summary_select_node(summaryview, node, FALSE, TRUE);
1517 }
1518
1519 /**
1520  * summary_select_node:
1521  * @summaryview: Summary view.
1522  * @node: Summary tree node.
1523  * @display_msg: TRUE to display the selected message.
1524  * @do_refresh: TRUE to refresh the widget.
1525  *
1526  * Select @node (bringing it into view by scrolling and expanding its
1527  * thread, if necessary) and unselect all others.  If @display_msg is
1528  * TRUE, display the corresponding message in the message view.
1529  * If @do_refresh is TRUE, the widget is refreshed.
1530  **/
1531 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1532                          gboolean display_msg, gboolean do_refresh)
1533 {
1534         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1535
1536         if (node) {
1537                 gtkut_ctree_expand_parent_all(ctree, node);
1538                 if (do_refresh) {
1539                         GTK_EVENTS_FLUSH();
1540                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1541                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1542                 }
1543                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1544                 if (display_msg && summaryview->displayed == node)
1545                         summaryview->displayed = NULL;
1546                 summaryview->display_msg = display_msg;
1547                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1548         }
1549 }
1550
1551 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1552 {
1553         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1554         MsgInfo *msginfo;
1555
1556         if (!node)
1557                 return 0;
1558         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1559         return msginfo->msgnum;
1560 }
1561
1562 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1563                                            GtkCTreeNode *current_node)
1564 {
1565         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1566         GtkCTreeNode *node;
1567         MsgInfo *msginfo;
1568
1569         if (current_node)
1570                 node = current_node;
1571         else
1572                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1573
1574         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1575                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1576                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1577         }
1578
1579         return node;
1580 }
1581
1582 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1583                                            GtkCTreeNode *current_node)
1584 {
1585         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1586         GtkCTreeNode *node;
1587         MsgInfo *msginfo;
1588
1589         if (current_node)
1590                 node = current_node;
1591         else
1592                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1593
1594         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1595                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1596                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1597         }
1598
1599         return node;
1600 }
1601
1602 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1603                                                    GtkCTreeNode *current_node,
1604                                                    MsgPermFlags flags,
1605                                                    gboolean start_from_prev)
1606 {
1607         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1608         GtkCTreeNode *node;
1609         MsgInfo *msginfo;
1610
1611         if (current_node) {
1612                 if (start_from_prev)
1613                         node = GTK_CTREE_NODE_PREV(current_node);
1614                 else
1615                         node = current_node;
1616         } else
1617                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1618
1619         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1620                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1621                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1622         }
1623
1624         return node;
1625 }
1626
1627 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1628                                                    GtkCTreeNode *current_node,
1629                                                    MsgPermFlags flags,
1630                                                    gboolean start_from_next)
1631 {
1632         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1633         GtkCTreeNode *node;
1634         MsgInfo *msginfo;
1635
1636         if (current_node) {
1637                 if (start_from_next)
1638                         node = gtkut_ctree_node_next(ctree, current_node);
1639                 else
1640                         node = current_node;
1641         } else
1642                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1643
1644         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1645                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1646                 /* Find msg with matching flags but ignore messages with
1647                    ignore flags, if searching for new or unread messages */
1648                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1649                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1650                         break;
1651         }
1652
1653         return node;
1654 }
1655
1656 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1657                                                 guint msgnum)
1658 {
1659         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1660         GtkCTreeNode *node;
1661         MsgInfo *msginfo;
1662
1663         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1664
1665         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1666                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1667                 if (msginfo && msginfo->msgnum == msgnum) break;
1668         }
1669
1670         return node;
1671 }
1672
1673 static guint attract_hash_func(gconstpointer key)
1674 {
1675         gchar *str;
1676         gchar *p;
1677         guint h;
1678
1679         Xstrdup_a(str, (const gchar *)key, return 0);
1680         trim_subject(str);
1681
1682         p = str;
1683         h = *p;
1684
1685         if (h) {
1686                 for (p += 1; *p != '\0'; p++)
1687                         h = (h << 5) - h + *p;
1688         }
1689
1690         return h;
1691 }
1692
1693 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1694 {
1695         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1696 }
1697
1698 void summary_attract_by_subject(SummaryView *summaryview)
1699 {
1700         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1701         GtkCList *clist = GTK_CLIST(ctree);
1702         GtkCTreeNode *src_node;
1703         GtkCTreeNode *dst_node, *sibling;
1704         GtkCTreeNode *tmp;
1705         MsgInfo *src_msginfo, *dst_msginfo;
1706         GHashTable *subject_table;
1707
1708         debug_print("Attracting messages by subject...");
1709         STATUSBAR_PUSH(summaryview->mainwin,
1710                        _("Attracting messages by subject..."));
1711
1712         main_window_cursor_wait(summaryview->mainwin);
1713         gtk_clist_freeze(clist);
1714
1715         subject_table = g_hash_table_new(attract_hash_func,
1716                                          attract_compare_func);
1717
1718         for (src_node = GTK_CTREE_NODE(clist->row_list);
1719              src_node != NULL;
1720              src_node = tmp) {
1721                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1722                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1723                 if (!src_msginfo) continue;
1724                 if (!src_msginfo->subject) continue;
1725
1726                 /* find attracting node */
1727                 dst_node = g_hash_table_lookup(subject_table,
1728                                                src_msginfo->subject);
1729
1730                 if (dst_node) {
1731                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1732
1733                         /* if the time difference is more than 20 days,
1734                            don't attract */
1735                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1736                             > 60 * 60 * 24 * 20)
1737                                 continue;
1738
1739                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1740                         if (src_node != sibling)
1741                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1742                 }
1743
1744                 g_hash_table_insert(subject_table,
1745                                     src_msginfo->subject, src_node);
1746         }
1747
1748         g_hash_table_destroy(subject_table);
1749
1750         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1751
1752         gtk_clist_thaw(clist);
1753
1754         debug_print("done.\n");
1755         STATUSBAR_POP(summaryview->mainwin);
1756
1757         main_window_cursor_normal(summaryview->mainwin);
1758 }
1759
1760 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1761                                       gpointer data)
1762 {
1763         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1764
1765         if (msginfo)
1766                 procmsg_msginfo_free(msginfo);
1767 }
1768
1769 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1770                                    gpointer data)
1771 {
1772         SummaryView *summaryview = data;
1773         MsgInfo *msginfo;
1774
1775         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1776
1777         if (msginfo->folder && msginfo->folder->folder &&
1778             msginfo->folder->folder->klass->type == F_NEWS)
1779                 news_flag_crosspost(msginfo);
1780
1781         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1782         && procmsg_msg_has_marked_parent(msginfo))
1783                 summaryview->unreadmarked++;
1784
1785         if (MSG_IS_DELETED(msginfo->flags))
1786                 summaryview->deleted++;
1787
1788         summaryview->total_size += msginfo->size;
1789
1790         summary_set_row_marks(summaryview, node);
1791 }
1792
1793 static void summary_update_status(SummaryView *summaryview)
1794 {
1795         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1796         GtkCTreeNode *node;
1797         MsgInfo *msginfo;
1798
1799         summaryview->total_size =
1800         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1801
1802         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1803              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1804                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1805
1806                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1807                 && procmsg_msg_has_marked_parent(msginfo))
1808                         summaryview->unreadmarked++;
1809                 if (MSG_IS_DELETED(msginfo->flags))
1810                         summaryview->deleted++;
1811                 if (MSG_IS_MOVE(msginfo->flags))
1812                         summaryview->moved++;
1813                 if (MSG_IS_COPY(msginfo->flags))
1814                         summaryview->copied++;
1815                 summaryview->total_size += msginfo->size;
1816         }
1817 }
1818
1819 static void summary_status_show(SummaryView *summaryview)
1820 {
1821         gchar *str;
1822         gchar *del, *mv, *cp;
1823         gchar *sel;
1824         gchar *spc;
1825         gchar *itstr;
1826         GList *rowlist, *cur;
1827         guint n_selected = 0;
1828         off_t sel_size = 0;
1829         MsgInfo *msginfo;
1830         gchar *name;
1831
1832         if (!summaryview->folder_item) {
1833                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1834                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1835                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1836                 return;
1837         }
1838
1839         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1840         for (cur = rowlist; cur != NULL; cur = cur->next) {
1841                 msginfo = gtk_ctree_node_get_row_data
1842                         (GTK_CTREE(summaryview->ctree),
1843                          GTK_CTREE_NODE(cur->data));
1844                 if (!msginfo)
1845                         g_warning("summary_status_show(): msginfo == NULL\n");
1846                 else {
1847                         sel_size += msginfo->size;
1848                         n_selected++;
1849                 }
1850         }
1851
1852         name = folder_item_get_name(summaryview->folder_item);
1853         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), name);
1854         g_free(name);
1855
1856         if (summaryview->deleted)
1857                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1858         else
1859                 del = g_strdup("");
1860         if (summaryview->moved)
1861                 mv = g_strdup_printf(_("%s%d moved"),
1862                                      summaryview->deleted ? _(", ") : "",
1863                                      summaryview->moved);
1864         else
1865                 mv = g_strdup("");
1866         if (summaryview->copied)
1867                 cp = g_strdup_printf(_("%s%d copied"),
1868                                      summaryview->deleted ||
1869                                      summaryview->moved ? _(", ") : "",
1870                                      summaryview->copied);
1871         else
1872                 cp = g_strdup("");
1873
1874         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1875                 spc = "    ";
1876         else
1877                 spc = "";
1878
1879         if (n_selected) {
1880                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1881                 if (n_selected == 1)
1882                         itstr = g_strdup(_(" item selected"));
1883                 else
1884                         itstr = g_strdup(_(" items selected"));
1885         } else {
1886                 sel = g_strdup("");
1887                 itstr = g_strdup("");
1888         }
1889                 
1890         str = g_strconcat(n_selected ? itos(n_selected) : "",
1891                                         itstr, sel, spc, del, mv, cp, NULL);
1892         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1893         g_free(str);
1894         g_free(sel);
1895         g_free(del);
1896         g_free(mv);
1897         g_free(cp);
1898         g_free(itstr);
1899
1900         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1901
1902                                       summaryview->folder_item->new_msgs,
1903                                       summaryview->folder_item->unread_msgs,
1904                                       summaryview->folder_item->total_msgs,
1905                                       to_human_readable(summaryview->total_size));
1906         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1907         g_free(str);
1908 }
1909
1910 static void summary_set_column_titles(SummaryView *summaryview)
1911 {
1912         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1913         GtkWidget *hbox;
1914         GtkWidget *label;
1915         GtkWidget *arrow;
1916         gint pos;
1917         const gchar *title;
1918         SummaryColumnType type;
1919         gboolean single_char;
1920         GtkJustification justify;
1921
1922         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1923                 SORT_BY_MARK,
1924                 SORT_BY_STATUS,
1925                 SORT_BY_MIME,
1926                 SORT_BY_SUBJECT,
1927                 SORT_BY_FROM,
1928                 SORT_BY_DATE,
1929                 SORT_BY_SIZE,
1930                 SORT_BY_NUMBER,
1931                 SORT_BY_SCORE,
1932                 SORT_BY_LOCKED
1933         };
1934
1935         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1936                 type = summaryview->col_state[pos].type;
1937
1938                 /* CLAWS: mime and unread are single char headers */
1939                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1940                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1941                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1942
1943                 switch (type) {
1944                 case S_COL_SUBJECT:
1945                 case S_COL_FROM:
1946                 case S_COL_DATE:
1947                 case S_COL_NUMBER:
1948                         if (prefs_common.trans_hdr)
1949                                 title = gettext(col_label[type]);
1950                         else
1951                                 title = col_label[type];
1952                         break;
1953                 /* CLAWS: dummies for mark and locked headers */        
1954                 case S_COL_MARK:        
1955                 case S_COL_LOCKED:
1956                         title = "";
1957                         break;
1958                 default:
1959                         title = gettext(col_label[type]);
1960                 }
1961
1962                 if (type == S_COL_MIME) {
1963                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1964                         gtk_widget_show(label);
1965                         gtk_clist_set_column_widget(clist, pos, label);
1966                         continue;
1967                 }
1968                 if (single_char) {
1969                         gtk_clist_set_column_title(clist, pos, title);
1970                         continue;
1971                 }
1972
1973                 /* CLAWS: changed so that locked and mark headers
1974                  * show a pixmap instead of single character */
1975                 hbox  = gtk_hbox_new(FALSE, 4);
1976                 
1977                 if (type == S_COL_LOCKED)
1978                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1979                 else if (type == S_COL_MARK) 
1980                         label = gtk_pixmap_new(markxpm, markxpmmask);
1981                 else 
1982                         label = gtk_label_new(title);
1983                 
1984                 if (justify == GTK_JUSTIFY_RIGHT)
1985                         gtk_box_pack_end(GTK_BOX(hbox), label,
1986                                          FALSE, FALSE, 0);
1987                 else
1988                         gtk_box_pack_start(GTK_BOX(hbox), label,
1989                                            FALSE, FALSE, 0);
1990
1991                 if (summaryview->sort_key == sort_by[type]) {
1992                         arrow = gtk_arrow_new
1993                                 (summaryview->sort_type == SORT_ASCENDING
1994                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1995                                  GTK_SHADOW_IN);
1996                         if (justify == GTK_JUSTIFY_RIGHT)
1997                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1998                                                    FALSE, FALSE, 0);
1999                         else
2000                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2001                                                  FALSE, FALSE, 0);
2002                 }
2003
2004                 gtk_widget_show_all(hbox);
2005                 gtk_clist_set_column_widget(clist, pos, hbox);
2006         }
2007 }
2008
2009 void summary_sort(SummaryView *summaryview,
2010                   FolderSortKey sort_key, FolderSortType sort_type)
2011 {
2012         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2013         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2014         GtkCListCompareFunc cmp_func = NULL;
2015
2016         switch (sort_key) {
2017         case SORT_BY_MARK:
2018                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2019                 break;
2020         case SORT_BY_STATUS:
2021                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2022                 break;
2023         case SORT_BY_MIME:
2024                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2025                 break;
2026         case SORT_BY_NUMBER:
2027                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2028                 break;
2029         case SORT_BY_SIZE:
2030                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2031                 break;
2032         case SORT_BY_DATE:
2033                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2034                 break;
2035         case SORT_BY_FROM:
2036                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2037                 break;
2038         case SORT_BY_SUBJECT:
2039                 if (summaryview->simplify_subject_preg)
2040                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2041                 else
2042                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2043                 break;
2044         case SORT_BY_SCORE:
2045                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2046                 break;
2047         case SORT_BY_LABEL:
2048                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2049                 break;
2050         case SORT_BY_TO:
2051                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2052                 break;
2053         case SORT_BY_LOCKED:
2054                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2055                 break;
2056         case SORT_BY_NONE:
2057                 break;
2058         default:
2059                 return;
2060         }
2061
2062         summaryview->sort_key = sort_key;
2063         summaryview->sort_type = sort_type;
2064
2065         summary_set_column_titles(summaryview);
2066         summary_set_menu_sensitive(summaryview);
2067
2068         /* allow fallback to don't sort */
2069         if (summaryview->sort_key == SORT_BY_NONE)
2070                 return;
2071
2072         if(cmp_func != NULL) {
2073                 debug_print("Sorting summary...");
2074                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2075
2076                 main_window_cursor_wait(summaryview->mainwin);
2077
2078                 gtk_clist_set_compare_func(clist, cmp_func);
2079
2080                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2081
2082                 gtk_sctree_sort_recursive(ctree, NULL);
2083
2084                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2085
2086                 main_window_cursor_normal(summaryview->mainwin);
2087
2088                 debug_print("done.\n");
2089                 STATUSBAR_POP(summaryview->mainwin);
2090         }
2091 }
2092
2093 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2094                                    GtkCTreeNode *cnode, gpointer data)
2095 {
2096         SummaryView *summaryview = (SummaryView *)data;
2097         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2098         gchar *text[N_SUMMARY_COLS];
2099         gint *col_pos = summaryview->col_pos;
2100         const gchar *msgid = msginfo->msgid;
2101         GHashTable *msgid_table = summaryview->msgid_table;
2102
2103         summary_set_header(summaryview, text, msginfo);
2104
2105         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2106                                 NULL, NULL, NULL, NULL, FALSE,
2107                                 gnode->parent->parent ? TRUE : FALSE);
2108 #define SET_TEXT(col) \
2109         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2110                                 text[col_pos[col]])
2111
2112         SET_TEXT(S_COL_NUMBER);
2113         SET_TEXT(S_COL_SCORE);
2114         SET_TEXT(S_COL_SIZE);
2115         SET_TEXT(S_COL_DATE);
2116         SET_TEXT(S_COL_FROM);
2117         SET_TEXT(S_COL_SUBJECT);
2118
2119 #undef SET_TEXT
2120
2121         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2122         summary_set_marks_func(ctree, cnode, summaryview);
2123
2124         if (msgid && msgid[0] != '\0')
2125                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2126
2127         return TRUE;
2128 }
2129
2130 static void summary_set_ctree_from_list(SummaryView *summaryview,
2131                                         GSList *mlist)
2132 {
2133         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2134         MsgInfo *msginfo;
2135                 GtkCTreeNode *node = NULL;
2136         GHashTable *msgid_table;
2137         GHashTable *subject_table;
2138         GSList * cur;
2139         
2140         if (!mlist) return;
2141
2142         debug_print("\tSetting summary from message data...");
2143         STATUSBAR_PUSH(summaryview->mainwin,
2144                        _("Setting summary from message data..."));
2145         gdk_flush();
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
2224 static gchar *summary_complete_address(const gchar *addr)
2225 {
2226         gint count;
2227         gchar *res, *tmp, *email_addr;
2228
2229         Xstrdup_a(email_addr, addr, return NULL);
2230         extract_address(email_addr);
2231         g_return_val_if_fail(*email_addr, NULL);
2232
2233         /*
2234          * completion stuff must be already initialized
2235          */
2236         res = NULL;
2237         if (1 < (count = complete_address(email_addr))) {
2238                 tmp = get_complete_address(1);
2239 /*      tmp = addressbook_lookup_name( email_addr );
2240         if( tmp ) { */
2241                 res = procheader_get_fromname(tmp);
2242                 g_free(tmp);
2243         }
2244
2245         return res;
2246 }
2247
2248 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2249                                MsgInfo *msginfo)
2250 {
2251         static gchar date_modified[80];
2252         static gchar *to = NULL;
2253         static gchar col_score[11];
2254         static gchar buf[BUFFSIZE];
2255         gint *col_pos = summaryview->col_pos;
2256         FolderType ftype = F_UNKNOWN;
2257
2258         text[col_pos[S_COL_MARK]]   = NULL;
2259         text[col_pos[S_COL_STATUS]] = NULL;
2260         text[col_pos[S_COL_MIME]]   = NULL;
2261         text[col_pos[S_COL_LOCKED]] = NULL;
2262         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2263         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2264         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2265
2266         if (msginfo->date_t) {
2267                 procheader_date_get_localtime(date_modified,
2268                                               sizeof(date_modified),
2269                                               msginfo->date_t);
2270                 text[col_pos[S_COL_DATE]] = date_modified;
2271         } else if (msginfo->date)
2272                 text[col_pos[S_COL_DATE]] = msginfo->date;
2273         else
2274                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2275
2276         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2277                 _("(No From)");
2278         
2279         if (msginfo->folder && msginfo->folder->folder)
2280                 ftype = msginfo->folder->folder->klass->type; 
2281                 
2282         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2283                 gchar *addr = NULL;
2284
2285                 Xstrdup_a(addr, msginfo->from, return);
2286                 extract_address(addr);
2287                 if (prefs_common.use_addr_book) {
2288                         if (account_find_from_address(addr)) {
2289                                 addr = summary_complete_address(msginfo->to);
2290                                 g_free(to);
2291                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2292                                 text[col_pos[S_COL_FROM]] = to;
2293                                 g_free(addr);
2294                         }
2295                 } else {
2296                         if (account_find_from_address(addr)) {
2297                                 g_free(to);
2298                                 to = g_strconcat("-->", msginfo->to, NULL);
2299                                 text[col_pos[S_COL_FROM]] = to;
2300                         }
2301                 }
2302         }
2303
2304         /*
2305          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2306          * checking whether the above block (which handles the special case of
2307          * the --> in sent boxes) was executed.
2308          */
2309         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2310                 gchar *from = summary_complete_address(msginfo->from);
2311                 if (from) {
2312                         g_free(to);
2313                         to = from;
2314                         text[col_pos[S_COL_FROM]] = to;
2315                 }                       
2316         }
2317
2318         if (summaryview->simplify_subject_preg != NULL)
2319                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2320                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2321                                         summaryview->simplify_subject_preg) : 
2322                         _("(No Subject)");
2323         else 
2324                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2325                         _("(No Subject)");
2326 }
2327
2328 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2329 {
2330         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2331 }
2332
2333 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2334                                       GtkCTreeNode *row)
2335 {
2336         g_return_if_fail(summaryview != NULL);
2337         g_return_if_fail(msginfo != NULL);
2338         g_return_if_fail(row != NULL);
2339
2340         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2341         && procmsg_msg_has_marked_parent(msginfo))
2342                 summaryview->unreadmarked--;
2343         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2344                 procmsg_msginfo_unset_flags
2345                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2346                 summary_set_row_marks(summaryview, row);
2347                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2348                 summary_status_show(summaryview);
2349         }
2350 }
2351
2352 typedef struct  {
2353         MsgInfo *msginfo;
2354         SummaryView *summaryview;
2355         GtkCTreeNode *row;
2356 } MarkAsReadData;
2357
2358 static int msginfo_mark_as_read_timeout(void *data)
2359 {
2360         MarkAsReadData *mdata = (MarkAsReadData *)data;
2361         if (!mdata)
2362                 return FALSE;
2363         
2364         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2365                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2366                                      mdata->row); 
2367
2368         g_free(mdata);
2369
2370         return FALSE;   
2371 }
2372
2373 static void summary_display_msg_full(SummaryView *summaryview,
2374                                      GtkCTreeNode *row,
2375                                      gboolean new_window, gboolean all_headers)
2376 {
2377         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2378         MsgInfo *msginfo;
2379         gint val;
2380
2381         if (!new_window) {
2382                 if (summaryview->displayed == row)
2383                         return;
2384                 else
2385                         summaryview->messageview->filtered = FALSE;
2386         }                       
2387         
2388         g_return_if_fail(row != NULL);
2389
2390         if (summary_is_locked(summaryview)) return;
2391         summary_lock(summaryview);
2392
2393         STATUSBAR_POP(summaryview->mainwin);
2394         GTK_EVENTS_FLUSH();
2395
2396         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2397
2398         if (new_window) {
2399                 MessageView *msgview;
2400
2401                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2402                 val = messageview_show(msgview, msginfo, all_headers);
2403         } else {
2404                 MessageView *msgview;
2405
2406                 msgview = summaryview->messageview;
2407
2408                 summaryview->displayed = row;
2409                 if (!messageview_is_visible(msgview))
2410                         main_window_toggle_message_view(summaryview->mainwin);
2411                 val = messageview_show(msgview, msginfo, all_headers);
2412                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2413                         gtk_widget_grab_focus(summaryview->ctree);
2414                 GTK_EVENTS_FLUSH();
2415                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2416         }
2417
2418         if (val == 0) {
2419                 if (prefs_common.mark_as_read_delay) {
2420                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2421                         data->summaryview = summaryview;
2422                         data->msginfo = msginfo;
2423                         data->row = row;
2424                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2425                                 msginfo_mark_as_read_timeout, data);
2426                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2427                         msginfo_mark_as_read(summaryview, msginfo, row);
2428                 }
2429         }
2430
2431         summary_set_menu_sensitive(summaryview);
2432         toolbar_main_set_sensitive(summaryview->mainwin);
2433         messageview_set_menu_sensitive(summaryview->messageview);
2434
2435         summary_unlock(summaryview);
2436 }
2437
2438 void summary_display_msg_selected(SummaryView *summaryview,
2439                                   gboolean all_headers)
2440 {
2441         if (summary_is_locked(summaryview)) return;
2442         summaryview->displayed = NULL;
2443         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2444                                  all_headers);
2445 }
2446
2447 void summary_redisplay_msg(SummaryView *summaryview)
2448 {
2449         GtkCTreeNode *node;
2450
2451         if (summaryview->displayed) {
2452                 node = summaryview->displayed;
2453                 summaryview->displayed = NULL;
2454                 summary_display_msg(summaryview, node);
2455         }
2456 }
2457
2458 void summary_open_msg(SummaryView *summaryview)
2459 {
2460         if (!summaryview->selected) return;
2461         
2462         /* CLAWS: if separate message view, don't open a new window
2463          * but rather use the current separated message view */
2464         summary_display_msg_full(summaryview, summaryview->selected,
2465                                  prefs_common.sep_msg ? FALSE : TRUE, 
2466                                  FALSE);
2467 }
2468
2469 void summary_view_source(SummaryView * summaryview)
2470 {
2471         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2472         MsgInfo *msginfo;
2473         SourceWindow *srcwin;
2474
2475         if (!summaryview->selected) return;
2476
2477         srcwin = source_window_create();
2478         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2479         source_window_show_msg(srcwin, msginfo);
2480         source_window_show(srcwin);
2481 }
2482
2483 void summary_reedit(SummaryView *summaryview)
2484 {
2485         MsgInfo *msginfo;
2486
2487         if (!summaryview->selected) return;
2488         if (!summaryview->folder_item) return;
2489         if (summaryview->folder_item->stype != F_OUTBOX &&
2490             summaryview->folder_item->stype != F_DRAFT  &&
2491             summaryview->folder_item->stype != F_QUEUE) return;
2492
2493         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2494                                               summaryview->selected);
2495         if (!msginfo) return;
2496
2497         compose_reedit(msginfo);
2498 }
2499
2500 void summary_step(SummaryView *summaryview, GtkScrollType type)
2501 {
2502         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2503         GtkCTreeNode *node;
2504
2505         if (summary_is_locked(summaryview)) return;
2506
2507         if (type == GTK_SCROLL_STEP_FORWARD) {
2508                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2509                 if (node)
2510                         gtkut_ctree_expand_parent_all(ctree, node);
2511                 else
2512                         return;
2513         } else {
2514                 if (summaryview->selected) {
2515                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2516                         if (!node) return;
2517                 }
2518         }
2519
2520         if (messageview_is_visible(summaryview->messageview))
2521                 summaryview->display_msg = TRUE;
2522
2523         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2524                                 type, 0.0);
2525
2526         if (GTK_CLIST(ctree)->selection)
2527                 gtk_sctree_set_anchor_row
2528                         (GTK_SCTREE(ctree),
2529                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2530
2531 }
2532
2533 void summary_toggle_view(SummaryView *summaryview)
2534 {
2535         if (!messageview_is_visible(summaryview->messageview) &&
2536             summaryview->selected)
2537                 summary_display_msg(summaryview,
2538                                     summaryview->selected);
2539         else
2540                 main_window_toggle_message_view(summaryview->mainwin);
2541 }
2542
2543 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2544                                                 GtkCTreeNode *node)
2545 {
2546         MsgInfo *msginfo;
2547
2548         if (node) {
2549                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2550                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2551                         return TRUE;
2552                 node = GTK_CTREE_ROW(node)->children;
2553         } else
2554                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2555
2556         while (node) {
2557                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2558                         return TRUE;
2559                 node = GTK_CTREE_ROW(node)->sibling;
2560         }
2561
2562         return FALSE;
2563 }
2564
2565 static gboolean summary_have_unread_children(SummaryView *summaryview,
2566                                              GtkCTreeNode *node)
2567 {
2568         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2569
2570         if (!node) return FALSE;
2571
2572         node = GTK_CTREE_ROW(node)->children;
2573
2574         while (node) {
2575                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2576                         return TRUE;
2577                 node = GTK_CTREE_ROW(node)->sibling;
2578         }
2579
2580         return FALSE;
2581 }
2582
2583 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2584 {
2585         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2586         GtkStyle *style = NULL;
2587         MsgInfo *msginfo;
2588         MsgFlags flags;
2589         gint *col_pos = summaryview->col_pos;
2590
2591         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2592         if (!msginfo) return;
2593
2594         flags = msginfo->flags;
2595
2596         gtk_ctree_node_set_foreground(ctree, row, NULL);
2597
2598         /* set new/unread column */
2599         if (MSG_IS_IGNORE_THREAD(flags)) {
2600                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2601                                           ignorethreadxpm, ignorethreadxpmmask);
2602         } else if (MSG_IS_NEW(flags)) {
2603                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2604                                           newxpm, newxpmmask);
2605         } else if (MSG_IS_UNREAD(flags)) {
2606                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2607                                           unreadxpm, unreadxpmmask);
2608         } else if (MSG_IS_REPLIED(flags)) {
2609                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2610                                           repliedxpm, repliedxpmmask);
2611         } else if (MSG_IS_FORWARDED(flags)) {
2612                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2613                                           forwardedxpm, forwardedxpmmask);
2614         } else {
2615                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2616                                         NULL);
2617         }
2618
2619         if (prefs_common.bold_unread &&
2620             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2621              (!GTK_CTREE_ROW(row)->expanded &&
2622               GTK_CTREE_ROW(row)->children &&
2623               summary_have_unread_children(summaryview, row))))
2624                 style = bold_style;
2625
2626         /* set mark column */
2627         if (MSG_IS_DELETED(flags)) {
2628                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2629                                           deletedxpm, deletedxpmmask);
2630                 if (style)
2631                         style = bold_deleted_style;
2632                 else {
2633                         style = small_deleted_style;
2634                 }
2635                         gtk_ctree_node_set_foreground
2636                                 (ctree, row, &summaryview->color_dim);
2637         } else if (MSG_IS_MARKED(flags)) {
2638                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2639                                           markxpm, markxpmmask);
2640         } else if (MSG_IS_MOVE(flags)) {
2641                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2642                 if (style)
2643                         style = bold_marked_style;
2644                 else {
2645                         style = small_marked_style;
2646                 }
2647                         gtk_ctree_node_set_foreground
2648                                 (ctree, row, &summaryview->color_marked);
2649         } else if (MSG_IS_COPY(flags)) {
2650                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2651                 if (style)
2652                         style = bold_marked_style;
2653                 else {
2654                         style = small_marked_style;
2655                 }
2656                         gtk_ctree_node_set_foreground
2657                                 (ctree, row, &summaryview->color_marked);
2658 #if 0
2659         } else if ((global_scoring ||
2660                   summaryview->folder_item->prefs->scoring) &&
2661                  (msginfo->score >= summaryview->important_score) &&
2662                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2663                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2664                 gtk_ctree_node_set_foreground(ctree, row,
2665                                               &summaryview->color_important);
2666 #endif
2667         } else {
2668                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2669         }
2670
2671         if (MSG_IS_LOCKED(flags)) {
2672                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2673                                           lockedxpm, lockedxpmmask);
2674         }
2675         else {
2676                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2677         }
2678
2679         if (MSG_IS_SIGNED(flags)) {
2680                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2681                                           gpgsignedxpm, gpgsignedxpmmask);
2682         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2683                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2684                                           clipkeyxpm, clipkeyxpmmask);
2685         } else if (MSG_IS_ENCRYPTED(flags)) {
2686                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2687                                           keyxpm, keyxpmmask);
2688         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2689                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2690                                           clipxpm, clipxpmmask);
2691         } else {
2692                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2693         }
2694         if (!style)
2695                 style = small_style;
2696
2697         gtk_ctree_node_set_row_style(ctree, row, style);
2698
2699         if (MSG_GET_COLORLABEL(flags))
2700                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2701 }
2702
2703 void summary_set_marks_selected(SummaryView *summaryview)
2704 {
2705         GList *cur;
2706
2707         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2708              cur = cur->next)
2709                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2710 }
2711
2712 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2713 {
2714         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2715         MsgInfo *msginfo;
2716
2717         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2718         if (MSG_IS_DELETED(msginfo->flags))
2719                 summaryview->deleted--;
2720         if (MSG_IS_MOVE(msginfo->flags))
2721                 summaryview->moved--;
2722         if (MSG_IS_COPY(msginfo->flags))
2723                 summaryview->copied--;
2724         procmsg_update_unread_children (msginfo, TRUE);
2725
2726         procmsg_msginfo_set_to_folder(msginfo, NULL);
2727         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2728         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2729         summary_set_row_marks(summaryview, row);
2730         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2731 }
2732
2733 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2734 {
2735         gboolean changed = FALSE;
2736         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2737         MsgInfo *msginfo;
2738
2739         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2740         if (MSG_IS_DELETED(msginfo->flags))
2741                 summaryview->deleted--;
2742         if (MSG_IS_MOVE(msginfo->flags)) {
2743                 summaryview->moved--;
2744                 changed = TRUE;
2745         }
2746         if (MSG_IS_COPY(msginfo->flags)) {
2747                 summaryview->copied--;
2748                 changed = TRUE;
2749         }
2750         procmsg_msginfo_set_to_folder(msginfo, NULL);
2751         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2752         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2753         summary_set_row_marks(summaryview, row);
2754         debug_print("Message %d is locked\n", msginfo->msgnum);
2755 }
2756
2757 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2758 {
2759         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2760         MsgInfo *msginfo;
2761
2762         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2763         if (!MSG_IS_LOCKED(msginfo->flags))
2764                 return;
2765         procmsg_msginfo_set_to_folder(msginfo, NULL);
2766         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2767         summary_set_row_marks(summaryview, row);
2768         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2769 }
2770
2771 void summary_mark(SummaryView *summaryview)
2772 {
2773         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2774         GList *cur;
2775
2776         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2777                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2778
2779         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2780         summary_status_show(summaryview);
2781 }
2782
2783 static void summary_mark_row_as_read(SummaryView *summaryview,
2784                                      GtkCTreeNode *row)
2785 {
2786         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2787         MsgInfo *msginfo;
2788
2789         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2790
2791         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2792                 return;
2793
2794         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2795         && procmsg_msg_has_marked_parent(msginfo))
2796                 summaryview->unreadmarked--;
2797
2798         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2799         summary_set_row_marks(summaryview, row);
2800         debug_print("Message %d is marked as read\n",
2801                 msginfo->msgnum);
2802 }
2803
2804 void summary_mark_as_read(SummaryView *summaryview)
2805 {
2806         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2807         GList *cur;
2808
2809         folder_item_update_freeze();
2810         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2811                 summary_mark_row_as_read(summaryview,
2812                                          GTK_CTREE_NODE(cur->data));
2813         folder_item_update_thaw();
2814         
2815         summary_status_show(summaryview);
2816 }
2817
2818 void summary_msgs_lock(SummaryView *summaryview)
2819 {
2820         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2821         GList *cur;
2822
2823         folder_item_update_freeze();
2824         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2825                 summary_lock_row(summaryview,
2826                                          GTK_CTREE_NODE(cur->data));
2827         folder_item_update_thaw();
2828         
2829         summary_status_show(summaryview);
2830 }
2831
2832 void summary_msgs_unlock(SummaryView *summaryview)
2833 {
2834         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2835         GList *cur;
2836
2837         folder_item_update_freeze();
2838         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2839                 summary_unlock_row(summaryview,
2840                                    GTK_CTREE_NODE(cur->data));
2841         folder_item_update_thaw();
2842         
2843         summary_status_show(summaryview);
2844 }
2845
2846 void summary_mark_all_read(SummaryView *summaryview)
2847 {
2848         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2849         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2850         GtkCTreeNode *node;
2851
2852         gtk_clist_freeze(clist);
2853         folder_item_update_freeze();
2854         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2855              node = gtkut_ctree_node_next(ctree, node))
2856                 summary_mark_row_as_read(summaryview, node);
2857         folder_item_update_thaw();
2858         gtk_clist_thaw(clist);
2859
2860         summary_status_show(summaryview);
2861 }
2862
2863 static void summary_mark_row_as_unread(SummaryView *summaryview,
2864                                        GtkCTreeNode *row)
2865 {
2866         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2867         MsgInfo *msginfo;
2868
2869         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2870         if (MSG_IS_DELETED(msginfo->flags)) {
2871                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2872                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2873                 summaryview->deleted--;
2874         }
2875
2876         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2877         && procmsg_msg_has_marked_parent(msginfo))
2878                 summaryview->unreadmarked++;
2879
2880         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2881         debug_print("Message %d is marked as unread\n",
2882                 msginfo->msgnum);
2883
2884         summary_set_row_marks(summaryview, row);
2885 }
2886
2887 void summary_mark_as_unread(SummaryView *summaryview)
2888 {
2889         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2890         GList *cur;
2891
2892         folder_item_update_freeze();
2893         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2894                 summary_mark_row_as_unread(summaryview,
2895                                            GTK_CTREE_NODE(cur->data));
2896         folder_item_update_thaw();
2897         
2898         summary_status_show(summaryview);
2899 }
2900
2901 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2902 {
2903         GList * cur;
2904         gboolean found;
2905
2906         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2907
2908         case F_NEWS:
2909
2910                 /*
2911                   security : checks if one the accounts correspond to
2912                   the author of the post
2913                 */
2914
2915                 found = FALSE;
2916                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2917                         PrefsAccount * account;
2918                         gchar * from_name;
2919                         
2920                         account = cur->data;
2921                         if (account->name && *account->name)
2922                                 from_name =
2923                                         g_strdup_printf("%s <%s>",
2924                                                         account->name,
2925                                                         account->address);
2926                         else
2927                                 from_name =
2928                                         g_strdup_printf("%s",
2929                                                         account->address);
2930                         
2931                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2932                                 g_free(from_name);
2933                                 found = TRUE;
2934                                 break;
2935                         }
2936                         g_free(from_name);
2937                 }
2938
2939                 if (!found) {
2940                         alertpanel_error(_("You're not the author of the article\n"));
2941                 }
2942                 
2943                 return found;
2944
2945         default:
2946                 return TRUE;
2947         }
2948 }
2949
2950 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2951 {
2952         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2953         MsgInfo *msginfo;
2954
2955         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2956
2957         if (!check_permission(summaryview, msginfo))
2958                 return;
2959
2960         if (MSG_IS_LOCKED(msginfo->flags)) return;
2961
2962         if (MSG_IS_DELETED(msginfo->flags)) return;
2963
2964         if (MSG_IS_MOVE(msginfo->flags))
2965                 summaryview->moved--;
2966         if (MSG_IS_COPY(msginfo->flags))
2967                 summaryview->copied--;
2968         procmsg_update_unread_children (msginfo, FALSE);
2969
2970         procmsg_msginfo_set_to_folder(msginfo, NULL);
2971         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2972         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2973         summaryview->deleted++;
2974
2975         if (!prefs_common.immediate_exec && 
2976             summaryview->folder_item->stype != F_TRASH)
2977                 summary_set_row_marks(summaryview, row);
2978
2979         debug_print("Message %s/%d is set to delete\n",
2980                     msginfo->folder->path, msginfo->msgnum);
2981 }
2982
2983 void summary_cancel(SummaryView *summaryview)
2984 {
2985         MsgInfo * msginfo;
2986         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2987
2988         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2989                                               summaryview->selected);
2990         if (!msginfo) return;
2991
2992         if (!check_permission(summaryview, msginfo))
2993                 return;
2994
2995         news_cancel_article(summaryview->folder_item->folder, msginfo);
2996         
2997         if (summary_is_locked(summaryview)) return;
2998
2999         summary_lock(summaryview);
3000
3001         gtk_clist_freeze(clist);
3002
3003         summary_update_status(summaryview);
3004         summary_status_show(summaryview);
3005
3006         gtk_clist_thaw(clist);
3007
3008         summary_unlock(summaryview);
3009 }
3010
3011 void summary_delete(SummaryView *summaryview)
3012 {
3013         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3014         FolderItem *item = summaryview->folder_item;
3015         GList *cur;
3016         GtkCTreeNode *sel_last = NULL;
3017         GtkCTreeNode *node;
3018
3019         if (!item) return;
3020
3021         if (summary_is_locked(summaryview)) return;
3022
3023         /* if current folder is trash, ask for confirmation */
3024         if (item->stype == F_TRASH) {
3025                 AlertValue aval;
3026                 MsgInfo *msginfo;
3027
3028                 aval = alertpanel(_("Delete message(s)"),
3029                                   _("Do you really want to delete message(s) from the trash?"),
3030                                   _("Yes"), _("No"), NULL);
3031                 if (aval != G_ALERTDEFAULT) return;
3032
3033                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3034                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3035                         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3036                         partial_mark_for_delete(msginfo);
3037                 }
3038         
3039         }
3040
3041         main_window_cursor_wait(summaryview->mainwin);
3042
3043         /* next code sets current row focus right. We need to find a row
3044          * that is not deleted. */
3045         folder_item_update_freeze();     
3046         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3047                 sel_last = GTK_CTREE_NODE(cur->data);
3048                 summary_delete_row(summaryview, sel_last);
3049         }
3050         folder_item_update_thaw();
3051
3052         node = summary_find_next_msg(summaryview, sel_last);
3053         if (!node)
3054                 node = summary_find_prev_msg(summaryview, sel_last);
3055
3056         summary_select_node(summaryview, node, FALSE, FALSE);
3057         
3058         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3059                 summary_execute(summaryview);
3060                 /* after deleting, the anchor may be at an invalid row
3061                  * so reset it to the node we found earlier */
3062                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3063         } else
3064                 summary_status_show(summaryview);
3065                 
3066         main_window_cursor_normal(summaryview->mainwin);
3067 }
3068
3069 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3070 {
3071         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3072         MsgInfo *msginfo;
3073
3074         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3075         if (MSG_IS_DELETED(msginfo->flags))
3076                 summaryview->deleted--;
3077         if (MSG_IS_MOVE(msginfo->flags))
3078                 summaryview->moved--;
3079         if (MSG_IS_COPY(msginfo->flags))
3080                 summaryview->copied--;
3081         procmsg_update_unread_children (msginfo, FALSE);
3082
3083         procmsg_msginfo_set_to_folder(msginfo, NULL);
3084         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3085         summary_set_row_marks(summaryview, row);
3086
3087         debug_print("Message %s/%d is unmarked\n",
3088                     msginfo->folder->path, msginfo->msgnum);
3089 }
3090
3091 void summary_unmark(SummaryView *summaryview)
3092 {
3093         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3094         GList *cur;
3095
3096         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3097                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3098
3099         summary_status_show(summaryview);
3100 }
3101
3102 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3103                                 FolderItem *to_folder)
3104 {
3105         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3106         MsgInfo *msginfo;
3107
3108         g_return_if_fail(to_folder != NULL);
3109
3110         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3111         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3112         if (MSG_IS_DELETED(msginfo->flags))
3113                 summaryview->deleted--;
3114         if (MSG_IS_COPY(msginfo->flags)) {
3115                 summaryview->copied--;
3116         }
3117         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3118         if (!MSG_IS_MOVE(msginfo->flags)) {
3119                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3120                 summaryview->moved++;
3121         }
3122         if (!prefs_common.immediate_exec) {
3123                 summary_set_row_marks(summaryview, row);
3124         }
3125
3126         debug_print("Message %d is set to move to %s\n",
3127                     msginfo->msgnum, to_folder->path);
3128 }
3129
3130 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3131 {
3132         GList *cur;
3133
3134         if (!to_folder) return;
3135         if (!summaryview->folder_item ||
3136             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3137
3138         if (summary_is_locked(summaryview)) return;
3139
3140         if (summaryview->folder_item == to_folder) {
3141                 alertpanel_error(_("Destination is same as current folder."));
3142                 return;
3143         }
3144
3145         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3146              cur != NULL; cur = cur->next)
3147                 summary_move_row_to
3148                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3149
3150         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3151
3152         if (prefs_common.immediate_exec)
3153                 summary_execute(summaryview);
3154         else {
3155                 summary_status_show(summaryview);
3156         }
3157         
3158         if (!summaryview->selected) { /* this was the last message */
3159                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3160                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3161                 if (node)
3162                         summary_select_node(summaryview, node, TRUE, TRUE);
3163         }
3164
3165 }
3166
3167 void summary_move_to(SummaryView *summaryview)
3168 {
3169         FolderItem *to_folder;
3170
3171         if (!summaryview->folder_item ||
3172             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3173
3174         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3175                                          FOLDER_SEL_MOVE, NULL);
3176         summary_move_selected_to(summaryview, to_folder);
3177 }
3178
3179 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3180                                 FolderItem *to_folder)
3181 {
3182         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3183         MsgInfo *msginfo;
3184
3185         g_return_if_fail(to_folder != NULL);
3186
3187         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3188         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3189         if (MSG_IS_DELETED(msginfo->flags))
3190                 summaryview->deleted--;
3191         if (MSG_IS_MOVE(msginfo->flags)) {
3192                 summaryview->moved--;
3193         }
3194         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3195         if (!MSG_IS_COPY(msginfo->flags)) {
3196                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3197                 summaryview->copied++;
3198         }
3199         if (!prefs_common.immediate_exec) {
3200                 summary_set_row_marks(summaryview, row);
3201         }
3202
3203         debug_print("Message %d is set to copy to %s\n",
3204                     msginfo->msgnum, to_folder->path);
3205 }
3206
3207 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3208 {
3209         GList *cur;
3210
3211         if (!to_folder) return;
3212         if (!summaryview->folder_item) return;
3213
3214         if (summary_is_locked(summaryview)) return;
3215
3216         if (summaryview->folder_item == to_folder) {
3217                 alertpanel_error
3218                         (_("Destination to copy is same as current folder."));
3219                 return;
3220         }
3221
3222         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3223              cur != NULL; cur = cur->next)
3224                 summary_copy_row_to
3225                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3226
3227         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3228
3229         if (prefs_common.immediate_exec)
3230                 summary_execute(summaryview);
3231         else {
3232                 summary_status_show(summaryview);
3233         }
3234 }
3235
3236 void summary_copy_to(SummaryView *summaryview)
3237 {
3238         FolderItem *to_folder;
3239
3240         if (!summaryview->folder_item) return;
3241
3242         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3243                                          FOLDER_SEL_COPY, NULL);
3244         summary_copy_selected_to(summaryview, to_folder);
3245 }
3246
3247 void summary_add_address(SummaryView *summaryview)
3248 {
3249         MsgInfo *msginfo;
3250         gchar *from;
3251
3252         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3253                                               summaryview->selected);
3254         if (!msginfo) return;
3255
3256         Xstrdup_a(from, msginfo->from, return);
3257         eliminate_address_comment(from);
3258         extract_address(from);
3259         addressbook_add_contact(msginfo->fromname, from, NULL);
3260 }
3261
3262 void summary_select_all(SummaryView *summaryview)
3263 {
3264         if (!summaryview->folder_item) return;
3265
3266         if (summaryview->folder_item->total_msgs >= 500) {
3267                 STATUSBAR_PUSH(summaryview->mainwin,
3268                                _("Selecting all messages..."));
3269                 main_window_cursor_wait(summaryview->mainwin);
3270         }
3271
3272         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3273
3274         if (summaryview->folder_item->total_msgs >= 500) {
3275                 STATUSBAR_POP(summaryview->mainwin);
3276                 main_window_cursor_normal(summaryview->mainwin);
3277         }
3278 }
3279
3280 void summary_unselect_all(SummaryView *summaryview)
3281 {
3282         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3283 }
3284
3285 void summary_select_thread(SummaryView *summaryview)
3286 {
3287         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3288         GtkCTreeNode *node = summaryview->selected;
3289
3290         if (!node) return;
3291
3292         while (GTK_CTREE_ROW(node)->parent != NULL)
3293                 node = GTK_CTREE_ROW(node)->parent;
3294
3295         if (node != summaryview->selected)
3296                 summary_select_node
3297                         (summaryview, node,
3298                          messageview_is_visible(summaryview->messageview),
3299                          FALSE);
3300
3301         gtk_ctree_select_recursive(ctree, node);
3302
3303         summary_status_show(summaryview);
3304 }
3305
3306 void summary_save_as(SummaryView *summaryview)
3307 {
3308         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3309         MsgInfo *msginfo;
3310         gchar *filename = NULL;
3311         gchar *src, *dest;
3312
3313         AlertValue aval = 0;
3314
3315         if (!summaryview->selected) return;
3316         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3317         if (!msginfo) return;
3318<