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