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