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