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