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