0ba613a926215745f664e432f6dab783f9329a47
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkpixmap.h>
28 #include <gtk/gtkctree.h>
29 #include <gtk/gtkcontainer.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkitemfactory.h>
35 #include <gtk/gtkvbox.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkwindow.h>
38 #include <gtk/gtkstyle.h>
39 #include <gtk/gtkarrow.h>
40 #include <gtk/gtkeventbox.h>
41 #include <gtk/gtkstatusbar.h>
42 #include <gtk/gtkmenuitem.h>
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48
49 #include "main.h"
50 #include "menu.h"
51 #include "mbox.h"
52 #include "mainwindow.h"
53 #include "folderview.h"
54 #include "summaryview.h"
55 #include "messageview.h"
56 #include "foldersel.h"
57 #include "procmsg.h"
58 #include "procheader.h"
59 #include "sourcewindow.h"
60 #include "prefs_common.h"
61 #include "prefs_summary_column.h"
62 #include "prefs_filtering.h"
63 #include "account.h"
64 #include "compose.h"
65 #include "utils.h"
66 #include "gtkutils.h"
67 #include "stock_pixmap.h"
68 #include "filesel.h"
69 #include "alertpanel.h"
70 #include "inputdialog.h"
71 #include "statusbar.h"
72 #include "folder.h"
73 #include "colorlabel.h"
74 #include "inc.h"
75 #include "imap.h"
76 #include "addressbook.h"
77 #include "addr_compl.h"
78 #include "folder_item_prefs.h"
79 #include "filtering.h"
80 #include "string_match.h"
81 #include "toolbar.h"
82 #include "news.h"
83 #include "hooks.h"
84 #include "description_window.h"
85 #include "folderutils.h"
86 #include "quicksearch.h"
87 #include "partial_download.h"
88 #include "timing.h"
89 #include "gedit-print.h"
90
91 #define SUMMARY_COL_MARK_WIDTH          10
92 #define SUMMARY_COL_STATUS_WIDTH        13
93 #define SUMMARY_COL_LOCKED_WIDTH        13
94 #define SUMMARY_COL_MIME_WIDTH          11
95
96
97 static GtkStyle *bold_style;
98 static GtkStyle *bold_marked_style;
99 static GtkStyle *bold_deleted_style;
100 static GtkStyle *small_style;
101 static GtkStyle *small_marked_style;
102 static GtkStyle *small_deleted_style;
103
104 static GdkPixmap *markxpm;
105 static GdkBitmap *markxpmmask;
106 static GdkPixmap *deletedxpm;
107 static GdkBitmap *deletedxpmmask;
108
109 static GdkPixmap *newxpm;
110 static GdkBitmap *newxpmmask;
111 static GdkPixmap *unreadxpm;
112 static GdkBitmap *unreadxpmmask;
113 static GdkPixmap *repliedxpm;
114 static GdkBitmap *repliedxpmmask;
115 static GdkPixmap *forwardedxpm;
116 static GdkBitmap *forwardedxpmmask;
117 static GdkPixmap *ignorethreadxpm;
118 static GdkBitmap *ignorethreadxpmmask;
119 static GdkPixmap *lockedxpm;
120 static GdkBitmap *lockedxpmmask;
121
122 static GdkPixmap *clipxpm;
123 static GdkBitmap *clipxpmmask;
124 static GdkPixmap *keyxpm;
125 static GdkBitmap *keyxpmmask;
126 static GdkPixmap *clipkeyxpm;
127 static GdkBitmap *clipkeyxpmmask;
128 static GdkPixmap *gpgsignedxpm;
129 static GdkBitmap *gpgsignedxpmmask;
130 static GdkPixmap *clipgpgsignedxpm;
131 static GdkBitmap *clipgpgsignedxpmmask;
132
133 static void summary_free_msginfo_func   (GtkCTree               *ctree,
134                                          GtkCTreeNode           *node,
135                                          gpointer                data);
136 static void summary_set_marks_func      (GtkCTree               *ctree,
137                                          GtkCTreeNode           *node,
138                                          gpointer                data);
139
140 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
141
142 static void summary_set_hide_read_msgs_menu (SummaryView *summaryview,
143                                              guint action);
144
145 static guint summary_get_msgnum         (SummaryView            *summaryview,
146                                          GtkCTreeNode           *node);
147
148 static GtkCTreeNode *summary_find_prev_msg
149                                         (SummaryView            *summaryview,
150                                          GtkCTreeNode           *current_node);
151 static GtkCTreeNode *summary_find_next_msg
152                                         (SummaryView            *summaryview,
153                                          GtkCTreeNode           *current_node);
154
155 static GtkCTreeNode *summary_find_prev_flagged_msg
156                                         (SummaryView    *summaryview,
157                                          GtkCTreeNode   *current_node,
158                                          MsgPermFlags    flags,
159                                          gboolean        start_from_prev);
160 static GtkCTreeNode *summary_find_next_flagged_msg
161                                         (SummaryView    *summaryview,
162                                          GtkCTreeNode   *current_node,
163                                          MsgPermFlags    flags,
164                                          gboolean        start_from_next);
165
166 static GtkCTreeNode *summary_find_msg_by_msgnum
167                                         (SummaryView            *summaryview,
168                                          guint                   msgnum);
169
170 static void summary_update_status       (SummaryView            *summaryview);
171
172 /* display functions */
173 static void summary_status_show         (SummaryView            *summaryview);
174 static void summary_set_column_titles   (SummaryView            *summaryview);
175 static void summary_set_ctree_from_list (SummaryView            *summaryview,
176                                          GSList                 *mlist);
177 static void summary_set_header          (SummaryView            *summaryview,
178                                          gchar                  *text[],
179                                          MsgInfo                *msginfo);
180 static void summary_display_msg         (SummaryView            *summaryview,
181                                          GtkCTreeNode           *row);
182 static void summary_display_msg_full    (SummaryView            *summaryview,
183                                          GtkCTreeNode           *row,
184                                          gboolean                new_window,
185                                          gboolean                all_headers);
186 static void summary_set_row_marks       (SummaryView            *summaryview,
187                                          GtkCTreeNode           *row);
188
189 /* message handling */
190 static void summary_mark_row            (SummaryView            *summaryview,
191                                          GtkCTreeNode           *row);
192 static void summary_lock_row            (SummaryView            *summaryview,
193                                          GtkCTreeNode           *row);
194 static void summary_unlock_row          (SummaryView            *summaryview,
195                                          GtkCTreeNode           *row);
196 static void summary_mark_row_as_read    (SummaryView            *summaryview,
197                                          GtkCTreeNode           *row);
198 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
199                                          GtkCTreeNode           *row);
200 static void summary_delete_row          (SummaryView            *summaryview,
201                                          GtkCTreeNode           *row);
202 static void summary_unmark_row          (SummaryView            *summaryview,
203                                          GtkCTreeNode           *row);
204 static void summary_move_row_to         (SummaryView            *summaryview,
205                                          GtkCTreeNode           *row,
206                                          FolderItem             *to_folder);
207 static void summary_copy_row_to         (SummaryView            *summaryview,
208                                          GtkCTreeNode           *row,
209                                          FolderItem             *to_folder);
210
211 static gint summary_execute_move        (SummaryView            *summaryview);
212 static void summary_execute_move_func   (GtkCTree               *ctree,
213                                          GtkCTreeNode           *node,
214                                          gpointer                data);
215 static void summary_execute_copy        (SummaryView            *summaryview);
216 static void summary_execute_copy_func   (GtkCTree               *ctree,
217                                          GtkCTreeNode           *node,
218                                          gpointer                data);
219 static void summary_execute_delete      (SummaryView            *summaryview);
220 static void summary_execute_delete_func (GtkCTree               *ctree,
221                                          GtkCTreeNode           *node,
222                                          gpointer                data);
223
224 static void summary_thread_init         (SummaryView            *summaryview);
225 static void summary_ignore_thread       (SummaryView            *summaryview);
226 static void summary_unignore_thread     (SummaryView            *summaryview);
227
228 static void summary_unthread_for_exec           (SummaryView    *summaryview);
229 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
230                                                  GtkCTreeNode   *node,
231                                                  gpointer        data);
232
233 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
234                               GSList * mlist);
235
236 #if 0
237 void summary_processing(SummaryView *summaryview, GSList * mlist);
238 #endif
239 static void summary_filter_func         (GtkCTree               *ctree,
240                                          GtkCTreeNode           *node,
241                                          gpointer                data);
242
243 static void summary_colorlabel_menu_item_activate_cb
244                                           (GtkWidget    *widget,
245                                            gpointer      data);
246 static void summary_colorlabel_menu_item_activate_item_cb
247                                           (GtkMenuItem  *label_menu_item,
248                                            gpointer      data);
249 static void summary_colorlabel_menu_create(SummaryView  *summaryview);
250
251 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
252
253 /* callback functions */
254 static gint summary_toggle_pressed      (GtkWidget              *eventbox,
255                                          GdkEventButton         *event,
256                                          SummaryView            *summaryview);
257 static gboolean summary_button_pressed  (GtkWidget              *ctree,
258                                          GdkEventButton         *event,
259                                          SummaryView            *summaryview);
260 static gboolean summary_button_released (GtkWidget              *ctree,
261                                          GdkEventButton         *event,
262                                          SummaryView            *summaryview);
263 static gboolean summary_key_pressed     (GtkWidget              *ctree,
264                                          GdkEventKey            *event,
265                                          SummaryView            *summaryview);
266 static void summary_open_row            (GtkSCTree              *sctree,
267                                          SummaryView            *summaryview);
268 static void summary_tree_expanded       (GtkCTree               *ctree,
269                                          GtkCTreeNode           *node,
270                                          SummaryView            *summaryview);
271 static void summary_tree_collapsed      (GtkCTree               *ctree,
272                                          GtkCTreeNode           *node,
273                                          SummaryView            *summaryview);
274 static void summary_selected            (GtkCTree               *ctree,
275                                          GtkCTreeNode           *row,
276                                          gint                    column,
277                                          SummaryView            *summaryview);
278 static void summary_col_resized         (GtkCList               *clist,
279                                          gint                    column,
280                                          gint                    width,
281                                          SummaryView            *summaryview);
282 static void summary_reply_cb            (SummaryView            *summaryview,
283                                          guint                   action,
284                                          GtkWidget              *widget);
285 static void summary_show_all_header_cb  (SummaryView            *summaryview,
286                                          guint                   action,
287                                          GtkWidget              *widget);
288
289 static void summary_add_address_cb      (SummaryView            *summaryview,
290                                          guint                   action,
291                                          GtkWidget              *widget);
292 static void summary_create_filter_cb    (SummaryView            *summaryview,
293                                          guint                   action,
294                                          GtkWidget              *widget);
295 static void summary_create_processing_cb(SummaryView            *summaryview,
296                                          guint                   action,
297                                          GtkWidget              *widget);
298
299 static void summary_mark_clicked        (GtkWidget              *button,
300                                          SummaryView            *summaryview);
301 static void summary_status_clicked      (GtkWidget              *button,
302                                          SummaryView            *summaryview);
303 static void summary_mime_clicked        (GtkWidget              *button,
304                                          SummaryView            *summaryview);
305 static void summary_num_clicked         (GtkWidget              *button,
306                                          SummaryView            *summaryview);
307 static void summary_score_clicked       (GtkWidget *button,
308                                          SummaryView *summaryview);
309 static void summary_size_clicked        (GtkWidget              *button,
310                                          SummaryView            *summaryview);
311 static void summary_date_clicked        (GtkWidget              *button,
312                                          SummaryView            *summaryview);
313 static void summary_from_clicked        (GtkWidget              *button,
314                                          SummaryView            *summaryview);
315 static void summary_to_clicked          (GtkWidget              *button,
316                                          SummaryView            *summaryview);
317 static void summary_subject_clicked     (GtkWidget              *button,
318                                          SummaryView            *summaryview);
319 static void summary_score_clicked       (GtkWidget              *button,
320                                          SummaryView            *summaryview);
321 static void summary_locked_clicked      (GtkWidget              *button,
322                                          SummaryView            *summaryview);
323
324 static void summary_start_drag          (GtkWidget        *widget, 
325                                          int button,
326                                          GdkEvent *event,
327                                          SummaryView      *summaryview);
328 static void summary_drag_data_get       (GtkWidget        *widget,
329                                          GdkDragContext   *drag_context,
330                                          GtkSelectionData *selection_data,
331                                          guint             info,
332                                          guint             time,
333                                          SummaryView      *summaryview);
334
335 /* custom compare functions for sorting */
336
337 static gint summary_cmp_by_mark         (GtkCList               *clist,
338                                          gconstpointer           ptr1,
339                                          gconstpointer           ptr2);
340 static gint summary_cmp_by_status       (GtkCList               *clist,
341                                          gconstpointer           ptr1,
342                                          gconstpointer           ptr2);
343 static gint summary_cmp_by_mime         (GtkCList               *clist,
344                                          gconstpointer           ptr1,
345                                          gconstpointer           ptr2);
346 static gint summary_cmp_by_num          (GtkCList               *clist,
347                                          gconstpointer           ptr1,
348                                          gconstpointer           ptr2);
349 static gint summary_cmp_by_size         (GtkCList               *clist,
350                                          gconstpointer           ptr1,
351                                          gconstpointer           ptr2);
352 static gint summary_cmp_by_date         (GtkCList               *clist,
353                                          gconstpointer           ptr1,
354                                          gconstpointer           ptr2);
355 static gint summary_cmp_by_from         (GtkCList               *clist,
356                                          gconstpointer           ptr1,
357                                          gconstpointer           ptr2);
358 static gint summary_cmp_by_simplified_subject
359                                         (GtkCList               *clist, 
360                                          gconstpointer           ptr1, 
361                                          gconstpointer           ptr2);
362 static gint summary_cmp_by_score        (GtkCList               *clist,
363                                          gconstpointer           ptr1,
364                                          gconstpointer           ptr2);
365 static gint summary_cmp_by_label        (GtkCList               *clist,
366                                          gconstpointer           ptr1,
367                                          gconstpointer           ptr2);
368 static gint summary_cmp_by_to           (GtkCList               *clist,
369                                          gconstpointer           ptr1,
370                                          gconstpointer           ptr2);
371 static gint summary_cmp_by_subject      (GtkCList               *clist,
372                                          gconstpointer           ptr1,
373                                          gconstpointer           ptr2);
374 static gint summary_cmp_by_locked       (GtkCList               *clist,
375                                          gconstpointer           ptr1, 
376                                          gconstpointer           ptr2);
377
378 static void news_flag_crosspost         (MsgInfo *msginfo);
379
380 static void quicksearch_execute_cb      (QuickSearch    *quicksearch,
381                                          gpointer        data);
382 static void tog_searchbar_cb            (GtkWidget      *w,
383                                          gpointer        data);
384
385 static void summary_find_answers        (SummaryView    *summaryview, 
386                                          MsgInfo        *msg);
387
388 static gboolean summary_update_msg      (gpointer source, gpointer data);
389
390 GtkTargetEntry summary_drag_types[1] =
391 {
392         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
393 };
394
395 static GtkItemFactoryEntry summary_popup_entries[] =
396 {
397         {N_("/_Reply"),                 "<control>R", summary_reply_cb, COMPOSE_REPLY, NULL},
398         {N_("/Repl_y to"),              NULL, NULL,             0, "<Branch>"},
399         {N_("/Repl_y to/_all"),         "<shift><control>R", summary_reply_cb,  COMPOSE_REPLY_TO_ALL, NULL},
400         {N_("/Repl_y to/_sender"),      NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
401         {N_("/Repl_y to/mailing _list"),
402                                         "<control>L", summary_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
403         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
404         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
405         {N_("/_Forward"),               "<control><alt>F", summary_reply_cb, COMPOSE_FORWARD, NULL},
406         {N_("/Redirect"),               NULL, summary_reply_cb, COMPOSE_REDIRECT, NULL},
407         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
408         {N_("/M_ove..."),               "<control>O", summary_move_to,  0, NULL},
409         {N_("/_Copy..."),               "<shift><control>O", summary_copy_to,   0, NULL},
410         {N_("/Move to _trash"),         "<control>D", summary_delete_trash,     0, NULL},
411         {N_("/_Delete..."),             NULL, summary_delete, 0, NULL},
412         {N_("/Cancel a news message"),  NULL, summary_cancel,   0, NULL},
413         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
414         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
415         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
416         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
417         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
418         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
419         {N_("/_Mark/Mark as rea_d"),    NULL, summary_mark_as_read, 0, NULL},
420         {N_("/_Mark/Mark all read"),    NULL, summary_mark_all_read, 0, NULL},
421         {N_("/_Mark/Ignore thread"),    NULL, summary_ignore_thread, 0, NULL},
422         {N_("/_Mark/Unignore thread"),  NULL, summary_unignore_thread, 0, NULL},
423         {N_("/_Mark/Lock"),             NULL, summary_msgs_lock, 0, NULL},
424         {N_("/_Mark/Unlock"),           NULL, summary_msgs_unlock, 0, NULL},
425         {N_("/Color la_bel"),           NULL, NULL,             0, NULL},
426
427         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
428         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
429         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
430         {N_("/Add sender to address boo_k"),
431                                         NULL, summary_add_address_cb, 0, NULL},
432         {N_("/Create f_ilter rule"),    NULL, NULL,             0, "<Branch>"},
433         {N_("/Create f_ilter rule/_Automatically"),
434                                         NULL, summary_create_filter_cb, FILTER_BY_AUTO, NULL},
435         {N_("/Create f_ilter rule/by _From"),
436                                         NULL, summary_create_filter_cb, FILTER_BY_FROM, NULL},
437         {N_("/Create f_ilter rule/by _To"),
438                                         NULL, summary_create_filter_cb, FILTER_BY_TO, NULL},
439         {N_("/Create f_ilter rule/by _Subject"),
440                                         NULL, summary_create_filter_cb, FILTER_BY_SUBJECT, NULL},
441         {N_("/Create processing rule"), NULL, NULL,             0, "<Branch>"},
442         {N_("/Create processing rule/_Automatically"),
443                                         NULL, summary_create_processing_cb, FILTER_BY_AUTO, NULL},
444         {N_("/Create processing rule/by _From"),
445                                         NULL, summary_create_processing_cb, FILTER_BY_FROM, NULL},
446         {N_("/Create processing rule/by _To"),
447                                         NULL, summary_create_processing_cb, FILTER_BY_TO, NULL},
448         {N_("/Create processing rule/by _Subject"),
449                                         NULL, summary_create_processing_cb, FILTER_BY_SUBJECT, NULL},
450         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
451         {N_("/_View"),                  NULL, NULL,             0, "<Branch>"},
452         {N_("/_View/Open in new _window"),
453                                         "<control><alt>N", summary_open_msg,    0, NULL},
454         {N_("/_View/_Source"),          "<control>U", summary_view_source, 0, NULL},
455         {N_("/_View/All _header"),      "<control>H", summary_show_all_header_cb, 0, "<ToggleItem>"},
456 };  /* see also list in menu_connect_identical_items() in menu.c if this changes */
457
458 static const gchar *const col_label[N_SUMMARY_COLS] = {
459         N_("M"),        /* S_COL_MARK    */
460         N_("S"),        /* S_COL_STATUS  */
461         "",             /* S_COL_MIME    */
462         N_("Subject"),  /* S_COL_SUBJECT */
463         N_("From"),     /* S_COL_FROM    */
464         N_("To"),       /* S_COL_TO      */
465         N_("Date"),     /* S_COL_DATE    */
466         N_("Size"),     /* S_COL_SIZE    */
467         N_("No."),      /* S_COL_NUMBER  */
468         N_("Score"),    /* S_COL_SCORE   */
469         N_("L")         /* S_COL_LOCKED  */
470 };
471
472 #define START_LONG_OPERATION(summaryview) {                     \
473         summary_lock(summaryview);                              \
474         main_window_cursor_wait(summaryview->mainwin);          \
475         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));        \
476         folder_item_update_freeze();                            \
477         inc_lock();                                             \
478 }
479 #define END_LONG_OPERATION(summaryview) {                       \
480         inc_unlock();                                           \
481         folder_item_update_thaw();                              \
482         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));          \
483         main_window_cursor_normal(summaryview->mainwin);        \
484         summary_unlock(summaryview);                            \
485 }
486
487 SummaryView *summary_create(void)
488 {
489         SummaryView *summaryview;
490         GtkWidget *vbox;
491         GtkWidget *scrolledwin;
492         GtkWidget *ctree;
493         GtkWidget *hbox;
494         GtkWidget *hbox_l;
495         GtkWidget *statlabel_folder;
496         GtkWidget *statlabel_select;
497         GtkWidget *statlabel_msgs;
498         GtkWidget *hbox_spc;
499         GtkWidget *toggle_eventbox;
500         GtkWidget *toggle_arrow;
501         GtkWidget *popupmenu;
502         GtkWidget *toggle_search;
503         GtkTooltips *search_tip;
504         GtkItemFactory *popupfactory;
505         gint n_entries;
506         QuickSearch *quicksearch;
507
508         debug_print("Creating summary view...\n");
509         summaryview = g_new0(SummaryView, 1);
510
511 #define SUMMARY_VBOX_SPACING 3
512         vbox = gtk_vbox_new(FALSE, SUMMARY_VBOX_SPACING);
513         
514         /* create status label */
515         hbox = gtk_hbox_new(FALSE, 0);
516         gtk_widget_show(hbox);
517
518         search_tip = gtk_tooltips_new();
519         toggle_search = gtk_toggle_button_new();
520         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_search),
521                                      prefs_common.show_searchbar);
522         gtk_widget_show(toggle_search);
523
524         gtk_tooltips_set_tip(GTK_TOOLTIPS(search_tip),
525                              toggle_search,
526                              _("Toggle quick-search bar"), NULL);
527         
528         gtk_box_pack_start(GTK_BOX(hbox), toggle_search, FALSE, FALSE, 2);      
529
530         hbox_l = gtk_hbox_new(FALSE, 0);
531         gtk_widget_show(hbox_l);
532         gtk_box_pack_start(GTK_BOX(hbox), hbox_l, TRUE, TRUE, 0);
533  
534         statlabel_folder = gtk_label_new("");
535         gtk_widget_show(statlabel_folder);
536         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
537         statlabel_select = gtk_label_new("");
538         gtk_widget_show(statlabel_select);
539         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
540  
541         /* toggle view button */
542         toggle_eventbox = gtk_event_box_new();
543         gtk_widget_show(toggle_eventbox);
544         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
545         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
546         gtk_widget_show(toggle_arrow);
547         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
548         g_signal_connect(G_OBJECT(toggle_eventbox), "button_press_event",
549                          G_CALLBACK(summary_toggle_pressed),
550                          summaryview);
551         
552         
553         statlabel_msgs = gtk_label_new("");
554         gtk_widget_show(statlabel_msgs);
555         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
556
557         hbox_spc = gtk_hbox_new(FALSE, 0);
558         gtk_widget_show(hbox_spc);
559         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
560
561         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
562         gtk_widget_show(scrolledwin);
563         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
564                                        GTK_POLICY_AUTOMATIC,
565                                        GTK_POLICY_AUTOMATIC);
566         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
567         gtk_widget_set_size_request(vbox,
568                              prefs_common.summaryview_width,
569                              prefs_common.summaryview_height);
570
571         ctree = summary_ctree_create(summaryview);
572         gtk_widget_show(ctree);
573
574         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
575                                             GTK_CLIST(ctree)->hadjustment);
576         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
577                                             GTK_CLIST(ctree)->vadjustment);
578         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
579
580         /* status label */
581         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
582
583         /* quick search */
584         quicksearch = quicksearch_new();
585         gtk_box_pack_start(GTK_BOX(vbox), quicksearch_get_widget(quicksearch), FALSE, FALSE, 0);
586
587         quicksearch_set_execute_callback(quicksearch, quicksearch_execute_cb, summaryview);
588
589         g_signal_connect (G_OBJECT(toggle_search), "toggled",
590                           G_CALLBACK(tog_searchbar_cb), summaryview);
591
592         /* create popup menu */
593         n_entries = sizeof(summary_popup_entries) /
594                 sizeof(summary_popup_entries[0]);
595         popupmenu = menu_create_items(summary_popup_entries, n_entries,
596                                       "<SummaryView>", &popupfactory,
597                                       summaryview);
598
599         summaryview->vbox = vbox;
600         summaryview->scrolledwin = scrolledwin;
601         summaryview->ctree = ctree;
602         summaryview->hbox = hbox;
603         summaryview->hbox_l = hbox_l;
604         summaryview->statlabel_folder = statlabel_folder;
605         summaryview->statlabel_select = statlabel_select;
606         summaryview->statlabel_msgs = statlabel_msgs;
607         summaryview->toggle_eventbox = toggle_eventbox;
608         summaryview->toggle_arrow = toggle_arrow;
609         summaryview->toggle_search = toggle_search;
610         summaryview->popupmenu = popupmenu;
611         summaryview->popupfactory = popupfactory;
612         summaryview->lock_count = 0;
613         summaryview->msginfo_update_callback_id =
614                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
615
616         summaryview->target_list = gtk_target_list_new(summary_drag_types, 1);
617
618         summaryview->quicksearch = quicksearch;
619
620         /* CLAWS: need this to get the SummaryView * from
621          * the CList */
622         g_object_set_data(G_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
623
624         gtk_widget_show_all(vbox);
625
626         gtk_widget_show(vbox);
627
628         if (prefs_common.show_searchbar)
629                 quicksearch_show(quicksearch);
630         else
631                 quicksearch_hide(quicksearch);
632         
633         return summaryview;
634 }
635
636 static void summary_set_fonts(SummaryView *summaryview)
637 {
638         PangoFontDescription *font_desc;
639         gint size;
640
641         font_desc = pango_font_description_from_string(NORMAL_FONT);
642         gtk_widget_modify_font(summaryview->ctree, font_desc);
643         pango_font_description_free(font_desc);
644
645         if (!bold_style) {
646                 bold_style = gtk_style_copy
647                         (gtk_widget_get_style(summaryview->ctree));
648                 font_desc = pango_font_description_from_string(NORMAL_FONT);
649                 if (font_desc) {
650                         pango_font_description_free(bold_style->font_desc);
651                         bold_style->font_desc = font_desc;
652                 }
653                 
654                 pango_font_description_set_weight
655                                 (bold_style->font_desc, PANGO_WEIGHT_BOLD);
656                 bold_marked_style = gtk_style_copy(bold_style);
657                 bold_marked_style->fg[GTK_STATE_NORMAL] =
658                         summaryview->color_marked;
659                 bold_deleted_style = gtk_style_copy(bold_style);
660                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
661                         summaryview->color_dim;
662         }
663
664         font_desc = pango_font_description_new();
665         size = pango_font_description_get_size
666                 (summaryview->ctree->style->font_desc);
667         pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
668         gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
669         gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
670         gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
671         pango_font_description_free(font_desc);
672 }
673
674 void summary_init(SummaryView *summaryview)
675 {
676         GtkWidget *pixmap;
677
678         gtk_widget_realize(summaryview->ctree);
679         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
680                          &markxpm, &markxpmmask);
681         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
682                          &deletedxpm, &deletedxpmmask);
683         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
684                          &newxpm, &newxpmmask);
685         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
686                          &unreadxpm, &unreadxpmmask);
687         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
688                          &repliedxpm, &repliedxpmmask);
689         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
690                          &forwardedxpm, &forwardedxpmmask);
691         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
692                          &clipxpm, &clipxpmmask);
693         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
694                          &lockedxpm, &lockedxpmmask);
695         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
696                          &ignorethreadxpm, &ignorethreadxpmmask);
697         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
698                          &clipkeyxpm, &clipkeyxpmmask);
699         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
700                          &keyxpm, &keyxpmmask);
701         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_GPG_SIGNED,
702                          &gpgsignedxpm, &gpgsignedxpmmask);
703         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_GPG_SIGNED,
704                          &clipgpgsignedxpm, &clipgpgsignedxpmmask);
705
706         summary_set_fonts(summaryview);
707
708         pixmap = stock_pixmap_widget(summaryview->hbox_l, STOCK_PIXMAP_DIR_OPEN);
709         gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), pixmap, FALSE, FALSE, 4);
710         gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), pixmap, 0);
711         gtk_widget_show(pixmap);
712         summaryview->folder_pixmap = pixmap;
713
714         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
715         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
716         gtk_widget_show(pixmap);
717         summaryview->quick_search_pixmap = pixmap;
718
719         /* Init summaryview prefs */
720         summaryview->sort_key = SORT_BY_NONE;
721         summaryview->sort_type = SORT_ASCENDING;
722
723         /* Init summaryview extra data */
724         summaryview->simplify_subject_preg = NULL;
725
726         summary_clear_list(summaryview);
727         summary_set_column_titles(summaryview);
728         summary_colorlabel_menu_create(summaryview);
729         summary_set_menu_sensitive(summaryview);
730
731 }
732
733 #if 0
734 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
735                                                  GtkCTreeNode *current_node)
736 {
737         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
738         GtkCTreeNode *node;
739         MsgInfo *msginfo;
740         gint best_score = MIN_SCORE;
741         GtkCTreeNode *best_node = NULL;
742
743         if (current_node)
744                 /*node = current_node;*/
745                 node = GTK_CTREE_NODE_NEXT(current_node);
746         else
747                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
748
749         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
750                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
751                 if (msginfo->score >= summaryview->important_score)
752                         break;
753                 if (msginfo->score > best_score) {
754                         best_score = msginfo->score;
755                         best_node = node;
756                 }
757         }
758
759         if (node != NULL)
760                 return node;
761         else
762                 return best_node;
763 }
764
765 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
766                                                  GtkCTreeNode *current_node)
767 {
768         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
769         GtkCTreeNode *node;
770         MsgInfo *msginfo;
771         gint best_score = MIN_SCORE;
772         GtkCTreeNode *best_node = NULL;
773
774         if (current_node)
775                 /*node = current_node;*/
776                 node = GTK_CTREE_NODE_PREV(current_node);
777         else
778                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
779
780         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
781                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
782                 if (msginfo->score >= summaryview->important_score)
783                         break;
784                 if (msginfo->score > best_score) {
785                         best_score = msginfo->score;
786                         best_node = node;
787                 }
788         }
789
790         if (node != NULL)
791                 return node;
792         else
793                 return best_node;
794 }
795 #endif
796
797 #define CURRENTLY_DISPLAYED(m) \
798 ( (m->msgnum == displayed_msgnum) \
799   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
800
801 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
802 {
803         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
804         GtkCTreeNode *node;
805         GSList *mlist = NULL;
806         gchar *buf;
807         gboolean is_refresh;
808         guint selected_msgnum = 0;
809         guint displayed_msgnum = 0;
810         GSList *cur;
811         GSList *not_killed;
812
813         if (summary_is_locked(summaryview)) return FALSE;
814
815         inc_lock();
816         summary_lock(summaryview);
817
818         if (!prefs_common.summary_quicksearch_sticky
819          && !prefs_common.summary_quicksearch_recurse
820          && !quicksearch_is_running(summaryview->quicksearch)) {
821                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
822         }
823
824         /* STATUSBAR_POP(summaryview->mainwin); */
825
826         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
827
828         if (is_refresh) {
829                 selected_msgnum = summary_get_msgnum(summaryview,
830                                                      summaryview->selected);
831                 displayed_msgnum = summary_get_msgnum(summaryview,
832                                                       summaryview->displayed);
833         }
834
835         /* process the marks if any */
836         if (summaryview->mainwin->lock_count == 0 &&
837             (summaryview->moved > 0 || summaryview->copied > 0)) {
838                 AlertValue val;
839                 gboolean changed = FALSE;
840
841                 val = alertpanel(_("Process mark"),
842                                  _("Some marks are left. Process it?"),
843                                  GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL);
844                 if (G_ALERTDEFAULT == val) {
845                         summary_unlock(summaryview);
846                         summary_execute(summaryview);
847                         summary_lock(summaryview);
848                         changed = TRUE;
849                 } else if (G_ALERTALTERNATE == val) {
850                         /* DO NOTHING */
851                 } else {
852                         summary_unlock(summaryview);
853                         inc_unlock();
854                         return FALSE;
855                 }
856                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
857                         folder_update_op_count();
858         }
859         
860         gtk_clist_freeze(GTK_CLIST(ctree));
861
862         summary_clear_list(summaryview);
863         summary_set_column_titles(summaryview);
864
865         buf = NULL;
866         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
867                 g_free(buf);
868                 debug_print("empty folder\n\n");
869                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
870                 summary_clear_all(summaryview);
871                 summaryview->folder_item = item;
872                 gtk_clist_thaw(GTK_CLIST(ctree));
873                 summary_unlock(summaryview);
874                 inc_unlock();
875                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
876                         main_window_cursor_wait(summaryview->mainwin);
877                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
878                         if (quicksearch_is_active(summaryview->quicksearch))
879                                 quicksearch_search_subfolders(summaryview->quicksearch, 
880                                               summaryview->folderview,
881                                               summaryview->folder_item);
882                         main_window_cursor_normal(summaryview->mainwin);
883                 }                       
884                 return TRUE;
885         }
886         g_free(buf);
887
888         if (!is_refresh)
889                 messageview_clear(summaryview->messageview);
890
891         summaryview->folder_item = item;
892         item->opened = TRUE;
893
894         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
895         debug_print("%s\n", buf);
896         STATUSBAR_PUSH(summaryview->mainwin, buf);
897         g_free(buf);
898
899         main_window_cursor_wait(summaryview->mainwin);
900
901         mlist = folder_item_get_msg_list(item);
902
903         if (summaryview->folder_item->hide_read_msgs &&
904             quicksearch_is_active(summaryview->quicksearch) == FALSE) {
905                 GSList *not_killed;
906                 
907                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
908                 not_killed = NULL;
909                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
910                         MsgInfo * msginfo = (MsgInfo *) cur->data;
911                         
912                         if (MSG_IS_UNREAD(msginfo->flags) &&
913                             !MSG_IS_IGNORE_THREAD(msginfo->flags))
914                                 not_killed = g_slist_prepend(not_killed, msginfo);
915                         else if (MSG_IS_MARKED(msginfo->flags) ||
916                                  MSG_IS_LOCKED(msginfo->flags))
917                                 not_killed = g_slist_prepend(not_killed, msginfo);
918                         else if (is_refresh &&
919                                 (msginfo->msgnum == selected_msgnum ||
920                                  msginfo->msgnum == displayed_msgnum))
921                                 not_killed = g_slist_prepend(not_killed, msginfo);
922                         else
923                                 procmsg_msginfo_free(msginfo);
924                 }
925                 g_slist_free(mlist);
926                 mlist = not_killed;
927         } else {
928                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
929         }
930
931         if (quicksearch_is_active(summaryview->quicksearch)) {
932                 GSList *not_killed;
933                 
934                 not_killed = NULL;
935                 for (cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
936                         MsgInfo * msginfo = (MsgInfo *) cur->data;
937
938                         if (quicksearch_match(summaryview->quicksearch, msginfo))
939                                 not_killed = g_slist_prepend(not_killed, msginfo);
940                         else
941                                 procmsg_msginfo_free(msginfo);
942                 }
943                 
944                 if (quicksearch_is_running(summaryview->quicksearch)) {
945                         /* only scan subfolders when quicksearch changed,
946                          * not when search is the same and folder changed */
947                         main_window_cursor_wait(summaryview->mainwin);
948                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
949                         quicksearch_search_subfolders(summaryview->quicksearch, 
950                                               summaryview->folderview,
951                                               summaryview->folder_item);
952                         main_window_cursor_normal(summaryview->mainwin);
953                 }
954                 
955                 g_slist_free(mlist);
956                 mlist = not_killed;
957         }
958
959 #if 0   
960         if ((global_scoring || item->prefs->scoring)) {
961                 GSList *not_killed;
962                 gint kill_score;
963
964                 not_killed = NULL;
965                 kill_score = prefs_common.kill_score;
966                 if (item->prefs->kill_score > kill_score)
967                         kill_score = item->prefs->kill_score;
968                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
969                         MsgInfo * msginfo = (MsgInfo *) cur->data;
970
971                         if (msginfo->score > kill_score)
972                                 not_killed = g_slist_prepend(not_killed, msginfo);
973                         else
974                                 procmsg_msginfo_free(msginfo);
975                 }
976                 g_slist_free(mlist);
977                 mlist = not_killed;
978         }
979 #endif
980         not_killed = NULL;
981         for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
982                 MsgInfo * msginfo = (MsgInfo *) cur->data;
983                 
984                 if (!msginfo->hidden)
985                         not_killed = g_slist_prepend(not_killed, msginfo);
986                 else
987                         procmsg_msginfo_free(msginfo);
988         }
989         g_slist_free(mlist);
990         mlist = not_killed;
991
992         STATUSBAR_POP(summaryview->mainwin);
993
994         /* set ctree and hash table from the msginfo list, and
995            create the thread */
996         summary_set_ctree_from_list(summaryview, mlist);
997
998         g_slist_free(mlist);
999
1000         gtk_clist_thaw(GTK_CLIST(ctree));
1001
1002         if (is_refresh) {
1003                 summaryview->displayed =
1004                         summary_find_msg_by_msgnum(summaryview,
1005                                                    displayed_msgnum);
1006                 if (!summaryview->displayed)
1007                         messageview_clear(summaryview->messageview);
1008                 summary_unlock(summaryview);
1009                 summary_select_by_msgnum(summaryview, selected_msgnum);
1010                 summary_lock(summaryview);
1011                 if (!summaryview->selected) {
1012                         /* no selected message - select first unread
1013                            message, but do not display it */
1014                         node = summary_find_next_flagged_msg(summaryview, NULL,
1015                                                              MSG_UNREAD, FALSE);
1016                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
1017                                 node = gtk_ctree_node_nth
1018                                         (ctree,
1019                                          item->sort_type == SORT_DESCENDING
1020                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
1021                         summary_unlock(summaryview);
1022                         summary_select_node(summaryview, node, FALSE, TRUE);
1023                         summary_lock(summaryview);
1024                 }
1025         } else {
1026                 switch (prefs_common.select_on_entry) {
1027                         case SELECTONENTRY_NEW:
1028                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1029                                                                      MSG_NEW, FALSE);
1030                                 if (node == NULL)
1031                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1032                                                                      MSG_UNREAD, FALSE);
1033                                 break;
1034                         case SELECTONENTRY_UNREAD:
1035                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1036                                                                      MSG_UNREAD, FALSE);
1037                                 if (node == NULL)
1038                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1039                                                                      MSG_NEW, FALSE);
1040                                 break;
1041                         default:
1042                                 node = NULL;
1043                 }
1044
1045                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1046                         node = gtk_ctree_node_nth
1047                                 (ctree,
1048                                  item->sort_type == SORT_DESCENDING
1049                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1050                 }
1051                 summary_unlock(summaryview);
1052                 summary_select_node(summaryview, node,
1053                                     prefs_common.always_show_msg,
1054                                     TRUE);
1055                 summary_lock(summaryview);
1056         }
1057
1058         summary_set_column_titles(summaryview);
1059         summary_status_show(summaryview);
1060         summary_set_menu_sensitive(summaryview);
1061         toolbar_main_set_sensitive(summaryview->mainwin);
1062
1063         debug_print("\n");
1064         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1065         STATUSBAR_POP(summaryview->mainwin);
1066         main_window_cursor_normal(summaryview->mainwin);
1067         summary_unlock(summaryview);
1068         inc_unlock();
1069
1070         return TRUE;
1071 }
1072
1073 #undef CURRENTLY_DISPLAYED
1074
1075
1076 void summary_clear_list(SummaryView *summaryview)
1077 {
1078         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1079         gint optimal_width;
1080
1081         gtk_clist_freeze(clist);
1082
1083         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1084                                 NULL, summary_free_msginfo_func, NULL);
1085
1086         if (summaryview->folder_item) {
1087                 summaryview->folder_item->opened = FALSE;
1088                 summaryview->folder_item = NULL;
1089         }
1090
1091         summaryview->display_msg = FALSE;
1092
1093         summaryview->selected = NULL;
1094         summaryview->displayed = NULL;
1095         summaryview->total_size = 0;
1096         summaryview->deleted = summaryview->moved = 0;
1097         summaryview->copied = 0;
1098         if (summaryview->msgid_table) {
1099                 g_hash_table_destroy(summaryview->msgid_table);
1100                 summaryview->msgid_table = NULL;
1101         }
1102         if (summaryview->subject_table) {
1103                 g_hash_table_destroy(summaryview->subject_table);
1104                 summaryview->subject_table = NULL;
1105         }
1106         summaryview->mlist = NULL;
1107
1108         gtk_clist_clear(clist);
1109         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1110                 optimal_width = gtk_clist_optimal_column_width
1111                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1112                 gtk_clist_set_column_width
1113                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1114                          optimal_width);
1115         }
1116
1117         gtk_clist_thaw(clist);
1118 }
1119
1120 void summary_clear_all(SummaryView *summaryview)
1121 {
1122         messageview_clear(summaryview->messageview);
1123         summary_clear_list(summaryview);
1124         summary_set_menu_sensitive(summaryview);
1125         toolbar_main_set_sensitive(summaryview->mainwin);
1126         summary_status_show(summaryview);
1127 }
1128
1129 void summary_lock(SummaryView *summaryview)
1130 {
1131         summaryview->lock_count++;
1132 }
1133
1134 void summary_unlock(SummaryView *summaryview)
1135 {
1136         if (summaryview->lock_count)
1137                 summaryview->lock_count--;
1138 }
1139
1140 gboolean summary_is_locked(SummaryView *summaryview)
1141 {
1142         return summaryview->lock_count > 0;
1143 }
1144
1145 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1146 {
1147         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1148         SummarySelection selection;
1149
1150         if (!clist->row_list)
1151                 selection = SUMMARY_NONE;
1152         else if (!clist->selection)
1153                 selection = SUMMARY_SELECTED_NONE;
1154         else if (!clist->selection->next)
1155                 selection = SUMMARY_SELECTED_SINGLE;
1156         else
1157                 selection = SUMMARY_SELECTED_MULTIPLE;
1158
1159         return selection;
1160 }
1161
1162 /*!
1163  *\return       MsgInfo * Selected message if there's one selected;
1164  *              if multiple selected, or none, return NULL.
1165  */ 
1166 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1167 {
1168         /* summaryview->selected may be valid when multiple 
1169          * messages were selected */
1170         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1171
1172         if (sellist == NULL || sellist->next) 
1173                 return NULL;
1174         
1175         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1176 }
1177
1178 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1179 {
1180         GSList *mlist = NULL;
1181         GList *cur;
1182         MsgInfo *msginfo;
1183
1184         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1185              cur = cur->next) {
1186                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1187                 mlist = g_slist_prepend(mlist, msginfo);
1188         }
1189
1190         mlist = g_slist_reverse(mlist);
1191
1192         return mlist;
1193 }
1194
1195 GSList *summary_get_msg_list(SummaryView *summaryview)
1196 {
1197         GSList *mlist = NULL;
1198         GtkCTree *ctree;
1199         GtkCTreeNode *node;
1200         MsgInfo *msginfo;
1201
1202         ctree = GTK_CTREE(summaryview->ctree);
1203
1204         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1205              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1206                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1207                 mlist = g_slist_prepend(mlist, msginfo);
1208         }
1209
1210         mlist = g_slist_reverse(mlist);
1211
1212         return mlist;
1213 }
1214
1215 static void summary_set_menu_sensitive(SummaryView *summaryview)
1216 {
1217         GtkItemFactory *ifactory = summaryview->popupfactory;
1218         SensitiveCond state;
1219         gboolean sensitive;
1220         GtkWidget *menuitem;
1221         gint i;
1222
1223         static const struct {
1224                 gchar *const entry;
1225                 SensitiveCond cond;
1226         } entry[] = {
1227                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1228                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1229                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1230                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1231                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1232                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1233
1234                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1235                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1236
1237                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1238
1239                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1240                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1241                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1242                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1243                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1244
1245                 {"/Mark"                        , M_TARGET_EXIST},
1246                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1247                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1248                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1249                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1250                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1251                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1252                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1253                 {"/Color label"                 , M_TARGET_EXIST},
1254
1255                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1256                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1257                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1258
1259                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1260                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1261                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1262                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1263 #if 0
1264                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1265                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1266
1267                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1268                 {"/Select all"                  , M_TARGET_EXIST},
1269 #endif
1270                 {NULL, 0}
1271         };
1272
1273         main_window_set_menu_sensitive(summaryview->mainwin);
1274
1275         state = main_window_get_current_state(summaryview->mainwin);
1276
1277         for (i = 0; entry[i].entry != NULL; i++) {
1278                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1279                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1280         }
1281
1282
1283         summary_lock(summaryview);
1284         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1285         gtk_check_menu_item_set_active
1286                 (GTK_CHECK_MENU_ITEM(menuitem),
1287                  summaryview->messageview->mimeview->textview->show_all_headers);
1288         summary_unlock(summaryview);
1289 }
1290
1291 void summary_select_prev_unread(SummaryView *summaryview)
1292 {
1293         GtkCTreeNode *node;
1294         gboolean skip_cur = FALSE;
1295
1296         if (summaryview->displayed 
1297         &&  summaryview->selected == summaryview->displayed) {
1298                 debug_print("skipping current\n");
1299                 skip_cur = TRUE;
1300         }
1301
1302         node = summary_find_prev_flagged_msg
1303                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1304
1305         if (!node) {
1306                 AlertValue val = 0;
1307
1308                 switch (prefs_common.next_unread_msg_dialog) {
1309                         case NEXTUNREADMSGDIALOG_ALWAYS:
1310                                 val = alertpanel(_("No more unread messages"),
1311                                                  _("No unread message found. "
1312                                                    "Search from the end?"),
1313                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1314                                 break;
1315                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1316                                 val = G_ALERTDEFAULT;
1317                                 break;
1318                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1319                                 val = !G_ALERTDEFAULT;
1320                                 break;
1321                         default:
1322                                 debug_print(
1323                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1324                 }
1325                 if (val != G_ALERTDEFAULT) return;
1326                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1327                                                      MSG_UNREAD, FALSE);
1328         }
1329
1330         if (!node)
1331                 alertpanel_notice(_("No unread messages."));
1332         else
1333                 summary_select_node(summaryview, node, TRUE, FALSE);
1334 }
1335
1336 void summary_select_next_unread(SummaryView *summaryview)
1337 {
1338         GtkCTreeNode *node = summaryview->selected;
1339         gboolean skip_cur = FALSE;
1340         
1341         if (summaryview->displayed 
1342         &&  summaryview->selected == summaryview->displayed) {
1343                 debug_print("skipping cur (%p %p)\n",
1344                         summaryview->displayed, summaryview->selected);
1345                 skip_cur = TRUE;
1346         }
1347
1348
1349         node = summary_find_next_flagged_msg
1350                 (summaryview, node, MSG_UNREAD, skip_cur);
1351         
1352         if (node)
1353                 summary_select_node(summaryview, node, TRUE, FALSE);
1354         else {
1355                 node = summary_find_next_flagged_msg
1356                         (summaryview, NULL, MSG_UNREAD, FALSE);
1357                 if (node == NULL) {
1358                         AlertValue val = 0;
1359
1360                         switch (prefs_common.next_unread_msg_dialog) {
1361                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1362                                         val = alertpanel(_("No more unread messages"),
1363                                                          _("No unread message found. "
1364                                                            "Go to next folder?"),
1365                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1366                                         break;
1367                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1368                                         val = G_ALERTDEFAULT;
1369                                         break;
1370                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1371                                         val = G_ALERTOTHER;
1372                                         break;
1373                                 default:
1374                                         debug_print(
1375                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1376                         }
1377
1378                         if (val == G_ALERTDEFAULT) {
1379                                 folderview_select_next_unread(summaryview->folderview);
1380                                 return;
1381                         } 
1382                         else
1383                                 return;
1384                 } else
1385                         summary_select_node(summaryview, node, TRUE, FALSE);
1386
1387         }
1388 }
1389
1390 void summary_select_prev_new(SummaryView *summaryview)
1391 {
1392         GtkCTreeNode *node;
1393         gboolean skip_cur = FALSE;
1394
1395         if (summaryview->displayed 
1396         &&  summaryview->selected == summaryview->displayed) {
1397                 debug_print("skipping current\n");
1398                 skip_cur = TRUE;
1399         }
1400
1401         node = summary_find_prev_flagged_msg
1402                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1403
1404         if (!node) {
1405                 AlertValue val = 0;
1406
1407                 switch (prefs_common.next_unread_msg_dialog) {
1408                         case NEXTUNREADMSGDIALOG_ALWAYS:
1409                                 val = alertpanel(_("No more new messages"),
1410                                                  _("No new message found. "
1411                                                    "Search from the end?"),
1412                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1413                                 break;
1414                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1415                                 val = G_ALERTDEFAULT;
1416                                 break;
1417                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1418                                 val = !G_ALERTDEFAULT;
1419                                 break;
1420                         default:
1421                                 debug_print(
1422                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1423                 }
1424                 if (val != G_ALERTDEFAULT) return;
1425                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1426                                                      MSG_NEW, FALSE);
1427         }
1428
1429         if (!node)
1430                 alertpanel_notice(_("No new messages."));
1431         else
1432                 summary_select_node(summaryview, node, TRUE, FALSE);
1433 }
1434
1435 void summary_select_next_new(SummaryView *summaryview)
1436 {
1437         GtkCTreeNode *node = summaryview->selected;
1438         gboolean skip_cur = FALSE;
1439         
1440         if (summaryview->displayed 
1441         &&  summaryview->selected == summaryview->displayed) {
1442                 debug_print("skipping cur (%p %p)\n",
1443                         summaryview->displayed, summaryview->selected);
1444                 skip_cur = TRUE;
1445         }
1446
1447
1448         node = summary_find_next_flagged_msg
1449                 (summaryview, node, MSG_NEW, skip_cur);
1450         
1451         if (node)
1452                 summary_select_node(summaryview, node, TRUE, FALSE);
1453         else {
1454                 node = summary_find_next_flagged_msg
1455                         (summaryview, NULL, MSG_NEW, FALSE);
1456                 if (node == NULL) {
1457                         AlertValue val = 0;
1458
1459                         switch (prefs_common.next_unread_msg_dialog) {
1460                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1461                                         val = alertpanel(_("No more new messages"),
1462                                                          _("No new message found. "
1463                                                            "Go to next folder?"),
1464                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1465                                         break;
1466                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1467                                         val = G_ALERTDEFAULT;
1468                                         break;
1469                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1470                                         val = G_ALERTOTHER;
1471                                         break;
1472                                 default:
1473                                         debug_print(
1474                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1475                         }
1476
1477                         if (val == G_ALERTDEFAULT) {
1478                                 folderview_select_next_new(summaryview->folderview);
1479                                 return;
1480                         } 
1481                         else
1482                                 return;
1483                 } else
1484                         summary_select_node(summaryview, node, TRUE, FALSE);
1485
1486         }
1487 }
1488
1489 void summary_select_prev_marked(SummaryView *summaryview)
1490 {
1491         GtkCTreeNode *node;
1492
1493         node = summary_find_prev_flagged_msg
1494                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1495
1496         if (!node) {
1497                 AlertValue val;
1498
1499                 val = alertpanel(_("No more marked messages"),
1500                                  _("No marked message found. "
1501                                    "Search from the end?"),
1502                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1503                 if (val != G_ALERTDEFAULT) return;
1504                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1505                                                      MSG_MARKED, TRUE);
1506         }
1507
1508         if (!node)
1509                 alertpanel_notice(_("No marked messages."));
1510         else
1511                 summary_select_node(summaryview, node, TRUE, FALSE);
1512 }
1513
1514 void summary_select_next_marked(SummaryView *summaryview)
1515 {
1516         GtkCTreeNode *node;
1517
1518         node = summary_find_next_flagged_msg
1519                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1520
1521         if (!node) {
1522                 AlertValue val;
1523
1524                 val = alertpanel(_("No more marked messages"),
1525                                  _("No marked message found. "
1526                                    "Search from the beginning?"),
1527                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1528                 if (val != G_ALERTDEFAULT) return;
1529                 node = summary_find_next_flagged_msg(summaryview, NULL,
1530                                                      MSG_MARKED, TRUE);
1531         }
1532
1533         if (!node)
1534                 alertpanel_notice(_("No marked messages."));
1535         else
1536                 summary_select_node(summaryview, node, TRUE, FALSE);
1537 }
1538
1539 void summary_select_prev_labeled(SummaryView *summaryview)
1540 {
1541         GtkCTreeNode *node;
1542
1543         node = summary_find_prev_flagged_msg
1544                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1545
1546         if (!node) {
1547                 AlertValue val;
1548
1549                 val = alertpanel(_("No more labeled messages"),
1550                                  _("No labeled message found. "
1551                                    "Search from the end?"),
1552                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1553                 if (val != G_ALERTDEFAULT) return;
1554                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1555                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1556         }
1557
1558         if (!node)
1559                 alertpanel_notice(_("No labeled messages."));
1560         else
1561                 summary_select_node(summaryview, node, TRUE, FALSE);
1562 }
1563
1564 void summary_select_next_labeled(SummaryView *summaryview)
1565 {
1566         GtkCTreeNode *node;
1567
1568         node = summary_find_next_flagged_msg
1569                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1570
1571         if (!node) {
1572                 AlertValue val;
1573
1574                 val = alertpanel(_("No more labeled messages"),
1575                                  _("No labeled message found. "
1576                                    "Search from the beginning?"),
1577                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1578                 if (val != G_ALERTDEFAULT) return;
1579                 node = summary_find_next_flagged_msg(summaryview, NULL,
1580                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1581         }
1582
1583         if (!node)
1584                 alertpanel_notice(_("No labeled messages."));
1585         else
1586                 summary_select_node(summaryview, node, TRUE, FALSE);
1587 }
1588
1589 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1590 {
1591         GtkCTreeNode *node;
1592
1593         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1594         summary_select_node(summaryview, node, FALSE, TRUE);
1595 }
1596
1597 /**
1598  * summary_select_node:
1599  * @summaryview: Summary view.
1600  * @node: Summary tree node.
1601  * @display_msg: TRUE to display the selected message.
1602  * @do_refresh: TRUE to refresh the widget.
1603  *
1604  * Select @node (bringing it into view by scrolling and expanding its
1605  * thread, if necessary) and unselect all others.  If @display_msg is
1606  * TRUE, display the corresponding message in the message view.
1607  * If @do_refresh is TRUE, the widget is refreshed.
1608  **/
1609 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1610                          gboolean display_msg, gboolean do_refresh)
1611 {
1612         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1613
1614         if (node) {
1615                 gtkut_ctree_expand_parent_all(ctree, node);
1616                 if (do_refresh) {
1617                         GTK_EVENTS_FLUSH();
1618                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1619                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1620                 }
1621                 summary_unselect_all(summaryview);
1622                 if (display_msg && summaryview->displayed == node)
1623                         summaryview->displayed = NULL;
1624                 summaryview->display_msg = display_msg;
1625                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1626                 if (summaryview->selected == NULL)
1627                         summaryview->selected = node;
1628         }
1629 }
1630
1631 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1632 {
1633         GtkCTree *ctree =NULL;
1634         MsgInfo *msginfo;
1635
1636         if (!summaryview)
1637                 return 0;
1638         ctree = GTK_CTREE(summaryview->ctree);
1639         if (!node)
1640                 return 0;
1641         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1642         return msginfo->msgnum;
1643 }
1644
1645 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1646                                            GtkCTreeNode *current_node)
1647 {
1648         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1649         GtkCTreeNode *node;
1650         MsgInfo *msginfo;
1651
1652         if (current_node)
1653                 node = current_node;
1654         else
1655                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1656
1657         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1658                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1659                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1660         }
1661
1662         return node;
1663 }
1664
1665 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1666                                            GtkCTreeNode *current_node)
1667 {
1668         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1669         GtkCTreeNode *node;
1670         MsgInfo *msginfo;
1671
1672         if (current_node)
1673                 node = current_node;
1674         else
1675                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1676
1677         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1678                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1679                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1680         }
1681
1682         return node;
1683 }
1684
1685 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1686                                                    GtkCTreeNode *current_node,
1687                                                    MsgPermFlags flags,
1688                                                    gboolean start_from_prev)
1689 {
1690         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1691         GtkCTreeNode *node;
1692         MsgInfo *msginfo;
1693
1694         if (current_node) {
1695                 if (start_from_prev)
1696                         node = GTK_CTREE_NODE_PREV(current_node);
1697                 else
1698                         node = current_node;
1699         } else
1700                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1701
1702         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1703                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1704                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1705         }
1706
1707         return node;
1708 }
1709
1710 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1711                                                    GtkCTreeNode *current_node,
1712                                                    MsgPermFlags flags,
1713                                                    gboolean start_from_next)
1714 {
1715         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1716         GtkCTreeNode *node;
1717         MsgInfo *msginfo;
1718
1719         if (current_node) {
1720                 if (start_from_next)
1721                         node = gtkut_ctree_node_next(ctree, current_node);
1722                 else
1723                         node = current_node;
1724         } else
1725                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1726
1727         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1728                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1729                 /* Find msg with matching flags but ignore messages with
1730                    ignore flags, if searching for new or unread messages */
1731                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1732                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1733                         break;
1734         }
1735
1736         return node;
1737 }
1738
1739 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1740                                                 guint msgnum)
1741 {
1742         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1743         GtkCTreeNode *node;
1744         MsgInfo *msginfo;
1745
1746         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1747
1748         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1749                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1750                 if (msginfo && msginfo->msgnum == msgnum) break;
1751         }
1752
1753         return node;
1754 }
1755
1756 static guint attract_hash_func(gconstpointer key)
1757 {
1758         gchar *str;
1759         gchar *p;
1760         guint h;
1761
1762         Xstrdup_a(str, (const gchar *)key, return 0);
1763         trim_subject(str);
1764
1765         p = str;
1766         h = *p;
1767
1768         if (h) {
1769                 for (p += 1; *p != '\0'; p++)
1770                         h = (h << 5) - h + *p;
1771         }
1772
1773         return h;
1774 }
1775
1776 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1777 {
1778         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1779 }
1780
1781 void summary_attract_by_subject(SummaryView *summaryview)
1782 {
1783         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1784         GtkCList *clist = GTK_CLIST(ctree);
1785         GtkCTreeNode *src_node;
1786         GtkCTreeNode *dst_node, *sibling;
1787         GtkCTreeNode *tmp;
1788         MsgInfo *src_msginfo, *dst_msginfo;
1789         GHashTable *subject_table;
1790
1791         debug_print("Attracting messages by subject...");
1792         STATUSBAR_PUSH(summaryview->mainwin,
1793                        _("Attracting messages by subject..."));
1794
1795         main_window_cursor_wait(summaryview->mainwin);
1796         gtk_clist_freeze(clist);
1797
1798         subject_table = g_hash_table_new(attract_hash_func,
1799                                          attract_compare_func);
1800
1801         for (src_node = GTK_CTREE_NODE(clist->row_list);
1802              src_node != NULL;
1803              src_node = tmp) {
1804                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1805                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1806                 if (!src_msginfo) continue;
1807                 if (!src_msginfo->subject) continue;
1808
1809                 /* find attracting node */
1810                 dst_node = g_hash_table_lookup(subject_table,
1811                                                src_msginfo->subject);
1812
1813                 if (dst_node) {
1814                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1815
1816                         /* if the time difference is more than 20 days,
1817                            don't attract */
1818                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1819                             > 60 * 60 * 24 * 20)
1820                                 continue;
1821
1822                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1823                         if (src_node != sibling)
1824                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1825                 }
1826
1827                 g_hash_table_insert(subject_table,
1828                                     src_msginfo->subject, src_node);
1829         }
1830
1831         g_hash_table_destroy(subject_table);
1832
1833         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1834
1835         gtk_clist_thaw(clist);
1836
1837         debug_print("done.\n");
1838         STATUSBAR_POP(summaryview->mainwin);
1839
1840         main_window_cursor_normal(summaryview->mainwin);
1841 }
1842
1843 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1844                                       gpointer data)
1845 {
1846         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1847
1848         if (msginfo)
1849                 procmsg_msginfo_free(msginfo);
1850 }
1851
1852 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1853                                    gpointer data)
1854 {
1855         SummaryView *summaryview = data;
1856         MsgInfo *msginfo;
1857
1858         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1859
1860         if (msginfo->folder && msginfo->folder->folder &&
1861             msginfo->folder->folder->klass->type == F_NEWS)
1862                 news_flag_crosspost(msginfo);
1863
1864         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1865         && procmsg_msg_has_marked_parent(msginfo))
1866                 summaryview->unreadmarked++;
1867
1868         if (MSG_IS_DELETED(msginfo->flags))
1869                 summaryview->deleted++;
1870
1871         summaryview->total_size += msginfo->size;
1872
1873         summary_set_row_marks(summaryview, node);
1874 }
1875
1876 static void summary_update_status(SummaryView *summaryview)
1877 {
1878         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1879         GtkCTreeNode *node;
1880         MsgInfo *msginfo;
1881
1882         summaryview->total_size =
1883         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1884
1885         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1886              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1887                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1888
1889                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1890                 && procmsg_msg_has_marked_parent(msginfo))
1891                         summaryview->unreadmarked++;
1892                 if (MSG_IS_DELETED(msginfo->flags))
1893                         summaryview->deleted++;
1894                 if (MSG_IS_MOVE(msginfo->flags))
1895                         summaryview->moved++;
1896                 if (MSG_IS_COPY(msginfo->flags))
1897                         summaryview->copied++;
1898                 summaryview->total_size += msginfo->size;
1899         }
1900 }
1901
1902 static void summary_status_show(SummaryView *summaryview)
1903 {
1904         gchar *str;
1905         gchar *del, *mv, *cp;
1906         gchar *sel;
1907         gchar *spc;
1908         gchar *itstr;
1909         GList *rowlist, *cur;
1910         guint n_selected = 0;
1911         off_t sel_size = 0;
1912         MsgInfo *msginfo;
1913         gchar *name;
1914         
1915         if (!summaryview->folder_item) {
1916                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1917                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1918                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1919                 return;
1920         }
1921
1922         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1923         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1924                 msginfo = gtk_ctree_node_get_row_data
1925                         (GTK_CTREE(summaryview->ctree),
1926                          GTK_CTREE_NODE(cur->data));
1927                 if (!msginfo)
1928                         g_warning("summary_status_show(): msginfo == NULL\n");
1929                 else {
1930                         sel_size += msginfo->size;
1931                         n_selected++;
1932                 }
1933         }
1934
1935         name = folder_item_get_name(summaryview->folder_item);
1936         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1937         g_free(name);
1938
1939         if (summaryview->deleted)
1940                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1941         else
1942                 del = g_strdup("");
1943         if (summaryview->moved)
1944                 mv = g_strdup_printf(_("%s%d moved"),
1945                                      summaryview->deleted ? _(", ") : "",
1946                                      summaryview->moved);
1947         else
1948                 mv = g_strdup("");
1949         if (summaryview->copied)
1950                 cp = g_strdup_printf(_("%s%d copied"),
1951                                      summaryview->deleted ||
1952                                      summaryview->moved ? _(", ") : "",
1953                                      summaryview->copied);
1954         else
1955                 cp = g_strdup("");
1956
1957         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1958                 spc = "    ";
1959         else
1960                 spc = "";
1961
1962         if (n_selected) {
1963                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1964                 if (n_selected == 1)
1965                         itstr = g_strdup(_(" item selected"));
1966                 else
1967                         itstr = g_strdup(_(" items selected"));
1968         } else {
1969                 sel = g_strdup("");
1970                 itstr = g_strdup("");
1971         }
1972                 
1973         str = g_strconcat(n_selected ? itos(n_selected) : "",
1974                                         itstr, sel, spc, del, mv, cp, NULL);
1975         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1976         g_free(str);
1977         g_free(sel);
1978         g_free(del);
1979         g_free(mv);
1980         g_free(cp);
1981         g_free(itstr);
1982
1983         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1984
1985                                       summaryview->folder_item->new_msgs,
1986                                       summaryview->folder_item->unread_msgs,
1987                                       summaryview->folder_item->total_msgs,
1988                                       to_human_readable(summaryview->total_size));
1989         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1990         g_free(str);
1991 }
1992
1993 static void summary_set_column_titles(SummaryView *summaryview)
1994 {
1995         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1996         GtkWidget *hbox;
1997         GtkWidget *label;
1998         GtkWidget *arrow;
1999         gint pos;
2000         const gchar *title;
2001         SummaryColumnType type;
2002         gboolean single_char;
2003         GtkJustification justify;
2004
2005         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2006                 SORT_BY_MARK,
2007                 SORT_BY_STATUS,
2008                 SORT_BY_MIME,
2009                 SORT_BY_SUBJECT,
2010                 SORT_BY_FROM,
2011                 SORT_BY_TO,
2012                 SORT_BY_DATE,
2013                 SORT_BY_SIZE,
2014                 SORT_BY_NUMBER,
2015                 SORT_BY_SCORE,
2016                 SORT_BY_LOCKED
2017         };
2018
2019         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2020                 type = summaryview->col_state[pos].type;
2021
2022                 /* CLAWS: mime and unread are single char headers */
2023                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
2024                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2025                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2026
2027                 switch (type) {
2028                 case S_COL_SUBJECT:
2029                 case S_COL_FROM:
2030                 case S_COL_TO:
2031                 case S_COL_DATE:
2032                 case S_COL_NUMBER:
2033                         if (prefs_common.trans_hdr)
2034                                 title = gettext(col_label[type]);
2035                         else
2036                                 title = col_label[type];
2037                         break;
2038                 /* CLAWS: dummies for mark and locked headers */        
2039                 case S_COL_MARK:        
2040                 case S_COL_LOCKED:
2041                         title = "";
2042                         break;
2043                 default:
2044                         title = gettext(col_label[type]);
2045                 }
2046
2047                 if (type == S_COL_MIME) {
2048                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2049                         gtk_widget_show(label);
2050                         gtk_clist_set_column_widget(clist, pos, label);
2051                         continue;
2052                 }
2053                 if (single_char) {
2054                         gtk_clist_set_column_title(clist, pos, title);
2055                         continue;
2056                 }
2057
2058                 /* CLAWS: changed so that locked and mark headers
2059                  * show a pixmap instead of single character */
2060                 hbox  = gtk_hbox_new(FALSE, 4);
2061                 
2062                 if (type == S_COL_LOCKED)
2063                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
2064                 else if (type == S_COL_MARK) 
2065                         label = gtk_pixmap_new(markxpm, markxpmmask);
2066                 else 
2067                         label = gtk_label_new(title);
2068                 
2069                 if (justify == GTK_JUSTIFY_RIGHT)
2070                         gtk_box_pack_end(GTK_BOX(hbox), label,
2071                                          FALSE, FALSE, 0);
2072                 else
2073                         gtk_box_pack_start(GTK_BOX(hbox), label,
2074                                            FALSE, FALSE, 0);
2075
2076                 if (summaryview->sort_key == sort_by[type]) {
2077                         arrow = gtk_arrow_new
2078                                 (summaryview->sort_type == SORT_ASCENDING
2079                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2080                                  GTK_SHADOW_IN);
2081                         if (justify == GTK_JUSTIFY_RIGHT)
2082                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2083                                                    FALSE, FALSE, 0);
2084                         else
2085                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2086                                                  FALSE, FALSE, 0);
2087                 }
2088
2089                 gtk_widget_show_all(hbox);
2090                 gtk_clist_set_column_widget(clist, pos, hbox);
2091         }
2092 }
2093
2094 void summary_reflect_prefs(void)
2095 {
2096         SummaryView *summaryview = NULL;
2097         if (!mainwindow_get_mainwindow())
2098                 return;
2099         summaryview = mainwindow_get_mainwindow()->summaryview;
2100         bold_style = bold_marked_style = bold_deleted_style = 
2101                 small_style = small_marked_style = small_deleted_style = NULL;
2102         summary_set_fonts(summaryview);
2103         summary_set_column_titles(summaryview);
2104         summary_show(summaryview, summaryview->folder_item);
2105 }
2106
2107 void summary_sort(SummaryView *summaryview,
2108                   FolderSortKey sort_key, FolderSortType sort_type)
2109 {
2110         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2111         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2112         GtkCListCompareFunc cmp_func = NULL;
2113         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2114                                        G_CALLBACK(summary_tree_expanded), summaryview);
2115         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2116
2117         switch (sort_key) {
2118         case SORT_BY_MARK:
2119                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2120                 break;
2121         case SORT_BY_STATUS:
2122                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2123                 break;
2124         case SORT_BY_MIME:
2125                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2126                 break;
2127         case SORT_BY_NUMBER:
2128                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2129                 break;
2130         case SORT_BY_SIZE:
2131                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2132                 break;
2133         case SORT_BY_DATE:
2134                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2135                 break;
2136         case SORT_BY_FROM:
2137                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2138                 break;
2139         case SORT_BY_SUBJECT:
2140                 if (summaryview->simplify_subject_preg)
2141                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2142                 else
2143                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2144                 break;
2145         case SORT_BY_SCORE:
2146                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2147                 break;
2148         case SORT_BY_LABEL:
2149                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2150                 break;
2151         case SORT_BY_TO:
2152                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2153                 break;
2154         case SORT_BY_LOCKED:
2155                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2156                 break;
2157         case SORT_BY_NONE:
2158                 break;
2159         default:
2160                 goto unlock;
2161         }
2162
2163         summaryview->sort_key = sort_key;
2164         summaryview->sort_type = sort_type;
2165
2166         summary_set_column_titles(summaryview);
2167         summary_set_menu_sensitive(summaryview);
2168
2169         /* allow fallback to don't sort */
2170         if (summaryview->sort_key == SORT_BY_NONE)
2171                 goto unlock;
2172
2173         if (cmp_func != NULL) {
2174                 debug_print("Sorting summary...");
2175                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2176
2177                 main_window_cursor_wait(summaryview->mainwin);
2178
2179                 gtk_clist_freeze(clist);
2180                 gtk_clist_set_compare_func(clist, cmp_func);
2181
2182                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2183
2184                 gtk_sctree_sort_recursive(ctree, NULL);
2185
2186                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2187
2188                 main_window_cursor_normal(summaryview->mainwin);
2189                 gtk_clist_thaw(clist);
2190
2191                 debug_print("done.\n");
2192                 STATUSBAR_POP(summaryview->mainwin);
2193         }
2194 unlock:
2195         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2196         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2197                                        G_CALLBACK(summary_tree_expanded), summaryview);
2198 }
2199
2200 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2201                                    GtkCTreeNode *cnode, gpointer data)
2202 {
2203         SummaryView *summaryview = (SummaryView *)data;
2204         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2205         gchar *text[N_SUMMARY_COLS];
2206         gint *col_pos = summaryview->col_pos;
2207         const gchar *msgid = msginfo->msgid;
2208         GHashTable *msgid_table = summaryview->msgid_table;
2209
2210         summary_set_header(summaryview, text, msginfo);
2211
2212         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2213                                 NULL, NULL, NULL, NULL, FALSE,
2214                                 gnode->parent->parent ? TRUE : FALSE);
2215 #define SET_TEXT(col) \
2216         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2217                                 text[col_pos[col]])
2218
2219         SET_TEXT(S_COL_NUMBER);
2220         SET_TEXT(S_COL_SCORE);
2221         SET_TEXT(S_COL_SIZE);
2222         SET_TEXT(S_COL_DATE);
2223         SET_TEXT(S_COL_FROM);
2224         SET_TEXT(S_COL_TO);
2225         SET_TEXT(S_COL_SUBJECT);
2226
2227 #undef SET_TEXT
2228
2229         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2230         summary_set_marks_func(ctree, cnode, summaryview);
2231
2232         if (msgid && msgid[0] != '\0')
2233                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2234
2235         return TRUE;
2236 }
2237
2238 static void summary_set_ctree_from_list(SummaryView *summaryview,
2239                                         GSList *mlist)
2240 {
2241         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2242         MsgInfo *msginfo;
2243         GtkCTreeNode *node = NULL;
2244         GHashTable *msgid_table;
2245         GHashTable *subject_table;
2246         GSList * cur;
2247         START_TIMING("summary_set_ctree_from_list");
2248         
2249         if (!mlist) return;
2250
2251         debug_print("\tSetting summary from message data...");
2252         STATUSBAR_PUSH(summaryview->mainwin,
2253                        _("Setting summary from message data..."));
2254         gdk_flush();
2255
2256         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2257                                        G_CALLBACK(summary_tree_expanded), summaryview);
2258
2259         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2260         summaryview->msgid_table = msgid_table;
2261         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2262         summaryview->subject_table = subject_table;
2263
2264         if (prefs_common.use_addr_book)
2265                 start_address_completion();
2266         
2267         for (cur = mlist ; cur != NULL && cur->data != NULL; cur = cur->next) {
2268                 msginfo = (MsgInfo *)cur->data;
2269                 msginfo->threadscore = msginfo->score;
2270         }
2271
2272         if (summaryview->threaded) {
2273                 GNode *root, *gnode;
2274
2275                 root = procmsg_get_thread_tree(mlist);
2276
2277                 for (gnode = root->children; gnode != NULL;
2278                      gnode = gnode->next) {
2279                         node = gtk_ctree_insert_gnode
2280                                 (ctree, NULL, node, gnode,
2281                                  summary_insert_gnode_func, summaryview);
2282                 }
2283
2284                 g_node_destroy(root);
2285                 
2286                 summary_thread_init(summaryview);
2287         } else {
2288                 gchar *text[N_SUMMARY_COLS];
2289                 cur = mlist;
2290                 for (; mlist != NULL; mlist = mlist->next) {
2291                         msginfo = (MsgInfo *)mlist->data;
2292
2293                         summary_set_header(summaryview, text, msginfo);
2294
2295                         node = gtk_ctree_insert_node
2296                                 (ctree, NULL, node, text, 2,
2297                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2298                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2299                         summary_set_marks_func(ctree, node, summaryview);
2300
2301                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2302                                 g_hash_table_insert(msgid_table,
2303                                                     msginfo->msgid, node);
2304
2305                         subject_table_insert(subject_table,
2306                                              msginfo->subject,
2307                                              node);
2308                 }
2309                 mlist = cur;
2310         }
2311
2312         if (prefs_common.enable_hscrollbar &&
2313             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2314                 gint optimal_width;
2315
2316                 optimal_width = gtk_clist_optimal_column_width
2317                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2318                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2319                                            summaryview->col_pos[S_COL_SUBJECT],
2320                                            optimal_width);
2321         }
2322
2323         if (prefs_common.use_addr_book)
2324                 end_address_completion();
2325
2326         debug_print("done.\n");
2327         STATUSBAR_POP(summaryview->mainwin);
2328         if (debug_get_mode()) {
2329                 debug_print("\tmsgid hash table size = %d\n",
2330                             g_hash_table_size(msgid_table));
2331                 debug_print("\tsubject hash table size = %d\n",
2332                             g_hash_table_size(subject_table));
2333         }
2334
2335         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2336
2337         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2338
2339         while (prefs_common.bold_unread && node) {
2340                 GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2341                 if (GTK_CTREE_ROW(node)->children)
2342                         summary_set_row_marks(summaryview, node);
2343                 node = next;
2344         }
2345
2346         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2347                                        G_CALLBACK(summary_tree_expanded), summaryview);
2348         END_TIMING();
2349 }
2350
2351 static gchar *summary_complete_address(const gchar *addr)
2352 {
2353         gint count;
2354         gchar *res, *tmp, *email_addr;
2355         
2356         if (addr == NULL)
2357                 return NULL;
2358
2359         Xstrdup_a(email_addr, addr, return NULL);
2360         extract_address(email_addr);
2361         g_return_val_if_fail(*email_addr, NULL);
2362
2363         /*
2364          * completion stuff must be already initialized
2365          */
2366         res = NULL;
2367         if (1 < (count = complete_address(email_addr))) {
2368                 tmp = get_complete_address(1);
2369                 res = procheader_get_fromname(tmp);
2370                 g_free(tmp);
2371         }
2372
2373         return res;
2374 }
2375
2376 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2377                                MsgInfo *msginfo)
2378 {
2379         static gchar date_modified[80];
2380         static gchar col_score[11];
2381         static gchar buf[BUFFSIZE];
2382         gint *col_pos = summaryview->col_pos;
2383         FolderType ftype = F_UNKNOWN;
2384         gchar *from_text = NULL, *to_text = NULL;
2385         gboolean should_swap = FALSE;
2386
2387         text[col_pos[S_COL_MARK]]   = NULL;
2388         text[col_pos[S_COL_STATUS]] = NULL;
2389         text[col_pos[S_COL_MIME]]   = NULL;
2390         text[col_pos[S_COL_LOCKED]] = NULL;
2391         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2392         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2393         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2394
2395         if (msginfo->date_t) {
2396                 procheader_date_get_localtime(date_modified,
2397                                               sizeof(date_modified),
2398                                               msginfo->date_t);
2399                 text[col_pos[S_COL_DATE]] = date_modified;
2400         } else if (msginfo->date)
2401                 text[col_pos[S_COL_DATE]] = msginfo->date;
2402         else
2403                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2404
2405         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2406                 gchar *addr = NULL;
2407                 
2408                 addr = g_strdup(msginfo->from);
2409
2410                 if (addr) {
2411                         extract_address(addr);
2412                         if (account_find_from_address(addr)) {
2413                                 should_swap = TRUE;
2414                         }
2415                         g_free(addr);
2416                 }
2417         }
2418
2419         if (!prefs_common.use_addr_book) {
2420                 from_text = msginfo->fromname ? 
2421                                 msginfo->fromname :
2422                                 _("(No From)");
2423         } else {
2424                 gchar *tmp = summary_complete_address(msginfo->from);
2425                 from_text = tmp ? tmp : (msginfo->fromname ?
2426                                          msginfo->fromname: 
2427                                                 _("(No From)"));
2428         }
2429         
2430         to_text = msginfo->to ? msginfo->to : 
2431                    (msginfo->cc ? msginfo->cc :
2432                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2433                      )
2434                    );
2435
2436         if (!should_swap) {
2437                 text[col_pos[S_COL_FROM]] = from_text;
2438                 text[col_pos[S_COL_TO]] = to_text;
2439         } else {
2440                 gchar *tmp = NULL;
2441                 tmp = g_strconcat("-->", to_text, NULL);
2442                 text[col_pos[S_COL_FROM]] = tmp;
2443                 tmp = g_strconcat("<--", from_text, NULL);
2444                 text[col_pos[S_COL_TO]] = tmp;
2445         }
2446         
2447         if (msginfo->folder && msginfo->folder->folder)
2448                 ftype = msginfo->folder->folder->klass->type; 
2449                 
2450         if (summaryview->simplify_subject_preg != NULL)
2451                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2452                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2453                                         summaryview->simplify_subject_preg) : 
2454                         _("(No Subject)");
2455         else 
2456                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2457                         _("(No Subject)");
2458 }
2459
2460 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2461 {
2462         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2463 }
2464
2465 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2466                                       GtkCTreeNode *row)
2467 {
2468         g_return_if_fail(summaryview != NULL);
2469         g_return_if_fail(msginfo != NULL);
2470         g_return_if_fail(row != NULL);
2471
2472         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2473         && procmsg_msg_has_marked_parent(msginfo))
2474                 summaryview->unreadmarked--;
2475         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2476                 procmsg_msginfo_unset_flags
2477                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2478                 summary_set_row_marks(summaryview, row);
2479                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2480                 summary_status_show(summaryview);
2481         }
2482 }
2483
2484 typedef struct  {
2485         MsgInfo *msginfo;
2486         SummaryView *summaryview;
2487         GtkCTreeNode *row;
2488 } MarkAsReadData;
2489
2490 static int msginfo_mark_as_read_timeout(void *data)
2491 {
2492         MarkAsReadData *mdata = (MarkAsReadData *)data;
2493         if (!mdata)
2494                 return FALSE;
2495         
2496         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2497                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2498                                      mdata->row); 
2499
2500         g_free(mdata);
2501
2502         return FALSE;   
2503 }
2504
2505 static void summary_display_msg_full(SummaryView *summaryview,
2506                                      GtkCTreeNode *row,
2507                                      gboolean new_window, gboolean all_headers)
2508 {
2509         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2510         MsgInfo *msginfo;
2511         gint val;
2512
2513         if (!new_window) {
2514                 if (summaryview->displayed == row)
2515                         return;
2516                 else
2517                         summaryview->messageview->filtered = FALSE;
2518         }                       
2519         
2520         g_return_if_fail(row != NULL);
2521
2522         if (summary_is_locked(summaryview)) return;
2523         summary_lock(summaryview);
2524
2525         STATUSBAR_POP(summaryview->mainwin);
2526         GTK_EVENTS_FLUSH();
2527
2528         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2529
2530         if (new_window) {
2531                 MessageView *msgview;
2532
2533                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2534                 val = messageview_show(msgview, msginfo, all_headers);
2535         } else {
2536                 MessageView *msgview;
2537
2538                 msgview = summaryview->messageview;
2539
2540                 summaryview->displayed = row;
2541                 if (!messageview_is_visible(msgview)) {
2542                         main_window_toggle_message_view(summaryview->mainwin);
2543                         GTK_EVENTS_FLUSH();
2544                 }
2545                 val = messageview_show(msgview, msginfo, all_headers);
2546                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2547                         gtk_widget_grab_focus(summaryview->ctree);
2548                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2549         }
2550
2551         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2552                 if (prefs_common.mark_as_read_delay) {
2553                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2554                         data->summaryview = summaryview;
2555                         data->msginfo = msginfo;
2556                         data->row = row;
2557                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2558                                 msginfo_mark_as_read_timeout, data);
2559                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2560                         msginfo_mark_as_read(summaryview, msginfo, row);
2561                 }
2562         }
2563
2564         summary_set_menu_sensitive(summaryview);
2565         toolbar_main_set_sensitive(summaryview->mainwin);
2566         messageview_set_menu_sensitive(summaryview->messageview);
2567
2568         summary_unlock(summaryview);
2569 }
2570
2571 void summary_display_msg_selected(SummaryView *summaryview,
2572                                   gboolean all_headers)
2573 {
2574         if (summary_is_locked(summaryview)) return;
2575         summaryview->displayed = NULL;
2576         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2577                                  all_headers);
2578 }
2579
2580 void summary_redisplay_msg(SummaryView *summaryview)
2581 {
2582         GtkCTreeNode *node;
2583
2584         if (summaryview->displayed) {
2585                 node = summaryview->displayed;
2586                 summaryview->displayed = NULL;
2587                 summary_display_msg(summaryview, node);
2588         }
2589 }
2590
2591 void summary_open_msg(SummaryView *summaryview)
2592 {
2593         if (!summaryview->selected) return;
2594         
2595         /* CLAWS: if separate message view, don't open a new window
2596          * but rather use the current separated message view */
2597         summary_display_msg_full(summaryview, summaryview->selected,
2598                                  prefs_common.sep_msg ? FALSE : TRUE, 
2599                                  FALSE);
2600 }
2601
2602 void summary_view_source(SummaryView * summaryview)
2603 {
2604         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2605         MsgInfo *msginfo;
2606         SourceWindow *srcwin;
2607
2608         if (!summaryview->selected) return;
2609
2610         srcwin = source_window_create();
2611         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2612         source_window_show_msg(srcwin, msginfo);
2613         source_window_show(srcwin);
2614 }
2615
2616 void summary_reedit(SummaryView *summaryview)
2617 {
2618         MsgInfo *msginfo;
2619
2620         if (!summaryview->selected) return;
2621         if (!summaryview->folder_item) return;
2622         if (!folder_has_parent_of_type(summaryview->folder_item, F_OUTBOX)
2623         &&  !folder_has_parent_of_type(summaryview->folder_item, F_DRAFT)
2624         &&  !folder_has_parent_of_type(summaryview->folder_item, F_QUEUE))
2625                 return;
2626
2627         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2628                                               summaryview->selected);
2629         if (!msginfo) return;
2630
2631         compose_reedit(msginfo);
2632 }
2633
2634 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2635 {
2636         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2637         GtkCTreeNode *node;
2638
2639         if (summary_is_locked(summaryview)) return FALSE;
2640         if (type == GTK_SCROLL_STEP_FORWARD) {
2641                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2642                 if (node)
2643                         gtkut_ctree_expand_parent_all(ctree, node);
2644                 else
2645                         return FALSE;
2646         } else {
2647                 if (summaryview->selected) {
2648                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2649                         if (!node) return FALSE;
2650                 }
2651         }
2652
2653         if (messageview_is_visible(summaryview->messageview))
2654                 summaryview->display_msg = TRUE;
2655
2656         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2657
2658         if (GTK_CLIST(ctree)->selection)
2659                 gtk_sctree_set_anchor_row
2660                         (GTK_SCTREE(ctree),
2661                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2662
2663         return TRUE;
2664 }
2665
2666 void summary_toggle_view(SummaryView *summaryview)
2667 {
2668         if (!messageview_is_visible(summaryview->messageview) &&
2669             summaryview->selected)
2670                 summary_display_msg(summaryview,
2671                                     summaryview->selected);
2672         else
2673                 main_window_toggle_message_view(summaryview->mainwin);
2674 }
2675
2676 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2677                                                 GtkCTreeNode *node)
2678 {
2679         MsgInfo *msginfo;
2680
2681         if (node) {
2682                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2683                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2684                         return TRUE;
2685                 node = GTK_CTREE_ROW(node)->children;
2686         } else
2687                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2688
2689         while (node) {
2690                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2691                         return TRUE;
2692                 node = GTK_CTREE_ROW(node)->sibling;
2693         }
2694
2695         return FALSE;
2696 }
2697
2698 static gboolean summary_have_unread_children(SummaryView *summaryview,
2699                                              GtkCTreeNode *node)
2700 {
2701         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2702
2703         if (!node) return FALSE;
2704
2705         node = GTK_CTREE_ROW(node)->children;
2706
2707         while (node) {
2708                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2709                         return TRUE;
2710                 node = GTK_CTREE_ROW(node)->sibling;
2711         }
2712         return FALSE;
2713 }
2714
2715 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2716 {
2717         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2718         GtkStyle *style = NULL;
2719         MsgInfo *msginfo;
2720         MsgFlags flags;
2721         gint *col_pos = summaryview->col_pos;
2722
2723         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2724         if (!msginfo) return;
2725
2726         flags = msginfo->flags;
2727
2728         gtk_ctree_node_set_foreground(ctree, row, NULL);
2729
2730         /* set new/unread column */
2731         if (MSG_IS_IGNORE_THREAD(flags)) {
2732                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2733                                           ignorethreadxpm, ignorethreadxpmmask);
2734         } else if (MSG_IS_NEW(flags)) {
2735                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2736                                           newxpm, newxpmmask);
2737         } else if (MSG_IS_UNREAD(flags)) {
2738                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2739                                           unreadxpm, unreadxpmmask);
2740         } else if (MSG_IS_REPLIED(flags)) {
2741                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2742                                           repliedxpm, repliedxpmmask);
2743         } else if (MSG_IS_FORWARDED(flags)) {
2744                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2745                                           forwardedxpm, forwardedxpmmask);
2746         } else {
2747                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2748                                         NULL);
2749         }
2750
2751         if (prefs_common.bold_unread &&
2752             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2753              (!GTK_CTREE_ROW(row)->expanded &&
2754               GTK_CTREE_ROW(row)->children &&
2755               summary_have_unread_children(summaryview, row))))
2756                 style = bold_style;
2757
2758         /* set mark column */
2759         if (MSG_IS_DELETED(flags)) {
2760                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2761                                           deletedxpm, deletedxpmmask);
2762                 if (style)
2763                         style = bold_deleted_style;
2764                 else {
2765                         style = small_deleted_style;
2766                 }
2767                         gtk_ctree_node_set_foreground
2768                                 (ctree, row, &summaryview->color_dim);
2769         } else if (MSG_IS_MARKED(flags)) {
2770                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2771                                           markxpm, markxpmmask);
2772         } else if (MSG_IS_MOVE(flags)) {
2773                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2774                 if (style)
2775                         style = bold_marked_style;
2776                 else {
2777                         style = small_marked_style;
2778                 }
2779                         gtk_ctree_node_set_foreground
2780                                 (ctree, row, &summaryview->color_marked);
2781         } else if (MSG_IS_COPY(flags)) {
2782                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2783                 if (style)
2784                         style = bold_marked_style;
2785                 else {
2786                         style = small_marked_style;
2787                 }
2788                         gtk_ctree_node_set_foreground
2789                                 (ctree, row, &summaryview->color_marked);
2790 #if 0
2791         } else if ((global_scoring ||
2792                   summaryview->folder_item->prefs->scoring) &&
2793                  (msginfo->score >= summaryview->important_score) &&
2794                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2795                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2796                 gtk_ctree_node_set_foreground(ctree, row,
2797                                               &summaryview->color_important);
2798 #endif
2799         } else {
2800                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2801         }
2802
2803         if (MSG_IS_LOCKED(flags)) {
2804                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2805                                           lockedxpm, lockedxpmmask);
2806         }
2807         else {
2808                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2809         }
2810
2811         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2812                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2813                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2814         } else if (MSG_IS_SIGNED(flags)) {
2815                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2816                                           gpgsignedxpm, gpgsignedxpmmask);
2817         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2818                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2819                                           clipkeyxpm, clipkeyxpmmask);
2820         } else if (MSG_IS_ENCRYPTED(flags)) {
2821                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2822                                           keyxpm, keyxpmmask);
2823         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2824                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2825                                           clipxpm, clipxpmmask);
2826         } else {
2827                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2828         }
2829         if (!style)
2830                 style = small_style;
2831
2832         gtk_ctree_node_set_row_style(ctree, row, style);
2833
2834         if (MSG_GET_COLORLABEL(flags))
2835                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2836 }
2837
2838 void summary_set_marks_selected(SummaryView *summaryview)
2839 {
2840         GList *cur;
2841         summary_lock(summaryview);
2842         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
2843              cur = cur->next)
2844                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2845         summary_unlock(summaryview);
2846 }
2847
2848 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2849 {
2850         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2851         MsgInfo *msginfo;
2852
2853         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2854         if (MSG_IS_DELETED(msginfo->flags))
2855                 summaryview->deleted--;
2856         if (MSG_IS_MOVE(msginfo->flags))
2857                 summaryview->moved--;
2858         if (MSG_IS_COPY(msginfo->flags))
2859                 summaryview->copied--;
2860
2861         procmsg_msginfo_set_to_folder(msginfo, NULL);
2862         procmsg_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2863         summary_set_row_marks(summaryview, row);
2864         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2865 }
2866
2867 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2868 {
2869         gboolean changed = FALSE;
2870         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2871         MsgInfo *msginfo;
2872
2873         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2874         if (MSG_IS_DELETED(msginfo->flags))
2875                 summaryview->deleted--;
2876         if (MSG_IS_MOVE(msginfo->flags)) {
2877                 summaryview->moved--;
2878                 changed = TRUE;
2879         }
2880         if (MSG_IS_COPY(msginfo->flags)) {
2881                 summaryview->copied--;
2882                 changed = TRUE;
2883         }
2884         procmsg_msginfo_set_to_folder(msginfo, NULL);
2885         procmsg_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2886         
2887         summary_set_row_marks(summaryview, row);
2888         debug_print("Message %d is locked\n", msginfo->msgnum);
2889 }
2890
2891 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2892 {
2893         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2894         MsgInfo *msginfo;
2895
2896         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2897         if (!MSG_IS_LOCKED(msginfo->flags))
2898                 return;
2899         procmsg_msginfo_set_to_folder(msginfo, NULL);
2900         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2901         summary_set_row_marks(summaryview, row);
2902         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2903 }
2904
2905 void summary_mark(SummaryView *summaryview)
2906 {
2907         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2908         GList *cur;
2909
2910         START_LONG_OPERATION(summaryview);
2911         folder_item_set_batch(summaryview->folder_item, TRUE);
2912         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2913                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2914         folder_item_set_batch(summaryview->folder_item, FALSE);
2915         END_LONG_OPERATION(summaryview);
2916
2917         summary_status_show(summaryview);
2918 }
2919
2920 static void summary_mark_row_as_read(SummaryView *summaryview,
2921                                      GtkCTreeNode *row)
2922 {
2923         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2924         MsgInfo *msginfo;
2925
2926         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2927
2928         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2929                 return;
2930
2931         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2932         && procmsg_msg_has_marked_parent(msginfo))
2933                 summaryview->unreadmarked--;
2934
2935         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2936         summary_set_row_marks(summaryview, row);
2937         debug_print("Message %d is marked as read\n",
2938                 msginfo->msgnum);
2939 }
2940
2941 void summary_mark_as_read(SummaryView *summaryview)
2942 {
2943         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2944         GList *cur;
2945
2946         START_LONG_OPERATION(summaryview);
2947         folder_item_set_batch(summaryview->folder_item, TRUE);
2948         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2949                 summary_mark_row_as_read(summaryview,
2950                                          GTK_CTREE_NODE(cur->data));
2951         folder_item_set_batch(summaryview->folder_item, FALSE);
2952         END_LONG_OPERATION(summaryview);
2953         
2954         summary_status_show(summaryview);
2955 }
2956
2957 void summary_msgs_lock(SummaryView *summaryview)
2958 {
2959         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2960         GList *cur;
2961
2962         START_LONG_OPERATION(summaryview);
2963         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2964                 summary_lock_row(summaryview,
2965                                          GTK_CTREE_NODE(cur->data));
2966         END_LONG_OPERATION(summaryview);
2967         
2968         summary_status_show(summaryview);
2969 }
2970
2971 void summary_msgs_unlock(SummaryView *summaryview)
2972 {
2973         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2974         GList *cur;
2975
2976         START_LONG_OPERATION(summaryview);
2977         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2978                 summary_unlock_row(summaryview,
2979                                    GTK_CTREE_NODE(cur->data));
2980         END_LONG_OPERATION(summaryview);
2981         
2982         summary_status_show(summaryview);
2983 }
2984
2985 void summary_mark_all_read(SummaryView *summaryview)
2986 {
2987         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2988         GtkCTreeNode *node;
2989
2990         START_LONG_OPERATION(summaryview);
2991         folder_item_set_batch(summaryview->folder_item, TRUE);
2992         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2993              node = gtkut_ctree_node_next(ctree, node))
2994                 summary_mark_row_as_read(summaryview, node);
2995         folder_item_set_batch(summaryview->folder_item, FALSE);
2996         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2997              node = gtkut_ctree_node_next(ctree, node)) {
2998                 if (!GTK_CTREE_ROW(node)->expanded)
2999                         summary_set_row_marks(summaryview, node);
3000         }
3001         END_LONG_OPERATION(summaryview);
3002         
3003         summary_status_show(summaryview);
3004 }
3005
3006 static void summary_mark_row_as_unread(SummaryView *summaryview,
3007                                        GtkCTreeNode *row)
3008 {
3009         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3010         MsgInfo *msginfo;
3011
3012         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3013         if (MSG_IS_DELETED(msginfo->flags)) {
3014                 procmsg_msginfo_set_to_folder(msginfo, NULL);
3015                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
3016                 summaryview->deleted--;
3017         }
3018
3019         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
3020         && procmsg_msg_has_marked_parent(msginfo))
3021                 summaryview->unreadmarked++;
3022
3023         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
3024         debug_print("Message %d is marked as unread\n",
3025                 msginfo->msgnum);
3026
3027         summary_set_row_marks(summaryview, row);
3028 }
3029
3030 void summary_mark_as_unread(SummaryView *summaryview)
3031 {
3032         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3033         GList *cur;
3034
3035         START_LONG_OPERATION(summaryview);
3036         folder_item_set_batch(summaryview->folder_item, TRUE);
3037         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3038                 cur = cur->next)
3039                 summary_mark_row_as_unread(summaryview,
3040                                            GTK_CTREE_NODE(cur->data));
3041         folder_item_set_batch(summaryview->folder_item, FALSE);
3042         END_LONG_OPERATION(summaryview);
3043         
3044         summary_status_show(summaryview);
3045 }
3046
3047 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
3048 {
3049         GList * cur;
3050         gboolean found;
3051
3052         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
3053
3054         case F_NEWS:
3055
3056                 /*
3057                   security : checks if one the accounts correspond to
3058                   the author of the post
3059                 */
3060
3061                 found = FALSE;
3062                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3063                         PrefsAccount * account;
3064                         gchar * from_name;
3065                         
3066                         account = cur->data;
3067                         if (account->name && *account->name)
3068                                 from_name =
3069                                         g_strdup_printf("%s <%s>",
3070                                                         account->name,
3071                                                         account->address);
3072                         else
3073                                 from_name =
3074                                         g_strdup_printf("%s",
3075                                                         account->address);
3076                         
3077                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3078                                 g_free(from_name);
3079                                 found = TRUE;
3080                                 break;
3081                         }
3082                         g_free(from_name);
3083                 }
3084
3085                 if (!found) {
3086                         alertpanel_error(_("You're not the author of the article\n"));
3087                 }
3088                 
3089                 return found;
3090
3091         default:
3092                 return TRUE;
3093         }
3094 }
3095
3096 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3097 {
3098         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3099         MsgInfo *msginfo;
3100
3101         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3102
3103         if (!check_permission(summaryview, msginfo))
3104                 return;
3105
3106         if (MSG_IS_LOCKED(msginfo->flags)) return;
3107
3108         if (MSG_IS_DELETED(msginfo->flags)) return;
3109
3110         if (MSG_IS_MOVE(msginfo->flags))
3111                 summaryview->moved--;
3112         if (MSG_IS_COPY(msginfo->flags))
3113                 summaryview->copied--;
3114
3115         procmsg_msginfo_set_to_folder(msginfo, NULL);
3116         procmsg_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, MSG_MOVE | MSG_COPY);
3117         summaryview->deleted++;
3118
3119         if (!prefs_common.immediate_exec && 
3120             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3121                 summary_set_row_marks(summaryview, row);
3122
3123         debug_print("Message %s/%d is set to delete\n",
3124                     msginfo->folder->path, msginfo->msgnum);
3125 }
3126
3127 void summary_cancel(SummaryView *summaryview)
3128 {
3129         MsgInfo * msginfo;
3130         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3131
3132         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3133                                               summaryview->selected);
3134         if (!msginfo) return;
3135
3136         if (!check_permission(summaryview, msginfo))
3137                 return;
3138
3139         news_cancel_article(summaryview->folder_item->folder, msginfo);
3140         
3141         if (summary_is_locked(summaryview)) return;
3142
3143         summary_lock(summaryview);
3144
3145         gtk_clist_freeze(clist);
3146
3147         summary_update_status(summaryview);
3148         summary_status_show(summaryview);
3149
3150         gtk_clist_thaw(clist);
3151
3152         summary_unlock(summaryview);
3153 }
3154
3155 void summary_delete(SummaryView *summaryview)
3156 {
3157         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3158         FolderItem *item = summaryview->folder_item;
3159         GList *cur;
3160         GtkCTreeNode *sel_last = NULL;
3161         GtkCTreeNode *node;
3162         AlertValue aval;
3163         MsgInfo *msginfo;
3164
3165         if (!item) return;
3166
3167         if (summary_is_locked(summaryview)) return;
3168
3169         if (!summaryview->folder_item ||
3170             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3171
3172         aval = alertpanel(_("Delete message(s)"),
3173                           _("Do you really want to delete selected message(s)?"),
3174                           GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3175         if (aval != G_ALERTDEFAULT) return;
3176
3177         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3178              cur = cur->next) {
3179                 GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3180                 msginfo = gtk_ctree_node_get_row_data(ctree, row);
3181                 if (msginfo->total_size != 0 && 
3182                     msginfo->size != (off_t)msginfo->total_size)
3183                         partial_mark_for_delete(msginfo);
3184         }
3185
3186         main_window_cursor_wait(summaryview->mainwin);
3187
3188         /* next code sets current row focus right. We need to find a row
3189          * that is not deleted. */
3190         summary_lock(summaryview);
3191         folder_item_update_freeze();
3192         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3193         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3194                 sel_last = GTK_CTREE_NODE(cur->data);
3195                 summary_delete_row(summaryview, sel_last);
3196         }
3197         folder_item_update_thaw();
3198         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3199         summary_unlock(summaryview);
3200
3201         node = summary_find_next_msg(summaryview, sel_last);
3202         if (!node)
3203                 node = summary_find_prev_msg(summaryview, sel_last);
3204
3205         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3206         
3207         if (prefs_common.immediate_exec || folder_has_parent_of_type(item, F_TRASH)) {
3208                 summary_execute(summaryview);
3209                 /* after deleting, the anchor may be at an invalid row
3210                  * so reset it to the node we found earlier */
3211                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3212         } else
3213                 summary_status_show(summaryview);
3214                 
3215         main_window_cursor_normal(summaryview->mainwin);
3216 }
3217
3218 void summary_delete_trash(SummaryView *summaryview)
3219 {
3220         FolderItem *to_folder = NULL;
3221         PrefsAccount *ac;
3222         if (!summaryview->folder_item ||
3223             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3224         
3225         if (NULL != (ac = account_find_from_item(summaryview->folder_item)))
3226                 to_folder = account_get_special_folder(ac, F_TRASH);
3227
3228         if (to_folder == NULL)
3229                 to_folder = summaryview->folder_item->folder->trash;
3230         
3231         if (to_folder == NULL || to_folder == summaryview->folder_item
3232             || folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3233                 summary_delete(summaryview);
3234         else
3235                 summary_move_selected_to(summaryview, to_folder);
3236 }
3237
3238
3239 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3240 {
3241         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3242         MsgInfo *msginfo;
3243
3244         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3245         if (MSG_IS_DELETED(msginfo->flags))
3246                 summaryview->deleted--;
3247         if (MSG_IS_MOVE(msginfo->flags))
3248                 summaryview->moved--;
3249         if (MSG_IS_COPY(msginfo->flags))
3250                 summaryview->copied--;
3251
3252         procmsg_msginfo_set_to_folder(msginfo, NULL);
3253         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3254         summary_set_row_marks(summaryview, row);
3255
3256         debug_print("Message %s/%d is unmarked\n",
3257                     msginfo->folder->path, msginfo->msgnum);
3258 }
3259
3260 void summary_unmark(SummaryView *summaryview)
3261 {
3262         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3263         GList *cur;
3264
3265         START_LONG_OPERATION(summaryview);
3266         folder_item_set_batch(summaryview->folder_item, TRUE);
3267         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3268                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3269         folder_item_set_batch(summaryview->folder_item, FALSE);
3270         END_LONG_OPERATION(summaryview);
3271         
3272         summary_status_show(summaryview);
3273 }
3274
3275 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3276                                 FolderItem *to_folder)
3277 {
3278         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3279         MsgInfo *msginfo;
3280
3281         g_return_if_fail(to_folder != NULL);
3282
3283         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3284         if (MSG_IS_LOCKED(msginfo->flags))
3285                 return;
3286
3287         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3288         if (MSG_IS_DELETED(msginfo->flags))
3289                 summaryview->deleted--;
3290         if (MSG_IS_COPY(msginfo->flags)) {
3291                 summaryview->copied--;
3292         }
3293         if (!MSG_IS_MOVE(msginfo->flags)) {
3294                 procmsg_msginfo_change_flags(msginfo, 0, MSG_MOVE, MSG_DELETED, MSG_COPY);
3295                 summaryview->moved++;
3296         } else {
3297                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_COPY);
3298         }
3299         
3300         if (!prefs_common.immediate_exec) {
3301                 summary_set_row_marks(summaryview, row);
3302         }
3303
3304         debug_print("Message %d is set to move to %s\n",
3305                     msginfo->msgnum, to_folder->path);
3306 }
3307
3308 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3309 {
3310         GList *cur;
3311
3312         if (!to_folder) return;
3313         if (!summaryview->folder_item ||
3314             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3315
3316         if (summary_is_locked(summaryview)) return;
3317
3318         if (summaryview->folder_item == to_folder) {
3319                 alertpanel_error(_("Destination is same as current folder."));
3320                 return;
3321         }
3322
3323         START_LONG_OPERATION(summaryview);
3324
3325         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3326              cur != NULL && cur->data != NULL; cur = cur->next)
3327                 summary_move_row_to
3328                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3329
3330         END_LONG_OPERATION(summaryview);
3331
3332         if (prefs_common.immediate_exec) {
3333                 summary_execute(summaryview);
3334         } else {
3335                 summary_status_show(summaryview);
3336         }
3337         
3338         if (!summaryview->selected) { /* this was the last message */
3339                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3340                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3341                 if (node)
3342                         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3343         }
3344
3345 }
3346
3347 void summary_move_to(SummaryView *summaryview)
3348 {
3349         FolderItem *to_folder;
3350
3351         if (!summaryview->folder_item ||
3352             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3353
3354         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3355                                          FOLDER_SEL_MOVE, NULL);
3356         summary_move_selected_to(summaryview, to_folder);
3357 }
3358
3359 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3360                                 FolderItem *to_folder)
3361 {
3362         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3363         MsgInfo *msginfo;
3364
3365         g_return_if_fail(to_folder != NULL);
3366
3367         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3368         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3369         if (MSG_IS_DELETED(msginfo->flags))
3370                 summaryview->deleted--;
3371         if (MSG_IS_MOVE(msginfo->flags)) {
3372                 summaryview->moved--;
3373         }
3374         
3375         if (!MSG_IS_COPY(msginfo->flags)) {
3376                 procmsg_msginfo_change_flags(msginfo, 0, MSG_COPY, MSG_DELETED, MSG_MOVE);
3377                 summaryview->copied++;
3378         } else {
3379                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE);
3380         }
3381         if (!prefs_common.immediate_exec) {
3382                 summary_set_row_marks(summaryview, row);
3383         }
3384
3385         debug_print("Message %d is set to copy to %s\n",
3386                     msginfo->msgnum, to_folder->path);
3387 }
3388
3389 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3390 {
3391         GList *cur;
3392
3393         if (!to_folder) return;
3394         if (!summaryview->folder_item) return;
3395
3396         if (summary_is_locked(summaryview)) return;
3397
3398         if (summaryview->folder_item == to_folder) {
3399                 alertpanel_error
3400                         (_("Destination to copy is same as current folder."));
3401                 return;
3402         }
3403
3404         START_LONG_OPERATION(summaryview);
3405
3406         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3407              cur != NULL && cur->data != NULL; cur = cur->next)
3408                 summary_copy_row_to
3409                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3410
3411         END_LONG_OPERATION(summaryview);
3412
3413         if (prefs_common.immediate_exec)
3414                 summary_execute(summaryview);
3415         else {
3416                 summary_status_show(summaryview);
3417         }
3418 }
3419
3420 void summary_copy_to(SummaryView *summaryview)
3421 {
3422         FolderItem *to_folder;
3423
3424         if (!summaryview->folder_item) return;
3425
3426         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3427                                          FOLDER_SEL_COPY, NULL);
3428         summary_copy_selected_to(summaryview, to_folder);
3429 }
3430
3431 void summary_add_address(SummaryView *summaryview)
3432 {
3433         MsgInfo *msginfo;
3434         gchar *from;
3435
3436         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3437                                               summaryview->selected);
3438         if (!msginfo) return;
3439
3440         Xstrdup_a(from, msginfo->from, return);
3441         eliminate_address_comment(from);
3442         extract_address(from);
3443         addressbook_add_contact(msginfo->fromname, from, NULL);
3444 }
3445
3446 void summary_select_all(SummaryView *summaryview)
3447 {
3448         if (!summaryview->folder_item) return;
3449
3450         summary_lock(summaryview);
3451         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3452         summary_unlock(summaryview);
3453         summary_status_show(summaryview);
3454 }
3455
3456 void summary_unselect_all(SummaryView *summaryview)
3457 {
3458         summary_lock(summaryview);
3459         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3460         summary_unlock(summaryview);
3461         summary_status_show(summaryview);
3462 }
3463
3464 void summary_select_thread(SummaryView *summaryview)
3465 {
3466         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3467         GtkCTreeNode *node = summaryview->selected;
3468
3469         if (!node) return;
3470
3471         while (GTK_CTREE_ROW(node)->parent != NULL)
3472                 node = GTK_CTREE_ROW(node)->parent;
3473
3474         if (node != summaryview->selected)
3475                 summary_select_node
3476                         (summaryview, node,
3477                          messageview_is_visible(summaryview->messageview),
3478                          FALSE);
3479
3480         gtk_ctree_select_recursive(ctree, node);
3481
3482         summary_status_show(summaryview);
3483 }
3484
3485 void summary_save_as(SummaryView *summaryview)
3486 {
3487         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3488         MsgInfo *msginfo;
3489         gchar *filename = NULL;
3490         gchar *src, *dest;
3491         gchar *tmp;
3492
3493         AlertValue aval = 0;
3494
3495         if (!summaryview->selected) return;
3496         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3497         if (!msginfo) return;
3498
3499         if (msginfo->subject) {
3500                 Xstrdup_a(filename, msginfo->subject, return);
3501                 subst_for_filename(filename);
3502         }
3503         if (g_getenv ("G_BROKEN_FILENAMES") &&
3504             filename && !g_utf8_validate(filename, -1, NULL)) {
3505                 gchar *oldstr = filename;
3506                 filename = conv_codeset_strdup(filename,
3507                                                conv_get_locale_charset_str(),
3508                                                CS_UTF_8);
3509                 if (!filename) {
3510                         g_warning("summary_save_as(): faild to convert character set.");
3511                         filename = g_strdup(oldstr);
3512                 }
3513                 dest = filesel_select_file_save(_("Save as"), filename);
3514                 g_free(filename);
3515         } else
3516                 dest = filesel_select_file_save(_("Save as"), filename);
3517         filename = NULL;
3518         if (!dest) return;
3519         if (is_file_exist(dest)) {
3520                 aval = alertpanel(_("Append or Overwrite"),
3521                                   _("Append or overwrite existing file?"),
3522                                   _("Append"), _("Overwrite"),
3523                                   GTK_STOCK_CANCEL);
3524                 if (aval != 0 && aval != 1)
3525                         return;
3526         }
3527
3528         src = procmsg_get_message_file(msginfo);
3529         tmp = g_path_get_basename(dest);
3530
3531         if ( aval==0 ) { /* append */
3532                 if (append_file(src, dest, TRUE) < 0) 
3533                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3534         } else { /* overwrite */
3535                 if (copy_file(src, dest, TRUE) < 0)
3536                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3537         }
3538         g_free(src);
3539         
3540         /*
3541          * If two or more msgs are selected,
3542          * append them to the output file.
3543          */
3544         if (GTK_CLIST(ctree)->selection->next) {
3545                 GList *item;
3546                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3547                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3548                         if (!msginfo) break;
3549                         src = procmsg_get_message_file(msginfo);
3550                         if (append_file(src, dest, TRUE) < 0)
3551                                 alertpanel_error(_("Can't save the file '%s'."), tmp);
3552                 }
3553                 g_free(src);
3554         }
3555         g_free(dest);
3556         g_free(tmp);
3557 }
3558
3559 void summary_print(SummaryView *summaryview)
3560 {
3561         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3562 #ifndef USE_GNOMEPRINT
3563         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3564         MsgInfo *msginfo;
3565         gchar *cmdline = NULL;
3566         gchar *p;
3567 #else
3568         gboolean first = TRUE;
3569         GList *tmplist;
3570 #endif
3571         GList *cur;
3572
3573         if (clist->selection == NULL) return;
3574 #ifndef USE_GNOMEPRINT
3575         cmdline = input_dialog(_("Print"),
3576                                _("Enter the print command line:\n"
3577                                  "('%s' will be replaced with file name)"),
3578                                prefs_common.print_cmd);
3579         if (!cmdline) return;
3580         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3581             strchr(p + 2, '%')) {
3582                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
3583                                  cmdline);
3584                 g_free(cmdline);
3585                 return;
3586         }
3587         for (cur = clist->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3588                 msginfo = gtk_ctree_node_get_row_data
3589                         (ctree, GTK_CTREE_NODE(cur->data));
3590                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3591         }
3592
3593         g_free(cmdline);
3594         
3595 #else
3596         tmplist = g_list_copy(clist->selection);
3597         summary_unselect_all(summaryview);
3598         for (cur = tmplist; cur != NULL && cur->data != NULL; cur = cur->next) {
3599                 GtkCTreeNode *node = GTK_CTREE_NODE(cur->data);
3600                 summary_select_node(summaryview, node, TRUE, TRUE);
3601                 if (!summaryview->messageview->mimeview 
3602                 ||  !summaryview->messageview->mimeview->textview
3603                 ||  !summaryview->messageview->mimeview->textview->text)
3604                         alertpanel_warning(_("Cannot print: the message doesn't "
3605                                              "contain text."));
3606                 gedit_print(
3607                         GTK_TEXT_VIEW(summaryview->messageview->mimeview
3608                                         ->textview->text));
3609         }
3610         for (cur = tmplist; cur != NULL && cur->data != NULL; cur = cur->next) {
3611                 GtkCTreeNode *node = GTK_CTREE_NODE(cur->data);
3612                 gtk_sctree_select_with_state(
3613                         GTK_SCTREE(clist), node, first ? 0:GDK_CONTROL_MASK);
3614                 first = FALSE;
3615         }
3616         g_list_free(tmplist);
3617 #endif
3618 }
3619
3620 gboolean summary_execute(SummaryView *summaryview)
3621 {
3622         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3623         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3624         GtkCTreeNode *node, *next;
3625         GtkCTreeNode *new_selected = NULL;
3626         gint move_val = -1;
3627
3628         if (!summaryview->folder_item) return FALSE;
3629
3630         if (summary_is_locked(summaryview)) return FALSE;
3631         summary_lock(summaryview);
3632
3633         gtk_clist_freeze(clist);
3634
3635         if (summaryview->threaded)
3636                 summary_unthread_for_exec(summaryview);
3637
3638         folder_item_update_freeze();
3639         move_val = summary_execute_move(summaryview);
3640         summary_execute_copy(summaryview);
3641         summary_execute_delete(summaryview);
3642         
3643         node = GTK_CTREE_NODE(clist->row_list);
3644         for (; node != NULL; node = next) {
3645                 next = gtkut_ctree_node_next(ctree, node);
3646                 if (gtk_ctree_node_get_row_data(ctree, node) != NULL) continue;
3647
3648                 if (node == summaryview->displayed) {
3649                         messageview_clear(summaryview->messageview);
3650                         summaryview->displayed = NULL;
3651                 }
3652                 if (GTK_CTREE_ROW(node)->children != NULL) {
3653                         g_warning("summary_execute(): children != NULL\n");
3654                         continue;
3655                 }
3656
3657                 if (!new_selected &&
3658                     gtkut_ctree_node_is_selected(ctree, node)) {
3659                         summary_unselect_all(summaryview);
3660                         new_selected = summary_find_next_msg(summaryview, node);
3661                         if (!new_selected)
3662                                 new_selected = summary_find_prev_msg
3663                                         (summaryview, node);
3664                 }
3665
3666                 gtk_ctree_remove_node(ctree, node);
3667         }
3668
3669         folder_item_update_thaw();
3670         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3671
3672         if (new_selected) {
3673                 summary_unlock(summaryview);
3674                 gtk_sctree_select
3675                         (GTK_SCTREE(ctree),
3676                          new_selected);
3677                 summary_lock(summaryview);
3678         }
3679
3680         if (summaryview->threaded) {
3681                 gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3682                 summary_thread_build(summaryview);
3683                 summary_thread_init(summaryview);
3684                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3685         }
3686
3687         summaryview->selected = clist->selection ?
3688                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3689
3690         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3691                 menu_set_insensitive_all
3692                         (GTK_MENU_SHELL(summaryview->popupmenu));
3693                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3694         } else
3695                 gtk_widget_grab_focus(summaryview->ctree);
3696
3697         summary_update_status(summaryview);
3698         summary_status_show(summaryview);
3699
3700         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3701
3702         summary_unlock(summaryview);
3703         
3704         if (move_val < 0) 
3705                 summary_show(summaryview, summaryview->folder_item);
3706         return TRUE;
3707 }
3708
3709 static gint summary_execute_move(SummaryView *summaryview)
3710 {
3711         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3712         GSList *cur;
3713         gint val = -1;
3714         /* search moving messages and execute */
3715         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3716                                 summaryview);
3717
3718         if (summaryview->mlist) {
3719                 val = procmsg_move_messages(summaryview->mlist);
3720
3721                 for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3722                         procmsg_msginfo_free((MsgInfo *)cur->data);
3723                 g_slist_free(summaryview->mlist);
3724                 summaryview->mlist = NULL;
3725         }
3726         return val;
3727 }
3728
3729 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3730                                       gpointer data)
3731 {
3732         SummaryView *summaryview = data;
3733         MsgInfo *msginfo;
3734
3735         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3736
3737         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3738                 summaryview->mlist =
3739                         g_slist_prepend(summaryview->mlist, msginfo);
3740                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3741
3742                 if (msginfo->msgid && *msginfo->msgid &&
3743                     node == g_hash_table_lookup(summaryview->msgid_table,
3744                                                 msginfo->msgid))
3745                         g_hash_table_remove(summaryview->msgid_table,
3746                                             msginfo->msgid);
3747         }
3748 }
3749
3750 static void summary_execute_copy(SummaryView *summaryview)
3751 {
3752         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3753
3754         /* search copying messages and execute */
3755         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3756                                 summaryview);
3757
3758         if (summaryview->mlist) {
3759                 summaryview->mlist = g_slist_reverse(summaryview->mlist);
3760                 procmsg_copy_messages(summaryview->mlist);
3761
3762                 g_slist_free(summaryview->mlist);
3763                 summaryview->mlist = NULL;
3764         }
3765 }
3766
3767 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3768                                       gpointer data)
3769 {
3770         SummaryView *summaryview = data;
3771         MsgInfo *msginfo;
3772
3773         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3774
3775         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3776                 summaryview->mlist =
3777                         g_slist_prepend(summaryview->mlist, msginfo);
3778
3779                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3780                 summary_set_row_marks(summaryview, node);
3781         }
3782 }
3783
3784 static void summary_execute_delete(SummaryView *summaryview)
3785 {
3786         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3787         GSList *cur;
3788
3789         /* search deleting messages and execute */
3790         gtk_ctree_pre_recursive
3791                 (ctree, NULL, summary_execute_delete_func, summaryview);
3792
3793         if (!summaryview->mlist) return;
3794
3795         folder_item_remove_msgs(summaryview->folder_item,
3796                                 summaryview->mlist);
3797
3798         for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3799                 procmsg_msginfo_free((MsgInfo *)cur->data);
3800
3801         g_slist_free(summaryview->mlist);
3802         summaryview->mlist = NULL;
3803 }
3804
3805 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3806                                         gpointer data)
3807 {
3808         SummaryView *summaryview = data;
3809         MsgInfo *msginfo;
3810
3811         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3812
3813         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3814                 summaryview->mlist =
3815                         g_slist_append(summaryview->mlist, msginfo);
3816                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3817
3818                 if (msginfo->msgid && *msginfo->msgid &&
3819                     node == g_hash_table_lookup(summaryview->msgid_table,
3820                                                 msginfo->msgid)) {
3821                         g_hash_table_remove(summaryview->msgid_table,
3822                                             msginfo->msgid);
3823                 }       
3824                 if (msginfo->subject && *msginfo->subject && 
3825                     node == subject_table_lookup(summaryview->subject_table,
3826                                                  msginfo->subject)) {
3827                         subject_table_remove(summaryview->subject_table,
3828                                              msginfo->subject);
3829                 }                                           
3830         }
3831 }
3832
3833 /* thread functions */
3834
3835 void summary_thread_build(SummaryView *summaryview)
3836 {
3837         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3838         GtkCTreeNode *node;
3839         GtkCTreeNode *next;
3840         GtkCTreeNode *parent;
3841         MsgInfo *msginfo;
3842         GSList *reflist;
3843
3844         summary_lock(summaryview);
3845
3846         debug_print("Building threads...");
3847         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3848         main_window_cursor_wait(summaryview->mainwin);
3849
3850         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3851                     &