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