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