2006-02-11 [colin] 2.0.0cvs43
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkpixmap.h>
28 #include <gtk/gtkctree.h>
29 #include <gtk/gtkcontainer.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkitemfactory.h>
35 #include <gtk/gtkvbox.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkwindow.h>
38 #include <gtk/gtkstyle.h>
39 #include <gtk/gtkarrow.h>
40 #include <gtk/gtkeventbox.h>
41 #include <gtk/gtkstatusbar.h>
42 #include <gtk/gtkmenuitem.h>
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48
49 #include "main.h"
50 #include "menu.h"
51 #include "mbox.h"
52 #include "mainwindow.h"
53 #include "folderview.h"
54 #include "summaryview.h"
55 #include "messageview.h"
56 #include "foldersel.h"
57 #include "procmsg.h"
58 #include "procheader.h"
59 #include "sourcewindow.h"
60 #include "prefs_common.h"
61 #include "prefs_summary_column.h"
62 #include "prefs_filtering.h"
63 #include "account.h"
64 #include "compose.h"
65 #include "utils.h"
66 #include "gtkutils.h"
67 #include "stock_pixmap.h"
68 #include "filesel.h"
69 #include "alertpanel.h"
70 #include "inputdialog.h"
71 #include "statusbar.h"
72 #include "folder.h"
73 #include "colorlabel.h"
74 #include "inc.h"
75 #include "imap.h"
76 #include "addressbook.h"
77 #include "addr_compl.h"
78 #include "folder_item_prefs.h"
79 #include "filtering.h"
80 #include "string_match.h"
81 #include "toolbar.h"
82 #include "news.h"
83 #include "hooks.h"
84 #include "description_window.h"
85 #include "folderutils.h"
86 #include "quicksearch.h"
87 #include "partial_download.h"
88 #include "timing.h"
89 #include "gedit-print.h"
90
91 #define SUMMARY_COL_MARK_WIDTH          10
92 #define SUMMARY_COL_STATUS_WIDTH        13
93 #define SUMMARY_COL_LOCKED_WIDTH        13
94 #define SUMMARY_COL_MIME_WIDTH          11
95
96
97 static GtkStyle *bold_style;
98 static GtkStyle *bold_marked_style;
99 static GtkStyle *bold_deleted_style;
100 static GtkStyle *small_style;
101 static GtkStyle *small_marked_style;
102 static GtkStyle *small_deleted_style;
103
104 static GdkPixmap *markxpm;
105 static GdkBitmap *markxpmmask;
106 static GdkPixmap *deletedxpm;
107 static GdkBitmap *deletedxpmmask;
108
109 static GdkPixmap *newxpm;
110 static GdkBitmap *newxpmmask;
111 static GdkPixmap *unreadxpm;
112 static GdkBitmap *unreadxpmmask;
113 static GdkPixmap *repliedxpm;
114 static GdkBitmap *repliedxpmmask;
115 static GdkPixmap *forwardedxpm;
116 static GdkBitmap *forwardedxpmmask;
117 static GdkPixmap *ignorethreadxpm;
118 static GdkBitmap *ignorethreadxpmmask;
119 static GdkPixmap *lockedxpm;
120 static GdkBitmap *lockedxpmmask;
121
122 static GdkPixmap *clipxpm;
123 static GdkBitmap *clipxpmmask;
124 static GdkPixmap *keyxpm;
125 static GdkBitmap *keyxpmmask;
126 static GdkPixmap *clipkeyxpm;
127 static GdkBitmap *clipkeyxpmmask;
128 static GdkPixmap *gpgsignedxpm;
129 static GdkBitmap *gpgsignedxpmmask;
130 static GdkPixmap *clipgpgsignedxpm;
131 static GdkBitmap *clipgpgsignedxpmmask;
132
133 static void summary_free_msginfo_func   (GtkCTree               *ctree,
134                                          GtkCTreeNode           *node,
135                                          gpointer                data);
136 static void summary_set_marks_func      (GtkCTree               *ctree,
137                                          GtkCTreeNode           *node,
138                                          gpointer                data);
139
140 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
141
142 static void summary_set_hide_read_msgs_menu (SummaryView *summaryview,
143                                              guint action);
144
145 static guint summary_get_msgnum         (SummaryView            *summaryview,
146                                          GtkCTreeNode           *node);
147
148 static GtkCTreeNode *summary_find_prev_msg
149                                         (SummaryView            *summaryview,
150                                          GtkCTreeNode           *current_node);
151 static GtkCTreeNode *summary_find_next_msg
152                                         (SummaryView            *summaryview,
153                                          GtkCTreeNode           *current_node);
154
155 static GtkCTreeNode *summary_find_prev_flagged_msg
156                                         (SummaryView    *summaryview,
157                                          GtkCTreeNode   *current_node,
158                                          MsgPermFlags    flags,
159                                          gboolean        start_from_prev);
160 static GtkCTreeNode *summary_find_next_flagged_msg
161                                         (SummaryView    *summaryview,
162                                          GtkCTreeNode   *current_node,
163                                          MsgPermFlags    flags,
164                                          gboolean        start_from_next);
165
166 static GtkCTreeNode *summary_find_msg_by_msgnum
167                                         (SummaryView            *summaryview,
168                                          guint                   msgnum);
169
170 static void summary_update_status       (SummaryView            *summaryview);
171
172 /* display functions */
173 static void summary_status_show         (SummaryView            *summaryview);
174 static void summary_set_column_titles   (SummaryView            *summaryview);
175 static void summary_set_ctree_from_list (SummaryView            *summaryview,
176                                          GSList                 *mlist);
177 static void summary_set_header          (SummaryView            *summaryview,
178                                          gchar                  *text[],
179                                          MsgInfo                *msginfo,
180                                          gboolean               *free_from_to);
181 static void summary_display_msg         (SummaryView            *summaryview,
182                                          GtkCTreeNode           *row);
183 static void summary_display_msg_full    (SummaryView            *summaryview,
184                                          GtkCTreeNode           *row,
185                                          gboolean                new_window,
186                                          gboolean                all_headers);
187 static void summary_set_row_marks       (SummaryView            *summaryview,
188                                          GtkCTreeNode           *row);
189
190 /* message handling */
191 static void summary_mark_row            (SummaryView            *summaryview,
192                                          GtkCTreeNode           *row);
193 static void summary_lock_row            (SummaryView            *summaryview,
194                                          GtkCTreeNode           *row);
195 static void summary_unlock_row          (SummaryView            *summaryview,
196                                          GtkCTreeNode           *row);
197 static void summary_mark_row_as_read    (SummaryView            *summaryview,
198                                          GtkCTreeNode           *row);
199 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
200                                          GtkCTreeNode           *row);
201 static void summary_delete_row          (SummaryView            *summaryview,
202                                          GtkCTreeNode           *row);
203 static void summary_unmark_row          (SummaryView            *summaryview,
204                                          GtkCTreeNode           *row);
205 static void summary_move_row_to         (SummaryView            *summaryview,
206                                          GtkCTreeNode           *row,
207                                          FolderItem             *to_folder);
208 static void summary_copy_row_to         (SummaryView            *summaryview,
209                                          GtkCTreeNode           *row,
210                                          FolderItem             *to_folder);
211
212 static gint summary_execute_move        (SummaryView            *summaryview);
213 static void summary_execute_move_func   (GtkCTree               *ctree,
214                                          GtkCTreeNode           *node,
215                                          gpointer                data);
216 static void summary_execute_copy        (SummaryView            *summaryview);
217 static void summary_execute_copy_func   (GtkCTree               *ctree,
218                                          GtkCTreeNode           *node,
219                                          gpointer                data);
220 static void summary_execute_delete      (SummaryView            *summaryview);
221 static void summary_execute_delete_func (GtkCTree               *ctree,
222                                          GtkCTreeNode           *node,
223                                          gpointer                data);
224
225 static void summary_thread_init         (SummaryView            *summaryview);
226 static void summary_ignore_thread       (SummaryView            *summaryview);
227 static void summary_unignore_thread     (SummaryView            *summaryview);
228
229 static void summary_unthread_for_exec           (SummaryView    *summaryview);
230 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
231                                                  GtkCTreeNode   *node,
232                                                  gpointer        data);
233
234 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
235                               GSList * mlist);
236
237 #if 0
238 void summary_processing(SummaryView *summaryview, GSList * mlist);
239 #endif
240 static void summary_filter_func         (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 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
820 {
821         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
822         GtkCTreeNode *node;
823         GSList *mlist = NULL;
824         gchar *buf;
825         gboolean is_refresh;
826         guint selected_msgnum = 0;
827         guint displayed_msgnum = 0;
828         GSList *cur;
829         GSList *not_killed;
830         gboolean hidden_removed = FALSE;
831
832         if (summary_is_locked(summaryview)) return FALSE;
833
834         if (!summaryview->mainwin)
835                 return FALSE;
836
837         inc_lock();
838         summary_lock(summaryview);
839
840         if (!prefs_common.summary_quicksearch_sticky
841          && !prefs_common.summary_quicksearch_recurse
842          && !quicksearch_is_running(summaryview->quicksearch)) {
843                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
844         }
845
846         /* STATUSBAR_POP(summaryview->mainwin); */
847
848         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
849
850         if (is_refresh) {
851                 selected_msgnum = summary_get_msgnum(summaryview,
852                                                      summaryview->selected);
853                 displayed_msgnum = summary_get_msgnum(summaryview,
854                                                       summaryview->displayed);
855         }
856
857         /* process the marks if any */
858         if (summaryview->mainwin->lock_count == 0 &&
859             (summaryview->moved > 0 || summaryview->copied > 0)) {
860                 AlertValue val;
861                 gboolean changed = FALSE;
862
863                 val = alertpanel(_("Process mark"),
864                                  _("Some marks are left. Process them?"),
865                                  GTK_STOCK_NO, GTK_STOCK_YES, GTK_STOCK_CANCEL);
866                 if (G_ALERTALTERNATE == val) {
867                         summary_unlock(summaryview);
868                         summary_execute(summaryview);
869                         summary_lock(summaryview);
870                         changed = TRUE;
871                 } else if (G_ALERTDEFAULT == val) {
872                         /* DO NOTHING */
873                 } else {
874                         summary_unlock(summaryview);
875                         inc_unlock();
876                         return FALSE;
877                 }
878                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
879                         folder_update_op_count();
880         }
881         
882         gtk_clist_freeze(GTK_CLIST(ctree));
883
884         summary_clear_list(summaryview);
885
886         buf = NULL;
887         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
888                 g_free(buf);
889                 debug_print("empty folder\n\n");
890                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
891                 summary_clear_all(summaryview);
892                 summaryview->folder_item = item;
893                 gtk_clist_thaw(GTK_CLIST(ctree));
894                 summary_unlock(summaryview);
895                 inc_unlock();
896                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
897                         main_window_cursor_wait(summaryview->mainwin);
898                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
899                         if (quicksearch_is_active(summaryview->quicksearch))
900                                 quicksearch_search_subfolders(summaryview->quicksearch, 
901                                               summaryview->folderview,
902                                               summaryview->folder_item);
903                         main_window_cursor_normal(summaryview->mainwin);
904                 }                       
905                 return TRUE;
906         }
907         g_free(buf);
908
909         if (!is_refresh)
910                 messageview_clear(summaryview->messageview);
911
912         summaryview->folder_item = item;
913         item->opened = TRUE;
914
915         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
916         debug_print("%s\n", buf);
917         STATUSBAR_PUSH(summaryview->mainwin, buf);
918         g_free(buf);
919
920         main_window_cursor_wait(summaryview->mainwin);
921
922         mlist = folder_item_get_msg_list(item);
923
924         if (summaryview->folder_item->hide_read_msgs &&
925             quicksearch_is_active(summaryview->quicksearch) == FALSE) {
926                 GSList *not_killed;
927                 
928                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
929                 not_killed = NULL;
930                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
931                         MsgInfo * msginfo = (MsgInfo *) cur->data;
932                         
933                         if (!msginfo->hidden) {
934                                 if (MSG_IS_UNREAD(msginfo->flags) &&
935                                     !MSG_IS_IGNORE_THREAD(msginfo->flags))
936                                         not_killed = g_slist_prepend(not_killed, msginfo);
937                                 else if (MSG_IS_MARKED(msginfo->flags) ||
938                                          MSG_IS_LOCKED(msginfo->flags))
939                                         not_killed = g_slist_prepend(not_killed, msginfo);
940                                 else if (is_refresh &&
941                                         (msginfo->msgnum == selected_msgnum ||
942                                          msginfo->msgnum == displayed_msgnum))
943                                         not_killed = g_slist_prepend(not_killed, msginfo);
944                                 else
945                                         procmsg_msginfo_free(msginfo);
946                          } else
947                                 procmsg_msginfo_free(msginfo);
948                 }
949                 hidden_removed = TRUE;
950                 g_slist_free(mlist);
951                 mlist = not_killed;
952         } else {
953                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
954         }
955
956         if (quicksearch_is_active(summaryview->quicksearch)) {
957                 GSList *not_killed;
958                 
959                 not_killed = NULL;
960                 for (cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
961                         MsgInfo * msginfo = (MsgInfo *) cur->data;
962
963                         if (!msginfo->hidden && quicksearch_match(summaryview->quicksearch, msginfo))
964                                 not_killed = g_slist_prepend(not_killed, msginfo);
965                         else
966                                 procmsg_msginfo_free(msginfo);
967                 }
968                 hidden_removed = TRUE;
969                 if (quicksearch_is_running(summaryview->quicksearch)) {
970                         /* only scan subfolders when quicksearch changed,
971                          * not when search is the same and folder changed */
972                         main_window_cursor_wait(summaryview->mainwin);
973                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
974                         quicksearch_search_subfolders(summaryview->quicksearch, 
975                                               summaryview->folderview,
976                                               summaryview->folder_item);
977                         main_window_cursor_normal(summaryview->mainwin);
978                 }
979                 
980                 g_slist_free(mlist);
981                 mlist = not_killed;
982         }
983
984         if (!hidden_removed) {
985                 not_killed = NULL;
986                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
987                         MsgInfo * msginfo = (MsgInfo *) cur->data;
988
989                         if (!msginfo->hidden)
990                                 not_killed = g_slist_prepend(not_killed, msginfo);
991                         else
992                                 procmsg_msginfo_free(msginfo);
993                 }
994                 g_slist_free(mlist);
995                 mlist = not_killed;
996         }
997
998         STATUSBAR_POP(summaryview->mainwin);
999
1000         /* set ctree and hash table from the msginfo list, and
1001            create the thread */
1002         summary_set_ctree_from_list(summaryview, mlist);
1003
1004         g_slist_free(mlist);
1005
1006         if (is_refresh) {
1007                 summaryview->displayed =
1008                         summary_find_msg_by_msgnum(summaryview,
1009                                                    displayed_msgnum);
1010                 if (!summaryview->displayed)
1011                         messageview_clear(summaryview->messageview);
1012                 summary_unlock(summaryview);
1013                 summary_select_by_msgnum(summaryview, selected_msgnum);
1014                 summary_lock(summaryview);
1015                 if (!summaryview->selected) {
1016                         /* no selected message - select first unread
1017                            message, but do not display it */
1018                         node = summary_find_next_flagged_msg(summaryview, NULL,
1019                                                              MSG_UNREAD, FALSE);
1020                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
1021                                 node = gtk_ctree_node_nth
1022                                         (ctree,
1023                                          item->sort_type == SORT_DESCENDING
1024                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
1025                         summary_unlock(summaryview);
1026                         summary_select_node(summaryview, node, FALSE, TRUE);
1027                         summary_lock(summaryview);
1028                 }
1029         } else {
1030                 switch (prefs_common.select_on_entry) {
1031                         case SELECTONENTRY_NEW:
1032                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1033                                                                      MSG_NEW, FALSE);
1034                                 if (node == NULL)
1035                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1036                                                                      MSG_UNREAD, FALSE);
1037                                 break;
1038                         case SELECTONENTRY_UNREAD:
1039                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1040                                                                      MSG_UNREAD, FALSE);
1041                                 if (node == NULL)
1042                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1043                                                                      MSG_NEW, FALSE);
1044                                 break;
1045                         default:
1046                                 node = NULL;
1047                 }
1048
1049                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1050                         node = gtk_ctree_node_nth
1051                                 (ctree,
1052                                  item->sort_type == SORT_DESCENDING
1053                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1054                 }
1055                 summary_unlock(summaryview);
1056                 summary_select_node(summaryview, node,
1057                                     prefs_common.always_show_msg,
1058                                     TRUE);
1059                 summary_lock(summaryview);
1060         }
1061
1062         summary_status_show(summaryview);
1063         summary_set_menu_sensitive(summaryview);
1064         toolbar_main_set_sensitive(summaryview->mainwin);
1065         
1066         gtk_clist_thaw(GTK_CLIST(ctree));
1067
1068         debug_print("\n");
1069         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1070         STATUSBAR_POP(summaryview->mainwin);
1071         main_window_cursor_normal(summaryview->mainwin);
1072         summary_unlock(summaryview);
1073         inc_unlock();
1074
1075         return TRUE;
1076 }
1077
1078 #undef CURRENTLY_DISPLAYED
1079
1080
1081 void summary_clear_list(SummaryView *summaryview)
1082 {
1083         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1084         gint optimal_width;
1085
1086         gtk_clist_freeze(clist);
1087
1088         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1089                                 NULL, summary_free_msginfo_func, NULL);
1090
1091         if (summaryview->folder_item) {
1092                 summaryview->folder_item->opened = FALSE;
1093                 summaryview->folder_item = NULL;
1094         }
1095
1096         summaryview->display_msg = FALSE;
1097
1098         summaryview->selected = NULL;
1099         summaryview->displayed = NULL;
1100         summaryview->total_size = 0;
1101         summaryview->deleted = summaryview->moved = 0;
1102         summaryview->copied = 0;
1103         if (summaryview->msgid_table) {
1104                 g_hash_table_destroy(summaryview->msgid_table);
1105                 summaryview->msgid_table = NULL;
1106         }
1107         if (summaryview->subject_table) {
1108                 g_hash_table_destroy(summaryview->subject_table);
1109                 summaryview->subject_table = NULL;
1110         }
1111         summaryview->mlist = NULL;
1112
1113         gtk_clist_clear(clist);
1114         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1115                 optimal_width = gtk_clist_optimal_column_width
1116                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1117                 gtk_clist_set_column_width
1118                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1119                          optimal_width);
1120         }
1121
1122         gtk_clist_thaw(clist);
1123 }
1124
1125 void summary_clear_all(SummaryView *summaryview)
1126 {
1127         messageview_clear(summaryview->messageview);
1128         summary_clear_list(summaryview);
1129         summary_set_menu_sensitive(summaryview);
1130         toolbar_main_set_sensitive(summaryview->mainwin);
1131         summary_status_show(summaryview);
1132 }
1133
1134 void summary_lock(SummaryView *summaryview)
1135 {
1136         summaryview->lock_count++;
1137 }
1138
1139 void summary_unlock(SummaryView *summaryview)
1140 {
1141         if (summaryview->lock_count)
1142                 summaryview->lock_count--;
1143 }
1144
1145 gboolean summary_is_locked(SummaryView *summaryview)
1146 {
1147         return summaryview->lock_count > 0;
1148 }
1149
1150 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1151 {
1152         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1153         SummarySelection selection;
1154
1155         if (!clist->row_list)
1156                 selection = SUMMARY_NONE;
1157         else if (!clist->selection)
1158                 selection = SUMMARY_SELECTED_NONE;
1159         else if (!clist->selection->next)
1160                 selection = SUMMARY_SELECTED_SINGLE;
1161         else
1162                 selection = SUMMARY_SELECTED_MULTIPLE;
1163
1164         return selection;
1165 }
1166
1167 /*!
1168  *\return       MsgInfo * Selected message if there's one selected;
1169  *              if multiple selected, or none, return NULL.
1170  */ 
1171 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1172 {
1173         /* summaryview->selected may be valid when multiple 
1174          * messages were selected */
1175         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1176
1177         if (sellist == NULL || sellist->next) 
1178                 return NULL;
1179         
1180         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1181 }
1182
1183 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1184 {
1185         GSList *mlist = NULL;
1186         GList *cur;
1187         MsgInfo *msginfo;
1188
1189         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1190              cur = cur->next) {
1191                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1192                 mlist = g_slist_prepend(mlist, msginfo);
1193         }
1194
1195         mlist = g_slist_reverse(mlist);
1196
1197         return mlist;
1198 }
1199
1200 GSList *summary_get_msg_list(SummaryView *summaryview)
1201 {
1202         GSList *mlist = NULL;
1203         GtkCTree *ctree;
1204         GtkCTreeNode *node;
1205         MsgInfo *msginfo;
1206
1207         ctree = GTK_CTREE(summaryview->ctree);
1208
1209         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1210              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1211                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1212                 mlist = g_slist_prepend(mlist, msginfo);
1213         }
1214
1215         mlist = g_slist_reverse(mlist);
1216
1217         return mlist;
1218 }
1219
1220 static void summary_set_menu_sensitive(SummaryView *summaryview)
1221 {
1222         GtkItemFactory *ifactory = summaryview->popupfactory;
1223         SensitiveCond state;
1224         gboolean sensitive;
1225         GtkWidget *menuitem;
1226         gint i;
1227
1228         static const struct {
1229                 gchar *const entry;
1230                 SensitiveCond cond;
1231         } entry[] = {
1232                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1233                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1234                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1235                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1236                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1237
1238                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1239                 {"/Forward as attachment"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1240                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1241
1242                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1243                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC},
1244                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1245                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE},
1246
1247                 {"/Mark"                        , M_TARGET_EXIST},
1248                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1249                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1250                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1251                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1252                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1253                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1254                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1255                 {"/Color label"                 , M_TARGET_EXIST},
1256
1257                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1258                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1259                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1260
1261                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1262                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1263                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1264                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1265                 {"/Save as..."                  , M_TARGET_EXIST},
1266                 {"/Print..."                    , M_TARGET_EXIST},
1267                 {NULL, 0}
1268         };
1269
1270         main_window_set_menu_sensitive(summaryview->mainwin);
1271
1272         state = main_window_get_current_state(summaryview->mainwin);
1273
1274         for (i = 0; entry[i].entry != NULL; i++) {
1275                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1276                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1277         }
1278
1279
1280         summary_lock(summaryview);
1281         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1282         if (summaryview->messageview 
1283         &&  summaryview->messageview->mimeview
1284         &&  summaryview->messageview->mimeview->textview)
1285                 gtk_check_menu_item_set_active
1286                         (GTK_CHECK_MENU_ITEM(menuitem),
1287                          summaryview->messageview->mimeview->textview->show_all_headers);
1288         summary_unlock(summaryview);
1289 }
1290
1291 void summary_select_prev_unread(SummaryView *summaryview)
1292 {
1293         GtkCTreeNode *node;
1294         gboolean skip_cur = FALSE;
1295
1296         if (summaryview->displayed 
1297         &&  summaryview->selected == summaryview->displayed) {
1298                 debug_print("skipping current\n");
1299                 skip_cur = TRUE;
1300         }
1301
1302         node = summary_find_prev_flagged_msg
1303                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1304
1305         if (!node || node == summaryview->selected) {
1306                 AlertValue val = 0;
1307
1308                 switch (prefs_common.next_unread_msg_dialog) {
1309                         case NEXTUNREADMSGDIALOG_ALWAYS:
1310                                 val = alertpanel(_("No more unread messages"),
1311                                                  _("No unread message found. "
1312                                                    "Search from the end?"),
1313                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1314                                 break;
1315                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1316                                 val = G_ALERTALTERNATE;
1317                                 break;
1318                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1319                                 val = !G_ALERTALTERNATE;
1320                                 break;
1321                         default:
1322                                 debug_print(
1323                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1324                 }
1325                 if (val != G_ALERTALTERNATE) return;
1326                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1327                                                      MSG_UNREAD, FALSE);
1328         }
1329
1330         if (!node)
1331                 alertpanel_notice(_("No unread messages."));
1332         else
1333                 summary_select_node(summaryview, node, TRUE, FALSE);
1334 }
1335
1336 void summary_select_next_unread(SummaryView *summaryview)
1337 {
1338         GtkCTreeNode *node = summaryview->selected;
1339         gboolean skip_cur = FALSE;
1340         
1341         if (summaryview->displayed 
1342         &&  summaryview->selected == summaryview->displayed) {
1343                 debug_print("skipping cur (%p %p)\n",
1344                         summaryview->displayed, summaryview->selected);
1345                 skip_cur = TRUE;
1346         }
1347
1348
1349         node = summary_find_next_flagged_msg
1350                 (summaryview, node, MSG_UNREAD, skip_cur);
1351         
1352         if (node)
1353                 summary_select_node(summaryview, node, TRUE, FALSE);
1354         else {
1355                 node = summary_find_next_flagged_msg
1356                         (summaryview, NULL, MSG_UNREAD, FALSE);
1357                 if (node == NULL || node == summaryview->selected) {
1358                         AlertValue val = 0;
1359
1360                         switch (prefs_common.next_unread_msg_dialog) {
1361                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1362                                         val = alertpanel(_("No more unread messages"),
1363                                                          _("No unread message found. "
1364                                                            "Go to next folder?"),
1365                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1366                                         break;
1367                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1368                                         val = G_ALERTALTERNATE;
1369                                         break;
1370                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1371                                         val = G_ALERTOTHER;
1372                                         break;
1373                                 default:
1374                                         debug_print(
1375                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1376                         }
1377
1378                         if (val == G_ALERTALTERNATE) {
1379                                 folderview_select_next_unread(summaryview->folderview);
1380                                 return;
1381                         } 
1382                         else
1383                                 return;
1384                 } else
1385                         summary_select_node(summaryview, node, TRUE, FALSE);
1386
1387         }
1388 }
1389
1390 void summary_select_prev_new(SummaryView *summaryview)
1391 {
1392         GtkCTreeNode *node;
1393         gboolean skip_cur = FALSE;
1394
1395         if (summaryview->displayed 
1396         &&  summaryview->selected == summaryview->displayed) {
1397                 debug_print("skipping current\n");
1398                 skip_cur = TRUE;
1399         }
1400
1401         node = summary_find_prev_flagged_msg
1402                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1403
1404         if (!node || node == summaryview->selected) {
1405                 AlertValue val = 0;
1406
1407                 switch (prefs_common.next_unread_msg_dialog) {
1408                         case NEXTUNREADMSGDIALOG_ALWAYS:
1409                                 val = alertpanel(_("No more new messages"),
1410                                                  _("No new message found. "
1411                                                    "Search from the end?"),
1412                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1413                                 break;
1414                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1415                                 val = G_ALERTALTERNATE;
1416                                 break;
1417                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1418                                 val = !G_ALERTALTERNATE;
1419                                 break;
1420                         default:
1421                                 debug_print(
1422                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1423                 }
1424                 if (val != G_ALERTALTERNATE) return;
1425                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1426                                                      MSG_NEW, FALSE);
1427         }
1428
1429         if (!node)
1430                 alertpanel_notice(_("No new messages."));
1431         else
1432                 summary_select_node(summaryview, node, TRUE, FALSE);
1433 }
1434
1435 void summary_select_next_new(SummaryView *summaryview)
1436 {
1437         GtkCTreeNode *node = summaryview->selected;
1438         gboolean skip_cur = FALSE;
1439         
1440         if (summaryview->displayed 
1441         &&  summaryview->selected == summaryview->displayed) {
1442                 debug_print("skipping cur (%p %p)\n",
1443                         summaryview->displayed, summaryview->selected);
1444                 skip_cur = TRUE;
1445         }
1446
1447
1448         node = summary_find_next_flagged_msg
1449                 (summaryview, node, MSG_NEW, skip_cur);
1450         
1451         if (node)
1452                 summary_select_node(summaryview, node, TRUE, FALSE);
1453         else {
1454                 node = summary_find_next_flagged_msg
1455                         (summaryview, NULL, MSG_NEW, FALSE);
1456                 if (node == NULL || node == summaryview->selected) {
1457                         AlertValue val = 0;
1458
1459                         switch (prefs_common.next_unread_msg_dialog) {
1460                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1461                                         val = alertpanel(_("No more new messages"),
1462                                                          _("No new message found. "
1463                                                            "Go to next folder?"),
1464                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1465                                         break;
1466                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1467                                         val = G_ALERTALTERNATE;
1468                                         break;
1469                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1470                                         val = G_ALERTOTHER;
1471                                         break;
1472                                 default:
1473                                         debug_print(
1474                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1475                         }
1476
1477                         if (val == G_ALERTALTERNATE) {
1478                                 folderview_select_next_new(summaryview->folderview);
1479                                 return;
1480                         } 
1481                         else
1482                                 return;
1483                 } else
1484                         summary_select_node(summaryview, node, TRUE, FALSE);
1485
1486         }
1487 }
1488
1489 void summary_select_prev_marked(SummaryView *summaryview)
1490 {
1491         GtkCTreeNode *node;
1492
1493         node = summary_find_prev_flagged_msg
1494                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1495
1496         if (!node) {
1497                 AlertValue val;
1498
1499                 val = alertpanel(_("No more marked messages"),
1500                                  _("No marked message found. "
1501                                    "Search from the end?"),
1502                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1503                 if (val != G_ALERTALTERNATE) return;
1504                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1505                                                      MSG_MARKED, TRUE);
1506         }
1507
1508         if (!node)
1509                 alertpanel_notice(_("No marked messages."));
1510         else
1511                 summary_select_node(summaryview, node, TRUE, FALSE);
1512 }
1513
1514 void summary_select_next_marked(SummaryView *summaryview)
1515 {
1516         GtkCTreeNode *node;
1517
1518         node = summary_find_next_flagged_msg
1519                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1520
1521         if (!node) {
1522                 AlertValue val;
1523
1524                 val = alertpanel(_("No more marked messages"),
1525                                  _("No marked message found. "
1526                                    "Search from the beginning?"),
1527                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1528                 if (val != G_ALERTALTERNATE) return;
1529                 node = summary_find_next_flagged_msg(summaryview, NULL,
1530                                                      MSG_MARKED, TRUE);
1531         }
1532
1533         if (!node)
1534                 alertpanel_notice(_("No marked messages."));
1535         else
1536                 summary_select_node(summaryview, node, TRUE, FALSE);
1537 }
1538
1539 void summary_select_prev_labeled(SummaryView *summaryview)
1540 {
1541         GtkCTreeNode *node;
1542
1543         node = summary_find_prev_flagged_msg
1544                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1545
1546         if (!node) {
1547                 AlertValue val;
1548
1549                 val = alertpanel(_("No more labeled messages"),
1550                                  _("No labeled message found. "
1551                                    "Search from the end?"),
1552                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1553                 if (val != G_ALERTALTERNATE) return;
1554                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1555                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1556         }
1557
1558         if (!node)
1559                 alertpanel_notice(_("No labeled messages."));
1560         else
1561                 summary_select_node(summaryview, node, TRUE, FALSE);
1562 }
1563
1564 void summary_select_next_labeled(SummaryView *summaryview)
1565 {
1566         GtkCTreeNode *node;
1567
1568         node = summary_find_next_flagged_msg
1569                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1570
1571         if (!node) {
1572                 AlertValue val;
1573
1574                 val = alertpanel(_("No more labeled messages"),
1575                                  _("No labeled message found. "
1576                                    "Search from the beginning?"),
1577                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1578                 if (val != G_ALERTALTERNATE) return;
1579                 node = summary_find_next_flagged_msg(summaryview, NULL,
1580                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1581         }
1582
1583         if (!node)
1584                 alertpanel_notice(_("No labeled messages."));
1585         else
1586                 summary_select_node(summaryview, node, TRUE, FALSE);
1587 }
1588
1589 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1590 {
1591         GtkCTreeNode *node;
1592
1593         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1594         summary_select_node(summaryview, node, FALSE, TRUE);
1595 }
1596
1597 /**
1598  * summary_select_node:
1599  * @summaryview: Summary view.
1600  * @node: Summary tree node.
1601  * @display_msg: TRUE to display the selected message.
1602  * @do_refresh: TRUE to refresh the widget.
1603  *
1604  * Select @node (bringing it into view by scrolling and expanding its
1605  * thread, if necessary) and unselect all others.  If @display_msg is
1606  * TRUE, display the corresponding message in the message view.
1607  * If @do_refresh is TRUE, the widget is refreshed.
1608  **/
1609 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1610                          gboolean display_msg, gboolean do_refresh)
1611 {
1612         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1613
1614         if (!summaryview->folder_item)
1615                 return;
1616         if (node) {
1617                 gtkut_ctree_expand_parent_all(ctree, node);
1618                 if (do_refresh) {
1619                         GTK_EVENTS_FLUSH();
1620                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1621                         if (GTK_CTREE_ROW(node) == NULL) {
1622                                 g_warning("crash avoidance hack 1\n");
1623                                 return;
1624                         }
1625                         if (((GtkCListRow *)(GTK_CTREE_ROW(node)))->state < GTK_STATE_NORMAL
1626                         ||  ((GtkCListRow *)(GTK_CTREE_ROW(node)))->state > GTK_STATE_INSENSITIVE) {
1627                                 g_warning("crash avoidance hack 2\n");
1628                                 return;
1629                         }
1630                         gtk_ctree_node_moveto(ctree, node, 0, 0.5, 0);
1631                 }
1632                 summary_unselect_all(summaryview);
1633                 if (display_msg && summaryview->displayed == node)
1634                         summaryview->displayed = NULL;
1635                 summaryview->display_msg = display_msg;
1636                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1637                 if (summaryview->selected == NULL)
1638                         summaryview->selected = node;
1639         }
1640 }
1641
1642 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1643 {
1644         GtkCTree *ctree =NULL;
1645         MsgInfo *msginfo;
1646
1647         if (!summaryview)
1648                 return 0;
1649         ctree = GTK_CTREE(summaryview->ctree);
1650         if (!node)
1651                 return 0;
1652         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1653         return msginfo->msgnum;
1654 }
1655
1656 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1657                                            GtkCTreeNode *current_node)
1658 {
1659         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1660         GtkCTreeNode *node;
1661         MsgInfo *msginfo;
1662
1663         if (current_node)
1664                 node = current_node;
1665         else
1666                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1667
1668         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1669                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1670                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1671         }
1672
1673         return node;
1674 }
1675
1676 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1677                                            GtkCTreeNode *current_node)
1678 {
1679         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1680         GtkCTreeNode *node;
1681         MsgInfo *msginfo;
1682
1683         if (current_node)
1684                 node = current_node;
1685         else
1686                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1687
1688         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1689                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1690                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1691         }
1692
1693         return node;
1694 }
1695
1696 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1697                                                    GtkCTreeNode *current_node,
1698                                                    MsgPermFlags flags,
1699                                                    gboolean start_from_prev)
1700 {
1701         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1702         GtkCTreeNode *node;
1703         MsgInfo *msginfo;
1704
1705         if (current_node) {
1706                 if (start_from_prev)
1707                         node = GTK_CTREE_NODE_PREV(current_node);
1708                 else
1709                         node = current_node;
1710         } else
1711                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1712
1713         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1714                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1715                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1716         }
1717
1718         return node;
1719 }
1720
1721 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1722                                                    GtkCTreeNode *current_node,
1723                                                    MsgPermFlags flags,
1724                                                    gboolean start_from_next)
1725 {
1726         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1727         GtkCTreeNode *node;
1728         MsgInfo *msginfo;
1729
1730         if (current_node) {
1731                 if (start_from_next)
1732                         node = gtkut_ctree_node_next(ctree, current_node);
1733                 else
1734                         node = current_node;
1735         } else
1736                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1737
1738         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1739                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1740                 /* Find msg with matching flags but ignore messages with
1741                    ignore flags, if searching for new or unread messages */
1742                 if ((msginfo && (msginfo->flags.perm_flags & flags) != 0) &&
1743                     !(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) 
1744                         )
1745                         break;
1746         }
1747
1748         return node;
1749 }
1750
1751 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1752                                                 guint msgnum)
1753 {
1754         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1755         GtkCTreeNode *node;
1756         MsgInfo *msginfo;
1757
1758         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1759
1760         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1761                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1762                 if (msginfo && msginfo->msgnum == msgnum) break;
1763         }
1764
1765         return node;
1766 }
1767
1768 static guint attract_hash_func(gconstpointer key)
1769 {
1770         gchar *str;
1771         gchar *p;
1772         guint h;
1773
1774         Xstrdup_a(str, (const gchar *)key, return 0);
1775         trim_subject(str);
1776
1777         p = str;
1778         h = *p;
1779
1780         if (h) {
1781                 for (p += 1; *p != '\0'; p++)
1782                         h = (h << 5) - h + *p;
1783         }
1784
1785         return h;
1786 }
1787
1788 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1789 {
1790         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1791 }
1792
1793 void summary_attract_by_subject(SummaryView *summaryview)
1794 {
1795         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1796         GtkCList *clist = GTK_CLIST(ctree);
1797         GtkCTreeNode *src_node;
1798         GtkCTreeNode *dst_node, *sibling;
1799         GtkCTreeNode *tmp;
1800         MsgInfo *src_msginfo, *dst_msginfo;
1801         GHashTable *subject_table;
1802
1803         debug_print("Attracting messages by subject...");
1804         STATUSBAR_PUSH(summaryview->mainwin,
1805                        _("Attracting messages by subject..."));
1806
1807         main_window_cursor_wait(summaryview->mainwin);
1808         gtk_clist_freeze(clist);
1809
1810         subject_table = g_hash_table_new(attract_hash_func,
1811                                          attract_compare_func);
1812
1813         for (src_node = GTK_CTREE_NODE(clist->row_list);
1814              src_node != NULL;
1815              src_node = tmp) {
1816                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1817                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1818                 if (!src_msginfo) continue;
1819                 if (!src_msginfo->subject) continue;
1820
1821                 /* find attracting node */
1822                 dst_node = g_hash_table_lookup(subject_table,
1823                                                src_msginfo->subject);
1824
1825                 if (dst_node) {
1826                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1827
1828                         /* if the time difference is more than 20 days,
1829                            don't attract */
1830                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1831                             > 60 * 60 * 24 * 20)
1832                                 continue;
1833
1834                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1835                         if (src_node != sibling)
1836                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1837                 }
1838
1839                 g_hash_table_insert(subject_table,
1840                                     src_msginfo->subject, src_node);
1841         }
1842
1843         g_hash_table_destroy(subject_table);
1844
1845         gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
1846
1847         gtk_clist_thaw(clist);
1848
1849         debug_print("done.\n");
1850         STATUSBAR_POP(summaryview->mainwin);
1851
1852         main_window_cursor_normal(summaryview->mainwin);
1853 }
1854
1855 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1856                                       gpointer data)
1857 {
1858         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1859
1860         if (msginfo)
1861                 procmsg_msginfo_free(msginfo);
1862 }
1863
1864 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1865                                    gpointer data)
1866 {
1867         SummaryView *summaryview = data;
1868         MsgInfo *msginfo;
1869
1870         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1871
1872         if (msginfo->folder && msginfo->folder->folder &&
1873             msginfo->folder->folder->klass->type == F_NEWS)
1874                 news_flag_crosspost(msginfo);
1875
1876         if (MSG_IS_DELETED(msginfo->flags))
1877                 summaryview->deleted++;
1878
1879         summaryview->total_size += msginfo->size;
1880
1881         summary_set_row_marks(summaryview, node);
1882 }
1883
1884 static void summary_update_status(SummaryView *summaryview)
1885 {
1886         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1887         GtkCTreeNode *node;
1888         MsgInfo *msginfo;
1889
1890         summaryview->total_size =
1891         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1892
1893         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1894              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1895                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1896
1897                 if (MSG_IS_DELETED(msginfo->flags))
1898                         summaryview->deleted++;
1899                 if (MSG_IS_MOVE(msginfo->flags))
1900                         summaryview->moved++;
1901                 if (MSG_IS_COPY(msginfo->flags))
1902                         summaryview->copied++;
1903                 summaryview->total_size += msginfo->size;
1904         }
1905 }
1906
1907 static void summary_status_show(SummaryView *summaryview)
1908 {
1909         gchar *str;
1910         gchar *del, *mv, *cp;
1911         gchar *sel;
1912         gchar *spc;
1913         gchar *itstr;
1914         GList *rowlist, *cur;
1915         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
1916         off_t sel_size = 0, n_size = 0;
1917         MsgInfo *msginfo;
1918         gchar *name;
1919         
1920         if (!summaryview->folder_item) {
1921                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1922                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1923                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1924                 return;
1925         }
1926
1927         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1928         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1929                 msginfo = gtk_ctree_node_get_row_data
1930                         (GTK_CTREE(summaryview->ctree),
1931                          GTK_CTREE_NODE(cur->data));
1932                 if (!msginfo)
1933                         g_warning("summary_status_show(): msginfo == NULL\n");
1934                 else {
1935                         sel_size += msginfo->size;
1936                         n_selected++;
1937                         
1938                 }
1939         }
1940         
1941         if (summaryview->folder_item->hide_read_msgs 
1942         || quicksearch_is_active(summaryview->quicksearch)) {
1943                 rowlist = GTK_CLIST(summaryview->ctree)->row_list;
1944                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1945                         msginfo = gtk_ctree_node_get_row_data
1946                                 (GTK_CTREE(summaryview->ctree),
1947                                  GTK_CTREE_NODE(cur));
1948                         if (!msginfo)
1949                                 g_warning("summary_status_show(): msginfo == NULL\n");
1950                         else {
1951                                 n_size += msginfo->size;
1952                                 n_total++;
1953                                 if (MSG_IS_NEW(msginfo->flags))
1954                                         n_new++;
1955                                 if (MSG_IS_UNREAD(msginfo->flags))
1956                                         n_unread++;
1957                         }
1958                 }
1959         } else {
1960                 n_new = summaryview->folder_item->new_msgs;
1961                 n_unread = summaryview->folder_item->unread_msgs;
1962                 n_total = summaryview->folder_item->total_msgs;
1963                 n_size = summaryview->total_size;
1964         }
1965
1966         name = folder_item_get_name(summaryview->folder_item);
1967         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1968         g_free(name);
1969
1970         if (summaryview->deleted)
1971                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1972         else
1973                 del = g_strdup("");
1974         if (summaryview->moved)
1975                 mv = g_strdup_printf(_("%s%d moved"),
1976                                      summaryview->deleted ? _(", ") : "",
1977                                      summaryview->moved);
1978         else
1979                 mv = g_strdup("");
1980         if (summaryview->copied)
1981                 cp = g_strdup_printf(_("%s%d copied"),
1982                                      summaryview->deleted ||
1983                                      summaryview->moved ? _(", ") : "",
1984                                      summaryview->copied);
1985         else
1986                 cp = g_strdup("");
1987
1988         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1989                 spc = "    ";
1990         else
1991                 spc = "";
1992
1993         if (n_selected) {
1994                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1995                 if (n_selected == 1)
1996                         itstr = g_strdup(_(" item selected"));
1997                 else
1998                         itstr = g_strdup(_(" items selected"));
1999         } else {
2000                 sel = g_strdup("");
2001                 itstr = g_strdup("");
2002         }
2003                 
2004         str = g_strconcat(n_selected ? itos(n_selected) : "",
2005                                         itstr, sel, spc, del, mv, cp, NULL);
2006         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2007         g_free(str);
2008         g_free(sel);
2009         g_free(del);
2010         g_free(mv);
2011         g_free(cp);
2012         g_free(itstr);
2013
2014         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2015
2016                                       n_new, n_unread, n_total,
2017                                       to_human_readable(n_size));
2018         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2019         g_free(str);
2020 }
2021
2022 static void summary_set_column_titles(SummaryView *summaryview)
2023 {
2024         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2025         GtkWidget *hbox;
2026         GtkWidget *label;
2027         GtkWidget *arrow;
2028         gint pos;
2029         const gchar *title;
2030         SummaryColumnType type;
2031         GtkJustification justify;
2032
2033         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2034                 SORT_BY_MARK,
2035                 SORT_BY_STATUS,
2036                 SORT_BY_MIME,
2037                 SORT_BY_SUBJECT,
2038                 SORT_BY_FROM,
2039                 SORT_BY_TO,
2040                 SORT_BY_DATE,
2041                 SORT_BY_SIZE,
2042                 SORT_BY_NUMBER,
2043                 SORT_BY_SCORE,
2044                 SORT_BY_LOCKED
2045         };
2046
2047         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2048                 type = summaryview->col_state[pos].type;
2049
2050                 /* CLAWS: mime and unread are single char headers */
2051                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2052                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2053
2054                 switch (type) {
2055                 case S_COL_SUBJECT:
2056                 case S_COL_FROM:
2057                 case S_COL_TO:
2058                 case S_COL_DATE:
2059                 case S_COL_NUMBER:
2060                         if (prefs_common.trans_hdr)
2061                                 title = gettext(col_label[type]);
2062                         else
2063                                 title = col_label[type];
2064                         break;
2065                 default:
2066                         title = gettext(col_label[type]);
2067                 }
2068
2069                 if (type == S_COL_MIME) {
2070                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2071                         gtk_widget_show(label);
2072                         gtk_clist_set_column_widget(clist, pos, label);
2073                         continue;
2074                 } else if (type == S_COL_MARK) {
2075                         label = gtk_image_new_from_pixmap(markxpm, markxpmmask);
2076                         gtk_widget_show(label);
2077                         gtk_clist_set_column_widget(clist, pos, label);
2078                         continue;
2079                 } else if (type == S_COL_LOCKED) {
2080                         label = gtk_image_new_from_pixmap(lockedxpm, lockedxpmmask);
2081                         gtk_widget_show(label);
2082                         gtk_clist_set_column_widget(clist, pos, label);
2083                         continue;
2084                 } else if (type == S_COL_STATUS) {
2085                         gtk_clist_set_column_title(clist, pos, title);
2086                         continue;
2087                 }
2088
2089                 hbox  = gtk_hbox_new(FALSE, 4);
2090                 label = gtk_label_new(title);
2091
2092                 if (justify == GTK_JUSTIFY_RIGHT)
2093                         gtk_box_pack_end(GTK_BOX(hbox), label,
2094                                          FALSE, FALSE, 0);
2095                 else
2096                         gtk_box_pack_start(GTK_BOX(hbox), label,
2097                                            FALSE, FALSE, 0);
2098
2099                 if (summaryview->sort_key == sort_by[type]) {
2100                         arrow = gtk_arrow_new
2101                                 (summaryview->sort_type == SORT_ASCENDING
2102                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2103                                  GTK_SHADOW_IN);
2104                         if (justify == GTK_JUSTIFY_RIGHT)
2105                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2106                                                    FALSE, FALSE, 0);
2107                         else
2108                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2109                                                  FALSE, FALSE, 0);
2110                 }
2111
2112                 gtk_widget_show_all(hbox);
2113                 gtk_clist_set_column_widget(clist, pos, hbox);
2114         }
2115 }
2116
2117 void summary_reflect_prefs(void)
2118 {
2119         static gchar *last_font = NULL;
2120         gboolean update_font = TRUE;
2121         SummaryView *summaryview = NULL;
2122
2123         if (!mainwindow_get_mainwindow())
2124                 return;
2125         summaryview = mainwindow_get_mainwindow()->summaryview;
2126
2127         if (last_font && !strcmp(last_font, NORMAL_FONT))
2128                 update_font = FALSE;
2129
2130         if (last_font)
2131                 g_free(last_font);
2132         
2133         last_font = g_strdup(NORMAL_FONT);
2134
2135         if (update_font) {      
2136                 bold_style = bold_marked_style = bold_deleted_style = 
2137                         small_style = small_marked_style = small_deleted_style = NULL;
2138                 summary_set_fonts(summaryview);
2139         }
2140
2141         summary_set_column_titles(summaryview);
2142         summary_show(summaryview, summaryview->folder_item);
2143 }
2144
2145 void summary_sort(SummaryView *summaryview,
2146                   FolderSortKey sort_key, FolderSortType sort_type)
2147 {
2148         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2149         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2150         GtkCListCompareFunc cmp_func = NULL;
2151         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2152                                        G_CALLBACK(summary_tree_expanded), summaryview);
2153         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2154
2155         switch (sort_key) {
2156         case SORT_BY_MARK:
2157                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2158                 break;
2159         case SORT_BY_STATUS:
2160                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2161                 break;
2162         case SORT_BY_MIME:
2163                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2164                 break;
2165         case SORT_BY_NUMBER:
2166                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2167                 break;
2168         case SORT_BY_SIZE:
2169                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2170                 break;
2171         case SORT_BY_DATE:
2172                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2173                 break;
2174         case SORT_BY_FROM:
2175                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2176                 break;
2177         case SORT_BY_SUBJECT:
2178                 if (summaryview->simplify_subject_preg)
2179                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2180                 else
2181                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2182                 break;
2183         case SORT_BY_SCORE:
2184                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2185                 break;
2186         case SORT_BY_LABEL:
2187                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2188                 break;
2189         case SORT_BY_TO:
2190                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2191                 break;
2192         case SORT_BY_LOCKED:
2193                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2194                 break;
2195         case SORT_BY_NONE:
2196                 break;
2197         default:
2198                 goto unlock;
2199         }
2200
2201         summaryview->sort_key = sort_key;
2202         summaryview->sort_type = sort_type;
2203
2204         summary_set_column_titles(summaryview);
2205         summary_set_menu_sensitive(summaryview);
2206
2207         /* allow fallback to don't sort */
2208         if (summaryview->sort_key == SORT_BY_NONE)
2209                 goto unlock;
2210
2211         if (cmp_func != NULL) {
2212                 debug_print("Sorting summary...");
2213                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2214
2215                 main_window_cursor_wait(summaryview->mainwin);
2216
2217                 gtk_clist_freeze(clist);
2218                 gtk_clist_set_compare_func(clist, cmp_func);
2219
2220                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2221
2222                 gtk_sctree_sort_recursive(ctree, NULL);
2223
2224                 gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2225
2226                 main_window_cursor_normal(summaryview->mainwin);
2227                 gtk_clist_thaw(clist);
2228
2229                 debug_print("done.\n");
2230                 STATUSBAR_POP(summaryview->mainwin);
2231         }
2232 unlock:
2233         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2234         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2235                                        G_CALLBACK(summary_tree_expanded), summaryview);
2236 }
2237
2238 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2239                                    GtkCTreeNode *cnode, gpointer data)
2240 {
2241         SummaryView *summaryview = (SummaryView *)data;
2242         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2243         gchar *text[N_SUMMARY_COLS];
2244         gint *col_pos = summaryview->col_pos;
2245         const gchar *msgid = msginfo->msgid;
2246         GHashTable *msgid_table = summaryview->msgid_table;
2247         gboolean free_from_to = FALSE;
2248         
2249         summary_set_header(summaryview, text, msginfo, &free_from_to);
2250
2251         gtk_sctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2252                                 NULL, NULL, NULL, NULL, FALSE,
2253                                 gnode->parent->parent ? TRUE : FALSE);
2254 #define SET_TEXT(col) \
2255         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2256                                 text[col_pos[col]])
2257
2258         SET_TEXT(S_COL_NUMBER);
2259         SET_TEXT(S_COL_SCORE);
2260         SET_TEXT(S_COL_SIZE);
2261         SET_TEXT(S_COL_DATE);
2262         SET_TEXT(S_COL_FROM);
2263         SET_TEXT(S_COL_TO);
2264         /* SET_TEXT(S_COL_SUBJECT);  already set by node info */
2265
2266         if (free_from_to) {
2267                 g_free(text[col_pos[S_COL_FROM]]);
2268                 g_free(text[col_pos[S_COL_TO]]);
2269                 text[col_pos[S_COL_FROM]] = NULL;
2270                 text[col_pos[S_COL_TO]] = NULL;
2271         }
2272
2273 #undef SET_TEXT
2274
2275         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2276         summary_set_marks_func(ctree, cnode, summaryview);
2277
2278         if (msgid && msgid[0] != '\0')
2279                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2280
2281         return TRUE;
2282 }
2283
2284 static void summary_set_ctree_from_list(SummaryView *summaryview,
2285                                         GSList *mlist)
2286 {
2287         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2288         MsgInfo *msginfo;
2289         GtkCTreeNode *node = NULL;
2290         GHashTable *msgid_table;
2291         GHashTable *subject_table;
2292         GSList * cur;
2293         START_TIMING("summary_set_ctree_from_list");
2294         
2295         if (!mlist) return;
2296
2297         debug_print("\tSetting summary from message data...");
2298         STATUSBAR_PUSH(summaryview->mainwin,
2299                        _("Setting summary from message data..."));
2300         gdk_flush();
2301
2302         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2303                                        G_CALLBACK(summary_tree_expanded), summaryview);
2304
2305         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2306         summaryview->msgid_table = msgid_table;
2307         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2308         summaryview->subject_table = subject_table;
2309
2310         if (prefs_common.use_addr_book)
2311                 start_address_completion();
2312         
2313         if (summaryview->threaded) {
2314                 GNode *root, *gnode;
2315
2316                 root = procmsg_get_thread_tree(mlist);
2317
2318                 for (gnode = root->children; gnode != NULL;
2319                      gnode = gnode->next) {
2320                         node = gtk_ctree_insert_gnode
2321                                 (ctree, NULL, node, gnode,
2322                                  summary_insert_gnode_func, summaryview);
2323                 }
2324
2325                 g_node_destroy(root);
2326                 
2327                 summary_thread_init(summaryview);
2328         } else {
2329                 gchar *text[N_SUMMARY_COLS];
2330                 gboolean free_from_to = FALSE;
2331                 gint *col_pos = summaryview->col_pos;
2332
2333                 cur = mlist;
2334                 for (; mlist != NULL; mlist = mlist->next) {
2335                         msginfo = (MsgInfo *)mlist->data;
2336
2337                         summary_set_header(summaryview, text, msginfo, &free_from_to);
2338
2339                         node = gtk_sctree_insert_node
2340                                 (ctree, NULL, node, text, 2,
2341                                  NULL, NULL, NULL, NULL,
2342                                  FALSE, FALSE);
2343                         if (free_from_to) {
2344                                 g_free(text[col_pos[S_COL_FROM]]);
2345                                 g_free(text[col_pos[S_COL_TO]]);
2346                                 text[col_pos[S_COL_FROM]] = NULL;
2347                                 text[col_pos[S_COL_TO]] = NULL;
2348                         }
2349                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2350                         summary_set_marks_func(ctree, node, summaryview);
2351
2352                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2353                                 g_hash_table_insert(msgid_table,
2354                                                     msginfo->msgid, node);
2355
2356                         subject_table_insert(subject_table,
2357                                              msginfo->subject,
2358                                              node);
2359                 }
2360                 mlist = cur;
2361         }
2362
2363         if (prefs_common.enable_hscrollbar &&
2364             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2365                 gint optimal_width;
2366
2367                 optimal_width = gtk_clist_optimal_column_width
2368                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2369                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2370                                            summaryview->col_pos[S_COL_SUBJECT],
2371                                            optimal_width);
2372         }
2373
2374         if (prefs_common.use_addr_book)
2375                 end_address_completion();
2376
2377         debug_print("done.\n");
2378         STATUSBAR_POP(summaryview->mainwin);
2379         if (debug_get_mode()) {
2380                 debug_print("\tmsgid hash table size = %d\n",
2381                             g_hash_table_size(msgid_table));
2382                 debug_print("\tsubject hash table size = %d\n",
2383                             g_hash_table_size(subject_table));
2384         }
2385
2386         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2387
2388         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2389
2390         if (prefs_common.bold_unread) {
2391                 while (node) {
2392                         GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2393                         if (GTK_CTREE_ROW(node)->children)
2394                                 summary_set_row_marks(summaryview, node);
2395                         node = next;
2396                 }
2397         }
2398
2399         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2400                                        G_CALLBACK(summary_tree_expanded), summaryview);
2401         END_TIMING();
2402 }
2403
2404 static gchar *summary_complete_address(const gchar *addr)
2405 {
2406         gint count;
2407         gchar *res, *tmp, *email_addr;
2408         
2409         if (addr == NULL)
2410                 return NULL;
2411
2412         Xstrdup_a(email_addr, addr, return NULL);
2413         extract_address(email_addr);
2414         g_return_val_if_fail(*email_addr, NULL);
2415
2416         /*
2417          * completion stuff must be already initialized
2418          */
2419         res = NULL;
2420         if (1 < (count = complete_address(email_addr))) {
2421                 tmp = get_complete_address(1);
2422                 res = procheader_get_fromname(tmp);
2423                 g_free(tmp);
2424         }
2425
2426         return res;
2427 }
2428
2429 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2430                                MsgInfo *msginfo, gboolean *free_from_to)
2431 {
2432         static gchar date_modified[80];
2433         static gchar col_score[11];
2434         static gchar buf[BUFFSIZE];
2435         gint *col_pos = summaryview->col_pos;
2436         gchar *from_text = NULL, *to_text = NULL;
2437         gboolean should_swap = FALSE;
2438
2439         text[col_pos[S_COL_FROM]]   = "";
2440         text[col_pos[S_COL_TO]]     = "";
2441         text[col_pos[S_COL_SUBJECT]]= "";
2442         text[col_pos[S_COL_MARK]]   = "";
2443         text[col_pos[S_COL_STATUS]] = "";
2444         text[col_pos[S_COL_MIME]]   = "";
2445         text[col_pos[S_COL_LOCKED]] = "";
2446         text[col_pos[S_COL_DATE]]   = "";
2447         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2448         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2449         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2450
2451         if (msginfo->date_t) {
2452                 procheader_date_get_localtime(date_modified,
2453                                               sizeof(date_modified),
2454                                               msginfo->date_t);
2455                 text[col_pos[S_COL_DATE]] = date_modified;
2456         } else if (msginfo->date)
2457                 text[col_pos[S_COL_DATE]] = msginfo->date;
2458         else
2459                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2460
2461         if (prefs_common.swap_from && msginfo->from && msginfo->to) {
2462                 gchar *addr = NULL;
2463                 
2464                 addr = g_strdup(msginfo->from);
2465
2466                 if (addr) {
2467                         extract_address(addr);
2468                         if (account_find_from_address(addr)) {
2469                                 should_swap = TRUE;
2470                         }
2471                         g_free(addr);
2472                 }
2473         }
2474
2475         if (!prefs_common.use_addr_book) {
2476                 from_text = msginfo->fromname ? 
2477                                 msginfo->fromname :
2478                                 _("(No From)");
2479         } else {
2480                 gchar *tmp = summary_complete_address(msginfo->from);
2481                 from_text = tmp ? tmp : (msginfo->fromname ?
2482                                          msginfo->fromname: 
2483                                                 _("(No From)"));
2484         }
2485         
2486         to_text = msginfo->to ? msginfo->to : 
2487                    (msginfo->cc ? msginfo->cc :
2488                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2489                      )
2490                    );
2491
2492         if (!should_swap) {
2493                 text[col_pos[S_COL_FROM]] = from_text;
2494                 text[col_pos[S_COL_TO]] = to_text;
2495                 *free_from_to = FALSE;
2496         } else {
2497                 gchar *tmp = NULL;
2498                 tmp = g_strconcat("-->", to_text, NULL);
2499                 text[col_pos[S_COL_FROM]] = tmp;
2500                 tmp = g_strconcat("<--", from_text, NULL);
2501                 text[col_pos[S_COL_TO]] = tmp;
2502                 *free_from_to = TRUE;
2503         }
2504         
2505         if (summaryview->simplify_subject_preg != NULL)
2506                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2507                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2508                                         summaryview->simplify_subject_preg) : 
2509                         _("(No Subject)");
2510         else 
2511                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2512                         _("(No Subject)");
2513 }
2514
2515 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2516 {
2517         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2518 }
2519
2520 static gboolean defer_change(gpointer data);
2521 typedef struct _ChangeData {
2522         MsgInfo *info;
2523         gint op; /* 0, 1, 2 for unset, set, change */
2524         MsgPermFlags set_flags;
2525         MsgTmpFlags  set_tmp_flags;
2526         MsgPermFlags unset_flags;
2527         MsgTmpFlags  unset_tmp_flags;
2528 } ChangeData;
2529
2530 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2531 {
2532         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2533                 debug_print("flags: doing unset now\n");
2534                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
2535         } else {
2536                 ChangeData *unset_data = g_new0(ChangeData, 1);
2537                 unset_data->info = msginfo;
2538                 unset_data->op = 0;
2539                 unset_data->unset_flags = flags;
2540                 unset_data->unset_tmp_flags = tmp_flags;
2541                 debug_print("flags: deferring unset\n");
2542                 g_timeout_add(100, defer_change, unset_data);
2543         }
2544 }
2545
2546 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2547 {
2548         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2549                 debug_print("flags: doing set now\n");
2550                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
2551         } else {
2552                 ChangeData *set_data = g_new0(ChangeData, 1);
2553                 set_data->info = msginfo;
2554                 set_data->op = 1;
2555                 set_data->set_flags = flags;
2556                 set_data->set_tmp_flags = tmp_flags;
2557                 debug_print("flags: deferring set\n");
2558                 g_timeout_add(100, defer_change, set_data);
2559         }
2560 }
2561
2562 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
2563                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
2564                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
2565 {
2566         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2567                 debug_print("flags: doing change now\n");
2568                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
2569                         rem_flags, rem_tmp_flags);
2570         } else {
2571                 ChangeData *change_data = g_new0(ChangeData, 1);
2572                 change_data->info = msginfo;
2573                 change_data->op = 2;
2574                 change_data->set_flags = add_flags;
2575                 change_data->set_tmp_flags = add_tmp_flags;
2576                 change_data->unset_flags = rem_flags;
2577                 change_data->unset_tmp_flags = rem_tmp_flags;
2578                 debug_print("flags: deferring change\n");
2579                 g_timeout_add(100, defer_change, change_data);
2580         }
2581 }
2582
2583 gboolean defer_change(gpointer data)
2584 {
2585         ChangeData *chg = (ChangeData *)data;
2586         if (chg->info->folder && chg->info->folder->processing_pending) {
2587                 debug_print("flags: trying later\n");
2588                 return TRUE; /* try again */
2589         } else {
2590                 debug_print("flags: finally doing it\n");
2591                 switch(chg->op) {
2592                 case 0:
2593                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
2594                         break;
2595                 case 1:
2596                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
2597                         break;
2598                 case 2:
2599                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
2600                                 chg->unset_flags, chg->unset_tmp_flags);
2601                         break;
2602                 default:
2603                         g_warning("shouldn't happen\n");
2604                 }
2605                 g_free(chg);
2606         }
2607         return FALSE;
2608 }
2609
2610 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2611                                       GtkCTreeNode *row)
2612 {
2613         g_return_if_fail(summaryview != NULL);
2614         g_return_if_fail(msginfo != NULL);
2615         g_return_if_fail(row != NULL);
2616
2617         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2618                 summary_msginfo_unset_flags
2619                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2620                 summary_set_row_marks(summaryview, row);
2621                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2622                 summary_status_show(summaryview);
2623         }
2624 }
2625
2626 typedef struct  {
2627         MsgInfo *msginfo;
2628         SummaryView *summaryview;
2629         GtkCTreeNode *row;
2630 } MarkAsReadData;
2631
2632 static int msginfo_mark_as_read_timeout(void *data)
2633 {
2634         MarkAsReadData *mdata = (MarkAsReadData *)data;
2635         if (!mdata)
2636                 return FALSE;
2637         
2638         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2639                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2640                                      mdata->row); 
2641
2642         g_free(mdata);
2643
2644         return FALSE;   
2645 }
2646
2647 static void summary_display_msg_full(SummaryView *summaryview,
2648                                      GtkCTreeNode *row,
2649                                      gboolean new_window, gboolean all_headers)
2650 {
2651         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2652         MsgInfo *msginfo;
2653         gint val;
2654
2655         if (!new_window) {
2656                 if (summaryview->displayed == row)
2657                         return;
2658                 else
2659                         summaryview->messageview->filtered = FALSE;
2660         }                       
2661         
2662         g_return_if_fail(row != NULL);
2663
2664         if (summary_is_locked(summaryview)) return;
2665         summary_lock(summaryview);
2666
2667         STATUSBAR_POP(summaryview->mainwin);
2668         GTK_EVENTS_FLUSH();
2669
2670         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2671
2672         if (new_window) {
2673                 MessageView *msgview;
2674
2675                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2676                 val = messageview_show(msgview, msginfo, all_headers);
2677         } else {
2678                 MessageView *msgview;
2679
2680                 msgview = summaryview->messageview;
2681
2682                 summaryview->displayed = row;
2683                 if (!messageview_is_visible(msgview)) {
2684                         main_window_toggle_message_view(summaryview->mainwin);
2685                         GTK_EVENTS_FLUSH();
2686                 }
2687                 val = messageview_show(msgview, msginfo, all_headers);
2688                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2689                         gtk_widget_grab_focus(summaryview->ctree);
2690                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2691         }
2692
2693         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2694                 if (prefs_common.mark_as_read_delay) {
2695                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2696                         data->summaryview = summaryview;
2697                         data->msginfo = msginfo;
2698                         data->row = row;
2699                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2700                                 msginfo_mark_as_read_timeout, data);
2701                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2702                         msginfo_mark_as_read(summaryview, msginfo, row);
2703                 }
2704         }
2705
2706         summary_set_menu_sensitive(summaryview);
2707         toolbar_main_set_sensitive(summaryview->mainwin);
2708         messageview_set_menu_sensitive(summaryview->messageview);
2709
2710         summary_unlock(summaryview);
2711 }
2712
2713 void summary_display_msg_selected(SummaryView *summaryview,
2714                                   gboolean all_headers)
2715 {
2716         if (summary_is_locked(summaryview)) return;
2717         summaryview->displayed = NULL;
2718         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2719                                  all_headers);
2720 }
2721
2722 void summary_redisplay_msg(SummaryView *summaryview)
2723 {
2724         GtkCTreeNode *node;
2725
2726         if (summaryview->displayed) {
2727                 node = summaryview->displayed;
2728                 summaryview->displayed = NULL;
2729                 summary_display_msg(summaryview, node);
2730         }
2731 }
2732
2733 void summary_open_msg(SummaryView *summaryview)
2734 {
2735         if (!summaryview->selected) return;
2736         
2737         /* CLAWS: if separate message view, don't open a new window
2738          * but rather use the current separated message view */
2739         summary_display_msg_full(summaryview, summaryview->selected,
2740                                  prefs_common.sep_msg ? FALSE : TRUE, 
2741                                  FALSE);
2742 }
2743
2744 void summary_view_source(SummaryView * summaryview)
2745 {
2746         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2747         MsgInfo *msginfo;
2748         SourceWindow *srcwin;
2749
2750         if (!summaryview->selected) return;
2751
2752         srcwin = source_window_create();
2753         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2754         source_window_show_msg(srcwin, msginfo);
2755         source_window_show(srcwin);
2756 }
2757
2758 void summary_reedit(SummaryView *summaryview)
2759 {
2760         MsgInfo *msginfo;
2761
2762         if (!summaryview->selected) return;
2763         if (!summaryview->folder_item) return;
2764         if (!folder_has_parent_of_type(summaryview->folder_item, F_OUTBOX)
2765         &&  !folder_has_parent_of_type(summaryview->folder_item, F_DRAFT)
2766         &&  !folder_has_parent_of_type(summaryview->folder_item, F_QUEUE))
2767                 return;
2768
2769         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2770                                               summaryview->selected);
2771         if (!msginfo) return;
2772
2773         compose_reedit(msginfo);
2774 }
2775
2776 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2777 {
2778         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2779         GtkCTreeNode *node;
2780
2781         if (summary_is_locked(summaryview)) return FALSE;
2782         if (type == GTK_SCROLL_STEP_FORWARD) {
2783                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2784                 if (node)
2785                         gtkut_ctree_expand_parent_all(ctree, node);
2786                 else
2787                         return FALSE;
2788         } else {
2789                 if (summaryview->selected) {
2790                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2791                         if (!node) return FALSE;
2792                 }
2793         }
2794
2795         if (messageview_is_visible(summaryview->messageview))
2796                 summaryview->display_msg = TRUE;
2797
2798         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2799
2800         if (GTK_CLIST(ctree)->selection)
2801                 gtk_sctree_set_anchor_row
2802                         (GTK_SCTREE(ctree),
2803                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2804
2805         return TRUE;
2806 }
2807
2808 void summary_toggle_view(SummaryView *summaryview)
2809 {
2810         if (!messageview_is_visible(summaryview->messageview) &&
2811             summaryview->selected)
2812                 summary_display_msg(summaryview,
2813                                     summaryview->selected);
2814         else
2815                 main_window_toggle_message_view(summaryview->mainwin);
2816 }
2817
2818 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2819                                                 GtkCTreeNode *node)
2820 {
2821         MsgInfo *msginfo;
2822
2823         if (node) {
2824                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2825                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2826                         return TRUE;
2827                 node = GTK_CTREE_ROW(node)->children;
2828         } else
2829                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2830
2831         while (node) {
2832                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2833                         return TRUE;
2834                 node = GTK_CTREE_ROW(node)->sibling;
2835         }
2836
2837         return FALSE;
2838 }
2839
2840 static gboolean summary_have_unread_children(SummaryView *summaryview,
2841                                              GtkCTreeNode *node)
2842 {
2843         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2844
2845         if (!node) return FALSE;
2846
2847         node = GTK_CTREE_ROW(node)->children;
2848
2849         while (node) {
2850                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2851                         return TRUE;
2852                 node = GTK_CTREE_ROW(node)->sibling;
2853         }
2854         return FALSE;
2855 }
2856
2857 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2858 {
2859         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2860         GtkStyle *style = NULL;
2861         MsgInfo *msginfo;
2862         MsgFlags flags;
2863         gint *col_pos = summaryview->col_pos;
2864
2865         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2866         if (!msginfo) return;
2867
2868         flags = msginfo->flags;
2869
2870         gtk_ctree_node_set_foreground(ctree, row, NULL);
2871
2872         /* set new/unread column */
2873         if (MSG_IS_IGNORE_THREAD(flags)) {
2874                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2875                                           ignorethreadxpm, ignorethreadxpmmask);
2876         } else if (MSG_IS_NEW(flags)) {
2877                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2878                                           newxpm, newxpmmask);
2879         } else if (MSG_IS_UNREAD(flags)) {
2880                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2881                                           unreadxpm, unreadxpmmask);
2882         } else if (MSG_IS_REPLIED(flags)) {
2883                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2884                                           repliedxpm, repliedxpmmask);
2885         } else if (MSG_IS_FORWARDED(flags)) {
2886                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2887                                           forwardedxpm, forwardedxpmmask);
2888         } else {
2889                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2890                                         "");
2891         }
2892
2893         if (prefs_common.bold_unread &&
2894             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2895              (!GTK_CTREE_ROW(row)->expanded &&
2896               GTK_CTREE_ROW(row)->children &&
2897               summary_have_unread_children(summaryview, row))))
2898                 style = bold_style;
2899
2900         /* set mark column */
2901         if (MSG_IS_DELETED(flags)) {
2902                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2903                                           deletedxpm, deletedxpmmask);
2904                 if (style)
2905                         style = bold_deleted_style;
2906                 else {
2907                         style = small_deleted_style;
2908                 }
2909                         gtk_ctree_node_set_foreground
2910                                 (ctree, row, &summaryview->color_dim);
2911         } else if (MSG_IS_MARKED(flags)) {
2912                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2913                                           markxpm, markxpmmask);
2914         } else if (MSG_IS_MOVE(flags)) {
2915                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2916                 if (style)
2917                         style = bold_marked_style;
2918                 else {
2919                         style = small_marked_style;
2920                 }
2921                         gtk_ctree_node_set_foreground
2922                                 (ctree, row, &summaryview->color_marked);
2923         } else if (MSG_IS_COPY(flags)) {
2924                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2925                 if (style)
2926                         style = bold_marked_style;
2927                 else {
2928                         style = small_marked_style;
2929                 }
2930                         gtk_ctree_node_set_foreground
2931                                 (ctree, row, &summaryview->color_marked);
2932 #if 0
2933         } else if ((global_scoring ||
2934                   summaryview->folder_item->prefs->scoring) &&
2935                  (msginfo->score >= summaryview->important_score) &&
2936                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2937                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2938                 gtk_ctree_node_set_foreground(ctree, row,
2939                                               &summaryview->color_important);
2940 #endif
2941         } else {
2942                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "");
2943         }
2944
2945         if (MSG_IS_LOCKED(flags)) {
2946                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2947                                           lockedxpm, lockedxpmmask);
2948         }
2949         else {
2950                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], "");
2951         }
2952
2953         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2954                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2955                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2956         } else if (MSG_IS_SIGNED(flags)) {
2957                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2958                                           gpgsignedxpm, gpgsignedxpmmask);
2959         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2960                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2961                                           clipkeyxpm, clipkeyxpmmask);
2962         } else if (MSG_IS_ENCRYPTED(flags)) {
2963                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2964                                           keyxpm, keyxpmmask);
2965         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2966                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2967                                           clipxpm, clipxpmmask);
2968         } else {
2969                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], "");
2970         }
2971         if (!style)
2972                 style = small_style;
2973
2974         gtk_ctree_node_set_row_style(ctree, row, style);
2975
2976         if (MSG_GET_COLORLABEL(flags))
2977                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2978 }
2979
2980 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2981 {
2982         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2983         MsgInfo *msginfo;
2984
2985         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2986         if (MSG_IS_DELETED(msginfo->flags))
2987                 summaryview->deleted--;
2988         if (MSG_IS_MOVE(msginfo->flags))
2989                 summaryview->moved--;
2990         if (MSG_IS_COPY(msginfo->flags))
2991                 summaryview->copied--;
2992
2993         procmsg_msginfo_set_to_folder(msginfo, NULL);
2994         summary_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2995         summary_set_row_marks(summaryview, row);
2996         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2997 }
2998
2999 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
3000 {
3001         gboolean changed = FALSE;
3002         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3003         MsgInfo *msginfo;
3004
3005         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3006         if (MSG_IS_DELETED(msginfo->flags))
3007                 summaryview->deleted--;
3008         if (MSG_IS_MOVE(msginfo->flags)) {
3009                 summaryview->moved--;
3010                 changed = TRUE;
3011         }
3012         if (MSG_IS_COPY(msginfo->flags)) {
3013                 summaryview->copied--;
3014                 changed = TRUE;
3015         }
3016         procmsg_msginfo_set_to_folder(msginfo, NULL);
3017         summary_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
3018         
3019         summary_set_row_marks(summaryview, row);
3020         debug_print("Message %d is locked\n", msginfo->msgnum);
3021 }
3022
3023 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
3024 {
3025         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3026         MsgInfo *msginfo;
3027
3028         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3029         if (!MSG_IS_LOCKED(msginfo->flags))
3030                 return;
3031         procmsg_msginfo_set_to_folder(msginfo, NULL);
3032         summary_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
3033         summary_set_row_marks(summaryview, row);
3034         debug_print("Message %d is unlocked\n", msginfo->msgnum);
3035 }
3036
3037 void summary_mark(SummaryView *summaryview)
3038 {
3039         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3040         GList *cur;
3041
3042         START_LONG_OPERATION(summaryview);
3043         folder_item_set_batch(summaryview->folder_item, TRUE);
3044         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3045                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
3046         folder_item_set_batch(summaryview->folder_item, FALSE);
3047         END_LONG_OPERATION(summaryview);
3048
3049         summary_status_show(summaryview);
3050 }
3051
3052 static void summary_mark_row_as_read(SummaryView *summaryview,
3053                                      GtkCTreeNode *row)
3054 {
3055         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3056         MsgInfo *msginfo;
3057
3058         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3059
3060         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
3061                 return;
3062
3063         summary_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
3064         summary_set_row_marks(summaryview, row);
3065         debug_print("Message %d is marked as read\n",
3066                 msginfo->msgnum);
3067 }
3068
3069 void summary_mark_as_read(SummaryView *summaryview)
3070 {
3071         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3072         GList *cur;
3073
3074         START_LONG_OPERATION(summaryview);
3075         folder_item_set_batch(summaryview->folder_item, TRUE);
3076         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3077                 summary_mark_row_as_read(summaryview,
3078                                          GTK_CTREE_NODE(cur->data));
3079         folder_item_set_batch(summaryview->folder_item, FALSE);
3080         END_LONG_OPERATION(summaryview);
3081         
3082         summary_status_show(summaryview);
3083 }
3084
3085 void summary_msgs_lock(SummaryView *summaryview)
3086 {
3087         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3088         GList *cur;
3089
3090         START_LONG_OPERATION(summaryview);
3091         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3092                 summary_lock_row(summaryview,
3093                                          GTK_CTREE_NODE(cur->data));
3094         END_LONG_OPERATION(summaryview);
3095         
3096         summary_status_show(summaryview);
3097 }
3098
3099 void summary_msgs_unlock(SummaryView *summaryview)
3100 {
3101         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3102         GList *cur;
3103
3104         START_LONG_OPERATION(summaryview);
3105         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3106                 summary_unlock_row(summaryview,
3107                                    GTK_CTREE_NODE(cur->data));
3108         END_LONG_OPERATION(summaryview);
3109         
3110         summary_status_show(summaryview);
3111 }
3112
3113 void summary_mark_all_read(SummaryView *summaryview)
3114 {
3115         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3116         GtkCTreeNode *node;
3117
3118         START_LONG_OPERATION(summaryview);
3119         folder_item_set_batch(summaryview->folder_item, TRUE);
3120         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3121              node = gtkut_ctree_node_next(ctree, node))
3122                 summary_mark_row_as_read(summaryview, node);
3123         folder_item_set_batch(summaryview->folder_item, FALSE);
3124         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3125              node = gtkut_ctree_node_next(ctree, node)) {
3126                 if (!GTK_CTREE_ROW(node)->expanded)
3127                         summary_set_row_marks(summaryview, node);
3128         }
3129         END_LONG_OPERATION(summaryview);
3130         
3131         summary_status_show(summaryview);
3132 }
3133
3134 static void summary_mark_row_as_unread(SummaryView *summaryview,
3135                                        GtkCTreeNode *row)
3136 {
3137         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3138         MsgInfo *msginfo;
3139
3140         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3141         if (MSG_IS_DELETED(msginfo->flags)) {
3142                 procmsg_msginfo_set_to_folder(msginfo, NULL);
3143                 summary_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
3144                 summaryview->deleted--;
3145         }
3146
3147         summary_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
3148         debug_print("Message %d is marked as unread\n",
3149                 msginfo->msgnum);
3150
3151         summary_set_row_marks(summaryview, row);
3152 }
3153
3154 void summary_mark_as_unread(SummaryView *summaryview)
3155 {
3156         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3157         GList *cur;
3158
3159         START_LONG_OPERATION(summaryview);
3160         folder_item_set_batch(summaryview->folder_item, TRUE);
3161         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3162                 cur = cur->next)
3163                 summary_mark_row_as_unread(summaryview,
3164                                            GTK_CTREE_NODE(cur->data));
3165         folder_item_set_batch(summaryview->folder_item, FALSE);
3166         END_LONG_OPERATION(summaryview);
3167         
3168         summary_status_show(summaryview);
3169 }
3170
3171 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
3172 {
3173         GList * cur;
3174         gboolean found;
3175
3176         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
3177
3178         case F_NEWS:
3179
3180                 /*
3181                   security : checks if one the accounts correspond to
3182                   the author of the post
3183                 */
3184
3185                 found = FALSE;
3186                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3187                         PrefsAccount * account;
3188                         gchar * from_name;
3189                         
3190                         account = cur->data;
3191                         if (account->name && *account->name)
3192                                 from_name =
3193                                         g_strdup_printf("%s <%s>",
3194                                                         account->name,
3195                                                         account->address);
3196                         else
3197                                 from_name =
3198                                         g_strdup_printf("%s",
3199                                                         account->address);
3200                         
3201                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3202                                 g_free(from_name);
3203                                 found = TRUE;
3204                                 break;
3205                         }
3206                         g_free(from_name);
3207                 }
3208
3209                 if (!found) {
3210                         alertpanel_error(_("You're not the author of the article.\n"));
3211                 }
3212                 
3213                 return found;
3214
3215         default:
3216                 return TRUE;
3217         }
3218 }
3219
3220 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3221 {
3222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3223         MsgInfo *msginfo;
3224
3225         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3226
3227         if (MSG_IS_LOCKED(msginfo->flags)) return;
3228
3229         if (MSG_IS_DELETED(msginfo->flags)) return;
3230
3231         if (MSG_IS_MOVE(msginfo->flags))
3232                 summaryview->moved--;
3233         if (MSG_IS_COPY(msginfo->flags))
3234                 summaryview->copied--;
3235
3236         procmsg_msginfo_set_to_folder(msginfo, NULL);
3237         summary_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, MSG_MOVE | MSG_COPY);
3238         summaryview->deleted++;
3239
3240         if (!prefs_common.immediate_exec && 
3241             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3242                 summary_set_row_marks(summaryview, row);