2005-01-27 [colin] 1.0.0cvs16
[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
786         if (summary_is_locked(summaryview)) return FALSE;
787
788         inc_lock();
789         summary_lock(summaryview);
790
791         if (!prefs_common.summary_quicksearch_sticky
792          && !prefs_common.summary_quicksearch_recurse
793          && !quicksearch_is_running(summaryview->quicksearch)) {
794                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
795         }
796
797         /* STATUSBAR_POP(summaryview->mainwin); */
798
799         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
800         if (is_refresh) {
801                 selected_msgnum = summary_get_msgnum(summaryview,
802                                                      summaryview->selected);
803                 displayed_msgnum = summary_get_msgnum(summaryview,
804                                                       summaryview->displayed);
805         }
806
807         /* process the marks if any */
808         if (summaryview->mainwin->lock_count == 0 &&
809             (summaryview->moved > 0 || summaryview->copied > 0)) {
810                 AlertValue val;
811                 gboolean changed = FALSE;
812
813                 val = alertpanel(_("Process mark"),
814                                  _("Some marks are left. Process it?"),
815                                  _("Yes"), _("No"), _("Cancel"));
816                 if (G_ALERTDEFAULT == val) {
817                         summary_unlock(summaryview);
818                         summary_execute(summaryview);
819                         summary_lock(summaryview);
820                         changed = TRUE;
821                 } else if (G_ALERTALTERNATE == val) {
822                         /* DO NOTHING */
823                 } else {
824                         summary_unlock(summaryview);
825                         inc_unlock();
826                         return FALSE;
827                 }
828                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
829                         folder_update_op_count();
830         }
831         
832         gtk_clist_freeze(GTK_CLIST(ctree));
833
834         summary_clear_list(summaryview);
835         summary_set_column_titles(summaryview);
836
837         buf = NULL;
838         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
839                 g_free(buf);
840                 debug_print("empty folder\n\n");
841                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
842                 summary_clear_all(summaryview);
843                 summaryview->folder_item = item;
844                 gtk_clist_thaw(GTK_CLIST(ctree));
845                 summary_unlock(summaryview);
846                 inc_unlock();
847                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
848                         main_window_cursor_wait(summaryview->mainwin);
849                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
850                         if (quicksearch_is_active(summaryview->quicksearch))
851                                 quicksearch_search_subfolders(summaryview->quicksearch, 
852                                               summaryview->folderview,
853                                               summaryview->folder_item);
854                         main_window_cursor_normal(summaryview->mainwin);
855                 }                       
856                 return TRUE;
857         }
858         g_free(buf);
859
860         if (!is_refresh)
861                 messageview_clear(summaryview->messageview);
862
863         summaryview->folder_item = item;
864         item->opened = TRUE;
865
866         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
867
868         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
869         debug_print("%s\n", buf);
870         STATUSBAR_PUSH(summaryview->mainwin, buf);
871         g_free(buf);
872
873         main_window_cursor_wait(summaryview->mainwin);
874
875 /*
876         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
877
878         !!! NEW !!!
879         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
880         BY OTHER PROGRAMS TO THE FOLDER
881 */
882         mlist = folder_item_get_msg_list(item);
883
884         if (summaryview->folder_item->hide_read_msgs) {
885                 GSList *not_killed;
886                 
887                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
888                 not_killed = NULL;
889                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
890                         MsgInfo * msginfo = (MsgInfo *) cur->data;
891                         
892                         if ((MSG_IS_UNREAD(msginfo->flags)
893                              || MSG_IS_MARKED(msginfo->flags)
894                              || MSG_IS_LOCKED(msginfo->flags)
895                              || CURRENTLY_DISPLAYED(msginfo))
896                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
897                                 not_killed = g_slist_prepend(not_killed, msginfo);
898                         else
899                                 procmsg_msginfo_free(msginfo);
900                 }
901                 g_slist_free(mlist);
902                 mlist = not_killed;
903         } else {
904                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
905         }
906
907         if (quicksearch_is_active(summaryview->quicksearch)) {
908                 GSList *not_killed;
909                 
910                 not_killed = NULL;
911                 for (cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
912                         MsgInfo * msginfo = (MsgInfo *) cur->data;
913
914                         if (quicksearch_match(summaryview->quicksearch, msginfo))
915                                 not_killed = g_slist_prepend(not_killed, msginfo);
916                         else
917                                 procmsg_msginfo_free(msginfo);
918                 }
919                 
920                 if (quicksearch_is_running(summaryview->quicksearch)) {
921                         /* only scan subfolders when quicksearch changed,
922                          * not when search is the same and folder changed */
923                         main_window_cursor_wait(summaryview->mainwin);
924                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
925                         quicksearch_search_subfolders(summaryview->quicksearch, 
926                                               summaryview->folderview,
927                                               summaryview->folder_item);
928                         main_window_cursor_normal(summaryview->mainwin);
929                 }
930                 
931                 g_slist_free(mlist);
932                 mlist = not_killed;
933         }
934
935 #if 0   
936         if ((global_scoring || item->prefs->scoring)) {
937                 GSList *not_killed;
938                 gint kill_score;
939
940                 not_killed = NULL;
941                 kill_score = prefs_common.kill_score;
942                 if (item->prefs->kill_score > kill_score)
943                         kill_score = item->prefs->kill_score;
944                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
945                         MsgInfo * msginfo = (MsgInfo *) cur->data;
946
947                         if (msginfo->score > kill_score)
948                                 not_killed = g_slist_prepend(not_killed, msginfo);
949                         else
950                                 procmsg_msginfo_free(msginfo);
951                 }
952                 g_slist_free(mlist);
953                 mlist = not_killed;
954         }
955 #endif
956         not_killed = NULL;
957         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
958                 MsgInfo * msginfo = (MsgInfo *) cur->data;
959                 
960                 if (!msginfo->hidden)
961                         not_killed = g_slist_prepend(not_killed, msginfo);
962                 else
963                         procmsg_msginfo_free(msginfo);
964         }
965         g_slist_free(mlist);
966         mlist = not_killed;
967
968         STATUSBAR_POP(summaryview->mainwin);
969
970         /* set ctree and hash table from the msginfo list, and
971            create the thread */
972         summary_set_ctree_from_list(summaryview, mlist);
973
974         g_slist_free(mlist);
975
976         if (summaryview->sort_key != SORT_BY_NONE)
977                 summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
978
979         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
980
981         gtk_clist_thaw(GTK_CLIST(ctree));
982
983         if (is_refresh) {
984                 summaryview->displayed =
985                         summary_find_msg_by_msgnum(summaryview,
986                                                    displayed_msgnum);
987                 if (!summaryview->displayed)
988                         messageview_clear(summaryview->messageview);
989                 summary_select_by_msgnum(summaryview, selected_msgnum);
990                 if (!summaryview->selected) {
991                         /* no selected message - select first unread
992                            message, but do not display it */
993                         node = summary_find_next_flagged_msg(summaryview, NULL,
994                                                              MSG_UNREAD, FALSE);
995                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
996                                 node = gtk_ctree_node_nth
997                                         (ctree,
998                                          item->sort_type == SORT_DESCENDING
999                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
1000                         summary_select_node(summaryview, node, FALSE, TRUE);
1001                 }
1002         } else {
1003                 /* select first unread message */
1004 #if 0
1005                 if (summaryview->sort_key == SORT_BY_SCORE)
1006                         node = summary_find_next_important_score(summaryview,
1007                                                                  NULL);
1008                 else
1009 #endif
1010                 node = summary_find_next_flagged_msg(summaryview, NULL,
1011                                                      MSG_UNREAD, FALSE);
1012                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1013                         node = gtk_ctree_node_nth
1014                                 (ctree,
1015                                  item->sort_type == SORT_DESCENDING
1016                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1017                 }
1018                 if (prefs_common.open_unread_on_enter ||
1019                     prefs_common.always_show_msg) {
1020                         summary_unlock(summaryview);
1021                         summary_select_node(summaryview, node, 
1022                                             messageview_is_visible(summaryview->messageview), 
1023                                             TRUE);
1024                         summary_lock(summaryview);
1025                 } else
1026                         summary_select_node(summaryview, node, FALSE, TRUE);
1027         }
1028
1029         summary_set_column_titles(summaryview);
1030         summary_status_show(summaryview);
1031         summary_set_menu_sensitive(summaryview);
1032         toolbar_main_set_sensitive(summaryview->mainwin);
1033
1034         debug_print("\n");
1035         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1036         STATUSBAR_POP(summaryview->mainwin);
1037         main_window_cursor_normal(summaryview->mainwin);
1038         summary_unlock(summaryview);
1039         inc_unlock();
1040
1041         return TRUE;
1042 }
1043
1044 #undef CURRENTLY_DISPLAYED
1045
1046 void summary_clear_list(SummaryView *summaryview)
1047 {
1048         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1049         gint optimal_width;
1050
1051         gtk_clist_freeze(clist);
1052
1053         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1054                                 NULL, summary_free_msginfo_func, NULL);
1055
1056         if (summaryview->folder_item) {
1057                 summaryview->folder_item->opened = FALSE;
1058                 summaryview->folder_item = NULL;
1059         }
1060
1061         summaryview->display_msg = FALSE;
1062
1063         summaryview->selected = NULL;
1064         summaryview->displayed = NULL;
1065         summaryview->total_size = 0;
1066         summaryview->deleted = summaryview->moved = 0;
1067         summaryview->copied = 0;
1068         if (summaryview->msgid_table) {
1069                 g_hash_table_destroy(summaryview->msgid_table);
1070                 summaryview->msgid_table = NULL;
1071         }
1072         if (summaryview->subject_table) {
1073                 g_hash_table_destroy(summaryview->subject_table);
1074                 summaryview->subject_table = NULL;
1075         }
1076         summaryview->mlist = NULL;
1077
1078         gtk_clist_clear(clist);
1079         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1080                 optimal_width = gtk_clist_optimal_column_width
1081                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1082                 gtk_clist_set_column_width
1083                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1084                          optimal_width);
1085         }
1086
1087         gtk_clist_thaw(clist);
1088 }
1089
1090 void summary_clear_all(SummaryView *summaryview)
1091 {
1092         messageview_clear(summaryview->messageview);
1093         summary_clear_list(summaryview);
1094         summary_set_menu_sensitive(summaryview);
1095         toolbar_main_set_sensitive(summaryview->mainwin);
1096         summary_status_show(summaryview);
1097 }
1098
1099 void summary_lock(SummaryView *summaryview)
1100 {
1101         summaryview->lock_count++;
1102 }
1103
1104 void summary_unlock(SummaryView *summaryview)
1105 {
1106         if (summaryview->lock_count)
1107                 summaryview->lock_count--;
1108 }
1109
1110 gboolean summary_is_locked(SummaryView *summaryview)
1111 {
1112         return summaryview->lock_count > 0;
1113 }
1114
1115 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1116 {
1117         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1118         SummarySelection selection;
1119
1120         if (!clist->row_list)
1121                 selection = SUMMARY_NONE;
1122         else if (!clist->selection)
1123                 selection = SUMMARY_SELECTED_NONE;
1124         else if (!clist->selection->next)
1125                 selection = SUMMARY_SELECTED_SINGLE;
1126         else
1127                 selection = SUMMARY_SELECTED_MULTIPLE;
1128
1129         return selection;
1130 }
1131
1132 /*!
1133  *\return       MsgInfo * Selected message if there's one selected;
1134  *              if multiple selected, or none, return NULL.
1135  */ 
1136 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1137 {
1138         /* summaryview->selected may be valid when multiple 
1139          * messages were selected */
1140         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1141
1142         if (sellist == NULL || sellist->next) 
1143                 return NULL;
1144         
1145         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1146 }
1147
1148 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1149 {
1150         GSList *mlist = NULL;
1151         GList *cur;
1152         MsgInfo *msginfo;
1153
1154         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
1155              cur = cur->next) {
1156                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1157                 mlist = g_slist_prepend(mlist, msginfo);
1158         }
1159
1160         mlist = g_slist_reverse(mlist);
1161
1162         return mlist;
1163 }
1164
1165 GSList *summary_get_msg_list(SummaryView *summaryview)
1166 {
1167         GSList *mlist = NULL;
1168         GtkCTree *ctree;
1169         GtkCTreeNode *node;
1170         MsgInfo *msginfo;
1171
1172         ctree = GTK_CTREE(summaryview->ctree);
1173
1174         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1175              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1176                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1177                 mlist = g_slist_prepend(mlist, msginfo);
1178         }
1179
1180         mlist = g_slist_reverse(mlist);
1181
1182         return mlist;
1183 }
1184
1185 static void summary_set_menu_sensitive(SummaryView *summaryview)
1186 {
1187         GtkItemFactory *ifactory = summaryview->popupfactory;
1188         SensitiveCond state;
1189         gboolean sensitive;
1190         GtkWidget *menuitem;
1191         gint i;
1192
1193         static const struct {
1194                 gchar *const entry;
1195                 SensitiveCond cond;
1196         } entry[] = {
1197                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1198                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1199                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1200                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1201                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1202                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1203
1204                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1205                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1206
1207                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1208
1209                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1210                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1211                 {"/Delete"                      , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1212                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1213
1214                 {"/Mark"                        , M_TARGET_EXIST},
1215                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1216                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1217                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1218                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1219                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1220                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1221                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1222                 {"/Color label"                 , M_TARGET_EXIST},
1223
1224                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1225                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1226                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1227
1228                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1229                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1230                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1231                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1232 #if 0
1233                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1234                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1235
1236                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1237                 {"/Select all"                  , M_TARGET_EXIST},
1238 #endif
1239                 {NULL, 0}
1240         };
1241
1242         main_window_set_menu_sensitive(summaryview->mainwin);
1243
1244         state = main_window_get_current_state(summaryview->mainwin);
1245
1246         for (i = 0; entry[i].entry != NULL; i++) {
1247                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1248                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1249         }
1250
1251
1252         summary_lock(summaryview);
1253         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1254         gtk_check_menu_item_set_active
1255                 (GTK_CHECK_MENU_ITEM(menuitem),
1256                  summaryview->messageview->mimeview->textview->show_all_headers);
1257         summary_unlock(summaryview);
1258 }
1259
1260 void summary_select_prev_unread(SummaryView *summaryview)
1261 {
1262         GtkCTreeNode *node;
1263         gboolean skip_cur = FALSE;
1264         
1265         if (summaryview->displayed 
1266         &&  summaryview->selected == summaryview->displayed) {
1267                 debug_print("skipping cur\n");
1268                 skip_cur = TRUE;
1269         }
1270
1271         node = summary_find_prev_flagged_msg
1272                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1273
1274         if (!node) {
1275                 AlertValue val = 0;
1276
1277                 switch (prefs_common.next_unread_msg_dialog) {
1278                         case NEXTUNREADMSGDIALOG_ALWAYS:
1279                                 val = alertpanel(_("No more unread messages"),
1280                                                  _("No unread message found. "
1281                                                    "Search from the end?"),
1282                                                  _("Yes"), _("No"), NULL);
1283                                 break;
1284                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1285                                 val = G_ALERTDEFAULT;
1286                                 break;
1287                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1288                                 val = !G_ALERTDEFAULT;
1289                                 break;
1290                         default:
1291                                 debug_print(
1292                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1293                 }
1294                 if (val != G_ALERTDEFAULT) return;
1295                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1296                                                      MSG_UNREAD, FALSE);
1297         }
1298
1299         if (!node)
1300                 alertpanel_notice(_("No unread messages."));
1301         else
1302                 summary_select_node(summaryview, node, TRUE, FALSE);
1303 }
1304
1305 void summary_select_next_unread(SummaryView *summaryview)
1306 {
1307         GtkCTreeNode *node = summaryview->selected;
1308         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1309         gboolean skip_cur = FALSE;
1310         
1311         if (summaryview->displayed 
1312         &&  summaryview->selected == summaryview->displayed) {
1313                 debug_print("skipping cur\n");
1314                 skip_cur = TRUE;
1315         }
1316
1317         node = summary_find_next_flagged_msg
1318                 (summaryview, node, MSG_UNREAD, skip_cur);
1319         
1320         if (node)
1321                 summary_select_node(summaryview, node, TRUE, FALSE);
1322         else {
1323                 node = summary_find_next_flagged_msg
1324                         (summaryview, NULL, MSG_UNREAD, FALSE);
1325                 if (node == NULL) {
1326                         AlertValue val = 0;
1327
1328                         switch (prefs_common.next_unread_msg_dialog) {
1329                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1330                                         val = alertpanel(_("No more unread messages"),
1331                                                          _("No unread message found. "
1332                                                            "Go to next folder?"),
1333                                                          _("Yes"), _("No"), NULL);
1334                                         break;
1335                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1336                                         val = G_ALERTDEFAULT;
1337                                         break;
1338                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1339                                         val = G_ALERTOTHER;
1340                                         break;
1341                                 default:
1342                                         debug_print(
1343                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1344                         }
1345
1346                         if (val == G_ALERTDEFAULT) {
1347                                 if (gtk_signal_n_emissions_by_name
1348                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1349                                                 gtk_signal_emit_stop_by_name
1350                                                         (GTK_OBJECT(ctree),
1351                                                          "key_press_event");
1352                                 folderview_select_next_unread(summaryview->folderview);
1353                                 return;
1354                         } 
1355                         else
1356                                 return;
1357                 } else
1358                         summary_select_node(summaryview, node, TRUE, FALSE);
1359
1360         }
1361 }
1362
1363 void summary_select_prev_new(SummaryView *summaryview)
1364 {
1365         GtkCTreeNode *node;
1366
1367         node = summary_find_prev_flagged_msg
1368                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1369
1370         if (!node) {
1371                 AlertValue val;
1372
1373                 val = alertpanel(_("No more new messages"),
1374                                  _("No new message found. "
1375                                    "Search from the end?"),
1376                                  _("Yes"), _("No"), NULL);
1377                 if (val != G_ALERTDEFAULT) return;
1378                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1379                                                      MSG_NEW, FALSE);
1380         }
1381
1382         if (!node)
1383                 alertpanel_notice(_("No new messages."));
1384         else
1385                 summary_select_node(summaryview, node, TRUE, FALSE);
1386 }
1387
1388 void summary_select_next_new(SummaryView *summaryview)
1389 {
1390         GtkCTreeNode *node = summaryview->selected;
1391         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1392
1393         while ((node = summary_find_next_flagged_msg
1394                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1395                 AlertValue val;
1396
1397                 val = alertpanel(_("No more new messages"),
1398                                  _("No new message found. "
1399                                    "Go to next folder?"),
1400                                  _("Yes"), _("Search again"), _("No"));
1401                 if (val == G_ALERTDEFAULT) {
1402                         if (gtk_signal_n_emissions_by_name
1403                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1404                                         gtk_signal_emit_stop_by_name
1405                                                 (GTK_OBJECT(ctree),
1406                                                  "key_press_event");
1407                         folderview_select_next_unread(summaryview->folderview);
1408                         return;
1409                 } else if (val == G_ALERTALTERNATE)
1410                         node = NULL;
1411                 else
1412                         return;
1413         }
1414
1415         if (node)
1416                 summary_select_node(summaryview, node, TRUE, FALSE);
1417 }
1418
1419 void summary_select_prev_marked(SummaryView *summaryview)
1420 {
1421         GtkCTreeNode *node;
1422
1423         node = summary_find_prev_flagged_msg
1424                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1425
1426         if (!node) {
1427                 AlertValue val;
1428
1429                 val = alertpanel(_("No more marked messages"),
1430                                  _("No marked message found. "
1431                                    "Search from the end?"),
1432                                  _("Yes"), _("No"), NULL);
1433                 if (val != G_ALERTDEFAULT) return;
1434                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1435                                                      MSG_MARKED, TRUE);
1436         }
1437
1438         if (!node)
1439                 alertpanel_notice(_("No marked messages."));
1440         else
1441                 summary_select_node(summaryview, node, TRUE, FALSE);
1442 }
1443
1444 void summary_select_next_marked(SummaryView *summaryview)
1445 {
1446         GtkCTreeNode *node;
1447
1448         node = summary_find_next_flagged_msg
1449                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1450
1451         if (!node) {
1452                 AlertValue val;
1453
1454                 val = alertpanel(_("No more marked messages"),
1455                                  _("No marked message found. "
1456                                    "Search from the beginning?"),
1457                                  _("Yes"), _("No"), NULL);
1458                 if (val != G_ALERTDEFAULT) return;
1459                 node = summary_find_next_flagged_msg(summaryview, NULL,
1460                                                      MSG_MARKED, TRUE);
1461         }
1462
1463         if (!node)
1464                 alertpanel_notice(_("No marked messages."));
1465         else
1466                 summary_select_node(summaryview, node, TRUE, FALSE);
1467 }
1468
1469 void summary_select_prev_labeled(SummaryView *summaryview)
1470 {
1471         GtkCTreeNode *node;
1472
1473         node = summary_find_prev_flagged_msg
1474                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1475
1476         if (!node) {
1477                 AlertValue val;
1478
1479                 val = alertpanel(_("No more labeled messages"),
1480                                  _("No labeled message found. "
1481                                    "Search from the end?"),
1482                                  _("Yes"), _("No"), NULL);
1483                 if (val != G_ALERTDEFAULT) return;
1484                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1485                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1486         }
1487
1488         if (!node)
1489                 alertpanel_notice(_("No labeled messages."));
1490         else
1491                 summary_select_node(summaryview, node, TRUE, FALSE);
1492 }
1493
1494 void summary_select_next_labeled(SummaryView *summaryview)
1495 {
1496         GtkCTreeNode *node;
1497
1498         node = summary_find_next_flagged_msg
1499                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1500
1501         if (!node) {
1502                 AlertValue val;
1503
1504                 val = alertpanel(_("No more labeled messages"),
1505                                  _("No labeled message found. "
1506                                    "Search from the beginning?"),
1507                                  _("Yes"), _("No"), NULL);
1508                 if (val != G_ALERTDEFAULT) return;
1509                 node = summary_find_next_flagged_msg(summaryview, NULL,
1510                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1511         }
1512
1513         if (!node)
1514                 alertpanel_notice(_("No labeled messages."));
1515         else
1516                 summary_select_node(summaryview, node, TRUE, FALSE);
1517 }
1518
1519 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1520 {
1521         GtkCTreeNode *node;
1522
1523         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1524         summary_select_node(summaryview, node, FALSE, TRUE);
1525 }
1526
1527 /**
1528  * summary_select_node:
1529  * @summaryview: Summary view.
1530  * @node: Summary tree node.
1531  * @display_msg: TRUE to display the selected message.
1532  * @do_refresh: TRUE to refresh the widget.
1533  *
1534  * Select @node (bringing it into view by scrolling and expanding its
1535  * thread, if necessary) and unselect all others.  If @display_msg is
1536  * TRUE, display the corresponding message in the message view.
1537  * If @do_refresh is TRUE, the widget is refreshed.
1538  **/
1539 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1540                          gboolean display_msg, gboolean do_refresh)
1541 {
1542         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1543
1544         if (node) {
1545                 gtkut_ctree_expand_parent_all(ctree, node);
1546                 if (do_refresh) {
1547                         GTK_EVENTS_FLUSH();
1548                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1549                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1550                 }
1551                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1552                 if (display_msg && summaryview->displayed == node)
1553                         summaryview->displayed = NULL;
1554                 summaryview->display_msg = display_msg;
1555                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1556         }
1557 }
1558
1559 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1560 {
1561         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1562         MsgInfo *msginfo;
1563
1564         if (!node)
1565                 return 0;
1566         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1567         return msginfo->msgnum;
1568 }
1569
1570 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1571                                            GtkCTreeNode *current_node)
1572 {
1573         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1574         GtkCTreeNode *node;
1575         MsgInfo *msginfo;
1576
1577         if (current_node)
1578                 node = current_node;
1579         else
1580                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1581
1582         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1583                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1584                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1585         }
1586
1587         return node;
1588 }
1589
1590 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1591                                            GtkCTreeNode *current_node)
1592 {
1593         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1594         GtkCTreeNode *node;
1595         MsgInfo *msginfo;
1596
1597         if (current_node)
1598                 node = current_node;
1599         else
1600                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1601
1602         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1603                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1604                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1605         }
1606
1607         return node;
1608 }
1609
1610 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1611                                                    GtkCTreeNode *current_node,
1612                                                    MsgPermFlags flags,
1613                                                    gboolean start_from_prev)
1614 {
1615         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1616         GtkCTreeNode *node;
1617         MsgInfo *msginfo;
1618
1619         if (current_node) {
1620                 if (start_from_prev)
1621                         node = GTK_CTREE_NODE_PREV(current_node);
1622                 else
1623                         node = current_node;
1624         } else
1625                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1626
1627         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1628                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1629                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1630         }
1631
1632         return node;
1633 }
1634
1635 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1636                                                    GtkCTreeNode *current_node,
1637                                                    MsgPermFlags flags,
1638                                                    gboolean start_from_next)
1639 {
1640         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1641         GtkCTreeNode *node;
1642         MsgInfo *msginfo;
1643
1644         if (current_node) {
1645                 if (start_from_next)
1646                         node = gtkut_ctree_node_next(ctree, current_node);
1647                 else
1648                         node = current_node;
1649         } else
1650                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1651
1652         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1653                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1654                 /* Find msg with matching flags but ignore messages with
1655                    ignore flags, if searching for new or unread messages */
1656                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1657                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1658                         break;
1659         }
1660
1661         return node;
1662 }
1663
1664 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1665                                                 guint msgnum)
1666 {
1667         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1668         GtkCTreeNode *node;
1669         MsgInfo *msginfo;
1670
1671         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1672
1673         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1674                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1675                 if (msginfo && msginfo->msgnum == msgnum) break;
1676         }
1677
1678         return node;
1679 }
1680
1681 static guint attract_hash_func(gconstpointer key)
1682 {
1683         gchar *str;
1684         gchar *p;
1685         guint h;
1686
1687         Xstrdup_a(str, (const gchar *)key, return 0);
1688         trim_subject(str);
1689
1690         p = str;
1691         h = *p;
1692
1693         if (h) {
1694                 for (p += 1; *p != '\0'; p++)
1695                         h = (h << 5) - h + *p;
1696         }
1697
1698         return h;
1699 }
1700
1701 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1702 {
1703         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1704 }
1705
1706 void summary_attract_by_subject(SummaryView *summaryview)
1707 {
1708         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1709         GtkCList *clist = GTK_CLIST(ctree);
1710         GtkCTreeNode *src_node;
1711         GtkCTreeNode *dst_node, *sibling;
1712         GtkCTreeNode *tmp;
1713         MsgInfo *src_msginfo, *dst_msginfo;
1714         GHashTable *subject_table;
1715
1716         debug_print("Attracting messages by subject...");
1717         STATUSBAR_PUSH(summaryview->mainwin,
1718                        _("Attracting messages by subject..."));
1719
1720         main_window_cursor_wait(summaryview->mainwin);
1721         gtk_clist_freeze(clist);
1722
1723         subject_table = g_hash_table_new(attract_hash_func,
1724                                          attract_compare_func);
1725
1726         for (src_node = GTK_CTREE_NODE(clist->row_list);
1727              src_node != NULL;
1728              src_node = tmp) {
1729                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1730                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1731                 if (!src_msginfo) continue;
1732                 if (!src_msginfo->subject) continue;
1733
1734                 /* find attracting node */
1735                 dst_node = g_hash_table_lookup(subject_table,
1736                                                src_msginfo->subject);
1737
1738                 if (dst_node) {
1739                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1740
1741                         /* if the time difference is more than 20 days,
1742                            don't attract */
1743                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1744                             > 60 * 60 * 24 * 20)
1745                                 continue;
1746
1747                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1748                         if (src_node != sibling)
1749                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1750                 }
1751
1752                 g_hash_table_insert(subject_table,
1753                                     src_msginfo->subject, src_node);
1754         }
1755
1756         g_hash_table_destroy(subject_table);
1757
1758         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1759
1760         gtk_clist_thaw(clist);
1761
1762         debug_print("done.\n");
1763         STATUSBAR_POP(summaryview->mainwin);
1764
1765         main_window_cursor_normal(summaryview->mainwin);
1766 }
1767
1768 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1769                                       gpointer data)
1770 {
1771         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1772
1773         if (msginfo)
1774                 procmsg_msginfo_free(msginfo);
1775 }
1776
1777 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1778                                    gpointer data)
1779 {
1780         SummaryView *summaryview = data;
1781         MsgInfo *msginfo;
1782
1783         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1784
1785         if (msginfo->folder && msginfo->folder->folder &&
1786             msginfo->folder->folder->klass->type == F_NEWS)
1787                 news_flag_crosspost(msginfo);
1788
1789         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1790         && procmsg_msg_has_marked_parent(msginfo))
1791                 summaryview->unreadmarked++;
1792
1793         if (MSG_IS_DELETED(msginfo->flags))
1794                 summaryview->deleted++;
1795
1796         summaryview->total_size += msginfo->size;
1797
1798         summary_set_row_marks(summaryview, node);
1799 }
1800
1801 static void summary_update_status(SummaryView *summaryview)
1802 {
1803         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1804         GtkCTreeNode *node;
1805         MsgInfo *msginfo;
1806
1807         summaryview->total_size =
1808         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1809
1810         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1811              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1812                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1813
1814                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1815                 && procmsg_msg_has_marked_parent(msginfo))
1816                         summaryview->unreadmarked++;
1817                 if (MSG_IS_DELETED(msginfo->flags))
1818                         summaryview->deleted++;
1819                 if (MSG_IS_MOVE(msginfo->flags))
1820                         summaryview->moved++;
1821                 if (MSG_IS_COPY(msginfo->flags))
1822                         summaryview->copied++;
1823                 summaryview->total_size += msginfo->size;
1824         }
1825 }
1826
1827 static void summary_status_show(SummaryView *summaryview)
1828 {
1829         gchar *str;
1830         gchar *del, *mv, *cp;
1831         gchar *sel;
1832         gchar *spc;
1833         gchar *itstr;
1834         GList *rowlist, *cur;
1835         guint n_selected = 0;
1836         off_t sel_size = 0;
1837         MsgInfo *msginfo;
1838         gchar *name;
1839
1840         if (!summaryview->folder_item) {
1841                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1842                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1843                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1844                 return;
1845         }
1846
1847         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1848         for (cur = rowlist; cur != NULL; cur = cur->next) {
1849                 msginfo = gtk_ctree_node_get_row_data
1850                         (GTK_CTREE(summaryview->ctree),
1851                          GTK_CTREE_NODE(cur->data));
1852                 if (!msginfo)
1853                         g_warning("summary_status_show(): msginfo == NULL\n");
1854                 else {
1855                         sel_size += msginfo->size;
1856                         n_selected++;
1857                 }
1858         }
1859
1860         name = folder_item_get_name(summaryview->folder_item);
1861         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), name);
1862         g_free(name);
1863
1864         if (summaryview->deleted)
1865                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1866         else
1867                 del = g_strdup("");
1868         if (summaryview->moved)
1869                 mv = g_strdup_printf(_("%s%d moved"),
1870                                      summaryview->deleted ? _(", ") : "",
1871                                      summaryview->moved);
1872         else
1873                 mv = g_strdup("");
1874         if (summaryview->copied)
1875                 cp = g_strdup_printf(_("%s%d copied"),
1876                                      summaryview->deleted ||
1877                                      summaryview->moved ? _(", ") : "",
1878                                      summaryview->copied);
1879         else
1880                 cp = g_strdup("");
1881
1882         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1883                 spc = "    ";
1884         else
1885                 spc = "";
1886
1887         if (n_selected) {
1888                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1889                 if (n_selected == 1)
1890                         itstr = g_strdup(_(" item selected"));
1891                 else
1892                         itstr = g_strdup(_(" items selected"));
1893         } else {
1894                 sel = g_strdup("");
1895                 itstr = g_strdup("");
1896         }
1897                 
1898         str = g_strconcat(n_selected ? itos(n_selected) : "",
1899                                         itstr, sel, spc, del, mv, cp, NULL);
1900         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1901         g_free(str);
1902         g_free(sel);
1903         g_free(del);
1904         g_free(mv);
1905         g_free(cp);
1906         g_free(itstr);
1907
1908         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1909
1910                                       summaryview->folder_item->new_msgs,
1911                                       summaryview->folder_item->unread_msgs,
1912                                       summaryview->folder_item->total_msgs,
1913                                       to_human_readable(summaryview->total_size));
1914         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1915         g_free(str);
1916 }
1917
1918 static void summary_set_column_titles(SummaryView *summaryview)
1919 {
1920         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1921         GtkWidget *hbox;
1922         GtkWidget *label;
1923         GtkWidget *arrow;
1924         gint pos;
1925         const gchar *title;
1926         SummaryColumnType type;
1927         gboolean single_char;
1928         GtkJustification justify;
1929
1930         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1931                 SORT_BY_MARK,
1932                 SORT_BY_STATUS,
1933                 SORT_BY_MIME,
1934                 SORT_BY_SUBJECT,
1935                 SORT_BY_FROM,
1936                 SORT_BY_DATE,
1937                 SORT_BY_SIZE,
1938                 SORT_BY_NUMBER,
1939                 SORT_BY_SCORE,
1940                 SORT_BY_LOCKED
1941         };
1942
1943         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1944                 type = summaryview->col_state[pos].type;
1945
1946                 /* CLAWS: mime and unread are single char headers */
1947                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1948                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1949                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1950
1951                 switch (type) {
1952                 case S_COL_SUBJECT:
1953                 case S_COL_FROM:
1954                 case S_COL_DATE:
1955                 case S_COL_NUMBER:
1956                         if (prefs_common.trans_hdr)
1957                                 title = gettext(col_label[type]);
1958                         else
1959                                 title = col_label[type];
1960                         break;
1961                 /* CLAWS: dummies for mark and locked headers */        
1962                 case S_COL_MARK:        
1963                 case S_COL_LOCKED:
1964                         title = "";
1965                         break;
1966                 default:
1967                         title = gettext(col_label[type]);
1968                 }
1969
1970                 if (type == S_COL_MIME) {
1971                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1972                         gtk_widget_show(label);
1973                         gtk_clist_set_column_widget(clist, pos, label);
1974                         continue;
1975                 }
1976                 if (single_char) {
1977                         gtk_clist_set_column_title(clist, pos, title);
1978                         continue;
1979                 }
1980
1981                 /* CLAWS: changed so that locked and mark headers
1982                  * show a pixmap instead of single character */
1983                 hbox  = gtk_hbox_new(FALSE, 4);
1984                 
1985                 if (type == S_COL_LOCKED)
1986                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1987                 else if (type == S_COL_MARK) 
1988                         label = gtk_pixmap_new(markxpm, markxpmmask);
1989                 else 
1990                         label = gtk_label_new(title);
1991                 
1992                 if (justify == GTK_JUSTIFY_RIGHT)
1993                         gtk_box_pack_end(GTK_BOX(hbox), label,
1994                                          FALSE, FALSE, 0);
1995                 else
1996                         gtk_box_pack_start(GTK_BOX(hbox), label,
1997                                            FALSE, FALSE, 0);
1998
1999                 if (summaryview->sort_key == sort_by[type]) {
2000                         arrow = gtk_arrow_new
2001                                 (summaryview->sort_type == SORT_ASCENDING
2002                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2003                                  GTK_SHADOW_IN);
2004                         if (justify == GTK_JUSTIFY_RIGHT)
2005                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2006                                                    FALSE, FALSE, 0);
2007                         else
2008                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2009                                                  FALSE, FALSE, 0);
2010                 }
2011
2012                 gtk_widget_show_all(hbox);
2013                 gtk_clist_set_column_widget(clist, pos, hbox);
2014         }
2015 }
2016
2017 void summary_sort(SummaryView *summaryview,
2018                   FolderSortKey sort_key, FolderSortType sort_type)
2019 {
2020         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2021         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2022         GtkCListCompareFunc cmp_func = NULL;
2023
2024         switch (sort_key) {
2025         case SORT_BY_MARK:
2026                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2027                 break;
2028         case SORT_BY_STATUS:
2029                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2030                 break;
2031         case SORT_BY_MIME:
2032                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2033                 break;
2034         case SORT_BY_NUMBER:
2035                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2036                 break;
2037         case SORT_BY_SIZE:
2038                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2039                 break;
2040         case SORT_BY_DATE:
2041                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2042                 break;
2043         case SORT_BY_FROM:
2044                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2045                 break;
2046         case SORT_BY_SUBJECT:
2047                 if (summaryview->simplify_subject_preg)
2048                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2049                 else
2050                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2051                 break;
2052         case SORT_BY_SCORE:
2053                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2054                 break;
2055         case SORT_BY_LABEL:
2056                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2057                 break;
2058         case SORT_BY_TO:
2059                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2060                 break;
2061         case SORT_BY_LOCKED:
2062                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2063                 break;
2064         case SORT_BY_NONE:
2065                 break;
2066         default:
2067                 return;
2068         }
2069
2070         summaryview->sort_key = sort_key;
2071         summaryview->sort_type = sort_type;
2072
2073         summary_set_column_titles(summaryview);
2074         summary_set_menu_sensitive(summaryview);
2075
2076         /* allow fallback to don't sort */
2077         if (summaryview->sort_key == SORT_BY_NONE)
2078                 return;
2079
2080         if(cmp_func != NULL) {
2081                 debug_print("Sorting summary...");
2082                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2083
2084                 main_window_cursor_wait(summaryview->mainwin);
2085
2086                 gtk_clist_set_compare_func(clist, cmp_func);
2087
2088                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2089
2090                 gtk_sctree_sort_recursive(ctree, NULL);
2091
2092                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2093
2094                 main_window_cursor_normal(summaryview->mainwin);
2095
2096                 debug_print("done.\n");
2097                 STATUSBAR_POP(summaryview->mainwin);
2098         }
2099 }
2100
2101 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2102                                    GtkCTreeNode *cnode, gpointer data)
2103 {
2104         SummaryView *summaryview = (SummaryView *)data;
2105         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2106         gchar *text[N_SUMMARY_COLS];
2107         gint *col_pos = summaryview->col_pos;
2108         const gchar *msgid = msginfo->msgid;
2109         GHashTable *msgid_table = summaryview->msgid_table;
2110
2111         summary_set_header(summaryview, text, msginfo);
2112
2113         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2114                                 NULL, NULL, NULL, NULL, FALSE,
2115                                 gnode->parent->parent ? TRUE : FALSE);
2116 #define SET_TEXT(col) \
2117         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2118                                 text[col_pos[col]])
2119
2120         SET_TEXT(S_COL_NUMBER);
2121         SET_TEXT(S_COL_SCORE);
2122         SET_TEXT(S_COL_SIZE);
2123         SET_TEXT(S_COL_DATE);
2124         SET_TEXT(S_COL_FROM);
2125         SET_TEXT(S_COL_SUBJECT);
2126
2127 #undef SET_TEXT
2128
2129         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2130         summary_set_marks_func(ctree, cnode, summaryview);
2131
2132         if (msgid && msgid[0] != '\0')
2133                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2134
2135         return TRUE;
2136 }
2137
2138 static void summary_set_ctree_from_list(SummaryView *summaryview,
2139                                         GSList *mlist)
2140 {
2141         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2142         MsgInfo *msginfo;
2143                 GtkCTreeNode *node = NULL;
2144         GHashTable *msgid_table;
2145         GHashTable *subject_table;
2146         GSList * cur;
2147         
2148         if (!mlist) return;
2149
2150         debug_print("\tSetting summary from message data...");
2151         STATUSBAR_PUSH(summaryview->mainwin,
2152                        _("Setting summary from message data..."));
2153         gdk_flush();
2154
2155         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2156         summaryview->msgid_table = msgid_table;
2157         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2158         summaryview->subject_table = subject_table;
2159
2160         if (prefs_common.use_addr_book)
2161                 start_address_completion();
2162         
2163         for (cur = mlist ; cur != NULL; cur = cur->next) {
2164                 msginfo = (MsgInfo *)cur->data;
2165                 msginfo->threadscore = msginfo->score;
2166         }
2167
2168         if (summaryview->threaded) {
2169                 GNode *root, *gnode;
2170
2171                 root = procmsg_get_thread_tree(mlist);
2172
2173                 for (gnode = root->children; gnode != NULL;
2174                      gnode = gnode->next) {
2175                         node = gtk_ctree_insert_gnode
2176                                 (ctree, NULL, node, gnode,
2177                                  summary_insert_gnode_func, summaryview);
2178                 }
2179
2180                 g_node_destroy(root);
2181
2182                 summary_thread_init(summaryview);
2183         } else {
2184                 gchar *text[N_SUMMARY_COLS];
2185                 cur = mlist;
2186                 for (; mlist != NULL; mlist = mlist->next) {
2187                         msginfo = (MsgInfo *)mlist->data;
2188
2189                         summary_set_header(summaryview, text, msginfo);
2190
2191                         node = gtk_ctree_insert_node
2192                                 (ctree, NULL, node, text, 2,
2193                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2194                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2195                         summary_set_marks_func(ctree, node, summaryview);
2196
2197                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2198                                 g_hash_table_insert(msgid_table,
2199                                                     msginfo->msgid, node);
2200
2201                         subject_table_insert(subject_table,
2202                                              msginfo->subject,
2203                                              node);
2204                 }
2205                 mlist = cur;
2206         }
2207
2208         if (prefs_common.enable_hscrollbar &&
2209             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2210                 gint optimal_width;
2211
2212                 optimal_width = gtk_clist_optimal_column_width
2213                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2214                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2215                                            summaryview->col_pos[S_COL_SUBJECT],
2216                                            optimal_width);
2217         }
2218
2219         if (prefs_common.use_addr_book)
2220                 end_address_completion();
2221
2222         debug_print("done.\n");
2223         STATUSBAR_POP(summaryview->mainwin);
2224         if (debug_get_mode()) {
2225                 debug_print("\tmsgid hash table size = %d\n",
2226                             g_hash_table_size(msgid_table));
2227                 debug_print("\tsubject hash table size = %d\n",
2228                             g_hash_table_size(subject_table));
2229         }
2230 }
2231
2232 static gchar *summary_complete_address(const gchar *addr)
2233 {
2234         gint count;
2235         gchar *res, *tmp, *email_addr;
2236
2237         Xstrdup_a(email_addr, addr, return NULL);
2238         extract_address(email_addr);
2239         g_return_val_if_fail(*email_addr, NULL);
2240
2241         /*
2242          * completion stuff must be already initialized
2243          */
2244         res = NULL;
2245         if (1 < (count = complete_address(email_addr))) {
2246                 tmp = get_complete_address(1);
2247 /*      tmp = addressbook_lookup_name( email_addr );
2248         if( tmp ) { */
2249                 res = procheader_get_fromname(tmp);
2250                 g_free(tmp);
2251         }
2252
2253         return res;
2254 }
2255
2256 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2257                                MsgInfo *msginfo)
2258 {
2259         static gchar date_modified[80];
2260         static gchar *to = NULL;
2261         static gchar col_score[11];
2262         static gchar buf[BUFFSIZE];
2263         gint *col_pos = summaryview->col_pos;
2264         FolderType ftype = F_UNKNOWN;
2265
2266         text[col_pos[S_COL_MARK]]   = NULL;
2267         text[col_pos[S_COL_STATUS]] = NULL;
2268         text[col_pos[S_COL_MIME]]   = NULL;
2269         text[col_pos[S_COL_LOCKED]] = NULL;
2270         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2271         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2272         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2273
2274         if (msginfo->date_t) {
2275                 procheader_date_get_localtime(date_modified,
2276                                               sizeof(date_modified),
2277                                               msginfo->date_t);
2278                 text[col_pos[S_COL_DATE]] = date_modified;
2279         } else if (msginfo->date)
2280                 text[col_pos[S_COL_DATE]] = msginfo->date;
2281         else
2282                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2283
2284         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2285                 _("(No From)");
2286         
2287         if (msginfo->folder && msginfo->folder->folder)
2288                 ftype = msginfo->folder->folder->klass->type; 
2289                 
2290         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2291                 gchar *addr = NULL;
2292
2293                 Xstrdup_a(addr, msginfo->from, return);
2294                 extract_address(addr);
2295                 if (prefs_common.use_addr_book) {
2296                         if (account_find_from_address(addr)) {
2297                                 addr = summary_complete_address(msginfo->to);
2298                                 g_free(to);
2299                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2300                                 text[col_pos[S_COL_FROM]] = to;
2301                                 g_free(addr);
2302                         }
2303                 } else {
2304                         if (account_find_from_address(addr)) {
2305                                 g_free(to);
2306                                 to = g_strconcat("-->", msginfo->to, NULL);
2307                                 text[col_pos[S_COL_FROM]] = to;
2308                         }
2309                 }
2310         }
2311
2312         /*
2313          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2314          * checking whether the above block (which handles the special case of
2315          * the --> in sent boxes) was executed.
2316          */
2317         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2318                 gchar *from = summary_complete_address(msginfo->from);
2319                 if (from) {
2320                         g_free(to);
2321                         to = from;
2322                         text[col_pos[S_COL_FROM]] = to;
2323                 }                       
2324         }
2325
2326         if (summaryview->simplify_subject_preg != NULL)
2327                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2328                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2329                                         summaryview->simplify_subject_preg) : 
2330                         _("(No Subject)");
2331         else 
2332                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2333                         _("(No Subject)");
2334 }
2335
2336 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2337 {
2338         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2339 }
2340
2341 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2342                                       GtkCTreeNode *row)
2343 {
2344         g_return_if_fail(summaryview != NULL);
2345         g_return_if_fail(msginfo != NULL);
2346         g_return_if_fail(row != NULL);
2347
2348         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2349         && procmsg_msg_has_marked_parent(msginfo))
2350                 summaryview->unreadmarked--;
2351         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2352                 procmsg_msginfo_unset_flags
2353                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2354                 summary_set_row_marks(summaryview, row);
2355                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2356                 summary_status_show(summaryview);
2357         }
2358 }
2359
2360 typedef struct  {
2361         MsgInfo *msginfo;
2362         SummaryView *summaryview;
2363         GtkCTreeNode *row;
2364 } MarkAsReadData;
2365
2366 static int msginfo_mark_as_read_timeout(void *data)
2367 {
2368         MarkAsReadData *mdata = (MarkAsReadData *)data;
2369         if (!mdata)
2370                 return FALSE;
2371         
2372         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2373                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2374                                      mdata->row); 
2375
2376         g_free(mdata);
2377
2378         return FALSE;   
2379 }
2380
2381 static void summary_display_msg_full(SummaryView *summaryview,
2382                                      GtkCTreeNode *row,
2383                                      gboolean new_window, gboolean all_headers)
2384 {
2385         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2386         MsgInfo *msginfo;
2387         gint val;
2388
2389         if (!new_window) {
2390                 if (summaryview->displayed == row)
2391                         return;
2392                 else
2393                         summaryview->messageview->filtered = FALSE;
2394         }                       
2395         
2396         g_return_if_fail(row != NULL);
2397
2398         if (summary_is_locked(summaryview)) return;
2399         summary_lock(summaryview);
2400
2401         STATUSBAR_POP(summaryview->mainwin);
2402         GTK_EVENTS_FLUSH();
2403
2404         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2405
2406         if (new_window) {
2407                 MessageView *msgview;
2408
2409                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2410                 val = messageview_show(msgview, msginfo, all_headers);
2411         } else {
2412                 MessageView *msgview;
2413
2414                 msgview = summaryview->messageview;
2415
2416                 summaryview->displayed = row;
2417                 if (!messageview_is_visible(msgview))
2418                         main_window_toggle_message_view(summaryview->mainwin);
2419                 val = messageview_show(msgview, msginfo, all_headers);
2420                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2421                         gtk_widget_grab_focus(summaryview->ctree);
2422                 GTK_EVENTS_FLUSH();
2423                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2424         }
2425
2426         if (val == 0) {
2427                 if (prefs_common.mark_as_read_delay) {
2428                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2429                         data->summaryview = summaryview;
2430                         data->msginfo = msginfo;
2431                         data->row = row;
2432                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2433                                 msginfo_mark_as_read_timeout, data);
2434                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2435                         msginfo_mark_as_read(summaryview, msginfo, row);
2436                 }
2437         }
2438
2439         summary_set_menu_sensitive(summaryview);
2440         toolbar_main_set_sensitive(summaryview->mainwin);
2441         messageview_set_menu_sensitive(summaryview->messageview);
2442
2443         summary_unlock(summaryview);
2444 }
2445
2446 void summary_display_msg_selected(SummaryView *summaryview,
2447                                   gboolean all_headers)
2448 {
2449         if (summary_is_locked(summaryview)) return;
2450         summaryview->displayed = NULL;
2451         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2452                                  all_headers);
2453 }
2454
2455 void summary_redisplay_msg(SummaryView *summaryview)
2456 {
2457         GtkCTreeNode *node;
2458
2459         if (summaryview->displayed) {
2460                 node = summaryview->displayed;
2461                 summaryview->displayed = NULL;
2462                 summary_display_msg(summaryview, node);
2463         }
2464 }
2465
2466 void summary_open_msg(SummaryView *summaryview)
2467 {
2468         if (!summaryview->selected) return;
2469         
2470         /* CLAWS: if separate message view, don't open a new window
2471          * but rather use the current separated message view */
2472         summary_display_msg_full(summaryview, summaryview->selected,
2473                                  prefs_common.sep_msg ? FALSE : TRUE, 
2474                                  FALSE);
2475 }
2476
2477 void summary_view_source(SummaryView * summaryview)
2478 {
2479         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2480         MsgInfo *msginfo;
2481         SourceWindow *srcwin;
2482
2483         if (!summaryview->selected) return;
2484
2485         srcwin = source_window_create();
2486         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2487         source_window_show_msg(srcwin, msginfo);
2488         source_window_show(srcwin);
2489 }
2490
2491 void summary_reedit(SummaryView *summaryview)
2492 {
2493         MsgInfo *msginfo;
2494
2495         if (!summaryview->selected) return;
2496         if (!summaryview->folder_item) return;
2497         if (summaryview->folder_item->stype != F_OUTBOX &&
2498             summaryview->folder_item->stype != F_DRAFT  &&
2499             summaryview->folder_item->stype != F_QUEUE) return;
2500
2501         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2502                                               summaryview->selected);
2503         if (!msginfo) return;
2504
2505         compose_reedit(msginfo);
2506 }
2507
2508 void summary_step(SummaryView *summaryview, GtkScrollType type)
2509 {
2510         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2511         GtkCTreeNode *node;
2512
2513         if (summary_is_locked(summaryview)) return;
2514
2515         if (type == GTK_SCROLL_STEP_FORWARD) {
2516                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2517                 if (node)
2518                         gtkut_ctree_expand_parent_all(ctree, node);
2519                 else
2520                         return;
2521         } else {
2522                 if (summaryview->selected) {
2523                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2524                         if (!node) return;
2525                 }
2526         }
2527
2528         if (messageview_is_visible(summaryview->messageview))
2529                 summaryview->display_msg = TRUE;
2530
2531         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2532                                 type, 0.0);
2533
2534         if (GTK_CLIST(ctree)->selection)
2535                 gtk_sctree_set_anchor_row
2536                         (GTK_SCTREE(ctree),
2537                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2538
2539 }
2540
2541 void summary_toggle_view(SummaryView *summaryview)
2542 {
2543         if (!messageview_is_visible(summaryview->messageview) &&
2544             summaryview->selected)
2545                 summary_display_msg(summaryview,
2546                                     summaryview->selected);
2547         else
2548                 main_window_toggle_message_view(summaryview->mainwin);
2549 }
2550
2551 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2552                                                 GtkCTreeNode *node)
2553 {
2554         MsgInfo *msginfo;
2555
2556         if (node) {
2557                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2558                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2559                         return TRUE;
2560                 node = GTK_CTREE_ROW(node)->children;
2561         } else
2562                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2563
2564         while (node) {
2565                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2566                         return TRUE;
2567                 node = GTK_CTREE_ROW(node)->sibling;
2568         }
2569
2570         return FALSE;
2571 }
2572
2573 static gboolean summary_have_unread_children(SummaryView *summaryview,
2574                                              GtkCTreeNode *node)
2575 {
2576         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2577
2578         if (!node) return FALSE;
2579
2580         node = GTK_CTREE_ROW(node)->children;
2581
2582         while (node) {
2583                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2584                         return TRUE;
2585                 node = GTK_CTREE_ROW(node)->sibling;
2586         }
2587
2588         return FALSE;
2589 }
2590
2591 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2592 {
2593         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2594         GtkStyle *style = NULL;
2595         MsgInfo *msginfo;
2596         MsgFlags flags;
2597         gint *col_pos = summaryview->col_pos;
2598
2599         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2600         if (!msginfo) return;
2601
2602         flags = msginfo->flags;
2603
2604         gtk_ctree_node_set_foreground(ctree, row, NULL);
2605
2606         /* set new/unread column */
2607         if (MSG_IS_IGNORE_THREAD(flags)) {
2608                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2609                                           ignorethreadxpm, ignorethreadxpmmask);
2610         } else if (MSG_IS_NEW(flags)) {
2611                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2612                                           newxpm, newxpmmask);
2613         } else if (MSG_IS_UNREAD(flags)) {
2614                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2615                                           unreadxpm, unreadxpmmask);
2616         } else if (MSG_IS_REPLIED(flags)) {
2617                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2618                                           repliedxpm, repliedxpmmask);
2619         } else if (MSG_IS_FORWARDED(flags)) {
2620                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2621                                           forwardedxpm, forwardedxpmmask);
2622         } else {
2623                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2624                                         NULL);
2625         }
2626
2627         if (prefs_common.bold_unread &&
2628             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2629              (!GTK_CTREE_ROW(row)->expanded &&
2630               GTK_CTREE_ROW(row)->children &&
2631               summary_have_unread_children(summaryview, row))))
2632                 style = bold_style;
2633
2634         /* set mark column */
2635         if (MSG_IS_DELETED(flags)) {
2636                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2637                                           deletedxpm, deletedxpmmask);
2638                 if (style)
2639                         style = bold_deleted_style;
2640                 else {
2641                         style = small_deleted_style;
2642                 }
2643                         gtk_ctree_node_set_foreground
2644                                 (ctree, row, &summaryview->color_dim);
2645         } else if (MSG_IS_MARKED(flags)) {
2646                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2647                                           markxpm, markxpmmask);
2648         } else if (MSG_IS_MOVE(flags)) {
2649                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2650                 if (style)
2651                         style = bold_marked_style;
2652                 else {
2653                         style = small_marked_style;
2654                 }
2655                         gtk_ctree_node_set_foreground
2656                                 (ctree, row, &summaryview->color_marked);
2657         } else if (MSG_IS_COPY(flags)) {
2658                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2659                 if (style)
2660                         style = bold_marked_style;
2661                 else {
2662                         style = small_marked_style;
2663                 }
2664                         gtk_ctree_node_set_foreground
2665                                 (ctree, row, &summaryview->color_marked);
2666 #if 0
2667         } else if ((global_scoring ||
2668                   summaryview->folder_item->prefs->scoring) &&
2669                  (msginfo->score >= summaryview->important_score) &&
2670                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2671                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2672                 gtk_ctree_node_set_foreground(ctree, row,
2673                                               &summaryview->color_important);
2674 #endif
2675         } else {
2676                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2677         }
2678
2679         if (MSG_IS_LOCKED(flags)) {
2680                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2681                                           lockedxpm, lockedxpmmask);
2682         }
2683         else {
2684                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2685         }
2686
2687         if (MSG_IS_SIGNED(flags)) {
2688                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2689                                           gpgsignedxpm, gpgsignedxpmmask);
2690         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2691                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2692                                           clipkeyxpm, clipkeyxpmmask);
2693         } else if (MSG_IS_ENCRYPTED(flags)) {
2694                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2695                                           keyxpm, keyxpmmask);
2696         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2697                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2698                                           clipxpm, clipxpmmask);
2699         } else {
2700                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2701         }
2702         if (!style)
2703                 style = small_style;
2704
2705         gtk_ctree_node_set_row_style(ctree, row, style);
2706
2707         if (MSG_GET_COLORLABEL(flags))
2708                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2709 }
2710
2711 void summary_set_marks_selected(SummaryView *summaryview)
2712 {
2713         GList *cur;
2714
2715         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2716              cur = cur->next)
2717                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2718 }
2719
2720 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2721 {
2722         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2723         MsgInfo *msginfo;
2724
2725         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2726         if (MSG_IS_DELETED(msginfo->flags))
2727                 summaryview->deleted--;
2728         if (MSG_IS_MOVE(msginfo->flags))
2729                 summaryview->moved--;
2730         if (MSG_IS_COPY(msginfo->flags))
2731                 summaryview->copied--;
2732         procmsg_update_unread_children (msginfo, TRUE);
2733
2734         procmsg_msginfo_set_to_folder(msginfo, NULL);
2735         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2736         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2737         summary_set_row_marks(summaryview, row);
2738         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2739 }
2740
2741 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2742 {
2743         gboolean changed = FALSE;
2744         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2745         MsgInfo *msginfo;
2746
2747         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2748         if (MSG_IS_DELETED(msginfo->flags))
2749                 summaryview->deleted--;
2750         if (MSG_IS_MOVE(msginfo->flags)) {
2751                 summaryview->moved--;
2752                 changed = TRUE;
2753         }
2754         if (MSG_IS_COPY(msginfo->flags)) {
2755                 summaryview->copied--;
2756                 changed = TRUE;
2757         }
2758         procmsg_msginfo_set_to_folder(msginfo, NULL);
2759         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2760         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2761         summary_set_row_marks(summaryview, row);
2762         debug_print("Message %d is locked\n", msginfo->msgnum);
2763 }
2764
2765 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2766 {
2767         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2768         MsgInfo *msginfo;
2769
2770         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2771         if (!MSG_IS_LOCKED(msginfo->flags))
2772                 return;
2773         procmsg_msginfo_set_to_folder(msginfo, NULL);
2774         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2775         summary_set_row_marks(summaryview, row);
2776         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2777 }
2778
2779 void summary_mark(SummaryView *summaryview)
2780 {
2781         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2782         GList *cur;
2783
2784         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2785                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2786
2787         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2788         summary_status_show(summaryview);
2789 }
2790
2791 static void summary_mark_row_as_read(SummaryView *summaryview,
2792                                      GtkCTreeNode *row)
2793 {
2794         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2795         MsgInfo *msginfo;
2796
2797         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2798
2799         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2800                 return;
2801
2802         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2803         && procmsg_msg_has_marked_parent(msginfo))
2804                 summaryview->unreadmarked--;
2805
2806         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2807         summary_set_row_marks(summaryview, row);
2808         debug_print("Message %d is marked as read\n",
2809                 msginfo->msgnum);
2810 }
2811
2812 void summary_mark_as_read(SummaryView *summaryview)
2813 {
2814         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2815         GList *cur;
2816
2817         folder_item_update_freeze();
2818         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2819         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2820                 summary_mark_row_as_read(summaryview,
2821                                          GTK_CTREE_NODE(cur->data));
2822         folder_item_update_thaw();
2823         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2824
2825         summary_status_show(summaryview);
2826 }
2827
2828 void summary_msgs_lock(SummaryView *summaryview)
2829 {
2830         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2831         GList *cur;
2832
2833         folder_item_update_freeze();
2834         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2835         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2836                 summary_lock_row(summaryview,
2837                                          GTK_CTREE_NODE(cur->data));
2838         folder_item_update_thaw();
2839         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2840         
2841         summary_status_show(summaryview);
2842 }
2843
2844 void summary_msgs_unlock(SummaryView *summaryview)
2845 {
2846         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2847         GList *cur;
2848
2849         folder_item_update_freeze();
2850         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2851         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2852                 summary_unlock_row(summaryview,
2853                                    GTK_CTREE_NODE(cur->data));
2854         folder_item_update_thaw();
2855         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2856         
2857         summary_status_show(summaryview);
2858 }
2859
2860 void summary_mark_all_read(SummaryView *summaryview)
2861 {
2862         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2863         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2864         GtkCTreeNode *node;
2865
2866         gtk_clist_freeze(clist);
2867         folder_item_update_freeze();
2868         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2869              node = gtkut_ctree_node_next(ctree, node))
2870                 summary_mark_row_as_read(summaryview, node);
2871         folder_item_update_thaw();
2872         gtk_clist_thaw(clist);
2873
2874         summary_status_show(summaryview);
2875 }
2876
2877 static void summary_mark_row_as_unread(SummaryView *summaryview,
2878                                        GtkCTreeNode *row)
2879 {
2880         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2881         MsgInfo *msginfo;
2882
2883         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2884         if (MSG_IS_DELETED(msginfo->flags)) {
2885                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2886                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2887                 summaryview->deleted--;
2888         }
2889
2890         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2891         && procmsg_msg_has_marked_parent(msginfo))
2892                 summaryview->unreadmarked++;
2893
2894         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2895         debug_print("Message %d is marked as unread\n",
2896                 msginfo->msgnum);
2897
2898         summary_set_row_marks(summaryview, row);
2899 }
2900
2901 void summary_mark_as_unread(SummaryView *summaryview)
2902 {
2903         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2904         GList *cur;
2905
2906         folder_item_update_freeze();
2907         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2908         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2909                 summary_mark_row_as_unread(summaryview,
2910                                            GTK_CTREE_NODE(cur->data));
2911         folder_item_update_thaw();
2912         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2913         
2914         summary_status_show(summaryview);
2915 }
2916
2917 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2918 {
2919         GList * cur;
2920         gboolean found;
2921
2922         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2923
2924         case F_NEWS:
2925
2926                 /*
2927                   security : checks if one the accounts correspond to
2928                   the author of the post
2929                 */
2930
2931                 found = FALSE;
2932                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2933                         PrefsAccount * account;
2934                         gchar * from_name;
2935                         
2936                         account = cur->data;
2937                         if (account->name && *account->name)
2938                                 from_name =
2939                                         g_strdup_printf("%s <%s>",
2940                                                         account->name,
2941                                                         account->address);
2942                         else
2943                                 from_name =
2944                                         g_strdup_printf("%s",
2945                                                         account->address);
2946                         
2947                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2948                                 g_free(from_name);
2949                                 found = TRUE;
2950                                 break;
2951                         }
2952                         g_free(from_name);
2953                 }
2954
2955                 if (!found) {
2956                         alertpanel_error(_("You're not the author of the article\n"));
2957                 }
2958                 
2959                 return found;
2960
2961         default:
2962                 return TRUE;
2963         }
2964 }
2965
2966 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2967 {
2968         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2969         MsgInfo *msginfo;
2970
2971         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2972
2973         if (!check_permission(summaryview, msginfo))
2974                 return;
2975
2976         if (MSG_IS_LOCKED(msginfo->flags)) return;
2977
2978         if (MSG_IS_DELETED(msginfo->flags)) return;
2979
2980         if (MSG_IS_MOVE(msginfo->flags))
2981                 summaryview->moved--;
2982         if (MSG_IS_COPY(msginfo->flags))
2983                 summaryview->copied--;
2984         procmsg_update_unread_children (msginfo, FALSE);
2985
2986         procmsg_msginfo_set_to_folder(msginfo, NULL);
2987         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2988         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2989         summaryview->deleted++;
2990
2991         if (!prefs_common.immediate_exec && 
2992             summaryview->folder_item->stype != F_TRASH)
2993                 summary_set_row_marks(summaryview, row);
2994
2995         debug_print("Message %s/%d is set to delete\n",
2996                     msginfo->folder->path, msginfo->msgnum);
2997 }
2998
2999 void summary_cancel(SummaryView *summaryview)
3000 {
3001         MsgInfo * msginfo;
3002         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3003
3004         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3005                                               summaryview->selected);
3006         if (!msginfo) return;
3007
3008         if (!check_permission(summaryview, msginfo))
3009                 return;
3010
3011         news_cancel_article(summaryview->folder_item->folder, msginfo);
3012         
3013         if (summary_is_locked(summaryview)) return;
3014
3015         summary_lock(summaryview);
3016
3017         gtk_clist_freeze(clist);
3018
3019         summary_update_status(summaryview);
3020         summary_status_show(summaryview);
3021
3022         gtk_clist_thaw(clist);
3023
3024         summary_unlock(summaryview);
3025 }
3026
3027 void summary_delete(SummaryView *summaryview)
3028 {
3029         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3030         FolderItem *item = summaryview->folder_item;
3031         GList *cur;
3032         GtkCTreeNode *sel_last = NULL;
3033         GtkCTreeNode *node;
3034
3035         if (!item) return;
3036
3037         if (summary_is_locked(summaryview)) return;
3038
3039         /* if current folder is trash, ask for confirmation */
3040         if (item->stype == F_TRASH) {
3041                 AlertValue aval;
3042                 MsgInfo *msginfo;
3043
3044                 aval = alertpanel(_("Delete message(s)"),
3045                                   _("Do you really want to delete message(s) from the trash?"),
3046                                   _("Yes"), _("No"), NULL);
3047                 if (aval != G_ALERTDEFAULT) return;
3048
3049                 for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3050                         GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3051                         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3052                         partial_mark_for_delete(msginfo);
3053                 }
3054         
3055         }
3056
3057         main_window_cursor_wait(summaryview->mainwin);
3058
3059         /* next code sets current row focus right. We need to find a row
3060          * that is not deleted. */
3061         folder_item_update_freeze();
3062         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3063         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
3064                 sel_last = GTK_CTREE_NODE(cur->data);
3065                 summary_delete_row(summaryview, sel_last);
3066         }
3067         folder_item_update_thaw();
3068         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3069
3070         node = summary_find_next_msg(summaryview, sel_last);
3071         if (!node)
3072                 node = summary_find_prev_msg(summaryview, sel_last);
3073
3074         summary_select_node(summaryview, node, FALSE, FALSE);
3075         
3076         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3077                 summary_execute(summaryview);
3078                 /* after deleting, the anchor may be at an invalid row
3079                  * so reset it to the node we found earlier */
3080                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3081         } else
3082                 summary_status_show(summaryview);
3083                 
3084         main_window_cursor_normal(summaryview->mainwin);
3085 }
3086
3087 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3088 {
3089         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3090         MsgInfo *msginfo;
3091
3092         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3093         if (MSG_IS_DELETED(msginfo->flags))
3094                 summaryview->deleted--;
3095         if (MSG_IS_MOVE(msginfo->flags))
3096                 summaryview->moved--;
3097         if (MSG_IS_COPY(msginfo->flags))
3098                 summaryview->copied--;
3099         procmsg_update_unread_children (msginfo, FALSE);
3100
3101         procmsg_msginfo_set_to_folder(msginfo, NULL);
3102         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3103         summary_set_row_marks(summaryview, row);
3104
3105         debug_print("Message %s/%d is unmarked\n",
3106                     msginfo->folder->path, msginfo->msgnum);
3107 }
3108
3109 void summary_unmark(SummaryView *summaryview)
3110 {
3111         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3112         GList *cur;
3113
3114         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
3115                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3116
3117         summary_status_show(summaryview);
3118 }
3119
3120 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3121                                 FolderItem *to_folder)
3122 {
3123         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3124         MsgInfo *msginfo;
3125
3126         g_return_if_fail(to_folder != NULL);
3127
3128         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3129         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3130         if (MSG_IS_DELETED(msginfo->flags))
3131                 summaryview->deleted--;
3132         if (MSG_IS_COPY(msginfo->flags)) {
3133                 summaryview->copied--;
3134         }
3135         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3136         if (!MSG_IS_MOVE(msginfo->flags)) {
3137                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3138                 summaryview->moved++;
3139         }
3140         if (!prefs_common.immediate_exec) {
3141                 summary_set_row_marks(summaryview, row);
3142         }
3143
3144         debug_print("Message %d is set to move to %s\n",
3145                     msginfo->msgnum, to_folder->path);
3146 }
3147
3148 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3149 {
3150         GList *cur;
3151
3152         if (!to_folder) return;
3153         if (!summaryview->folder_item ||
3154             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3155
3156         if (summary_is_locked(summaryview)) return;
3157
3158         if (summaryview->folder_item == to_folder) {
3159                 alertpanel_error(_("Destination is same as current folder."));
3160                 return;
3161         }
3162
3163         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3164              cur != NULL; cur = cur->next)
3165                 summary_move_row_to
3166                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3167
3168         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3169
3170         if (prefs_common.immediate_exec)
3171                 summary_execute(summaryview);
3172         else {
3173                 summary_status_show(summaryview);
3174         }
3175         
3176         if (!summaryview->selected) { /* this was the last message */
3177                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3178                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3179                 if (node)
3180                         summary_select_node(summaryview, node, TRUE, TRUE);
3181         }
3182
3183 }
3184
3185 void summary_move_to(SummaryView *summaryview)
3186 {
3187         FolderItem *to_folder;
3188
3189         if (!summaryview->folder_item ||
3190             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3191
3192         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3193                                          FOLDER_SEL_MOVE, NULL);
3194         summary_move_selected_to(summaryview, to_folder);
3195 }
3196
3197 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3198                                 FolderItem *to_folder)
3199 {
3200         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3201         MsgInfo *msginfo;
3202
3203         g_return_if_fail(to_folder != NULL);
3204
3205         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3206         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3207         if (MSG_IS_DELETED(msginfo->flags))
3208                 summaryview->deleted--;
3209         if (MSG_IS_MOVE(msginfo->flags)) {
3210                 summaryview->moved--;
3211         }
3212         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3213         if (!MSG_IS_COPY(msginfo->flags)) {
3214                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3215                 summaryview->copied++;
3216         }
3217         if (!prefs_common.immediate_exec) {
3218                 summary_set_row_marks(summaryview, row);
3219         }
3220
3221         debug_print("Message %d is set to copy to %s\n",
3222                     msginfo->msgnum, to_folder->path);
3223 }
3224
3225 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3226 {
3227         GList *cur;
3228
3229         if (!to_folder) return;
3230         if (!summaryview->folder_item) return;
3231
3232         if (summary_is_locked(summaryview)) return;
3233
3234         if (summaryview->folder_item == to_folder) {
3235                 alertpanel_error
3236                         (_("Destination to copy is same as current folder."));
3237                 return;
3238         }
3239
3240         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3241              cur != NULL; cur = cur->next)
3242                 summary_copy_row_to
3243                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3244
3245         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3246
3247         if (prefs_common.immediate_exec)
3248                 summary_execute(summaryview);
3249         else {
3250                 summary_status_show(summaryview);
3251         }
3252 }
3253
3254 void summary_copy_to(SummaryView *summaryview)
3255 {
3256         FolderItem *to_folder;
3257
3258         if (!summaryview->folder_item) return;
3259
3260         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3261                                          FOLDER_SEL_COPY, NULL);
3262         summary_copy_selected_to(summaryview, to_folder);
3263 }
3264
3265 void summary_add_address(SummaryView *summaryview)
3266 {
3267         MsgInfo *msginfo;
3268         gchar *from;
3269
3270         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3271                                               summaryview->selected);
3272         if (!msginfo) return;
3273
3274         Xstrdup_a(from, msginfo->from, return);
3275         eliminate_address_comment(from);
3276         extract_address(from);
3277         addressbook_add_contact(msginfo->fromname, from, NULL);
3278 }
3279
3280 void summary_select_all(SummaryView *summaryview)
3281 {
3282         if (!summaryview->folder_item) return;
3283
3284         if (summaryview->folder_item->total_msgs >= 500) {
3285                 STATUSBAR_PUSH(summaryview->mainwin,
3286                                _("Selecting all messages..."));
3287                 main_window_cursor_wait(summaryview->mainwin);
3288         }
3289
3290         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3291
3292         if (summaryview->folder_item->total_msgs >= 500) {
3293                 STATUSBAR_POP(summaryview->mainwin);
3294                 main_window_cursor_normal(summaryview->mainwin);
3295         }
3296 }
3297
3298 void summary_unselect_all(SummaryView *summaryview)
3299 {
3300         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3301 }
3302
3303 void summary_select_thread(SummaryView *summaryview)
3304 {
3305         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3306         GtkCTreeNode *node = summaryview->selected;
3307
3308         if (!node) return;
3309
3310         while (GTK_CTREE_ROW(node)->parent != NULL)
3311               &nb