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