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