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