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