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