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