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