fcef86b082048effa0ea6da8e9d20ab481dd2142
[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                 /* select first unread message */
1018                 node = summary_find_next_flagged_msg(summaryview, NULL,
1019                                                      MSG_UNREAD, FALSE);
1020                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1021                         node = gtk_ctree_node_nth
1022                                 (ctree,
1023                                  item->sort_type == SORT_DESCENDING
1024                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1025                 }
1026                 if (prefs_common.open_unread_on_enter) {
1027                         summary_unlock(summaryview);
1028                         summary_select_node(summaryview, node, 
1029                                             messageview_is_visible(summaryview->messageview), 
1030                                             TRUE);
1031                         summary_lock(summaryview);
1032                 } else {
1033                         summary_unlock(summaryview);
1034                         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
1035                         summary_lock(summaryview);
1036                 }
1037         }
1038
1039         summary_set_column_titles(summaryview);
1040         summary_status_show(summaryview);
1041         summary_set_menu_sensitive(summaryview);
1042         toolbar_main_set_sensitive(summaryview->mainwin);
1043
1044         debug_print("\n");
1045         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1046         STATUSBAR_POP(summaryview->mainwin);
1047         main_window_cursor_normal(summaryview->mainwin);
1048         summary_unlock(summaryview);
1049         inc_unlock();
1050
1051         return TRUE;
1052 }
1053
1054 #undef CURRENTLY_DISPLAYED
1055
1056
1057 void summary_clear_list(SummaryView *summaryview)
1058 {
1059         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1060         gint optimal_width;
1061
1062         gtk_clist_freeze(clist);
1063
1064         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1065                                 NULL, summary_free_msginfo_func, NULL);
1066
1067         if (summaryview->folder_item) {
1068                 summaryview->folder_item->opened = FALSE;
1069                 summaryview->folder_item = NULL;
1070         }
1071
1072         summaryview->display_msg = FALSE;
1073
1074         summaryview->selected = NULL;
1075         summaryview->displayed = NULL;
1076         summaryview->total_size = 0;
1077         summaryview->deleted = summaryview->moved = 0;
1078         summaryview->copied = 0;
1079         if (summaryview->msgid_table) {
1080                 g_hash_table_destroy(summaryview->msgid_table);
1081                 summaryview->msgid_table = NULL;
1082         }
1083         if (summaryview->subject_table) {
1084                 g_hash_table_destroy(summaryview->subject_table);
1085                 summaryview->subject_table = NULL;
1086         }
1087         summaryview->mlist = NULL;
1088
1089         gtk_clist_clear(clist);
1090         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1091                 optimal_width = gtk_clist_optimal_column_width
1092                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1093                 gtk_clist_set_column_width
1094                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1095                          optimal_width);
1096         }
1097
1098         gtk_clist_thaw(clist);
1099 }
1100
1101 void summary_clear_all(SummaryView *summaryview)
1102 {
1103         messageview_clear(summaryview->messageview);
1104         summary_clear_list(summaryview);
1105         summary_set_menu_sensitive(summaryview);
1106         toolbar_main_set_sensitive(summaryview->mainwin);
1107         summary_status_show(summaryview);
1108 }
1109
1110 void summary_lock(SummaryView *summaryview)
1111 {
1112         summaryview->lock_count++;
1113 }
1114
1115 void summary_unlock(SummaryView *summaryview)
1116 {
1117         if (summaryview->lock_count)
1118                 summaryview->lock_count--;
1119 }
1120
1121 gboolean summary_is_locked(SummaryView *summaryview)
1122 {
1123         return summaryview->lock_count > 0;
1124 }
1125
1126 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1127 {
1128         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1129         SummarySelection selection;
1130
1131         if (!clist->row_list)
1132                 selection = SUMMARY_NONE;
1133         else if (!clist->selection)
1134                 selection = SUMMARY_SELECTED_NONE;
1135         else if (!clist->selection->next)
1136                 selection = SUMMARY_SELECTED_SINGLE;
1137         else
1138                 selection = SUMMARY_SELECTED_MULTIPLE;
1139
1140         return selection;
1141 }
1142
1143 /*!
1144  *\return       MsgInfo * Selected message if there's one selected;
1145  *              if multiple selected, or none, return NULL.
1146  */ 
1147 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1148 {
1149         /* summaryview->selected may be valid when multiple 
1150          * messages were selected */
1151         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1152
1153         if (sellist == NULL || sellist->next) 
1154                 return NULL;
1155         
1156         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1157 }
1158
1159 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1160 {
1161         GSList *mlist = NULL;
1162         GList *cur;
1163         MsgInfo *msginfo;
1164
1165         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1166              cur = cur->next) {
1167                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1168                 mlist = g_slist_prepend(mlist, msginfo);
1169         }
1170
1171         mlist = g_slist_reverse(mlist);
1172
1173         return mlist;
1174 }
1175
1176 GSList *summary_get_msg_list(SummaryView *summaryview)
1177 {
1178         GSList *mlist = NULL;
1179         GtkCTree *ctree;
1180         GtkCTreeNode *node;
1181         MsgInfo *msginfo;
1182
1183         ctree = GTK_CTREE(summaryview->ctree);
1184
1185         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1186              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1187                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1188                 mlist = g_slist_prepend(mlist, msginfo);
1189         }
1190
1191         mlist = g_slist_reverse(mlist);
1192
1193         return mlist;
1194 }
1195
1196 static void summary_set_menu_sensitive(SummaryView *summaryview)
1197 {
1198         GtkItemFactory *ifactory = summaryview->popupfactory;
1199         SensitiveCond state;
1200         gboolean sensitive;
1201         GtkWidget *menuitem;
1202         gint i;
1203
1204         static const struct {
1205                 gchar *const entry;
1206                 SensitiveCond cond;
1207         } entry[] = {
1208                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1209                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1210                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1211                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1212                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1213                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1214
1215                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1216                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1217
1218                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1219
1220                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1221                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1222                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1223                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1224                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1225
1226                 {"/Mark"                        , M_TARGET_EXIST},
1227                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1228                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1229                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1230                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1231                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1232                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1233                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1234                 {"/Color label"                 , M_TARGET_EXIST},
1235
1236                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1237                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1238                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1239
1240                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1241                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1242                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1243                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1244 #if 0
1245                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1246                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1247
1248                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1249                 {"/Select all"                  , M_TARGET_EXIST},
1250 #endif
1251                 {NULL, 0}
1252         };
1253
1254         main_window_set_menu_sensitive(summaryview->mainwin);
1255
1256         state = main_window_get_current_state(summaryview->mainwin);
1257
1258         for (i = 0; entry[i].entry != NULL; i++) {
1259                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1260                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1261         }
1262
1263
1264         summary_lock(summaryview);
1265         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1266         gtk_check_menu_item_set_active
1267                 (GTK_CHECK_MENU_ITEM(menuitem),
1268                  summaryview->messageview->mimeview->textview->show_all_headers);
1269         summary_unlock(summaryview);
1270 }
1271
1272 void summary_select_prev_unread(SummaryView *summaryview)
1273 {
1274         GtkCTreeNode *node;
1275         gboolean skip_cur = FALSE;
1276
1277         if (summaryview->displayed 
1278         &&  summaryview->selected == summaryview->displayed) {
1279                 debug_print("skipping current\n");
1280                 skip_cur = TRUE;
1281         }
1282
1283         node = summary_find_prev_flagged_msg
1284                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1285
1286         if (!node) {
1287                 AlertValue val = 0;
1288
1289                 switch (prefs_common.next_unread_msg_dialog) {
1290                         case NEXTUNREADMSGDIALOG_ALWAYS:
1291                                 val = alertpanel(_("No more unread messages"),
1292                                                  _("No unread message found. "
1293                                                    "Search from the end?"),
1294                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1295                                 break;
1296                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1297                                 val = G_ALERTDEFAULT;
1298                                 break;
1299                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1300                                 val = !G_ALERTDEFAULT;
1301                                 break;
1302                         default:
1303                                 debug_print(
1304                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1305                 }
1306                 if (val != G_ALERTDEFAULT) return;
1307                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1308                                                      MSG_UNREAD, FALSE);
1309         }
1310
1311         if (!node)
1312                 alertpanel_notice(_("No unread messages."));
1313         else
1314                 summary_select_node(summaryview, node, TRUE, FALSE);
1315 }
1316
1317 void summary_select_next_unread(SummaryView *summaryview)
1318 {
1319         GtkCTreeNode *node = summaryview->selected;
1320         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1321         gboolean skip_cur = FALSE;
1322         
1323         if (summaryview->displayed 
1324         &&  summaryview->selected == summaryview->displayed) {
1325                 debug_print("skipping cur (%p %p)\n",
1326                         summaryview->displayed, summaryview->selected);
1327                 skip_cur = TRUE;
1328         }
1329
1330
1331         node = summary_find_next_flagged_msg
1332                 (summaryview, node, MSG_UNREAD, skip_cur);
1333         
1334         if (node)
1335                 summary_select_node(summaryview, node, TRUE, FALSE);
1336         else {
1337                 node = summary_find_next_flagged_msg
1338                         (summaryview, NULL, MSG_UNREAD, FALSE);
1339                 if (node == NULL) {
1340                         AlertValue val = 0;
1341
1342                         switch (prefs_common.next_unread_msg_dialog) {
1343                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1344                                         val = alertpanel(_("No more unread messages"),
1345                                                          _("No unread message found. "
1346                                                            "Go to next folder?"),
1347                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1348                                         break;
1349                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1350                                         val = G_ALERTDEFAULT;
1351                                         break;
1352                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1353                                         val = G_ALERTOTHER;
1354                                         break;
1355                                 default:
1356                                         debug_print(
1357                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1358                         }
1359
1360                         if (val == G_ALERTDEFAULT) {
1361                                 g_signal_stop_emission_by_name(G_OBJECT(ctree),
1362                                                          "key_press_event");
1363                                 folderview_select_next_unread(summaryview->folderview);
1364                                 return;
1365                         } 
1366                         else
1367                                 return;
1368                 } else
1369                         summary_select_node(summaryview, node, TRUE, FALSE);
1370
1371         }
1372 }
1373
1374 void summary_select_prev_new(SummaryView *summaryview)
1375 {
1376         GtkCTreeNode *node;
1377
1378         node = summary_find_prev_flagged_msg
1379                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1380
1381         if (!node) {
1382                 AlertValue val;
1383
1384                 val = alertpanel(_("No more new messages"),
1385                                  _("No new message found. "
1386                                    "Search from the end?"),
1387                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1388                 if (val != G_ALERTDEFAULT) return;
1389                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1390                                                      MSG_NEW, FALSE);
1391         }
1392
1393         if (!node)
1394                 alertpanel_notice(_("No new messages."));
1395         else
1396                 summary_select_node(summaryview, node, TRUE, FALSE);
1397 }
1398
1399 void summary_select_next_new(SummaryView *summaryview)
1400 {
1401         GtkCTreeNode *node = summaryview->selected;
1402         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1403
1404         while ((node = summary_find_next_flagged_msg
1405                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1406                 AlertValue val;
1407
1408                 val = alertpanel(_("No more new messages"),
1409                                  _("No new message found. "
1410                                    "Go to next folder?"),
1411                                  GTK_STOCK_YES, _("_Search again"),
1412                                  GTK_STOCK_NO);
1413                 if (val == G_ALERTDEFAULT) {
1414                         g_signal_stop_emission_by_name(G_OBJECT(ctree),"key_press_event");
1415                         folderview_select_next_unread(summaryview->folderview);
1416                         return;
1417                 } else if (val == G_ALERTALTERNATE)
1418                         node = NULL;
1419                 else
1420                         return;
1421         }
1422
1423         if (node)
1424                 summary_select_node(summaryview, node, TRUE, FALSE);
1425 }
1426
1427 void summary_select_prev_marked(SummaryView *summaryview)
1428 {
1429         GtkCTreeNode *node;
1430
1431         node = summary_find_prev_flagged_msg
1432                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1433
1434         if (!node) {
1435                 AlertValue val;
1436
1437                 val = alertpanel(_("No more marked messages"),
1438                                  _("No marked message found. "
1439                                    "Search from the end?"),
1440                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1441                 if (val != G_ALERTDEFAULT) return;
1442                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1443                                                      MSG_MARKED, TRUE);
1444         }
1445
1446         if (!node)
1447                 alertpanel_notice(_("No marked messages."));
1448         else
1449                 summary_select_node(summaryview, node, TRUE, FALSE);
1450 }
1451
1452 void summary_select_next_marked(SummaryView *summaryview)
1453 {
1454         GtkCTreeNode *node;
1455
1456         node = summary_find_next_flagged_msg
1457                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1458
1459         if (!node) {
1460                 AlertValue val;
1461
1462                 val = alertpanel(_("No more marked messages"),
1463                                  _("No marked message found. "
1464                                    "Search from the beginning?"),
1465                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1466                 if (val != G_ALERTDEFAULT) return;
1467                 node = summary_find_next_flagged_msg(summaryview, NULL,
1468                                                      MSG_MARKED, TRUE);
1469         }
1470
1471         if (!node)
1472                 alertpanel_notice(_("No marked messages."));
1473         else
1474                 summary_select_node(summaryview, node, TRUE, FALSE);
1475 }
1476
1477 void summary_select_prev_labeled(SummaryView *summaryview)
1478 {
1479         GtkCTreeNode *node;
1480
1481         node = summary_find_prev_flagged_msg
1482                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1483
1484         if (!node) {
1485                 AlertValue val;
1486
1487                 val = alertpanel(_("No more labeled messages"),
1488                                  _("No labeled message found. "
1489                                    "Search from the end?"),
1490                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1491                 if (val != G_ALERTDEFAULT) return;
1492                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1493                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1494         }
1495
1496         if (!node)
1497                 alertpanel_notice(_("No labeled messages."));
1498         else
1499                 summary_select_node(summaryview, node, TRUE, FALSE);
1500 }
1501
1502 void summary_select_next_labeled(SummaryView *summaryview)
1503 {
1504         GtkCTreeNode *node;
1505
1506         node = summary_find_next_flagged_msg
1507                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1508
1509         if (!node) {
1510                 AlertValue val;
1511
1512                 val = alertpanel(_("No more labeled messages"),
1513                                  _("No labeled message found. "
1514                                    "Search from the beginning?"),
1515                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1516                 if (val != G_ALERTDEFAULT) return;
1517                 node = summary_find_next_flagged_msg(summaryview, NULL,
1518                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1519         }
1520
1521         if (!node)
1522                 alertpanel_notice(_("No labeled messages."));
1523         else
1524                 summary_select_node(summaryview, node, TRUE, FALSE);
1525 }
1526
1527 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1528 {
1529         GtkCTreeNode *node;
1530
1531         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1532         summary_select_node(summaryview, node, FALSE, TRUE);
1533 }
1534
1535 /**
1536  * summary_select_node:
1537  * @summaryview: Summary view.
1538  * @node: Summary tree node.
1539  * @display_msg: TRUE to display the selected message.
1540  * @do_refresh: TRUE to refresh the widget.
1541  *
1542  * Select @node (bringing it into view by scrolling and expanding its
1543  * thread, if necessary) and unselect all others.  If @display_msg is
1544  * TRUE, display the corresponding message in the message view.
1545  * If @do_refresh is TRUE, the widget is refreshed.
1546  **/
1547 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1548                          gboolean display_msg, gboolean do_refresh)
1549 {
1550         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1551
1552         if (node) {
1553                 gtkut_ctree_expand_parent_all(ctree, node);
1554                 if (do_refresh) {
1555                         GTK_EVENTS_FLUSH();
1556                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1557                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1558                 }
1559                 summary_unselect_all(summaryview);
1560                 if (display_msg && summaryview->displayed == node)
1561                         summaryview->displayed = NULL;
1562                 summaryview->display_msg = display_msg;
1563                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1564                 if (summaryview->selected == NULL)
1565                         summaryview->selected = node;
1566         }
1567 }
1568
1569 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1570 {
1571         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1572         MsgInfo *msginfo;
1573
1574         if (!node)
1575                 return 0;
1576         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1577         return msginfo->msgnum;
1578 }
1579
1580 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1581                                            GtkCTreeNode *current_node)
1582 {
1583         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1584         GtkCTreeNode *node;
1585         MsgInfo *msginfo;
1586
1587         if (current_node)
1588                 node = current_node;
1589         else
1590                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1591
1592         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1593                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1594                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1595         }
1596
1597         return node;
1598 }
1599
1600 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1601                                            GtkCTreeNode *current_node)
1602 {
1603         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1604         GtkCTreeNode *node;
1605         MsgInfo *msginfo;
1606
1607         if (current_node)
1608                 node = current_node;
1609         else
1610                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1611
1612         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1613                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1614                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1615         }
1616
1617         return node;
1618 }
1619
1620 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1621                                                    GtkCTreeNode *current_node,
1622                                                    MsgPermFlags flags,
1623                                                    gboolean start_from_prev)
1624 {
1625         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1626         GtkCTreeNode *node;
1627         MsgInfo *msginfo;
1628
1629         if (current_node) {
1630                 if (start_from_prev)
1631                         node = GTK_CTREE_NODE_PREV(current_node);
1632                 else
1633                         node = current_node;
1634         } else
1635                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1636
1637         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1638                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1639                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1640         }
1641
1642         return node;
1643 }
1644
1645 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1646                                                    GtkCTreeNode *current_node,
1647                                                    MsgPermFlags flags,
1648                                                    gboolean start_from_next)
1649 {
1650         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1651         GtkCTreeNode *node;
1652         MsgInfo *msginfo;
1653
1654         if (current_node) {
1655                 if (start_from_next)
1656                         node = gtkut_ctree_node_next(ctree, current_node);
1657                 else
1658                         node = current_node;
1659         } else
1660                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1661
1662         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1663                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1664                 /* Find msg with matching flags but ignore messages with
1665                    ignore flags, if searching for new or unread messages */
1666                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1667                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1668                         break;
1669         }
1670
1671         return node;
1672 }
1673
1674 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1675                                                 guint msgnum)
1676 {
1677         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1678         GtkCTreeNode *node;
1679         MsgInfo *msginfo;
1680
1681         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1682
1683         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1684                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1685                 if (msginfo && msginfo->msgnum == msgnum) break;
1686         }
1687
1688         return node;
1689 }
1690
1691 static guint attract_hash_func(gconstpointer key)
1692 {
1693         gchar *str;
1694         gchar *p;
1695         guint h;
1696
1697         Xstrdup_a(str, (const gchar *)key, return 0);
1698         trim_subject(str);
1699
1700         p = str;
1701         h = *p;
1702
1703         if (h) {
1704                 for (p += 1; *p != '\0'; p++)
1705                         h = (h << 5) - h + *p;
1706         }
1707
1708         return h;
1709 }
1710
1711 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1712 {
1713         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1714 }
1715
1716 void summary_attract_by_subject(SummaryView *summaryview)
1717 {
1718         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1719         GtkCList *clist = GTK_CLIST(ctree);
1720         GtkCTreeNode *src_node;
1721         GtkCTreeNode *dst_node, *sibling;
1722         GtkCTreeNode *tmp;
1723         MsgInfo *src_msginfo, *dst_msginfo;
1724         GHashTable *subject_table;
1725
1726         debug_print("Attracting messages by subject...");
1727         STATUSBAR_PUSH(summaryview->mainwin,
1728                        _("Attracting messages by subject..."));
1729
1730         main_window_cursor_wait(summaryview->mainwin);
1731         gtk_clist_freeze(clist);
1732
1733         subject_table = g_hash_table_new(attract_hash_func,
1734                                          attract_compare_func);
1735
1736         for (src_node = GTK_CTREE_NODE(clist->row_list);
1737              src_node != NULL;
1738              src_node = tmp) {
1739                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1740                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1741                 if (!src_msginfo) continue;
1742                 if (!src_msginfo->subject) continue;
1743
1744                 /* find attracting node */
1745                 dst_node = g_hash_table_lookup(subject_table,
1746                                                src_msginfo->subject);
1747
1748                 if (dst_node) {
1749                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1750
1751                         /* if the time difference is more than 20 days,
1752                            don't attract */
1753                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1754                             > 60 * 60 * 24 * 20)
1755                                 continue;
1756
1757                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1758                         if (src_node != sibling)
1759                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1760                 }
1761
1762                 g_hash_table_insert(subject_table,
1763                                     src_msginfo->subject, src_node);
1764         }
1765
1766         g_hash_table_destroy(subject_table);
1767
1768         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1769
1770         gtk_clist_thaw(clist);
1771
1772         debug_print("done.\n");
1773         STATUSBAR_POP(summaryview->mainwin);
1774
1775         main_window_cursor_normal(summaryview->mainwin);
1776 }
1777
1778 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1779                                       gpointer data)
1780 {
1781         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1782
1783         if (msginfo)
1784                 procmsg_msginfo_free(msginfo);
1785 }
1786
1787 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1788                                    gpointer data)
1789 {
1790         SummaryView *summaryview = data;
1791         MsgInfo *msginfo;
1792
1793         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1794
1795         if (msginfo->folder && msginfo->folder->folder &&
1796             msginfo->folder->folder->klass->type == F_NEWS)
1797                 news_flag_crosspost(msginfo);
1798
1799         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1800         && procmsg_msg_has_marked_parent(msginfo))
1801                 summaryview->unreadmarked++;
1802
1803         if (MSG_IS_DELETED(msginfo->flags))
1804                 summaryview->deleted++;
1805
1806         summaryview->total_size += msginfo->size;
1807
1808         summary_set_row_marks(summaryview, node);
1809 }
1810
1811 static void summary_update_status(SummaryView *summaryview)
1812 {
1813         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1814         GtkCTreeNode *node;
1815         MsgInfo *msginfo;
1816
1817         summaryview->total_size =
1818         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1819
1820         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1821              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1822                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1823
1824                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1825                 && procmsg_msg_has_marked_parent(msginfo))
1826                         summaryview->unreadmarked++;
1827                 if (MSG_IS_DELETED(msginfo->flags))
1828                         summaryview->deleted++;
1829                 if (MSG_IS_MOVE(msginfo->flags))
1830                         summaryview->moved++;
1831                 if (MSG_IS_COPY(msginfo->flags))
1832                         summaryview->copied++;
1833                 summaryview->total_size += msginfo->size;
1834         }
1835 }
1836
1837 static void summary_status_show(SummaryView *summaryview)
1838 {
1839         gchar *str;
1840         gchar *del, *mv, *cp;
1841         gchar *sel;
1842         gchar *spc;
1843         gchar *itstr;
1844         GList *rowlist, *cur;
1845         guint n_selected = 0;
1846         off_t sel_size = 0;
1847         MsgInfo *msginfo;
1848         gchar *name;
1849         
1850         if (!summaryview->folder_item) {
1851                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1852                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1853                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1854                 return;
1855         }
1856
1857         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1858         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1859                 msginfo = gtk_ctree_node_get_row_data
1860                         (GTK_CTREE(summaryview->ctree),
1861                          GTK_CTREE_NODE(cur->data));
1862                 if (!msginfo)
1863                         g_warning("summary_status_show(): msginfo == NULL\n");
1864                 else {
1865                         sel_size += msginfo->size;
1866                         n_selected++;
1867                 }
1868         }
1869
1870         name = folder_item_get_name(summaryview->folder_item);
1871         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1872         g_free(name);
1873
1874         if (summaryview->deleted)
1875                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1876         else
1877                 del = g_strdup("");
1878         if (summaryview->moved)
1879                 mv = g_strdup_printf(_("%s%d moved"),
1880                                      summaryview->deleted ? _(", ") : "",
1881                                      summaryview->moved);
1882         else
1883                 mv = g_strdup("");
1884         if (summaryview->copied)
1885                 cp = g_strdup_printf(_("%s%d copied"),
1886                                      summaryview->deleted ||
1887                                      summaryview->moved ? _(", ") : "",
1888                                      summaryview->copied);
1889         else
1890                 cp = g_strdup("");
1891
1892         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1893                 spc = "    ";
1894         else
1895                 spc = "";
1896
1897         if (n_selected) {
1898                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1899                 if (n_selected == 1)
1900                         itstr = g_strdup(_(" item selected"));
1901                 else
1902                         itstr = g_strdup(_(" items selected"));
1903         } else {
1904                 sel = g_strdup("");
1905                 itstr = g_strdup("");
1906         }
1907                 
1908         str = g_strconcat(n_selected ? itos(n_selected) : "",
1909                                         itstr, sel, spc, del, mv, cp, NULL);
1910         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1911         g_free(str);
1912         g_free(sel);
1913         g_free(del);
1914         g_free(mv);
1915         g_free(cp);
1916         g_free(itstr);
1917
1918         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1919
1920                                       summaryview->folder_item->new_msgs,
1921                                       summaryview->folder_item->unread_msgs,
1922                                       summaryview->folder_item->total_msgs,
1923                                       to_human_readable(summaryview->total_size));
1924         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1925         g_free(str);
1926 }
1927
1928 static void summary_set_column_titles(SummaryView *summaryview)
1929 {
1930         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1931         GtkWidget *hbox;
1932         GtkWidget *label;
1933         GtkWidget *arrow;
1934         gint pos;
1935         const gchar *title;
1936         SummaryColumnType type;
1937         gboolean single_char;
1938         GtkJustification justify;
1939
1940         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1941                 SORT_BY_MARK,
1942                 SORT_BY_STATUS,
1943                 SORT_BY_MIME,
1944                 SORT_BY_SUBJECT,
1945                 SORT_BY_FROM,
1946                 SORT_BY_DATE,
1947                 SORT_BY_SIZE,
1948                 SORT_BY_NUMBER,
1949                 SORT_BY_SCORE,
1950                 SORT_BY_LOCKED
1951         };
1952
1953         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1954                 type = summaryview->col_state[pos].type;
1955
1956                 /* CLAWS: mime and unread are single char headers */
1957                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1958                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1959                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1960
1961                 switch (type) {
1962                 case S_COL_SUBJECT:
1963                 case S_COL_FROM:
1964                 case S_COL_DATE:
1965                 case S_COL_NUMBER:
1966                         if (prefs_common.trans_hdr)
1967                                 title = gettext(col_label[type]);
1968                         else
1969                                 title = col_label[type];
1970                         break;
1971                 /* CLAWS: dummies for mark and locked headers */        
1972                 case S_COL_MARK:        
1973                 case S_COL_LOCKED:
1974                         title = "";
1975                         break;
1976                 default:
1977                         title = gettext(col_label[type]);
1978                 }
1979
1980                 if (type == S_COL_MIME) {
1981                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
1982                         gtk_widget_show(label);
1983                         gtk_clist_set_column_widget(clist, pos, label);
1984                         continue;
1985                 }
1986                 if (single_char) {
1987                         gtk_clist_set_column_title(clist, pos, title);
1988                         continue;
1989                 }
1990
1991                 /* CLAWS: changed so that locked and mark headers
1992                  * show a pixmap instead of single character */
1993                 hbox  = gtk_hbox_new(FALSE, 4);
1994                 
1995                 if (type == S_COL_LOCKED)
1996                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1997                 else if (type == S_COL_MARK) 
1998                         label = gtk_pixmap_new(markxpm, markxpmmask);
1999                 else 
2000                         label = gtk_label_new(title);
2001                 
2002                 if (justify == GTK_JUSTIFY_RIGHT)
2003                         gtk_box_pack_end(GTK_BOX(hbox), label,
2004                                          FALSE, FALSE, 0);
2005                 else
2006                         gtk_box_pack_start(GTK_BOX(hbox), label,
2007                                            FALSE, FALSE, 0);
2008
2009                 if (summaryview->sort_key == sort_by[type]) {
2010                         arrow = gtk_arrow_new
2011                                 (summaryview->sort_type == SORT_ASCENDING
2012                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2013                                  GTK_SHADOW_IN);
2014                         if (justify == GTK_JUSTIFY_RIGHT)
2015                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2016                                                    FALSE, FALSE, 0);
2017                         else
2018                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2019                                                  FALSE, FALSE, 0);
2020                 }
2021
2022                 gtk_widget_show_all(hbox);
2023                 gtk_clist_set_column_widget(clist, pos, hbox);
2024         }
2025 }
2026
2027 void summary_reflect_prefs(void)
2028 {
2029         SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
2030         bold_style = bold_marked_style = bold_deleted_style = 
2031                 small_style = small_marked_style = small_deleted_style = NULL;
2032         summary_set_fonts(summaryview);
2033         summary_set_column_titles(summaryview);
2034         summary_show(summaryview, summaryview->folder_item);
2035 }
2036
2037 void summary_sort(SummaryView *summaryview,
2038                   FolderSortKey sort_key, FolderSortType sort_type)
2039 {
2040         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2041         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2042         GtkCListCompareFunc cmp_func = NULL;
2043
2044         switch (sort_key) {
2045         case SORT_BY_MARK:
2046                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2047                 break;
2048         case SORT_BY_STATUS:
2049                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2050                 break;
2051         case SORT_BY_MIME:
2052                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2053                 break;
2054         case SORT_BY_NUMBER:
2055                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2056                 break;
2057         case SORT_BY_SIZE:
2058                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2059                 break;
2060         case SORT_BY_DATE:
2061                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2062                 break;
2063         case SORT_BY_FROM:
2064                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2065                 break;
2066         case SORT_BY_SUBJECT:
2067                 if (summaryview->simplify_subject_preg)
2068                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2069                 else
2070                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2071                 break;
2072         case SORT_BY_SCORE:
2073                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2074                 break;
2075         case SORT_BY_LABEL:
2076                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2077                 break;
2078         case SORT_BY_TO:
2079                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2080                 break;
2081         case SORT_BY_LOCKED:
2082                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2083                 break;
2084         case SORT_BY_NONE:
2085                 break;
2086         default:
2087                 return;
2088         }
2089
2090         summaryview->sort_key = sort_key;
2091         summaryview->sort_type = sort_type;
2092
2093         summary_set_column_titles(summaryview);
2094         summary_set_menu_sensitive(summaryview);
2095
2096         /* allow fallback to don't sort */
2097         if (summaryview->sort_key == SORT_BY_NONE)
2098                 return;
2099
2100         if (cmp_func != NULL) {
2101                 debug_print("Sorting summary...");
2102                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2103
2104                 main_window_cursor_wait(summaryview->mainwin);
2105
2106                 gtk_clist_freeze(clist);
2107                 gtk_clist_set_compare_func(clist, cmp_func);
2108
2109                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2110
2111                 gtk_sctree_sort_recursive(ctree, NULL);
2112
2113                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2114
2115                 main_window_cursor_normal(summaryview->mainwin);
2116                 gtk_clist_thaw(clist);
2117
2118                 debug_print("done.\n");
2119                 STATUSBAR_POP(summaryview->mainwin);
2120         }
2121 }
2122
2123 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2124                                    GtkCTreeNode *cnode, gpointer data)
2125 {
2126         SummaryView *summaryview = (SummaryView *)data;
2127         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2128         gchar *text[N_SUMMARY_COLS];
2129         gint *col_pos = summaryview->col_pos;
2130         const gchar *msgid = msginfo->msgid;
2131         GHashTable *msgid_table = summaryview->msgid_table;
2132
2133         summary_set_header(summaryview, text, msginfo);
2134
2135         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2136                                 NULL, NULL, NULL, NULL, FALSE,
2137                                 gnode->parent->parent ? TRUE : FALSE);
2138 #define SET_TEXT(col) \
2139         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2140                                 text[col_pos[col]])
2141
2142         SET_TEXT(S_COL_NUMBER);
2143         SET_TEXT(S_COL_SCORE);
2144         SET_TEXT(S_COL_SIZE);
2145         SET_TEXT(S_COL_DATE);
2146         SET_TEXT(S_COL_FROM);
2147         SET_TEXT(S_COL_SUBJECT);
2148
2149 #undef SET_TEXT
2150
2151         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2152         summary_set_marks_func(ctree, cnode, summaryview);
2153
2154         if (msgid && msgid[0] != '\0')
2155                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2156
2157         return TRUE;
2158 }
2159
2160 static void summary_set_ctree_from_list(SummaryView *summaryview,
2161                                         GSList *mlist)
2162 {
2163         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2164         MsgInfo *msginfo;
2165         GtkCTreeNode *node = NULL;
2166         GHashTable *msgid_table;
2167         GHashTable *subject_table;
2168         GSList * cur;
2169         START_TIMING("summary_set_ctree_from_list");
2170         
2171         if (!mlist) return;
2172
2173         debug_print("\tSetting summary from message data...");
2174         STATUSBAR_PUSH(summaryview->mainwin,
2175                        _("Setting summary from message data..."));
2176         gdk_flush();
2177
2178         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2179                                        G_CALLBACK(summary_tree_expanded), summaryview);
2180
2181         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2182         summaryview->msgid_table = msgid_table;
2183         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2184         summaryview->subject_table = subject_table;
2185
2186         if (prefs_common.use_addr_book)
2187                 start_address_completion();
2188         
2189         for (cur = mlist ; cur != NULL && cur->data != NULL; cur = cur->next) {
2190                 msginfo = (MsgInfo *)cur->data;
2191                 msginfo->threadscore = msginfo->score;
2192         }
2193
2194         if (summaryview->threaded) {
2195                 GNode *root, *gnode;
2196
2197                 root = procmsg_get_thread_tree(mlist);
2198
2199                 for (gnode = root->children; gnode != NULL;
2200                      gnode = gnode->next) {
2201                         node = gtk_ctree_insert_gnode
2202                                 (ctree, NULL, node, gnode,
2203                                  summary_insert_gnode_func, summaryview);
2204                 }
2205
2206                 g_node_destroy(root);
2207                 
2208                 summary_thread_init(summaryview);
2209         } else {
2210                 gchar *text[N_SUMMARY_COLS];
2211                 cur = mlist;
2212                 for (; mlist != NULL; mlist = mlist->next) {
2213                         msginfo = (MsgInfo *)mlist->data;
2214
2215                         summary_set_header(summaryview, text, msginfo);
2216
2217                         node = gtk_ctree_insert_node
2218                                 (ctree, NULL, node, text, 2,
2219                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2220                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2221                         summary_set_marks_func(ctree, node, summaryview);
2222
2223                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2224                                 g_hash_table_insert(msgid_table,
2225                                                     msginfo->msgid, node);
2226
2227                         subject_table_insert(subject_table,
2228                                              msginfo->subject,
2229                                              node);
2230                 }
2231                 mlist = cur;
2232         }
2233
2234         if (prefs_common.enable_hscrollbar &&
2235             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2236                 gint optimal_width;
2237
2238                 optimal_width = gtk_clist_optimal_column_width
2239                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2240                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2241                                            summaryview->col_pos[S_COL_SUBJECT],
2242                                            optimal_width);
2243         }
2244
2245         if (prefs_common.use_addr_book)
2246                 end_address_completion();
2247
2248         debug_print("done.\n");
2249         STATUSBAR_POP(summaryview->mainwin);
2250         if (debug_get_mode()) {
2251                 debug_print("\tmsgid hash table size = %d\n",
2252                             g_hash_table_size(msgid_table));
2253                 debug_print("\tsubject hash table size = %d\n",
2254                             g_hash_table_size(subject_table));
2255         }
2256
2257         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2258
2259         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2260
2261         while (prefs_common.bold_unread && node) {
2262                 GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2263                 if (GTK_CTREE_ROW(node)->children)
2264                         summary_set_row_marks(summaryview, node);
2265                 node = next;
2266         }
2267
2268         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2269                                        G_CALLBACK(summary_tree_expanded), summaryview);
2270         END_TIMING();
2271 }
2272
2273 static gchar *summary_complete_address(const gchar *addr)
2274 {
2275         gint count;
2276         gchar *res, *tmp, *email_addr;
2277
2278         Xstrdup_a(email_addr, addr, return NULL);
2279         extract_address(email_addr);
2280         g_return_val_if_fail(*email_addr, NULL);
2281
2282         /*
2283          * completion stuff must be already initialized
2284          */
2285         res = NULL;
2286         if (1 < (count = complete_address(email_addr))) {
2287                 tmp = get_complete_address(1);
2288 /*      tmp = addressbook_lookup_name( email_addr );
2289         if( tmp ) { */
2290                 res = procheader_get_fromname(tmp);
2291                 g_free(tmp);
2292         }
2293
2294         return res;
2295 }
2296
2297 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2298                                MsgInfo *msginfo)
2299 {
2300         static gchar date_modified[80];
2301         static gchar *to = NULL;
2302         static gchar col_score[11];
2303         static gchar buf[BUFFSIZE];
2304         gint *col_pos = summaryview->col_pos;
2305         FolderType ftype = F_UNKNOWN;
2306
2307         text[col_pos[S_COL_MARK]]   = NULL;
2308         text[col_pos[S_COL_STATUS]] = NULL;
2309         text[col_pos[S_COL_MIME]]   = NULL;
2310         text[col_pos[S_COL_LOCKED]] = NULL;
2311         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2312         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2313         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2314
2315         if (msginfo->date_t) {
2316                 procheader_date_get_localtime(date_modified,
2317                                               sizeof(date_modified),
2318                                               msginfo->date_t);
2319                 text[col_pos[S_COL_DATE]] = date_modified;
2320         } else if (msginfo->date)
2321                 text[col_pos[S_COL_DATE]] = msginfo->date;
2322         else
2323                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2324
2325         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2326                 _("(No From)");
2327         
2328         if (msginfo->folder && msginfo->folder->folder)
2329                 ftype = msginfo->folder->folder->klass->type; 
2330                 
2331         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2332                 gchar *addr = NULL;
2333
2334                 Xstrdup_a(addr, msginfo->from, return);
2335                 extract_address(addr);
2336                 if (prefs_common.use_addr_book) {
2337                         if (account_find_from_address(addr)) {
2338                                 addr = summary_complete_address(msginfo->to);
2339                                 g_free(to);
2340                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2341                                 text[col_pos[S_COL_FROM]] = to;
2342                                 g_free(addr);
2343                         }
2344                 } else {
2345                         if (account_find_from_address(addr)) {
2346                                 g_free(to);
2347                                 to = g_strconcat("-->", msginfo->to, NULL);
2348                                 text[col_pos[S_COL_FROM]] = to;
2349                         }
2350                 }
2351         }
2352
2353         /*
2354          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2355          * checking whether the above block (which handles the special case of
2356          * the --> in sent boxes) was executed.
2357          */
2358         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2359                 gchar *from = summary_complete_address(msginfo->from);
2360                 if (from) {
2361                         g_free(to);
2362                         to = from;
2363                         text[col_pos[S_COL_FROM]] = to;
2364                 }                       
2365         }
2366
2367         if (summaryview->simplify_subject_preg != NULL)
2368                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2369                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2370                                         summaryview->simplify_subject_preg) : 
2371                         _("(No Subject)");
2372         else 
2373                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2374                         _("(No Subject)");
2375 }
2376
2377 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2378 {
2379         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2380 }
2381
2382 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2383                                       GtkCTreeNode *row)
2384 {
2385         g_return_if_fail(summaryview != NULL);
2386         g_return_if_fail(msginfo != NULL);
2387         g_return_if_fail(row != NULL);
2388
2389         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2390         && procmsg_msg_has_marked_parent(msginfo))
2391                 summaryview->unreadmarked--;
2392         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2393                 procmsg_msginfo_unset_flags
2394                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2395                 summary_set_row_marks(summaryview, row);
2396                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2397                 summary_status_show(summaryview);
2398         }
2399 }
2400
2401 typedef struct  {
2402         MsgInfo *msginfo;
2403         SummaryView *summaryview;
2404         GtkCTreeNode *row;
2405 } MarkAsReadData;
2406
2407 static int msginfo_mark_as_read_timeout(void *data)
2408 {
2409         MarkAsReadData *mdata = (MarkAsReadData *)data;
2410         if (!mdata)
2411                 return FALSE;
2412         
2413         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2414                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2415                                      mdata->row); 
2416
2417         g_free(mdata);
2418
2419         return FALSE;   
2420 }
2421
2422 static void summary_display_msg_full(SummaryView *summaryview,
2423                                      GtkCTreeNode *row,
2424                                      gboolean new_window, gboolean all_headers)
2425 {
2426         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2427         MsgInfo *msginfo;
2428         gint val;
2429
2430         if (!new_window) {
2431                 if (summaryview->displayed == row)
2432                         return;
2433                 else
2434                         summaryview->messageview->filtered = FALSE;
2435         }                       
2436         
2437         g_return_if_fail(row != NULL);
2438
2439         if (summary_is_locked(summaryview)) return;
2440         summary_lock(summaryview);
2441
2442         STATUSBAR_POP(summaryview->mainwin);
2443         GTK_EVENTS_FLUSH();
2444
2445         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2446
2447         if (new_window) {
2448                 MessageView *msgview;
2449
2450                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2451                 val = messageview_show(msgview, msginfo, all_headers);
2452         } else {
2453                 MessageView *msgview;
2454
2455                 msgview = summaryview->messageview;
2456
2457                 summaryview->displayed = row;
2458                 if (!messageview_is_visible(msgview)) {
2459                         main_window_toggle_message_view(summaryview->mainwin);
2460                         GTK_EVENTS_FLUSH();
2461                 }
2462                 val = messageview_show(msgview, msginfo, all_headers);
2463                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2464                         gtk_widget_grab_focus(summaryview->ctree);
2465                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2466         }
2467
2468         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2469                 if (prefs_common.mark_as_read_delay) {
2470                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2471                         data->summaryview = summaryview;
2472                         data->msginfo = msginfo;
2473                         data->row = row;
2474                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2475                                 msginfo_mark_as_read_timeout, data);
2476                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2477                         msginfo_mark_as_read(summaryview, msginfo, row);
2478                 }
2479         }
2480
2481         summary_set_menu_sensitive(summaryview);
2482         toolbar_main_set_sensitive(summaryview->mainwin);
2483         messageview_set_menu_sensitive(summaryview->messageview);
2484
2485         summary_unlock(summaryview);
2486 }
2487
2488 void summary_display_msg_selected(SummaryView *summaryview,
2489                                   gboolean all_headers)
2490 {
2491         if (summary_is_locked(summaryview)) return;
2492         summaryview->displayed = NULL;
2493         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2494                                  all_headers);
2495 }
2496
2497 void summary_redisplay_msg(SummaryView *summaryview)
2498 {
2499         GtkCTreeNode *node;
2500
2501         if (summaryview->displayed) {
2502                 node = summaryview->displayed;
2503                 summaryview->displayed = NULL;
2504                 summary_display_msg(summaryview, node);
2505         }
2506 }
2507
2508 void summary_open_msg(SummaryView *summaryview)
2509 {
2510         if (!summaryview->selected) return;
2511         
2512         /* CLAWS: if separate message view, don't open a new window
2513          * but rather use the current separated message view */
2514         summary_display_msg_full(summaryview, summaryview->selected,
2515                                  prefs_common.sep_msg ? FALSE : TRUE, 
2516                                  FALSE);
2517 }
2518
2519 void summary_view_source(SummaryView * summaryview)
2520 {
2521         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2522         MsgInfo *msginfo;
2523         SourceWindow *srcwin;
2524
2525         if (!summaryview->selected) return;
2526
2527         srcwin = source_window_create();
2528         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2529         source_window_show_msg(srcwin, msginfo);
2530         source_window_show(srcwin);
2531 }
2532
2533 void summary_reedit(SummaryView *summaryview)
2534 {
2535         MsgInfo *msginfo;
2536
2537         if (!summaryview->selected) return;
2538         if (!summaryview->folder_item) return;
2539         if (summaryview->folder_item->stype != F_OUTBOX &&
2540             summaryview->folder_item->stype != F_DRAFT  &&
2541             summaryview->folder_item->stype != F_QUEUE) return;
2542
2543         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2544                                               summaryview->selected);
2545         if (!msginfo) return;
2546
2547         compose_reedit(msginfo);
2548 }
2549
2550 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2551 {
2552         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2553         GtkCTreeNode *node;
2554
2555         if (summary_is_locked(summaryview)) return FALSE;
2556         if (type == GTK_SCROLL_STEP_FORWARD) {
2557                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2558                 if (node)
2559                         gtkut_ctree_expand_parent_all(ctree, node);
2560                 else
2561                         return FALSE;
2562         } else {
2563                 if (summaryview->selected) {
2564                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2565                         if (!node) return FALSE;
2566                 }
2567         }
2568
2569         if (messageview_is_visible(summaryview->messageview))
2570                 summaryview->display_msg = TRUE;
2571
2572         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2573
2574         if (GTK_CLIST(ctree)->selection)
2575                 gtk_sctree_set_anchor_row
2576                         (GTK_SCTREE(ctree),
2577                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2578
2579         return TRUE;
2580 }
2581
2582 void summary_toggle_view(SummaryView *summaryview)
2583 {
2584         if (!messageview_is_visible(summaryview->messageview) &&
2585             summaryview->selected)
2586                 summary_display_msg(summaryview,
2587                                     summaryview->selected);
2588         else
2589                 main_window_toggle_message_view(summaryview->mainwin);
2590 }
2591
2592 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2593                                                 GtkCTreeNode *node)
2594 {
2595         MsgInfo *msginfo;
2596
2597         if (node) {
2598                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2599                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2600                         return TRUE;
2601                 node = GTK_CTREE_ROW(node)->children;
2602         } else
2603                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2604
2605         while (node) {
2606                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2607                         return TRUE;
2608                 node = GTK_CTREE_ROW(node)->sibling;
2609         }
2610
2611         return FALSE;
2612 }
2613
2614 static gboolean summary_have_unread_children(SummaryView *summaryview,
2615                                              GtkCTreeNode *node)
2616 {
2617         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2618
2619         if (!node) return FALSE;
2620
2621         node = GTK_CTREE_ROW(node)->children;
2622
2623         while (node) {
2624                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2625                         return TRUE;
2626                 node = GTK_CTREE_ROW(node)->sibling;
2627         }
2628         return FALSE;
2629 }
2630
2631 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2632 {
2633         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2634         GtkStyle *style = NULL;
2635         MsgInfo *msginfo;
2636         MsgFlags flags;
2637         gint *col_pos = summaryview->col_pos;
2638
2639         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2640         if (!msginfo) return;
2641
2642         flags = msginfo->flags;
2643
2644         gtk_ctree_node_set_foreground(ctree, row, NULL);
2645
2646         /* set new/unread column */
2647         if (MSG_IS_IGNORE_THREAD(flags)) {
2648                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2649                                           ignorethreadxpm, ignorethreadxpmmask);
2650         } else if (MSG_IS_NEW(flags)) {
2651                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2652                                           newxpm, newxpmmask);
2653         } else if (MSG_IS_UNREAD(flags)) {
2654                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2655                                           unreadxpm, unreadxpmmask);
2656         } else if (MSG_IS_REPLIED(flags)) {
2657                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2658                                           repliedxpm, repliedxpmmask);
2659         } else if (MSG_IS_FORWARDED(flags)) {
2660                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2661                                           forwardedxpm, forwardedxpmmask);
2662         } else {
2663                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2664                                         NULL);
2665         }
2666
2667         if (prefs_common.bold_unread &&
2668             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2669              (!GTK_CTREE_ROW(row)->expanded &&
2670               GTK_CTREE_ROW(row)->children &&
2671               summary_have_unread_children(summaryview, row))))
2672                 style = bold_style;
2673
2674         /* set mark column */
2675         if (MSG_IS_DELETED(flags)) {
2676                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2677                                           deletedxpm, deletedxpmmask);
2678                 if (style)
2679                         style = bold_deleted_style;
2680                 else {
2681                         style = small_deleted_style;
2682                 }
2683                         gtk_ctree_node_set_foreground
2684                                 (ctree, row, &summaryview->color_dim);
2685         } else if (MSG_IS_MARKED(flags)) {
2686                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2687                                           markxpm, markxpmmask);
2688         } else if (MSG_IS_MOVE(flags)) {
2689                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2690                 if (style)
2691                         style = bold_marked_style;
2692                 else {
2693                         style = small_marked_style;
2694                 }
2695                         gtk_ctree_node_set_foreground
2696                                 (ctree, row, &summaryview->color_marked);
2697         } else if (MSG_IS_COPY(flags)) {
2698                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2699                 if (style)
2700                         style = bold_marked_style;
2701                 else {
2702                         style = small_marked_style;
2703                 }
2704                         gtk_ctree_node_set_foreground
2705                                 (ctree, row, &summaryview->color_marked);
2706 #if 0
2707         } else if ((global_scoring ||
2708                   summaryview->folder_item->prefs->scoring) &&
2709                  (msginfo->score >= summaryview->important_score) &&
2710                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2711                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2712                 gtk_ctree_node_set_foreground(ctree, row,
2713                                               &summaryview->color_important);
2714 #endif
2715         } else {
2716                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2717         }
2718
2719         if (MSG_IS_LOCKED(flags)) {
2720                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2721                                           lockedxpm, lockedxpmmask);
2722         }
2723         else {
2724                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2725         }
2726
2727         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2728                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2729                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2730         } else if (MSG_IS_SIGNED(flags)) {
2731                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2732                                           gpgsignedxpm, gpgsignedxpmmask);
2733         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2734                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2735                                           clipkeyxpm, clipkeyxpmmask);
2736         } else if (MSG_IS_ENCRYPTED(flags)) {
2737                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2738                                           keyxpm, keyxpmmask);
2739         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2740                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2741                                           clipxpm, clipxpmmask);
2742         } else {
2743                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2744         }
2745         if (!style)
2746                 style = small_style;
2747
2748         gtk_ctree_node_set_row_style(ctree, row, style);
2749
2750         if (MSG_GET_COLORLABEL(flags))
2751                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2752 }
2753
2754 void summary_set_marks_selected(SummaryView *summaryview)
2755 {
2756         GList *cur;
2757         summary_lock(summaryview);
2758         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
2759              cur = cur->next)
2760                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2761         summary_unlock(summaryview);
2762 }
2763
2764 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2765 {
2766         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2767         MsgInfo *msginfo;
2768
2769         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2770         if (MSG_IS_DELETED(msginfo->flags))
2771                 summaryview->deleted--;
2772         if (MSG_IS_MOVE(msginfo->flags))
2773                 summaryview->moved--;
2774         if (MSG_IS_COPY(msginfo->flags))
2775                 summaryview->copied--;
2776
2777         procmsg_msginfo_set_to_folder(msginfo, NULL);
2778         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2779         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2780         summary_set_row_marks(summaryview, row);
2781         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2782 }
2783
2784 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2785 {
2786         gboolean changed = FALSE;
2787         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2788         MsgInfo *msginfo;
2789
2790         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2791         if (MSG_IS_DELETED(msginfo->flags))
2792                 summaryview->deleted--;
2793         if (MSG_IS_MOVE(msginfo->flags)) {
2794                 summaryview->moved--;
2795                 changed = TRUE;
2796         }
2797         if (MSG_IS_COPY(msginfo->flags)) {
2798                 summaryview->copied--;
2799                 changed = TRUE;
2800         }
2801         procmsg_msginfo_set_to_folder(msginfo, NULL);
2802         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2803         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2804         summary_set_row_marks(summaryview, row);
2805         debug_print("Message %d is locked\n", msginfo->msgnum);
2806 }
2807
2808 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2809 {
2810         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2811         MsgInfo *msginfo;
2812
2813         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2814         if (!MSG_IS_LOCKED(msginfo->flags))
2815                 return;
2816         procmsg_msginfo_set_to_folder(msginfo, NULL);
2817         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2818         summary_set_row_marks(summaryview, row);
2819         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2820 }
2821
2822 void summary_mark(SummaryView *summaryview)
2823 {
2824         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2825         GList *cur;
2826
2827         START_LONG_OPERATION(summaryview);
2828         folder_item_set_batch(summaryview->folder_item, TRUE);
2829         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2830                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2831         folder_item_set_batch(summaryview->folder_item, FALSE);
2832         END_LONG_OPERATION(summaryview);
2833
2834         summary_status_show(summaryview);
2835 }
2836
2837 static void summary_mark_row_as_read(SummaryView *summaryview,
2838                                      GtkCTreeNode *row)
2839 {
2840         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2841         MsgInfo *msginfo;
2842
2843         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2844
2845         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2846                 return;
2847
2848         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2849         && procmsg_msg_has_marked_parent(msginfo))
2850                 summaryview->unreadmarked--;
2851
2852         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2853         summary_set_row_marks(summaryview, row);
2854         debug_print("Message %d is marked as read\n",
2855                 msginfo->msgnum);
2856 }
2857
2858 void summary_mark_as_read(SummaryView *summaryview)
2859 {
2860         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2861         GList *cur;
2862
2863         START_LONG_OPERATION(summaryview);
2864         folder_item_set_batch(summaryview->folder_item, TRUE);
2865         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2866                 summary_mark_row_as_read(summaryview,
2867                                          GTK_CTREE_NODE(cur->data));
2868         folder_item_set_batch(summaryview->folder_item, FALSE);
2869         END_LONG_OPERATION(summaryview);
2870         
2871         summary_status_show(summaryview);
2872 }
2873
2874 void summary_msgs_lock(SummaryView *summaryview)
2875 {
2876         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2877         GList *cur;
2878
2879         START_LONG_OPERATION(summaryview);
2880         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2881                 summary_lock_row(summaryview,
2882                                          GTK_CTREE_NODE(cur->data));
2883         END_LONG_OPERATION(summaryview);
2884         
2885         summary_status_show(summaryview);
2886 }
2887
2888 void summary_msgs_unlock(SummaryView *summaryview)
2889 {
2890         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2891         GList *cur;
2892
2893         START_LONG_OPERATION(summaryview);
2894         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2895                 summary_unlock_row(summaryview,
2896                                    GTK_CTREE_NODE(cur->data));
2897         END_LONG_OPERATION(summaryview);
2898         
2899         summary_status_show(summaryview);
2900 }
2901
2902 void summary_mark_all_read(SummaryView *summaryview)
2903 {
2904         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2905         GtkCTreeNode *node;
2906
2907         START_LONG_OPERATION(summaryview);
2908         folder_item_set_batch(summaryview->folder_item, TRUE);
2909         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2910              node = gtkut_ctree_node_next(ctree, node))
2911                 summary_mark_row_as_read(summaryview, node);
2912         folder_item_set_batch(summaryview->folder_item, FALSE);
2913         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2914              node = gtkut_ctree_node_next(ctree, node)) {
2915                 if (!GTK_CTREE_ROW(node)->expanded)
2916                         summary_set_row_marks(summaryview, node);
2917         }
2918         END_LONG_OPERATION(summaryview);
2919         
2920         summary_status_show(summaryview);
2921 }
2922
2923 static void summary_mark_row_as_unread(SummaryView *summaryview,
2924                                        GtkCTreeNode *row)
2925 {
2926         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2927         MsgInfo *msginfo;
2928
2929         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2930         if (MSG_IS_DELETED(msginfo->flags)) {
2931                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2932                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2933                 summaryview->deleted--;
2934         }
2935
2936         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2937         && procmsg_msg_has_marked_parent(msginfo))
2938                 summaryview->unreadmarked++;
2939
2940         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2941         debug_print("Message %d is marked as unread\n",
2942                 msginfo->msgnum);
2943
2944         summary_set_row_marks(summaryview, row);
2945 }
2946
2947 void summary_mark_as_unread(SummaryView *summaryview)
2948 {
2949         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2950         GList *cur;
2951
2952         START_LONG_OPERATION(summaryview);
2953         folder_item_set_batch(summaryview->folder_item, TRUE);
2954         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
2955                 cur = cur->next)
2956                 summary_mark_row_as_unread(summaryview,
2957                                            GTK_CTREE_NODE(cur->data));
2958         folder_item_set_batch(summaryview->folder_item, FALSE);
2959         END_LONG_OPERATION(summaryview);
2960         
2961         summary_status_show(summaryview);
2962 }
2963
2964 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2965 {
2966         GList * cur;
2967         gboolean found;
2968
2969         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2970
2971         case F_NEWS:
2972
2973                 /*
2974                   security : checks if one the accounts correspond to
2975                   the author of the post
2976                 */
2977
2978                 found = FALSE;
2979                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2980                         PrefsAccount * account;
2981                         gchar * from_name;
2982                         
2983                         account = cur->data;
2984                         if (account->name && *account->name)
2985                                 from_name =
2986                                         g_strdup_printf("%s <%s>",
2987                                                         account->name,
2988                                                         account->address);
2989                         else
2990                                 from_name =
2991                                         g_strdup_printf("%s",
2992                                                         account->address);
2993                         
2994                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
2995                                 g_free(from_name);
2996                                 found = TRUE;
2997                                 break;
2998                         }
2999                         g_free(from_name);
3000                 }
3001
3002                 if (!found) {
3003                         alertpanel_error(_("You're not the author of the article\n"));
3004                 }
3005                 
3006                 return found;
3007
3008         default:
3009                 return TRUE;
3010         }
3011 }
3012
3013 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3014 {
3015         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3016         MsgInfo *msginfo;
3017
3018         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3019
3020         if (!check_permission(summaryview, msginfo))
3021                 return;
3022
3023         if (MSG_IS_LOCKED(msginfo->flags)) return;
3024
3025         if (MSG_IS_DELETED(msginfo->flags)) return;
3026
3027         if (MSG_IS_MOVE(msginfo->flags))
3028                 summaryview->moved--;
3029         if (MSG_IS_COPY(msginfo->flags))
3030                 summaryview->copied--;
3031
3032         procmsg_msginfo_set_to_folder(msginfo, NULL);
3033         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
3034         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
3035         summaryview->deleted++;
3036
3037         if (!prefs_common.immediate_exec && 
3038             summaryview->folder_item->stype != F_TRASH)
3039                 summary_set_row_marks(summaryview, row);
3040
3041         debug_print("Message %s/%d is set to delete\n",
3042                     msginfo->folder->path, msginfo->msgnum);
3043 }
3044
3045 void summary_cancel(SummaryView *summaryview)
3046 {
3047         MsgInfo * msginfo;
3048         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3049
3050         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3051                                               summaryview->selected);
3052         if (!msginfo) return;
3053
3054         if (!check_permission(summaryview, msginfo))
3055                 return;
3056
3057         news_cancel_article(summaryview->folder_item->folder, msginfo);
3058         
3059         if (summary_is_locked(summaryview)) return;
3060
3061         summary_lock(summaryview);
3062
3063         gtk_clist_freeze(clist);
3064
3065         summary_update_status(summaryview);
3066         summary_status_show(summaryview);
3067
3068         gtk_clist_thaw(clist);
3069
3070         summary_unlock(summaryview);
3071 }
3072
3073 void summary_delete(SummaryView *summaryview)
3074 {
3075         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3076         FolderItem *item = summaryview->folder_item;
3077         GList *cur;
3078         GtkCTreeNode *sel_last = NULL;
3079         GtkCTreeNode *node;
3080         AlertValue aval;
3081         MsgInfo *msginfo;
3082
3083         if (!item) return;
3084
3085         if (summary_is_locked(summaryview)) return;
3086
3087         if (!summaryview->folder_item ||
3088             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3089
3090         aval = alertpanel(_("Delete message(s)"),
3091                           _("Do you really want to delete selected message(s)?"),
3092                           GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3093         if (aval != G_ALERTDEFAULT) return;
3094
3095         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3096              cur = cur->next) {
3097                 GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3098                 msginfo = gtk_ctree_node_get_row_data(ctree, row);
3099                 if (msginfo->total_size != 0 && 
3100                     msginfo->size != (off_t)msginfo->total_size)
3101                         partial_mark_for_delete(msginfo);
3102         }
3103
3104         main_window_cursor_wait(summaryview->mainwin);
3105
3106         /* next code sets current row focus right. We need to find a row
3107          * that is not deleted. */
3108         summary_lock(summaryview);
3109         folder_item_update_freeze();
3110         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3111         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3112                 sel_last = GTK_CTREE_NODE(cur->data);
3113                 summary_delete_row(summaryview, sel_last);
3114         }
3115         folder_item_update_thaw();
3116         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3117         summary_unlock(summaryview);
3118
3119         node = summary_find_next_msg(summaryview, sel_last);
3120         if (!node)
3121                 node = summary_find_prev_msg(summaryview, sel_last);
3122
3123         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3124         
3125         if (prefs_common.immediate_exec || item->stype == F_TRASH) {
3126                 summary_execute(summaryview);
3127                 /* after deleting, the anchor may be at an invalid row
3128                  * so reset it to the node we found earlier */
3129                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3130         } else
3131                 summary_status_show(summaryview);
3132                 
3133         main_window_cursor_normal(summaryview->mainwin);
3134 }
3135
3136 void summary_delete_trash(SummaryView *summaryview)
3137 {
3138         FolderItem *to_folder;
3139
3140         if (!summaryview->folder_item ||
3141             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3142
3143         to_folder = summaryview->folder_item->folder->trash;
3144         
3145         if (to_folder == NULL || to_folder == summaryview->folder_item)
3146                 summary_delete(summaryview);
3147         else
3148                 summary_move_selected_to(summaryview, to_folder);
3149 }
3150
3151
3152 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3153 {
3154         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3155         MsgInfo *msginfo;
3156
3157         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3158         if (MSG_IS_DELETED(msginfo->flags))
3159                 summaryview->deleted--;
3160         if (MSG_IS_MOVE(msginfo->flags))
3161                 summaryview->moved--;
3162         if (MSG_IS_COPY(msginfo->flags))
3163                 summaryview->copied--;
3164
3165         procmsg_msginfo_set_to_folder(msginfo, NULL);
3166         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3167         summary_set_row_marks(summaryview, row);
3168
3169         debug_print("Message %s/%d is unmarked\n",
3170                     msginfo->folder->path, msginfo->msgnum);
3171 }
3172
3173 void summary_unmark(SummaryView *summaryview)
3174 {
3175         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3176         GList *cur;
3177
3178         START_LONG_OPERATION(summaryview);
3179         folder_item_set_batch(summaryview->folder_item, TRUE);
3180         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3181                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3182         folder_item_set_batch(summaryview->folder_item, FALSE);
3183         END_LONG_OPERATION(summaryview);
3184         
3185         summary_status_show(summaryview);
3186 }
3187
3188 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3189                                 FolderItem *to_folder)
3190 {
3191         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3192         MsgInfo *msginfo;
3193
3194         g_return_if_fail(to_folder != NULL);
3195
3196         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3197         if (MSG_IS_LOCKED(msginfo->flags))
3198                 return;
3199
3200         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3201         if (MSG_IS_DELETED(msginfo->flags))
3202                 summaryview->deleted--;
3203         if (MSG_IS_COPY(msginfo->flags)) {
3204                 summaryview->copied--;
3205         }
3206         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3207         if (!MSG_IS_MOVE(msginfo->flags)) {
3208                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
3209                 summaryview->moved++;
3210         }
3211         if (!prefs_common.immediate_exec) {
3212                 summary_set_row_marks(summaryview, row);
3213         }
3214
3215         debug_print("Message %d is set to move to %s\n",
3216                     msginfo->msgnum, to_folder->path);
3217 }
3218
3219 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3220 {
3221         GList *cur;
3222
3223         if (!to_folder) return;
3224         if (!summaryview->folder_item ||
3225             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3226
3227         if (summary_is_locked(summaryview)) return;
3228
3229         if (summaryview->folder_item == to_folder) {
3230                 alertpanel_error(_("Destination is same as current folder."));
3231                 return;
3232         }
3233
3234         START_LONG_OPERATION(summaryview);
3235
3236         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3237              cur != NULL && cur->data != NULL; cur = cur->next)
3238                 summary_move_row_to
3239                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3240
3241         END_LONG_OPERATION(summaryview);
3242
3243         if (prefs_common.immediate_exec) {
3244                 summary_execute(summaryview);
3245         } else {
3246                 summary_status_show(summaryview);
3247         }
3248         
3249         if (!summaryview->selected) { /* this was the last message */
3250                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3251                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3252                 if (node)
3253                         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3254         }
3255
3256 }
3257
3258 void summary_move_to(SummaryView *summaryview)
3259 {
3260         FolderItem *to_folder;
3261
3262         if (!summaryview->folder_item ||
3263             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3264
3265         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3266                                          FOLDER_SEL_MOVE, NULL);
3267         summary_move_selected_to(summaryview, to_folder);
3268 }
3269
3270 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3271                                 FolderItem *to_folder)
3272 {
3273         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3274         MsgInfo *msginfo;
3275
3276         g_return_if_fail(to_folder != NULL);
3277
3278         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3279         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3280         if (MSG_IS_DELETED(msginfo->flags))
3281                 summaryview->deleted--;
3282         if (MSG_IS_MOVE(msginfo->flags)) {
3283                 summaryview->moved--;
3284         }
3285         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
3286         if (!MSG_IS_COPY(msginfo->flags)) {
3287                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
3288                 summaryview->copied++;
3289         }
3290         if (!prefs_common.immediate_exec) {
3291                 summary_set_row_marks(summaryview, row);
3292         }
3293
3294         debug_print("Message %d is set to copy to %s\n",
3295                     msginfo->msgnum, to_folder->path);
3296 }
3297
3298 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3299 {
3300         GList *cur;
3301
3302         if (!to_folder) return;
3303         if (!summaryview->folder_item) return;
3304
3305         if (summary_is_locked(summaryview)) return;
3306
3307         if (summaryview->folder_item == to_folder) {
3308                 alertpanel_error
3309                         (_("Destination to copy is same as current folder."));
3310                 return;
3311         }
3312
3313         START_LONG_OPERATION(summaryview);
3314
3315         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3316              cur != NULL && cur->data != NULL; cur = cur->next)
3317                 summary_copy_row_to
3318                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3319
3320         END_LONG_OPERATION(summaryview);
3321
3322         if (prefs_common.immediate_exec)
3323                 summary_execute(summaryview);
3324         else {
3325                 summary_status_show(summaryview);
3326         }
3327 }
3328
3329 void summary_copy_to(SummaryView *summaryview)
3330 {
3331         FolderItem *to_folder;
3332
3333         if (!summaryview->folder_item) return;
3334
3335         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3336                                          FOLDER_SEL_COPY, NULL);
3337         summary_copy_selected_to(summaryview, to_folder);
3338 }
3339
3340 void summary_add_address(SummaryView *summaryview)
3341 {
3342         MsgInfo *msginfo;
3343         gchar *from;
3344
3345         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3346                                               summaryview->selected);
3347         if (!msginfo) return;
3348
3349         Xstrdup_a(from, msginfo->from, return);
3350         eliminate_address_comment(from);
3351         extract_address(from);
3352         addressbook_add_contact(msginfo->fromname, from, NULL);
3353 }
3354
3355 void summary_select_all(SummaryView *summaryview)
3356 {
3357         if (!summaryview->folder_item) return;
3358
3359         summary_lock(summaryview);
3360         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3361         summary_unlock(summaryview);
3362         summary_status_show(summaryview);
3363 }
3364
3365 void summary_unselect_all(SummaryView *summaryview)
3366 {
3367         summary_lock(summaryview);
3368         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3369         summary_unlock(summaryview);
3370         summary_status_show(summaryview);
3371 }
3372
3373 void summary_select_thread(SummaryView *summaryview)
3374 {
3375         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3376         GtkCTreeNode *node = summaryview->selected;
3377
3378         if (!node) return;
3379
3380         while (GTK_CTREE_ROW(node)->parent != NULL)
3381                 node = GTK_CTREE_ROW(node)->parent;
3382
3383         if (node != summaryview->selected)
3384                 summary_select_node
3385                         (summaryview, node,
3386                          messageview_is_visible(summaryview->messageview),
3387                          FALSE);
3388
3389         gtk_ctree_select_recursive(ctree, node);
3390
3391         summary_status_show(summaryview);
3392 }
3393
3394 void summary_save_as(SummaryView *summaryview)
3395 {
3396         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3397         MsgInfo *msginfo;
3398         gchar *filename = NULL;
3399         gchar *src, *dest;
3400         gchar *tmp;
3401
3402         AlertValue aval = 0;
3403
3404         if (!summaryview->selected) return;
3405         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3406         if (!msginfo) return;
3407
3408         if (msginfo->subject) {
3409                 Xstrdup_a(filename, msginfo->subject, return);
3410                 subst_for_filename(filename);
3411         }
3412         if (g_getenv ("G_BROKEN_FILENAMES") &&
3413             filename && !g_utf8_validate(filename, -1, NULL)) {
3414                 gchar *oldstr = filename;
3415                 filename = conv_codeset_strdup(filename,
3416                                                conv_get_locale_charset_str(),
3417                                                CS_UTF_8);
3418                 if (!filename) {
3419                         g_warning("summary_save_as(): faild to convert character set.");
3420                         filename = g_strdup(oldstr);
3421                 }
3422                 dest = filesel_select_file_save(_("Save as"), filename);
3423                 g_free(filename);
3424         } else
3425                 dest = filesel_select_file_save(_("Save as"), filename);
3426         filename = NULL;
3427         if (!dest) return;
3428         if (is_file_exist(dest)) {
3429                 aval = alertpanel(_("Append or Overwrite"),
3430                                   _("Append or overwrite existing file?"),
3431                                   _("Append"), _("Overwrite"),
3432                                   GTK_STOCK_CANCEL);
3433                 if (aval != 0 && aval != 1)
3434                         return;
3435         }
3436
3437         src = procmsg_get_message_file(msginfo);
3438         tmp = g_path_get_basename(dest);
3439
3440         if ( aval==0 ) { /* append */
3441                 if (append_file(src, dest, TRUE) < 0) 
3442                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3443         } else { /* overwrite */
3444                 if (copy_file(src, dest, TRUE) < 0)
3445                         alertpanel_error(_("Can't save the file '%s'."), tmp);
3446         }
3447         g_free(src);
3448         
3449         /*
3450          * If two or more msgs are selected,
3451          * append them to the output file.
3452          */
3453         if (GTK_CLIST(ctree)->selection->next) {
3454                 GList *item;
3455                 for (item = GTK_CLIST(ctree)->selection->next; item != NULL; item=item->next) {
3456                         msginfo = gtk_ctree_node_get_row_data(ctree, (GtkCTreeNode*)item->data);
3457                         if (!msginfo) break;
3458                         src = procmsg_get_message_file(msginfo);
3459                         if (append_file(src, dest, TRUE) < 0)
3460                                 alertpanel_error(_("Can't save the file '%s'."), tmp);
3461                 }
3462                 g_free(src);
3463         }
3464         g_free(dest);
3465         g_free(tmp);
3466 }
3467
3468 void summary_print(SummaryView *summaryview)
3469 {
3470         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3471 #ifndef USE_GNOMEPRINT
3472         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3473         MsgInfo *msginfo;
3474         gchar *cmdline = NULL;
3475         gchar *p;
3476 #else
3477         gboolean first = TRUE;
3478         GList *tmplist;
3479 #endif
3480         GList *cur;
3481
3482         if (clist->selection == NULL) return;
3483 #ifndef USE_GNOMEPRINT
3484         cmdline = input_dialog(_("Print"),
3485                                _("Enter the print command line:\n"
3486                                  "('%s' will be replaced with file name)"),
3487                                prefs_common.print_cmd);
3488         if (!cmdline) return;
3489         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3490             strchr(p + 2, '%')) {
3491                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
3492                                  cmdline);
3493                 g_free(cmdline);
3494                 return;
3495         }
3496         for (cur = clist->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3497                 msginfo = gtk_ctree_node_get_row_data
3498                         (ctree, GTK_CTREE_NODE(cur->data));
3499                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3500         }
3501
3502         g_free(cmdline);
3503         
3504 #else
3505         tmplist = g_list_copy(clist->selection);
3506         summary_unselect_all(summaryview);
3507         for (cur = tmplist; cur != NULL && cur->data != NULL; cur = cur->next) {
3508                 GtkCTreeNode *node = GTK_CTREE_NODE(cur->data);
3509                 summary_select_node(summaryview, node, TRUE, TRUE);
3510                 if (!summaryview->messageview->mimeview 
3511                 ||  !summaryview->messageview->mimeview->textview
3512                 ||  !summaryview->messageview->mimeview->textview->text)
3513                         alertpanel_warning(_("Cannot print: the message doesn't "
3514                                              "contain text."));
3515                 gedit_print(
3516                         GTK_TEXT_VIEW(summaryview->messageview->mimeview
3517                                         ->textview->text));
3518         }
3519         for (cur = tmplist; cur != NULL && cur->data != NULL; cur = cur->next) {
3520                 GtkCTreeNode *node = GTK_CTREE_NODE(cur->data);
3521                 gtk_sctree_select_with_state(
3522                         GTK_SCTREE(clist), node, first ? 0:GDK_CONTROL_MASK);
3523                 first = FALSE;
3524         }
3525         g_list_free(tmplist);
3526 #endif
3527 }
3528
3529 gboolean summary_execute(SummaryView *summaryview)
3530 {
3531         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3532         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3533         GtkCTreeNode *node, *next;
3534         GtkCTreeNode *new_selected = NULL;
3535
3536         if (!summaryview->folder_item) return FALSE;
3537
3538         if (summary_is_locked(summaryview)) return FALSE;
3539         summary_lock(summaryview);
3540
3541         gtk_clist_freeze(clist);
3542
3543         if (summaryview->threaded)
3544                 summary_unthread_for_exec(summaryview);
3545
3546         folder_item_update_freeze();
3547         summary_execute_move(summaryview);
3548         summary_execute_copy(summaryview);
3549         summary_execute_delete(summaryview);
3550         
3551         node = GTK_CTREE_NODE(clist->row_list);
3552         for (; node != NULL; node = next) {
3553                 next = gtkut_ctree_node_next(ctree, node);
3554                 if (gtk_ctree_node_get_row_data(ctree, node) != NULL) continue;
3555
3556                 if (node == summaryview->displayed) {
3557                         messageview_clear(summaryview->messageview);
3558                         summaryview->displayed = NULL;
3559                 }
3560                 if (GTK_CTREE_ROW(node)->children != NULL) {
3561                         g_warning("summary_execute(): children != NULL\n");
3562                         continue;
3563                 }
3564
3565                 if (!new_selected &&
3566                     gtkut_ctree_node_is_selected(ctree, node)) {
3567                         summary_unselect_all(summaryview);
3568                         new_selected = summary_find_next_msg(summaryview, node);
3569                         if (!new_selected)
3570                                 new_selected = summary_find_prev_msg
3571                                         (summaryview, node);
3572                 }
3573
3574                 gtk_ctree_remove_node(ctree, node);
3575         }
3576
3577         folder_item_update_thaw();
3578         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3579
3580         if (new_selected) {
3581                 summary_unlock(summaryview);
3582                 gtk_sctree_select
3583                         (GTK_SCTREE(ctree),
3584                          summaryview->displayed ? summaryview->displayed
3585                          : new_selected);
3586                 summary_lock(summaryview);
3587         }
3588
3589         if (summaryview->threaded) {
3590                 gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3591                 summary_thread_build(summaryview);
3592                 summary_thread_init(summaryview);
3593                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3594         }
3595
3596         summaryview->selected = clist->selection ?
3597                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3598
3599         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3600                 menu_set_insensitive_all
3601                         (GTK_MENU_SHELL(summaryview->popupmenu));
3602                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3603         } else
3604                 gtk_widget_grab_focus(summaryview->ctree);
3605
3606         summary_update_status(summaryview);
3607         summary_status_show(summaryview);
3608
3609         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3610
3611         summary_unlock(summaryview);
3612         return TRUE;
3613 }
3614
3615 static void summary_execute_move(SummaryView *summaryview)
3616 {
3617         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3618         GSList *cur;
3619
3620         /* search moving messages and execute */
3621         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3622                                 summaryview);
3623
3624         if (summaryview->mlist) {
3625                 procmsg_move_messages(summaryview->mlist);
3626
3627                 for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3628                         procmsg_msginfo_free((MsgInfo *)cur->data);
3629                 g_slist_free(summaryview->mlist);
3630                 summaryview->mlist = NULL;
3631         }
3632 }
3633
3634 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3635                                       gpointer data)
3636 {
3637         SummaryView *summaryview = data;
3638         MsgInfo *msginfo;
3639
3640         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3641
3642         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3643                 summaryview->mlist =
3644                         g_slist_prepend(summaryview->mlist, msginfo);
3645                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3646
3647                 if (msginfo->msgid && *msginfo->msgid &&
3648                     node == g_hash_table_lookup(summaryview->msgid_table,
3649                                                 msginfo->msgid))
3650                         g_hash_table_remove(summaryview->msgid_table,
3651                                             msginfo->msgid);
3652         }
3653 }
3654
3655 static void summary_execute_copy(SummaryView *summaryview)
3656 {
3657         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3658
3659         /* search copying messages and execute */
3660         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3661                                 summaryview);
3662
3663         if (summaryview->mlist) {
3664                 summaryview->mlist = g_slist_reverse(summaryview->mlist);
3665                 procmsg_copy_messages(summaryview->mlist);
3666
3667                 g_slist_free(summaryview->mlist);
3668                 summaryview->mlist = NULL;
3669         }
3670 }
3671
3672 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3673                                       gpointer data)
3674 {
3675         SummaryView *summaryview = data;
3676         MsgInfo *msginfo;
3677
3678         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3679
3680         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3681                 summaryview->mlist =
3682                         g_slist_prepend(summaryview->mlist, msginfo);
3683
3684                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);
3685                 summary_set_row_marks(summaryview, node);
3686         }
3687 }
3688
3689 static void summary_execute_delete(SummaryView *summaryview)
3690 {
3691         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3692         GSList *cur;
3693
3694         /* search deleting messages and execute */
3695         gtk_ctree_pre_recursive
3696                 (ctree, NULL, summary_execute_delete_func, summaryview);
3697
3698         if (!summaryview->mlist) return;
3699
3700         folder_item_remove_msgs(summaryview->folder_item,
3701                                 summaryview->mlist);
3702
3703         for (cur = summaryview->mlist; cur != NULL && cur->data != NULL; cur = cur->next)
3704                 procmsg_msginfo_free((MsgInfo *)cur->data);
3705
3706         g_slist_free(summaryview->mlist);
3707         summaryview->mlist = NULL;
3708 }
3709
3710 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3711                                         gpointer data)
3712 {
3713         SummaryView *summaryview = data;
3714         MsgInfo *msginfo;
3715
3716         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3717
3718         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3719                 summaryview->mlist =
3720                         g_slist_append(summaryview->mlist, msginfo);
3721                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3722
3723                 if (msginfo->msgid && *msginfo->msgid &&
3724                     node == g_hash_table_lookup(summaryview->msgid_table,
3725                                                 msginfo->msgid)) {
3726                         g_hash_table_remove(summaryview->msgid_table,
3727                                             msginfo->msgid);
3728                 }       
3729                 if (msginfo->subject && *msginfo->subject && 
3730                     node == subject_table_lookup(summaryview->subject_table,
3731                                                  msginfo->subject)) {
3732                         subject_table_remove(summaryview->subject_table,
3733                                              msginfo->subject);
3734                 }                                           
3735         }
3736 }
3737
3738 /* thread functions */
3739
3740 void summary_thread_build(SummaryView *summaryview)
3741 {
3742         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3743         GtkCTreeNode *node;
3744         GtkCTreeNode *next;
3745         GtkCTreeNode *parent;
3746         MsgInfo *msginfo;
3747         GSList *reflist;
3748
3749         summary_lock(summaryview);
3750
3751         debug_print("Building threads...");
3752         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3753         main_window_cursor_wait(summaryview->mainwin);
3754
3755         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3756                                        G_CALLBACK(summary_tree_expanded), summaryview);
3757         gtk_clist_freeze(GTK_CLIST(ctree));
3758
3759         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3760         while (node) {
3761                 next = GTK_CTREE_ROW(node)->sibling;
3762
3763                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3764
3765                 parent = NULL;
3766
3767                 if (msginfo && msginfo->inreplyto) {
3768                         parent = g_hash_table_lookup(summaryview->msgid_table,
3769                                                      msginfo->inreplyto);
3770                                                      
3771                         if (!parent && msginfo->references) {
3772                                 for (reflist = msginfo->references;
3773                                      reflist != NULL; reflist = reflist->next)
3774                                         if ((parent = g_hash_table_lookup
3775                                                 (summaryview->msgid_table,
3776                                                  reflist->data)))
3777                                                 break;
3778                         }
3779                 }
3780
3781                 if (prefs_common.thread_by_subject && parent == NULL) {
3782                         parent = subject_table_lookup
3783                                 (summaryview->subject_table,
3784                                  msginfo->subject);
3785                 }
3786
3787                 if (parent && parent != node) {
3788                         gtk_ctree_move(ctree, node, parent, NULL);
3789                 }
3790
3791                 node = next;
3792         }
3793
3794         gtkut_ctree_set_focus_row(ctree, summaryview->selected);
3795
3796         gtk_clist_thaw(GTK_CLIST(ctree));
3797         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3798                                          G_CALLBACK(summary_tree_expanded), summaryview);
3799
3800         debug_print("done.\n");
3801         STATUSBAR_POP(summaryview->mainwin);
3802         main_window_cursor_normal(summaryview->mainwin);
3803
3804         summaryview->threaded = TRUE;
3805
3806         summary_unlock(summaryview);
3807 }
3808
3809 static void summary_thread_init(SummaryView *summaryview)
3810 {
3811         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3812         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3813         GtkCTreeNode *next;
3814
3815         if (!summaryview->thread_collapsed) {
3816                 g_signal_handlers_block_by_func(G_OBJECT(ctree),
3817                                        G_CALLBACK(summary_tree_expanded), summaryview);
3818                 while (node) {
3819                         next = GTK_CTREE_ROW(node)->sibling;
3820                         if (GTK_CTREE_ROW(node)->children)
3821                                 gtk_ctree_expand_recursive(ctree, node);
3822                         node = next;
3823                 }
3824                 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3825                                        G_CALLBACK(summary_tree_expanded), summaryview);
3826         } 
3827 }
3828
3829 void summary_unthread(SummaryView *summaryview)
3830 {
3831         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3832         GtkCTreeNode *node;
3833         GtkCTreeNode *child;
3834         GtkCTreeNode *sibling;
3835         GtkCTreeNode *next_child;
3836
3837         summary_lock(summaryview);
3838
3839         debug_print("Unthreading...");
3840         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
3841         main_window_cursor_wait(summaryview->mainwin);
3842         
3843         g_signal_handlers_block_by_func(G_OBJECT(ctree),
3844                                         summary_tree_collapsed, summaryview);
3845         gtk_clist_freeze(GTK_CLIST(ctree));
3846
3847         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3848              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3849                 child = GTK_CTREE_ROW(node)->children;
3850                 sibling = GTK_CTREE_ROW(node)->sibling;
3851
3852                 while (child != NULL) {
3853                         next_child = GTK_CTREE_ROW(child)->sibling;
3854                         gtk_ctree_move(ctree, child, NULL, sibling);
3855                         child = next_child;
3856                 }
3857         }
3858
3859         /* CLAWS: and sort it */
3860         gtk_sctree_sort_recursive(ctree, NULL); 
3861
3862         gtk_clist_thaw(GTK_CLIST(ctree));
3863         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3864                                            G_CALLBACK(summary_tree_collapsed), summaryview);
3865
3866         debug_print("done.\n");
3867         STATUSBAR_POP(summaryview->mainwin);
3868         main_window_cursor_normal(summaryview->mainwin);