83365a01bc3016033b8f2d212885182008bc675d
[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 gint 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                                 if (node == NULL)
1023                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1024                                                                      MSG_UNREAD, FALSE);
1025                                 break;
1026                         case SELECTONENTRY_UNREAD:
1027                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1028                                                                      MSG_UNREAD, FALSE);
1029                                 if (node == NULL)
1030                                         node = summary_find_next_flagged_msg(summaryview, NULL,
1031                                                                      MSG_NEW, FALSE);
1032                                 break;
1033                         default:
1034                                 node = NULL;
1035                 }
1036
1037                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1038                         node = gtk_ctree_node_nth
1039                                 (ctree,
1040                                  item->sort_type == SORT_DESCENDING
1041                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1042                 }
1043                 summary_unlock(summaryview);
1044                 summary_select_node(summaryview, node,
1045                                     prefs_common.always_show_msg,
1046                                     TRUE);
1047                 summary_lock(summaryview);
1048         }
1049
1050         summary_set_column_titles(summaryview);
1051         summary_status_show(summaryview);
1052         summary_set_menu_sensitive(summaryview);
1053         toolbar_main_set_sensitive(summaryview->mainwin);
1054
1055         debug_print("\n");
1056         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1057         STATUSBAR_POP(summaryview->mainwin);
1058         main_window_cursor_normal(summaryview->mainwin);
1059         summary_unlock(summaryview);
1060         inc_unlock();
1061
1062         return TRUE;
1063 }
1064
1065 #undef CURRENTLY_DISPLAYED
1066
1067
1068 void summary_clear_list(SummaryView *summaryview)
1069 {
1070         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1071         gint optimal_width;
1072
1073         gtk_clist_freeze(clist);
1074
1075         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1076                                 NULL, summary_free_msginfo_func, NULL);
1077
1078         if (summaryview->folder_item) {
1079                 summaryview->folder_item->opened = FALSE;
1080                 summaryview->folder_item = NULL;
1081         }
1082
1083         summaryview->display_msg = FALSE;
1084
1085         summaryview->selected = NULL;
1086         summaryview->displayed = NULL;
1087         summaryview->total_size = 0;
1088         summaryview->deleted = summaryview->moved = 0;
1089         summaryview->copied = 0;
1090         if (summaryview->msgid_table) {
1091                 g_hash_table_destroy(summaryview->msgid_table);
1092                 summaryview->msgid_table = NULL;
1093         }
1094         if (summaryview->subject_table) {
1095                 g_hash_table_destroy(summaryview->subject_table);
1096                 summaryview->subject_table = NULL;
1097         }
1098         summaryview->mlist = NULL;
1099
1100         gtk_clist_clear(clist);
1101         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1102                 optimal_width = gtk_clist_optimal_column_width
1103                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1104                 gtk_clist_set_column_width
1105                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1106                          optimal_width);
1107         }
1108
1109         gtk_clist_thaw(clist);
1110 }
1111
1112 void summary_clear_all(SummaryView *summaryview)
1113 {
1114         messageview_clear(summaryview->messageview);
1115         summary_clear_list(summaryview);
1116         summary_set_menu_sensitive(summaryview);
1117         toolbar_main_set_sensitive(summaryview->mainwin);
1118         summary_status_show(summaryview);
1119 }
1120
1121 void summary_lock(SummaryView *summaryview)
1122 {
1123         summaryview->lock_count++;
1124 }
1125
1126 void summary_unlock(SummaryView *summaryview)
1127 {
1128         if (summaryview->lock_count)
1129                 summaryview->lock_count--;
1130 }
1131
1132 gboolean summary_is_locked(SummaryView *summaryview)
1133 {
1134         return summaryview->lock_count > 0;
1135 }
1136
1137 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1138 {
1139         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1140         SummarySelection selection;
1141
1142         if (!clist->row_list)
1143                 selection = SUMMARY_NONE;
1144         else if (!clist->selection)
1145                 selection = SUMMARY_SELECTED_NONE;
1146         else if (!clist->selection->next)
1147                 selection = SUMMARY_SELECTED_SINGLE;
1148         else
1149                 selection = SUMMARY_SELECTED_MULTIPLE;
1150
1151         return selection;
1152 }
1153
1154 /*!
1155  *\return       MsgInfo * Selected message if there's one selected;
1156  *              if multiple selected, or none, return NULL.
1157  */ 
1158 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1159 {
1160         /* summaryview->selected may be valid when multiple 
1161          * messages were selected */
1162         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1163
1164         if (sellist == NULL || sellist->next) 
1165                 return NULL;
1166         
1167         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1168 }
1169
1170 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1171 {
1172         GSList *mlist = NULL;
1173         GList *cur;
1174         MsgInfo *msginfo;
1175
1176         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1177              cur = cur->next) {
1178                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1179                 mlist = g_slist_prepend(mlist, msginfo);
1180         }
1181
1182         mlist = g_slist_reverse(mlist);
1183
1184         return mlist;
1185 }
1186
1187 GSList *summary_get_msg_list(SummaryView *summaryview)
1188 {
1189         GSList *mlist = NULL;
1190         GtkCTree *ctree;
1191         GtkCTreeNode *node;
1192         MsgInfo *msginfo;
1193
1194         ctree = GTK_CTREE(summaryview->ctree);
1195
1196         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1197              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1198                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1199                 mlist = g_slist_prepend(mlist, msginfo);
1200         }
1201
1202         mlist = g_slist_reverse(mlist);
1203
1204         return mlist;
1205 }
1206
1207 static void summary_set_menu_sensitive(SummaryView *summaryview)
1208 {
1209         GtkItemFactory *ifactory = summaryview->popupfactory;
1210         SensitiveCond state;
1211         gboolean sensitive;
1212         GtkWidget *menuitem;
1213         gint i;
1214
1215         static const struct {
1216                 gchar *const entry;
1217                 SensitiveCond cond;
1218         } entry[] = {
1219                 {"/Reply"                       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1220                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1221                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1222                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1223                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1224                 {"/Follow-up and reply to"      , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1225
1226                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1227                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1228
1229                 {"/Re-edit"                     , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1230
1231                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1232                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1233                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1234                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1235                 {"/Cancel a news message"       , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1236
1237                 {"/Mark"                        , M_TARGET_EXIST},
1238                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1239                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1240                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1241                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1242                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1243                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1244                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1245                 {"/Color label"                 , M_TARGET_EXIST},
1246
1247                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1248                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1249                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1250
1251                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1252                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1253                 {"/View/Source"                 , M_SINGLE_TARGET_EXIST},
1254                 {"/View/All header"             , M_SINGLE_TARGET_EXIST},
1255 #if 0
1256                 {"/Save as..."                  , M_TARGET_EXIST|M_UNLOCKED},
1257                 {"/Print..."                    , M_TARGET_EXIST|M_UNLOCKED},
1258
1259                 {"/Select thread"               , M_SINGLE_TARGET_EXIST},
1260                 {"/Select all"                  , M_TARGET_EXIST},
1261 #endif
1262                 {NULL, 0}
1263         };
1264
1265         main_window_set_menu_sensitive(summaryview->mainwin);
1266
1267         state = main_window_get_current_state(summaryview->mainwin);
1268
1269         for (i = 0; entry[i].entry != NULL; i++) {
1270                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1271                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1272         }
1273
1274
1275         summary_lock(summaryview);
1276         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1277         gtk_check_menu_item_set_active
1278                 (GTK_CHECK_MENU_ITEM(menuitem),
1279                  summaryview->messageview->mimeview->textview->show_all_headers);
1280         summary_unlock(summaryview);
1281 }
1282
1283 void summary_select_prev_unread(SummaryView *summaryview)
1284 {
1285         GtkCTreeNode *node;
1286         gboolean skip_cur = FALSE;
1287
1288         if (summaryview->displayed 
1289         &&  summaryview->selected == summaryview->displayed) {
1290                 debug_print("skipping current\n");
1291                 skip_cur = TRUE;
1292         }
1293
1294         node = summary_find_prev_flagged_msg
1295                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1296
1297         if (!node) {
1298                 AlertValue val = 0;
1299
1300                 switch (prefs_common.next_unread_msg_dialog) {
1301                         case NEXTUNREADMSGDIALOG_ALWAYS:
1302                                 val = alertpanel(_("No more unread messages"),
1303                                                  _("No unread message found. "
1304                                                    "Search from the end?"),
1305                                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1306                                 break;
1307                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1308                                 val = G_ALERTDEFAULT;
1309                                 break;
1310                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1311                                 val = !G_ALERTDEFAULT;
1312                                 break;
1313                         default:
1314                                 debug_print(
1315                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1316                 }
1317                 if (val != G_ALERTDEFAULT) return;
1318                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1319                                                      MSG_UNREAD, FALSE);
1320         }
1321
1322         if (!node)
1323                 alertpanel_notice(_("No unread messages."));
1324         else
1325                 summary_select_node(summaryview, node, TRUE, FALSE);
1326 }
1327
1328 void summary_select_next_unread(SummaryView *summaryview)
1329 {
1330         GtkCTreeNode *node = summaryview->selected;
1331         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1332         gboolean skip_cur = FALSE;
1333         
1334         if (summaryview->displayed 
1335         &&  summaryview->selected == summaryview->displayed) {
1336                 debug_print("skipping cur (%p %p)\n",
1337                         summaryview->displayed, summaryview->selected);
1338                 skip_cur = TRUE;
1339         }
1340
1341
1342         node = summary_find_next_flagged_msg
1343                 (summaryview, node, MSG_UNREAD, skip_cur);
1344         
1345         if (node)
1346                 summary_select_node(summaryview, node, TRUE, FALSE);
1347         else {
1348                 node = summary_find_next_flagged_msg
1349                         (summaryview, NULL, MSG_UNREAD, FALSE);
1350                 if (node == NULL) {
1351                         AlertValue val = 0;
1352
1353                         switch (prefs_common.next_unread_msg_dialog) {
1354                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1355                                         val = alertpanel(_("No more unread messages"),
1356                                                          _("No unread message found. "
1357                                                            "Go to next folder?"),
1358                                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1359                                         break;
1360                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1361                                         val = G_ALERTDEFAULT;
1362                                         break;
1363                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1364                                         val = G_ALERTOTHER;
1365                                         break;
1366                                 default:
1367                                         debug_print(
1368                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1369                         }
1370
1371                         if (val == G_ALERTDEFAULT) {
1372                                 g_signal_stop_emission_by_name(G_OBJECT(ctree),
1373                                                          "key_press_event");
1374                                 folderview_select_next_unread(summaryview->folderview);
1375                                 return;
1376                         } 
1377                         else
1378                                 return;
1379                 } else
1380                         summary_select_node(summaryview, node, TRUE, FALSE);
1381
1382         }
1383 }
1384
1385 void summary_select_prev_new(SummaryView *summaryview)
1386 {
1387         GtkCTreeNode *node;
1388
1389         node = summary_find_prev_flagged_msg
1390                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1391
1392         if (!node) {
1393                 AlertValue val;
1394
1395                 val = alertpanel(_("No more new messages"),
1396                                  _("No new message found. "
1397                                    "Search from the end?"),
1398                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1399                 if (val != G_ALERTDEFAULT) return;
1400                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1401                                                      MSG_NEW, FALSE);
1402         }
1403
1404         if (!node)
1405                 alertpanel_notice(_("No new messages."));
1406         else
1407                 summary_select_node(summaryview, node, TRUE, FALSE);
1408 }
1409
1410 void summary_select_next_new(SummaryView *summaryview)
1411 {
1412         GtkCTreeNode *node = summaryview->selected;
1413         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1414
1415         while ((node = summary_find_next_flagged_msg
1416                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1417                 AlertValue val;
1418
1419                 val = alertpanel(_("No more new messages"),
1420                                  _("No new message found. "
1421                                    "Go to next folder?"),
1422                                  GTK_STOCK_YES, _("_Search again"),
1423                                  GTK_STOCK_NO);
1424                 if (val == G_ALERTDEFAULT) {
1425                         g_signal_stop_emission_by_name(G_OBJECT(ctree),"key_press_event");
1426                         folderview_select_next_unread(summaryview->folderview);
1427                         return;
1428                 } else if (val == G_ALERTALTERNATE)
1429                         node = NULL;
1430                 else
1431                         return;
1432         }
1433
1434         if (node)
1435                 summary_select_node(summaryview, node, TRUE, FALSE);
1436 }
1437
1438 void summary_select_prev_marked(SummaryView *summaryview)
1439 {
1440         GtkCTreeNode *node;
1441
1442         node = summary_find_prev_flagged_msg
1443                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1444
1445         if (!node) {
1446                 AlertValue val;
1447
1448                 val = alertpanel(_("No more marked messages"),
1449                                  _("No marked message found. "
1450                                    "Search from the end?"),
1451                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1452                 if (val != G_ALERTDEFAULT) return;
1453                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1454                                                      MSG_MARKED, TRUE);
1455         }
1456
1457         if (!node)
1458                 alertpanel_notice(_("No marked messages."));
1459         else
1460                 summary_select_node(summaryview, node, TRUE, FALSE);
1461 }
1462
1463 void summary_select_next_marked(SummaryView *summaryview)
1464 {
1465         GtkCTreeNode *node;
1466
1467         node = summary_find_next_flagged_msg
1468                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1469
1470         if (!node) {
1471                 AlertValue val;
1472
1473                 val = alertpanel(_("No more marked messages"),
1474                                  _("No marked message found. "
1475                                    "Search from the beginning?"),
1476                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1477                 if (val != G_ALERTDEFAULT) return;
1478                 node = summary_find_next_flagged_msg(summaryview, NULL,
1479                                                      MSG_MARKED, TRUE);
1480         }
1481
1482         if (!node)
1483                 alertpanel_notice(_("No marked messages."));
1484         else
1485                 summary_select_node(summaryview, node, TRUE, FALSE);
1486 }
1487
1488 void summary_select_prev_labeled(SummaryView *summaryview)
1489 {
1490         GtkCTreeNode *node;
1491
1492         node = summary_find_prev_flagged_msg
1493                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1494
1495         if (!node) {
1496                 AlertValue val;
1497
1498                 val = alertpanel(_("No more labeled messages"),
1499                                  _("No labeled message found. "
1500                                    "Search from the end?"),
1501                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1502                 if (val != G_ALERTDEFAULT) return;
1503                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1504                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1505         }
1506
1507         if (!node)
1508                 alertpanel_notice(_("No labeled messages."));
1509         else
1510                 summary_select_node(summaryview, node, TRUE, FALSE);
1511 }
1512
1513 void summary_select_next_labeled(SummaryView *summaryview)
1514 {
1515         GtkCTreeNode *node;
1516
1517         node = summary_find_next_flagged_msg
1518                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1519
1520         if (!node) {
1521                 AlertValue val;
1522
1523                 val = alertpanel(_("No more labeled messages"),
1524                                  _("No labeled message found. "
1525                                    "Search from the beginning?"),
1526                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1527                 if (val != G_ALERTDEFAULT) return;
1528                 node = summary_find_next_flagged_msg(summaryview, NULL,
1529                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1530         }
1531
1532         if (!node)
1533                 alertpanel_notice(_("No labeled messages."));
1534         else
1535                 summary_select_node(summaryview, node, TRUE, FALSE);
1536 }
1537
1538 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1539 {
1540         GtkCTreeNode *node;
1541
1542         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1543         summary_select_node(summaryview, node, FALSE, TRUE);
1544 }
1545
1546 /**
1547  * summary_select_node:
1548  * @summaryview: Summary view.
1549  * @node: Summary tree node.
1550  * @display_msg: TRUE to display the selected message.
1551  * @do_refresh: TRUE to refresh the widget.
1552  *
1553  * Select @node (bringing it into view by scrolling and expanding its
1554  * thread, if necessary) and unselect all others.  If @display_msg is
1555  * TRUE, display the corresponding message in the message view.
1556  * If @do_refresh is TRUE, the widget is refreshed.
1557  **/
1558 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1559                          gboolean display_msg, gboolean do_refresh)
1560 {
1561         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1562
1563         if (node) {
1564                 gtkut_ctree_expand_parent_all(ctree, node);
1565                 if (do_refresh) {
1566                         GTK_EVENTS_FLUSH();
1567                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1568                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1569                 }
1570                 summary_unselect_all(summaryview);
1571                 if (display_msg && summaryview->displayed == node)
1572                         summaryview->displayed = NULL;
1573                 summaryview->display_msg = display_msg;
1574                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1575                 if (summaryview->selected == NULL)
1576                         summaryview->selected = node;
1577         }
1578 }
1579
1580 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1581 {
1582         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1583         MsgInfo *msginfo;
1584
1585         if (!node)
1586                 return 0;
1587         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1588         return msginfo->msgnum;
1589 }
1590
1591 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1592                                            GtkCTreeNode *current_node)
1593 {
1594         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1595         GtkCTreeNode *node;
1596         MsgInfo *msginfo;
1597
1598         if (current_node)
1599                 node = current_node;
1600         else
1601                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1602
1603         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1604                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1605                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1606         }
1607
1608         return node;
1609 }
1610
1611 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1612                                            GtkCTreeNode *current_node)
1613 {
1614         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1615         GtkCTreeNode *node;
1616         MsgInfo *msginfo;
1617
1618         if (current_node)
1619                 node = current_node;
1620         else
1621                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1622
1623         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1624                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1625                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
1626         }
1627
1628         return node;
1629 }
1630
1631 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1632                                                    GtkCTreeNode *current_node,
1633                                                    MsgPermFlags flags,
1634                                                    gboolean start_from_prev)
1635 {
1636         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1637         GtkCTreeNode *node;
1638         MsgInfo *msginfo;
1639
1640         if (current_node) {
1641                 if (start_from_prev)
1642                         node = GTK_CTREE_NODE_PREV(current_node);
1643                 else
1644                         node = current_node;
1645         } else
1646                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1647
1648         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1649                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1650                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
1651         }
1652
1653         return node;
1654 }
1655
1656 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1657                                                    GtkCTreeNode *current_node,
1658                                                    MsgPermFlags flags,
1659                                                    gboolean start_from_next)
1660 {
1661         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1662         GtkCTreeNode *node;
1663         MsgInfo *msginfo;
1664
1665         if (current_node) {
1666                 if (start_from_next)
1667                         node = gtkut_ctree_node_next(ctree, current_node);
1668                 else
1669                         node = current_node;
1670         } else
1671                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1672
1673         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1674                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1675                 /* Find msg with matching flags but ignore messages with
1676                    ignore flags, if searching for new or unread messages */
1677                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1678                         (msginfo && (msginfo->flags.perm_flags & flags) != 0))
1679                         break;
1680         }
1681
1682         return node;
1683 }
1684
1685 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1686                                                 guint msgnum)
1687 {
1688         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1689         GtkCTreeNode *node;
1690         MsgInfo *msginfo;
1691
1692         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1693
1694         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1695                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1696                 if (msginfo && msginfo->msgnum == msgnum) break;
1697         }
1698
1699         return node;
1700 }
1701
1702 static guint attract_hash_func(gconstpointer key)
1703 {
1704         gchar *str;
1705         gchar *p;
1706         guint h;
1707
1708         Xstrdup_a(str, (const gchar *)key, return 0);
1709         trim_subject(str);
1710
1711         p = str;
1712         h = *p;
1713
1714         if (h) {
1715                 for (p += 1; *p != '\0'; p++)
1716                         h = (h << 5) - h + *p;
1717         }
1718
1719         return h;
1720 }
1721
1722 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1723 {
1724         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1725 }
1726
1727 void summary_attract_by_subject(SummaryView *summaryview)
1728 {
1729         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1730         GtkCList *clist = GTK_CLIST(ctree);
1731         GtkCTreeNode *src_node;
1732         GtkCTreeNode *dst_node, *sibling;
1733         GtkCTreeNode *tmp;
1734         MsgInfo *src_msginfo, *dst_msginfo;
1735         GHashTable *subject_table;
1736
1737         debug_print("Attracting messages by subject...");
1738         STATUSBAR_PUSH(summaryview->mainwin,
1739                        _("Attracting messages by subject..."));
1740
1741         main_window_cursor_wait(summaryview->mainwin);
1742         gtk_clist_freeze(clist);
1743
1744         subject_table = g_hash_table_new(attract_hash_func,
1745                                          attract_compare_func);
1746
1747         for (src_node = GTK_CTREE_NODE(clist->row_list);
1748              src_node != NULL;
1749              src_node = tmp) {
1750                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1751                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1752                 if (!src_msginfo) continue;
1753                 if (!src_msginfo->subject) continue;
1754
1755                 /* find attracting node */
1756                 dst_node = g_hash_table_lookup(subject_table,
1757                                                src_msginfo->subject);
1758
1759                 if (dst_node) {
1760                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1761
1762                         /* if the time difference is more than 20 days,
1763                            don't attract */
1764                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1765                             > 60 * 60 * 24 * 20)
1766                                 continue;
1767
1768                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1769                         if (src_node != sibling)
1770                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1771                 }
1772
1773                 g_hash_table_insert(subject_table,
1774                                     src_msginfo->subject, src_node);
1775         }
1776
1777         g_hash_table_destroy(subject_table);
1778
1779         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1780
1781         gtk_clist_thaw(clist);
1782
1783         debug_print("done.\n");
1784         STATUSBAR_POP(summaryview->mainwin);
1785
1786         main_window_cursor_normal(summaryview->mainwin);
1787 }
1788
1789 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1790                                       gpointer data)
1791 {
1792         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1793
1794         if (msginfo)
1795                 procmsg_msginfo_free(msginfo);
1796 }
1797
1798 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1799                                    gpointer data)
1800 {
1801         SummaryView *summaryview = data;
1802         MsgInfo *msginfo;
1803
1804         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1805
1806         if (msginfo->folder && msginfo->folder->folder &&
1807             msginfo->folder->folder->klass->type == F_NEWS)
1808                 news_flag_crosspost(msginfo);
1809
1810         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1811         && procmsg_msg_has_marked_parent(msginfo))
1812                 summaryview->unreadmarked++;
1813
1814         if (MSG_IS_DELETED(msginfo->flags))
1815                 summaryview->deleted++;
1816
1817         summaryview->total_size += msginfo->size;
1818
1819         summary_set_row_marks(summaryview, node);
1820 }
1821
1822 static void summary_update_status(SummaryView *summaryview)
1823 {
1824         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1825         GtkCTreeNode *node;
1826         MsgInfo *msginfo;
1827
1828         summaryview->total_size =
1829         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1830
1831         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1832              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1833                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1834
1835                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1836                 && procmsg_msg_has_marked_parent(msginfo))
1837                         summaryview->unreadmarked++;
1838                 if (MSG_IS_DELETED(msginfo->flags))
1839                         summaryview->deleted++;
1840                 if (MSG_IS_MOVE(msginfo->flags))
1841                         summaryview->moved++;
1842                 if (MSG_IS_COPY(msginfo->flags))
1843                         summaryview->copied++;
1844                 summaryview->total_size += msginfo->size;
1845         }
1846 }
1847
1848 static void summary_status_show(SummaryView *summaryview)
1849 {
1850         gchar *str;
1851         gchar *del, *mv, *cp;
1852         gchar *sel;
1853         gchar *spc;
1854         gchar *itstr;
1855         GList *rowlist, *cur;
1856         guint n_selected = 0;
1857         off_t sel_size = 0;
1858         MsgInfo *msginfo;
1859         gchar *name;
1860         
1861         if (!summaryview->folder_item) {
1862                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
1863                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
1864                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
1865                 return;
1866         }
1867
1868         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1869         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
1870                 msginfo = gtk_ctree_node_get_row_data
1871                         (GTK_CTREE(summaryview->ctree),
1872                          GTK_CTREE_NODE(cur->data));
1873                 if (!msginfo)
1874                         g_warning("summary_status_show(): msginfo == NULL\n");
1875                 else {
1876                         sel_size += msginfo->size;
1877                         n_selected++;
1878                 }
1879         }
1880
1881         name = folder_item_get_name(summaryview->folder_item);
1882         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
1883         g_free(name);
1884
1885         if (summaryview->deleted)
1886                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1887         else
1888                 del = g_strdup("");
1889         if (summaryview->moved)
1890                 mv = g_strdup_printf(_("%s%d moved"),
1891                                      summaryview->deleted ? _(", ") : "",
1892                                      summaryview->moved);
1893         else
1894                 mv = g_strdup("");
1895         if (summaryview->copied)
1896                 cp = g_strdup_printf(_("%s%d copied"),
1897                                      summaryview->deleted ||
1898                                      summaryview->moved ? _(", ") : "",
1899                                      summaryview->copied);
1900         else
1901                 cp = g_strdup("");
1902
1903         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1904                 spc = "    ";
1905         else
1906                 spc = "";
1907
1908         if (n_selected) {
1909                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1910                 if (n_selected == 1)
1911                         itstr = g_strdup(_(" item selected"));
1912                 else
1913                         itstr = g_strdup(_(" items selected"));
1914         } else {
1915                 sel = g_strdup("");
1916                 itstr = g_strdup("");
1917         }
1918                 
1919         str = g_strconcat(n_selected ? itos(n_selected) : "",
1920                                         itstr, sel, spc, del, mv, cp, NULL);
1921         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
1922         g_free(str);
1923         g_free(sel);
1924         g_free(del);
1925         g_free(mv);
1926         g_free(cp);
1927         g_free(itstr);
1928
1929         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1930
1931                                       summaryview->folder_item->new_msgs,
1932                                       summaryview->folder_item->unread_msgs,
1933                                       summaryview->folder_item->total_msgs,
1934                                       to_human_readable(summaryview->total_size));
1935         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
1936         g_free(str);
1937 }
1938
1939 static void summary_set_column_titles(SummaryView *summaryview)
1940 {
1941         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1942         GtkWidget *hbox;
1943         GtkWidget *label;
1944         GtkWidget *arrow;
1945         gint pos;
1946         const gchar *title;
1947         SummaryColumnType type;
1948         gboolean single_char;
1949         GtkJustification justify;
1950
1951         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1952                 SORT_BY_MARK,
1953                 SORT_BY_STATUS,
1954                 SORT_BY_MIME,
1955                 SORT_BY_SUBJECT,
1956                 SORT_BY_FROM,
1957                 SORT_BY_TO,
1958                 SORT_BY_DATE,
1959                 SORT_BY_SIZE,
1960                 SORT_BY_NUMBER,
1961                 SORT_BY_SCORE,
1962                 SORT_BY_LOCKED
1963         };
1964
1965         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1966                 type = summaryview->col_state[pos].type;
1967
1968                 /* CLAWS: mime and unread are single char headers */
1969                 single_char = (type == S_COL_MIME || type == S_COL_STATUS);
1970                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1971                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1972
1973                 switch (type) {
1974                 case S_COL_SUBJECT:
1975                 case S_COL_FROM:
1976                 case S_COL_TO:
1977                 case S_COL_DATE:
1978                 case S_COL_NUMBER:
1979                         if (prefs_common.trans_hdr)
1980                                 title = gettext(col_label[type]);
1981                         else
1982                                 title = col_label[type];
1983                         break;
1984                 /* CLAWS: dummies for mark and locked headers */        
1985                 case S_COL_MARK:        
1986                 case S_COL_LOCKED:
1987                         title = "";
1988                         break;
1989                 default:
1990                         title = gettext(col_label[type]);
1991                 }
1992
1993                 if (type == S_COL_MIME) {
1994                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
1995                         gtk_widget_show(label);
1996                         gtk_clist_set_column_widget(clist, pos, label);
1997                         continue;
1998                 }
1999                 if (single_char) {
2000                         gtk_clist_set_column_title(clist, pos, title);
2001                         continue;
2002                 }
2003
2004                 /* CLAWS: changed so that locked and mark headers
2005                  * show a pixmap instead of single character */
2006                 hbox  = gtk_hbox_new(FALSE, 4);
2007                 
2008                 if (type == S_COL_LOCKED)
2009                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
2010                 else if (type == S_COL_MARK) 
2011                         label = gtk_pixmap_new(markxpm, markxpmmask);
2012                 else 
2013                         label = gtk_label_new(title);
2014                 
2015                 if (justify == GTK_JUSTIFY_RIGHT)
2016                         gtk_box_pack_end(GTK_BOX(hbox), label,
2017                                          FALSE, FALSE, 0);
2018                 else
2019                         gtk_box_pack_start(GTK_BOX(hbox), label,
2020                                            FALSE, FALSE, 0);
2021
2022                 if (summaryview->sort_key == sort_by[type]) {
2023                         arrow = gtk_arrow_new
2024                                 (summaryview->sort_type == SORT_ASCENDING
2025                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2026                                  GTK_SHADOW_IN);
2027                         if (justify == GTK_JUSTIFY_RIGHT)
2028                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2029                                                    FALSE, FALSE, 0);
2030                         else
2031                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2032                                                  FALSE, FALSE, 0);
2033                 }
2034
2035                 gtk_widget_show_all(hbox);
2036                 gtk_clist_set_column_widget(clist, pos, hbox);
2037         }
2038 }
2039
2040 void summary_reflect_prefs(void)
2041 {
2042         SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
2043         bold_style = bold_marked_style = bold_deleted_style = 
2044                 small_style = small_marked_style = small_deleted_style = NULL;
2045         summary_set_fonts(summaryview);
2046         summary_set_column_titles(summaryview);
2047         summary_show(summaryview, summaryview->folder_item);
2048 }
2049
2050 void summary_sort(SummaryView *summaryview,
2051                   FolderSortKey sort_key, FolderSortType sort_type)
2052 {
2053         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2054         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2055         GtkCListCompareFunc cmp_func = NULL;
2056         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2057                                        G_CALLBACK(summary_tree_expanded), summaryview);
2058         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
2059
2060         switch (sort_key) {
2061         case SORT_BY_MARK:
2062                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2063                 break;
2064         case SORT_BY_STATUS:
2065                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2066                 break;
2067         case SORT_BY_MIME:
2068                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2069                 break;
2070         case SORT_BY_NUMBER:
2071                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2072                 break;
2073         case SORT_BY_SIZE:
2074                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2075                 break;
2076         case SORT_BY_DATE:
2077                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2078                 break;
2079         case SORT_BY_FROM:
2080                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2081                 break;
2082         case SORT_BY_SUBJECT:
2083                 if (summaryview->simplify_subject_preg)
2084                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2085                 else
2086                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2087                 break;
2088         case SORT_BY_SCORE:
2089                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2090                 break;
2091         case SORT_BY_LABEL:
2092                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2093                 break;
2094         case SORT_BY_TO:
2095                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2096                 break;
2097         case SORT_BY_LOCKED:
2098                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2099                 break;
2100         case SORT_BY_NONE:
2101                 break;
2102         default:
2103                 goto unlock;
2104         }
2105
2106         summaryview->sort_key = sort_key;
2107         summaryview->sort_type = sort_type;
2108
2109         summary_set_column_titles(summaryview);
2110         summary_set_menu_sensitive(summaryview);
2111
2112         /* allow fallback to don't sort */
2113         if (summaryview->sort_key == SORT_BY_NONE)
2114                 goto unlock;
2115
2116         if (cmp_func != NULL) {
2117                 debug_print("Sorting summary...");
2118                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2119
2120                 main_window_cursor_wait(summaryview->mainwin);
2121
2122                 gtk_clist_freeze(clist);
2123                 gtk_clist_set_compare_func(clist, cmp_func);
2124
2125                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2126
2127                 gtk_sctree_sort_recursive(ctree, NULL);
2128
2129                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
2130
2131                 main_window_cursor_normal(summaryview->mainwin);
2132                 gtk_clist_thaw(clist);
2133
2134                 debug_print("done.\n");
2135                 STATUSBAR_POP(summaryview->mainwin);
2136         }
2137 unlock:
2138         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2139         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2140                                        G_CALLBACK(summary_tree_expanded), summaryview);
2141 }
2142
2143 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2144                                    GtkCTreeNode *cnode, gpointer data)
2145 {
2146         SummaryView *summaryview = (SummaryView *)data;
2147         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2148         gchar *text[N_SUMMARY_COLS];
2149         gint *col_pos = summaryview->col_pos;
2150         const gchar *msgid = msginfo->msgid;
2151         GHashTable *msgid_table = summaryview->msgid_table;
2152
2153         summary_set_header(summaryview, text, msginfo);
2154
2155         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2156                                 NULL, NULL, NULL, NULL, FALSE,
2157                                 gnode->parent->parent ? TRUE : FALSE);
2158 #define SET_TEXT(col) \
2159         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
2160                                 text[col_pos[col]])
2161
2162         SET_TEXT(S_COL_NUMBER);
2163         SET_TEXT(S_COL_SCORE);
2164         SET_TEXT(S_COL_SIZE);
2165         SET_TEXT(S_COL_DATE);
2166         SET_TEXT(S_COL_FROM);
2167         SET_TEXT(S_COL_TO);
2168         SET_TEXT(S_COL_SUBJECT);
2169
2170 #undef SET_TEXT
2171
2172         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2173         summary_set_marks_func(ctree, cnode, summaryview);
2174
2175         if (msgid && msgid[0] != '\0')
2176                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2177
2178         return TRUE;
2179 }
2180
2181 static void summary_set_ctree_from_list(SummaryView *summaryview,
2182                                         GSList *mlist)
2183 {
2184         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2185         MsgInfo *msginfo;
2186         GtkCTreeNode *node = NULL;
2187         GHashTable *msgid_table;
2188         GHashTable *subject_table;
2189         GSList * cur;
2190         START_TIMING("summary_set_ctree_from_list");
2191         
2192         if (!mlist) return;
2193
2194         debug_print("\tSetting summary from message data...");
2195         STATUSBAR_PUSH(summaryview->mainwin,
2196                        _("Setting summary from message data..."));
2197         gdk_flush();
2198
2199         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2200                                        G_CALLBACK(summary_tree_expanded), summaryview);
2201
2202         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2203         summaryview->msgid_table = msgid_table;
2204         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2205         summaryview->subject_table = subject_table;
2206
2207         if (prefs_common.use_addr_book)
2208                 start_address_completion();
2209         
2210         for (cur = mlist ; cur != NULL && cur->data != NULL; cur = cur->next) {
2211                 msginfo = (MsgInfo *)cur->data;
2212                 msginfo->threadscore = msginfo->score;
2213         }
2214
2215         if (summaryview->threaded) {
2216                 GNode *root, *gnode;
2217
2218                 root = procmsg_get_thread_tree(mlist);
2219
2220                 for (gnode = root->children; gnode != NULL;
2221                      gnode = gnode->next) {
2222                         node = gtk_ctree_insert_gnode
2223                                 (ctree, NULL, node, gnode,
2224                                  summary_insert_gnode_func, summaryview);
2225                 }
2226
2227                 g_node_destroy(root);
2228                 
2229                 summary_thread_init(summaryview);
2230         } else {
2231                 gchar *text[N_SUMMARY_COLS];
2232                 cur = mlist;
2233                 for (; mlist != NULL; mlist = mlist->next) {
2234                         msginfo = (MsgInfo *)mlist->data;
2235
2236                         summary_set_header(summaryview, text, msginfo);
2237
2238                         node = gtk_ctree_insert_node
2239                                 (ctree, NULL, node, text, 2,
2240                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2241                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2242                         summary_set_marks_func(ctree, node, summaryview);
2243
2244                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2245                                 g_hash_table_insert(msgid_table,
2246                                                     msginfo->msgid, node);
2247
2248                         subject_table_insert(subject_table,
2249                                              msginfo->subject,
2250                                              node);
2251                 }
2252                 mlist = cur;
2253         }
2254
2255         if (prefs_common.enable_hscrollbar &&
2256             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2257                 gint optimal_width;
2258
2259                 optimal_width = gtk_clist_optimal_column_width
2260                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2261                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2262                                            summaryview->col_pos[S_COL_SUBJECT],
2263                                            optimal_width);
2264         }
2265
2266         if (prefs_common.use_addr_book)
2267                 end_address_completion();
2268
2269         debug_print("done.\n");
2270         STATUSBAR_POP(summaryview->mainwin);
2271         if (debug_get_mode()) {
2272                 debug_print("\tmsgid hash table size = %d\n",
2273                             g_hash_table_size(msgid_table));
2274                 debug_print("\tsubject hash table size = %d\n",
2275                             g_hash_table_size(subject_table));
2276         }
2277
2278         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2279
2280         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2281
2282         while (prefs_common.bold_unread && node) {
2283                 GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2284                 if (GTK_CTREE_ROW(node)->children)
2285                         summary_set_row_marks(summaryview, node);
2286                 node = next;
2287         }
2288
2289         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2290                                        G_CALLBACK(summary_tree_expanded), summaryview);
2291         END_TIMING();
2292 }
2293
2294 static gchar *summary_complete_address(const gchar *addr)
2295 {
2296         gint count;
2297         gchar *res, *tmp, *email_addr;
2298         
2299         if (addr == NULL)
2300                 return NULL;
2301
2302         Xstrdup_a(email_addr, addr, return NULL);
2303         extract_address(email_addr);
2304         g_return_val_if_fail(*email_addr, NULL);
2305
2306         /*
2307          * completion stuff must be already initialized
2308          */
2309         res = NULL;
2310         if (1 < (count = complete_address(email_addr))) {
2311                 tmp = get_complete_address(1);
2312                 res = procheader_get_fromname(tmp);
2313                 g_free(tmp);
2314         }
2315
2316         return res;
2317 }
2318
2319 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2320                                MsgInfo *msginfo)
2321 {
2322         static gchar date_modified[80];
2323         static gchar col_score[11];
2324         static gchar buf[BUFFSIZE];
2325         gint *col_pos = summaryview->col_pos;
2326         FolderType ftype = F_UNKNOWN;
2327         gchar *from_text = NULL, *to_text = NULL;
2328         gboolean should_swap = FALSE;
2329
2330         text[col_pos[S_COL_MARK]]   = NULL;
2331         text[col_pos[S_COL_STATUS]] = NULL;
2332         text[col_pos[S_COL_MIME]]   = NULL;
2333         text[col_pos[S_COL_LOCKED]] = NULL;
2334         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2335         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2336         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2337
2338         if (msginfo->date_t) {
2339                 procheader_date_get_localtime(date_modified,
2340                                               sizeof(date_modified),
2341                                               msginfo->date_t);
2342                 text[col_pos[S_COL_DATE]] = date_modified;
2343         } else if (msginfo->date)
2344                 text[col_pos[S_COL_DATE]] = msginfo->date;
2345         else
2346                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2347
2348         if (ftype != F_NEWS && prefs_common.swap_from && msginfo->from && msginfo->to) {
2349                 gchar *addr = NULL;
2350                 
2351                 addr = g_strdup(msginfo->from);
2352
2353                 if (addr) {
2354                         extract_address(addr);
2355                         if (account_find_from_address(addr)) {
2356                                 should_swap = TRUE;
2357                         }
2358                         g_free(addr);
2359                 }
2360         }
2361
2362         if (!prefs_common.use_addr_book) {
2363                 from_text = msginfo->fromname ? 
2364                                 msginfo->fromname :
2365                                 _("(No From)");
2366         } else {
2367                 gchar *tmp = summary_complete_address(msginfo->from);
2368                 from_text = tmp ? tmp : (msginfo->fromname ?
2369                                          msginfo->fromname: 
2370                                                 _("(No From)"));
2371         }
2372         
2373         to_text = msginfo->to ? msginfo->to : 
2374                    (msginfo->cc ? msginfo->cc :
2375                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2376                      )
2377                    );
2378
2379         if (!should_swap) {
2380                 text[col_pos[S_COL_FROM]] = from_text;
2381                 text[col_pos[S_COL_TO]] = to_text;
2382         } else {
2383                 gchar *tmp = NULL;
2384                 tmp = g_strconcat("-->", to_text, NULL);
2385                 text[col_pos[S_COL_FROM]] = tmp;
2386                 tmp = g_strconcat("<--", from_text, NULL);
2387                 text[col_pos[S_COL_TO]] = tmp;
2388         }
2389         
2390         if (msginfo->folder && msginfo->folder->folder)
2391                 ftype = msginfo->folder->folder->klass->type; 
2392                 
2393         if (summaryview->simplify_subject_preg != NULL)
2394                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2395                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2396                                         summaryview->simplify_subject_preg) : 
2397                         _("(No Subject)");
2398         else 
2399                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2400                         _("(No Subject)");
2401 }
2402
2403 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2404 {
2405         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2406 }
2407
2408 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
2409                                       GtkCTreeNode *row)
2410 {
2411         g_return_if_fail(summaryview != NULL);
2412         g_return_if_fail(msginfo != NULL);
2413         g_return_if_fail(row != NULL);
2414
2415         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags) 
2416         && procmsg_msg_has_marked_parent(msginfo))
2417                 summaryview->unreadmarked--;
2418         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2419                 procmsg_msginfo_unset_flags
2420                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
2421                 summary_set_row_marks(summaryview, row);
2422                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
2423                 summary_status_show(summaryview);
2424         }
2425 }
2426
2427 typedef struct  {
2428         MsgInfo *msginfo;
2429         SummaryView *summaryview;
2430         GtkCTreeNode *row;
2431 } MarkAsReadData;
2432
2433 static int msginfo_mark_as_read_timeout(void *data)
2434 {
2435         MarkAsReadData *mdata = (MarkAsReadData *)data;
2436         if (!mdata)
2437                 return FALSE;
2438         
2439         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
2440                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
2441                                      mdata->row); 
2442
2443         g_free(mdata);
2444
2445         return FALSE;   
2446 }
2447
2448 static void summary_display_msg_full(SummaryView *summaryview,
2449                                      GtkCTreeNode *row,
2450                                      gboolean new_window, gboolean all_headers)
2451 {
2452         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2453         MsgInfo *msginfo;
2454         gint val;
2455
2456         if (!new_window) {
2457                 if (summaryview->displayed == row)
2458                         return;
2459                 else
2460                         summaryview->messageview->filtered = FALSE;
2461         }                       
2462         
2463         g_return_if_fail(row != NULL);
2464
2465         if (summary_is_locked(summaryview)) return;
2466         summary_lock(summaryview);
2467
2468         STATUSBAR_POP(summaryview->mainwin);
2469         GTK_EVENTS_FLUSH();
2470
2471         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2472
2473         if (new_window) {
2474                 MessageView *msgview;
2475
2476                 msgview = messageview_create_with_new_window(summaryview->mainwin);
2477                 val = messageview_show(msgview, msginfo, all_headers);
2478         } else {
2479                 MessageView *msgview;
2480
2481                 msgview = summaryview->messageview;
2482
2483                 summaryview->displayed = row;
2484                 if (!messageview_is_visible(msgview)) {
2485                         main_window_toggle_message_view(summaryview->mainwin);
2486                         GTK_EVENTS_FLUSH();
2487                 }
2488                 val = messageview_show(msgview, msginfo, all_headers);
2489                 if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
2490                         gtk_widget_grab_focus(summaryview->ctree);
2491                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2492         }
2493
2494         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
2495                 if (prefs_common.mark_as_read_delay) {
2496                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
2497                         data->summaryview = summaryview;
2498                         data->msginfo = msginfo;
2499                         data->row = row;
2500                         gtk_timeout_add(prefs_common.mark_as_read_delay * 1000,
2501                                 msginfo_mark_as_read_timeout, data);
2502                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
2503                         msginfo_mark_as_read(summaryview, msginfo, row);
2504                 }
2505         }
2506
2507         summary_set_menu_sensitive(summaryview);
2508         toolbar_main_set_sensitive(summaryview->mainwin);
2509         messageview_set_menu_sensitive(summaryview->messageview);
2510
2511         summary_unlock(summaryview);
2512 }
2513
2514 void summary_display_msg_selected(SummaryView *summaryview,
2515                                   gboolean all_headers)
2516 {
2517         if (summary_is_locked(summaryview)) return;
2518         summaryview->displayed = NULL;
2519         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2520                                  all_headers);
2521 }
2522
2523 void summary_redisplay_msg(SummaryView *summaryview)
2524 {
2525         GtkCTreeNode *node;
2526
2527         if (summaryview->displayed) {
2528                 node = summaryview->displayed;
2529                 summaryview->displayed = NULL;
2530                 summary_display_msg(summaryview, node);
2531         }
2532 }
2533
2534 void summary_open_msg(SummaryView *summaryview)
2535 {
2536         if (!summaryview->selected) return;
2537         
2538         /* CLAWS: if separate message view, don't open a new window
2539          * but rather use the current separated message view */
2540         summary_display_msg_full(summaryview, summaryview->selected,
2541                                  prefs_common.sep_msg ? FALSE : TRUE, 
2542                                  FALSE);
2543 }
2544
2545 void summary_view_source(SummaryView * summaryview)
2546 {
2547         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2548         MsgInfo *msginfo;
2549         SourceWindow *srcwin;
2550
2551         if (!summaryview->selected) return;
2552
2553         srcwin = source_window_create();
2554         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2555         source_window_show_msg(srcwin, msginfo);
2556         source_window_show(srcwin);
2557 }
2558
2559 void summary_reedit(SummaryView *summaryview)
2560 {
2561         MsgInfo *msginfo;
2562
2563         if (!summaryview->selected) return;
2564         if (!summaryview->folder_item) return;
2565         if (!folder_has_parent_of_type(summaryview->folder_item, F_OUTBOX)
2566         &&  !folder_has_parent_of_type(summaryview->folder_item, F_DRAFT)
2567         &&  !folder_has_parent_of_type(summaryview->folder_item, F_QUEUE))
2568                 return;
2569
2570         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2571                                               summaryview->selected);
2572         if (!msginfo) return;
2573
2574         compose_reedit(msginfo);
2575 }
2576
2577 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
2578 {
2579         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2580         GtkCTreeNode *node;
2581
2582         if (summary_is_locked(summaryview)) return FALSE;
2583         if (type == GTK_SCROLL_STEP_FORWARD) {
2584                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2585                 if (node)
2586                         gtkut_ctree_expand_parent_all(ctree, node);
2587                 else
2588                         return FALSE;
2589         } else {
2590                 if (summaryview->selected) {
2591                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2592                         if (!node) return FALSE;
2593                 }
2594         }
2595
2596         if (messageview_is_visible(summaryview->messageview))
2597                 summaryview->display_msg = TRUE;
2598
2599         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
2600
2601         if (GTK_CLIST(ctree)->selection)
2602                 gtk_sctree_set_anchor_row
2603                         (GTK_SCTREE(ctree),
2604                          GTK_CTREE_NODE(GTK_CLIST(ctree)->selection->data));
2605
2606         return TRUE;
2607 }
2608
2609 void summary_toggle_view(SummaryView *summaryview)
2610 {
2611         if (!messageview_is_visible(summaryview->messageview) &&
2612             summaryview->selected)
2613                 summary_display_msg(summaryview,
2614                                     summaryview->selected);
2615         else
2616                 main_window_toggle_message_view(summaryview->mainwin);
2617 }
2618
2619 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2620                                                 GtkCTreeNode *node)
2621 {
2622         MsgInfo *msginfo;
2623
2624         if (node) {
2625                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2626                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2627                         return TRUE;
2628                 node = GTK_CTREE_ROW(node)->children;
2629         } else
2630                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2631
2632         while (node) {
2633                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2634                         return TRUE;
2635                 node = GTK_CTREE_ROW(node)->sibling;
2636         }
2637
2638         return FALSE;
2639 }
2640
2641 static gboolean summary_have_unread_children(SummaryView *summaryview,
2642                                              GtkCTreeNode *node)
2643 {
2644         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2645
2646         if (!node) return FALSE;
2647
2648         node = GTK_CTREE_ROW(node)->children;
2649
2650         while (node) {
2651                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2652                         return TRUE;
2653                 node = GTK_CTREE_ROW(node)->sibling;
2654         }
2655         return FALSE;
2656 }
2657
2658 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2659 {
2660         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2661         GtkStyle *style = NULL;
2662         MsgInfo *msginfo;
2663         MsgFlags flags;
2664         gint *col_pos = summaryview->col_pos;
2665
2666         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2667         if (!msginfo) return;
2668
2669         flags = msginfo->flags;
2670
2671         gtk_ctree_node_set_foreground(ctree, row, NULL);
2672
2673         /* set new/unread column */
2674         if (MSG_IS_IGNORE_THREAD(flags)) {
2675                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2676                                           ignorethreadxpm, ignorethreadxpmmask);
2677         } else if (MSG_IS_NEW(flags)) {
2678                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2679                                           newxpm, newxpmmask);
2680         } else if (MSG_IS_UNREAD(flags)) {
2681                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2682                                           unreadxpm, unreadxpmmask);
2683         } else if (MSG_IS_REPLIED(flags)) {
2684                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2685                                           repliedxpm, repliedxpmmask);
2686         } else if (MSG_IS_FORWARDED(flags)) {
2687                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_STATUS],
2688                                           forwardedxpm, forwardedxpmmask);
2689         } else {
2690                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_STATUS],
2691                                         NULL);
2692         }
2693
2694         if (prefs_common.bold_unread &&
2695             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2696              (!GTK_CTREE_ROW(row)->expanded &&
2697               GTK_CTREE_ROW(row)->children &&
2698               summary_have_unread_children(summaryview, row))))
2699                 style = bold_style;
2700
2701         /* set mark column */
2702         if (MSG_IS_DELETED(flags)) {
2703                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2704                                           deletedxpm, deletedxpmmask);
2705                 if (style)
2706                         style = bold_deleted_style;
2707                 else {
2708                         style = small_deleted_style;
2709                 }
2710                         gtk_ctree_node_set_foreground
2711                                 (ctree, row, &summaryview->color_dim);
2712         } else if (MSG_IS_MARKED(flags)) {
2713                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2714                                           markxpm, markxpmmask);
2715         } else if (MSG_IS_MOVE(flags)) {
2716                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2717                 if (style)
2718                         style = bold_marked_style;
2719                 else {
2720                         style = small_marked_style;
2721                 }
2722                         gtk_ctree_node_set_foreground
2723                                 (ctree, row, &summaryview->color_marked);
2724         } else if (MSG_IS_COPY(flags)) {
2725                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2726                 if (style)
2727                         style = bold_marked_style;
2728                 else {
2729                         style = small_marked_style;
2730                 }
2731                         gtk_ctree_node_set_foreground
2732                                 (ctree, row, &summaryview->color_marked);
2733 #if 0
2734         } else if ((global_scoring ||
2735                   summaryview->folder_item->prefs->scoring) &&
2736                  (msginfo->score >= summaryview->important_score) &&
2737                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2738                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2739                 gtk_ctree_node_set_foreground(ctree, row,
2740                                               &summaryview->color_important);
2741 #endif
2742         } else {
2743                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2744         }
2745
2746         if (MSG_IS_LOCKED(flags)) {
2747                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2748                                           lockedxpm, lockedxpmmask);
2749         }
2750         else {
2751                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2752         }
2753
2754         if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_SIGNED(flags)) {
2755                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2756                                           clipgpgsignedxpm, clipgpgsignedxpmmask);
2757         } else if (MSG_IS_SIGNED(flags)) {
2758                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2759                                           gpgsignedxpm, gpgsignedxpmmask);
2760         } else if (MSG_IS_WITH_ATTACHMENT(flags) && MSG_IS_ENCRYPTED(flags)) {
2761                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2762                                           clipkeyxpm, clipkeyxpmmask);
2763         } else if (MSG_IS_ENCRYPTED(flags)) {
2764                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2765                                           keyxpm, keyxpmmask);
2766         } else if (MSG_IS_WITH_ATTACHMENT(flags)) {
2767                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2768                                           clipxpm, clipxpmmask);
2769         } else {
2770                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2771         }
2772         if (!style)
2773                 style = small_style;
2774
2775         gtk_ctree_node_set_row_style(ctree, row, style);
2776
2777         if (MSG_GET_COLORLABEL(flags))
2778                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2779 }
2780
2781 void summary_set_marks_selected(SummaryView *summaryview)
2782 {
2783         GList *cur;
2784         summary_lock(summaryview);
2785         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
2786              cur = cur->next)
2787                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2788         summary_unlock(summaryview);
2789 }
2790
2791 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2792 {
2793         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2794         MsgInfo *msginfo;
2795
2796         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2797         if (MSG_IS_DELETED(msginfo->flags))
2798                 summaryview->deleted--;
2799         if (MSG_IS_MOVE(msginfo->flags))
2800                 summaryview->moved--;
2801         if (MSG_IS_COPY(msginfo->flags))
2802                 summaryview->copied--;
2803
2804         procmsg_msginfo_set_to_folder(msginfo, NULL);
2805         procmsg_msginfo_change_flags(msginfo, MSG_MARKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2806         summary_set_row_marks(summaryview, row);
2807         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2808 }
2809
2810 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2811 {
2812         gboolean changed = FALSE;
2813         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2814         MsgInfo *msginfo;
2815
2816         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2817         if (MSG_IS_DELETED(msginfo->flags))
2818                 summaryview->deleted--;
2819         if (MSG_IS_MOVE(msginfo->flags)) {
2820                 summaryview->moved--;
2821                 changed = TRUE;
2822         }
2823         if (MSG_IS_COPY(msginfo->flags)) {
2824                 summaryview->copied--;
2825                 changed = TRUE;
2826         }
2827         procmsg_msginfo_set_to_folder(msginfo, NULL);
2828         procmsg_msginfo_change_flags(msginfo, MSG_LOCKED, 0, MSG_DELETED, MSG_MOVE | MSG_COPY);
2829         
2830         summary_set_row_marks(summaryview, row);
2831         debug_print("Message %d is locked\n", msginfo->msgnum);
2832 }
2833
2834 static void summary_unlock_row(SummaryView *summaryview, GtkCTreeNode *row)
2835 {
2836         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2837         MsgInfo *msginfo;
2838
2839         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2840         if (!MSG_IS_LOCKED(msginfo->flags))
2841                 return;
2842         procmsg_msginfo_set_to_folder(msginfo, NULL);
2843         procmsg_msginfo_unset_flags(msginfo, MSG_LOCKED, 0);
2844         summary_set_row_marks(summaryview, row);
2845         debug_print("Message %d is unlocked\n", msginfo->msgnum);
2846 }
2847
2848 void summary_mark(SummaryView *summaryview)
2849 {
2850         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2851         GList *cur;
2852
2853         START_LONG_OPERATION(summaryview);
2854         folder_item_set_batch(summaryview->folder_item, TRUE);
2855         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2856                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2857         folder_item_set_batch(summaryview->folder_item, FALSE);
2858         END_LONG_OPERATION(summaryview);
2859
2860         summary_status_show(summaryview);
2861 }
2862
2863 static void summary_mark_row_as_read(SummaryView *summaryview,
2864                                      GtkCTreeNode *row)
2865 {
2866         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2867         MsgInfo *msginfo;
2868
2869         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2870
2871         if(!(MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2872                 return;
2873
2874         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2875         && procmsg_msg_has_marked_parent(msginfo))
2876                 summaryview->unreadmarked--;
2877
2878         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2879         summary_set_row_marks(summaryview, row);
2880         debug_print("Message %d is marked as read\n",
2881                 msginfo->msgnum);
2882 }
2883
2884 void summary_mark_as_read(SummaryView *summaryview)
2885 {
2886         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2887         GList *cur;
2888
2889         START_LONG_OPERATION(summaryview);
2890         folder_item_set_batch(summaryview->folder_item, TRUE);
2891         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2892                 summary_mark_row_as_read(summaryview,
2893                                          GTK_CTREE_NODE(cur->data));
2894         folder_item_set_batch(summaryview->folder_item, FALSE);
2895         END_LONG_OPERATION(summaryview);
2896         
2897         summary_status_show(summaryview);
2898 }
2899
2900 void summary_msgs_lock(SummaryView *summaryview)
2901 {
2902         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2903         GList *cur;
2904
2905         START_LONG_OPERATION(summaryview);
2906         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2907                 summary_lock_row(summaryview,
2908                                          GTK_CTREE_NODE(cur->data));
2909         END_LONG_OPERATION(summaryview);
2910         
2911         summary_status_show(summaryview);
2912 }
2913
2914 void summary_msgs_unlock(SummaryView *summaryview)
2915 {
2916         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2917         GList *cur;
2918
2919         START_LONG_OPERATION(summaryview);
2920         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
2921                 summary_unlock_row(summaryview,
2922                                    GTK_CTREE_NODE(cur->data));
2923         END_LONG_OPERATION(summaryview);
2924         
2925         summary_status_show(summaryview);
2926 }
2927
2928 void summary_mark_all_read(SummaryView *summaryview)
2929 {
2930         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2931         GtkCTreeNode *node;
2932
2933         START_LONG_OPERATION(summaryview);
2934         folder_item_set_batch(summaryview->folder_item, TRUE);
2935         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2936              node = gtkut_ctree_node_next(ctree, node))
2937                 summary_mark_row_as_read(summaryview, node);
2938         folder_item_set_batch(summaryview->folder_item, FALSE);
2939         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2940              node = gtkut_ctree_node_next(ctree, node)) {
2941                 if (!GTK_CTREE_ROW(node)->expanded)
2942                         summary_set_row_marks(summaryview, node);
2943         }
2944         END_LONG_OPERATION(summaryview);
2945         
2946         summary_status_show(summaryview);
2947 }
2948
2949 static void summary_mark_row_as_unread(SummaryView *summaryview,
2950                                        GtkCTreeNode *row)
2951 {
2952         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2953         MsgInfo *msginfo;
2954
2955         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2956         if (MSG_IS_DELETED(msginfo->flags)) {
2957                 procmsg_msginfo_set_to_folder(msginfo, NULL);
2958                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2959                 summaryview->deleted--;
2960         }
2961
2962         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)
2963         && procmsg_msg_has_marked_parent(msginfo))
2964                 summaryview->unreadmarked++;
2965
2966         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2967         debug_print("Message %d is marked as unread\n",
2968                 msginfo->msgnum);
2969
2970         summary_set_row_marks(summaryview, row);
2971 }
2972
2973 void summary_mark_as_unread(SummaryView *summaryview)
2974 {
2975         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2976         GList *cur;
2977
2978         START_LONG_OPERATION(summaryview);
2979         folder_item_set_batch(summaryview->folder_item, TRUE);
2980         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
2981                 cur = cur->next)
2982                 summary_mark_row_as_unread(summaryview,
2983                                            GTK_CTREE_NODE(cur->data));
2984         folder_item_set_batch(summaryview->folder_item, FALSE);
2985         END_LONG_OPERATION(summaryview);
2986         
2987         summary_status_show(summaryview);
2988 }
2989
2990 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2991 {
2992         GList * cur;
2993         gboolean found;
2994
2995         switch (FOLDER_TYPE(summaryview->folder_item->folder)) {
2996
2997         case F_NEWS:
2998
2999                 /*
3000                   security : checks if one the accounts correspond to
3001                   the author of the post
3002                 */
3003
3004                 found = FALSE;
3005                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
3006                         PrefsAccount * account;
3007                         gchar * from_name;
3008                         
3009                         account = cur->data;
3010                         if (account->name && *account->name)
3011                                 from_name =
3012                                         g_strdup_printf("%s <%s>",
3013                                                         account->name,
3014                                                         account->address);
3015                         else
3016                                 from_name =
3017                                         g_strdup_printf("%s",
3018                                                         account->address);
3019                         
3020                         if (g_utf8_collate(from_name, msginfo->from) == 0) {
3021                                 g_free(from_name);
3022                                 found = TRUE;
3023                                 break;
3024                         }
3025                         g_free(from_name);
3026                 }
3027
3028                 if (!found) {
3029                         alertpanel_error(_("You're not the author of the article\n"));
3030                 }
3031                 
3032                 return found;
3033
3034         default:
3035                 return TRUE;
3036         }
3037 }
3038
3039 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
3040 {
3041         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3042         MsgInfo *msginfo;
3043
3044         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3045
3046         if (!check_permission(summaryview, msginfo))
3047                 return;
3048
3049         if (MSG_IS_LOCKED(msginfo->flags)) return;
3050
3051         if (MSG_IS_DELETED(msginfo->flags)) return;
3052
3053         if (MSG_IS_MOVE(msginfo->flags))
3054                 summaryview->moved--;
3055         if (MSG_IS_COPY(msginfo->flags))
3056                 summaryview->copied--;
3057
3058         procmsg_msginfo_set_to_folder(msginfo, NULL);
3059         procmsg_msginfo_change_flags(msginfo, MSG_DELETED, 0, MSG_MARKED, MSG_MOVE | MSG_COPY);
3060         summaryview->deleted++;
3061
3062         if (!prefs_common.immediate_exec && 
3063             !folder_has_parent_of_type(summaryview->folder_item, F_TRASH))
3064                 summary_set_row_marks(summaryview, row);
3065
3066         debug_print("Message %s/%d is set to delete\n",
3067                     msginfo->folder->path, msginfo->msgnum);
3068 }
3069
3070 void summary_cancel(SummaryView *summaryview)
3071 {
3072         MsgInfo * msginfo;
3073         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3074
3075         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3076                                               summaryview->selected);
3077         if (!msginfo) return;
3078
3079         if (!check_permission(summaryview, msginfo))
3080                 return;
3081
3082         news_cancel_article(summaryview->folder_item->folder, msginfo);
3083         
3084         if (summary_is_locked(summaryview)) return;
3085
3086         summary_lock(summaryview);
3087
3088         gtk_clist_freeze(clist);
3089
3090         summary_update_status(summaryview);
3091         summary_status_show(summaryview);
3092
3093         gtk_clist_thaw(clist);
3094
3095         summary_unlock(summaryview);
3096 }
3097
3098 void summary_delete(SummaryView *summaryview)
3099 {
3100         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3101         FolderItem *item = summaryview->folder_item;
3102         GList *cur;
3103         GtkCTreeNode *sel_last = NULL;
3104         GtkCTreeNode *node;
3105         AlertValue aval;
3106         MsgInfo *msginfo;
3107
3108         if (!item) return;
3109
3110         if (summary_is_locked(summaryview)) return;
3111
3112         if (!summaryview->folder_item ||
3113             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3114
3115         aval = alertpanel(_("Delete message(s)"),
3116                           _("Do you really want to delete selected message(s)?"),
3117                           GTK_STOCK_YES, GTK_STOCK_NO, NULL);
3118         if (aval != G_ALERTDEFAULT) return;
3119
3120         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; 
3121              cur = cur->next) {
3122                 GtkCTreeNode *row = GTK_CTREE_NODE(cur->data);
3123                 msginfo = gtk_ctree_node_get_row_data(ctree, row);
3124                 if (msginfo->total_size != 0 && 
3125                     msginfo->size != (off_t)msginfo->total_size)
3126                         partial_mark_for_delete(msginfo);
3127         }
3128
3129         main_window_cursor_wait(summaryview->mainwin);
3130
3131         /* next code sets current row focus right. We need to find a row
3132          * that is not deleted. */
3133         summary_lock(summaryview);
3134         folder_item_update_freeze();
3135         gtk_clist_freeze(GTK_CLIST(summaryview->ctree)); 
3136         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next) {
3137                 sel_last = GTK_CTREE_NODE(cur->data);
3138                 summary_delete_row(summaryview, sel_last);
3139         }
3140         folder_item_update_thaw();
3141         gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3142         summary_unlock(summaryview);
3143
3144         node = summary_find_next_msg(summaryview, sel_last);
3145         if (!node)
3146                 node = summary_find_prev_msg(summaryview, sel_last);
3147
3148         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3149         
3150         if (prefs_common.immediate_exec || folder_has_parent_of_type(item, F_TRASH)) {
3151                 summary_execute(summaryview);
3152                 /* after deleting, the anchor may be at an invalid row
3153                  * so reset it to the node we found earlier */
3154                 gtk_sctree_set_anchor_row(GTK_SCTREE(ctree), node);
3155         } else
3156                 summary_status_show(summaryview);
3157                 
3158         main_window_cursor_normal(summaryview->mainwin);
3159 }
3160
3161 void summary_delete_trash(SummaryView *summaryview)
3162 {
3163         FolderItem *to_folder = NULL;
3164         PrefsAccount *ac;
3165         if (!summaryview->folder_item ||
3166             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3167         
3168         if (NULL != (ac = account_find_from_item(summaryview->folder_item)))
3169                 to_folder = account_get_special_folder(ac, F_TRASH);
3170
3171         if (to_folder == NULL)
3172                 to_folder = summaryview->folder_item->folder->trash;
3173         
3174         if (to_folder == NULL || to_folder == summaryview->folder_item)
3175                 summary_delete(summaryview);
3176         else
3177                 summary_move_selected_to(summaryview, to_folder);
3178 }
3179
3180
3181 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
3182 {
3183         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3184         MsgInfo *msginfo;
3185
3186         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3187         if (MSG_IS_DELETED(msginfo->flags))
3188                 summaryview->deleted--;
3189         if (MSG_IS_MOVE(msginfo->flags))
3190                 summaryview->moved--;
3191         if (MSG_IS_COPY(msginfo->flags))
3192                 summaryview->copied--;
3193
3194         procmsg_msginfo_set_to_folder(msginfo, NULL);
3195         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
3196         summary_set_row_marks(summaryview, row);
3197
3198         debug_print("Message %s/%d is unmarked\n",
3199                     msginfo->folder->path, msginfo->msgnum);
3200 }
3201
3202 void summary_unmark(SummaryView *summaryview)
3203 {
3204         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3205         GList *cur;
3206
3207         START_LONG_OPERATION(summaryview);
3208         folder_item_set_batch(summaryview->folder_item, TRUE);
3209         for (cur = GTK_CLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
3210                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
3211         folder_item_set_batch(summaryview->folder_item, FALSE);
3212         END_LONG_OPERATION(summaryview);
3213         
3214         summary_status_show(summaryview);
3215 }
3216
3217 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
3218                                 FolderItem *to_folder)
3219 {
3220         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3221         MsgInfo *msginfo;
3222
3223         g_return_if_fail(to_folder != NULL);
3224
3225         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3226         if (MSG_IS_LOCKED(msginfo->flags))
3227                 return;
3228
3229         procmsg_msginfo_set_to_folder(msginfo, to_folder);
3230         if (MSG_IS_DELETED(msginfo->flags))
3231                 summaryview->deleted--;
3232         if (MSG_IS_COPY(msginfo->flags)) {
3233                 summaryview->copied--;
3234         }
3235         if (!MSG_IS_MOVE(msginfo->flags)) {
3236                 procmsg_msginfo_change_flags(msginfo, 0, MSG_MOVE, MSG_MARKED | MSG_DELETED, MSG_COPY);
3237                 summaryview->moved++;
3238         } else {
3239                 procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
3240         }
3241         
3242         if (!prefs_common.immediate_exec) {
3243                 summary_set_row_marks(summaryview, row);
3244         }
3245
3246         debug_print("Message %d is set to move to %s\n",
3247                     msginfo->msgnum, to_folder->path);
3248 }
3249
3250 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3251 {
3252         GList *cur;
3253
3254         if (!to_folder) return;
3255         if (!summaryview->folder_item ||
3256             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3257
3258         if (summary_is_locked(summaryview)) return;
3259
3260         if (summaryview->folder_item == to_folder) {
3261                 alertpanel_error(_("Destination is same as current folder."));
3262                 return;
3263         }
3264
3265         START_LONG_OPERATION(summaryview);
3266
3267         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3268              cur != NULL && cur->data != NULL; cur = cur->next)
3269                 summary_move_row_to
3270                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3271
3272         END_LONG_OPERATION(summaryview);
3273
3274         if (prefs_common.immediate_exec) {
3275                 summary_execute(summaryview);
3276         } else {
3277                 summary_status_show(summaryview);
3278         }
3279         
3280         if (!summaryview->selected) { /* this was the last message */
3281                 GtkCTreeNode *node = gtk_ctree_node_nth (GTK_CTREE(summaryview->ctree), 
3282                                                          GTK_CLIST(summaryview->ctree)->rows - 1);
3283                 if (node)
3284                         summary_select_node(summaryview, node, prefs_common.always_show_msg, TRUE);
3285         }
3286
3287 }
3288
3289 void summary_move_to(SummaryView *summaryview)
3290 {
3291         FolderItem *to_folder;
3292
3293         if (!summaryview->folder_item ||
3294             FOLDER_TYPE(summaryview->folder_item->folder) == F_NEWS) return;
3295
3296         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3297                                          FOLDER_SEL_MOVE, NULL);
3298         summary_move_selected_to(summaryview, to_folder);