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