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