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