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