2007-05-06 [paul] 2.9.1cvs45
[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                         default:
1377                                 node = NULL;
1378                 }
1379
1380                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
1381                         node = gtk_ctree_node_nth
1382                                 (ctree,
1383                                  item->sort_type == SORT_DESCENDING
1384                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
1385                 }
1386                 summary_unlock(summaryview);
1387                 summary_select_node(summaryview, node,
1388                                     prefs_common.always_show_msg,
1389                                     TRUE);
1390                 summary_lock(summaryview);
1391         }
1392
1393         summary_status_show(summaryview);
1394         summary_set_menu_sensitive(summaryview);
1395         toolbar_main_set_sensitive(summaryview->mainwin);
1396         
1397         summary_thaw(summaryview);
1398         debug_print("\n");
1399         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1400         STATUSBAR_POP(summaryview->mainwin);
1401         main_window_cursor_normal(summaryview->mainwin);
1402         summary_unlock(summaryview);
1403         inc_unlock();
1404         END_TIMING();
1405         return TRUE;
1406 }
1407
1408 #undef CURRENTLY_DISPLAYED
1409
1410
1411 void summary_clear_list(SummaryView *summaryview)
1412 {
1413         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1414         gint optimal_width;
1415
1416         summary_freeze(summaryview);
1417
1418         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
1419                                 NULL, summary_free_msginfo_func, NULL);
1420
1421         if (summaryview->folder_item) {
1422                 summaryview->folder_item->opened = FALSE;
1423                 summaryview->folder_item = NULL;
1424         }
1425
1426         summaryview->display_msg = FALSE;
1427
1428         summaryview->selected = NULL;
1429         summaryview->displayed = NULL;
1430         summaryview->total_size = 0;
1431         summaryview->deleted = summaryview->moved = 0;
1432         summaryview->copied = 0;
1433         if (summaryview->msgid_table) {
1434                 g_hash_table_destroy(summaryview->msgid_table);
1435                 summaryview->msgid_table = NULL;
1436         }
1437         if (summaryview->subject_table) {
1438                 g_hash_table_destroy(summaryview->subject_table);
1439                 summaryview->subject_table = NULL;
1440         }
1441         summaryview->mlist = NULL;
1442
1443         gtk_clist_clear(clist);
1444         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1445                 optimal_width = gtk_clist_optimal_column_width
1446                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1447                 gtk_clist_set_column_width
1448                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1449                          optimal_width);
1450         }
1451
1452         summary_thaw(summaryview);
1453 }
1454
1455 void summary_clear_all(SummaryView *summaryview)
1456 {
1457         messageview_clear(summaryview->messageview);
1458         summary_clear_list(summaryview);
1459         summary_set_menu_sensitive(summaryview);
1460         toolbar_main_set_sensitive(summaryview->mainwin);
1461         summary_status_show(summaryview);
1462 }
1463
1464 void summary_lock(SummaryView *summaryview)
1465 {
1466         summaryview->lock_count++;
1467 }
1468
1469 void summary_unlock(SummaryView *summaryview)
1470 {
1471         if (summaryview->lock_count)
1472                 summaryview->lock_count--;
1473 }
1474
1475 gboolean summary_is_locked(SummaryView *summaryview)
1476 {
1477         return summaryview->lock_count > 0;
1478 }
1479
1480 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1481 {
1482         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1483         SummarySelection selection;
1484
1485         if (!clist->row_list)
1486                 selection = SUMMARY_NONE;
1487         else if (!clist->selection)
1488                 selection = SUMMARY_SELECTED_NONE;
1489         else if (!clist->selection->next)
1490                 selection = SUMMARY_SELECTED_SINGLE;
1491         else
1492                 selection = SUMMARY_SELECTED_MULTIPLE;
1493
1494         return selection;
1495 }
1496
1497 /*!
1498  *\return       MsgInfo * Selected message if there's one selected;
1499  *              if multiple selected, or none, return NULL.
1500  */ 
1501 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1502 {
1503         /* summaryview->selected may be valid when multiple 
1504          * messages were selected */
1505         GList *sellist = GTK_CLIST(summaryview->ctree)->selection;
1506
1507         if (sellist == NULL || sellist->next) 
1508                 return NULL;
1509         
1510         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1511 }
1512
1513 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1514 {
1515         GSList *mlist = NULL;
1516         GList *cur;
1517         MsgInfo *msginfo;
1518
1519         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1520              cur = cur->next) {
1521                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1522                 mlist = g_slist_prepend(mlist, msginfo);
1523         }
1524
1525         mlist = g_slist_reverse(mlist);
1526
1527         return mlist;
1528 }
1529
1530 void summary_set_menu_sensitive(SummaryView *summaryview)
1531 {
1532         GtkItemFactory *ifactory = summaryview->popupfactory;
1533         SensitiveCond state;
1534         gboolean sensitive;
1535         GtkWidget *menuitem;
1536         gint i;
1537
1538         static const struct {
1539                 gchar *const entry;
1540                 SensitiveCond cond;
1541         } entry[] = {
1542                 {"/Reply"                       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1543                 {"/Reply to"                    , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1544                 {"/Reply to/all"                , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1545                 {"/Reply to/sender"             , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1546                 {"/Reply to/mailing list"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1547
1548                 {"/Forward"                     , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1549                 {"/Forward as attachment"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1550                 {"/Redirect"                    , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1551
1552                 {"/Move..."                     , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1553                 {"/Copy..."                     , M_TARGET_EXIST|M_EXEC},
1554                 {"/Move to trash"               , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1555                 {"/Delete..."                   , M_TARGET_EXIST|M_ALLOW_DELETE},
1556
1557                 {"/Mark"                        , M_TARGET_EXIST},
1558                 {"/Mark/Mark"                   , M_TARGET_EXIST},
1559                 {"/Mark/Unmark"                 , M_TARGET_EXIST},
1560                 {"/Mark/Mark as unread"         , M_TARGET_EXIST},
1561                 {"/Mark/Mark all read"          , M_TARGET_EXIST},
1562                 {"/Mark/Ignore thread"          , M_TARGET_EXIST},
1563                 {"/Mark/Unignore thread"        , M_TARGET_EXIST},
1564                 {"/Mark/Lock"                   , M_TARGET_EXIST},
1565                 {"/Mark/Unlock"                 , M_TARGET_EXIST},
1566                 {"/Mark/Mark as spam"           , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1567                 {"/Mark/Mark as ham"            , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1568                 {"/Color label"                 , M_TARGET_EXIST},
1569
1570                 {"/Add sender to address book"  , M_SINGLE_TARGET_EXIST},
1571                 {"/Create filter rule"          , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1572                 {"/Create processing rule"      , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1573
1574                 {"/View"                        , M_SINGLE_TARGET_EXIST},
1575                 {"/View/Open in new window"     , M_SINGLE_TARGET_EXIST},
1576                 {"/View/Message source"         , M_SINGLE_TARGET_EXIST},
1577                 {"/View/All headers"            , M_SINGLE_TARGET_EXIST},
1578                 {"/Save as..."                  , M_TARGET_EXIST},
1579                 {"/Print..."                    , M_TARGET_EXIST},
1580                 {NULL, 0}
1581         };
1582
1583         main_window_set_menu_sensitive(summaryview->mainwin);
1584
1585         state = main_window_get_current_state(summaryview->mainwin);
1586
1587         for (i = 0; entry[i].entry != NULL; i++) {
1588                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1589                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1590         }
1591
1592
1593         summary_lock(summaryview);
1594         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All headers");
1595         if (summaryview->messageview 
1596         &&  summaryview->messageview->mimeview
1597         &&  summaryview->messageview->mimeview->textview)
1598                 gtk_check_menu_item_set_active
1599                         (GTK_CHECK_MENU_ITEM(menuitem),
1600                          summaryview->messageview->mimeview->textview->show_all_headers);
1601         summary_unlock(summaryview);
1602 }
1603
1604 void summary_select_prev_unread(SummaryView *summaryview)
1605 {
1606         GtkCTreeNode *node;
1607         gboolean skip_cur = FALSE;
1608
1609         if (summaryview->displayed 
1610         &&  summaryview->selected == summaryview->displayed) {
1611                 debug_print("skipping current\n");
1612                 skip_cur = TRUE;
1613         }
1614
1615         node = summary_find_prev_flagged_msg
1616                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1617
1618         if (!node || node == summaryview->selected) {
1619                 AlertValue val = 0;
1620
1621                 switch (prefs_common.next_unread_msg_dialog) {
1622                         case NEXTUNREADMSGDIALOG_ALWAYS:
1623                                 val = alertpanel(_("No more unread messages"),
1624                                                  _("No unread message found. "
1625                                                    "Search from the end?"),
1626                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1627                                 break;
1628                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1629                                 val = G_ALERTALTERNATE;
1630                                 break;
1631                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1632                                 val = !G_ALERTALTERNATE;
1633                                 break;
1634                         default:
1635                                 debug_print(
1636                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1637                 }
1638                 if (val != G_ALERTALTERNATE) return;
1639                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1640                                                      MSG_UNREAD, FALSE);
1641         }
1642
1643         if (!node)
1644                 alertpanel_notice(_("No unread messages."));
1645         else
1646                 summary_select_node(summaryview, node, TRUE, FALSE);
1647 }
1648
1649 void summary_select_next_unread(SummaryView *summaryview)
1650 {
1651         GtkCTreeNode *node = summaryview->selected;
1652         gboolean skip_cur = FALSE;
1653         
1654         if (summaryview->displayed 
1655         &&  summaryview->selected == summaryview->displayed) {
1656                 debug_print("skipping cur (%p %p)\n",
1657                         summaryview->displayed, summaryview->selected);
1658                 skip_cur = TRUE;
1659         }
1660
1661
1662         node = summary_find_next_flagged_msg
1663                 (summaryview, node, MSG_UNREAD, skip_cur);
1664         
1665         if (node)
1666                 summary_select_node(summaryview, node, TRUE, FALSE);
1667         else {
1668                 node = summary_find_next_flagged_msg
1669                         (summaryview, NULL, MSG_UNREAD, FALSE);
1670                 if (node == NULL || node == summaryview->selected) {
1671                         AlertValue val = 0;
1672
1673                         switch (prefs_common.next_unread_msg_dialog) {
1674                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1675                                         val = alertpanel(_("No more unread messages"),
1676                                                          _("No unread message found. "
1677                                                            "Go to next folder?"),
1678                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1679                                         break;
1680                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1681                                         val = G_ALERTALTERNATE;
1682                                         break;
1683                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1684                                         val = G_ALERTOTHER;
1685                                         break;
1686                                 default:
1687                                         debug_print(
1688                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1689                         }
1690
1691                         if (val == G_ALERTALTERNATE) {
1692                                 folderview_select_next_unread(summaryview->folderview, TRUE);
1693                                 return;
1694                         } 
1695                         else
1696                                 return;
1697                 } else
1698                         summary_select_node(summaryview, node, TRUE, FALSE);
1699
1700         }
1701 }
1702
1703 void summary_select_prev_new(SummaryView *summaryview)
1704 {
1705         GtkCTreeNode *node;
1706         gboolean skip_cur = FALSE;
1707
1708         if (summaryview->displayed 
1709         &&  summaryview->selected == summaryview->displayed) {
1710                 debug_print("skipping current\n");
1711                 skip_cur = TRUE;
1712         }
1713
1714         node = summary_find_prev_flagged_msg
1715                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1716
1717         if (!node || node == summaryview->selected) {
1718                 AlertValue val = 0;
1719
1720                 switch (prefs_common.next_unread_msg_dialog) {
1721                         case NEXTUNREADMSGDIALOG_ALWAYS:
1722                                 val = alertpanel(_("No more new messages"),
1723                                                  _("No new message found. "
1724                                                    "Search from the end?"),
1725                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1726                                 break;
1727                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1728                                 val = G_ALERTALTERNATE;
1729                                 break;
1730                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1731                                 val = !G_ALERTALTERNATE;
1732                                 break;
1733                         default:
1734                                 debug_print(
1735                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1736                 }
1737                 if (val != G_ALERTALTERNATE) return;
1738                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1739                                                      MSG_NEW, FALSE);
1740         }
1741
1742         if (!node)
1743                 alertpanel_notice(_("No new messages."));
1744         else
1745                 summary_select_node(summaryview, node, TRUE, FALSE);
1746 }
1747
1748 void summary_select_next_new(SummaryView *summaryview)
1749 {
1750         GtkCTreeNode *node = summaryview->selected;
1751         gboolean skip_cur = FALSE;
1752         
1753         if (summaryview->displayed 
1754         &&  summaryview->selected == summaryview->displayed) {
1755                 debug_print("skipping cur (%p %p)\n",
1756                         summaryview->displayed, summaryview->selected);
1757                 skip_cur = TRUE;
1758         }
1759
1760
1761         node = summary_find_next_flagged_msg
1762                 (summaryview, node, MSG_NEW, skip_cur);
1763         
1764         if (node)
1765                 summary_select_node(summaryview, node, TRUE, FALSE);
1766         else {
1767                 node = summary_find_next_flagged_msg
1768                         (summaryview, NULL, MSG_NEW, FALSE);
1769                 if (node == NULL || node == summaryview->selected) {
1770                         AlertValue val = 0;
1771
1772                         switch (prefs_common.next_unread_msg_dialog) {
1773                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1774                                         val = alertpanel(_("No more new messages"),
1775                                                          _("No new message found. "
1776                                                            "Go to next folder?"),
1777                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1778                                         break;
1779                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1780                                         val = G_ALERTALTERNATE;
1781                                         break;
1782                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1783                                         val = G_ALERTOTHER;
1784                                         break;
1785                                 default:
1786                                         debug_print(
1787                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1788                         }
1789
1790                         if (val == G_ALERTALTERNATE) {
1791                                 folderview_select_next_new(summaryview->folderview);
1792                                 return;
1793                         } 
1794                         else
1795                                 return;
1796                 } else
1797                         summary_select_node(summaryview, node, TRUE, FALSE);
1798
1799         }
1800 }
1801
1802 void summary_select_prev_marked(SummaryView *summaryview)
1803 {
1804         GtkCTreeNode *node;
1805
1806         node = summary_find_prev_flagged_msg
1807                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1808
1809         if (!node) {
1810                 AlertValue val;
1811
1812                 val = alertpanel(_("No more marked messages"),
1813                                  _("No marked message found. "
1814                                    "Search from the end?"),
1815                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1816                 if (val != G_ALERTALTERNATE) return;
1817                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1818                                                      MSG_MARKED, TRUE);
1819         }
1820
1821         if (!node)
1822                 alertpanel_notice(_("No marked messages."));
1823         else
1824                 summary_select_node(summaryview, node, TRUE, FALSE);
1825 }
1826
1827 void summary_select_next_marked(SummaryView *summaryview)
1828 {
1829         GtkCTreeNode *node = summaryview->selected;
1830         gboolean skip_cur = FALSE;
1831         
1832         if (summaryview->displayed 
1833         &&  summaryview->selected == summaryview->displayed) {
1834                 debug_print("skipping cur (%p %p)\n",
1835                         summaryview->displayed, summaryview->selected);
1836                 skip_cur = TRUE;
1837         }
1838
1839
1840         node = summary_find_next_flagged_msg
1841                 (summaryview, node, MSG_MARKED, skip_cur);
1842         
1843         if (node)
1844                 summary_select_node(summaryview, node, TRUE, FALSE);
1845         else {
1846                 node = summary_find_next_flagged_msg
1847                         (summaryview, NULL, MSG_MARKED, FALSE);
1848                 if (node == NULL || node == summaryview->selected) {
1849                         AlertValue val = 0;
1850
1851                         switch (prefs_common.next_unread_msg_dialog) {
1852                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1853                                         val = alertpanel(_("No more marked messages"),
1854                                                          _("No marked message found. "
1855                                                            "Go to next folder?"),
1856                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1857                                         break;
1858                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1859                                         val = G_ALERTALTERNATE;
1860                                         break;
1861                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1862                                         val = G_ALERTOTHER;
1863                                         break;
1864                                 default:
1865                                         debug_print(
1866                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1867                         }
1868
1869                         if (val == G_ALERTALTERNATE) {
1870                                 folderview_select_next_marked(summaryview->folderview);
1871                                 return;
1872                         } 
1873                         else
1874                                 return;
1875                 } else
1876                         summary_select_node(summaryview, node, TRUE, FALSE);
1877
1878         }
1879 }
1880
1881 void summary_select_prev_labeled(SummaryView *summaryview)
1882 {
1883         GtkCTreeNode *node;
1884
1885         node = summary_find_prev_flagged_msg
1886                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1887
1888         if (!node) {
1889                 AlertValue val;
1890
1891                 val = alertpanel(_("No more labeled messages"),
1892                                  _("No labeled message found. "
1893                                    "Search from the end?"),
1894                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1895                 if (val != G_ALERTALTERNATE) return;
1896                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1897                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1898         }
1899
1900         if (!node)
1901                 alertpanel_notice(_("No labeled messages."));
1902         else
1903                 summary_select_node(summaryview, node, TRUE, FALSE);
1904 }
1905
1906 void summary_select_next_labeled(SummaryView *summaryview)
1907 {
1908         GtkCTreeNode *node;
1909
1910         node = summary_find_next_flagged_msg
1911                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1912
1913         if (!node) {
1914                 AlertValue val;
1915
1916                 val = alertpanel(_("No more labeled messages"),
1917                                  _("No labeled message found. "
1918                                    "Search from the beginning?"),
1919                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1920                 if (val != G_ALERTALTERNATE) return;
1921                 node = summary_find_next_flagged_msg(summaryview, NULL,
1922                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1923         }
1924
1925         if (!node)
1926                 alertpanel_notice(_("No labeled messages."));
1927         else
1928                 summary_select_node(summaryview, node, TRUE, FALSE);
1929 }
1930
1931 void summary_select_last_read(SummaryView *summaryview)
1932 {
1933         if (summaryview->last_displayed)
1934                 summary_select_node(summaryview, summaryview->last_displayed, TRUE, FALSE);
1935 }
1936
1937 void summary_select_parent(SummaryView *summaryview)
1938 {
1939         GtkCTreeNode *node = NULL;
1940
1941         if (summaryview->selected)
1942                 node = GTK_CTREE_ROW(summaryview->selected)->parent;
1943         if (node)
1944                 summary_select_node(summaryview, node, TRUE, FALSE);
1945 }
1946
1947 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1948 {
1949         GtkCTreeNode *node;
1950
1951         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1952         summary_select_node(summaryview, node, FALSE, TRUE);
1953 }
1954
1955 typedef struct _PostponedSelectData
1956 {
1957         GtkCTree *ctree;
1958         GtkCTreeNode *row;
1959         GtkCTreeNode *node;
1960         GtkScrollType type;
1961         gint column;
1962         SummaryView *summaryview;
1963         gboolean display_msg;
1964         gboolean do_refresh;
1965 } PostponedSelectData;
1966
1967 static gboolean summary_select_retry(void *data)
1968 {
1969         PostponedSelectData *psdata = (PostponedSelectData *)data;
1970         debug_print("trying again\n");
1971         if (psdata->row)
1972                 summary_selected(psdata->ctree, psdata->row,
1973                             psdata->column, psdata->summaryview);
1974         else if (psdata->node)
1975                 summary_select_node(psdata->summaryview, psdata->node,
1976                             psdata->display_msg, psdata->do_refresh);
1977         else
1978                 summary_step(psdata->summaryview, psdata->type);
1979         g_free(psdata);
1980         return FALSE;
1981 }
1982
1983 /**
1984  * summary_select_node:
1985  * @summaryview: Summary view.
1986  * @node: Summary tree node.
1987  * @display_msg: TRUE to display the selected message.
1988  * @do_refresh: TRUE to refresh the widget.
1989  *
1990  * Select @node (bringing it into view by scrolling and expanding its
1991  * thread, if necessary) and unselect all others.  If @display_msg is
1992  * TRUE, display the corresponding message in the message view.
1993  * If @do_refresh is TRUE, the widget is refreshed.
1994  **/
1995 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1996                          gboolean display_msg, gboolean do_refresh)
1997 {
1998         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1999         
2000         if (summary_is_locked(summaryview)
2001         && !GTK_SCTREE(ctree)->selecting_range
2002         && summaryview->messageview->mimeview
2003         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
2004         && summaryview->messageview->mimeview->textview->loading) {
2005                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2006                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
2007                 
2008                 data->ctree = ctree;
2009                 data->row = NULL;
2010                 data->node = node;
2011                 data->summaryview = summaryview;
2012                 data->display_msg = display_msg;
2013                 data->do_refresh = do_refresh;
2014                 debug_print("postponing open of message till end of load\n");
2015                 g_timeout_add(100, summary_select_retry, data);
2016                 return;
2017         }
2018         if (summary_is_locked(summaryview)) {
2019                 return;
2020         }
2021         if (!summaryview->folder_item)
2022                 return;
2023         if (node) {
2024                 gtkut_ctree_expand_parent_all(ctree, node);
2025                 if (do_refresh) {
2026                         summary_lock(summaryview);
2027                         GTK_EVENTS_FLUSH();
2028                         summary_unlock(summaryview);
2029                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
2030                         gtk_ctree_node_moveto(ctree, node, 0, 0.5, 0);
2031                 }
2032                 if (display_msg && summaryview->displayed == node)
2033                         summaryview->displayed = NULL;
2034                 summaryview->display_msg = display_msg;
2035                 gtk_sctree_select(GTK_SCTREE(ctree), node);
2036                 if (summaryview->selected == NULL)
2037                         summaryview->selected = node;
2038         }
2039 }
2040
2041 guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
2042 {
2043         GtkCTree *ctree =NULL;
2044         MsgInfo *msginfo;
2045
2046         if (!summaryview)
2047                 return 0;
2048         ctree = GTK_CTREE(summaryview->ctree);
2049         if (!node)
2050                 return 0;
2051         msginfo = gtk_ctree_node_get_row_data(ctree, node);
2052         if (msginfo)
2053                 return msginfo->msgnum;
2054         else 
2055                 return -1;
2056 }
2057
2058 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
2059                                            GtkCTreeNode *current_node)
2060 {
2061         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2062         GtkCTreeNode *node;
2063         MsgInfo *msginfo;
2064
2065         if (current_node)
2066                 node = current_node;
2067         else
2068                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
2069
2070         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
2071                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2072                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
2073         }
2074
2075         return node;
2076 }
2077
2078 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
2079                                            GtkCTreeNode *current_node)
2080 {
2081         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2082         GtkCTreeNode *node;
2083         MsgInfo *msginfo;
2084
2085         if (current_node)
2086                 node = current_node;
2087         else
2088                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2089
2090         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2091                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2092                 if (msginfo && !MSG_IS_DELETED(msginfo->flags) 
2093                 && !MSG_IS_MOVE(msginfo->flags)) break;
2094         }
2095
2096         return node;
2097 }
2098
2099 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
2100                                                    GtkCTreeNode *current_node,
2101                                                    MsgPermFlags flags,
2102                                                    gboolean start_from_prev)
2103 {
2104         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2105         GtkCTreeNode *node;
2106         MsgInfo *msginfo;
2107
2108         if (current_node) {
2109                 if (start_from_prev)
2110                         node = GTK_CTREE_NODE_PREV(current_node);
2111                 else
2112                         node = current_node;
2113         } else
2114                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
2115
2116         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
2117                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2118                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
2119         }
2120
2121         return node;
2122 }
2123
2124 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
2125                                                    GtkCTreeNode *current_node,
2126                                                    MsgPermFlags flags,
2127                                                    gboolean start_from_next)
2128 {
2129         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2130         GtkCTreeNode *node;
2131         MsgInfo *msginfo;
2132
2133         if (current_node) {
2134                 if (start_from_next)
2135                         node = gtkut_ctree_node_next(ctree, current_node);
2136                 else
2137                         node = current_node;
2138         } else
2139                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2140
2141         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2142                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2143                 /* Find msg with matching flags but ignore messages with
2144                    ignore flags, if searching for new or unread messages */
2145                 if ((msginfo && (msginfo->flags.perm_flags & flags) != 0) &&
2146                     !(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) 
2147                         )
2148                         break;
2149         }
2150
2151         return node;
2152 }
2153
2154 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
2155                                                 guint msgnum)
2156 {
2157         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2158         GtkCTreeNode *node;
2159         MsgInfo *msginfo;
2160
2161         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2162
2163         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2164                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2165                 if (msginfo && msginfo->msgnum == msgnum) break;
2166         }
2167
2168         return node;
2169 }
2170
2171 static guint attract_hash_func(gconstpointer key)
2172 {
2173         gchar *str;
2174         gchar *p;
2175         guint h;
2176
2177         Xstrdup_a(str, (const gchar *)key, return 0);
2178         trim_subject(str);
2179
2180         p = str;
2181         h = *p;
2182
2183         if (h) {
2184                 for (p += 1; *p != '\0'; p++)
2185                         h = (h << 5) - h + *p;
2186         }
2187
2188         return h;
2189 }
2190
2191 static gint attract_compare_func(gconstpointer a, gconstpointer b)
2192 {
2193         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
2194 }
2195
2196 void summary_attract_by_subject(SummaryView *summaryview)
2197 {
2198         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2199         GtkCList *clist = GTK_CLIST(ctree);
2200         GtkCTreeNode *src_node;
2201         GtkCTreeNode *dst_node, *sibling;
2202         GtkCTreeNode *tmp;
2203         MsgInfo *src_msginfo, *dst_msginfo;
2204         GHashTable *subject_table;
2205
2206         debug_print("Attracting messages by subject...");
2207         STATUSBAR_PUSH(summaryview->mainwin,
2208                        _("Attracting messages by subject..."));
2209
2210         main_window_cursor_wait(summaryview->mainwin);
2211         summary_freeze(summaryview);
2212
2213         subject_table = g_hash_table_new(attract_hash_func,
2214                                          attract_compare_func);
2215
2216         for (src_node = GTK_CTREE_NODE(clist->row_list);
2217              src_node != NULL;
2218              src_node = tmp) {
2219                 tmp = GTK_CTREE_ROW(src_node)->sibling;
2220                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
2221                 if (!src_msginfo) continue;
2222                 if (!src_msginfo->subject) continue;
2223
2224                 /* find attracting node */
2225                 dst_node = g_hash_table_lookup(subject_table,
2226                                                src_msginfo->subject);
2227
2228                 if (dst_node) {
2229                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
2230
2231                         /* if the time difference is more than 20 days,
2232                            don't attract */
2233                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
2234                             > 60 * 60 * 24 * 20)
2235                                 continue;
2236
2237                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
2238                         if (src_node != sibling)
2239                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
2240                 }
2241
2242                 g_hash_table_insert(subject_table,
2243                                     src_msginfo->subject, src_node);
2244         }
2245
2246         g_hash_table_destroy(subject_table);
2247
2248         gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2249
2250         summary_thaw(summaryview);
2251
2252         debug_print("done.\n");
2253         STATUSBAR_POP(summaryview->mainwin);
2254
2255         main_window_cursor_normal(summaryview->mainwin);
2256 }
2257
2258 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
2259                                       gpointer data)
2260 {
2261         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
2262
2263         if (msginfo)
2264                 procmsg_msginfo_free(msginfo);
2265 }
2266
2267 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
2268                                    gpointer data)
2269 {
2270         SummaryView *summaryview = data;
2271         MsgInfo *msginfo;
2272
2273         msginfo = gtk_ctree_node_get_row_data(ctree, node);
2274
2275         if (MSG_IS_DELETED(msginfo->flags))
2276                 summaryview->deleted++;
2277
2278         summaryview->total_size += msginfo->size;
2279
2280         summary_set_row_marks(summaryview, node);
2281 }
2282
2283 static void summary_update_status(SummaryView *summaryview)
2284 {
2285         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2286         GtkCTreeNode *node;
2287         MsgInfo *msginfo;
2288
2289         summaryview->total_size =
2290         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
2291
2292         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2293              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2294                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2295
2296                 if (MSG_IS_DELETED(msginfo->flags))
2297                         summaryview->deleted++;
2298                 if (MSG_IS_MOVE(msginfo->flags))
2299                         summaryview->moved++;
2300                 if (MSG_IS_COPY(msginfo->flags))
2301                         summaryview->copied++;
2302                 summaryview->total_size += msginfo->size;
2303         }
2304 }
2305
2306 static void summary_status_show(SummaryView *summaryview)
2307 {
2308         gchar *str;
2309         gchar *del, *mv, *cp;
2310         gchar *sel;
2311         gchar *spc;
2312         gchar *itstr;
2313         GList *rowlist, *cur;
2314         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
2315         off_t sel_size = 0, n_size = 0;
2316         MsgInfo *msginfo;
2317         gchar *name;
2318         
2319         if (!summaryview->folder_item) {
2320                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
2321                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
2322                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
2323                 toolbar_main_set_sensitive(summaryview->mainwin);
2324                 return;
2325         }
2326
2327         rowlist = GTK_CLIST(summaryview->ctree)->selection;
2328         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2329                 msginfo = gtk_ctree_node_get_row_data
2330                         (GTK_CTREE(summaryview->ctree),
2331                          GTK_CTREE_NODE(cur->data));
2332                 if (msginfo) {
2333                         sel_size += msginfo->size;
2334                         n_selected++;
2335                 }
2336         }
2337         
2338         if (summaryview->folder_item->hide_read_msgs 
2339         || quicksearch_is_active(summaryview->quicksearch)) {
2340                 rowlist = GTK_CLIST(summaryview->ctree)->row_list;
2341                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2342                         msginfo = gtk_ctree_node_get_row_data
2343                                 (GTK_CTREE(summaryview->ctree),
2344                                  GTK_CTREE_NODE(cur));
2345                         if (msginfo) {
2346                                 n_size += msginfo->size;
2347                                 n_total++;
2348                                 if (MSG_IS_NEW(msginfo->flags))
2349                                         n_new++;
2350                                 if (MSG_IS_UNREAD(msginfo->flags))
2351                                         n_unread++;
2352                         }
2353                 }
2354         } else {
2355                 n_new = summaryview->folder_item->new_msgs;
2356                 n_unread = summaryview->folder_item->unread_msgs;
2357                 n_total = summaryview->folder_item->total_msgs;
2358                 n_size = summaryview->total_size;
2359         }
2360
2361         name = folder_item_get_name(summaryview->folder_item);
2362         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
2363         g_free(name);
2364
2365         if (summaryview->deleted)
2366                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
2367         else
2368                 del = g_strdup("");
2369         if (summaryview->moved)
2370                 mv = g_strdup_printf(_("%s%d moved"),
2371                                      summaryview->deleted ? _(", ") : "",
2372                                      summaryview->moved);
2373         else
2374                 mv = g_strdup("");
2375         if (summaryview->copied)
2376                 cp = g_strdup_printf(_("%s%d copied"),
2377                                      summaryview->deleted ||
2378                                      summaryview->moved ? _(", ") : "",
2379                                      summaryview->copied);
2380         else
2381                 cp = g_strdup("");
2382
2383         if (summaryview->deleted || summaryview->moved || summaryview->copied)
2384                 spc = "    ";
2385         else
2386                 spc = "";
2387
2388         if (n_selected) {
2389                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
2390                 if (n_selected == 1)
2391                         itstr = g_strdup(_(" item selected"));
2392                 else
2393                         itstr = g_strdup(_(" items selected"));
2394         } else {
2395                 sel = g_strdup("");
2396                 itstr = g_strdup("");
2397         }
2398                 
2399         str = g_strconcat(n_selected ? itos(n_selected) : "",
2400                                         itstr, sel, spc, del, mv, cp, NULL);
2401         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2402         g_free(str);
2403         g_free(sel);
2404         g_free(del);
2405         g_free(mv);
2406         g_free(cp);
2407         g_free(itstr);
2408
2409         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2410
2411                                       n_new, n_unread, n_total,
2412                                       to_human_readable(n_size));
2413         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2414         g_free(str);
2415         toolbar_main_set_sensitive(summaryview->mainwin);
2416 }
2417
2418 static void summary_set_column_titles(SummaryView *summaryview)
2419 {
2420         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2421         FolderItem *item = summaryview->folder_item;
2422         GtkWidget *hbox;
2423         GtkWidget *label;
2424         GtkWidget *arrow;
2425         gint pos;
2426         const gchar *title;
2427         SummaryColumnType type;
2428         GtkJustification justify;
2429
2430         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2431                 SORT_BY_MARK,
2432                 SORT_BY_STATUS,
2433                 SORT_BY_MIME,
2434                 SORT_BY_SUBJECT,
2435                 SORT_BY_FROM,
2436                 SORT_BY_TO,
2437                 SORT_BY_DATE,
2438                 SORT_BY_SIZE,
2439                 SORT_BY_NUMBER,
2440                 SORT_BY_SCORE,
2441                 SORT_BY_LOCKED
2442         };
2443
2444         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2445                 type = summaryview->col_state[pos].type;
2446
2447                 /* CLAWS: mime and unread are single char headers */
2448                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2449                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2450
2451                 switch (type) {
2452                 case S_COL_SUBJECT:
2453                 case S_COL_FROM:
2454                 case S_COL_TO:
2455                 case S_COL_DATE:
2456                 case S_COL_NUMBER:
2457                         if(type == S_COL_FROM && item != NULL &&
2458                                         FOLDER_SHOWS_TO_HDR(item) &&
2459                                         !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2460                                 type = S_COL_TO;
2461                         if(type == S_COL_NUMBER)
2462                                 title = gettext(col_label[type]);
2463                         else
2464                                 title = prefs_common_translated_header_name(col_label[type]);
2465                         break;
2466                 default:
2467                         title = gettext(col_label[type]);
2468                 }
2469
2470                 if (type == S_COL_MIME) {
2471                         label = gtk_image_new_from_pixmap(clipxpm, clipxpmmask);
2472                         gtk_widget_show(label);
2473                         gtk_clist_set_column_widget(clist, pos, label);
2474                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Attachment"));
2475                         continue;
2476                 } else if (type == S_COL_MARK) {
2477                         label = gtk_image_new_from_pixmap(markxpm, markxpmmask);
2478                         gtk_widget_show(label);
2479                         gtk_clist_set_column_widget(clist, pos, label);
2480                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Mark"));
2481                         continue;
2482                 } else if (type == S_COL_LOCKED) {
2483                         label = gtk_image_new_from_pixmap(lockedxpm, lockedxpmmask);
2484                         gtk_widget_show(label);
2485                         gtk_clist_set_column_widget(clist, pos, label);
2486                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Locked"));
2487                         continue;
2488                 } else if (type == S_COL_STATUS) {
2489                         gtk_clist_set_column_title(clist, pos, title);
2490                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Status"));
2491                         continue;
2492                 }
2493
2494                 hbox  = gtk_hbox_new(FALSE, 4);
2495                 label = gtk_label_new(title);
2496
2497                 if (justify == GTK_JUSTIFY_RIGHT)
2498                         gtk_box_pack_end(GTK_BOX(hbox), label,
2499                                          FALSE, FALSE, 0);
2500                 else
2501                         gtk_box_pack_start(GTK_BOX(hbox), label,
2502                                            FALSE, FALSE, 0);
2503
2504                 if (summaryview->sort_key == sort_by[type]) {
2505                         arrow = gtk_arrow_new
2506                                 (summaryview->sort_type == SORT_ASCENDING
2507                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2508                                  GTK_SHADOW_IN);
2509                         if (justify == GTK_JUSTIFY_RIGHT)
2510                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2511                                                    FALSE, FALSE, 0);
2512                         else
2513                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2514                                                  FALSE, FALSE, 0);
2515                 }
2516
2517                 gtk_widget_show_all(hbox);
2518                 gtk_clist_set_column_widget(clist, pos, hbox);
2519         }
2520 }
2521
2522 void summary_reflect_prefs(void)
2523 {
2524         static gchar *last_font = NULL;
2525         gboolean update_font = TRUE;
2526         SummaryView *summaryview = NULL;
2527
2528         if (!mainwindow_get_mainwindow())
2529                 return;
2530         summaryview = mainwindow_get_mainwindow()->summaryview;
2531
2532         if (last_font && !strcmp(last_font, NORMAL_FONT))
2533                 update_font = FALSE;
2534
2535         g_free(last_font);
2536         
2537         last_font = g_strdup(NORMAL_FONT);
2538
2539         if (update_font) {      
2540                 bold_style = bold_marked_style = bold_deleted_style = 
2541                         small_style = small_marked_style = small_deleted_style = NULL;
2542                 summary_set_fonts(summaryview);
2543         }
2544
2545         summary_set_column_titles(summaryview);
2546         summary_show(summaryview, summaryview->folder_item);
2547 }
2548
2549 void summary_sort(SummaryView *summaryview,
2550                   FolderSortKey sort_key, FolderSortType sort_type)
2551 {
2552         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2553         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2554         GtkCListCompareFunc cmp_func = NULL;
2555         START_TIMING("");
2556         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2557                                        G_CALLBACK(summary_tree_expanded), summaryview);
2558         summary_freeze(summaryview);
2559
2560         switch (sort_key) {
2561         case SORT_BY_MARK:
2562                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
2563                 break;
2564         case SORT_BY_STATUS:
2565                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_status;
2566                 break;
2567         case SORT_BY_MIME:
2568                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
2569                 break;
2570         case SORT_BY_NUMBER:
2571                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
2572                 break;
2573         case SORT_BY_SIZE:
2574                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
2575                 break;
2576         case SORT_BY_DATE:
2577                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
2578                 break;
2579         case SORT_BY_FROM:
2580                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
2581                 break;
2582         case SORT_BY_SUBJECT:
2583                 if (summaryview->simplify_subject_preg)
2584                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
2585                 else
2586                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
2587                 break;
2588         case SORT_BY_SCORE:
2589                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
2590                 break;
2591         case SORT_BY_LABEL:
2592                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
2593                 break;
2594         case SORT_BY_TO:
2595                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_to;
2596                 break;
2597         case SORT_BY_LOCKED:
2598                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
2599                 break;
2600         case SORT_BY_NONE:
2601                 break;
2602         default:
2603                 goto unlock;
2604         }
2605
2606         summaryview->sort_key = sort_key;
2607         summaryview->sort_type = sort_type;
2608
2609         summary_set_column_titles(summaryview);
2610         summary_set_menu_sensitive(summaryview);
2611
2612         /* allow fallback to don't sort */
2613         if (summaryview->sort_key == SORT_BY_NONE)
2614                 goto unlock;
2615
2616         if (cmp_func != NULL) {
2617                 debug_print("Sorting summary...");
2618                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2619
2620                 main_window_cursor_wait(summaryview->mainwin);
2621
2622                 gtk_clist_set_compare_func(clist, cmp_func);
2623
2624                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
2625                 gtk_sctree_sort_recursive(ctree, NULL);
2626
2627                 gtk_ctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2628
2629                 main_window_cursor_normal(summaryview->mainwin);
2630
2631                 debug_print("done.\n");
2632                 STATUSBAR_POP(summaryview->mainwin);
2633         }
2634 unlock:
2635         summary_thaw(summaryview);
2636         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2637                                        G_CALLBACK(summary_tree_expanded), summaryview);
2638         END_TIMING();
2639 }
2640
2641 static gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
2642                                    GtkCTreeNode *cnode, gpointer data)
2643 {
2644         SummaryView *summaryview = (SummaryView *)data;
2645         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2646         gchar *text[N_SUMMARY_COLS];
2647         gint *col_pos = summaryview->col_pos;
2648         const gchar *msgid = msginfo->msgid;
2649         GHashTable *msgid_table = summaryview->msgid_table;
2650         
2651         summary_set_header(summaryview, text, msginfo);
2652
2653         gtk_sctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2654                                 NULL, NULL, NULL, NULL, FALSE, summaryview->threaded && !summaryview->thread_collapsed);
2655 #define SET_TEXT(col) {                                         \
2656         gtk_ctree_node_set_text(ctree, cnode, col_pos[col],     \
2657                                 text[col_pos[col]]);            \
2658 }
2659
2660         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
2661                 SET_TEXT(S_COL_NUMBER);
2662         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
2663                 SET_TEXT(S_COL_SCORE);
2664         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
2665                 SET_TEXT(S_COL_SIZE);
2666         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible)
2667                 SET_TEXT(S_COL_DATE);
2668         if (summaryview->col_state[summaryview->col_pos[S_COL_FROM]].visible)
2669                 SET_TEXT(S_COL_FROM);
2670         if (summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2671                 SET_TEXT(S_COL_TO);
2672
2673 #undef SET_TEXT
2674
2675         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2676         summary_set_marks_func(ctree, cnode, summaryview);
2677
2678         if (msgid && msgid[0] != '\0')
2679                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2680
2681         return TRUE;
2682 }
2683
2684 static void summary_set_ctree_from_list(SummaryView *summaryview,
2685                                         GSList *mlist)
2686 {
2687         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2688         MsgInfo *msginfo;
2689         GtkCTreeNode *node = NULL;
2690         GHashTable *msgid_table;
2691         GHashTable *subject_table = NULL;
2692         GSList * cur;
2693         START_TIMING("");
2694         
2695         if (!mlist) return;
2696
2697         debug_print("\tSetting summary from message data...\n");
2698         STATUSBAR_PUSH(summaryview->mainwin,
2699                        _("Setting summary from message data..."));
2700         gdk_flush();
2701
2702         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2703                                        G_CALLBACK(summary_tree_expanded), summaryview);
2704
2705         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2706         summaryview->msgid_table = msgid_table;
2707
2708         if (prefs_common.thread_by_subject) {
2709                 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2710                 summaryview->subject_table = subject_table;
2711         } else {
2712                 summaryview->subject_table = NULL;
2713         }
2714
2715         if (prefs_common.use_addr_book)
2716                 start_address_completion(NULL);
2717         
2718         if (summaryview->threaded) {
2719                 GNode *root, *gnode;
2720                 START_TIMING("threaded");
2721                 root = procmsg_get_thread_tree(mlist);
2722
2723                 for (gnode = root->children; gnode != NULL;
2724                      gnode = gnode->next) {
2725                         node = gtk_sctree_insert_gnode
2726                                 (ctree, NULL, node, gnode,
2727                                  summary_insert_gnode_func, summaryview);
2728                 }
2729
2730                 g_node_destroy(root);
2731                 
2732                 END_TIMING();
2733         } else {
2734                 gchar *text[N_SUMMARY_COLS];
2735                 START_TIMING("unthreaded");
2736                 cur = mlist;
2737                 for (; mlist != NULL; mlist = mlist->next) {
2738                         msginfo = (MsgInfo *)mlist->data;
2739
2740                         summary_set_header(summaryview, text, msginfo);
2741
2742                         node = gtk_sctree_insert_node
2743                                 (ctree, NULL, node, text, 2,
2744                                  NULL, NULL, NULL, NULL,
2745                                  FALSE, FALSE);
2746
2747                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2748                         summary_set_marks_func(ctree, node, summaryview);
2749
2750                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2751                                 g_hash_table_insert(msgid_table,
2752                                                     msginfo->msgid, node);
2753
2754                         if (prefs_common.thread_by_subject)
2755                                 subject_table_insert(subject_table,
2756                                              msginfo->subject,
2757                                              node);
2758                 }
2759                 mlist = cur;
2760                 END_TIMING();
2761         }
2762
2763         if (prefs_common.enable_hscrollbar &&
2764             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2765                 gint optimal_width;
2766
2767                 optimal_width = gtk_clist_optimal_column_width
2768                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2769                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2770                                            summaryview->col_pos[S_COL_SUBJECT],
2771                                            optimal_width);
2772         }
2773
2774         if (prefs_common.use_addr_book)
2775                 end_address_completion();
2776
2777         debug_print("done.\n");
2778         STATUSBAR_POP(summaryview->mainwin);
2779         if (debug_get_mode()) {
2780                 debug_print("\tmsgid hash table size = %d\n",
2781                             g_hash_table_size(msgid_table));
2782                 if (prefs_common.thread_by_subject)
2783                         debug_print("\tsubject hash table size = %d\n",
2784                             g_hash_table_size(subject_table));
2785         }
2786
2787         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
2788
2789         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2790
2791         if (prefs_common.bold_unread) {
2792                 START_TIMING("bold_unread");
2793                 while (node) {
2794                         GtkCTreeNode *next = GTK_CTREE_NODE_NEXT(node);
2795                         if (GTK_CTREE_ROW(node)->children)
2796                                 summary_set_row_marks(summaryview, node);
2797                         node = next;
2798                 }
2799                 END_TIMING();
2800         }
2801
2802         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
2803                                        G_CALLBACK(summary_tree_expanded), summaryview);
2804         END_TIMING();
2805 }
2806
2807 static gchar *summary_complete_address(const gchar *addr)
2808 {
2809         gint count;
2810         gchar *res, *tmp, *email_addr;
2811         
2812         if (addr == NULL)
2813                 return NULL;
2814
2815         Xstrdup_a(email_addr, addr, return NULL);
2816         extract_address(email_addr);
2817         if (!*email_addr)
2818                 return NULL;
2819
2820         /*
2821          * completion stuff must be already initialized
2822          */
2823         res = NULL;
2824         if (1 < (count = complete_address(email_addr))) {
2825                 tmp = get_complete_address(1);
2826                 res = procheader_get_fromname(tmp);
2827                 g_free(tmp);
2828         }
2829
2830         return res;
2831 }
2832
2833 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2834                                MsgInfo *msginfo)
2835 {
2836         static gchar date_modified[80];
2837         static gchar col_score[11];
2838         static gchar buf[BUFFSIZE];
2839         gint *col_pos = summaryview->col_pos;
2840         gchar *from_text = NULL, *to_text = NULL;
2841         gboolean should_swap = FALSE;
2842
2843         text[col_pos[S_COL_FROM]]   = "";
2844         text[col_pos[S_COL_TO]]     = "";
2845         text[col_pos[S_COL_SUBJECT]]= "";
2846         text[col_pos[S_COL_MARK]]   = "";
2847         text[col_pos[S_COL_STATUS]] = "";
2848         text[col_pos[S_COL_MIME]]   = "";
2849         text[col_pos[S_COL_LOCKED]] = "";
2850         text[col_pos[S_COL_DATE]]   = "";
2851         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
2852                 text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2853         else
2854                 text[col_pos[S_COL_NUMBER]] = "";
2855
2856         /* slow! */
2857         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
2858                 text[col_pos[S_COL_SIZE]] = to_human_readable(msginfo->size);
2859         else
2860                 text[col_pos[S_COL_SIZE]] = "";
2861
2862         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
2863                 text[col_pos[S_COL_SCORE]] = itos_buf(col_score, msginfo->score);
2864         else
2865                 text[col_pos[S_COL_SCORE]] = "";
2866
2867         /* slow! */
2868         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible) {
2869                 if (msginfo->date_t) {
2870                         procheader_date_get_localtime(date_modified,
2871                                                       sizeof(date_modified),
2872                                                       msginfo->date_t);
2873                         text[col_pos[S_COL_DATE]] = date_modified;
2874                 } else if (msginfo->date)
2875                         text[col_pos[S_COL_DATE]] = msginfo->date;
2876                 else
2877                         text[col_pos[S_COL_DATE]] = _("(No Date)");
2878         }
2879         
2880         if (prefs_common.swap_from && msginfo->from && msginfo->to
2881         &&  !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible) {
2882                 gchar *addr = NULL;
2883                 
2884                 addr = g_strdup(msginfo->from);
2885
2886                 if (addr) {
2887                         extract_address(addr);
2888                         if (account_find_from_address(addr)) {
2889                                 should_swap = TRUE;
2890                         }
2891                         g_free(addr);
2892                 }
2893         }
2894
2895         if (!prefs_common.use_addr_book) {
2896                 from_text = msginfo->fromname ? 
2897                                 msginfo->fromname :
2898                                 _("(No From)");
2899         } else {
2900                 gchar buf[BUFFSIZE];
2901                 gchar *tmp = summary_complete_address(msginfo->from);
2902                 if (tmp) {
2903                         strncpy2(buf, tmp, sizeof(buf));
2904                         g_free(tmp);
2905                         from_text = buf;
2906                 } else {
2907                         from_text = (msginfo->fromname) ?
2908                                         msginfo->fromname: 
2909                                         _("(No From)");
2910                 }
2911         }
2912         
2913         to_text = msginfo->to ? msginfo->to : 
2914                    (msginfo->cc ? msginfo->cc :
2915                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
2916                      )
2917                    );
2918
2919         text[col_pos[S_COL_TO]] = to_text;
2920         if (!should_swap) {
2921                 text[col_pos[S_COL_FROM]] = from_text;
2922         } else {
2923                 gchar tmp[BUFFSIZE];
2924                 snprintf(tmp, BUFFSIZE-1, "--> %s", to_text);
2925                 tmp[BUFFSIZE-1]='\0';
2926                 text[col_pos[S_COL_FROM]] = tmp;
2927         }
2928         
2929         if (summaryview->simplify_subject_preg != NULL)
2930                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2931                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2932                                         summaryview->simplify_subject_preg) : 
2933                         _("(No Subject)");
2934         else 
2935                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2936                         _("(No Subject)");
2937 }
2938
2939 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2940 {
2941         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2942 }
2943
2944 static gboolean defer_change(gpointer data);
2945 typedef struct _ChangeData {
2946         MsgInfo *info;
2947         gint op; /* 0, 1, 2 for unset, set, change */
2948         MsgPermFlags set_flags;
2949         MsgTmpFlags  set_tmp_flags;
2950         MsgPermFlags unset_flags;
2951         MsgTmpFlags  unset_tmp_flags;
2952 } ChangeData;
2953
2954 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2955 {
2956         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2957                 debug_print("flags: doing unset now\n");
2958                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
2959         } else {
2960                 ChangeData *unset_data = g_new0(ChangeData, 1);
2961                 unset_data->info = msginfo;
2962                 unset_data->op = 0;
2963                 unset_data->unset_flags = flags;
2964                 unset_data->unset_tmp_flags = tmp_flags;
2965                 debug_print("flags: deferring unset\n");
2966                 g_timeout_add(100, defer_change, unset_data);
2967         }
2968 }
2969
2970 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
2971 {
2972         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2973                 debug_print("flags: doing set now\n");
2974                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
2975         } else {
2976                 ChangeData *set_data = g_new0(ChangeData, 1);
2977                 set_data->info = msginfo;
2978                 set_data->op = 1;
2979                 set_data->set_flags = flags;
2980                 set_data->set_tmp_flags = tmp_flags;
2981                 debug_print("flags: deferring set\n");
2982                 g_timeout_add(100, defer_change, set_data);
2983         }
2984 }
2985
2986 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
2987                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
2988                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
2989 {
2990         if (!msginfo->folder || !msginfo->folder->processing_pending) {
2991                 debug_print("flags: doing change now\n");
2992                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
2993                         rem_flags, rem_tmp_flags);
2994         } else {
2995                 ChangeData *change_data = g_new0(ChangeData, 1);
2996                 change_data->info = msginfo;
2997                 change_data->op = 2;
2998                 change_data->set_flags = add_flags;
2999                 change_data->set_tmp_flags = add_tmp_flags;
3000                 change_data->unset_flags = rem_flags;
3001                 change_data->unset_tmp_flags = rem_tmp_flags;
3002                 debug_print("flags: deferring change\n");
3003                 g_timeout_add(100, defer_change, change_data);
3004         }
3005 }
3006
3007 gboolean defer_change(gpointer data)
3008 {
3009         ChangeData *chg = (ChangeData *)data;
3010         if (chg->info->folder && chg->info->folder->processing_pending) {
3011                 debug_print("flags: trying later\n");
3012                 return TRUE; /* try again */
3013         } else {
3014                 debug_print("flags: finally doing it\n");
3015                 switch(chg->op) {
3016                 case 0:
3017                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
3018                         break;
3019                 case 1:
3020                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
3021                         break;
3022                 case 2:
3023                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
3024                                 chg->unset_flags, chg->unset_tmp_flags);
3025                         break;
3026                 default:
3027                         g_warning("shouldn't happen\n");
3028                 }
3029                 g_free(chg);
3030         }
3031         return FALSE;
3032 }
3033
3034 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
3035                                       GtkCTreeNode *row)
3036 {
3037         g_return_if_fail(summaryview != NULL);
3038         g_return_if_fail(msginfo != NULL);
3039         g_return_if_fail(row != NULL);
3040
3041         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
3042                 summary_msginfo_unset_flags
3043                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
3044                 summary_set_row_marks(summaryview, row);
3045                 summary_status_show(summaryview);
3046         }
3047 }
3048
3049 typedef struct  {
3050         MsgInfo *msginfo;
3051         SummaryView *summaryview;
3052 } MarkAsReadData;
3053
3054 static int msginfo_mark_as_read_timeout(void *data)
3055 {
3056         MarkAsReadData *mdata = (MarkAsReadData *)data;
3057         if (!mdata)
3058                 return FALSE;
3059         
3060         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
3061                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
3062                                      mdata->summaryview->selected); 
3063
3064         g_free(mdata);
3065
3066         return FALSE;   
3067 }
3068
3069 static void summary_display_msg_full(SummaryView *summaryview,
3070                                      GtkCTreeNode *row,
3071                                      gboolean new_window, gboolean all_headers)
3072 {
3073         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3074         MsgInfo *msginfo;
3075         gint val;
3076         START_TIMING("");
3077         if (!new_window) {
3078                 if (summaryview->displayed == row)
3079                         return;
3080                 else if (summaryview->messageview)
3081                         summaryview->messageview->filtered = FALSE;
3082         }                       
3083         g_return_if_fail(row != NULL);
3084
3085         if (summary_is_locked(summaryview)) return;
3086         summary_lock(summaryview);
3087
3088         STATUSBAR_POP(summaryview->mainwin);
3089         GTK_EVENTS_FLUSH();
3090
3091         msginfo = gtk_ctree_node_get_row_data(ctree, row);
3092         
3093         g_return_if_fail(msginfo);
3094
3095         if (new_window && prefs_common.layout_mode != SMALL_LAYOUT) {
3096                 MessageView *msgview;
3097
3098                 msgview = messageview_create_with_new_window(summaryview->mainwin);
3099                 val = messageview_show(msgview, msginfo, all_headers);
3100         } else {
3101                 MessageView *msgview;
3102
3103                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
3104                         if (summaryview->ext_messageview == NULL)
3105                                 summaryview->ext_messageview = messageview_create_with_new_window(summaryview->mainwin);
3106                         else
3107                                 gtkut_window_popup(summaryview->ext_messageview->window);
3108                         msgview = summaryview->ext_messageview;
3109                         summaryview->last_displayed = summaryview->displayed;
3110                         summaryview->displayed = row;
3111                         val = messageview_show(msgview, msginfo, all_headers);
3112                         if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
3113                                 gtk_widget_grab_focus(summaryview->ctree);
3114                         gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3115                                 GTK_CLIST(summaryview->ctree)->focus_row);
3116                 } else {
3117                         msgview = summaryview->messageview;
3118                         summaryview->last_displayed = summaryview->displayed;
3119                         summaryview->displayed = row;
3120                         if (!messageview_is_visible(msgview)) {
3121                                 main_window_toggle_message_view(summaryview->mainwin);
3122                                 GTK_EVENTS_FLUSH();
3123                         }
3124                         val = messageview_show(msgview, msginfo, all_headers);
3125                         if (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL)
3126                                 gtk_widget_grab_focus(summaryview->ctree);
3127                         gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3128                                 GTK_CLIST(summaryview->ctree)->focus_row);
3129                 }
3130         }
3131
3132         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
3133                 if (prefs_common.mark_as_read_delay) {
3134                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
3135                         data->summaryview = summaryview;
3136                         data->msginfo = msginfo;
3137                         g_timeout_add(prefs_common.mark_as_read_delay * 1000,
3138                                 msginfo_mark_as_read_timeout, data);
3139                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
3140                         msginfo_mark_as_read(summaryview, msginfo, row);
3141                 }
3142         }
3143
3144         summary_set_menu_sensitive(summaryview);
3145         toolbar_main_set_sensitive(summaryview->mainwin);
3146         messageview_set_menu_sensitive(summaryview->messageview);
3147
3148         summary_unlock(summaryview);
3149         END_TIMING();
3150 }
3151
3152 void summary_display_msg_selected(SummaryView *summaryview,
3153                                   gboolean all_headers)
3154 {
3155         if (summary_is_locked(summaryview)) return;
3156         summaryview->displayed = NULL;
3157         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
3158                                  all_headers);
3159 }
3160
3161 void summary_redisplay_msg(SummaryView *summaryview)
3162 {
3163         GtkCTreeNode *node;
3164
3165         if (summaryview->displayed) {
3166                 node = summaryview->displayed;
3167                 summaryview->displayed = NULL;
3168                 summary_display_msg(summaryview, node);
3169         }
3170 }
3171
3172 void summary_open_msg(SummaryView *summaryview)
3173 {
3174         if (!summaryview->selected) return;
3175