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