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