2006-02-08 [colin] 2.0.0cvs33
[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 (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1743                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1744                         break;
1745         }
1746
1747         return node;
1748 }
1749
1750 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1751                                                 guint msgnum)
1752 {
1753         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1754         GtkCTreeNode *node;
1755         MsgInfo *msginfo;
1756
1757         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1758
1759         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1760                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1761                 if (msginfo && msginfo->msgnum == msgnum) break;
1762         }
1763
1764         return node;
1765 }
1766
1767 static guint attract_hash_func(gconstpointer key)
1768 {
1769         gchar *str;
1770         gchar *p;
1771         guint h;
1772
1773         Xstrdup_a(str, (const gchar *)key, return 0);
1774         trim_subject(str);
1775
1776         p = str;
1777         h = *p;
1778
1779         if (h) {
1780                 for (p += 1; *p != '\0'; p++)
1781                         h = (h << 5) - h + *p;
1782         }
1783
1784         return h;
1785 }
1786
1787 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1788 {
1789         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1790 }
1791
1792 void summary_attract_by_subject(SummaryView *summaryview)
1793 {
1794         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1795         GtkCList *clist = GTK_CLIST(ctree);
1796         GtkCTreeNode *src_node;
1797         GtkCTreeNode *dst_node, *sibling;
1798         GtkCTreeNode *tmp;
1799         MsgInfo *src_msginfo, *dst_msginfo;
1800         GHashTable *subject_table;
1801
1802         debug_print("Attracting messages by subject...");
1803         STATUSBAR_PUSH(summaryview->mainwin,
1804                        _("Attracting messages by subject..."));
1805
1806         main_window_cursor_wait(summaryview->mainwin);
1807         gtk_clist_freeze(clist);
1808
1809         subject_table = g_hash_table_new(attract_hash_func,
1810                                          attract_compare_func);
1811
1812         for (src_node = GTK_CTREE_NODE(clist->row_list);
1813              src_node != NULL;
1814              src_node = tmp) {
1815                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1816                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1817                 if (!src_msginfo) continue;
1818                 if (!src_msginfo->subject) continue;
1819
1820                 /* find attracting node */
1821                 dst_node = g_hash_table_lookup(subject_table,
1822                                                src_msginfo->subject);
1823
1824                 if (dst_node) {
1825                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1826
1827                         /* if the time difference is more than 20 days,
1828                            don't attract */
1829                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1830                             > 60 * 60 * 24 * 20)
1831                                 continue;
1832
1833                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1834                         if (src_node != sibling)
1835                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1836                 }
1837
1838                 g_hash_table_insert(subject_table,
1839                                     src_msginfo->subject, src_node);
1840         }
1841
1842         g_hash_table_destroy(subject_table);
1843
1844         gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
1845
1846         gtk_clist_thaw(clist);
1847
1848         debug_print("done.\n");
1849         STATUSBAR_POP(summaryview->mainwin);
1850
1851         main_window_cursor_normal(summaryview->mainwin);
1852 }
1853
1854 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1855                                       gpointer data)
1856 {
1857         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1858
1859         if (msginfo)
1860                 procmsg_msginfo_free(msginfo);
1861 }
1862
1863 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1864                                    gpointer data)
1865 {
1866         SummaryView *summaryview = data;
1867         MsgInfo *msginfo;
1868
1869         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1870
1871         if (msginfo->folder && msginfo->folder->folder &&
1872             msginfo->folder->folder->klass->type == F_NEWS)
1873                 news_flag_crosspost(msginfo);
1874
1875         if (MSG_IS_DELETED(msginfo->flags))
1876                 summaryview->deleted++;
1877
1878         summaryview->total_size += msginfo->size;
1879
1880         summary_set_row_marks(summaryview, node);
1881 }
1882
1883 static void summary_update_status(SummaryView *summaryview)
1884 {
1885         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1886         GtkCTreeNode *node;
1887         MsgInfo *msginfo;
1888
1889         summaryview->total_size =
1890         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1891
1892         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1893              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1894                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1895
1896                 if (MSG_IS_DELETED(msginfo->flags))
1897                         summaryview->deleted++;
1898                 if (MSG_IS_MOVE(msginfo->flags))
1899                         summaryview->moved++;
1900                 if (MSG_IS_COPY(msginfo->flags))
1901                         summaryview->copied++;
1902                 summaryview->total_size += msginfo->size;
1903         }
1904 }
1905
1906 static void summary_status_show(SummaryView *summaryview)
1907 {
1908         gchar *str;
1909         gchar *del, *mv, *cp;
1910         gchar *sel;
1911         gchar *spc;
1912         gchar *itstr;
1913         GList *rowlist, *cur;
1914         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
1915         off_t sel_size = 0, n_size = 0;
1916         MsgInfo *msginfo;
1917         gchar *name;
1918         
1919         if (!summaryview->folder_item) {
1920                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1921                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1922                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1923                 return;
1924         }
1925
1926         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1927         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1928                 msginfo = gtk_ctree_node_get_row_data
1929                         (GTK_CTREE(summaryview->ctree),
1930                          GTK_CTREE_NODE(cur->data));
1931                 if (!msginfo)
1932                         g_warning("summary_status_show(): msginfo == NULL\n");
1933                 else {
1934                         sel_size += msginfo->size;
1935                         n_selected++;
1936                         
1937                 }
1938         }
1939         
1940         if (summaryview->folder_item->hide_read_msgs 
1941         || quicksearch_is_active(summaryview->quicksearch)) {
1942                 rowlist = GTK_CLIST(summaryview->ctree)->row_list;
1943                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1944                         msginfo = gtk_ctree_node_get_row_data
1945                                 (GTK_CTREE(summaryview->ctree),
1946                                  GTK_CTREE_NODE(cur));
1947                         if (!msginfo)
1948                                 g_warning("summary_status_show(): msginfo == NULL\n");
1949                         else {
1950                                 n_size += msginfo->size;
1951                                 n_total++;
1952                                 if (MSG_IS_NEW(msginfo->flags))
1953                                         n_new++;
1954                                 if (MSG_IS_UNREAD(msginfo->flags))
1955                                         n_unread++;
1956                         }
1957                 }
1958         } else {
1959                 n_new = summaryview->folder_item->new_msgs;
1960                 n_unread = summaryview->folder_item->unread_msgs;
1961                 n_total = summaryview->folder_item->total_msgs;
1962                 n_size = summaryview->total_size;
1963         }
1964
1965         name = folder_item_get_name(summaryview->folder_item);
1966         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1967         g_free(name);
1968
1969         if (summaryview->deleted)
1970                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1971         else
1972                 del = g_strdup("");
1973         if (summaryview->moved)
1974                 mv = g_strdup_printf(_("%s%d moved"),
1975                                      summaryview->deleted ? _(", ") : "",
1976                                      summaryview->moved);
1977         else
1978                 mv = g_strdup("");
1979         if (summaryview->copied)
1980                 cp = g_strdup_printf(_("%s%d copied"),
1981                                      summaryview->deleted ||
1982                                      summaryview->moved ? _(", ") : "",
1983                                      summaryview->copied);
1984         else
1985                 cp = g_strdup("");
1986
1987         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1988                 spc = "    ";
1989         else
1990                 spc = "";
1991
1992         if (n_selected) {
1993                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1994                 if (n_selected == 1)
1995                         itstr = g_strdup(_(" item selected"));
1996                 else
1997                         itstr = g_strdup(_(" items selected"));
1998         } else {
1999                 sel = g_strdup("");
2000                 itstr = g_strdup("");
2001         }
2002                 
2003         str = g_strconcat(n_selected ? itos(n_selected) : "",
2004                                         itstr, sel, spc, del, mv, cp, NULL);
2005         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2006         g_free(str);
2007         g_free(sel);
2008         g_free(del);
2009         g_free(mv);
2010         g_free(cp);
2011         g_free(itstr);
2012
2013         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2014
2015                                       n_new, n_unread, n_total,
2016                                       to_human_readable(n_size));
2017         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2018         g_free(str);
2019 }
2020
2021 static void summary_set_column_titles(SummaryView *summaryview)
2022 {
2023         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2024         GtkWidget *hbox;
2025         GtkWidget *label;
2026         GtkWidget *arrow;
2027         gint pos;
2028         const gchar *title;
2029         SummaryColumnType type;
2030         GtkJustification justify;
2031
2032         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2033                 SORT_BY_MARK,
2034                 SORT_BY_STATUS,
2035                 SORT_BY_MIME,
2036                 SORT_BY_SUBJECT,
2037                 SORT_BY_FROM,
2038                 SORT_BY_TO,
2039                 SORT_BY_DATE,
2040                 SORT_BY_SIZE,
2041                 SORT_BY_NUMBER,
2042                 SORT_BY_SCORE,
2043                 SORT_BY_LOCKED
2044         };
2045
2046         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2047                 type = summaryview->col_state[pos].type;
2048
2049                 /* CLAWS: mime and unread are single char headers */
2050                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2051                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2052
2053                 switch (type) {
2054                 case S_COL_SUBJECT:
2055                 case S_COL_FROM:
2056                 case S_COL_TO:
2057                 case S_COL_DATE:
2058                 case S_COL_NUMBER:
2059                         if (prefs_common.trans_hdr)
2060                                 title = gettext(col_label[type]);
2061                         else
2062                                 title = col_label[type];
2063                         break;
2064                 default:
2065                         title = gettext(col_label[type]);
2066                 }
2067
2068                 if (type == S_COL_MIME) {
2069                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2070                         gtk_widget_show(label);
2071                         gtk_clist_set_column_widget(clist, pos, label);
2072                         continue;
2073                 } else if (type == S_COL_MARK) {
2074                         label = gtk_image_new_from_pixmap(markxpm, markxpmmask);
2075                         gtk_widget_show(label);
2076                         gtk_clist_set_column_widget(clist, pos, label);
2077                         continue;
2078                 } else if (type == S_COL_LOCKED) {
2079                         label = gtk_image_new_from_pixmap(lockedxpm, lockedxpmmask);
2080                         gtk_widget_show(label);
2081                         gtk_clist_set_column_widget(clist, pos, label);
2082                         continue;
2083                 } else if (type == S_COL_STATUS) {
2084                         gtk_clist_set_column_title(clist, pos, title);
2085                         continue;
2086                 }
2087
2088                 hbox  = gtk_hbox_new(FALSE, 4);
2089                 label = gtk_label_new(title);
2090
2091                 if (justify == GTK_JUSTIFY_RIGHT)
2092                         gtk_box_pack_end(GTK_BOX(hbox), label,
2093                                          FALSE, FALSE, 0);
2094                 else
2095                         gtk_box_pack_start(GTK_BOX(hbox), label,
2096                                            FALSE, FALSE, 0);
2097
2098                 if (summaryview->sort_key == sort_by[type]) {
2099                         arrow = gtk_arrow_new
2100                                 (summaryview->sort_type == SORT_ASCENDING
2101                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2102                                  GTK_SHADOW_IN);
2103                         if (justify == GTK_JUSTIFY_RIGHT)
2104                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2105                                                    FALSE, FALSE, 0);
2106                         else
2107                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2108                                                  FALSE, FALSE, 0);
2109                 }
2110
2111                 gtk_widget_show_all(hbox);
2112                 gtk_clist_set_column_widget(clist, pos, hbox);
2113         }
2114 }
2115
2116 void summary_reflect_prefs(void)
2117 {
2118         static gchar *last_font = NULL;
2119         gboolean update_font = TRUE;
2120         SummaryView *summaryview = NULL;
2121
2122         if (!mainwindow_get_mainwindow())
2123                 return;
2124         summaryview = mainwindow_get_mainwindow()->summaryview;
2125
2126         if (last_font && !strcmp(last_font, NORMAL_FONT))
2127                 update_font = FALSE;
2128
2129         if (last_font)
2130                 g_free(last_font);
2131         
2132         last_font = g_strdup(NORMAL_FONT);
2133
2134         if (update_font) {      
2135                 bold_style = bold_marked_style = bold_deleted_style = 
2136                         small_style = small_marked_style = small_deleted_style = NULL;
2137                 summary_set_fonts(summaryview);
2138         }
2139
2140         summary_set_column_titles(summaryview);
2141         summary_show(summaryview, summaryview->folder_item);
2142 }
2143
2144 void summary_sort(SummaryView *summaryview,
2145                   FolderSortKey sort_key, FolderSortType sort_type)
2146 {
2147         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2148         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2149         GtkCListCompareFunc cmp_func = NULL;
2150         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2151                                        G_CALLBACK(summary_tree_expanded), summaryview);
2152         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2153
2154         switch (sort_key) {
2155         case SORT_BY_MARK:
2156                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2157                 break;
2158         case SORT_BY_STATUS:
2159                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2160                 break;
2161         case SORT_BY_MIME:
2162                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2163                 break;
2164         case SORT_BY_NUMBER:
2165                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2166                 break;
2167         case SORT_BY_SIZE:
2168                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2169                 break;
2170         case SORT_BY_DATE:
2171                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2172                 break;
2173         case SORT_BY_FROM:
2174                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2175                 break;
2176         case SORT_BY_SUBJECT:
2177                 if (summaryview->simplify_subject_preg)
2178                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2179                 else
2180                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2181                 break;
2182         case SORT_BY_SCORE:
2183                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2184                 break;
2185         case SORT_BY_LABEL:
2186                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2187                 break;
2188         case SORT_BY_TO:
2189                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2190                 break;
2191         case SORT_BY_LOCKED:
2192                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2193                 break;
2194         case SORT_BY_NONE:
2195                 break;
2196         default:
2197                 goto unlock;
2198         }
2199
2200         summaryview->sort_key = sort_key;
2201         summaryview->sort_type = sort_type;
2202
2203         summary_set_column_titles(summaryview);
2204         summary_set_menu_sensitive(summaryview);
2205
2206         /* allow fallback to don't sort */
2207         if (summaryview->sort_key == SORT_BY_NONE)
2208                 goto unlock;
2209
2210         if (cmp_func != NULL) {
2211                 debug_print("Sorting summary...");
2212                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2213
2214                 main_window_cursor_wait(summaryview->mainwin);
2215
2216                 gtk_clist_freeze(clist);
2217                 gtk_clist_set_compare_func(clist, cmp_func);
2218
2219                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2220
2221                 gtk_sctree_sort_recursive(ctree, NULL);
2222
2223                 gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2224
2225                 main_window_cursor_normal(summaryview->mainwin);
2226                 gtk_clist_thaw(clist);
2227
2228                 debug_print("done.\n");
2229                 STATUSBAR_POP(summaryview->mainwin);
2230         }
2231 unlock:
2232         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2233         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2234                                        G_CALLBACK(summary_tree_expanded), summaryview);
2235 }
2236
2237 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2238                                    GtkCTreeNode *cnode, gpointer data)
2239 {
2240         SummaryView *summaryview = (SummaryView *)data;
2241         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2242         gchar *text[N_SUMMARY_COLS];
2243         gint *col_pos = summaryview->col_pos;
2244         const gchar *msgid = msginfo->msgid;
2245         GHashTable *msgid_table = summaryview->msgid_table;
2246         gboolean free_from_to = FALSE;
2247         
2248         summary_set_header(summaryview, text, msginfo, &free_from_to);
2249
2250         gtk_sctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2251                                 NULL, NULL, NULL, NULL, FALSE,
2252                                 gnode->parent->parent ? TRUE : FALSE);
2253 #define SET_TEXT(col) \
2254         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2255                                 text[col_pos[col]])
2256
2257         SET_TEXT(S_COL_NUMBER);
2258         SET_TEXT(S_COL_SCORE);
2259         SET_TEXT(S_COL_SIZE);
2260         SET_TEXT(S_COL_DATE);
2261         SET_TEXT(S_COL_FROM);
2262         SET_TEXT(S_COL_TO);
2263         /* SET_TEXT(S_COL_SUBJECT);  already set by node info */
2264
2265         if (free_from_to) {
2266                 g_free(text[col_pos[S_COL_FROM]]);
2267                 g_free(text[col_pos[S_COL_TO]]);
2268                 text[col_pos[S_COL_FROM]] = NULL;
2269                 text[col_pos[S_COL_TO]] = NULL;
2270         }
2271
2272 #undef SET_TEXT
2273
2274         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2275         summary_set_marks_func(ctree, cnode, summaryview);
2276
2277         if (msgid && msgid[0] != '\0')
2278                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2279
2280         return TRUE;
2281 }
2282
2283 static void summary_set_ctree_from_list(SummaryView *summaryview,
2284                                         GSList *mlist)
2285 {
2286         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2287         MsgInfo *msginfo;
2288         GtkCTreeNode *node = NULL;
2289         GHashTable *msgid_table;
2290         GHashTable *subject_table;
2291         GSList * cur;
2292         START_TIMING("summary_set_ctree_from_list");
2293         
2294         if (!mlist) return;
2295
2296         debug_print("\tSetting summary from message data...");
2297         STATUSBAR_PUSH(summaryview->mainwin,
2298                        _("Setting summary from message data..."));
2299         gdk_flush();
2300
2301         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2302                                        G_CALLBACK(summary_tree_expanded), summaryview);
2303
2304         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2305         summaryview->msgid_table = msgid_table;
2306         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2307         summaryview->subject_table = subject_table;
2308
2309         if (prefs_common.use_addr_book)
2310                 start_address_completion();
2311         
2312         if (summaryview->threaded) {
2313                 GNode *root, *gnode;
2314
2315                 root = procmsg_get_thread_tree(mlist);
2316
2317                 for (gnode = root->children; gnode != NULL;
2318                      gnode = gnode->next) {
2319                         node = gtk_ctree_insert_gnode
2320                                 (ctree, NULL, node, gnode,
2321                                  summary_insert_gnode_func, summaryview);
2322                 }
2323
2324                 g_node_destroy(root);
2325                 
2326                 summary_thread_init(summaryview);
2327         } else {
2328                 gchar *text[N_SUMMARY_COLS];
2329                 gboolean free_from_to = FALSE;
2330                 gint *col_pos = summaryview->col_pos;
2331
2332                 cur = mlist;
2333                 for (; mlist != NULL; mlist = mlist->next) {
2334                         msginfo = (MsgInfo *)mlist->data;
2335
2336                         summary_set_header(summaryview, text, msginfo, &free_from_to);
2337
2338                         node = gtk_sctree_insert_node
2339                                 (ctree, NULL, node, text, 2,
2340                                  NULL, NULL, NULL, NULL,
2341                                  FALSE, FALSE);
2342                         if (free_from_to) {
2343                                 g_free(text[col_pos[S_COL_FROM]]);
2344                                 g_free(text[col_pos[S_COL_TO]]);
2345                                 text[col_pos[S_COL_FROM]] = NULL;
2346                                 text[col_pos[S_COL_TO]] = NULL;
2347                         }
2348                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2349                         summary_set_marks_func(ctree, node, summaryview);
2350
2351                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2352                                 g_hash_table_insert(msgid_table,
2353                                                     msginfo->msgid, node);
2354
2355                         subject_table_insert(subject_table,
2356                                              msginfo->subject,
2357                                              node);
2358                 }
2359                 mlist = cur;
2360         }
2361
2362         if (prefs_common.enable_hscrollbar &&
2363             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2364                 gint optimal_width;
2365
2366                 optimal_width = gtk_clist_optimal_column_width
2367                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2368                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2369                                            summaryview->col_pos[S_COL_SUBJECT],
2370                                            optimal_width);
2371         }
2372
2373         if (prefs_common.use_addr_book)
2374                 end_address_completion();
2375
2376         debug_print("done.\n");
2377         STATUSBAR_POP(summaryview->mainwin);
2378         if (debug_get_mode()) {
2379                 debug_print("\tmsgid hash table size = %d\n",
2380                             g_hash_table_size(msgid_table));
2381                 debug_print("\tsubject hash table size = %d\n",
2382                             g_hash_table_size(subject_table));
2383         }
2384
2385         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2386
2387         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2388
2389         if (prefs_common.bold_unread) {
2390                 while (node) {
2391                         GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2392                         if (GTK_CTREE_ROW(node)->children)
2393                                 summary_set_row_marks(summaryview, node);
2394                         node = next;
2395                 }
2396         }
2397
2398         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2399                                        G_CALLBACK(summary_tree_expanded), summaryview);
2400         END_TIMING();
2401 }
2402
2403 static gchar *summary_complete_address(const gchar *addr)
2404 {
2405         gint count;
2406         gchar *res, *tmp, *email_addr;
2407         
2408         if (addr == NULL)
2409                 return NULL;
2410
2411         Xstrdup_a(email_addr, addr, return NULL);
2412         extract_address(email_addr);
2413         g_return_val_if_fail(*email_addr, NULL);
2414
2415         /*
2416          * completion stuff must be already initialized
2417          */
2418         res = NULL;
2419         if (1 < (count = complete_address(email_addr))) {
2420                 tmp = get_complete_address(1);
2421                 res = procheader_get_fromname(tmp);
2422                 g_free(tmp);
2423         }
2424
2425         return res;
2426 }
2427
2428 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2429                                MsgInfo *msginfo, gboolean *free_from_to)
2430 {
2431         static gchar date_modified[80];
2432         static gchar col_score[11];
2433         static gchar buf[BUFFSIZE];
2434         gint *col_pos = summaryview->col_pos;
2435         gchar *from_text = NULL, *to_text = NULL;
2436         gboolean should_swap = FALSE;
2437
2438         text[col_pos[S_COL_FROM]]   = "";
2439         text[col_pos[S_COL_TO]]     = "";
2440         text[col_pos[S_COL_SUBJECT]]= "";
2441         text[col_pos[S_COL_MARK]]   = "";
2442         text[col_pos[S_COL_STATUS]] = "";
2443         text[col_pos[S_COL_MIME]]   = "";
2444         text[col_pos[S_COL_LOCKED]] = "";
2445         text[col_pos[S_COL_DATE]]   = "";
2446         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2447         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2448         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2449
2450         if (msginfo->date_t) {
2451                 procheader_date_get_localtime(date_modified,
2452                                               sizeof(date_modified),
2453                                               msginfo->date_t);
2454                 text[col_pos[S_COL_DATE]] = date_modified;
2455         } else if (msginfo->date)
2456                 text[col_pos[S_COL_DATE]] = msginfo->date;
2457         else
2458                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2459
2460         if (prefs_common.swap_from && msginfo->from && msginfo->to) {
2461                 gchar *addr = NULL;
2462                 
2463                 addr = g_strdup(msginfo->from);
2464
2465                 if (addr) {
2466                         extract_address(addr);
2467                         if (account_find_from_address(addr)) {
2468                                 should_swap = TRUE;
2469                         }
2470                         g_free(addr);
2471                 }
2472         }
2473
2474         if (!prefs_common.use_addr_book) {
2475                 from_text = msginfo->fromname ? 
2476                                 msginfo->fromname :
2477                                 _("(No From)");
2478         } else {
2479                 gchar *tmp = summary_complete_address(msginfo->from);
2480                 from_text = tmp ? tmp : (msginfo->fromname ?
2481                                          msginfo->fromname: 
2482                                                 _("(No From)"));
2483         }
2484         
2485         to_text = msginfo->to ? msginfo->to : 
2486                    (msginfo->cc ? msginfo->cc :
2487                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2488                      )
2489                    );
2490
2491         if (!should_swap) {
2492                 text[col_pos[S_COL_FROM]] = from_text;
2493                 text[col_pos[S_COL_TO]] = to_text;
2494                 *free_from_to = FALSE;
2495         } else {
2496                 gchar *tmp = NULL;
2497                 tmp = g_strconcat("-->", to_text, NULL);
2498                 text[col_pos[S_COL_FROM]] = tmp;
2499                 tmp = g_strconcat("<--", from_text, NULL);
2500                 text[col_pos[S_COL_TO]] = tmp;
2501                 *free_from_to = TRUE;
2502         }
2503         
2504         if (summaryview->simplify_subject_preg != NULL)
2505                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2506                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2507                                         summaryview->simplify_subject_preg) : 
2508                         _("(No Subject)");
2509         else 
2510                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2511                         _("(No Subject)");
2512 }
2513
2514 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2515 {
2516         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2517 }
2518
2519 static gboolean defer_change(gpointer data);
2520 typedef struct _ChangeData {
2521         MsgInfo *info;
2522         gint op; /* 0, 1, 2 for unset, set, change */
2523         MsgPermFlags set_flags;
2524         MsgTmpFlags  set_tmp_flags;
2525         MsgPermFlags unset_flags;
2526         MsgTmpFlags  unset_tmp_flags;
2527 } ChangeData;
2528
2529 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2530 {
2531         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2532                 debug_print("flags: doing unset now\n");
2533                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
2534         } else {
2535                 ChangeData *unset_data = g_new0(ChangeData, 1);
2536                 unset_data->info = msginfo;
2537                 unset_data->op = 0;
2538                 unset_data->unset_flags = flags;
2539                 unset_data->unset_tmp_flags = tmp_flags;
2540                 debug_print("flags: deferring unset\n");
2541                 g_timeout_add(100, defer_change, unset_data);
2542         }
2543 }
2544
2545 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2546 {
2547         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2548                 debug_print("flags: doing set now\n");
2549                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
2550         } else {
2551                 ChangeData *set_data = g_new0(ChangeData, 1);
2552                 set_data->info = msginfo;
2553                 set_data->op = 1;
2554                 set_data->set_flags = flags;
2555                 set_data->set_tmp_flags = tmp_flags;
2556                 debug_print("flags: deferring set\n");
2557                 g_timeout_add(100, defer_change, set_data);
2558         }
2559 }
2560
2561 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
2562                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
2563                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
2564 {
2565         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2566                 debug_print("flags: doing change now\n");
2567                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
2568                         rem_flags, rem_tmp_flags);
2569         } else {
2570                 ChangeData *change_data = g_new0(ChangeData, 1);
2571                 change_data->info = msginfo;
2572                 change_data->op = 2;
2573                 change_data->set_flags = add_flags;
2574                 change_data->set_tmp_flags = add_tmp_flags;
2575                 change_data->unset_flags = rem_flags;
2576                 change_data->unset_tmp_flags = rem_tmp_flags;
2577                 debug_print("flags: deferring change\n");
2578                 g_timeout_add(100, defer_change, change_data);
2579         }
2580 }
2581
2582 gboolean defer_change(gpointer data)
2583 {
2584         ChangeData *chg = (ChangeData *)data;
2585         if (chg->info->folder && chg->info->folder->processing_pending) {
2586                 debug_print("flags: trying later\n");
2587                 return TRUE; /* try again */
2588         } else {
2589                 debug_print("flags: finally doing it\n");
2590                 switch(chg->op) {
2591                 case 0:
2592                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
2593                         break;
2594                 case 1:
2595                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
2596                         break;
2597                 case 2:
2598                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
2599                                 chg->unset_flags, chg->unset_tmp_flags);
2600                         break;
2601                 default:
2602                         g_warning("shouldn't happen\n");
2603                 }
2604                 g_free(chg);
2605         }
2606         return FALSE;
2607 }
2608
2609 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2610                                       GtkCTreeNode *row)
2611 {
2612         g_return_if_fail(summaryview != NULL);
2613         g_return_if_fail(msginfo != NULL);
2614         g_return_if_fail(row != NULL);
2615
2616         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2617                 summary_msginfo_unset_flags
2618                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2619                 summary_set_row_marks(summaryview, row);
2620                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2621                 summary_status_show(summaryview);
2622         }
2623 }
2624
2625 typedef struct  {
2626         MsgInfo *msginfo;
2627         SummaryView *summaryview;
2628         GtkCTreeNode *row;
2629 } MarkAsReadData;
2630
2631 static int msginfo_mark_as_read_timeout(void *data)
2632 {
2633         MarkAsReadData *mdata = (MarkAsReadData *)data;
2634         if (!mdata)
2635                 return FALSE;
2636         
2637         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2638                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2639                                      mdata->row); 
2640
2641         g_free(mdata);
2642
2643         return FALSE;   
2644 }
2645
2646 static void summary_display_msg_full(SummaryView *summaryview,
2647                                      GtkCTreeNode *row,
2648                                      gboolean new_window, gboolean all_headers)
2649 {
2650         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2651         MsgInfo *msginfo;
2652         gint val;
2653
2654         if (!new_window) {
2655                 if (summaryview->displayed == row)
2656                         return;
2657                 else
2658                         summaryview->messageview->filtered = FALSE;
2659         }                       
2660         
2661         g_return_if_fail(row != NULL);
2662
2663         if (summary_is_locked(summaryview)) return;
2664         summary_lock(summaryview);
2665
2666         STATUSBAR_POP(summaryview->mainwin);
2667         GTK_EVENTS_FLUSH();
2668
2669         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2670
2671         if (new_window) {
2672                 MessageView *msgview;
2673
2674                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2675                 val = messageview_show(msgview, msginfo, all_headers);
2676         } else {
2677                 MessageView *msgview;
2678
2679                 msgview = summaryview->messageview;
2680
2681                 summaryview->displayed = row;
2682                 if (!messageview_is_visible(msgview)) {
2683                         main_window_toggle_message_view(summaryview->mainwin);
2684                         GTK_EVENTS_FLUSH();
2685                 }
2686                 val = messageview_show(msgview, msginfo, all_headers);
2687                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2688                         gtk_widget_grab_focus(summaryview->ctree);
2689                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2690         }
2691
2692         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2693                 if (prefs_common.mark_as_read_delay) {
2694                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2695                         data->summaryview = summaryview;
2696                         data->msginfo = msginfo;
2697                         data->row = row;
2698                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2699                                 msginfo_mark_as_read_timeout, data);
2700                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2701                         msginfo_mark_as_read(summaryview, msginfo, row);
2702                 }
2703         }
2704
2705         summary_set_menu_sensitive(summaryview);
2706         toolbar_main_set_sensitive(summaryview->mainwin);
2707         messageview_set_menu_sensitive(summaryview->messageview);
2708
2709         summary_unlock(summaryview);
2710 }
2711
2712 void summary_display_msg_selected(SummaryView *summaryview,
2713                                   gboolean all_headers)
2714 {
2715         if (summary_is_locked(summaryview)) return;
2716         summaryview->displayed = NULL;
2717         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2718                                  all_headers);
2719 }
2720
2721 void summary_redisplay_msg(SummaryView *summaryview)
2722 {
2723         GtkCTreeNode *node;
2724
2725         if (summaryview->displayed) {
2726                 node = summaryview->displayed;
2727                 summaryview->displayed = NULL;
2728                 summary_display_msg(summaryview, node);
2729         }
2730 }
2731
2732 void summary_open_msg(SummaryView *summaryview)
2733 {
2734         if (!summaryview->selected) return;
2735         
2736         /* CLAWS: if separate message view, don't open a new window
2737          * but rather use the current separated message view */
2738         summary_display_msg_full(summaryview, summaryview->selected,
2739                                  prefs_common.sep_msg ? FALSE : TRUE, 
2740                                  FALSE);
2741 }
2742
2743 void summary_view_source(SummaryView * summaryview)
2744 {
2745         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2746         MsgInfo *msginfo;
2747         SourceWindow *srcwin;
2748
2749         if (!summaryview->selected) return;
2750
2751         srcwin = source_window_create();
2752         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2753         source_window_show_msg(srcwin, msginfo);
2754         source_window_show(srcwin);
2755 }
2756
2757 void summary_reedit(SummaryView *summaryview)
2758 {
2759         MsgInfo *msginfo;
2760
2761         if (!summaryview->selected) return;
2762         if (!summaryview->folder_item) return;
2763         if (!folder_has_parent_of_type(summaryview->folder_item, F_OUTBOX)
2764         &&  !folder_has_parent_of_type(summaryview->folder_item, F_DRAFT)
2765         &&  !folder_has_parent_of_type(summaryview->folder_item, F_QUEUE))
2766                 return;
2767
2768         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2769                                               summaryview->selected);
2770         if (!msginfo) return;
2771
2772         compose_reedit(msginfo);
2773 }
2774
2775 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2776 {
2777         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2778         GtkCTreeNode *node;
2779
2780         if (summary_is_locked(summaryview)) return FALSE;
2781         if (type == GTK_SCROLL_STEP_FORWARD) {
2782                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2783                 if (node)
2784                         gtkut_ctree_expand_parent_all(ctree, node);
2785                 else
2786                         return FALSE;
2787         } else {
2788                 if (summaryview->selected) {
2789                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2790                         if (!node) return FALSE;
2791                 }
2792         }
2793
2794         if (messageview_is_visible(summaryview->messageview))
2795                 summaryview->display_msg = TRUE;
2796
2797         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2798
2799         if (GTK_CLIST(ctree)->selection)
2800                 gtk_sctree_set_anchor_row
2801                         (GTK_SCTREE(ctree),
2802                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2803
2804         return TRUE;
2805 }
2806
2807 void summary_toggle_view(SummaryView *summaryview)
2808 {
2809         if (!messageview_is_visible(summaryview->messageview) &&
2810             summaryview->selected)
2811                 summary_display_msg(summaryview,
2812                                     summaryview->selected);
2813         else
2814                 main_window_toggle_message_view(summaryview->mainwin);
2815 }
2816
2817 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2818                                                 GtkCTreeNode *node)
2819 {
2820         MsgInfo *msginfo;
2821
2822         if (node) {
2823                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2824                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2825                         return TRUE;
2826                 node = GTK_CTREE_ROW(node)->children;
2827         } else
2828                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2829
2830         while (node) {
2831                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2832                         return TRUE;
2833                 node = GTK_CTREE_ROW(node)->sibling;
2834         }
2835
2836         return FALSE;
2837 }
2838
2839 static gboolean summary_have_unread_children(SummaryView *summaryview,
2840                                              GtkCTreeNode *node)
2841 {
2842         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2843
2844         if (!node) return FALSE;
2845
2846         node = GTK_CTREE_ROW(node)->children;
2847
2848         while (node) {
2849                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2850                         return TRUE;
2851                 node = GTK_CTREE_ROW(node)->sibling;
2852         }
2853         return FALSE;
2854 }
2855
2856 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2857 {
2858         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2859         GtkStyle *style = NULL;
2860         MsgInfo *msginfo;
2861         MsgFlags flags;
2862         gint *col_pos = summaryview->col_pos;
2863
2864         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2865         if (!msginfo) return;
2866
2867         flags = msginfo->flags;
2868
2869         gtk_ctree_node_set_foreground(ctree, row, NULL);
2870
2871         /* set new/unread column */
2872         if (MSG_IS_IGNORE_THREAD(flags)) {
2873                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2874                                           ignorethreadxpm, ignorethreadxpmmask);
2875         } else if (MSG_IS_NEW(flags)) {
2876                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2877                                           newxpm, newxpmmask);
2878         } else if (MSG_IS_UNREAD(flags)) {
2879                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2880                                           unreadxpm, unreadxpmmask);
2881         } else if (MSG_IS_REPLIED(flags)) {
2882                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2883                                           repliedxpm, repliedxpmmask);
2884         } else if (MSG_IS_FORWARDED(flags)) {
2885                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2886                                           forwardedxpm, forwardedxpmmask);
2887         } else {
2888                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2889                                         "");
2890         }
2891
2892         if (prefs_common.bold_unread &&
2893             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2894              (!GTK_CTREE_ROW(row)->expanded &&
2895               GTK_CTREE_ROW(row)->children &&
2896               summary_have_unread_children(summaryview, row))))
2897                 style = bold_style;
2898
2899         /* set mark column */
2900         if (MSG_IS_DELETED(flags)) {
2901                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2902                                           deletedxpm, deletedxpmmask);
2903                 if (style)
2904                         style = bold_deleted_style;
2905                 else {
2906                         style = small_deleted_style;
2907                 }
2908                         gtk_ctree_node_set_foreground
2909                                 (ctree, row, &summaryview->color_dim);
2910         } else if (MSG_IS_MARKED(flags)) {
2911                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2912                                           markxpm, markxpmmask);
2913         } else if (MSG_IS_MOVE(flags)) {
2914                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2915                 if (style)
2916                         style = bold_marked_style;
2917                 else {
2918                         style = small_marked_style;
2919                 }
2920                         gtk_ctree_node_set_foreground
2921                                 (ctree, row, &summaryview->color_marked);
2922         } else if (MSG_IS_COPY(flags)) {
2923                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2924                 if (style)
2925                         style = bold_marked_style;
2926                 else {
2927                         style = small_marked_style;
2928                 }
2929                         gtk_ctree_node_set_foreground
2930                                 (ctree, row, &summaryview->color_marked);
2931 #if 0
2932         } else if ((global_scoring ||
2933                   summaryview->folder_item->prefs->scoring) &&
2934                  (msginfo->score >= summaryview->important_score) &&
2935                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2936                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2937                 gtk_ctree_node_set_foreground(ctree, row,
2938                                               &summaryview->color_important);
2939 #endif
2940         } else {
2941                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "");
2942         }
2943
2944         if (MSG_IS_LOCKED(flags)) {
2945                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2946                                           lockedxpm, lockedxpmmask);
2947         }
2948         else {
2949                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], "");
2950         }
2951
2952         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2953                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2954                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2955         } else if (MSG_IS_SIGNED(flags)) {
2956                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2957                                           gpgsignedxpm, gpgsignedxpmmask);
2958         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2959                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2960                                           clipkeyxpm, clipkeyxpmmask);
2961         } else if (MSG_IS_ENCRYPTED(flags)) {
2962                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2963                                           keyxpm, keyxpmmask);
2964         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2965                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2966                                           clipxpm, clipxpmmask);
2967         } else {
2968                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], "");
2969         }
2970         if (!style)
2971                 style = small_style;
2972
2973         gtk_ctree_node_set_row_style(ctree, row, style);
2974
2975         if (MSG_GET_COLORLABEL(flags))
2976                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2977 }
2978
2979 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2980 {
2981         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2982         MsgInfo *msginfo;
2983
2984         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2985         if (MSG_IS_DELETED(msginfo->flags))
2986                 summaryview->deleted--;
2987         if (MSG_IS_MOVE(msginfo->flags))
2988                 summaryview->moved--;
2989         if (MSG_IS_COPY(msginfo->flags))
2990                 summaryview->copied--;
2991
2992         procmsg_msginfo_set_to_folder(msginfo, NULL);
2993         summary_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2994         summary_set_row_marks(summaryview, row);
2995         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2996 }
2997
2998 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2999 {
3000         gboolean changed = FALSE;
3001         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3002         MsgInfo *msginfo;
3003
3004         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3005         if (MSG_IS_DELETED(msginfo->flags))
3006                 summaryview->deleted--;
3007         if (MSG_IS_MOVE(msginfo->flags)) {
3008                 summaryview->moved--;
3009                 changed = TRUE;
3010         }
3011         if (MSG_IS_COPY(msginfo->flags)) {
3012                 summaryview->copied--;
3013                 changed = TRUE;
3014         }
3015         procmsg_msginfo_set_to_folder(msginfo, NULL);
3016         summary_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
3017         
3018         summary_set_row_marks(summaryview, row);
3019         debug_print("Message %d is locked\n", msginfo->msgnum);
3020 }
3021
3022 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
3023 {
3024         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3025         MsgInfo *msginfo;
3026
3027         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3028         if (!MSG_IS_LOCKED(msginfo->flags))
3029                 return;
3030         procmsg_msginfo_set_to_folder(msginfo, NULL);
3031         summary_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
3032         summary_set_row_marks(summaryview, row);
3033         debug_print("Message %d is unlocked\n", msginfo->msgnum);
3034 }
3035
3036 void summary_mark(SummaryView *summaryview)
3037 {
3038         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3039         GList *cur;
3040
3041         START_LONG_OPERATION(summaryview);
3042         folder_item_set_batch(summaryview->folder_item, TRUE);
3043         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3044                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
3045         folder_item_set_batch(summaryview->folder_item, FALSE);
3046         END_LONG_OPERATION(summaryview);
3047
3048         summary_status_show(summaryview);
3049 }
3050
3051 static void summary_mark_row_as_read(SummaryView *summaryview,
3052                                      GtkCTreeNode *row)
3053 {
3054         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3055         MsgInfo *msginfo;
3056
3057         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3058
3059         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
3060                 return;
3061
3062         summary_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
3063         summary_set_row_marks(summaryview, row);
3064         debug_print("Message %d is marked as read\n",
3065                 msginfo->msgnum);
3066 }
3067
3068 void summary_mark_as_read(SummaryView *summaryview)
3069 {
3070         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3071         GList *cur;
3072
3073         START_LONG_OPERATION(summaryview);
3074         folder_item_set_batch(summaryview->folder_item, TRUE);
3075         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3076                 summary_mark_row_as_read(summaryview,
3077                                          GTK_CTREE_NODE(cur->data));
3078         folder_item_set_batch(summaryview->folder_item, FALSE);
3079         END_LONG_OPERATION(summaryview);
3080         
3081         summary_status_show(summaryview);
3082 }
3083
3084 void summary_msgs_lock(SummaryView *summaryview)
3085 {
3086         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3087         GList *cur;
3088
3089         START_LONG_OPERATION(summaryview);
3090         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3091                 summary_lock_row(summaryview,
3092                                          GTK_CTREE_NODE(cur->data));
3093         END_LONG_OPERATION(summaryview);
3094         
3095         summary_status_show(summaryview);
3096 }
3097
3098 void summary_msgs_unlock(SummaryView *summaryview)
3099 {
3100         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3101         GList *cur;
3102
3103         START_LONG_OPERATION(summaryview);
3104         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3105                 summary_unlock_row(summaryview,
3106                                    GTK_CTREE_NODE(cur->data));
3107         END_LONG_OPERATION(summaryview);
3108         
3109         summary_status_show(summaryview);
3110 }
3111
3112 void summary_mark_all_read(SummaryView *summaryview)
3113 {
3114         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3115         GtkCTreeNode *node;
3116
3117         START_LONG_OPERATION(summaryview);
3118         folder_item_set_batch(summaryview->folder_item, TRUE);
3119         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3120              node = gtkut_ctree_node_next(ctree, node))
3121                 summary_mark_row_as_read(summaryview, node);
3122         folder_item_set_batch(summaryview->folder_item, FALSE);
3123         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
3124              node = gtkut_ctree_node_next(ctree, node)) {
3125                 if (!GTK_CTREE_ROW(node)->expanded)
3126                         summary_set_row_marks(summaryview, node);
3127         }
3128         END_LONG_OPERATION(summaryview);
3129         
3130         summary_status_show(summaryview);
3131 }
3132
3133 static void summary_mark_row_as_unread(SummaryView *summaryview,
3134                                        GtkCTreeNode *row)
3135 {
3136         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3137         MsgInfo *msginfo;
3138
3139         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3140         if (MSG_IS_DELETED(msginfo->flags)) {
3141                 procmsg_msginfo_set_to_folder(msginfo, NULL);
3142                 summary_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
3143                 summaryview->deleted--;
3144         }
3145
3146         summary_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
3147         debug_print("Message %d is marked as unread\n",
3148                 msginfo->msgnum);
3149
3150         summary_set_row_marks(summaryview, row);
3151 }
3152
3153 void summary_mark_as_unread(SummaryView *summaryview)
3154 {
3155         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3156         GList *cur;
3157
3158         START_LONG_OPERATION(summaryview);
3159         folder_item_set_batch(summaryview->folder_item, TRUE);
3160         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3161                 cur = cur->next)
3162                 summary_mark_row_as_unread(summaryview,
3163                                            GTK_CTREE_NODE(cur->data));
3164         folder_item_set_batch(summaryview->folder_item, FALSE);
3165         END_LONG_OPERATION(summaryview);
3166         
3167         summary_status_show(summaryview);
3168 }
3169
3170 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
3171 {
3172         GList * cur;
3173         gboolean found;
3174
3175         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
3176
3177         case F_NEWS:
3178
3179                 /*
3180                   security : checks if one the accounts correspond to
3181                   the author of the post
3182                 */
3183
3184                 found = FALSE;
3185                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3186                         PrefsAccount * account;
3187                         gchar * from_name;
3188                         
3189                         account = cur->data;
3190                         if (account->name && *account->name)
3191                                 from_name =
3192                                         g_strdup_printf("%s <%s>",
3193                                                         account->name,
3194                                                         account->address);
3195                         else
3196                                 from_name =
3197                                         g_strdup_printf("%s",
3198                                                         account->address);
3199                         
3200                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3201                                 g_free(from_name);
3202                                 found = TRUE;
3203                                 break;
3204                         }
3205                         g_free(from_name);
3206                 }
3207
3208                 if (!found) {
3209                         alertpanel_error(_("You're not the author of the article.\n"));
3210                 }
3211                 
3212                 return found;
3213
3214         default:
3215                 return TRUE;
3216         }
3217 }
3218
3219 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3220 {
3221         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3222         MsgInfo *msginfo;
3223
3224         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3225
3226         if (MSG_IS_LOCKED(msginfo->flags)) return;
3227
3228         if (MSG_IS_DELETED(msginfo->flags)) return;
3229
3230         if (MSG_IS_MOVE(msginfo->flags))
3231                 summaryview->moved--;
3232         if (MSG_IS_COPY(msginfo->flags))
3233                 summaryview->copied--;
3234
3235         procmsg_msginfo_set_to_folder(msginfo, NULL);
3236         summary_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, MSG_MOVE | MSG_COPY);
3237         summaryview->deleted++;
3238
3239         if (!prefs_common.immediate_exec && 
3240             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3241                 summary_set_row_marks(summaryview, row);
3242
3243         debug_print(&quo