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