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