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