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