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