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