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